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_FONTNAMES: //pseudodefine
120 case ST_VIDEOFRAME: //pseudodefine
121 case ST_NAMECHARACTER: //pseudodefine
126 swf_SetTagPos(t,oldTagPos);
131 SRECT swf_GetDefineBBox(TAG * t)
137 oldTagPos = swf_GetTagPos(t);
142 switch (swf_GetTagID(t))
143 { case ST_DEFINESHAPE:
144 case ST_DEFINESHAPE2:
145 case ST_DEFINESHAPE3:
146 case ST_DEFINEEDITTEXT:
149 case ST_DEFINEVIDEOSTREAM:
153 case ST_DEFINEMORPHSHAPE:
157 swf_ExpandRect2(&b1, &b2);
159 case ST_DEFINEBITSLOSSLESS:
160 case ST_DEFINEBITSLOSSLESS2:
162 case ST_DEFINEBITSJPEG2:
163 case ST_DEFINEBITSJPEG3:
168 swf_SetTagPos(t,oldTagPos);
173 U16 swf_GetPlaceID(TAG * t)
178 oldTagPos = swf_GetTagPos(t);
181 switch (swf_GetTagID(t))
182 { case ST_PLACEOBJECT:
183 case ST_REMOVEOBJECT:
184 case ST_FREECHARACTER:
189 case ST_PLACEOBJECT2:
190 { U8 flags = swf_GetU8(t);
191 U16 d = swf_GetU16(t);
192 id = (flags&PF_CHAR)?swf_GetU16(t):id;
197 swf_SetTagPos(t,oldTagPos);
202 static int swf_definingtagids[] =
215 ST_DEFINEBITSLOSSLESS,
216 ST_DEFINEBITSLOSSLESS2,
222 ST_DEFINEVIDEOSTREAM,
226 // tags which may be used inside a sprite definition
227 static int swf_spritetagids[] =
232 ST_REMOVEOBJECT2, //?
243 static int swf_pseudodefiningtagids[] =
247 ST_DEFINEBUTTONCXFORM,
248 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);
662 case ST_DEFINEFONTINFO:
663 case ST_DEFINEFONTINFO2:
665 callback(tag, tag->pos + base, callback_data);
667 case ST_DEFINEVIDEOSTREAM:
670 case ST_DOINITACTION:
671 callback(tag, tag->pos + base, callback_data);
674 case ST_DEFINEMORPHSHAPE:
675 case ST_DEFINESHAPE3:
677 case ST_DEFINESHAPE2:
679 case ST_DEFINESHAPE: {
685 if(tag->id == ST_DEFINEMORPHSHAPE) {
690 id = swf_GetU16(tag); // id;
691 swf_GetRect(tag, NULL); // bounds
693 swf_ResetReadBits(tag);
694 swf_GetRect(tag, NULL); // bounds2
695 swf_GetU32(tag); //offset to endedges
698 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
700 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
701 DEBUG_ENUMERATE printf("-------\n");
702 while(--numshapes>=0) /* morph shapes define two shapes */
704 DEBUG_ENUMERATE printf("shape:%d\n", numshapes);
705 fillbits = swf_GetBits(tag, 4);
706 linebits = swf_GetBits(tag, 4);
707 DEBUG_ENUMERATE printf("%d %d\n", fillbits, linebits);
708 swf_ResetReadBits(tag);
711 flags = swf_GetBits(tag, 1);
712 if(!flags) { //style change
713 flags = swf_GetBits(tag, 5);
717 int n = swf_GetBits(tag, 5);
719 x = swf_GetBits(tag, n); //x
720 y = swf_GetBits(tag, n); //y
721 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
723 if(flags&2) { //fill0
725 fill0 = swf_GetBits(tag, fillbits);
726 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
728 if(flags&4) { //fill1
730 fill1 = swf_GetBits(tag, fillbits);
731 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
733 if(flags&8) { //linestyle
735 line = swf_GetBits(tag, linebits);
736 DEBUG_ENUMERATE printf("linestyle %d\n",line);
739 DEBUG_ENUMERATE printf("more fillstyles\n");
740 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
741 fillbits = swf_GetBits(tag, 4);
742 linebits = swf_GetBits(tag, 4);
745 flags = swf_GetBits(tag, 1);
746 if(flags) { //straight edge
747 int n = swf_GetBits(tag, 4) + 2;
748 if(swf_GetBits(tag, 1)) { //line flag
750 x = swf_GetSBits(tag, n); //delta x
751 y = swf_GetSBits(tag, n); //delta y
752 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
754 int v=swf_GetBits(tag, 1);
756 d = swf_GetSBits(tag, n); //vert/horz
757 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
759 } else { //curved edge
760 int n = swf_GetBits(tag, 4) + 2;
762 x1 = swf_GetSBits(tag, n);
763 y1 = swf_GetSBits(tag, n);
764 x2 = swf_GetSBits(tag, n);
765 y2 = swf_GetSBits(tag, n);
766 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
778 void callbackCount(TAG * t,int pos, void*ptr)
781 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
784 void callbackFillin(TAG * t,int pos, void*ptr)
788 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
791 int swf_GetNumUsedIDs(TAG * t)
794 enumerateUsedIDs(t, 0, callbackCount, &num);
798 void swf_GetUsedIDs(TAG * t, int * positions)
800 int * ptr = positions;
801 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
804 void swf_Relocate (SWF*swf, char*bitmap)
808 memset(slaveids, -1, sizeof(slaveids));
816 if(swf_isDefiningTag(tag))
821 id = swf_GetDefineID(tag); //own id
823 if(!bitmap[id]) { //free
828 for (t=1;t<65536;t++)
838 slaveids[id] = newid;
840 swf_SetDefineID(tag, newid);
843 num = swf_GetNumUsedIDs(tag);
844 ptr = malloc(sizeof(int)*num);
845 swf_GetUsedIDs(tag, ptr);
848 int id = GET16(&tag->data[ptr[t]]);
850 fprintf(stderr, "swf_Relocate: Mapping id never encountered before: %d\n", id);
854 PUT16(&tag->data[ptr[t]], id);
860 void swf_RelocateDepth(SWF*swf, char*bitmap)
865 for(nr=65535;nr>=0;nr--) {
869 // now nr is the highest used depth. So we start
870 // assigning depths at nr+1
875 int depth = swf_GetDepth(tag);
877 int newdepth = depth+nr;
879 fprintf(stderr, "Couldn't relocate depths: too large values\n");
882 swf_SetDepth(tag, newdepth);
888 TAG* swf_Concatenate (TAG*list1,TAG*list2)
890 TAG*tag=0,*lasttag=0;
892 char depthmap[65536];
894 memset(bitmap, 0, sizeof(bitmap));
895 memset(depthmap, 0, sizeof(depthmap));
896 memset(&swf1, 0, sizeof(swf1));
897 memset(&swf2, 0, sizeof(swf2));
899 swf1.firstTag = list1;
901 swf2.firstTag = list2;
906 if(!swf_isDefiningTag(tag)) {
907 int id = swf_GetDefineID(tag);
910 if(tag->id == ST_PLACEOBJECT ||
911 tag->id == ST_PLACEOBJECT2) {
912 int depth = swf_GetDepth(tag);
915 if(tag->id == ST_REMOVEOBJECT ||
916 tag->id == ST_REMOVEOBJECT2) {
917 int depth = swf_GetDepth(tag);
923 swf_Relocate(&swf2, bitmap);
924 swf_RelocateDepth(&swf2, depthmap);
925 lasttag->next = swf2.firstTag;
926 swf2.firstTag->prev = lasttag;
928 return swf1.firstTag;