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