added correct implementation
[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
11 static struct options_t options[] = {
12 {"h", "help"},
13 {"o", "output"},
14 {"V", "version"},
15 {0,0}
16 };
17
18 static char*filename = 0;
19 static char*outputname = "output.png";
20
21 int args_callback_option(char*name,char*val)
22 {
23     if(!strcmp(name, "V")) {
24         printf("swfrender - part of %s %s\n", PACKAGE, VERSION);
25         exit(0);
26     } else if(!strcmp(name, "o")) {
27         outputname = strdup(val);
28         return 1;
29     } else {
30         printf("Unknown option: -%s\n", name);
31         exit(1);
32     }
33
34     return 0;
35 }
36 int args_callback_longoption(char*name,char*val)
37 {
38     return args_long2shortoption(options, name, val);
39 }
40 void args_callback_usage(char *name)
41 {
42     printf("\n");
43     printf("Usage: %s file.swf [-o output.png]\n", name);
44     printf("\n");
45     printf("-h , --help                    Print short help message and exit\n");
46     printf("-o , --output                  Output file (default: output.png)\n");
47     printf("\n");
48 }
49 int args_callback_command(char*name,char*val)
50 {
51     if(filename) {
52         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
53                  filename, name);
54     }
55     filename = name;
56     return 0;
57 }
58
59
60
61 int main(int argn, char*argv[])
62 {
63     SWF swf;
64     int fi;
65
66     processargs(argn, argv);
67
68     if(!filename) {
69         fprintf(stderr, "You must supply a filename.\n");
70         return 1;
71     }
72
73     fi = open(filename, O_RDONLY|O_BINARY);
74     if (fi<=0) { 
75         fprintf(stderr,"Couldn't open %s\n", filename);
76         perror(argv[1]);
77         exit(1);
78     }
79
80     if(swf_ReadSWF(fi,&swf)<0) { 
81         fprintf(stderr,"%s is not a valid SWF file or contains errors.\n",argv[1]);
82         close(fi);
83     }
84
85     RENDERBUF buf;
86     swf_Render_Init(&buf, 0,0, (swf.movieSize.xmax - swf.movieSize.xmin) / 20,
87                                (swf.movieSize.ymax - swf.movieSize.ymin) / 20, 2, 1);
88
89     swf_RenderSWF(&buf, &swf);
90
91     RGBA* img = swf_Render(&buf);
92
93     writePNG(outputname, (unsigned char*)img, buf.width, buf.height);
94
95     swf_Render_Delete(&buf);
96     return 0;
97 }
98