added swfbbox.
[swftools.git] / src / swfbbox.c
1 /* swfbbox.c
2    Tool for playing around with SWF bounding boxes.
3
4    Part of the swftools package.
5    
6    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
7
8    This file is distributed under the GPL, see file COPYING for details */
9
10 #include "../config.h"
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include <assert.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include "../lib/rfxswf.h"
17 #include "../lib/args.h"
18
19 static char * filename = 0;
20 static char * outfilename = "output.swf";
21 static int optimize = 0;
22 static int swifty = 0;
23 static int verbose = 0;
24
25 struct options_t options[] =
26 {
27  {"V","version"},
28  {"O","optimize"},
29  {"o","output"},
30  {"S","swifty"},
31  {"v","verbose"},
32  {0,0}
33 };
34
35 int args_callback_option(char*name,char*val)
36 {
37     if(!strcmp(name, "V")) {
38         printf("swfbbox - part of %s %s\n", PACKAGE, VERSION);
39         exit(0);
40     } 
41     else if(!strcmp(name, "O")) {
42         optimize = 1;
43         return 0;
44     } 
45     else if(!strcmp(name, "S")) {
46         swifty = 1;
47         return 0;
48     } 
49     else if(!strcmp(name, "v")) {
50         verbose = 1;
51         return 0;
52     } 
53     else if(!strcmp(name, "o")) {
54         outfilename = val;
55         return 1;
56     } 
57     else {
58         printf("Unknown option: -%s\n", name);
59         exit(1);
60     }
61
62     return 0;
63 }
64 int args_callback_longoption(char*name,char*val)
65 {
66     return args_long2shortoption(options, name, val);
67 }
68 void args_callback_usage(char*name)
69 {    
70     printf("Usage: %s [-at] file.swf\n", name);
71     printf("\t-h , --help\t\t Print help and exit\n");
72     printf("\t-O , --optimize\t\t Print help and exit\n");
73     printf("\t-S , --swifty\t\t Print out transformed bounding boxes\n");
74     printf("\t-o , --output\t\t Set output filename (for -O)\n");
75     printf("\t-V , --version\t\t Print program version and exit\n");
76 }
77 int args_callback_command(char*name,char*val)
78 {
79     if(filename) {
80         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
81                  filename, name);
82     }
83     filename = name;
84     return 0;
85 }
86
87 #define swf_ResetReadBits(tag)   if (tag->readBit)  { tag->pos++; tag->readBit = 0; }
88
89 void parseFillStyleArray(TAG*tag, SHAPE2*shape)
90 {
91     U16 count;
92     int t;
93     int num=0;
94     if(tag->id == ST_DEFINESHAPE)
95         num = 1;
96     else if(tag->id == ST_DEFINESHAPE2)
97         num = 2;
98     else if(tag->id == ST_DEFINESHAPE3)
99         num = 3;
100
101     count = swf_GetU8(tag);
102     if(count == 0xff && num>1) // defineshape2,3 only
103         count = swf_GetU16(tag);
104
105     if(verbose) printf("num: %d\n", count);
106     shape->numfillstyles = count;
107     shape->fillstyles = malloc(sizeof(FILLSTYLE)*count);
108
109     for(t=0;t<count;t++)
110     {
111         int type;
112         U8*pos;
113         FILLSTYLE*dest = &shape->fillstyles[t];
114         swf_ResetReadBits(tag);
115         type = swf_GetU8(tag); //type
116         shape->fillstyles[t].type = type;
117         if(type == 0) {
118             /* plain color */
119             if(num == 3)
120                 swf_GetRGBA(tag, &dest->color);
121             else 
122                 swf_GetRGB(tag, &dest->color);
123         }
124         else if(type == 0x10 || type == 0x12)
125         {
126             /* linear/radial gradient fill */
127             swf_ResetReadBits(tag);
128             swf_GetMatrix(tag, &dest->m);
129             swf_ResetReadBits(tag);
130             dest->gradient = malloc(sizeof(GRADIENT)); // TODO: free this again
131             swf_GetGradient(tag, dest->gradient, num>=3?1:0);
132         }
133         else if(type == 0x40 || type == 0x41)
134         {
135             /* bitmap fill */
136             swf_ResetReadBits(tag);
137             dest->id_bitmap = swf_GetU16(tag); //id
138             swf_ResetReadBits(tag); //?
139             swf_GetMatrix(tag, &dest->m);
140         }
141         else {
142             fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
143         }
144     }
145     swf_ResetReadBits(tag);
146     count = swf_GetU8(tag); // line style array
147     if(count == 0xff)
148         count = swf_GetU16(tag);
149
150     if(verbose) printf("lnum: %d\n", count);
151
152     shape->numlinestyles = count;
153     shape->linestyles = malloc(sizeof(LINESTYLE)*count);
154     /* TODO: should we start with 1 and insert a correct definition of the
155        "built in" linestyle 0? */
156     for(t=0;t<count;t++) 
157     {
158         shape->linestyles[t].width = swf_GetU16(tag);
159         if(num == 3)
160             swf_GetRGBA(tag, &shape->linestyles[t].color);
161         else
162             swf_GetRGB(tag, &shape->linestyles[t].color);
163     }
164     return;
165 }
166
167 void swf_ParseDefineShape(TAG*tag, SHAPE2*shape)
168 {
169     int num = 0, id;
170     U16 fill,line;
171     SRECT r;
172     SRECT r2;
173     SHAPELINE*l;
174     if(tag->id == ST_DEFINESHAPE)
175         num = 1;
176     else if(tag->id == ST_DEFINESHAPE2)
177         num = 2;
178     else if(tag->id == ST_DEFINESHAPE3)
179         num = 3;
180     else {
181         fprintf(stderr, "parseDefineShape must be called with a shape tag");
182     }
183
184     id = swf_GetU16(tag); //id
185     memset(shape, 0, sizeof(SHAPE2));
186     shape->bbox = malloc(sizeof(SRECT));
187     swf_GetRect(tag, &r);
188
189     memcpy(shape->bbox, &r, sizeof(SRECT));
190     parseFillStyleArray(tag, shape);
191
192     swf_ResetReadBits(tag); 
193     fill = (U16)swf_GetBits(tag,4);
194     line = (U16)swf_GetBits(tag,4);
195
196     shape->lines = swf_ParseShapeData(&tag->data[tag->pos], (tag->len - tag->pos)*8, fill, line);
197
198     l = shape->lines;
199 }
200
201 void swf_Shape2Optimize(SHAPE2*shape)
202 {
203     if(!shape->bbox)
204         shape->bbox = malloc(sizeof(SRECT));
205     *(shape->bbox) = swf_GetShapeBoundingBox(shape);
206 }
207
208 void swf_Shape2ToShape(SHAPE2*shape2, SHAPE*shape)
209 {
210     TAG*tag = swf_InsertTag(0,0);
211     SHAPELINE*l,*next;
212     int newx=0,newy=0,lastx=0,lasty=0,oldls=0,oldfs0=0,oldfs1=0;
213
214     memset(shape, 0, sizeof(SHAPE));
215
216     shape->linestyle.n = shape2->numlinestyles;
217     shape->linestyle.data = (LINESTYLE*)malloc(sizeof(LINESTYLE)*shape->linestyle.n);
218     memcpy(shape->linestyle.data, shape2->linestyles, sizeof(LINESTYLE)*shape->linestyle.n);
219     
220     shape->fillstyle.n =  shape2->numfillstyles;
221     shape->fillstyle.data = (FILLSTYLE*)malloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
222     memcpy(shape->fillstyle.data, shape2->fillstyles, sizeof(FILLSTYLE)*shape->fillstyle.n);
223
224     swf_ShapeCountBits(shape,NULL,NULL);
225
226     l = shape2->lines;
227
228     while(l) {
229         int ls=0,fs0=0,fs1=0;
230
231         if(l->type != moveTo) {
232             if(oldls != l->linestyle) {oldls = ls = l->linestyle;if(!ls) ls=0x8000;}
233             if(oldfs0 != l->fillstyle0) {oldfs0 = fs0 = l->fillstyle0;if(!fs0) fs0=0x8000;}
234             if(oldfs1 != l->fillstyle1) {oldfs1 = fs1 = l->fillstyle1;if(!fs1) fs1=0x8000;}
235
236             if(ls || fs0 || fs1 || newx!=0x7fffffff || newy!=0x7fffffff) {
237                 swf_ShapeSetAll(tag,shape,newx,newy,ls,fs0,fs1);
238                 newx = 0x7fffffff;
239                 newy = 0x7fffffff;
240             }
241         }
242
243         if(l->type == lineTo) {
244             swf_ShapeSetLine(tag,shape,l->x-lastx,l->y-lasty);
245         } else if(l->type == splineTo) {
246             swf_ShapeSetCurve(tag,shape, l->sx-lastx,l->sy-lasty, l->x-l->sx,l->y-l->sy);
247         }
248         if(l->type == moveTo) {
249             newx = l->x;
250             newy = l->y;
251         }
252
253         lastx = l->x;
254         lasty = l->y;
255         l = l->next;
256     }
257     swf_ShapeSetEnd(tag);
258     shape->data = tag->data;
259     shape->bitlen = tag->len*8;
260 }
261
262 void swf_SetShape2(TAG*tag, SHAPE2*shape2)
263 {
264     SHAPE shape;
265     swf_Shape2ToShape(shape2, &shape);
266
267     swf_SetRect(tag,shape2->bbox);
268     swf_SetShapeStyles(tag, &shape);
269     swf_ShapeCountBits(&shape,NULL,NULL);
270     swf_SetShapeBits(tag,&shape);
271
272     swf_SetBlock(tag, shape.data, (shape.bitlen+7)/8);
273 }
274
275 /*
276    {char {x1 y1 x2 y2 x3 y3 x4 y4]]
277 */
278
279 SRECT bboxes[65536];
280 U16 depth2id[65536];
281 char*depth2name[65536];
282
283 int hasid(TAG*tag)
284 {
285     if(tag->id == ST_PLACEOBJECT)
286         return 1;
287     if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 2))
288         return 1;
289     return 0;
290 }
291
292 int hasname(TAG*tag)
293 {
294     if(tag->id == ST_PLACEOBJECT)
295         return 0;
296     if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 0x20))
297         return 1;
298     return 0;
299 }
300
301 char* getname(TAG*tag)
302 {
303     if(tag->id == ST_PLACEOBJECT)
304         return 0;
305     if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 0x20)) {
306         SWFPLACEOBJECT o;
307         tag->pos = 0;tag->readBit = 0;
308         swf_GetPlaceObject(tag, &o);
309         return o.name;
310     }
311     return 0;
312 }
313
314 MATRIX getmatrix(TAG*tag)
315 {
316     SWFPLACEOBJECT o;
317     tag->pos = 0;tag->readBit = 0;
318     swf_GetPlaceObject(tag, &o);
319     return o.matrix;
320 }
321
322 int main (int argc,char ** argv)
323
324     TAG*tag;
325     SWF swf;
326     int fi;
327     int frame=0;
328     memset(bboxes, 0, sizeof(bboxes));
329     memset(depth2name, 0, sizeof(depth2name));
330
331     processargs(argc, argv);
332
333     if(!filename) {
334         fprintf(stderr, "You must supply a filename.\n");
335         return 1;
336     }
337
338     fi = open(filename,O_RDONLY|O_BINARY);
339
340     if (fi<0)
341     { 
342         perror("Couldn't open file: ");
343         exit(1);
344     }
345     if FAILED(swf_ReadSWF(fi,&swf))
346     { 
347         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
348         close(fi);
349         exit(1);
350     }
351     close(fi);
352
353     swf_FoldAll(&swf);
354     tag = swf.firstTag;
355
356     if(swifty)
357         printf("{frame %d}\n", frame++);
358
359     while (tag) {
360         if (tag->id == ST_DEFINESHAPE ||
361             tag->id == ST_DEFINESHAPE2 ||
362             tag->id == ST_DEFINESHAPE3) {
363             SHAPE2 s;
364             if(verbose) printf("%s\n", swf_TagGetName(tag));
365             swf_ParseDefineShape(tag, &s);
366             swf_Shape2Optimize(&s);
367             tag->len = 2;
368             swf_SetShape2(tag, &s);
369         }
370         if(swf_isDefiningTag(tag) && tag->id != ST_DEFINESPRITE) {
371             bboxes[swf_GetDefineID(tag)] = swf_GetDefineBBox(tag);
372         }
373         if(swifty) {
374             if (tag->id == ST_SHOWFRAME) {
375                 printf("{frame %d}\n", frame++);
376             }
377             if (tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
378                 if(hasid(tag)) {
379                     depth2id[swf_GetDepth(tag)] = swf_GetPlaceID(tag);
380                 }
381                 if(hasname(tag)) {
382                     depth2name[swf_GetDepth(tag)] = getname(tag);
383                 }
384             }
385             if (tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
386                 MATRIX m = getmatrix(tag);
387                 U16 id = depth2id[swf_GetDepth(tag)];
388                 char*name = depth2name[swf_GetDepth(tag)];
389                 char buf[40];
390                 SRECT bbox = bboxes[id];
391                 SPOINT p1,p2,p3,p4;
392                 p1.x = bbox.xmin; p1.y = bbox.ymin;
393                 p2.x = bbox.xmax; p2.y = bbox.ymin;
394                 p3.x = bbox.xmin; p3.y = bbox.ymax;
395                 p4.x = bbox.xmax; p4.y = bbox.ymax;
396                 p1 = swf_TurnPoint(p1, &m);
397                 p2 = swf_TurnPoint(p2, &m);
398                 p3 = swf_TurnPoint(p3, &m);
399                 p4 = swf_TurnPoint(p4, &m);
400                 if(!name) {
401                     sprintf(buf, "ID%d", id);name = buf;
402                 }
403                 printf("{%s {%f %f %f %f %f %f %f %f}}\n", name, 
404                         p1.x/20.0, p1.y/20.0, p2.x/20.0, p2.y/20.0,
405                         p3.x/20.0, p3.y/20.0, p4.x/20.0, p4.y/20.0);
406             }
407         }
408         tag = tag->next;
409     }
410
411     if(optimize) {
412         fi = open(outfilename, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
413         if(swf_WriteSWF(fi, &swf) < 0) {
414             fprintf(stderr, "Error writing file %s", outfilename);
415             close(fi);
416             exit(1);
417         }
418         close(fi);
419     }
420
421     swf_FreeTags(&swf);
422     return 0;
423 }