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 program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 // Matrix & Math tools for SWF files
27 SFIXED RFXSWF_SP(SFIXED a1,SFIXED a2,SFIXED b1,SFIXED b2)
29 a = (S64)a1*(S64)b1+(S64)a2*(S64)b2;
30 return (SFIXED)(a>>16);
32 SFIXED RFXSWF_QFIX(int zaehler,int nenner) // bildet Quotient von zwei INTs in SFIXED
33 { S64 z = zaehler<<16;
34 S64 a = z/(S64)nenner;
39 MATRIX * swf_MatrixJoin(MATRIX * d,MATRIX * s1,MATRIX * s2)
42 if (!s1) return (s2)?(MATRIX *)memcpy(d,s2,sizeof(MATRIX)):NULL;
43 if (!s2) return (MATRIX *)memcpy(d,s1,sizeof(MATRIX));
45 d->tx = s1->tx + s2->tx;
46 d->ty = s1->ty + s2->ty;
48 d->sx = RFXSWF_SP(s1->sx,s1->r1,s2->sx,s2->r0);
49 d->sy = RFXSWF_SP(s1->r0,s1->sy,s2->r1,s2->sy);
50 d->r0 = RFXSWF_SP(s1->r0,s1->sy,s2->sx,s2->r0);
51 d->r1 = RFXSWF_SP(s1->sx,s1->r1,s2->r1,s2->sy);
58 MATRIX * swf_MatrixMapTriangle(MATRIX * m,int dx,int dy,int x0,int y0,
59 int x1,int y1,int x2,int y2)
66 if ((!dx)||(!dy)) return NULL; // check DIV by zero
70 m->sx = RFXSWF_QFIX(dx1,dx);
71 m->sy = RFXSWF_QFIX(dy2,dy);
72 m->r0 = RFXSWF_QFIX(dy1,dx);
73 m->r1 = RFXSWF_QFIX(dx2,dy);
78 void swf_SetDefineID(TAG * tag, U16 newid)
80 int oldlen = tag->len;
82 swf_SetU16(tag, newid); /* set defining ID */
86 U16 swf_GetDefineID(TAG * t)
91 oldTagPos = swf_GetTagPos(t);
94 switch (swf_GetTagID(t))
95 { case ST_DEFINESHAPE:
98 case ST_DEFINEMORPHSHAPE:
99 case ST_DEFINEEDITTEXT:
101 case ST_DEFINEBITSJPEG2:
102 case ST_DEFINEBITSJPEG3:
103 case ST_DEFINEBITSLOSSLESS:
104 case ST_DEFINEBITSLOSSLESS2:
105 case ST_DEFINEBUTTON:
106 case ST_DEFINEBUTTON2:
107 case ST_DEFINEBUTTONCXFORM: //pseudodefine
108 case ST_DEFINEBUTTONSOUND: //pseudodefine
111 case ST_DEFINEFONTINFO: //pseudodefine
112 case ST_DEFINEFONTINFO2: //pseudodefine
116 case ST_DEFINESPRITE:
118 case ST_DEFINEVIDEOSTREAM:
119 case ST_VIDEOFRAME: //pseudodefine
120 case ST_NAMECHARACTER: //pseudodefine
125 swf_SetTagPos(t,oldTagPos);
130 SRECT swf_GetDefineBBox(TAG * t)
136 oldTagPos = swf_GetTagPos(t);
141 switch (swf_GetTagID(t))
142 { case ST_DEFINESHAPE:
143 case ST_DEFINESHAPE2:
144 case ST_DEFINESHAPE3:
145 case ST_DEFINEEDITTEXT:
146 case ST_DEFINEBUTTON:
147 case ST_DEFINEBUTTON2:
150 case ST_DEFINEVIDEOSTREAM:
154 case ST_DEFINEMORPHSHAPE:
158 swf_ExpandRect2(&b1, &b2);
160 case ST_DEFINEBITSLOSSLESS:
161 case ST_DEFINEBITSLOSSLESS2:
163 case ST_DEFINEBITSJPEG2:
164 case ST_DEFINEBITSJPEG3:
169 swf_SetTagPos(t,oldTagPos);
174 U16 swf_GetPlaceID(TAG * t)
179 oldTagPos = swf_GetTagPos(t);
182 switch (swf_GetTagID(t))
183 { case ST_PLACEOBJECT:
184 case ST_REMOVEOBJECT:
185 case ST_FREECHARACTER:
190 case ST_PLACEOBJECT2:
191 { U8 flags = swf_GetU8(t);
192 U16 d = swf_GetU16(t);
193 id = (flags&PF_CHAR)?swf_GetU16(t):id;
198 swf_SetTagPos(t,oldTagPos);
203 static int swf_definingtagids[] =
216 ST_DEFINEBITSLOSSLESS,
217 ST_DEFINEBITSLOSSLESS2,
223 ST_DEFINEVIDEOSTREAM,
227 // tags which may be used inside a sprite definition
228 static int swf_spritetagids[] =
233 ST_REMOVEOBJECT2, //?
244 static int swf_pseudodefiningtagids[] =
248 ST_DEFINEBUTTONCXFORM,
249 ST_DEFINEBUTTONSOUND,
256 U8 swf_isAllowedSpriteTag(TAG * tag)
260 while(swf_spritetagids[t]>=0)
262 if(swf_spritetagids[t] == id)
269 U8 swf_isDefiningTag(TAG * tag)
273 while(swf_definingtagids[t]>=0)
275 if(swf_definingtagids[t] == id)
282 U8 swf_isPseudoDefiningTag(TAG * tag)
286 while(swf_pseudodefiningtagids[t]>=0)
288 if(swf_pseudodefiningtagids[t] == id)
295 U16 swf_GetDepth(TAG * t)
300 oldTagPos = swf_GetTagPos(t);
303 switch (swf_GetTagID(t))
304 { case ST_PLACEOBJECT:
305 case ST_REMOVEOBJECT:
307 depth = swf_GetU16(t);
309 case ST_REMOVEOBJECT2:
310 depth = swf_GetU16(t);
312 case ST_PLACEOBJECT2:
313 { U8 flags = swf_GetU8(t);
314 depth = swf_GetU16(t);
317 swf_SetTagPos(t,oldTagPos);
321 char* swf_GetName(TAG * t)
327 oldTagPos = swf_GetTagPos(t);
329 switch(swf_GetTagID(t))
332 name = &t->data[swf_GetTagPos(t)];
334 case ST_PLACEOBJECT2: {
335 U8 flags = swf_GetU8(t);
336 swf_GetU16(t); //depth;
340 swf_GetMatrix(t, &m);
342 swf_GetCXForm(t, &c, 1);
345 if(flags&PF_CLIPACTION)
348 swf_ResetReadBits(t);
349 name = &t->data[swf_GetTagPos(t)];
354 swf_SetTagPos(t,oldTagPos);
358 /* used in enumerateUsedIDs */
359 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
369 gradient2->num = swf_GetU8(tag);
370 for(t=0;t<gradient1->num;t++)
375 gradient1->ratios[t] = swf_GetU8(tag);
376 swf_GetRGBA(tag, &gradient1->rgba[t]);
377 gradient2->ratios[t] = swf_GetU8(tag);
378 swf_GetRGBA(tag, &gradient2->rgba[t]);
382 #define DEBUG_ENUMERATE if(0)
384 static void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
388 count = swf_GetU8(tag);
389 if(count == 0xff && num>1) // defineshape2,3 only
390 count = swf_GetU16(tag);
396 swf_ResetReadBits(tag);
397 type = swf_GetU8(tag); //type
400 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
402 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
404 else if(type == 0x10 || type == 0x12)
406 swf_ResetReadBits(tag);
407 swf_GetMatrix(tag, NULL);
409 swf_GetMatrix(tag, NULL);
410 swf_ResetReadBits(tag);
412 swf_GetMorphGradient(tag, NULL, NULL);
414 swf_GetGradient(tag, NULL, /*alpha*/ num>=3?1:0);
416 else if(type == 0x40 || type == 0x41)
418 swf_ResetReadBits(tag);
420 if(tag->data[tag->pos] != 0xff ||
421 tag->data[tag->pos+1] != 0xff)
422 (callback)(tag, tag->pos, callback_data);
425 swf_ResetReadBits(tag);
426 swf_GetMatrix(tag, NULL);
428 swf_GetMatrix(tag, NULL);
431 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
434 swf_ResetReadBits(tag);
435 count = swf_GetU8(tag); // line style array
437 count = swf_GetU16(tag);
444 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
446 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
450 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
453 swf_ResetReadBits(tag);
457 case ST_DEFINEBUTTONCXFORM: {
459 callback(tag, tag->pos + base, callback_data);
462 callback(tag, tag->pos + base, callback_data);
463 swf_GetU16(tag); //sound id
464 flags = swf_GetU8(tag);
466 swf_GetU32(tag); // in point
468 swf_GetU32(tag); // out points
470 swf_GetU16(tag); // loop count
473 int npoints = swf_GetU8(tag);
475 for(s=0;s<npoints;s++)
484 case ST_DEFINEBUTTONSOUND:
485 callback(tag, tag->pos + base, callback_data); //button id
488 case ST_EXPORTASSETS: {
489 int num = swf_GetU16(tag);
492 callback(tag, tag->pos + base, callback_data); //button id
493 swf_GetU16(tag); //id
494 while(swf_GetU8(tag)); //name
498 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
499 case ST_NAMECHARACTER:
500 case ST_GENERATORTEXT:
501 callback(tag, tag->pos + base, callback_data);
504 callback(tag, tag->pos + base, callback_data);
506 case ST_PLACEOBJECT2:
507 // only if placeflaghascharacter
508 if(!(tag->data[0]&2))
510 callback(tag, 3 + base, callback_data);
512 case ST_REMOVEOBJECT:
513 callback(tag, tag->pos + base, callback_data);
516 callback(tag, tag->pos + base, callback_data);
518 case ST_DEFINESPRITE: {
520 break; // sprite is expanded
522 swf_GetU16(tag); // id
523 swf_GetU16(tag); // framenum
526 U16 flags = swf_GetU16(tag);
529 TAG *tag2 = swf_InsertTag(NULL, id);
532 len = swf_GetU32(tag);
535 tag2->len = tag2->memsize = len;
536 tag2->data = malloc(len);
537 memcpy(tag2->data, &tag->data[tag->pos], len);
538 /* I never saw recursive sprites, but they are (theoretically)
539 possible, so better add base here again */
540 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
542 swf_GetBlock(tag, NULL, len);
546 case ST_DEFINEBUTTON2: // has some font ids in the button records
549 case ST_DEFINEBUTTON: {
550 swf_GetU16(tag); //button id
554 swf_GetU8(tag); //flag
555 offset = swf_GetU16(tag); //offset
560 if(!swf_GetU8(tag)) //flags
562 callback(tag, tag->pos + base, callback_data);
563 swf_GetU16(tag); //char
564 swf_GetU16(tag); //layer
565 swf_ResetReadBits(tag);
566 swf_GetMatrix(tag, NULL);
568 swf_ResetReadBits(tag);
569 swf_GetCXForm(tag, NULL, 1);
575 case ST_DEFINEEDITTEXT: {
577 swf_GetU16(tag); //id
578 swf_GetRect(tag, NULL); //bounding box
579 swf_ResetReadBits(tag);
580 flags1 = swf_GetU8(tag);
581 flags2 = swf_GetU8(tag);
583 callback(tag, tag->pos + base, callback_data);
588 case ST_DEFINETEXT: {
589 int glyphbits, advancebits;
591 id = swf_GetU16(tag); //id
592 swf_GetRect(tag, NULL); //bounding box
593 swf_ResetReadBits(tag);
594 swf_GetMatrix(tag, NULL); //matrix
595 swf_ResetReadBits(tag);
596 glyphbits = swf_GetU8(tag); //glyphbits
597 advancebits = swf_GetU8(tag); //advancebits
600 swf_ResetReadBits(tag);
601 flags = swf_GetBits(tag, 8);
603 if(flags & 128) // text style record
605 swf_ResetReadBits(tag);
606 if(flags & 8) { // hasfont
607 callback(tag, tag->pos + base, callback_data);
608 id = swf_GetU16(tag);
610 if(flags & 4) { // hascolor
611 if(num==1) swf_GetRGB(tag, NULL);
612 else swf_GetRGBA(tag, NULL);
614 if(flags & 2) { //has x offset
615 swf_ResetReadBits(tag);
618 if(flags & 1) { //has y offset
619 swf_ResetReadBits(tag);
622 if(flags & 8) { //has height
623 swf_ResetReadBits(tag);
626 } else { // glyph record
628 swf_ResetReadBits(tag);
629 for(t=0;t<flags;t++) {
630 swf_GetBits(tag, glyphbits);
631 swf_GetBits(tag, advancebits);
637 case ST_DEFINEFONTINFO:
638 case ST_DEFINEFONTINFO2:
640 callback(tag, tag->pos + base, callback_data);
642 case ST_DEFINEVIDEOSTREAM:
645 case ST_DOINITACTION:
646 callback(tag, tag->pos + base, callback_data);
649 case ST_DEFINEMORPHSHAPE:
650 case ST_DEFINESHAPE3:
652 case ST_DEFINESHAPE2:
654 case ST_DEFINESHAPE: {
660 if(tag->id == ST_DEFINEMORPHSHAPE) {
665 id = swf_GetU16(tag); // id;
666 swf_GetRect(tag, NULL); // bounds
668 swf_ResetReadBits(tag);
669 swf_GetRect(tag, NULL); // bounds2
670 swf_GetU32(tag); //offset to endedges
673 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
675 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
676 DEBUG_ENUMERATE printf("-------\n");
677 while(--numshapes>=0) /* morph shapes define two shapes */
679 DEBUG_ENUMERATE printf("shape:%d\n", numshapes);
680 fillbits = swf_GetBits(tag, 4);
681 linebits = swf_GetBits(tag, 4);
682 DEBUG_ENUMERATE printf("%d %d\n", fillbits, linebits);
683 swf_ResetReadBits(tag);
686 flags = swf_GetBits(tag, 1);
687 if(!flags) { //style change
688 flags = swf_GetBits(tag, 5);
692 int n = swf_GetBits(tag, 5);
694 x = swf_GetBits(tag, n); //x
695 y = swf_GetBits(tag, n); //y
696 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
698 if(flags&2) { //fill0
700 fill0 = swf_GetBits(tag, fillbits);
701 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
703 if(flags&4) { //fill1
705 fill1 = swf_GetBits(tag, fillbits);
706 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
708 if(flags&8) { //linestyle
710 line = swf_GetBits(tag, linebits);
711 DEBUG_ENUMERATE printf("linestyle %d\n",line);
714 DEBUG_ENUMERATE printf("more fillstyles\n");
715 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
716 fillbits = swf_GetBits(tag, 4);
717 linebits = swf_GetBits(tag, 4);
720 flags = swf_GetBits(tag, 1);
721 if(flags) { //straight edge
722 int n = swf_GetBits(tag, 4) + 2;
723 if(swf_GetBits(tag, 1)) { //line flag
725 x = swf_GetSBits(tag, n); //delta x
726 y = swf_GetSBits(tag, n); //delta y
727 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
729 int v=swf_GetBits(tag, 1);
731 d = swf_GetSBits(tag, n); //vert/horz
732 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
734 } else { //curved edge
735 int n = swf_GetBits(tag, 4) + 2;
737 x1 = swf_GetSBits(tag, n);
738 y1 = swf_GetSBits(tag, n);
739 x2 = swf_GetSBits(tag, n);
740 y2 = swf_GetSBits(tag, n);
741 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
753 void callbackCount(TAG * t,int pos, void*ptr)
756 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
759 void callbackFillin(TAG * t,int pos, void*ptr)
763 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
766 int swf_GetNumUsedIDs(TAG * t)
769 enumerateUsedIDs(t, 0, callbackCount, &num);
773 void swf_GetUsedIDs(TAG * t, int * positions)
775 int * ptr = positions;
776 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
779 void swf_Relocate (SWF*swf, char*bitmap)
783 memset(slaveids, -1, sizeof(slaveids));
791 if(swf_isDefiningTag(tag))
796 id = swf_GetDefineID(tag); //own id
798 if(!bitmap[id]) { //free
803 for (t=1;t<65536;t++)
813 slaveids[id] = newid;
815 swf_SetDefineID(tag, newid);
818 num = swf_GetNumUsedIDs(tag);
819 ptr = malloc(sizeof(int)*num);
820 swf_GetUsedIDs(tag, ptr);
823 int id = GET16(&tag->data[ptr[t]]);
825 fprintf(stderr, "swf_Relocate: Mapping id never encountered before: %d\n", id);
829 PUT16(&tag->data[ptr[t]], id);