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"
31 static char * filename = 0;
32 static char * outfilename = "output.swf";
33 static int optimize = 0;
34 static int swifty = 0;
35 static int verbose = 0;
37 struct options_t options[] =
47 int args_callback_option(char*name,char*val)
49 if(!strcmp(name, "V")) {
50 printf("swfbbox - part of %s %s\n", PACKAGE, VERSION);
53 else if(!strcmp(name, "O")) {
57 else if(!strcmp(name, "S")) {
61 else if(!strcmp(name, "v")) {
65 else if(!strcmp(name, "o")) {
70 printf("Unknown option: -%s\n", name);
76 int args_callback_longoption(char*name,char*val)
78 return args_long2shortoption(options, name, val);
80 void args_callback_usage(char*name)
82 printf("Usage: %s [-at] file.swf\n", name);
83 printf("\t-h , --help\t\t Print help and exit\n");
84 printf("\t-O , --optimize\t\t Recalculate bounding boxes\n");
85 printf("\t-S , --swifty\t\t Print out transformed bounding boxes\n");
86 printf("\t-o , --output\t\t Set output filename (for -O)\n");
87 printf("\t-V , --version\t\t Print program version and exit\n");
89 int args_callback_command(char*name,char*val)
92 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
99 #define swf_ResetReadBits(tag) if (tag->readBit) { tag->pos++; tag->readBit = 0; }
101 void parseFillStyleArray(TAG*tag, SHAPE2*shape)
106 if(tag->id == ST_DEFINESHAPE)
108 else if(tag->id == ST_DEFINESHAPE2)
110 else if(tag->id == ST_DEFINESHAPE3)
113 count = swf_GetU8(tag);
114 if(count == 0xff && num>1) // defineshape2,3 only
115 count = swf_GetU16(tag);
117 if(verbose) printf("num: %d\n", count);
118 shape->numfillstyles = count;
119 shape->fillstyles = malloc(sizeof(FILLSTYLE)*count);
125 FILLSTYLE*dest = &shape->fillstyles[t];
126 swf_ResetReadBits(tag);
127 type = swf_GetU8(tag); //type
128 shape->fillstyles[t].type = type;
132 swf_GetRGBA(tag, &dest->color);
134 swf_GetRGB(tag, &dest->color);
136 else if(type == 0x10 || type == 0x12)
138 /* linear/radial gradient fill */
139 swf_ResetReadBits(tag);
140 swf_GetMatrix(tag, &dest->m);
141 swf_ResetReadBits(tag);
142 dest->gradient = malloc(sizeof(GRADIENT)); // TODO: free this again
143 swf_GetGradient(tag, dest->gradient, num>=3?1:0);
145 else if(type == 0x40 || type == 0x41)
148 swf_ResetReadBits(tag);
149 dest->id_bitmap = swf_GetU16(tag); //id
150 swf_ResetReadBits(tag); //?
151 swf_GetMatrix(tag, &dest->m);
154 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
157 swf_ResetReadBits(tag);
158 count = swf_GetU8(tag); // line style array
160 count = swf_GetU16(tag);
162 if(verbose) printf("lnum: %d\n", count);
164 shape->numlinestyles = count;
165 shape->linestyles = malloc(sizeof(LINESTYLE)*count);
166 /* TODO: should we start with 1 and insert a correct definition of the
167 "built in" linestyle 0? */
170 shape->linestyles[t].width = swf_GetU16(tag);
172 swf_GetRGBA(tag, &shape->linestyles[t].color);
174 swf_GetRGB(tag, &shape->linestyles[t].color);
179 void swf_ParseDefineShape(TAG*tag, SHAPE2*shape)
186 if(tag->id == ST_DEFINESHAPE)
188 else if(tag->id == ST_DEFINESHAPE2)
190 else if(tag->id == ST_DEFINESHAPE3)
193 fprintf(stderr, "parseDefineShape must be called with a shape tag");
196 id = swf_GetU16(tag); //id
197 memset(shape, 0, sizeof(SHAPE2));
198 shape->bbox = malloc(sizeof(SRECT));
199 swf_GetRect(tag, &r);
201 memcpy(shape->bbox, &r, sizeof(SRECT));
202 parseFillStyleArray(tag, shape);
204 swf_ResetReadBits(tag);
205 fill = (U16)swf_GetBits(tag,4);
206 line = (U16)swf_GetBits(tag,4);
208 shape->lines = swf_ParseShapeData(&tag->data[tag->pos], (tag->len - tag->pos)*8, fill, line);
213 void swf_Shape2Optimize(SHAPE2*shape)
216 shape->bbox = malloc(sizeof(SRECT));
217 *(shape->bbox) = swf_GetShapeBoundingBox(shape);
220 void swf_Shape2ToShape(SHAPE2*shape2, SHAPE*shape)
222 TAG*tag = swf_InsertTag(0,0);
224 int newx=0,newy=0,lastx=0,lasty=0,oldls=0,oldfs0=0,oldfs1=0;
226 memset(shape, 0, sizeof(SHAPE));
228 shape->linestyle.n = shape2->numlinestyles;
229 shape->linestyle.data = (LINESTYLE*)malloc(sizeof(LINESTYLE)*shape->linestyle.n);
230 memcpy(shape->linestyle.data, shape2->linestyles, sizeof(LINESTYLE)*shape->linestyle.n);
232 shape->fillstyle.n = shape2->numfillstyles;
233 shape->fillstyle.data = (FILLSTYLE*)malloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
234 memcpy(shape->fillstyle.data, shape2->fillstyles, sizeof(FILLSTYLE)*shape->fillstyle.n);
236 swf_ShapeCountBits(shape,NULL,NULL);
241 int ls=0,fs0=0,fs1=0;
243 if(l->type != moveTo) {
244 if(oldls != l->linestyle) {oldls = ls = l->linestyle;if(!ls) ls=0x8000;}
245 if(oldfs0 != l->fillstyle0) {oldfs0 = fs0 = l->fillstyle0;if(!fs0) fs0=0x8000;}
246 if(oldfs1 != l->fillstyle1) {oldfs1 = fs1 = l->fillstyle1;if(!fs1) fs1=0x8000;}
248 if(ls || fs0 || fs1 || newx!=0x7fffffff || newy!=0x7fffffff) {
249 swf_ShapeSetAll(tag,shape,newx,newy,ls,fs0,fs1);
255 if(l->type == lineTo) {
256 swf_ShapeSetLine(tag,shape,l->x-lastx,l->y-lasty);
257 } else if(l->type == splineTo) {
258 swf_ShapeSetCurve(tag,shape, l->sx-lastx,l->sy-lasty, l->x-l->sx,l->y-l->sy);
260 if(l->type == moveTo) {
269 swf_ShapeSetEnd(tag);
270 shape->data = tag->data;
271 shape->bitlen = tag->len*8;
274 void swf_SetShape2(TAG*tag, SHAPE2*shape2)
277 swf_Shape2ToShape(shape2, &shape);
279 swf_SetRect(tag,shape2->bbox);
280 swf_SetShapeStyles(tag, &shape);
281 swf_ShapeCountBits(&shape,NULL,NULL);
282 swf_SetShapeBits(tag,&shape);
284 swf_SetBlock(tag, shape.data, (shape.bitlen+7)/8);
288 {char {x1 y1 x2 y2 x3 y3 x4 y4]]
293 char*depth2name[65536];
297 if(tag->id == ST_PLACEOBJECT)
299 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 2))
306 if(tag->id == ST_PLACEOBJECT)
308 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 0x20))
313 char* getname(TAG*tag)
315 if(tag->id == ST_PLACEOBJECT)
317 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 0x20)) {
319 tag->pos = 0;tag->readBit = 0;
320 swf_GetPlaceObject(tag, &o);
326 MATRIX getmatrix(TAG*tag)
329 tag->pos = 0;tag->readBit = 0;
330 swf_GetPlaceObject(tag, &o);
334 int main (int argc,char ** argv)
340 memset(bboxes, 0, sizeof(bboxes));
341 memset(depth2name, 0, sizeof(depth2name));
343 processargs(argc, argv);
346 fprintf(stderr, "You must supply a filename.\n");
350 fi = open(filename,O_RDONLY|O_BINARY);
354 perror("Couldn't open file: ");
357 if FAILED(swf_ReadSWF(fi,&swf))
359 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
369 printf("{\n\t{frame %d}\n", frame++);
373 if (tag->id == ST_DEFINESHAPE ||
374 tag->id == ST_DEFINESHAPE2 ||
375 tag->id == ST_DEFINESHAPE3) {
377 if(verbose) printf("%s\n", swf_TagGetName(tag));
378 swf_ParseDefineShape(tag, &s);
379 swf_Shape2Optimize(&s);
381 swf_SetShape2(tag, &s);
383 if(swf_isDefiningTag(tag) && tag->id != ST_DEFINESPRITE) {
384 bboxes[swf_GetDefineID(tag)] = swf_GetDefineBBox(tag);
387 if (tag->id == ST_SHOWFRAME) {
388 printf("}\n{\n\t{frame %d}\n", frame++);
390 if (tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
392 depth2id[swf_GetDepth(tag)] = swf_GetPlaceID(tag);
395 depth2name[swf_GetDepth(tag)] = getname(tag);
398 if (tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
399 MATRIX m = getmatrix(tag);
400 U16 id = depth2id[swf_GetDepth(tag)];
401 char*name = depth2name[swf_GetDepth(tag)];
403 SRECT bbox = bboxes[id];
405 p1.x = bbox.xmin; p1.y = bbox.ymin;
406 p2.x = bbox.xmax; p2.y = bbox.ymin;
407 p3.x = bbox.xmin; p3.y = bbox.ymax;
408 p4.x = bbox.xmax; p4.y = bbox.ymax;
409 p1 = swf_TurnPoint(p1, &m);
410 p2 = swf_TurnPoint(p2, &m);
411 p3 = swf_TurnPoint(p3, &m);
412 p4 = swf_TurnPoint(p4, &m);
414 sprintf(buf, "ID%d", id);name = buf;
416 //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);
417 printf("\t{%s {%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f}}\n", name,
418 p1.x/20.0, p1.y/20.0, p2.x/20.0, p2.y/20.0,
419 p3.x/20.0, p3.y/20.0, p4.x/20.0, p4.y/20.0);
429 fi = open(outfilename, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
430 if(swf_WriteSWF(fi, &swf) < 0) {
431 fprintf(stderr, "Error writing file %s", outfilename);