fixed bug in jpeg2000 decoding
[swftools.git] / src / gfx2gfx.c
1 /* pdf2swf.c
2    main routine for pdf2swf(1)
3
4    Part of the swftools package.
5    
6    Copyright (c) 2001,2002,2003 Matthias Kramm <kramm@quiss.org> 
7  
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.
12
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.
17
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 */
21
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/ocr.h"
43 #include "../../swftools/lib/devices/rescale.h"
44 #include "../../swftools/lib/devices/record.h"
45 #include "../../swftools/lib/readers/image.h"
46 #include "../../swftools/lib/readers/swf.h"
47 #include "../../swftools/lib/pdf/pdf.h"
48 #include "../../swftools/lib/log.h"
49
50 static gfxsource_t*driver = 0;
51
52 static char * outputname = 0;
53 static int loglevel = 3;
54 static char * pagerange = 0;
55 static char * filename = 0;
56 static const char * format = 0;
57
58 int args_callback_option(char*name,char*val) {
59     if (!strcmp(name, "o"))
60     {
61         outputname = val;
62         return 1;
63     }
64     else if (!strcmp(name, "v"))
65     {
66         loglevel ++;
67         setConsoleLogging(loglevel);
68         return 0;
69     }
70     else if (!strcmp(name, "f"))
71     {
72         format = val;
73         return 1;
74     }
75     else if (!strcmp(name, "q"))
76     {
77         loglevel --;
78         setConsoleLogging(loglevel);
79         return 0;
80     }
81     else if (name[0]=='p')
82     {
83         do {
84             name++;
85         } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
86
87         if(*name) {
88             pagerange = name;
89             return 0;
90         } 
91         pagerange = val;        
92         return 1;
93     }
94     else if (!strcmp(name, "s"))
95     {
96         if(!driver) {
97             fprintf(stderr, "Specify input file before -s\n");
98             exit(1);
99         }
100         char*s = strdup(val);
101         char*c = strchr(s, '=');
102         if(c && *c && c[1])  {
103             *c = 0;
104             c++;
105             driver->setparameter(driver, s,c);
106         } else {
107             driver->setparameter(driver, s,"1");
108         }
109         free(s);
110         return 1;
111     }
112     else if (!strcmp(name, "V"))
113     {   
114         printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
115         exit(0);
116     }
117     else 
118     {
119         fprintf(stderr, "Unknown option: -%s\n", name);
120         exit(1);
121     }
122     return 0;
123 }
124
125 struct options_t options[] =
126 {{"o","output"},
127  {"q","quiet"},
128  {"V","version"},
129  {"s","set"},
130  {"p","pages"},
131  {0,0}
132 };
133
134 int args_callback_longoption(char*name,char*val) {
135     return args_long2shortoption(options, name, val);
136 }
137
138 int args_callback_command(char*name, char*val) {
139     if (!filename) {
140
141         filename = name;
142
143         if(strstr(filename, ".pdf") || strstr(filename, ".PDF")) {
144             msg("<notice> Treating file as PDF");
145             driver = gfxsource_pdf_create();
146         } else if(strstr(filename, ".swf") || strstr(filename, ".SWF")) {
147             msg("<notice> Treating file as SWF");
148             driver = gfxsource_swf_create();
149         } else if(strstr(filename, ".jpg") || strstr(filename, ".JPG") ||
150                   strstr(filename, ".png") || strstr(filename, ".PNG")) {
151             msg("<notice> Treating file as Image");
152             driver = gfxsource_image_create();
153         }
154     } else {
155         if(outputname)
156         {
157              fprintf(stderr, "Error: Do you want the output to go to %s or to %s?", 
158                      outputname, name);
159              exit(1);
160         }
161         outputname = name;
162     }
163     return 0;
164 }
165
166 void args_callback_usage(char*name)
167 {
168 }
169
170 int main(int argn, char *argv[])
171 {
172     processargs(argn, argv);
173     initLog(0,-1,0,0,-1,loglevel);
174     
175     if(!filename) {
176         fprintf(stderr, "Please specify an input file\n");
177         exit(1);
178     }
179     
180     if(!outputname)
181     {
182         if(filename) {
183             outputname = stripFilename(filename, ".out");
184             msg("<notice> Output filename not given. Writing to %s", outputname);
185         } 
186     }
187     if(!outputname)
188     {
189         fprintf(stderr, "Please use -o to specify an output file\n");
190         exit(1);
191     }
192     is_in_range(0x7fffffff, pagerange);
193     if(pagerange)
194         driver->setparameter(driver, "pages", pagerange);
195
196     if(!filename) {
197         args_callback_usage(argv[0]);
198         exit(0);
199     }
200
201     gfxdocument_t* doc = driver->open(driver, filename);
202     if(!doc) {
203         msg("<error> Couldn't open %s", filename);
204         exit(1);
205     }
206
207     if(!format) {
208         char*x = strrchr(outputname, '.');
209         if(x) 
210             format = x+1;
211     }
212
213
214     gfxresult_t*result = 0;
215 #ifdef HAVE_LRF
216     if(!strcasecmp(format, "lrf")) {
217         gfxdevice_t lrf;
218         gfxdevice_lrf_init(&lrf);
219
220         gfxdevice_t rescale;
221         gfxdevice_rescale_init(&rescale, &lrf, 592, 732, 0);
222
223         gfxdevice_t*out = &rescale;
224         out->setparameter(out, "keepratio", "1");
225         out->setparameter(out, "pagepattern", outputname);
226
227         gfxdevice_t bbox2,*bbox=&bbox2;
228         gfxdevice_bbox_init(bbox);
229         bbox->setparameter(bbox, "graphics", "0");
230
231         int pagenr;
232
233         for(pagenr = 1; pagenr <= doc->num_pages; pagenr++) 
234         {
235             if(is_in_range(pagenr, pagerange)) {
236                 gfxpage_t* page = doc->getpage(doc, pagenr);
237                 bbox->startpage(bbox,-1,-1);
238                 page->render(page, bbox);
239                 gfxbbox_t b = gfxdevice_bbox_getbbox(bbox);
240
241                 out->startpage(out, b.xmax-b.xmin, b.ymax-b.ymin);
242                 page->rendersection(page, out, -b.xmin, -b.ymin, 0,0,b.xmax-b.xmin,b.ymax-b.ymin);
243                 out->endpage(out);
244
245                 page->destroy(page);
246             }
247         }
248         result = out->finish(out);
249     } else 
250 #endif
251     {
252         gfxdevice_t _out,*out=&_out;
253         if(!strcasecmp(format, "ocr")) {
254             gfxdevice_ocr_init(out);
255         } else if(!strcasecmp(format, "swf")) {
256             gfxdevice_swf_init(out);
257         } else if(!strcasecmp(format, "img") || !strcasecmp(format, "png")) {
258             gfxdevice_render_init(out);
259             out->setparameter(out, "antialize", "4");
260         } else if(!strcasecmp(format, "txt")) {
261             gfxdevice_text_init(out);
262         } else if(!strcasecmp(format, "log")) {
263             gfxdevice_file_init(out, "/tmp/device.log");
264         } else if(!strcasecmp(format, "pdf")) {
265             gfxdevice_pdf_init(out);
266         } else {
267             msg("<error> Invalid output format: %s", format);
268             exit(1);
269         }
270
271         int pagenr;
272         for(pagenr = 1; pagenr <= doc->num_pages; pagenr++) 
273         {
274             if(is_in_range(pagenr, pagerange)) {
275                 gfxpage_t* page = doc->getpage(doc, pagenr);
276                 out->startpage(out, page->width, page->height);
277                 page->render(page, out);
278                 out->endpage(out);
279                 page->destroy(page);
280             }
281         }
282         result = out->finish(out);
283     }
284
285     if(result) {
286         if(result->save(result, outputname) < 0) {
287             exit(1);
288         }
289         result->destroy(result);
290     }
291
292     doc->destroy(doc);
293
294     driver->destroy(driver);
295     return 0;
296 }
297