3 Math and matrix functions, misc tools
5 Extension module for the rfxswf library.
6 Part of the swftools package.
8 Copyright (c) 2000, 2001 Rainer Böhme <rfxswf@reflex-studio.de>
10 This file is distributed under the GPL, see file COPYING for details
14 // Matrix & Math tools for SWF files
17 SFIXED RFXSWF_SP(SFIXED a1,SFIXED a2,SFIXED b1,SFIXED b2)
19 a = (S64)a1*(S64)b1+(S64)a2*(S64)b2;
20 return (SFIXED)(a>>16);
22 SFIXED RFXSWF_QFIX(int zaehler,int nenner) // bildet Quotient von zwei INTs in SFIXED
23 { S64 z = zaehler<<16;
24 S64 a = z/(S64)nenner;
29 MATRIX * swf_MatrixJoin(MATRIX * d,MATRIX * s1,MATRIX * s2)
32 if (!s1) return (s2)?(MATRIX *)memcpy(d,s2,sizeof(MATRIX)):NULL;
33 if (!s2) return (MATRIX *)memcpy(d,s1,sizeof(MATRIX));
35 d->tx = s1->tx + s2->tx;
36 d->ty = s1->ty + s2->ty;
38 d->sx = RFXSWF_SP(s1->sx,s1->r1,s2->sx,s2->r0);
39 d->sy = RFXSWF_SP(s1->r0,s1->sy,s2->r1,s2->sy);
40 d->r0 = RFXSWF_SP(s1->r0,s1->sy,s2->sx,s2->r0);
41 d->r1 = RFXSWF_SP(s1->sx,s1->r1,s2->r1,s2->sy);
48 MATRIX * swf_MatrixMapTriangle(MATRIX * m,int dx,int dy,int x0,int y0,
49 int x1,int y1,int x2,int y2)
56 if ((!dx)||(!dy)) return NULL; // check DIV by zero
60 m->sx = RFXSWF_QFIX(dx1,dx);
61 m->sy = RFXSWF_QFIX(dy2,dy);
62 m->r0 = RFXSWF_QFIX(dy1,dx);
63 m->r1 = RFXSWF_QFIX(dx2,dy);
68 void swf_SetDefineID(TAG * tag, U16 newid)
70 int oldlen = tag->len;
72 swf_SetU16(tag, newid); /* set defining ID */
76 U16 swf_GetDefineID(TAG * t)
81 oldTagPos = swf_GetTagPos(t);
84 switch (swf_GetTagID(t))
85 { case ST_DEFINESHAPE:
88 case ST_DEFINEMORPHSHAPE:
89 case ST_DEFINEEDITTEXT:
91 case ST_DEFINEBITSJPEG2:
92 case ST_DEFINEBITSJPEG3:
93 case ST_DEFINEBITSLOSSLESS:
94 case ST_DEFINEBITSLOSSLESS2:
96 case ST_DEFINEBUTTON2:
97 case ST_DEFINEBUTTONCXFORM: //pseudodefine
98 case ST_DEFINEBUTTONSOUND: //pseudodefine
101 case ST_DEFINEFONTINFO: //pseudodefine
105 case ST_DEFINESPRITE:
106 case ST_NAMECHARACTER: //pseudodefine
111 swf_SetTagPos(t,oldTagPos);
116 U16 swf_GetPlaceID(TAG * t)
121 oldTagPos = swf_GetTagPos(t);
124 switch (swf_GetTagID(t))
125 { case ST_PLACEOBJECT:
126 case ST_REMOVEOBJECT:
127 case ST_FREECHARACTER:
132 case ST_PLACEOBJECT2:
133 { U8 flags = swf_GetU8(t);
134 U16 d = swf_GetU16(t);
135 id = (flags&PF_CHAR)?swf_GetU16(t):id;
140 swf_SetTagPos(t,oldTagPos);
145 static int swf_definingtagids[] =
158 ST_DEFINEBITSLOSSLESS,
159 ST_DEFINEBITSLOSSLESS2,
168 // tags which may be used inside a sprite definition
169 static int swf_spritetagids[] =
174 ST_REMOVEOBJECT2, //?
185 static int swf_pseudodefiningtagids[] =
188 ST_DEFINEBUTTONCXFORM,
189 ST_DEFINEBUTTONSOUND,
194 U8 swf_isAllowedSpriteTag(TAG * tag)
198 while(swf_spritetagids[t]>=0)
200 if(swf_spritetagids[t] == id)
207 U8 swf_isDefiningTag(TAG * tag)
211 while(swf_definingtagids[t]>=0)
213 if(swf_definingtagids[t] == id)
220 U8 swf_isPseudoDefiningTag(TAG * tag)
224 while(swf_pseudodefiningtagids[t]>=0)
226 if(swf_pseudodefiningtagids[t] == id)
233 U16 swf_GetDepth(TAG * t)
238 oldTagPos = swf_GetTagPos(t);
241 switch (swf_GetTagID(t))
242 { case ST_PLACEOBJECT:
243 case ST_REMOVEOBJECT:
245 depth = swf_GetU16(t);
247 case ST_REMOVEOBJECT2:
248 depth = swf_GetU16(t);
250 case ST_PLACEOBJECT2:
251 { U8 flags = swf_GetU8(t);
252 depth = swf_GetU16(t);
255 swf_SetTagPos(t,oldTagPos);
259 char* swf_GetName(TAG * t)
265 oldTagPos = swf_GetTagPos(t);
267 switch(swf_GetTagID(t))
270 name = &t->data[swf_GetTagPos(t)];
272 case ST_PLACEOBJECT2: {
273 U8 flags = swf_GetU8(t);
274 swf_GetU16(t); //depth;
278 swf_GetMatrix(t, &m);
280 swf_GetCXForm(t, &c, 1);
284 swf_ResetReadBits(t);
285 name = &t->data[swf_GetTagPos(t)];
290 swf_SetTagPos(t,oldTagPos);
294 #define DEBUG_ENUMERATE if(0)
296 static void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
300 count = swf_GetU8(tag);
301 if(count == 0xff && num>1) // defineshape2,3 only
302 count = swf_GetU16(tag);
308 swf_ResetReadBits(tag);
309 type = swf_GetU8(tag); //type
312 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
314 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
316 else if(type == 0x10 || type == 0x12)
318 swf_ResetReadBits(tag);
319 swf_GetMatrix(tag, NULL);
321 swf_GetMatrix(tag, NULL);
322 swf_ResetReadBits(tag);
324 swf_GetMorphGradient(tag, NULL, NULL);
326 swf_GetGradient(tag, NULL, /*alpha*/ num>=3?1:0);
328 else if(type == 0x40 || type == 0x41)
330 swf_ResetReadBits(tag);
332 if(tag->data[tag->pos] != 0xff ||
333 tag->data[tag->pos+1] != 0xff)
334 (callback)(tag, tag->pos, callback_data);
337 swf_ResetReadBits(tag);
338 swf_GetMatrix(tag, NULL);
340 swf_GetMatrix(tag, NULL);
343 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
346 swf_ResetReadBits(tag);
347 count = swf_GetU8(tag); // line style array
349 count = swf_GetU16(tag);
356 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
358 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
362 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
365 swf_ResetReadBits(tag);
369 case ST_DEFINEBUTTONCXFORM: {
371 callback(tag, tag->pos + base, callback_data);
374 callback(tag, tag->pos + base, callback_data);
375 swf_GetU16(tag); //sound id
376 flags = swf_GetU8(tag);
378 swf_GetU32(tag); // in point
380 swf_GetU32(tag); // out points
382 swf_GetU16(tag); // loop count
385 int npoints = swf_GetU8(tag);
387 for(s=0;s<npoints;s++)
396 case ST_DEFINEBUTTONSOUND:
397 callback(tag, tag->pos + base, callback_data); //button id
400 case ST_EXPORTASSETS: {
401 int num = swf_GetU16(tag);
404 callback(tag, tag->pos + base, callback_data); //button id
405 swf_GetU16(tag); //id
406 while(swf_GetU8(tag)); //name
410 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
411 case ST_NAMECHARACTER:
412 case ST_GENERATORTEXT:
414 callback(tag, tag->pos + base, callback_data);
417 callback(tag, tag->pos + base, callback_data);
419 case ST_PLACEOBJECT2:
420 // only if placeflaghascharacter
421 if(!(tag->data[0]&2))
423 callback(tag, 3 + base, callback_data);
425 case ST_REMOVEOBJECT:
426 callback(tag, tag->pos + base, callback_data);
429 callback(tag, tag->pos + base, callback_data);
431 case ST_DEFINESPRITE: {
433 break; // sprite is expanded
435 swf_GetU16(tag); // id
436 swf_GetU16(tag); // framenum
439 U16 flags = swf_GetU16(tag);
442 TAG *tag2 = swf_InsertTag(NULL, id);
445 len = swf_GetU32(tag);
448 tag2->len = tag2->memsize = len;
449 tag2->data = malloc(len);
450 memcpy(tag2->data, &tag->data[tag->pos], len);
451 /* I never saw recursive sprites, but they are (theoretically)
452 possible, so better add base here again */
453 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
455 swf_GetBlock(tag, NULL, len);
459 case ST_DEFINEBUTTON2: // has some font ids in the button records
462 case ST_DEFINEBUTTON: {
463 swf_GetU16(tag); //button id
467 swf_GetU8(tag); //flag
468 offset = swf_GetU16(tag); //offset
473 if(!swf_GetU8(tag)) //flags
475 callback(tag, tag->pos + base, callback_data);
476 swf_GetU16(tag); //char
477 swf_GetU16(tag); //layer
478 swf_ResetReadBits(tag);
479 swf_GetMatrix(tag, NULL);
481 swf_ResetReadBits(tag);
482 swf_GetCXForm(tag, NULL, 1);
488 case ST_DEFINEEDITTEXT: {
490 swf_GetU16(tag); //id
491 swf_GetRect(tag, NULL); //bounding box
492 swf_ResetReadBits(tag);
493 flags1 = swf_GetU8(tag);
494 flags2 = swf_GetU8(tag);
496 callback(tag, tag->pos + base, callback_data);
501 case ST_DEFINETEXT: {
502 int glyphbits, advancebits;
504 id = swf_GetU16(tag); //id
505 swf_GetRect(tag, NULL); //bounding box
506 swf_ResetReadBits(tag);
507 swf_GetMatrix(tag, NULL); //matrix
508 swf_ResetReadBits(tag);
509 glyphbits = swf_GetU8(tag); //glyphbits
510 advancebits = swf_GetU8(tag); //advancebits
513 swf_ResetReadBits(tag);
514 flags = swf_GetBits(tag, 8);
516 if(flags & 128) // text style record
518 swf_ResetReadBits(tag);
519 if(flags & 8) { // hasfont
520 callback(tag, tag->pos + base, callback_data);
521 id = swf_GetU16(tag);
523 if(flags & 4) { // hascolor
524 if(num==1) swf_GetRGB(tag, NULL);
525 else swf_GetRGBA(tag, NULL);
527 if(flags & 2) { //has x offset
528 swf_ResetReadBits(tag);
531 if(flags & 1) { //has y offset
532 swf_ResetReadBits(tag);
535 if(flags & 8) { //has height
536 swf_ResetReadBits(tag);
539 } else { // glyph record
541 swf_ResetReadBits(tag);
542 for(t=0;t<flags;t++) {
543 swf_GetBits(tag, glyphbits);
544 swf_GetBits(tag, advancebits);
550 case ST_DEFINEFONTINFO:
551 callback(tag, tag->pos + base, callback_data);
554 //case ST_DEFINEMORPHSHAPE: /* disabled for now (doesn't work) */
556 case ST_DEFINESHAPE3: // these thingies might have bitmap ids in their fillstyles
558 case ST_DEFINESHAPE2:
560 case ST_DEFINESHAPE: {
565 if(tag->id == ST_DEFINEMORPHSHAPE)
568 id = swf_GetU16(tag); // id;
569 swf_GetRect(tag, NULL); // bounds
571 swf_GetRect(tag, NULL); // bounds2
572 swf_GetU32(tag); //offset to endedges
575 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
577 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
578 DEBUG_ENUMERATE printf("-------\n");
579 while(--morph>=0) /* morph shapes define two shapes */
581 fillbits = swf_GetBits(tag, 4);
582 linebits = swf_GetBits(tag, 4);
583 DEBUG_ENUMERATE printf("%d %d\n", fillbits, linebits);
584 swf_ResetReadBits(tag);
587 flags = swf_GetBits(tag, 1);
588 if(!flags) { //style change
589 flags = swf_GetBits(tag, 5);
593 int n = swf_GetBits(tag, 5);
595 x = swf_GetBits(tag, n); //x
596 y = swf_GetBits(tag, n); //y
597 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
599 if(flags&2) { //fill0
601 fill0 = swf_GetBits(tag, fillbits);
602 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
604 if(flags&4) { //fill1
606 fill1 = swf_GetBits(tag, fillbits);
607 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
609 if(flags&8) { //linestyle
611 line = swf_GetBits(tag, linebits);
612 DEBUG_ENUMERATE printf("linestyle %d\n",line);
615 DEBUG_ENUMERATE printf("more fillstyles\n");
616 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
617 fillbits = swf_GetBits(tag, 4);
618 linebits = swf_GetBits(tag, 4);
621 flags = swf_GetBits(tag, 1);
622 if(flags) { //straight edge
623 int n = swf_GetBits(tag, 4) + 2;
624 if(swf_GetBits(tag, 1)) { //line flag
626 x = swf_GetSBits(tag, n); //delta x
627 y = swf_GetSBits(tag, n); //delta y
628 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
630 int v=swf_GetBits(tag, 1);
632 d = swf_GetSBits(tag, n); //vert/horz
633 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
635 } else { //curved edge
636 int n = swf_GetBits(tag, 4) + 2;
638 x1 = swf_GetSBits(tag, n);
639 y1 = swf_GetSBits(tag, n);
640 x2 = swf_GetSBits(tag, n);
641 y2 = swf_GetSBits(tag, n);
642 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
654 void callbackCount(TAG * t,int pos, void*ptr)
657 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
660 void callbackFillin(TAG * t,int pos, void*ptr)
664 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
667 int swf_GetNumUsedIDs(TAG * t)
670 enumerateUsedIDs(t, 0, callbackCount, &num);
674 void swf_GetUsedIDs(TAG * t, int * positions)
676 int * ptr = positions;
677 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
680 void swf_Relocate (SWF*swf, char*bitmap)
684 memset(slaveids, -1, sizeof(slaveids));
692 if(swf_isDefiningTag(tag))
697 id = swf_GetDefineID(tag); //own id
699 if(!bitmap[id]) { //free
704 for (t=1;t<65536;t++)
714 slaveids[id] = newid;
716 swf_SetDefineID(tag, newid);
719 num = swf_GetNumUsedIDs(tag);
720 ptr = malloc(sizeof(int)*num);
721 swf_GetUsedIDs(tag, ptr);
724 int id = GET16(&tag->data[ptr[t]]);
726 fprintf(stderr, "swf_Relocate: Mapping id never encountered before: %d\n", id);
730 PUT16(&tag->data[ptr[t]], id);