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