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
26 #include "../rfxswf.h"
29 SFIXED RFXSWF_SP(SFIXED a1,SFIXED a2,SFIXED b1,SFIXED b2)
30 { S64 a = ((S64)a1*(S64)b1+(S64)a2*(S64)b2)>>16;
31 SFIXED result = (SFIXED)(a);
33 fprintf(stderr, "Warning: overflow in matrix multiplication");
36 SFIXED RFXSWF_QFIX(int zaehler,int nenner) // bildet Quotient von zwei INTs in SFIXED
37 { S64 z = zaehler<<16;
38 S64 a = z/(S64)nenner;
43 MATRIX * swf_MatrixJoin(MATRIX * d,MATRIX * s1,MATRIX * s2)
46 if (!s1) return (s2)?(MATRIX *)memcpy(d,s2,sizeof(MATRIX)):NULL;
47 if (!s2) return (MATRIX *)memcpy(d,s1,sizeof(MATRIX));
49 d->tx = s1->tx + RFXSWF_SP(s1->sx,s1->r1,s2->tx,s2->ty);
50 d->ty = s1->ty + RFXSWF_SP(s1->r0,s1->sy,s2->tx,s2->ty);
52 d->sx = RFXSWF_SP(s1->sx,s1->r1,s2->sx,s2->r0);
53 d->r0 = RFXSWF_SP(s1->r0,s1->sy,s2->sx,s2->r0);
55 d->r1 = RFXSWF_SP(s1->sx,s1->r1,s2->r1,s2->sy);
56 d->sy = RFXSWF_SP(s1->r0,s1->sy,s2->r1,s2->sy);
63 MATRIX * swf_MatrixMapTriangle(MATRIX * m,int dx,int dy,int x0,int y0,
64 int x1,int y1,int x2,int y2)
71 if ((!dx)||(!dy)) return NULL; // check DIV by zero
75 m->sx = RFXSWF_QFIX(dx1,dx);
76 m->sy = RFXSWF_QFIX(dy2,dy);
77 m->r0 = RFXSWF_QFIX(dy1,dx);
78 m->r1 = RFXSWF_QFIX(dx2,dy);
83 void swf_SetDefineID(TAG * tag, U16 newid)
85 int oldlen = tag->len;
87 swf_SetU16(tag, newid); /* set defining ID */
91 U16 swf_GetDefineID(TAG * t)
96 oldTagPos = swf_GetTagPos(t);
99 switch (swf_GetTagID(t))
100 { case ST_DEFINESHAPE:
101 case ST_DEFINESHAPE2:
102 case ST_DEFINESHAPE3:
103 case ST_DEFINESHAPE4:
104 case ST_DEFINEMORPHSHAPE:
105 case ST_DEFINEMORPHSHAPE2:
106 case ST_DEFINEEDITTEXT:
108 case ST_DEFINEBITSJPEG2:
109 case ST_DEFINEBITSJPEG3:
110 case ST_DEFINEBITSLOSSLESS:
111 case ST_DEFINEBITSLOSSLESS2:
112 case ST_DEFINESCALINGGRID: //pseudodefine
113 case ST_DEFINEBUTTON:
114 case ST_DEFINEBUTTON2:
115 case ST_DEFINEBUTTONCXFORM: //pseudodefine
116 case ST_DEFINEBUTTONSOUND: //pseudodefine
117 case ST_CSMTEXTSETTINGS: //pseudodefine
121 case ST_DEFINEFONTINFO: //pseudodefine
122 case ST_DEFINEFONTINFO2: //pseudodefine
123 case ST_DEFINEFONTALIGNZONES: //pseudodefine
124 case ST_DEFINEFONTNAME: //pseudodefine
128 case ST_DEFINESPRITE:
130 case ST_DEFINEVIDEOSTREAM:
131 case ST_GLYPHNAMES: //pseudodefine
132 case ST_VIDEOFRAME: //pseudodefine
133 case ST_NAMECHARACTER: //pseudodefine
134 case ST_DOINITACTION: //pseudodefine
138 fprintf(stderr, "rfxswf: Error: tag %d (%s) has no id\n", t->id, swf_TagGetName(t));
141 swf_SetTagPos(t,oldTagPos);
146 SRECT swf_GetDefineBBox(TAG * t)
151 memset(&b1, 0, sizeof(b1));
153 oldTagPos = swf_GetTagPos(t);
158 switch (swf_GetTagID(t))
159 { case ST_DEFINESHAPE:
160 case ST_DEFINESHAPE2:
161 case ST_DEFINESHAPE3:
162 case ST_DEFINESHAPE4:
163 case ST_DEFINEEDITTEXT:
166 case ST_DEFINEVIDEOSTREAM:
170 case ST_DEFINEMORPHSHAPE:
174 swf_ExpandRect2(&b1, &b2);
176 case ST_DEFINEBITSLOSSLESS:
177 case ST_DEFINEBITSLOSSLESS2:
179 case ST_DEFINEBITSJPEG2:
180 case ST_DEFINEBITSJPEG3:
185 swf_SetTagPos(t,oldTagPos);
190 U16 swf_GetPlaceID(TAG * t)
195 oldTagPos = swf_GetTagPos(t);
198 switch (swf_GetTagID(t))
199 { case ST_PLACEOBJECT:
200 case ST_REMOVEOBJECT:
201 case ST_FREECHARACTER:
206 case ST_PLACEOBJECT2:
207 { U8 flags = swf_GetU8(t);
208 U16 d = swf_GetU16(t);
209 id = (flags&PF_CHAR)?swf_GetU16(t):id;
211 case ST_PLACEOBJECT3:
212 { U8 flags = swf_GetU8(t);
213 U8 flags2 = swf_GetU8(t);
214 U16 d = swf_GetU16(t);
215 id = (flags&PF_CHAR)?swf_GetU16(t):id;
220 swf_SetTagPos(t,oldTagPos);
225 static int swf_definingtagids[] =
231 ST_DEFINEMORPHSHAPE2,
241 ST_DEFINEBITSLOSSLESS,
242 ST_DEFINEBITSLOSSLESS2,
248 ST_DEFINEVIDEOSTREAM,
253 // tags which may be used inside a sprite definition
254 static int swf_spritetagids[] =
272 /* tags which add content or information to a character with a given ID */
273 static int swf_pseudodefiningtagids[] =
277 ST_DEFINEFONTALIGNZONES,
279 ST_DEFINEBUTTONCXFORM,
280 ST_DEFINEBUTTONSOUND,
281 ST_DEFINESCALINGGRID,
290 U8 swf_isAllowedSpriteTag(TAG * tag)
294 while(swf_spritetagids[t]>=0)
296 if(swf_spritetagids[t] == id)
303 U8 swf_isDefiningTag(TAG * tag)
307 while(swf_definingtagids[t]>=0)
309 if(swf_definingtagids[t] == id)
316 U8 swf_isPseudoDefiningTag(TAG * tag)
320 while(swf_pseudodefiningtagids[t]>=0)
322 if(swf_pseudodefiningtagids[t] == id)
329 int swf_GetDepth(TAG * t)
333 oldTagPos = swf_GetTagPos(t);
336 switch (swf_GetTagID(t))
337 { case ST_PLACEOBJECT:
338 case ST_REMOVEOBJECT:
340 depth = swf_GetU16(t);
342 case ST_REMOVEOBJECT2:
343 depth = swf_GetU16(t);
345 case ST_PLACEOBJECT2:
346 { U8 flags = swf_GetU8(t);
347 depth = swf_GetU16(t);
349 case ST_PLACEOBJECT3:
350 { U8 flags = swf_GetU8(t);
351 U8 flags2 = swf_GetU8(t);
352 depth = swf_GetU16(t);
356 depth = swf_GetU16(t);
359 swf_SetTagPos(t,oldTagPos);
363 void swf_SetDepth(TAG * t, U16 depth)
365 switch (swf_GetTagID(t))
366 { case ST_PLACEOBJECT:
367 case ST_REMOVEOBJECT:
368 PUT16(t->data, depth);
370 case ST_REMOVEOBJECT2:
371 PUT16(t->data, depth);
373 case ST_PLACEOBJECT2:
374 PUT16(&t->data[1], depth);
377 PUT16(t->data, depth);
380 fprintf(stderr, "rfxswf: Error: tag %d has no depth\n", t->id);
384 char* swf_GetName(TAG * t)
390 oldTagPos = swf_GetTagPos(t);
392 switch(swf_GetTagID(t))
395 name = (char*)&t->data[swf_GetTagPos(t)];
397 case ST_PLACEOBJECT3:
398 case ST_PLACEOBJECT2: {
399 U8 flags = swf_GetU8(t);
400 if(t->id == ST_PLACEOBJECT3)
402 swf_GetU16(t); //depth;
406 swf_GetMatrix(t, &m);
408 swf_GetCXForm(t, &c, 1);
411 if(flags&PF_CLIPDEPTH)
414 swf_ResetReadBits(t);
415 name = (char*)&t->data[swf_GetTagPos(t)];
420 swf_SetTagPos(t,oldTagPos);
424 /* used in enumerateUsedIDs */
425 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
428 int num = swf_GetU8(tag) & 15;
429 if(gradient1) gradient1->num = num;
430 if(gradient2) gradient2->num = num;
433 gradient1->num = num;
434 gradient1->rgba = (RGBA*)rfx_calloc(sizeof(RGBA)*gradient1->num);
435 gradient1->ratios = (U8*)rfx_calloc(sizeof(gradient1->ratios[0])*gradient1->num);
438 gradient2->num = num;
439 gradient2->rgba = (RGBA*)rfx_calloc(sizeof(RGBA)*gradient2->num);
440 gradient2->ratios = (U8*)rfx_calloc(sizeof(gradient2->ratios[0])*gradient2->num);
447 ratio = swf_GetU8(tag);
448 swf_GetRGBA(tag, &color);
450 gradient1->ratios[t] = ratio;
451 gradient1->rgba[t] = color;
454 ratio = swf_GetU8(tag);
455 swf_GetRGBA(tag, &color);
457 gradient2->ratios[t] = ratio;
458 gradient2->rgba[t] = color;
463 #define DEBUG_ENUMERATE if(0)
464 //#define DEBUG_ENUMERATE
466 void enumerateUsedIDs_fillstyle(TAG * tag, int t, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
469 type = swf_GetU8(tag); //type
470 DEBUG_ENUMERATE printf("fill style %d) type=%02x (tagpos=%d)\n", t, type, tag->pos);
474 {swf_GetRGBA(tag, &color);if(morph) swf_GetRGBA(tag, NULL);}
476 {swf_GetRGB(tag, &color);if(morph) swf_GetRGB(tag, NULL);}
477 DEBUG_ENUMERATE printf(" %02x%02x%02x%02x\n", color.r,color.g,color.b,color.a);
479 else if(type == 0x10 || type == 0x12 || type == 0x13)
481 swf_ResetReadBits(tag);
483 swf_GetMatrix(tag, &m);
484 DEBUG_ENUMERATE swf_DumpMatrix(stdout, &m);
486 swf_GetMatrix(tag, &m);
487 DEBUG_ENUMERATE swf_DumpMatrix(stdout, &m);
489 swf_ResetReadBits(tag);
491 swf_GetMorphGradient(tag, NULL, NULL);
498 swf_GetGradient(tag, &g, /*alpha*/ num>=3?1:0);
499 DEBUG_ENUMERATE swf_DumpGradient(stdout, &g);
504 else if(type == 0x40 || type == 0x41 || type == 0x42 || type == 0x43)
506 swf_ResetReadBits(tag);
507 if(tag->data[tag->pos] != 0xff ||
508 tag->data[tag->pos+1] != 0xff)
509 (callback)(tag, tag->pos, callback_data);
512 swf_ResetReadBits(tag);
513 swf_GetMatrix(tag, NULL);
515 swf_GetMatrix(tag, NULL);
518 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x in tag %02d\n",type, tag->id);
522 void enumerateUsedIDs_linestyle(TAG * tag, int t, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
526 width = swf_GetU16(tag);
531 U16 flags = swf_GetU16(tag);
532 DEBUG_ENUMERATE printf("line style %d) flags: %08x\n", t, flags);
533 if((flags & 0x30) == 0x20) {
534 U16 miter = swf_GetU16(tag); // miter limit
535 DEBUG_ENUMERATE printf("line style %d) miter join: %08x\n", t, miter);
543 {swf_GetRGBA(tag, &color);if(morph) swf_GetRGBA(tag, NULL);}
545 {swf_GetRGB(tag, &color);if(morph) swf_GetRGB(tag, NULL);}
547 enumerateUsedIDs_fillstyle(tag, t, callback, callback_data, num, morph);
549 DEBUG_ENUMERATE printf("line style %d) width=%.2f color=%02x%02x%02x%02x \n", t, width/20.0, color.r,color.g,color.b,color.a);
552 void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
556 count = swf_GetU8(tag);
557 if(count == 0xff && num>1) // defineshape2,3,4 only
558 count = swf_GetU16(tag);
560 DEBUG_ENUMERATE printf("%d fill styles\n", count);
563 enumerateUsedIDs_fillstyle(tag, t, callback, callback_data, num, morph);
565 swf_ResetReadBits(tag);
566 count = swf_GetU8(tag); // line style array
568 count = swf_GetU16(tag);
569 DEBUG_ENUMERATE printf("%d line styles\n", count);
572 enumerateUsedIDs_linestyle(tag, t, callback, callback_data, num, morph);
576 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
579 swf_ResetReadBits(tag);
583 case ST_DEFINEBUTTONSOUND: {
585 callback(tag, tag->pos + base, callback_data);
588 callback(tag, tag->pos + base, callback_data);
589 swf_GetU16(tag); //sound id
590 flags = swf_GetU8(tag);
592 swf_GetU32(tag); // in point
594 swf_GetU32(tag); // out points
596 swf_GetU16(tag); // loop count
599 int npoints = swf_GetU8(tag);
601 for(s=0;s<npoints;s++)
610 case ST_DEFINEBUTTONCXFORM:
611 callback(tag, tag->pos + base, callback_data); //button id
614 case ST_EXPORTASSETS: {
615 int num = swf_GetU16(tag);
618 callback(tag, tag->pos + base, callback_data); //button id
619 swf_GetU16(tag); //id
620 while(swf_GetU8(tag)); //name
624 case ST_IMPORTASSETS:
625 case ST_IMPORTASSETS2: {
626 swf_GetString(tag); //count
627 swf_GetU8(tag); //reserved
628 swf_GetU8(tag); //reserved
629 int num = swf_GetU16(tag); //url
632 callback(tag, tag->pos + base, callback_data); //button id
633 swf_GetU16(tag); //id
634 while(swf_GetU8(tag)); //name
638 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
639 case ST_NAMECHARACTER:
640 case ST_DEFINEBINARY:
641 case ST_DEFINEFONTNAME:
642 case ST_GENERATORTEXT:
643 callback(tag, tag->pos + base, callback_data);
646 callback(tag, tag->pos + base, callback_data);
648 case ST_PLACEOBJECT2:
649 // only if placeflaghascharacter
650 if(!(tag->data[0]&2))
652 callback(tag, 3 + base, callback_data);
654 case ST_PLACEOBJECT3:
655 // only if placeflaghascharacter
656 if(!(tag->data[0]&2))
658 callback(tag, 4 + base, callback_data);
660 case ST_REMOVEOBJECT:
661 callback(tag, tag->pos + base, callback_data);
664 callback(tag, tag->pos + base, callback_data);
666 case ST_DEFINESPRITE: {
668 break; // sprite is expanded
670 swf_GetU16(tag); // id
671 swf_GetU16(tag); // framenum
674 U16 flags = swf_GetU16(tag);
677 TAG *tag2 = swf_InsertTag(NULL, id);
680 len = swf_GetU32(tag);
683 tag2->len = tag2->memsize = len;
684 tag2->data = (U8*)rfx_alloc(len);
685 memcpy(tag2->data, &tag->data[tag->pos], len);
686 /* I never saw recursive sprites, but they are (theoretically)
687 possible, so better add base here again */
688 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
690 swf_GetBlock(tag, NULL, len);
694 case ST_DEFINEBUTTON2: // has some font ids in the button records
697 case ST_DEFINEBUTTON: {
698 swf_GetU16(tag); //button id
702 swf_GetU8(tag); //flag
703 offset = swf_GetU16(tag); //offset
707 U8 flags = swf_GetU8(tag);
710 callback(tag, tag->pos + base, callback_data);
711 swf_GetU16(tag); //char
712 swf_GetU16(tag); //layer
713 swf_ResetReadBits(tag);
714 swf_GetMatrix(tag, NULL);
716 swf_ResetReadBits(tag);
717 swf_GetCXForm(tag, NULL, 1);
720 U8 num = swf_GetU8(tag);
723 swf_DeleteFilter(swf_GetFilter(tag));
727 U8 blendmode = swf_GetU8(tag);
733 case ST_DEFINEEDITTEXT: {
735 swf_GetU16(tag); //id
736 swf_GetRect(tag, NULL); //bounding box
737 swf_ResetReadBits(tag);
738 flags1 = swf_GetU8(tag);
739 flags2 = swf_GetU8(tag);
741 callback(tag, tag->pos + base, callback_data);
746 case ST_DEFINETEXT: {
747 int glyphbits, advancebits;
749 id = swf_GetU16(tag); //id
750 swf_GetRect(tag, NULL); //bounding box
751 swf_ResetReadBits(tag);
752 swf_GetMatrix(tag, NULL); //matrix
753 swf_ResetReadBits(tag);
754 glyphbits = swf_GetU8(tag); //glyphbits
755 advancebits = swf_GetU8(tag); //advancebits
759 swf_ResetReadBits(tag);
760 flags = swf_GetBits(tag, 8);
763 swf_ResetReadBits(tag);
764 if(flags & 8) { // hasfont
765 callback(tag, tag->pos + base, callback_data);
766 id = swf_GetU16(tag);
768 if(flags & 4) { // hascolor
769 if(num==1) swf_GetRGB(tag, NULL);
770 else swf_GetRGBA(tag, NULL);
772 if(flags & 2) { //has x offset
773 swf_ResetReadBits(tag);
776 if(flags & 1) { //has y offset
777 swf_ResetReadBits(tag);
780 if(flags & 8) { //has height
781 swf_ResetReadBits(tag);
785 flags = swf_GetBits(tag, 8);
787 swf_ResetReadBits(tag);
788 for(t=0;t<flags;t++) {
789 swf_GetBits(tag, glyphbits);
790 swf_GetBits(tag, advancebits);
795 case ST_DEFINEFONTALIGNZONES:
796 case ST_DEFINESCALINGGRID:
798 case ST_CSMTEXTSETTINGS:
799 case ST_DEFINEFONTINFO:
800 case ST_DEFINEFONTINFO2:
802 callback(tag, tag->pos + base, callback_data);
804 case ST_DEFINEVIDEOSTREAM:
807 case ST_DOINITACTION:
808 callback(tag, tag->pos + base, callback_data);
811 case ST_DEFINEMORPHSHAPE2:
812 case ST_DEFINESHAPE4:
814 case ST_DEFINEMORPHSHAPE:
815 case ST_DEFINESHAPE3:
817 case ST_DEFINESHAPE2:
819 case ST_DEFINESHAPE: {
825 if(tag->id == ST_DEFINEMORPHSHAPE || tag->id==ST_DEFINEMORPHSHAPE2) {
830 id = swf_GetU16(tag); // id;
831 SRECT r={0,0,0,0},r2={0,0,0,0};
832 swf_GetRect(tag, &r); // shape bounds
834 swf_ResetReadBits(tag);
835 swf_GetRect(tag, NULL); // shape bounds2
837 swf_ResetReadBits(tag);
838 swf_GetRect(tag, NULL); // edge bounds1
842 swf_ResetReadBits(tag);
843 swf_GetRect(tag, &r2); // edge bounds
844 U8 flags = swf_GetU8(tag); // flags, &1: contains scaling stroke, &2: contains non-scaling stroke
845 DEBUG_ENUMERATE printf("flags: %02x (1=scaling strokes, 2=non-scaling strokes)\n", flags);
848 swf_GetU32(tag); //offset to endedges
851 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
852 DEBUG_ENUMERATE printf("BBox %.2f %.2f %.2f %.2f\n", r.xmin/20.0,r.ymin/20.0,r.xmax/20.0,r.ymax/20.0);
853 DEBUG_ENUMERATE printf("BBox %.2f %.2f %.2f %.2f\n", r2.xmin/20.0,r2.ymin/20.0,r2.xmax/20.0,r2.ymax/20.0);
855 DEBUG_ENUMERATE printf("style tag pos: %d\n", tag->pos);
856 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
857 DEBUG_ENUMERATE printf("-------\n");
858 swf_ResetReadBits(tag);
859 while(--numshapes>=0) /* morph shapes define two shapes */
861 DEBUG_ENUMERATE printf("shape:%d\n", numshapes);
862 fillbits = swf_GetBits(tag, 4);
863 linebits = swf_GetBits(tag, 4);
864 DEBUG_ENUMERATE printf("fillbits=%d linebits=%d\n", fillbits, linebits);
865 swf_ResetReadBits(tag);
869 flags = swf_GetBits(tag, 1);
870 if(!flags) { //style change
871 flags = swf_GetBits(tag, 5);
875 int n = swf_GetBits(tag, 5);
876 x = swf_GetBits(tag, n); //x
877 y = swf_GetBits(tag, n); //y
878 DEBUG_ENUMERATE printf("moveTo %.2f %.2f\n",x/20.0,y/20.0);
880 if(flags&2) { //fill0
882 fill0 = swf_GetBits(tag, fillbits);
883 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
885 if(flags&4) { //fill1
887 fill1 = swf_GetBits(tag, fillbits);
888 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
890 if(flags&8) { //linestyle
892 line = swf_GetBits(tag, linebits);
893 DEBUG_ENUMERATE printf("linestyle %d\n",line);
896 DEBUG_ENUMERATE printf("more fillstyles\n");
897 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
898 fillbits = swf_GetBits(tag, 4);
899 linebits = swf_GetBits(tag, 4);
902 flags = swf_GetBits(tag, 1);
903 if(flags) { //straight edge
904 int n = swf_GetBits(tag, 4) + 2;
905 if(swf_GetBits(tag, 1)) { //line flag
906 x += swf_GetSBits(tag, n); //delta x
907 y += swf_GetSBits(tag, n); //delta y
908 DEBUG_ENUMERATE printf("lineTo %.2f %.2f\n",x/20.0,y/20.0);
910 int v=swf_GetBits(tag, 1);
912 d = swf_GetSBits(tag, n); //vert/horz
917 DEBUG_ENUMERATE printf("lineTo %.2f %.2f (%s)\n",x/20.0,y/20.0, v?"vertical":"horizontal");
919 } else { //curved edge
920 int n = swf_GetBits(tag, 4) + 2;
922 x1 = swf_GetSBits(tag, n);
923 y1 = swf_GetSBits(tag, n);
924 x2 = swf_GetSBits(tag, n);
925 y2 = swf_GetSBits(tag, n);
926 DEBUG_ENUMERATE printf("splineTo %.2f %.2f %.2f %.2f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
938 void callbackCount(TAG * t,int pos, void*ptr)
941 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
944 void callbackFillin(TAG * t,int pos, void*ptr)
948 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
951 int swf_GetNumUsedIDs(TAG * t)
954 enumerateUsedIDs(t, 0, callbackCount, &num);
958 void swf_GetUsedIDs(TAG * t, int * positions)
960 int * ptr = positions;
961 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
964 void swf_Relocate (SWF*swf, char*bitmap)
968 memset(slaveids, -1, sizeof(slaveids));
976 if(swf_isDefiningTag(tag))
981 id = swf_GetDefineID(tag); //own id
983 if(!bitmap[id]) { //free
988 for (t=1;t<65536;t++)
998 slaveids[id] = newid;
1000 swf_SetDefineID(tag, newid);
1003 num = swf_GetNumUsedIDs(tag);
1005 ptr = (int*)rfx_alloc(sizeof(int)*num);
1006 swf_GetUsedIDs(tag, ptr);
1008 for(t=0;t<num;t++) {
1009 int id = GET16(&tag->data[ptr[t]]);
1010 if(slaveids[id]<0) {
1011 fprintf(stderr, "swf_Relocate: Mapping id (%d) never encountered before in %s\n", id,
1012 swf_TagGetName(tag));
1015 PUT16(&tag->data[ptr[t]], id);
1024 void swf_Relocate2(SWF*swf, int*id2id)
1027 tag = swf->firstTag;
1029 if(swf_isDefiningTag(tag)) {
1030 int id = swf_GetDefineID(tag);
1033 swf_SetDefineID(tag, id);
1036 int num = swf_GetNumUsedIDs(tag);
1040 ptr = (int*)rfx_alloc(sizeof(int)*num);
1041 swf_GetUsedIDs(tag, ptr);
1042 for(t=0;t<num;t++) {
1043 int id = GET16(&tag->data[ptr[t]]);
1046 PUT16(&tag->data[ptr[t]], id);
1053 void swf_RelocateDepth(SWF*swf, char*bitmap)
1057 tag = swf->firstTag;
1058 for(nr=65535;nr>=0;nr--) {
1062 // now nr is the highest used depth. So we start
1063 // assigning depths at nr+1
1069 /* TODO * clip depths
1072 if(tag->id == ST_PLACEOBJECT2) {
1074 swf_GetPlaceObject(tag, &obj);
1076 int newdepth = obj.clipdepth+nr;
1077 if(newdepth>65535) {
1078 fprintf(stderr, "Couldn't relocate depths: too large values\n");
1081 obj.clipdepth = newdepth;
1082 swf_ResetTag(tag, ST_PLACEOBJECT2);
1083 swf_SetPlaceObject(tag, &obj);
1085 swf_PlaceObjectFree(&obj);
1088 depth = swf_GetDepth(tag);
1090 int newdepth = depth+nr;
1091 if(newdepth>65535) {
1092 fprintf(stderr, "Couldn't relocate depths: too large values\n");
1095 swf_SetDepth(tag, newdepth);
1101 U8 swf_isShapeTag(TAG*tag)
1103 if(tag->id == ST_DEFINESHAPE ||
1104 tag->id == ST_DEFINESHAPE2 ||
1105 tag->id == ST_DEFINESHAPE3 ||
1106 tag->id == ST_DEFINESHAPE4)
1111 U8 swf_isPlaceTag(TAG*tag)
1113 if(tag->id == ST_PLACEOBJECT ||
1114 tag->id == ST_PLACEOBJECT2 ||
1115 tag->id == ST_PLACEOBJECT3)
1119 U8 swf_isTextTag(TAG*tag)
1121 if(tag->id == ST_DEFINETEXT ||
1122 tag->id == ST_DEFINETEXT2)
1127 U8 swf_isFontTag(TAG*tag)
1129 if(tag->id == ST_DEFINEFONT ||
1130 tag->id == ST_DEFINEFONT2 ||
1131 tag->id == ST_DEFINEFONTINFO)
1136 U8 swf_isImageTag(TAG*tag)
1138 if(tag->id == ST_DEFINEBITSJPEG ||
1139 tag->id == ST_DEFINEBITSJPEG2 ||
1140 tag->id == ST_DEFINEBITSJPEG3 ||
1141 tag->id == ST_DEFINEBITSLOSSLESS ||
1142 tag->id == ST_DEFINEBITSLOSSLESS2)
1147 TAG* swf_Concatenate (TAG*list1,TAG*list2)
1149 TAG*tag=0,*lasttag=0;
1151 char depthmap[65536];
1153 memset(bitmap, 0, sizeof(bitmap));
1154 memset(depthmap, 0, sizeof(depthmap));
1155 memset(&swf1, 0, sizeof(swf1));
1156 memset(&swf2, 0, sizeof(swf2));
1158 swf1.firstTag = list1;
1160 swf2.firstTag = list2;
1165 if(!swf_isDefiningTag(tag)) {
1166 int id = swf_GetDefineID(tag);
1169 if(tag->id == ST_PLACEOBJECT ||
1170 tag->id == ST_PLACEOBJECT2) {
1171 int depth = swf_GetDepth(tag);
1172 depthmap[depth] = 1;
1174 if(tag->id == ST_REMOVEOBJECT ||
1175 tag->id == ST_REMOVEOBJECT2) {
1176 int depth = swf_GetDepth(tag);
1177 depthmap[depth] = 0;
1182 swf_Relocate(&swf2, bitmap);
1183 swf_RelocateDepth(&swf2, depthmap);
1184 lasttag->next = swf2.firstTag;
1185 swf2.firstTag->prev = lasttag;
1187 return swf1.firstTag;
1190 static int tagHash(TAG*tag)
1193 unsigned int a = 0x6b973e5a;
1194 /* start at pos 2, as 0 and 1 are the id */
1195 for(t=2;t<tag->len;t++) {
1198 a += tag->data[t]*0xefbc35a5*b*(t+1);
1200 return a&0x7fffffff; //always return positive number
1203 void swf_Optimize(SWF*swf)
1205 const int hash_size = 131072;
1206 char* dontremap = (char*)rfx_calloc(sizeof(char)*65536);
1207 U16* remap = (U16*)rfx_alloc(sizeof(U16)*65536);
1208 TAG* id2tag = (TAG*)rfx_calloc(sizeof(TAG*)*65536);
1209 TAG** hashmap = (TAG**)rfx_calloc(sizeof(TAG*)*hash_size);
1212 for(t=0;t<65536;t++) {
1218 tag = swf->firstTag;
1220 /* make sure we don't remap to this tag,
1221 as it might have different "helper tags"
1222 FIXME: a better way would be to compare
1223 the helper tags, too.
1225 if(swf_isPseudoDefiningTag(tag) &&
1226 tag->id != ST_NAMECHARACTER) {
1227 dontremap[swf_GetDefineID(tag)] = 1;
1231 tag = swf->firstTag;
1233 TAG*next = tag->next;
1236 int num = swf_GetNumUsedIDs(tag);
1237 int*positions = (int*)rfx_alloc(sizeof(int)*num);
1239 swf_GetUsedIDs(tag, positions);
1240 for(t=0;t<num;t++) {
1241 int id = GET16(&tag->data[positions[t]]);
1243 PUT16(&tag->data[positions[t]], id);
1245 rfx_free(positions);
1247 /* now look for previous tags with the same
1249 if(swf_isDefiningTag(tag)) {
1251 int id = swf_GetDefineID(tag);
1252 int hash = tagHash(tag);
1255 while((tag2 = hashmap[hash%hash_size])) {
1256 if(tag2 != (TAG*)0 && tag->len == tag2->len) {
1257 if(memcmp(&tag->data[2],&tag2->data[2],tag->len-2) == 0) {
1265 while(hashmap[hash%hash_size]) hash++;
1266 hashmap[hash%hash_size] = tag;
1268 /* we found two identical tags- remap one
1270 remap[id] = swf_GetDefineID(tag2);
1272 if(tag == swf->firstTag)
1273 swf->firstTag = next;
1275 } else if(swf_isPseudoDefiningTag(tag)) {
1276 int id = swf_GetDefineID(tag);
1278 /* if this tag was remapped, we don't
1279 need the helper tag anymore. Discard
1282 if(tag == swf->firstTag)
1283 swf->firstTag = next;
1290 rfx_free(dontremap);
1296 void swf_SetDefineBBox(TAG * tag, SRECT newbbox)
1300 swf_SetTagPos(tag,0);
1302 switch (swf_GetTagID(tag))
1304 case ST_DEFINESHAPE:
1305 case ST_DEFINESHAPE2:
1306 case ST_DEFINESHAPE3:
1307 case ST_DEFINEEDITTEXT:
1309 case ST_DEFINETEXT2:
1310 case ST_DEFINEVIDEOSTREAM: {
1311 U32 after_bbox_offset = 0, len;
1313 id = swf_GetU16(tag);
1314 swf_GetRect(tag, &b1);
1315 swf_ResetReadBits(tag);
1316 after_bbox_offset = tag->pos;
1317 len = tag->len - after_bbox_offset;
1318 data = (U8*)malloc(len);
1319 memcpy(data, &tag->data[after_bbox_offset], len);
1322 swf_SetRect(tag, &newbbox);
1323 swf_SetBlock(tag, data, len);
1325 tag->pos = tag->readBit = 0;
1329 fprintf(stderr, "rfxswf: Tag %d (%s) has no bbox\n", tag->id, swf_TagGetName(tag));
1333 RGBA swf_GetSWFBackgroundColor(SWF*swf)
1335 TAG*t=swf->firstTag;
1337 color.r = color.b = color.g = 0;
1340 if(t->id == ST_SETBACKGROUNDCOLOR) {
1341 swf_SetTagPos(t, 0);
1342 color.r = swf_GetU8(t);
1343 color.g = swf_GetU8(t);
1344 color.b = swf_GetU8(t);