2 Tool for playing around with SWF bounding boxes.
4 Part of the swftools package.
6 Copyright (c) 2003 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
22 #include "../config.h"
28 #include "../lib/rfxswf.h"
29 #include "../lib/args.h"
30 #include "../lib/log.h"
32 static char * filename = 0;
33 static char * outfilename = "output.swf";
34 static int optimize = 0;
35 static int swifty = 0;
36 static int verbose = 0;
38 struct options_t options[] =
48 int args_callback_option(char*name,char*val)
50 if(!strcmp(name, "V")) {
51 printf("swfbbox - part of %s %s\n", PACKAGE, VERSION);
54 else if(!strcmp(name, "O")) {
58 else if(!strcmp(name, "S")) {
62 else if(!strcmp(name, "v")) {
66 else if(!strcmp(name, "o")) {
71 printf("Unknown option: -%s\n", name);
77 int args_callback_longoption(char*name,char*val)
79 return args_long2shortoption(options, name, val);
81 void args_callback_usage(char*name)
83 printf("Usage: %s [-OS] file.swf\n", name);
84 printf("\t-h , --help\t\t Print help and exit\n");
85 printf("\t-O , --optimize\t\t Recalculate bounding boxes\n");
86 printf("\t-S , --swifty\t\t Print out transformed bounding boxes\n");
87 printf("\t-o , --output\t\t Set output filename (for -O)\n");
88 printf("\t-v , --verbose\t\t Be more verbose\n");
89 printf("\t-V , --version\t\t Print program version and exit\n");
91 int args_callback_command(char*name,char*val)
94 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
101 #define swf_ResetReadBits(tag) if (tag->readBit) { tag->pos++; tag->readBit = 0; }
103 void parseFillStyleArray(TAG*tag, SHAPE2*shape)
108 if(tag->id == ST_DEFINESHAPE)
110 else if(tag->id == ST_DEFINESHAPE2)
112 else if(tag->id == ST_DEFINESHAPE3)
115 count = swf_GetU8(tag);
116 if(count == 0xff && num>1) // defineshape2,3 only
117 count = swf_GetU16(tag);
119 if(verbose) printf("num: %d\n", count);
120 shape->numfillstyles = count;
121 shape->fillstyles = malloc(sizeof(FILLSTYLE)*count);
127 FILLSTYLE*dest = &shape->fillstyles[t];
128 swf_ResetReadBits(tag);
129 type = swf_GetU8(tag); //type
130 shape->fillstyles[t].type = type;
134 swf_GetRGBA(tag, &dest->color);
136 swf_GetRGB(tag, &dest->color);
138 else if(type == 0x10 || type == 0x12)
140 /* linear/radial gradient fill */
141 swf_ResetReadBits(tag);
142 swf_GetMatrix(tag, &dest->m);
143 swf_ResetReadBits(tag);
144 swf_GetGradient(tag, &dest->gradient, num>=3?1:0);
146 else if(type == 0x40 || type == 0x41)
149 swf_ResetReadBits(tag);
150 dest->id_bitmap = swf_GetU16(tag); //id
151 swf_ResetReadBits(tag); //?
152 swf_GetMatrix(tag, &dest->m);
155 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
158 swf_ResetReadBits(tag);
159 count = swf_GetU8(tag); // line style array
161 count = swf_GetU16(tag);
163 if(verbose) printf("lnum: %d\n", count);
165 shape->numlinestyles = count;
166 shape->linestyles = malloc(sizeof(LINESTYLE)*count);
167 /* TODO: should we start with 1 and insert a correct definition of the
168 "built in" linestyle 0? */
171 shape->linestyles[t].width = swf_GetU16(tag);
173 swf_GetRGBA(tag, &shape->linestyles[t].color);
175 swf_GetRGB(tag, &shape->linestyles[t].color);
180 void swf_ParseDefineShape(TAG*tag, SHAPE2*shape)
187 if(tag->id == ST_DEFINESHAPE)
189 else if(tag->id == ST_DEFINESHAPE2)
191 else if(tag->id == ST_DEFINESHAPE3)
194 fprintf(stderr, "parseDefineShape must be called with a shape tag");
197 id = swf_GetU16(tag); //id
198 memset(shape, 0, sizeof(SHAPE2));
199 shape->bbox = malloc(sizeof(SRECT));
200 swf_GetRect(tag, &r);
202 memcpy(shape->bbox, &r, sizeof(SRECT));
203 parseFillStyleArray(tag, shape);
205 swf_ResetReadBits(tag);
206 fill = (U16)swf_GetBits(tag,4);
207 line = (U16)swf_GetBits(tag,4);
209 shape->lines = swf_ParseShapeData(&tag->data[tag->pos], (tag->len - tag->pos)*8, fill, line);
214 void swf_Shape2Optimize(SHAPE2*shape)
217 shape->bbox = malloc(sizeof(SRECT));
218 *(shape->bbox) = swf_GetShapeBoundingBox(shape);
221 void swf_Shape2ToShape(SHAPE2*shape2, SHAPE*shape)
223 TAG*tag = swf_InsertTag(0,0);
225 int newx=0,newy=0,lastx=0,lasty=0,oldls=0,oldfs0=0,oldfs1=0;
227 memset(shape, 0, sizeof(SHAPE));
229 shape->linestyle.n = shape2->numlinestyles;
230 shape->linestyle.data = (LINESTYLE*)malloc(sizeof(LINESTYLE)*shape->linestyle.n);
231 memcpy(shape->linestyle.data, shape2->linestyles, sizeof(LINESTYLE)*shape->linestyle.n);
233 shape->fillstyle.n = shape2->numfillstyles;
234 shape->fillstyle.data = (FILLSTYLE*)malloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
235 memcpy(shape->fillstyle.data, shape2->fillstyles, sizeof(FILLSTYLE)*shape->fillstyle.n);
237 swf_ShapeCountBits(shape,NULL,NULL);
242 int ls=0,fs0=0,fs1=0;
244 if(l->type != moveTo) {
245 if(oldls != l->linestyle) {oldls = ls = l->linestyle;if(!ls) ls=0x8000;}
246 if(oldfs0 != l->fillstyle0) {oldfs0 = fs0 = l->fillstyle0;if(!fs0) fs0=0x8000;}
247 if(oldfs1 != l->fillstyle1) {oldfs1 = fs1 = l->fillstyle1;if(!fs1) fs1=0x8000;}
249 if(ls || fs0 || fs1 || newx!=0x7fffffff || newy!=0x7fffffff) {
250 swf_ShapeSetAll(tag,shape,newx,newy,ls,fs0,fs1);
256 if(l->type == lineTo) {
257 swf_ShapeSetLine(tag,shape,l->x-lastx,l->y-lasty);
258 } else if(l->type == splineTo) {
259 swf_ShapeSetCurve(tag,shape, l->sx-lastx,l->sy-lasty, l->x-l->sx,l->y-l->sy);
261 if(l->type == moveTo) {
270 swf_ShapeSetEnd(tag);
271 shape->data = tag->data;
272 shape->bitlen = tag->len*8;
275 void swf_SetShape2(TAG*tag, SHAPE2*shape2)
278 swf_Shape2ToShape(shape2, &shape);
280 swf_SetRect(tag,shape2->bbox);
281 swf_SetShapeStyles(tag, &shape);
282 swf_ShapeCountBits(&shape,NULL,NULL);
283 swf_SetShapeBits(tag,&shape);
285 swf_SetBlock(tag, shape.data, (shape.bitlen+7)/8);
289 {char {x1 y1 x2 y2 x3 y3 x4 y4]]
294 char*depth2name[65536];
298 if(tag->id == ST_PLACEOBJECT)
300 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 2))
307 if(tag->id == ST_PLACEOBJECT)
309 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 0x20))
314 char* getname(TAG*tag)
316 if(tag->id == ST_PLACEOBJECT)
318 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 0x20)) {
320 tag->pos = 0;tag->readBit = 0;
321 swf_GetPlaceObject(tag, &o);
327 MATRIX getmatrix(TAG*tag)
330 tag->pos = 0;tag->readBit = 0;
331 swf_GetPlaceObject(tag, &o);
335 static void swf_OptimizeBoundingBoxes(SWF*swf)
337 TAG* tag = swf->firstTag;
339 if (tag->id == ST_DEFINESHAPE ||
340 tag->id == ST_DEFINESHAPE2 ||
341 tag->id == ST_DEFINESHAPE3) {
343 if(verbose) printf("%s\n", swf_TagGetName(tag));
344 swf_ParseDefineShape(tag, &s);
345 swf_Shape2Optimize(&s);
348 swf_SetShape2(tag, &s);
353 static void showSwiftyOutput(SWF*swf)
355 TAG*tag = swf->firstTag;
357 printf("{\n\t{frame %d}\n", frame++);
360 if (tag->id == ST_SHOWFRAME) {
361 printf("}\n{\n\t{frame %d}\n", frame++);
363 if (tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
365 depth2id[swf_GetDepth(tag)] = swf_GetPlaceID(tag);
368 depth2name[swf_GetDepth(tag)] = getname(tag);
371 if (tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
372 MATRIX m = getmatrix(tag);
373 U16 id = depth2id[swf_GetDepth(tag)];
374 char*name = depth2name[swf_GetDepth(tag)];
376 SRECT bbox = bboxes[id];
378 p1.x = bbox.xmin; p1.y = bbox.ymin;
379 p2.x = bbox.xmax; p2.y = bbox.ymin;
380 p3.x = bbox.xmin; p3.y = bbox.ymax;
381 p4.x = bbox.xmax; p4.y = bbox.ymax;
382 p1 = swf_TurnPoint(p1, &m);
383 p2 = swf_TurnPoint(p2, &m);
384 p3 = swf_TurnPoint(p3, &m);
385 p4 = swf_TurnPoint(p4, &m);
387 sprintf(buf, "ID%d", id);name = buf;
389 //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);
390 printf("\t{%s {%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f}}\n", name,
391 p1.x/20.0, p1.y/20.0, p2.x/20.0, p2.y/20.0,
392 p3.x/20.0, p3.y/20.0, p4.x/20.0, p4.y/20.0);
400 int main (int argc,char ** argv)
405 memset(bboxes, 0, sizeof(bboxes));
406 memset(depth2name, 0, sizeof(depth2name));
408 processargs(argc, argv);
409 initLog(0,0,0,0,0,verbose?LOGLEVEL_DEBUG:LOGLEVEL_WARNING);
412 fprintf(stderr, "You must supply a filename.\n");
416 fi = open(filename,O_RDONLY|O_BINARY);
420 perror("Couldn't open file: ");
423 if FAILED(swf_ReadSWF(fi,&swf))
425 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
431 swf_OptimizeTagOrder(&swf);
434 /* Optimize bounding boxes in case -O flag was set */
436 swf_OptimizeBoundingBoxes(&swf);
439 /* Create an ID to Bounding Box table */
442 if(swf_isDefiningTag(tag) && tag->id != ST_DEFINESPRITE) {
443 bboxes[swf_GetDefineID(tag)] = swf_GetDefineBBox(tag);
448 /* Create an ID->Bounding Box table for all bounding boxes */
450 showSwiftyOutput(&swf);
454 fi = open(outfilename, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
455 if(swf_WriteSWF(fi, &swf) < 0) {
456 fprintf(stderr, "Error writing file %s", outfilename);