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_GLYPHNAMES: //pseudodefine
120 case ST_VIDEOFRAME: //pseudodefine
121 case ST_NAMECHARACTER: //pseudodefine
122 case ST_DOINITACTION: //pseudodefine
127 swf_SetTagPos(t,oldTagPos);
132 SRECT swf_GetDefineBBox(TAG * t)
138 oldTagPos = swf_GetTagPos(t);
143 switch (swf_GetTagID(t))
144 { case ST_DEFINESHAPE:
145 case ST_DEFINESHAPE2:
146 case ST_DEFINESHAPE3:
147 case ST_DEFINEEDITTEXT:
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,
257 U8 swf_isAllowedSpriteTag(TAG * tag)
261 while(swf_spritetagids[t]>=0)
263 if(swf_spritetagids[t] == id)
270 U8 swf_isDefiningTag(TAG * tag)
274 while(swf_definingtagids[t]>=0)
276 if(swf_definingtagids[t] == id)
283 U8 swf_isPseudoDefiningTag(TAG * tag)
287 while(swf_pseudodefiningtagids[t]>=0)
289 if(swf_pseudodefiningtagids[t] == id)
296 int 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);
318 depth = swf_GetU16(t);
321 swf_SetTagPos(t,oldTagPos);
325 void swf_SetDepth(TAG * t, U16 depth)
327 switch (swf_GetTagID(t))
328 { case ST_PLACEOBJECT:
329 case ST_REMOVEOBJECT:
330 PUT16(t->data, depth);
332 case ST_REMOVEOBJECT2:
333 PUT16(t->data, depth);
335 case ST_PLACEOBJECT2:
336 PUT16(&t->data[1], depth);
339 PUT16(t->data, depth);
342 fprintf(stderr, "rfxswf: Error: tag %d has no depth\n", t->id);
346 char* swf_GetName(TAG * t)
352 oldTagPos = swf_GetTagPos(t);
354 switch(swf_GetTagID(t))
357 name = &t->data[swf_GetTagPos(t)];
359 case ST_PLACEOBJECT2: {
360 U8 flags = swf_GetU8(t);
361 swf_GetU16(t); //depth;
365 swf_GetMatrix(t, &m);
367 swf_GetCXForm(t, &c, 1);
370 if(flags&PF_CLIPACTION)
373 swf_ResetReadBits(t);
374 name = &t->data[swf_GetTagPos(t)];
379 swf_SetTagPos(t,oldTagPos);
383 /* used in enumerateUsedIDs */
384 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
394 gradient2->num = swf_GetU8(tag);
395 for(t=0;t<gradient1->num;t++)
400 gradient1->ratios[t] = swf_GetU8(tag);
401 swf_GetRGBA(tag, &gradient1->rgba[t]);
402 gradient2->ratios[t] = swf_GetU8(tag);
403 swf_GetRGBA(tag, &gradient2->rgba[t]);
407 #define DEBUG_ENUMERATE if(0)
409 static void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
413 count = swf_GetU8(tag);
414 if(count == 0xff && num>1) // defineshape2,3 only
415 count = swf_GetU16(tag);
421 swf_ResetReadBits(tag);
422 type = swf_GetU8(tag); //type
425 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
427 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
429 else if(type == 0x10 || type == 0x12)
431 swf_ResetReadBits(tag);
432 swf_GetMatrix(tag, NULL);
434 swf_GetMatrix(tag, NULL);
435 swf_ResetReadBits(tag);
437 swf_GetMorphGradient(tag, NULL, NULL);
439 swf_GetGradient(tag, NULL, /*alpha*/ num>=3?1:0);
441 else if(type == 0x40 || type == 0x41)
443 swf_ResetReadBits(tag);
445 if(tag->data[tag->pos] != 0xff ||
446 tag->data[tag->pos+1] != 0xff)
447 (callback)(tag, tag->pos, callback_data);
450 swf_ResetReadBits(tag);
451 swf_GetMatrix(tag, NULL);
453 swf_GetMatrix(tag, NULL);
456 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
459 swf_ResetReadBits(tag);
460 count = swf_GetU8(tag); // line style array
462 count = swf_GetU16(tag);
469 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
471 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
475 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
478 swf_ResetReadBits(tag);
482 case ST_DEFINEBUTTONCXFORM: {
484 callback(tag, tag->pos + base, callback_data);
487 callback(tag, tag->pos + base, callback_data);
488 swf_GetU16(tag); //sound id
489 flags = swf_GetU8(tag);
491 swf_GetU32(tag); // in point
493 swf_GetU32(tag); // out points
495 swf_GetU16(tag); // loop count
498 int npoints = swf_GetU8(tag);
500 for(s=0;s<npoints;s++)
509 case ST_DEFINEBUTTONSOUND:
510 callback(tag, tag->pos + base, callback_data); //button id
513 case ST_EXPORTASSETS: {
514 int num = swf_GetU16(tag);
517 callback(tag, tag->pos + base, callback_data); //button id
518 swf_GetU16(tag); //id
519 while(swf_GetU8(tag)); //name
523 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
524 case ST_NAMECHARACTER:
525 case ST_GENERATORTEXT:
526 callback(tag, tag->pos + base, callback_data);
529 callback(tag, tag->pos + base, callback_data);
531 case ST_PLACEOBJECT2:
532 // only if placeflaghascharacter
533 if(!(tag->data[0]&2))
535 callback(tag, 3 + base, callback_data);
537 case ST_REMOVEOBJECT:
538 callback(tag, tag->pos + base, callback_data);
541 callback(tag, tag->pos + base, callback_data);
543 case ST_DEFINESPRITE: {
545 break; // sprite is expanded
547 swf_GetU16(tag); // id
548 swf_GetU16(tag); // framenum
551 U16 flags = swf_GetU16(tag);
554 TAG *tag2 = swf_InsertTag(NULL, id);
557 len = swf_GetU32(tag);
560 tag2->len = tag2->memsize = len;
561 tag2->data = malloc(len);
562 memcpy(tag2->data, &tag->data[tag->pos], len);
563 /* I never saw recursive sprites, but they are (theoretically)
564 possible, so better add base here again */
565 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
567 swf_GetBlock(tag, NULL, len);
571 case ST_DEFINEBUTTON2: // has some font ids in the button records
574 case ST_DEFINEBUTTON: {
575 swf_GetU16(tag); //button id
579 swf_GetU8(tag); //flag
580 offset = swf_GetU16(tag); //offset
585 if(!swf_GetU8(tag)) //flags
587 callback(tag, tag->pos + base, callback_data);
588 swf_GetU16(tag); //char
589 swf_GetU16(tag); //layer
590 swf_ResetReadBits(tag);
591 swf_GetMatrix(tag, NULL);
593 swf_ResetReadBits(tag);
594 swf_GetCXForm(tag, NULL, 1);
600 case ST_DEFINEEDITTEXT: {
602 swf_GetU16(tag); //id
603 swf_GetRect(tag, NULL); //bounding box
604 swf_ResetReadBits(tag);
605 flags1 = swf_GetU8(tag);
606 flags2 = swf_GetU8(tag);
608 callback(tag, tag->pos + base, callback_data);
613 case ST_DEFINETEXT: {
614 int glyphbits, advancebits;
616 id = swf_GetU16(tag); //id
617 swf_GetRect(tag, NULL); //bounding box
618 swf_ResetReadBits(tag);
619 swf_GetMatrix(tag, NULL); //matrix
620 swf_ResetReadBits(tag);
621 glyphbits = swf_GetU8(tag); //glyphbits
622 advancebits = swf_GetU8(tag); //advancebits
625 swf_ResetReadBits(tag);
626 flags = swf_GetBits(tag, 8);
628 if(flags & 128) // text style record
630 swf_ResetReadBits(tag);
631 if(flags & 8) { // hasfont
632 callback(tag, tag->pos + base, callback_data);
633 id = swf_GetU16(tag);
635 if(flags & 4) { // hascolor
636 if(num==1) swf_GetRGB(tag, NULL);
637 else swf_GetRGBA(tag, NULL);
639 if(flags & 2) { //has x offset
640 swf_ResetReadBits(tag);
643 if(flags & 1) { //has y offset
644 swf_ResetReadBits(tag);
647 if(flags & 8) { //has height
648 swf_ResetReadBits(tag);
651 } else { // glyph record
653 swf_ResetReadBits(tag);
654 for(t=0;t<flags;t++) {
655 swf_GetBits(tag, glyphbits);
656 swf_GetBits(tag, advancebits);
663 case ST_DEFINEFONTINFO:
664 case ST_DEFINEFONTINFO2:
666 callback(tag, tag->pos + base, callback_data);
668 case ST_DEFINEVIDEOSTREAM:
671 case ST_DOINITACTION:
672 callback(tag, tag->pos + base, callback_data);
675 case ST_DEFINEMORPHSHAPE:
676 case ST_DEFINESHAPE3:
678 case ST_DEFINESHAPE2:
680 case ST_DEFINESHAPE: {
686 if(tag->id == ST_DEFINEMORPHSHAPE) {
691 id = swf_GetU16(tag); // id;
692 swf_GetRect(tag, NULL); // bounds
694 swf_ResetReadBits(tag);
695 swf_GetRect(tag, NULL); // bounds2
696 swf_GetU32(tag); //offset to endedges
699 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
701 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
702 DEBUG_ENUMERATE printf("-------\n");
703 while(--numshapes>=0) /* morph shapes define two shapes */
705 DEBUG_ENUMERATE printf("shape:%d\n", numshapes);
706 fillbits = swf_GetBits(tag, 4);
707 linebits = swf_GetBits(tag, 4);
708 DEBUG_ENUMERATE printf("%d %d\n", fillbits, linebits);
709 swf_ResetReadBits(tag);
712 flags = swf_GetBits(tag, 1);
713 if(!flags) { //style change
714 flags = swf_GetBits(tag, 5);
718 int n = swf_GetBits(tag, 5);
720 x = swf_GetBits(tag, n); //x
721 y = swf_GetBits(tag, n); //y
722 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
724 if(flags&2) { //fill0
726 fill0 = swf_GetBits(tag, fillbits);
727 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
729 if(flags&4) { //fill1
731 fill1 = swf_GetBits(tag, fillbits);
732 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
734 if(flags&8) { //linestyle
736 line = swf_GetBits(tag, linebits);
737 DEBUG_ENUMERATE printf("linestyle %d\n",line);
740 DEBUG_ENUMERATE printf("more fillstyles\n");
741 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
742 fillbits = swf_GetBits(tag, 4);
743 linebits = swf_GetBits(tag, 4);
746 flags = swf_GetBits(tag, 1);
747 if(flags) { //straight edge
748 int n = swf_GetBits(tag, 4) + 2;
749 if(swf_GetBits(tag, 1)) { //line flag
751 x = swf_GetSBits(tag, n); //delta x
752 y = swf_GetSBits(tag, n); //delta y
753 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
755 int v=swf_GetBits(tag, 1);
757 d = swf_GetSBits(tag, n); //vert/horz
758 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
760 } else { //curved edge
761 int n = swf_GetBits(tag, 4) + 2;
763 x1 = swf_GetSBits(tag, n);
764 y1 = swf_GetSBits(tag, n);
765 x2 = swf_GetSBits(tag, n);
766 y2 = swf_GetSBits(tag, n);
767 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
779 void callbackCount(TAG * t,int pos, void*ptr)
782 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
785 void callbackFillin(TAG * t,int pos, void*ptr)
789 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
792 int swf_GetNumUsedIDs(TAG * t)
795 enumerateUsedIDs(t, 0, callbackCount, &num);
799 void swf_GetUsedIDs(TAG * t, int * positions)
801 int * ptr = positions;
802 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
805 void swf_Relocate (SWF*swf, char*bitmap)
809 memset(slaveids, -1, sizeof(slaveids));
817 if(swf_isDefiningTag(tag))
822 id = swf_GetDefineID(tag); //own id
824 if(!bitmap[id]) { //free
829 for (t=1;t<65536;t++)
839 slaveids[id] = newid;
841 swf_SetDefineID(tag, newid);
844 num = swf_GetNumUsedIDs(tag);
845 ptr = malloc(sizeof(int)*num);
846 swf_GetUsedIDs(tag, ptr);
849 int id = GET16(&tag->data[ptr[t]]);
851 fprintf(stderr, "swf_Relocate: Mapping id never encountered before: %d\n", id);
855 PUT16(&tag->data[ptr[t]], id);
861 void swf_RelocateDepth(SWF*swf, char*bitmap)
866 for(nr=65535;nr>=0;nr--) {
870 // now nr is the highest used depth. So we start
871 // assigning depths at nr+1
876 /* TODO * clip depths
879 int depth = swf_GetDepth(tag);
881 int newdepth = depth+nr;
883 fprintf(stderr, "Couldn't relocate depths: too large values\n");
886 swf_SetDepth(tag, newdepth);
892 TAG* swf_Concatenate (TAG*list1,TAG*list2)
894 TAG*tag=0,*lasttag=0;
896 char depthmap[65536];
898 memset(bitmap, 0, sizeof(bitmap));
899 memset(depthmap, 0, sizeof(depthmap));
900 memset(&swf1, 0, sizeof(swf1));
901 memset(&swf2, 0, sizeof(swf2));
903 swf1.firstTag = list1;
905 swf2.firstTag = list2;
910 if(!swf_isDefiningTag(tag)) {
911 int id = swf_GetDefineID(tag);
914 if(tag->id == ST_PLACEOBJECT ||
915 tag->id == ST_PLACEOBJECT2) {
916 int depth = swf_GetDepth(tag);
919 if(tag->id == ST_REMOVEOBJECT ||
920 tag->id == ST_REMOVEOBJECT2) {
921 int depth = swf_GetDepth(tag);
927 swf_Relocate(&swf2, bitmap);
928 swf_RelocateDepth(&swf2, depthmap);
929 lasttag->next = swf2.firstTag;
930 swf2.firstTag->prev = lasttag;
932 return swf1.firstTag;