for ng, use 4x4 antializing
[swftools.git] / src / swfrender.c
1 #include "../config.h"
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <assert.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include "../lib/rfxswf.h"
8 #include "../lib/png.h"
9 #include "../lib/args.h"
10 #include "../lib/gfxsource.h"
11 #include "../lib/readers/swf.h"
12 #include "../lib/devices/render.h"
13 #include "../lib/devices/rescale.h"
14
15 static struct options_t options[] = {
16 {"h", "help"},
17 {"o", "output"},
18 {"l", "legacy"},
19 {"V", "version"},
20 {"X", "width"},
21 {"Y", "height"},
22 {0,0}
23 };
24
25 static int ng = 1;
26 static char*filename = 0;
27 static char*outputname = "output.png";
28 static int quantize = 0;
29
30 static int width = 0;
31 static int height = 0;
32
33 int args_callback_option(char*name,char*val)
34 {
35     if(!strcmp(name, "V")) {
36         printf("swfrender - part of %s %s\n", PACKAGE, VERSION);
37         exit(0);
38     } else if(!strcmp(name, "o")) {
39         outputname = strdup(val);
40         return 1;
41     } else if(!strcmp(name, "l")) {
42         ng = 0;
43         return 0;
44     } else if(!strcmp(name, "q")) {
45         quantize = 1;
46         return 0;
47     } else if(!strcmp(name, "X")) {
48         width = atoi(val);
49         return 1;
50     } else if(!strcmp(name, "Y")) {
51         height = atoi(val);
52         return 1;
53     } else {
54         printf("Unknown option: -%s\n", name);
55         exit(1);
56     }
57
58     return 0;
59 }
60 int args_callback_longoption(char*name,char*val)
61 {
62     return args_long2shortoption(options, name, val);
63 }
64 void args_callback_usage(char *name)
65 {
66     printf("\n");
67     printf("Usage: %s file.swf [-o output.png]\n", name);
68     printf("\n");
69     printf("-h , --help                    Print short help message and exit\n");
70     printf("-l , --legacy                  Use old rendering framework\n");
71     printf("-o , --output                  Output file (default: output.png)\n");
72     printf("\n");
73 }
74 int args_callback_command(char*name,char*val)
75 {
76     if(filename) {
77         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
78                  filename, name);
79     }
80     filename = name;
81     return 0;
82 }
83
84
85
86 int main(int argn, char*argv[])
87 {
88     SWF swf;
89     int fi;
90
91     processargs(argn, argv);
92
93     if(!filename) {
94         fprintf(stderr, "You must supply a filename.\n");
95         return 1;
96     }
97
98     if(!ng) {
99         fi = open(filename, O_RDONLY|O_BINARY);
100         if (fi<=0) { 
101             fprintf(stderr,"Couldn't open %s\n", filename);
102             perror(argv[1]);
103             exit(1);
104         }
105         if(swf_ReadSWF(fi,&swf)<0) { 
106             fprintf(stderr,"%s is not a valid SWF file or contains errors.\n",argv[1]);
107             close(fi);
108         }
109         RENDERBUF buf;
110         swf_Render_Init(&buf, 0,0, (swf.movieSize.xmax - swf.movieSize.xmin) / 20,
111                                    (swf.movieSize.ymax - swf.movieSize.ymin) / 20, 2, 1);
112         swf_RenderSWF(&buf, &swf);
113         RGBA* img = swf_Render(&buf);
114         if(quantize)
115             writePalettePNG(outputname, (unsigned char*)img, buf.width, buf.height);
116         else
117             writePNG(outputname, (unsigned char*)img, buf.width, buf.height);
118         swf_Render_Delete(&buf);
119     } else {
120         gfxsource_t*src = gfxsource_swf_create();
121         gfxdocument_t*doc = src->open(src, filename);
122         if(!doc) {
123             fprintf(stderr,"Couldn't open %s\n", filename);
124             exit(1);
125         }
126         gfxdevice_t dev2,*dev=&dev2;
127         gfxdevice_render_init(dev);
128         dev->setparameter(dev, "antialise", "4");
129         if(quantize) {
130             dev->setparameter(dev, "palette", "1");
131         }
132         if(width || height) {
133             dev = gfxdevice_rescale_new(dev, width, height, 0);
134         }
135
136         int t;
137         for(t=1;t<=doc->num_pages;t++) {
138             gfxpage_t* page = doc->getpage(doc, t);
139             dev->startpage(dev, page->width, page->height);
140             page->render(page, dev);
141             dev->endpage(dev);
142             page->destroy(page);
143             break;
144         }
145         gfxresult_t*result = dev->finish(dev);
146         if(result) {
147             if(result->save(result, outputname) < 0) {
148                 exit(1);
149             }
150             result->destroy(result);
151         }
152         doc->destroy(doc);
153     }
154     return 0;
155 }
156