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