2 Tool for playing around with SWF bounding boxes.
4 Part of the swftools package.
6 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8 This file is distributed under the GPL, see file COPYING for details */
10 #include "../config.h"
16 #include "../lib/rfxswf.h"
17 #include "../lib/args.h"
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;
25 struct options_t options[] =
35 int args_callback_option(char*name,char*val)
37 if(!strcmp(name, "V")) {
38 printf("swfbbox - part of %s %s\n", PACKAGE, VERSION);
41 else if(!strcmp(name, "O")) {
45 else if(!strcmp(name, "S")) {
49 else if(!strcmp(name, "v")) {
53 else if(!strcmp(name, "o")) {
58 printf("Unknown option: -%s\n", name);
64 int args_callback_longoption(char*name,char*val)
66 return args_long2shortoption(options, name, val);
68 void args_callback_usage(char*name)
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 Recalculate bounding boxes\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");
77 int args_callback_command(char*name,char*val)
80 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
87 #define swf_ResetReadBits(tag) if (tag->readBit) { tag->pos++; tag->readBit = 0; }
89 void parseFillStyleArray(TAG*tag, SHAPE2*shape)
94 if(tag->id == ST_DEFINESHAPE)
96 else if(tag->id == ST_DEFINESHAPE2)
98 else if(tag->id == ST_DEFINESHAPE3)
101 count = swf_GetU8(tag);
102 if(count == 0xff && num>1) // defineshape2,3 only
103 count = swf_GetU16(tag);
105 if(verbose) printf("num: %d\n", count);
106 shape->numfillstyles = count;
107 shape->fillstyles = malloc(sizeof(FILLSTYLE)*count);
113 FILLSTYLE*dest = &shape->fillstyles[t];
114 swf_ResetReadBits(tag);
115 type = swf_GetU8(tag); //type
116 shape->fillstyles[t].type = type;
120 swf_GetRGBA(tag, &dest->color);
122 swf_GetRGB(tag, &dest->color);
124 else if(type == 0x10 || type == 0x12)
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);
133 else if(type == 0x40 || type == 0x41)
136 swf_ResetReadBits(tag);
137 dest->id_bitmap = swf_GetU16(tag); //id
138 swf_ResetReadBits(tag); //?
139 swf_GetMatrix(tag, &dest->m);
142 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
145 swf_ResetReadBits(tag);
146 count = swf_GetU8(tag); // line style array
148 count = swf_GetU16(tag);
150 if(verbose) printf("lnum: %d\n", count);
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? */
158 shape->linestyles[t].width = swf_GetU16(tag);
160 swf_GetRGBA(tag, &shape->linestyles[t].color);
162 swf_GetRGB(tag, &shape->linestyles[t].color);
167 void swf_ParseDefineShape(TAG*tag, SHAPE2*shape)
174 if(tag->id == ST_DEFINESHAPE)
176 else if(tag->id == ST_DEFINESHAPE2)
178 else if(tag->id == ST_DEFINESHAPE3)
181 fprintf(stderr, "parseDefineShape must be called with a shape tag");
184 id = swf_GetU16(tag); //id
185 memset(shape, 0, sizeof(SHAPE2));
186 shape->bbox = malloc(sizeof(SRECT));
187 swf_GetRect(tag, &r);
189 memcpy(shape->bbox, &r, sizeof(SRECT));
190 parseFillStyleArray(tag, shape);
192 swf_ResetReadBits(tag);
193 fill = (U16)swf_GetBits(tag,4);
194 line = (U16)swf_GetBits(tag,4);
196 shape->lines = swf_ParseShapeData(&tag->data[tag->pos], (tag->len - tag->pos)*8, fill, line);
201 void swf_Shape2Optimize(SHAPE2*shape)
204 shape->bbox = malloc(sizeof(SRECT));
205 *(shape->bbox) = swf_GetShapeBoundingBox(shape);
208 void swf_Shape2ToShape(SHAPE2*shape2, SHAPE*shape)
210 TAG*tag = swf_InsertTag(0,0);
212 int newx=0,newy=0,lastx=0,lasty=0,oldls=0,oldfs0=0,oldfs1=0;
214 memset(shape, 0, sizeof(SHAPE));
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);
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);
224 swf_ShapeCountBits(shape,NULL,NULL);
229 int ls=0,fs0=0,fs1=0;
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;}
236 if(ls || fs0 || fs1 || newx!=0x7fffffff || newy!=0x7fffffff) {
237 swf_ShapeSetAll(tag,shape,newx,newy,ls,fs0,fs1);
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);
248 if(l->type == moveTo) {
257 swf_ShapeSetEnd(tag);
258 shape->data = tag->data;
259 shape->bitlen = tag->len*8;
262 void swf_SetShape2(TAG*tag, SHAPE2*shape2)
265 swf_Shape2ToShape(shape2, &shape);
267 swf_SetRect(tag,shape2->bbox);
268 swf_SetShapeStyles(tag, &shape);
269 swf_ShapeCountBits(&shape,NULL,NULL);
270 swf_SetShapeBits(tag,&shape);
272 swf_SetBlock(tag, shape.data, (shape.bitlen+7)/8);
276 {char {x1 y1 x2 y2 x3 y3 x4 y4]]
281 char*depth2name[65536];
285 if(tag->id == ST_PLACEOBJECT)
287 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 2))
294 if(tag->id == ST_PLACEOBJECT)
296 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 0x20))
301 char* getname(TAG*tag)
303 if(tag->id == ST_PLACEOBJECT)
305 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 0x20)) {
307 tag->pos = 0;tag->readBit = 0;
308 swf_GetPlaceObject(tag, &o);
314 MATRIX getmatrix(TAG*tag)
317 tag->pos = 0;tag->readBit = 0;
318 swf_GetPlaceObject(tag, &o);
322 int main (int argc,char ** argv)
328 memset(bboxes, 0, sizeof(bboxes));
329 memset(depth2name, 0, sizeof(depth2name));
331 processargs(argc, argv);
334 fprintf(stderr, "You must supply a filename.\n");
338 fi = open(filename,O_RDONLY|O_BINARY);
342 perror("Couldn't open file: ");
345 if FAILED(swf_ReadSWF(fi,&swf))
347 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
357 printf("{\n\t{frame %d}\n", frame++);
361 if (tag->id == ST_DEFINESHAPE ||
362 tag->id == ST_DEFINESHAPE2 ||
363 tag->id == ST_DEFINESHAPE3) {
365 if(verbose) printf("%s\n", swf_TagGetName(tag));
366 swf_ParseDefineShape(tag, &s);
367 swf_Shape2Optimize(&s);
369 swf_SetShape2(tag, &s);
371 if(swf_isDefiningTag(tag) && tag->id != ST_DEFINESPRITE) {
372 bboxes[swf_GetDefineID(tag)] = swf_GetDefineBBox(tag);
375 if (tag->id == ST_SHOWFRAME) {
376 printf("}\n{\n\t{frame %d}\n", frame++);
378 if (tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
380 depth2id[swf_GetDepth(tag)] = swf_GetPlaceID(tag);
383 depth2name[swf_GetDepth(tag)] = getname(tag);
386 if (tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
387 MATRIX m = getmatrix(tag);
388 U16 id = depth2id[swf_GetDepth(tag)];
389 char*name = depth2name[swf_GetDepth(tag)];
391 SRECT bbox = bboxes[id];
393 p1.x = bbox.xmin; p1.y = bbox.ymin;
394 p2.x = bbox.xmax; p2.y = bbox.ymin;
395 p3.x = bbox.xmin; p3.y = bbox.ymax;
396 p4.x = bbox.xmax; p4.y = bbox.ymax;
397 p1 = swf_TurnPoint(p1, &m);
398 p2 = swf_TurnPoint(p2, &m);
399 p3 = swf_TurnPoint(p3, &m);
400 p4 = swf_TurnPoint(p4, &m);
402 sprintf(buf, "ID%d", id);name = buf;
404 //printf("\t#%.4f %.4f %.4f %.4f | %.4f %.4f\n", m.sx/65536.0, m.r1/65536.0, m.r0/65536.0, m.sy/65536.0, m.tx/20.0, m.ty/20.0);
405 printf("\t{%s {%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f}}\n", name,
406 p1.x/20.0, p1.y/20.0, p2.x/20.0, p2.y/20.0,
407 p3.x/20.0, p3.y/20.0, p4.x/20.0, p4.y/20.0);
417 fi = open(outfilename, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
418 if(swf_WriteSWF(fi, &swf) < 0) {
419 fprintf(stderr, "Error writing file %s", outfilename);