fix crashes reported by Debian Cylab Mayhem Team
[swftools.git] / src / gfx2gfx.c
blobfcace342504d785e818a3abe57609bf91d77a2b1
1 /* pdf2swf.c
2 main routine for pdf2swf(1)
4 Part of the swftools package.
6 Copyright (c) 2001,2002,2003 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include "../../swftools/config.h"
28 #include "../../swftools/lib/args.h"
29 #include "../../swftools/lib/os.h"
30 #include "../../swftools/lib/gfxsource.h"
31 #include "../../swftools/lib/gfxdevice.h"
32 #include "../../swftools/lib/gfxpoly.h"
33 #include "../../swftools/lib/devices/pdf.h"
34 #include "../../swftools/lib/devices/swf.h"
35 #include "../../swftools/lib/devices/text.h"
36 #include "../../swftools/lib/devices/render.h"
37 #include "../../swftools/lib/devices/file.h"
38 #include "../../swftools/lib/devices/bbox.h"
39 #ifdef HAVE_LRF
40 #include "../../swftools/lib/devices/lrf.h"
41 #endif
42 #include "../../swftools/lib/devices/rescale.h"
43 #include "../../swftools/lib/devices/record.h"
44 #include "../../swftools/lib/readers/image.h"
45 #include "../../swftools/lib/readers/swf.h"
46 #include "../../swftools/lib/pdf/pdf.h"
47 #include "../../swftools/lib/log.h"
49 static gfxsource_t*driver = 0;
51 static char * outputname = 0;
52 static int loglevel = 3;
53 static char * pagerange = 0;
54 static char * filename = 0;
55 static const char * format = 0;
57 int args_callback_option(char*name,char*val) {
58 if (!strcmp(name, "o"))
60 outputname = val;
61 return 1;
63 else if (!strcmp(name, "v"))
65 loglevel ++;
66 setConsoleLogging(loglevel);
67 return 0;
69 else if (!strcmp(name, "f"))
71 format = val;
72 return 1;
74 else if (!strcmp(name, "q"))
76 loglevel --;
77 setConsoleLogging(loglevel);
78 return 0;
80 else if (name[0]=='p')
82 do {
83 name++;
84 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
86 if(*name) {
87 pagerange = name;
88 return 0;
90 pagerange = val;
91 return 1;
93 else if (!strcmp(name, "s"))
95 if(!driver) {
96 fprintf(stderr, "Specify input file before -s\n");
97 exit(1);
99 char*s = strdup(val);
100 char*c = strchr(s, '=');
101 if(c && *c && c[1]) {
102 *c = 0;
103 c++;
104 driver->setparameter(driver, s,c);
105 } else {
106 driver->setparameter(driver, s,"1");
108 free(s);
109 return 1;
111 else if (!strcmp(name, "V"))
113 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
114 exit(0);
116 else
118 fprintf(stderr, "Unknown option: -%s\n", name);
119 exit(1);
121 return 0;
124 struct options_t options[] =
125 {{"o","output"},
126 {"q","quiet"},
127 {"V","version"},
128 {"s","set"},
129 {"p","pages"},
130 {0,0}
133 int args_callback_longoption(char*name,char*val) {
134 return args_long2shortoption(options, name, val);
137 int args_callback_command(char*name, char*val) {
138 if (!filename) {
140 filename = name;
142 if(strstr(filename, ".pdf") || strstr(filename, ".PDF")) {
143 msg("<notice> Treating file as PDF");
144 driver = gfxsource_pdf_create();
145 } else if(strstr(filename, ".swf") || strstr(filename, ".SWF")) {
146 msg("<notice> Treating file as SWF");
147 driver = gfxsource_swf_create();
148 } else if(strstr(filename, ".jpg") || strstr(filename, ".JPG") ||
149 strstr(filename, ".png") || strstr(filename, ".PNG")) {
150 msg("<notice> Treating file as Image");
151 driver = gfxsource_image_create();
153 } else {
154 if(outputname)
156 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
157 outputname, name);
158 exit(1);
160 outputname = name;
162 return 0;
165 void args_callback_usage(char*name)
169 int main(int argn, char *argv[])
171 processargs(argn, argv);
172 initLog(0,-1,0,0,-1,loglevel);
174 if(!filename) {
175 fprintf(stderr, "Please specify an input file\n");
176 exit(1);
179 if(!outputname)
181 if(filename) {
182 outputname = stripFilename(filename, ".out");
183 msg("<notice> Output filename not given. Writing to %s", outputname);
186 if(!outputname)
188 fprintf(stderr, "Please use -o to specify an output file\n");
189 exit(1);
191 is_in_range(0x7fffffff, pagerange);
192 if(pagerange)
193 driver->setparameter(driver, "pages", pagerange);
195 if(!filename) {
196 args_callback_usage(argv[0]);
197 exit(0);
200 gfxdocument_t* doc = driver->open(driver, filename);
201 if(!doc) {
202 msg("<error> Couldn't open %s", filename);
203 exit(1);
206 if(!format) {
207 char*x = strrchr(outputname, '.');
208 if(x)
209 format = x+1;
213 gfxresult_t*result = 0;
214 #ifdef HAVE_LRF
215 if(!strcasecmp(format, "lrf")) {
216 gfxdevice_t lrf;
217 gfxdevice_lrf_init(&lrf);
219 gfxdevice_t rescale;
220 gfxdevice_rescale_init(&rescale, &lrf, 592, 732, 0);
222 gfxdevice_t*out = &rescale;
223 out->setparameter(out, "keepratio", "1");
224 out->setparameter(out, "pagepattern", outputname);
226 gfxdevice_t bbox2,*bbox=&bbox2;
227 gfxdevice_bbox_init(bbox);
228 bbox->setparameter(bbox, "graphics", "0");
230 int pagenr;
232 for(pagenr = 1; pagenr <= doc->num_pages; pagenr++)
234 if(is_in_range(pagenr, pagerange)) {
235 gfxpage_t* page = doc->getpage(doc, pagenr);
236 bbox->startpage(bbox,-1,-1);
237 page->render(page, bbox);
238 gfxbbox_t b = gfxdevice_bbox_getbbox(bbox);
240 out->startpage(out, b.xmax-b.xmin, b.ymax-b.ymin);
241 page->rendersection(page, out, -b.xmin, -b.ymin, 0,0,b.xmax-b.xmin,b.ymax-b.ymin);
242 out->endpage(out);
244 page->destroy(page);
247 result = out->finish(out);
248 } else
249 #endif
251 gfxdevice_t _out,*out=&_out;
252 if(!strcasecmp(format, "swf")) {
253 gfxdevice_swf_init(out);
254 } else if(!strcasecmp(format, "img") || !strcasecmp(format, "png")) {
255 gfxdevice_render_init(out);
256 out->setparameter(out, "antialize", "4");
257 } else if(!strcasecmp(format, "txt")) {
258 gfxdevice_text_init(out);
259 } else if(!strcasecmp(format, "log")) {
260 gfxdevice_file_init(out, "/tmp/device.log");
261 } else if(!strcasecmp(format, "pdf")) {
262 gfxdevice_pdf_init(out);
263 } else {
264 msg("<error> Invalid output format: %s", format);
265 exit(1);
268 int pagenr;
269 for(pagenr = 1; pagenr <= doc->num_pages; pagenr++)
271 if(is_in_range(pagenr, pagerange)) {
272 gfxpage_t* page = doc->getpage(doc, pagenr);
273 out->startpage(out, page->width, page->height);
274 page->render(page, out);
275 out->endpage(out);
276 page->destroy(page);
279 result = out->finish(out);
282 if(result) {
283 if(result->save(result, outputname) < 0) {
284 exit(1);
286 result->destroy(result);
289 doc->destroy(doc);
291 driver->destroy(driver);
292 return 0;