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)
28 { S64 a = ((S64)a1*(S64)b1+(S64)a2*(S64)b2)>>16;
29 SFIXED result = (SFIXED)(a);
31 fprintf(stderr, "Warning: overflow in matrix multiplication");
34 SFIXED RFXSWF_QFIX(int zaehler,int nenner) // bildet Quotient von zwei INTs in SFIXED
35 { S64 z = zaehler<<16;
36 S64 a = z/(S64)nenner;
41 MATRIX * swf_MatrixJoin(MATRIX * d,MATRIX * s1,MATRIX * s2)
44 if (!s1) return (s2)?(MATRIX *)memcpy(d,s2,sizeof(MATRIX)):NULL;
45 if (!s2) return (MATRIX *)memcpy(d,s1,sizeof(MATRIX));
47 d->tx = s1->tx + RFXSWF_SP(s1->sx,s1->r1,s2->tx,s2->ty);
48 d->ty = s1->ty + RFXSWF_SP(s1->r0,s1->sy,s2->tx,s2->ty);
50 d->sx = RFXSWF_SP(s1->sx,s1->r1,s2->sx,s2->r0);
51 d->r0 = RFXSWF_SP(s1->r0,s1->sy,s2->sx,s2->r0);
53 d->r1 = RFXSWF_SP(s1->sx,s1->r1,s2->r1,s2->sy);
54 d->sy = RFXSWF_SP(s1->r0,s1->sy,s2->r1,s2->sy);
61 MATRIX * swf_MatrixMapTriangle(MATRIX * m,int dx,int dy,int x0,int y0,
62 int x1,int y1,int x2,int y2)
69 if ((!dx)||(!dy)) return NULL; // check DIV by zero
73 m->sx = RFXSWF_QFIX(dx1,dx);
74 m->sy = RFXSWF_QFIX(dy2,dy);
75 m->r0 = RFXSWF_QFIX(dy1,dx);
76 m->r1 = RFXSWF_QFIX(dx2,dy);
81 void swf_SetDefineID(TAG * tag, U16 newid)
83 int oldlen = tag->len;
85 swf_SetU16(tag, newid); /* set defining ID */
89 U16 swf_GetDefineID(TAG * t)
94 oldTagPos = swf_GetTagPos(t);
97 switch (swf_GetTagID(t))
98 { case ST_DEFINESHAPE:
100 case ST_DEFINESHAPE3:
101 case ST_DEFINESHAPE4:
102 case ST_DEFINEMORPHSHAPE:
103 case ST_DEFINEEDITTEXT:
105 case ST_DEFINEBITSJPEG2:
106 case ST_DEFINEBITSJPEG3:
107 case ST_DEFINEBITSLOSSLESS:
108 case ST_DEFINEBITSLOSSLESS2:
109 case ST_DEFINESCALINGGRID: //pseudodefine
110 case ST_DEFINEBUTTON:
111 case ST_DEFINEBUTTON2:
112 case ST_DEFINEBUTTONCXFORM: //pseudodefine
113 case ST_DEFINEBUTTONSOUND: //pseudodefine
114 case ST_CSMTEXTSETTINGS: //pseudodefine
118 case ST_DEFINEFONTINFO: //pseudodefine
119 case ST_DEFINEFONTINFO2: //pseudodefine
120 case ST_DEFINEFONTALIGNZONES: //pseudodefine
124 case ST_DEFINESPRITE:
126 case ST_DEFINEVIDEOSTREAM:
127 case ST_GLYPHNAMES: //pseudodefine
128 case ST_VIDEOFRAME: //pseudodefine
129 case ST_NAMECHARACTER: //pseudodefine
130 case ST_DOINITACTION: //pseudodefine
134 fprintf(stderr, "rfxswf: Error: tag %d (%s) has no id\n", t->id, swf_TagGetName(t));
137 swf_SetTagPos(t,oldTagPos);
142 SRECT swf_GetDefineBBox(TAG * t)
147 memset(&b1, 0, sizeof(b1));
149 oldTagPos = swf_GetTagPos(t);
154 switch (swf_GetTagID(t))
155 { case ST_DEFINESHAPE:
156 case ST_DEFINESHAPE2:
157 case ST_DEFINESHAPE3:
158 case ST_DEFINESHAPE4:
159 case ST_DEFINEEDITTEXT:
162 case ST_DEFINEVIDEOSTREAM:
166 case ST_DEFINEMORPHSHAPE:
170 swf_ExpandRect2(&b1, &b2);
172 case ST_DEFINEBITSLOSSLESS:
173 case ST_DEFINEBITSLOSSLESS2:
175 case ST_DEFINEBITSJPEG2:
176 case ST_DEFINEBITSJPEG3:
181 swf_SetTagPos(t,oldTagPos);
186 U16 swf_GetPlaceID(TAG * t)
191 oldTagPos = swf_GetTagPos(t);
194 switch (swf_GetTagID(t))
195 { case ST_PLACEOBJECT:
196 case ST_REMOVEOBJECT:
197 case ST_FREECHARACTER:
202 case ST_PLACEOBJECT2:
203 { U8 flags = swf_GetU8(t);
204 U16 d = swf_GetU16(t);
205 id = (flags&PF_CHAR)?swf_GetU16(t):id;
207 case ST_PLACEOBJECT3:
208 { U8 flags = swf_GetU8(t);
209 U8 flags2 = swf_GetU8(t);
210 U16 d = swf_GetU16(t);
211 id = (flags&PF_CHAR)?swf_GetU16(t):id;
216 swf_SetTagPos(t,oldTagPos);
221 static int swf_definingtagids[] =
236 ST_DEFINEBITSLOSSLESS,
237 ST_DEFINEBITSLOSSLESS2,
243 ST_DEFINEVIDEOSTREAM,
248 // tags which may be used inside a sprite definition
249 static int swf_spritetagids[] =
267 /* tags which add content or information to a character with a given ID */
268 static int swf_pseudodefiningtagids[] =
272 ST_DEFINEFONTALIGNZONES,
273 ST_DEFINEBUTTONCXFORM,
274 ST_DEFINEBUTTONSOUND,
275 ST_DEFINESCALINGGRID,
284 U8 swf_isAllowedSpriteTag(TAG * tag)
288 while(swf_spritetagids[t]>=0)
290 if(swf_spritetagids[t] == id)
297 U8 swf_isDefiningTag(TAG * tag)
301 while(swf_definingtagids[t]>=0)
303 if(swf_definingtagids[t] == id)
310 U8 swf_isPseudoDefiningTag(TAG * tag)
314 while(swf_pseudodefiningtagids[t]>=0)
316 if(swf_pseudodefiningtagids[t] == id)
323 int swf_GetDepth(TAG * t)
327 oldTagPos = swf_GetTagPos(t);
330 switch (swf_GetTagID(t))
331 { case ST_PLACEOBJECT:
332 case ST_REMOVEOBJECT:
334 depth = swf_GetU16(t);
336 case ST_REMOVEOBJECT2:
337 depth = swf_GetU16(t);
339 case ST_PLACEOBJECT2:
340 { U8 flags = swf_GetU8(t);
341 depth = swf_GetU16(t);
343 case ST_PLACEOBJECT3:
344 { U8 flags = swf_GetU8(t);
345 U8 flags2 = swf_GetU8(t);
346 depth = swf_GetU16(t);
350 depth = swf_GetU16(t);
353 swf_SetTagPos(t,oldTagPos);
357 void swf_SetDepth(TAG * t, U16 depth)
359 switch (swf_GetTagID(t))
360 { case ST_PLACEOBJECT:
361 case ST_REMOVEOBJECT:
362 PUT16(t->data, depth);
364 case ST_REMOVEOBJECT2:
365 PUT16(t->data, depth);
367 case ST_PLACEOBJECT2:
368 PUT16(&t->data[1], depth);
371 PUT16(t->data, depth);
374 fprintf(stderr, "rfxswf: Error: tag %d has no depth\n", t->id);
378 char* swf_GetName(TAG * t)
384 oldTagPos = swf_GetTagPos(t);
386 switch(swf_GetTagID(t))
389 name = &t->data[swf_GetTagPos(t)];
391 case ST_PLACEOBJECT3:
392 case ST_PLACEOBJECT2: {
393 U8 flags = swf_GetU8(t);
394 if(t->id == ST_PLACEOBJECT3)
396 swf_GetU16(t); //depth;
400 swf_GetMatrix(t, &m);
402 swf_GetCXForm(t, &c, 1);
405 if(flags&PF_CLIPDEPTH)
408 swf_ResetReadBits(t);
409 name = &t->data[swf_GetTagPos(t)];
414 swf_SetTagPos(t,oldTagPos);
418 /* used in enumerateUsedIDs */
419 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
422 int num = swf_GetU8(tag) & 15;
423 if(gradient1) gradient1->num = num;
424 if(gradient2) gradient2->num = num;
427 gradient1->num = num;
428 gradient1->rgba = rfx_calloc(sizeof(RGBA)*gradient1->num);
429 gradient1->ratios = rfx_calloc(sizeof(gradient1->ratios[0])*gradient1->num);
432 gradient2->num = num;
433 gradient2->rgba = rfx_calloc(sizeof(RGBA)*gradient2->num);
434 gradient2->ratios = rfx_calloc(sizeof(gradient2->ratios[0])*gradient2->num);
441 ratio = swf_GetU8(tag);
442 swf_GetRGBA(tag, &color);
444 gradient1->ratios[t] = ratio;
445 gradient1->rgba[t] = color;
448 ratio = swf_GetU8(tag);
449 swf_GetRGBA(tag, &color);
451 gradient2->ratios[t] = ratio;
452 gradient2->rgba[t] = color;
457 #define DEBUG_ENUMERATE if(0)
458 //#define DEBUG_ENUMERATE
460 void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
464 count = swf_GetU8(tag);
465 if(count == 0xff && num>1) // defineshape2,3,4 only
466 count = swf_GetU16(tag);
468 DEBUG_ENUMERATE printf("%d fill styles\n", count);
473 type = swf_GetU8(tag); //type
474 DEBUG_ENUMERATE printf("fill style %d) %02x (tagpos=%d)\n", t, type, tag->pos);
477 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
479 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
481 else if(type == 0x10 || type == 0x12 || type == 0x13)
483 swf_ResetReadBits(tag);
485 swf_GetMatrix(tag, &m);
486 DEBUG_ENUMERATE swf_DumpMatrix(stdout, &m);
488 swf_GetMatrix(tag, NULL);
489 swf_ResetReadBits(tag);
491 swf_GetMorphGradient(tag, NULL, NULL);
494 swf_GetGradient(tag, &g, /*alpha*/ num>=3?1:0);
495 DEBUG_ENUMERATE swf_DumpGradient(stdout, &g);
500 else if(type == 0x40 || type == 0x41 || type == 0x42 || type == 0x43)
502 swf_ResetReadBits(tag);
503 if(tag->data[tag->pos] != 0xff ||
504 tag->data[tag->pos+1] != 0xff)
505 (callback)(tag, tag->pos, callback_data);
508 swf_ResetReadBits(tag);
509 swf_GetMatrix(tag, NULL);
511 swf_GetMatrix(tag, NULL);
514 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x in tag %02x\n",type, tag->id);
517 swf_ResetReadBits(tag);
518 count = swf_GetU8(tag); // line style array
520 count = swf_GetU16(tag);
521 DEBUG_ENUMERATE printf("%d line styles\n", count);
526 width = swf_GetU16(tag);
530 U16 flags = swf_GetU16(tag);
532 swf_GetU16(tag); // miter limit
534 fprintf(stderr, "Filled strokes parsing not yet supported\n");
538 {swf_GetRGBA(tag, &color);if(morph) swf_GetRGBA(tag, NULL);}
540 {swf_GetRGB(tag, &color);if(morph) swf_GetRGB(tag, NULL);}
541 DEBUG_ENUMERATE printf("line style %d: %02x%02x%02x%02x \n", t, color.r,color.g,color.b,color.a);
545 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
548 swf_ResetReadBits(tag);
552 case ST_DEFINEBUTTONSOUND: {
554 callback(tag, tag->pos + base, callback_data);
557 callback(tag, tag->pos + base, callback_data);
558 swf_GetU16(tag); //sound id
559 flags = swf_GetU8(tag);
561 swf_GetU32(tag); // in point
563 swf_GetU32(tag); // out points
565 swf_GetU16(tag); // loop count
568 int npoints = swf_GetU8(tag);
570 for(s=0;s<npoints;s++)
579 case ST_DEFINEBUTTONCXFORM:
580 callback(tag, tag->pos + base, callback_data); //button id
583 case ST_EXPORTASSETS: {
584 int num = swf_GetU16(tag);
587 callback(tag, tag->pos + base, callback_data); //button id
588 swf_GetU16(tag); //id
589 while(swf_GetU8(tag)); //name
593 case ST_IMPORTASSETS:
594 case ST_IMPORTASSETS2: {
595 swf_GetString(tag); //count
596 swf_GetU8(tag); //reserved
597 swf_GetU8(tag); //reserved
598 int num = swf_GetU16(tag); //url
601 callback(tag, tag->pos + base, callback_data); //button id
602 swf_GetU16(tag); //id
603 while(swf_GetU8(tag)); //name
607 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
608 case ST_NAMECHARACTER:
609 case ST_DEFINEBINARY:
610 case ST_GENERATORTEXT:
611 callback(tag, tag->pos + base, callback_data);
614 callback(tag, tag->pos + base, callback_data);
616 case ST_PLACEOBJECT2:
617 // only if placeflaghascharacter
618 if(!(tag->data[0]&2))
620 callback(tag, 3 + base, callback_data);
622 case ST_PLACEOBJECT3:
623 // only if placeflaghascharacter
624 if(!(tag->data[0]&2))
626 callback(tag, 4 + base, callback_data);
628 case ST_REMOVEOBJECT:
629 callback(tag, tag->pos + base, callback_data);
632 callback(tag, tag->pos + base, callback_data);
634 case ST_DEFINESPRITE: {
636 break; // sprite is expanded
638 swf_GetU16(tag); // id
639 swf_GetU16(tag); // framenum
642 U16 flags = swf_GetU16(tag);
645 TAG *tag2 = swf_InsertTag(NULL, id);
648 len = swf_GetU32(tag);
651 tag2->len = tag2->memsize = len;
652 tag2->data = rfx_alloc(len);
653 memcpy(tag2->data, &tag->data[tag->pos], len);
654 /* I never saw recursive sprites, but they are (theoretically)
655 possible, so better add base here again */
656 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
658 swf_GetBlock(tag, NULL, len);
662 case ST_DEFINEBUTTON2: // has some font ids in the button records
665 case ST_DEFINEBUTTON: {
666 swf_GetU16(tag); //button id
670 swf_GetU8(tag); //flag
671 offset = swf_GetU16(tag); //offset
676 if(!swf_GetU8(tag)) //flags
678 callback(tag, tag->pos + base, callback_data);
679 swf_GetU16(tag); //char
680 swf_GetU16(tag); //layer
681 swf_ResetReadBits(tag);
682 swf_GetMatrix(tag, NULL);
684 swf_ResetReadBits(tag);
685 swf_GetCXForm(tag, NULL, 1);
691 case ST_DEFINEEDITTEXT: {
693 swf_GetU16(tag); //id
694 swf_GetRect(tag, NULL); //bounding box
695 swf_ResetReadBits(tag);
696 flags1 = swf_GetU8(tag);
697 flags2 = swf_GetU8(tag);
699 callback(tag, tag->pos + base, callback_data);
704 case ST_DEFINETEXT: {
705 int glyphbits, advancebits;
707 id = swf_GetU16(tag); //id
708 swf_GetRect(tag, NULL); //bounding box
709 swf_ResetReadBits(tag);
710 swf_GetMatrix(tag, NULL); //matrix
711 swf_ResetReadBits(tag);
712 glyphbits = swf_GetU8(tag); //glyphbits
713 advancebits = swf_GetU8(tag); //advancebits
717 swf_ResetReadBits(tag);
718 flags = swf_GetBits(tag, 8);
721 swf_ResetReadBits(tag);
722 if(flags & 8) { // hasfont
723 callback(tag, tag->pos + base, callback_data);
724 id = swf_GetU16(tag);
726 if(flags & 4) { // hascolor
727 if(num==1) swf_GetRGB(tag, NULL);
728 else swf_GetRGBA(tag, NULL);
730 if(flags & 2) { //has x offset
731 swf_ResetReadBits(tag);
734 if(flags & 1) { //has y offset
735 swf_ResetReadBits(tag);
738 if(flags & 8) { //has height
739 swf_ResetReadBits(tag);
743 flags = swf_GetBits(tag, 8);
745 swf_ResetReadBits(tag);
746 for(t=0;t<flags;t++) {
747 swf_GetBits(tag, glyphbits);
748 swf_GetBits(tag, advancebits);
753 case ST_DEFINEFONTALIGNZONES:
754 case ST_DEFINESCALINGGRID:
756 case ST_CSMTEXTSETTINGS:
757 case ST_DEFINEFONTINFO:
758 case ST_DEFINEFONTINFO2:
760 callback(tag, tag->pos + base, callback_data);
762 case ST_DEFINEVIDEOSTREAM:
765 case ST_DOINITACTION:
766 callback(tag, tag->pos + base, callback_data);
769 case ST_DEFINEMORPHSHAPE2:
770 case ST_DEFINESHAPE4:
772 case ST_DEFINEMORPHSHAPE:
773 case ST_DEFINESHAPE3:
775 case ST_DEFINESHAPE2:
777 case ST_DEFINESHAPE: {
783 if(tag->id == ST_DEFINEMORPHSHAPE || tag->id==ST_DEFINEMORPHSHAPE2) {
788 id = swf_GetU16(tag); // id;
789 SRECT r={0,0,0,0},r2={0,0,0,0};
790 swf_GetRect(tag, &r); // shape bounds
792 swf_ResetReadBits(tag);
793 swf_GetRect(tag, NULL); // shape bounds2
795 swf_ResetReadBits(tag);
796 swf_GetRect(tag, NULL); // edge bounds1
800 swf_ResetReadBits(tag);
801 swf_GetRect(tag, &r2); // edge bounds
802 U8 flags = swf_GetU8(tag); // flags, &1: contains scaling stroke, &2: contains non-scaling stroke
803 DEBUG_ENUMERATE printf("flags: %02x (1=scaling strokes, 2=non-scaling strokes)\n", flags);
806 swf_GetU32(tag); //offset to endedges
809 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
810 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);
811 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);
813 DEBUG_ENUMERATE printf("style tag pos: %d\n", tag->pos);
814 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
815 DEBUG_ENUMERATE printf("-------\n");
816 swf_ResetReadBits(tag);
817 while(--numshapes>=0) /* morph shapes define two shapes */
819 DEBUG_ENUMERATE printf("shape:%d\n", numshapes);
820 fillbits = swf_GetBits(tag, 4);
821 linebits = swf_GetBits(tag, 4);
822 DEBUG_ENUMERATE printf("fillbits=%d linebits=%d\n", fillbits, linebits);
823 swf_ResetReadBits(tag);
826 flags = swf_GetBits(tag, 1);
827 if(!flags) { //style change
828 flags = swf_GetBits(tag, 5);
832 int n = swf_GetBits(tag, 5);
834 x = swf_GetBits(tag, n); //x
835 y = swf_GetBits(tag, n); //y
836 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
838 if(flags&2) { //fill0
840 fill0 = swf_GetBits(tag, fillbits);
841 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
843 if(flags&4) { //fill1
845 fill1 = swf_GetBits(tag, fillbits);
846 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
848 if(flags&8) { //linestyle
850 line = swf_GetBits(tag, linebits);
851 DEBUG_ENUMERATE printf("linestyle %d\n",line);
854 DEBUG_ENUMERATE printf("more fillstyles\n");
855 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
856 fillbits = swf_GetBits(tag, 4);
857 linebits = swf_GetBits(tag, 4);
860 flags = swf_GetBits(tag, 1);
861 if(flags) { //straight edge
862 int n = swf_GetBits(tag, 4) + 2;
863 if(swf_GetBits(tag, 1)) { //line flag
865 x = swf_GetSBits(tag, n); //delta x
866 y = swf_GetSBits(tag, n); //delta y
867 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
869 int v=swf_GetBits(tag, 1);
871 d = swf_GetSBits(tag, n); //vert/horz
872 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
874 } else { //curved edge
875 int n = swf_GetBits(tag, 4) + 2;
877 x1 = swf_GetSBits(tag, n);
878 y1 = swf_GetSBits(tag, n);
879 x2 = swf_GetSBits(tag, n);
880 y2 = swf_GetSBits(tag, n);
881 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
893 void callbackCount(TAG * t,int pos, void*ptr)
896 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
899 void callbackFillin(TAG * t,int pos, void*ptr)
903 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
906 int swf_GetNumUsedIDs(TAG * t)
909 enumerateUsedIDs(t, 0, callbackCount, &num);
913 void swf_GetUsedIDs(TAG * t, int * positions)
915 int * ptr = positions;
916 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
919 void swf_Relocate (SWF*swf, char*bitmap)
923 memset(slaveids, -1, sizeof(slaveids));
931 if(swf_isDefiningTag(tag))
936 id = swf_GetDefineID(tag); //own id
938 if(!bitmap[id]) { //free
943 for (t=1;t<65536;t++)
953 slaveids[id] = newid;
955 swf_SetDefineID(tag, newid);
958 num = swf_GetNumUsedIDs(tag);
960 ptr = rfx_alloc(sizeof(int)*num);
961 swf_GetUsedIDs(tag, ptr);
964 int id = GET16(&tag->data[ptr[t]]);
966 fprintf(stderr, "swf_Relocate: Mapping id (%d) never encountered before in %s\n", id,
967 swf_TagGetName(tag));
970 PUT16(&tag->data[ptr[t]], id);
979 void swf_Relocate2(SWF*swf, int*id2id)
984 if(swf_isDefiningTag(tag)) {
985 int id = swf_GetDefineID(tag);
988 swf_SetDefineID(tag, id);
991 int num = swf_GetNumUsedIDs(tag);
995 ptr = rfx_alloc(sizeof(int)*num);
996 swf_GetUsedIDs(tag, ptr);
998 int id = GET16(&tag->data[ptr[t]]);
1001 PUT16(&tag->data[ptr[t]], id);
1008 void swf_RelocateDepth(SWF*swf, char*bitmap)
1012 tag = swf->firstTag;
1013 for(nr=65535;nr>=0;nr--) {
1017 // now nr is the highest used depth. So we start
1018 // assigning depths at nr+1
1024 /* TODO * clip depths
1027 if(tag->id == ST_PLACEOBJECT2) {
1029 swf_GetPlaceObject(tag, &obj);
1031 int newdepth = obj.clipdepth+nr;
1032 if(newdepth>65535) {
1033 fprintf(stderr, "Couldn't relocate depths: too large values\n");
1036 obj.clipdepth = newdepth;
1037 swf_ResetTag(tag, ST_PLACEOBJECT2);
1038 swf_SetPlaceObject(tag, &obj);
1040 swf_PlaceObjectFree(&obj);
1043 depth = swf_GetDepth(tag);
1045 int newdepth = depth+nr;
1046 if(newdepth>65535) {
1047 fprintf(stderr, "Couldn't relocate depths: too large values\n");
1050 swf_SetDepth(tag, newdepth);
1056 U8 swf_isShapeTag(TAG*tag)
1058 if(tag->id == ST_DEFINESHAPE ||
1059 tag->id == ST_DEFINESHAPE2 ||
1060 tag->id == ST_DEFINESHAPE3 ||
1061 tag->id == ST_DEFINESHAPE4)
1066 U8 swf_isPlaceTag(TAG*tag)
1068 if(tag->id == ST_PLACEOBJECT ||
1069 tag->id == ST_PLACEOBJECT2 ||
1070 tag->id == ST_PLACEOBJECT3)
1074 U8 swf_isTextTag(TAG*tag)
1076 if(tag->id == ST_DEFINETEXT ||
1077 tag->id == ST_DEFINETEXT2)
1082 U8 swf_isFontTag(TAG*tag)
1084 if(tag->id == ST_DEFINEFONT ||
1085 tag->id == ST_DEFINEFONT2 ||
1086 tag->id == ST_DEFINEFONTINFO)
1091 U8 swf_isImageTag(TAG*tag)
1093 if(tag->id == ST_DEFINEBITSJPEG ||
1094 tag->id == ST_DEFINEBITSJPEG2 ||
1095 tag->id == ST_DEFINEBITSJPEG3 ||
1096 tag->id == ST_DEFINEBITSLOSSLESS ||
1097 tag->id == ST_DEFINEBITSLOSSLESS2)
1102 TAG* swf_Concatenate (TAG*list1,TAG*list2)
1104 TAG*tag=0,*lasttag=0;
1106 char depthmap[65536];
1108 memset(bitmap, 0, sizeof(bitmap));
1109 memset(depthmap, 0, sizeof(depthmap));
1110 memset(&swf1, 0, sizeof(swf1));
1111 memset(&swf2, 0, sizeof(swf2));
1113 swf1.firstTag = list1;
1115 swf2.firstTag = list2;
1120 if(!swf_isDefiningTag(tag)) {
1121 int id = swf_GetDefineID(tag);
1124 if(tag->id == ST_PLACEOBJECT ||
1125 tag->id == ST_PLACEOBJECT2) {
1126 int depth = swf_GetDepth(tag);
1127 depthmap[depth] = 1;
1129 if(tag->id == ST_REMOVEOBJECT ||
1130 tag->id == ST_REMOVEOBJECT2) {
1131 int depth = swf_GetDepth(tag);
1132 depthmap[depth] = 0;
1137 swf_Relocate(&swf2, bitmap);
1138 swf_RelocateDepth(&swf2, depthmap);
1139 lasttag->next = swf2.firstTag;
1140 swf2.firstTag->prev = lasttag;
1142 return swf1.firstTag;
1145 static int tagHash(TAG*tag)
1148 unsigned int a = 0x6b973e5a;
1149 /* start at pos 2, as 0 and 1 are the id */
1150 for(t=2;t<tag->len;t++) {
1153 a += tag->data[t]*0xefbc35a5*b*(t+1);
1155 return a&0x7fffffff; //always return positive number
1158 void swf_Optimize(SWF*swf)
1160 const int hash_size = 131072;
1161 char* dontremap = rfx_calloc(sizeof(char)*65536);
1162 U16* remap = rfx_alloc(sizeof(U16)*65536);
1163 TAG* id2tag = rfx_calloc(sizeof(TAG*)*65536);
1164 TAG** hashmap = rfx_calloc(sizeof(TAG*)*hash_size);
1167 for(t=0;t<65536;t++) {
1173 tag = swf->firstTag;
1175 /* make sure we don't remap to this tag,
1176 as it might have different "helper tags"
1177 FIXME: a better way would be to compare
1178 the helper tags, too.
1180 if(swf_isPseudoDefiningTag(tag) &&
1181 tag->id != ST_NAMECHARACTER) {
1182 dontremap[swf_GetDefineID(tag)] = 1;
1186 tag = swf->firstTag;
1188 TAG*next = tag->next;
1191 int num = swf_GetNumUsedIDs(tag);
1192 int*positions = rfx_alloc(sizeof(int)*num);
1194 swf_GetUsedIDs(tag, positions);
1195 for(t=0;t<num;t++) {
1196 int id = GET16(&tag->data[positions[t]]);
1198 PUT16(&tag->data[positions[t]], id);
1200 rfx_free(positions);
1202 /* now look for previous tags with the same
1204 if(swf_isDefiningTag(tag)) {
1206 int id = swf_GetDefineID(tag);
1207 int hash = tagHash(tag);
1210 while((tag2 = hashmap[hash%hash_size])) {
1211 if(tag2 != (TAG*)0 && tag->len == tag2->len) {
1212 if(memcmp(&tag->data[2],&tag2->data[2],tag->len-2) == 0) {
1220 while(hashmap[hash%hash_size]) hash++;
1221 hashmap[hash%hash_size] = tag;
1223 /* we found two identical tags- remap one
1225 remap[id] = swf_GetDefineID(tag2);
1227 if(tag == swf->firstTag)
1228 swf->firstTag = next;
1230 } else if(swf_isPseudoDefiningTag(tag)) {
1231 int id = swf_GetDefineID(tag);
1233 /* if this tag was remapped, we don't
1234 need the helper tag anymore. Discard
1237 if(tag == swf->firstTag)
1238 swf->firstTag = next;
1245 rfx_free(dontremap);
1251 void swf_SetDefineBBox(TAG * tag, SRECT newbbox)
1255 swf_SetTagPos(tag,0);
1257 switch (swf_GetTagID(tag))
1259 case ST_DEFINESHAPE:
1260 case ST_DEFINESHAPE2:
1261 case ST_DEFINESHAPE3:
1262 case ST_DEFINEEDITTEXT:
1264 case ST_DEFINETEXT2:
1265 case ST_DEFINEVIDEOSTREAM: {
1266 U32 after_bbox_offset = 0, len;
1268 id = swf_GetU16(tag);
1269 swf_GetRect(tag, &b1);
1270 swf_ResetReadBits(tag);
1271 after_bbox_offset = tag->pos;
1272 len = tag->len - after_bbox_offset;
1274 memcpy(data, &tag->data[after_bbox_offset], len);
1277 swf_SetRect(tag, &newbbox);
1278 swf_SetBlock(tag, data, len);
1280 tag->pos = tag->readBit = 0;
1284 fprintf(stderr, "rfxswf: Tag %d (%s) has no bbox\n", tag->id, swf_TagGetName(tag));
1288 RGBA swf_GetSWFBackgroundColor(SWF*swf)
1290 TAG*t=swf->firstTag;
1292 color.r = color.b = color.g = 0;
1295 if(t->id == ST_SETBACKGROUNDCOLOR) {
1296 swf_SetTagPos(t, 0);
1297 color.r = swf_GetU8(t);
1298 color.g = swf_GetU8(t);
1299 color.b = swf_GetU8(t);