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 int swf_GetDepth(TAG * t)
299 oldTagPos = swf_GetTagPos(t);
302 switch (swf_GetTagID(t))
303 { case ST_PLACEOBJECT:
304 case ST_REMOVEOBJECT:
306 depth = swf_GetU16(t);
308 case ST_REMOVEOBJECT2:
309 depth = swf_GetU16(t);
311 case ST_PLACEOBJECT2:
312 { U8 flags = swf_GetU8(t);
313 depth = swf_GetU16(t);
317 depth = swf_GetU16(t);
320 swf_SetTagPos(t,oldTagPos);
324 void swf_SetDepth(TAG * t, U16 depth)
326 switch (swf_GetTagID(t))
327 { case ST_PLACEOBJECT:
328 case ST_REMOVEOBJECT:
329 PUT16(t->data, depth);
331 case ST_REMOVEOBJECT2:
332 PUT16(t->data, depth);
334 case ST_PLACEOBJECT2:
335 PUT16(&t->data[1], depth);
338 PUT16(t->data, depth);
341 fprintf(stderr, "rfxswf: Error: tag %d has no depth\n", t->id);
345 char* swf_GetName(TAG * t)
351 oldTagPos = swf_GetTagPos(t);
353 switch(swf_GetTagID(t))
356 name = &t->data[swf_GetTagPos(t)];
358 case ST_PLACEOBJECT2: {
359 U8 flags = swf_GetU8(t);
360 swf_GetU16(t); //depth;
364 swf_GetMatrix(t, &m);
366 swf_GetCXForm(t, &c, 1);
369 if(flags&PF_CLIPACTION)
372 swf_ResetReadBits(t);
373 name = &t->data[swf_GetTagPos(t)];
378 swf_SetTagPos(t,oldTagPos);
382 /* used in enumerateUsedIDs */
383 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
393 gradient2->num = swf_GetU8(tag);
394 for(t=0;t<gradient1->num;t++)
399 gradient1->ratios[t] = swf_GetU8(tag);
400 swf_GetRGBA(tag, &gradient1->rgba[t]);
401 gradient2->ratios[t] = swf_GetU8(tag);
402 swf_GetRGBA(tag, &gradient2->rgba[t]);
406 #define DEBUG_ENUMERATE if(0)
408 static void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
412 count = swf_GetU8(tag);
413 if(count == 0xff && num>1) // defineshape2,3 only
414 count = swf_GetU16(tag);
420 swf_ResetReadBits(tag);
421 type = swf_GetU8(tag); //type
424 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
426 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
428 else if(type == 0x10 || type == 0x12)
430 swf_ResetReadBits(tag);
431 swf_GetMatrix(tag, NULL);
433 swf_GetMatrix(tag, NULL);
434 swf_ResetReadBits(tag);
436 swf_GetMorphGradient(tag, NULL, NULL);
438 swf_GetGradient(tag, NULL, /*alpha*/ num>=3?1:0);
440 else if(type == 0x40 || type == 0x41)
442 swf_ResetReadBits(tag);
444 if(tag->data[tag->pos] != 0xff ||
445 tag->data[tag->pos+1] != 0xff)
446 (callback)(tag, tag->pos, callback_data);
449 swf_ResetReadBits(tag);
450 swf_GetMatrix(tag, NULL);
452 swf_GetMatrix(tag, NULL);
455 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
458 swf_ResetReadBits(tag);
459 count = swf_GetU8(tag); // line style array
461 count = swf_GetU16(tag);
468 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
470 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
474 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
477 swf_ResetReadBits(tag);
481 case ST_DEFINEBUTTONCXFORM: {
483 callback(tag, tag->pos + base, callback_data);
486 callback(tag, tag->pos + base, callback_data);
487 swf_GetU16(tag); //sound id
488 flags = swf_GetU8(tag);
490 swf_GetU32(tag); // in point
492 swf_GetU32(tag); // out points
494 swf_GetU16(tag); // loop count
497 int npoints = swf_GetU8(tag);
499 for(s=0;s<npoints;s++)
508 case ST_DEFINEBUTTONSOUND:
509 callback(tag, tag->pos + base, callback_data); //button id
512 case ST_EXPORTASSETS: {
513 int num = swf_GetU16(tag);
516 callback(tag, tag->pos + base, callback_data); //button id
517 swf_GetU16(tag); //id
518 while(swf_GetU8(tag)); //name
522 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
523 case ST_NAMECHARACTER:
524 case ST_GENERATORTEXT:
525 callback(tag, tag->pos + base, callback_data);
528 callback(tag, tag->pos + base, callback_data);
530 case ST_PLACEOBJECT2:
531 // only if placeflaghascharacter
532 if(!(tag->data[0]&2))
534 callback(tag, 3 + base, callback_data);
536 case ST_REMOVEOBJECT:
537 callback(tag, tag->pos + base, callback_data);
540 callback(tag, tag->pos + base, callback_data);
542 case ST_DEFINESPRITE: {
544 break; // sprite is expanded
546 swf_GetU16(tag); // id
547 swf_GetU16(tag); // framenum
550 U16 flags = swf_GetU16(tag);
553 TAG *tag2 = swf_InsertTag(NULL, id);
556 len = swf_GetU32(tag);
559 tag2->len = tag2->memsize = len;
560 tag2->data = malloc(len);
561 memcpy(tag2->data, &tag->data[tag->pos], len);
562 /* I never saw recursive sprites, but they are (theoretically)
563 possible, so better add base here again */
564 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
566 swf_GetBlock(tag, NULL, len);
570 case ST_DEFINEBUTTON2: // has some font ids in the button records
573 case ST_DEFINEBUTTON: {
574 swf_GetU16(tag); //button id
578 swf_GetU8(tag); //flag
579 offset = swf_GetU16(tag); //offset
584 if(!swf_GetU8(tag)) //flags
586 callback(tag, tag->pos + base, callback_data);
587 swf_GetU16(tag); //char
588 swf_GetU16(tag); //layer
589 swf_ResetReadBits(tag);
590 swf_GetMatrix(tag, NULL);
592 swf_ResetReadBits(tag);
593 swf_GetCXForm(tag, NULL, 1);
599 case ST_DEFINEEDITTEXT: {
601 swf_GetU16(tag); //id
602 swf_GetRect(tag, NULL); //bounding box
603 swf_ResetReadBits(tag);
604 flags1 = swf_GetU8(tag);
605 flags2 = swf_GetU8(tag);
607 callback(tag, tag->pos + base, callback_data);
612 case ST_DEFINETEXT: {
613 int glyphbits, advancebits;
615 id = swf_GetU16(tag); //id
616 swf_GetRect(tag, NULL); //bounding box
617 swf_ResetReadBits(tag);
618 swf_GetMatrix(tag, NULL); //matrix
619 swf_ResetReadBits(tag);
620 glyphbits = swf_GetU8(tag); //glyphbits
621 advancebits = swf_GetU8(tag); //advancebits
624 swf_ResetReadBits(tag);
625 flags = swf_GetBits(tag, 8);
627 if(flags & 128) // text style record
629 swf_ResetReadBits(tag);
630 if(flags & 8) { // hasfont
631 callback(tag, tag->pos + base, callback_data);
632 id = swf_GetU16(tag);
634 if(flags & 4) { // hascolor
635 if(num==1) swf_GetRGB(tag, NULL);
636 else swf_GetRGBA(tag, NULL);
638 if(flags & 2) { //has x offset
639 swf_ResetReadBits(tag);
642 if(flags & 1) { //has y offset
643 swf_ResetReadBits(tag);
646 if(flags & 8) { //has height
647 swf_ResetReadBits(tag);
650 } else { // glyph record
652 swf_ResetReadBits(tag);
653 for(t=0;t<flags;t++) {
654 swf_GetBits(tag, glyphbits);
655 swf_GetBits(tag, advancebits);
661 case ST_DEFINEFONTINFO:
662 case ST_DEFINEFONTINFO2:
664 callback(tag, tag->pos + base, callback_data);
666 case ST_DEFINEVIDEOSTREAM:
669 case ST_DOINITACTION:
670 callback(tag, tag->pos + base, callback_data);
673 case ST_DEFINEMORPHSHAPE:
674 case ST_DEFINESHAPE3:
676 case ST_DEFINESHAPE2:
678 case ST_DEFINESHAPE: {
684 if(tag->id == ST_DEFINEMORPHSHAPE) {
689 id = swf_GetU16(tag); // id;
690 swf_GetRect(tag, NULL); // bounds
692 swf_ResetReadBits(tag);
693 swf_GetRect(tag, NULL); // bounds2
694 swf_GetU32(tag); //offset to endedges
697 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
699 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
700 DEBUG_ENUMERATE printf("-------\n");
701 while(--numshapes>=0) /* morph shapes define two shapes */
703 DEBUG_ENUMERATE printf("shape:%d\n", numshapes);
704 fillbits = swf_GetBits(tag, 4);
705 linebits = swf_GetBits(tag, 4);
706 DEBUG_ENUMERATE printf("%d %d\n", fillbits, linebits);
707 swf_ResetReadBits(tag);
710 flags = swf_GetBits(tag, 1);
711 if(!flags) { //style change
712 flags = swf_GetBits(tag, 5);
716 int n = swf_GetBits(tag, 5);
718 x = swf_GetBits(tag, n); //x
719 y = swf_GetBits(tag, n); //y
720 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
722 if(flags&2) { //fill0
724 fill0 = swf_GetBits(tag, fillbits);
725 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
727 if(flags&4) { //fill1
729 fill1 = swf_GetBits(tag, fillbits);
730 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
732 if(flags&8) { //linestyle
734 line = swf_GetBits(tag, linebits);
735 DEBUG_ENUMERATE printf("linestyle %d\n",line);
738 DEBUG_ENUMERATE printf("more fillstyles\n");
739 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
740 fillbits = swf_GetBits(tag, 4);
741 linebits = swf_GetBits(tag, 4);
744 flags = swf_GetBits(tag, 1);
745 if(flags) { //straight edge
746 int n = swf_GetBits(tag, 4) + 2;
747 if(swf_GetBits(tag, 1)) { //line flag
749 x = swf_GetSBits(tag, n); //delta x
750 y = swf_GetSBits(tag, n); //delta y
751 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
753 int v=swf_GetBits(tag, 1);
755 d = swf_GetSBits(tag, n); //vert/horz
756 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
758 } else { //curved edge
759 int n = swf_GetBits(tag, 4) + 2;
761 x1 = swf_GetSBits(tag, n);
762 y1 = swf_GetSBits(tag, n);
763 x2 = swf_GetSBits(tag, n);
764 y2 = swf_GetSBits(tag, n);
765 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
777 void callbackCount(TAG * t,int pos, void*ptr)
780 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
783 void callbackFillin(TAG * t,int pos, void*ptr)
787 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
790 int swf_GetNumUsedIDs(TAG * t)
793 enumerateUsedIDs(t, 0, callbackCount, &num);
797 void swf_GetUsedIDs(TAG * t, int * positions)
799 int * ptr = positions;
800 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
803 void swf_Relocate (SWF*swf, char*bitmap)
807 memset(slaveids, -1, sizeof(slaveids));
815 if(swf_isDefiningTag(tag))
820 id = swf_GetDefineID(tag); //own id
822 if(!bitmap[id]) { //free
827 for (t=1;t<65536;t++)
837 slaveids[id] = newid;
839 swf_SetDefineID(tag, newid);
842 num = swf_GetNumUsedIDs(tag);
843 ptr = malloc(sizeof(int)*num);
844 swf_GetUsedIDs(tag, ptr);
847 int id = GET16(&tag->data[ptr[t]]);
849 fprintf(stderr, "swf_Relocate: Mapping id never encountered before: %d\n", id);
853 PUT16(&tag->data[ptr[t]], id);
859 void swf_RelocateDepth(SWF*swf, char*bitmap)
864 for(nr=65535;nr>=0;nr--) {
868 // now nr is the highest used depth. So we start
869 // assigning depths at nr+1
874 int depth = swf_GetDepth(tag);
876 int newdepth = depth+nr;
878 fprintf(stderr, "Couldn't relocate depths: too large values\n");
881 swf_SetDepth(tag, newdepth);
887 TAG* swf_Concatenate (TAG*list1,TAG*list2)
889 TAG*tag=0,*lasttag=0;
891 char depthmap[65536];
892 memset(bitmap, 0, sizeof(bitmap));
893 memset(depthmap, 0, sizeof(depthmap));
895 memset(&swf1, 0, sizeof(swf1));
896 memset(&swf2, 0, sizeof(swf2));
898 swf1.firstTag = list1;
900 swf2.firstTag = list2;
905 if(!swf_isDefiningTag(tag)) {
906 int id = swf_GetDefineID(tag);
909 if(tag->id == ST_PLACEOBJECT ||
910 tag->id == ST_PLACEOBJECT2) {
911 int depth = swf_GetDepth(tag);
914 if(tag->id == ST_REMOVEOBJECT ||
915 tag->id == ST_REMOVEOBJECT2) {
916 int depth = swf_GetDepth(tag);
922 swf_Relocate(&swf2, bitmap);
923 swf_RelocateDepth(&swf2, depthmap);
924 lasttag->next = swf2.firstTag;
925 swf2.firstTag->prev = lasttag;
927 return swf1.firstTag;