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;
37 static int showbbox = 0;
38 static int showorigbbox = 1;
39 static int expand = 0;
42 static struct options_t options[] = {
56 int args_callback_option(char*name,char*val)
58 if(!strcmp(name, "V")) {
59 printf("swfbbox - part of %s %s\n", PACKAGE, VERSION);
62 else if(!strcmp(name, "b")) {
64 if(showbbox == 1) showbbox = 0;
67 else if(!strcmp(name, "B")) {
71 else if(!strcmp(name, "O")) {
73 if(showorigbbox == 1) showorigbbox = 0;
76 else if(!strcmp(name, "S")) {
78 if(showorigbbox == 1) showorigbbox = 0;
81 else if(!strcmp(name, "c")) {
82 if(showorigbbox == 1) showorigbbox = 0;
87 else if(!strcmp(name, "v")) {
91 else if(!strcmp(name, "q")) {
96 else if(!strcmp(name, "e")) {
100 else if(!strcmp(name, "o")) {
105 printf("Unknown option: -%s\n", name);
111 int args_callback_longoption(char*name,char*val)
113 return args_long2shortoption(options, name, val);
115 void args_callback_usage(char *name)
118 printf("Usage: %s [-OS] file.swf\n", name);
120 printf("-h , --help Print help and exit\n");
121 printf("-b , --bbox Show movie bounding box (default)\n");
122 printf("-B , --newbbox Show recalculated (optimized/expanded) bounding box\n");
123 printf("-e , --expand Write out a new file using the recalculated bounding box\n");
124 printf("-O , --optimize Recalculate bounding boxes\n");
125 printf("-S , --swifty Print out transformed bounding boxes\n");
126 printf("-c , --clip Clip bounding boxes to movie size\n");
127 printf("-o , --output <filename> Set output filename to <filename> (for -O)\n");
128 printf("-v , --verbose Be more verbose\n");
129 printf("-V , --version Print program version and exit\n");
132 int args_callback_command(char*name,char*val)
135 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
142 #define swf_ResetReadBits(tag) if (tag->readBit) { tag->pos++; tag->readBit = 0; }
144 void swf_Shape2Optimize(SHAPE2*shape)
147 shape->bbox = malloc(sizeof(SRECT));
148 *(shape->bbox) = swf_GetShapeBoundingBox(shape);
152 {char {x1 y1 x2 y2 x3 y3 x4 y4]]
157 char*depth2name[65536];
161 if(tag->id == ST_PLACEOBJECT)
163 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 2))
170 if(tag->id == ST_PLACEOBJECT)
172 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 0x20))
177 char* getname(TAG*tag)
179 if(tag->id == ST_PLACEOBJECT)
181 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 0x20)) {
183 tag->pos = 0;tag->readBit = 0;
184 swf_GetPlaceObject(tag, &o);
190 MATRIX getmatrix(TAG*tag)
193 tag->pos = 0;tag->readBit = 0;
194 swf_GetPlaceObject(tag, &o);
199 static int fontnum = -1;
200 static SWFFONT**fonts;
202 static void fontcallback1(void*self, U16 id,U8 * name)
205 static void fontcallback2(void*self, U16 id,U8 * name)
208 swf_FontExtract(c_swf,id,&fonts[fontnum]);
210 if(fonts[fontnum]) printf("Extracting font %d (%s)\n", id, name);
211 else printf("Extracting font %d (%s) failed\n", id, name);
216 typedef struct _textbounds
219 MATRIX m; // character transform matrix
222 typedef struct _placement
228 static placement_t* placements;
230 static placement_t* readPlacements(SWF*swf)
232 placement_t* p = (placement_t*)rfx_calloc(sizeof(placement_t)*65536);
233 TAG*tag = swf->firstTag;
235 if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
236 SWFPLACEOBJECT*po = rfx_alloc(sizeof(SWFPLACEOBJECT));
238 swf_GetPlaceObject(tag, po);
241 fprintf(stderr, "MOVE tags not supported with -c");
252 static void freePlacements(placement_t*p)
255 for(t=0;t<65536;t++) {
257 swf_PlaceObjectFree(p[t].po); p[t].po = 0;
263 SRECT swf_ClipRect(SRECT border, SRECT r)
265 if(r.xmax > border.xmax) r.xmax = border.xmax;
266 if(r.ymax > border.ymax) r.ymax = border.ymax;
267 if(r.xmax < border.xmin) r.xmax = border.xmin;
268 if(r.ymax < border.ymin) r.ymax = border.ymin;
270 if(r.xmin > border.xmax) r.xmin = border.xmax;
271 if(r.ymin > border.ymax) r.ymin = border.ymax;
272 if(r.xmin < border.xmin) r.xmin = border.xmin;
273 if(r.ymin < border.ymin) r.ymin = border.ymin;
277 static SRECT clipBBox(TAG*tag, SRECT mbbox, SRECT r)
279 int id = swf_GetDefineID(tag);
281 if(!placements[id].po) {
283 printf("Id %d is never set\n", id);
286 if(placements[id].num>1) {
288 printf("Id %d is set more than once\n", id);
291 m = placements[id].po->matrix;
293 fprintf(stderr, "Rotating PLACEOBJECTS are not supported with -c\n");
298 printf("ID %d\n", id);
299 swf_DumpMatrix(stdout, &m);
305 mbbox.xmin *= 65536.0/m.sx;
306 mbbox.xmax *= 65536.0/m.sx;
307 mbbox.ymin *= 65536.0/m.sy;
308 mbbox.ymax *= 65536.0/m.sy;
311 printf("border: %f/%f/%f/%f - rect: %f/%f/%f/%f\n",
323 r = swf_ClipRect(mbbox, r);
326 printf("new rect: %f/%f/%f/%f\n",
337 static void textcallback(void*self, int*chars, int*xpos, int nr, int fontid, int fontsize,
338 int xstart, int ystart, RGBA* color)
340 textbounds_t * bounds = (textbounds_t*)self;
343 for(t=0;t<fontnum;t++) {
344 if(fonts[t]->id == fontid) {
350 fprintf(stderr, "Font %d unknown\n", fontid);
354 /* This is an expensive operation- but what should we do, we
355 need the glyph's bounding boxes */
356 swf_FontCreateLayout(font);
360 printf("%d chars, font %d, size %d, at (%d,%d)\n", nr, fontid, fontsize, xstart, ystart);
363 /* not tested yet- the matrix/fontsize calculation is probably all wrong */
364 int x = xstart + xpos[t];
367 SRECT newglyphbbox, glyphbbox = font->layout->bounds[chars[t]];
368 MATRIX m = bounds->m;
371 if(chars[t] < font->numchars && font->glyph2ascii) {
372 ch = font->glyph2ascii[chars[t]];
376 p = swf_TurnPoint(p, &m);
378 m.sx = (m.sx * fontsize) / 1024;
379 m.sy = (m.sy * fontsize) / 1024;
380 m.r0 = (m.r0 * fontsize) / 1024;
381 m.r1 = (m.r1 * fontsize) / 1024;
385 newglyphbbox = swf_TurnRect(glyphbbox, &m);
389 swf_ExpandRect2(&(bounds->r), &newglyphbbox);
391 printf("%5d %c, %d %d %d %d (%d %d %d %d) -> %d %d %d %d\n",
393 glyphbbox.xmin, glyphbbox.ymin, glyphbbox.xmax, glyphbbox.ymax,
394 newglyphbbox.xmin, newglyphbbox.ymin, newglyphbbox.xmax, newglyphbbox.ymax,
395 bounds->r.xmin, bounds->r.ymin, bounds->r.xmax, bounds->r.ymax);
401 static void swf_OptimizeBoundingBoxes(SWF*swf)
403 TAG* tag = swf->firstTag;
406 if (tag->id == ST_DEFINESHAPE ||
407 tag->id == ST_DEFINESHAPE2 ||
408 tag->id == ST_DEFINESHAPE3) {
410 if(verbose) printf("%s\n", swf_TagGetName(tag));
411 swf_ParseDefineShape(tag, &s);
412 swf_Shape2Optimize(&s);
416 fprintf(stderr, "Internal error (5)\n");
420 *s.bbox = clipBBox(tag, swf->movieSize, *s.bbox);
422 swf_SetShape2(tag, &s);
424 if (tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
430 if(verbose) printf("%s\n", swf_TagGetName(tag));
432 if(verbose) printf("Extracting fonts...\n");
435 swf_FontEnumerate(swf,&fontcallback1,0);
436 fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
437 memset(fonts, 0, fontnum*sizeof(SWFFONT*));
439 swf_FontEnumerate(swf,&fontcallback2,0);
442 memset(&bounds, 0, sizeof(bounds));
444 swf_SetTagPos(tag, 0);
446 swf_GetRect(tag,&oldbox);
447 swf_ResetReadBits(tag);
448 matrix_offset = tag->pos;
449 swf_GetMatrix(tag,&bounds.m);
450 swf_ParseDefineText(tag, textcallback, &bounds);
453 swf_DumpMatrix(stdout, &bounds.m);
454 printf("old: %d %d %d %d\n", oldbox.xmin, oldbox.ymin, oldbox.xmax, oldbox.ymax);
455 printf("new: %d %d %d %d\n", bounds.r.xmin, bounds.r.ymin, bounds.r.xmax, bounds.r.ymax);
458 bounds.r = clipBBox(tag, swf->movieSize, bounds.r);
461 /* now comes the tricky part:
462 we have to fiddle the data back in
463 thank heavens that the bbox is follow by a matrix
464 struct, which always starts on a byte boundary.
466 len = tag->len - matrix_offset;
468 memcpy(data, &tag->data[matrix_offset], len);
471 swf_SetRect(tag, &bounds.r);
472 swf_SetBlock(tag, data, len);
474 tag->pos = tag->readBit = 0;
480 static void showSwiftyOutput(SWF*swf)
482 TAG*tag = swf->firstTag;
484 printf("{\n\t{frame %d}\n", frame++);
487 if (tag->id == ST_SHOWFRAME) {
488 printf("}\n{\n\t{frame %d}\n", frame++);
490 if (tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
492 depth2id[swf_GetDepth(tag)] = swf_GetPlaceID(tag);
495 depth2name[swf_GetDepth(tag)] = getname(tag);
498 if (tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
499 MATRIX m = getmatrix(tag);
500 U16 id = depth2id[swf_GetDepth(tag)];
501 char*name = depth2name[swf_GetDepth(tag)];
503 SRECT bbox = bboxes[id];
505 p1.x = bbox.xmin; p1.y = bbox.ymin;
506 p2.x = bbox.xmax; p2.y = bbox.ymin;
507 p3.x = bbox.xmin; p3.y = bbox.ymax;
508 p4.x = bbox.xmax; p4.y = bbox.ymax;
509 p1 = swf_TurnPoint(p1, &m);
510 p2 = swf_TurnPoint(p2, &m);
511 p3 = swf_TurnPoint(p3, &m);
512 p4 = swf_TurnPoint(p4, &m);
514 sprintf(buf, "ID%d", id);name = buf;
516 //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);
517 printf("\t{%s {%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f}}\n", name,
518 p1.x/20.0, p1.y/20.0, p2.x/20.0, p2.y/20.0,
519 p3.x/20.0, p3.y/20.0, p4.x/20.0, p4.y/20.0);
525 static SRECT getMovieClipBBox(TAG*tag)
527 //TAG*tag = swf->firstTag;
531 memset(depth2id, 0, sizeof(depth2id));
533 memset(&movieSize,0,sizeof(SRECT));
535 while (tag->id != ST_END) {
536 if (tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
538 depth2id[swf_GetDepth(tag)] = swf_GetPlaceID(tag);
541 if (tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
542 MATRIX m = getmatrix(tag);
543 U16 id = depth2id[swf_GetDepth(tag)];
544 SRECT bbox = bboxes[id];
546 SRECT tbbox = swf_TurnRect(bbox, &m);
547 swf_ExpandRect2(&movieSize, &tbbox);
554 static SRECT getSWFBBox(SWF*swf)
556 SRECT movieSize = getMovieClipBBox(swf->firstTag);
561 int main (int argc,char ** argv)
568 memset(bboxes, 0, sizeof(bboxes));
569 memset(depth2name, 0, sizeof(depth2name));
571 processargs(argc, argv);
572 initLog(0,0,0,0,0,verbose?LOGLEVEL_DEBUG:LOGLEVEL_WARNING);
575 fprintf(stderr, "You must supply a filename.\n");
579 fi = open(filename,O_RDONLY|O_BINARY);
583 perror("Couldn't open file: ");
586 if FAILED(swf_ReadSWF(fi,&swf))
588 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
594 swf_OptimizeTagOrder(&swf);
597 placements = readPlacements(&swf);
602 /* Optimize bounding boxes in case -O flag was set */
604 swf_OptimizeBoundingBoxes(&swf);
607 /* Create an ID to Bounding Box table */
610 if(swf_isDefiningTag(tag)) {
611 int id = swf_GetDefineID(tag);
612 if(tag->id != ST_DEFINESPRITE) {
613 bboxes[id] = swf_GetDefineBBox(tag);
615 swf_UnFoldSprite(tag);
616 bboxes[id] = getMovieClipBBox(tag);
619 printf("sprite %d is %.2fx%.2f\n", id,
620 (bboxes[id].xmax - bboxes[id].xmin)/20.0,
621 (bboxes[id].ymax - bboxes[id].ymin)/20.0);
628 /* Create an ID->Bounding Box table for all bounding boxes */
630 showSwiftyOutput(&swf);
633 oldMovieSize = swf.movieSize;
634 newMovieSize = getSWFBBox(&swf);
636 if(optimize || expand) {
639 swf.movieSize = newMovieSize;
641 fi = open(outfilename, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
642 if(swf_WriteSWF(fi, &swf) < 0) {
643 fprintf(stderr, "Error writing file %s", outfilename);
652 printf("Real Movie Size: ");
653 printf("%.2f x %.2f :%.2f :%.2f\n",
654 (newMovieSize.xmax-newMovieSize.xmin)/20.0,
655 (newMovieSize.ymax-newMovieSize.ymin)/20.0,
656 (newMovieSize.xmin)/20.0,
657 (newMovieSize.ymin)/20.0
662 printf("Original Movie Size: ");
663 printf("%.2f x %.2f :%.2f :%.2f\n",
664 (oldMovieSize.xmax-oldMovieSize.xmin)/20.0,
665 (oldMovieSize.ymax-oldMovieSize.ymin)/20.0,
666 (oldMovieSize.xmin)/20.0,
667 (oldMovieSize.ymin)/20.0
674 freePlacements(placements);