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
121 case ST_DEFINEFONTNAME: //pseudodefine
125 case ST_DEFINESPRITE:
127 case ST_DEFINEVIDEOSTREAM:
128 case ST_GLYPHNAMES: //pseudodefine
129 case ST_VIDEOFRAME: //pseudodefine
130 case ST_NAMECHARACTER: //pseudodefine
131 case ST_DOINITACTION: //pseudodefine
135 fprintf(stderr, "rfxswf: Error: tag %d (%s) has no id\n", t->id, swf_TagGetName(t));
138 swf_SetTagPos(t,oldTagPos);
143 SRECT swf_GetDefineBBox(TAG * t)
148 memset(&b1, 0, sizeof(b1));
150 oldTagPos = swf_GetTagPos(t);
155 switch (swf_GetTagID(t))
156 { case ST_DEFINESHAPE:
157 case ST_DEFINESHAPE2:
158 case ST_DEFINESHAPE3:
159 case ST_DEFINESHAPE4:
160 case ST_DEFINEEDITTEXT:
163 case ST_DEFINEVIDEOSTREAM:
167 case ST_DEFINEMORPHSHAPE:
171 swf_ExpandRect2(&b1, &b2);
173 case ST_DEFINEBITSLOSSLESS:
174 case ST_DEFINEBITSLOSSLESS2:
176 case ST_DEFINEBITSJPEG2:
177 case ST_DEFINEBITSJPEG3:
182 swf_SetTagPos(t,oldTagPos);
187 U16 swf_GetPlaceID(TAG * t)
192 oldTagPos = swf_GetTagPos(t);
195 switch (swf_GetTagID(t))
196 { case ST_PLACEOBJECT:
197 case ST_REMOVEOBJECT:
198 case ST_FREECHARACTER:
203 case ST_PLACEOBJECT2:
204 { U8 flags = swf_GetU8(t);
205 U16 d = swf_GetU16(t);
206 id = (flags&PF_CHAR)?swf_GetU16(t):id;
208 case ST_PLACEOBJECT3:
209 { U8 flags = swf_GetU8(t);
210 U8 flags2 = swf_GetU8(t);
211 U16 d = swf_GetU16(t);
212 id = (flags&PF_CHAR)?swf_GetU16(t):id;
217 swf_SetTagPos(t,oldTagPos);
222 static int swf_definingtagids[] =
237 ST_DEFINEBITSLOSSLESS,
238 ST_DEFINEBITSLOSSLESS2,
244 ST_DEFINEVIDEOSTREAM,
249 // tags which may be used inside a sprite definition
250 static int swf_spritetagids[] =
268 /* tags which add content or information to a character with a given ID */
269 static int swf_pseudodefiningtagids[] =
273 ST_DEFINEFONTALIGNZONES,
275 ST_DEFINEBUTTONCXFORM,
276 ST_DEFINEBUTTONSOUND,
277 ST_DEFINESCALINGGRID,
286 U8 swf_isAllowedSpriteTag(TAG * tag)
290 while(swf_spritetagids[t]>=0)
292 if(swf_spritetagids[t] == id)
299 U8 swf_isDefiningTag(TAG * tag)
303 while(swf_definingtagids[t]>=0)
305 if(swf_definingtagids[t] == id)
312 U8 swf_isPseudoDefiningTag(TAG * tag)
316 while(swf_pseudodefiningtagids[t]>=0)
318 if(swf_pseudodefiningtagids[t] == id)
325 int swf_GetDepth(TAG * t)
329 oldTagPos = swf_GetTagPos(t);
332 switch (swf_GetTagID(t))
333 { case ST_PLACEOBJECT:
334 case ST_REMOVEOBJECT:
336 depth = swf_GetU16(t);
338 case ST_REMOVEOBJECT2:
339 depth = swf_GetU16(t);
341 case ST_PLACEOBJECT2:
342 { U8 flags = swf_GetU8(t);
343 depth = swf_GetU16(t);
345 case ST_PLACEOBJECT3:
346 { U8 flags = swf_GetU8(t);
347 U8 flags2 = swf_GetU8(t);
348 depth = swf_GetU16(t);
352 depth = swf_GetU16(t);
355 swf_SetTagPos(t,oldTagPos);
359 void swf_SetDepth(TAG * t, U16 depth)
361 switch (swf_GetTagID(t))
362 { case ST_PLACEOBJECT:
363 case ST_REMOVEOBJECT:
364 PUT16(t->data, depth);
366 case ST_REMOVEOBJECT2:
367 PUT16(t->data, depth);
369 case ST_PLACEOBJECT2:
370 PUT16(&t->data[1], depth);
373 PUT16(t->data, depth);
376 fprintf(stderr, "rfxswf: Error: tag %d has no depth\n", t->id);
380 char* swf_GetName(TAG * t)
386 oldTagPos = swf_GetTagPos(t);
388 switch(swf_GetTagID(t))
391 name = (char*)&t->data[swf_GetTagPos(t)];
393 case ST_PLACEOBJECT3:
394 case ST_PLACEOBJECT2: {
395 U8 flags = swf_GetU8(t);
396 if(t->id == ST_PLACEOBJECT3)
398 swf_GetU16(t); //depth;
402 swf_GetMatrix(t, &m);
404 swf_GetCXForm(t, &c, 1);
407 if(flags&PF_CLIPDEPTH)
410 swf_ResetReadBits(t);
411 name = (char*)&t->data[swf_GetTagPos(t)];
416 swf_SetTagPos(t,oldTagPos);
420 /* used in enumerateUsedIDs */
421 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
424 int num = swf_GetU8(tag) & 15;
425 if(gradient1) gradient1->num = num;
426 if(gradient2) gradient2->num = num;
429 gradient1->num = num;
430 gradient1->rgba = (RGBA*)rfx_calloc(sizeof(RGBA)*gradient1->num);
431 gradient1->ratios = (U8*)rfx_calloc(sizeof(gradient1->ratios[0])*gradient1->num);
434 gradient2->num = num;
435 gradient2->rgba = (RGBA*)rfx_calloc(sizeof(RGBA)*gradient2->num);
436 gradient2->ratios = (U8*)rfx_calloc(sizeof(gradient2->ratios[0])*gradient2->num);
443 ratio = swf_GetU8(tag);
444 swf_GetRGBA(tag, &color);
446 gradient1->ratios[t] = ratio;
447 gradient1->rgba[t] = color;
450 ratio = swf_GetU8(tag);
451 swf_GetRGBA(tag, &color);
453 gradient2->ratios[t] = ratio;
454 gradient2->rgba[t] = color;
459 #define DEBUG_ENUMERATE if(0)
460 //#define DEBUG_ENUMERATE
462 void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
466 count = swf_GetU8(tag);
467 if(count == 0xff && num>1) // defineshape2,3,4 only
468 count = swf_GetU16(tag);
470 DEBUG_ENUMERATE printf("%d fill styles\n", count);
474 type = swf_GetU8(tag); //type
475 DEBUG_ENUMERATE printf("fill style %d) %02x (tagpos=%d)\n", t, type, tag->pos);
478 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
480 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
482 else if(type == 0x10 || type == 0x12 || type == 0x13)
484 swf_ResetReadBits(tag);
486 swf_GetMatrix(tag, &m);
487 DEBUG_ENUMERATE swf_DumpMatrix(stdout, &m);
489 swf_GetMatrix(tag, NULL);
490 swf_ResetReadBits(tag);
492 swf_GetMorphGradient(tag, NULL, NULL);
495 swf_GetGradient(tag, &g, /*alpha*/ num>=3?1:0);
496 DEBUG_ENUMERATE swf_DumpGradient(stdout, &g);
501 else if(type == 0x40 || type == 0x41 || type == 0x42 || type == 0x43)
503 swf_ResetReadBits(tag);
504 if(tag->data[tag->pos] != 0xff ||
505 tag->data[tag->pos+1] != 0xff)
506 (callback)(tag, tag->pos, callback_data);
509 swf_ResetReadBits(tag);
510 swf_GetMatrix(tag, NULL);
512 swf_GetMatrix(tag, NULL);
515 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x in tag %02x\n",type, tag->id);
518 swf_ResetReadBits(tag);
519 count = swf_GetU8(tag); // line style array
521 count = swf_GetU16(tag);
522 DEBUG_ENUMERATE printf("%d line styles\n", count);
527 width = swf_GetU16(tag);
531 U16 flags = swf_GetU16(tag);
533 swf_GetU16(tag); // miter limit
535 fprintf(stderr, "Filled strokes parsing not yet supported\n");
539 {swf_GetRGBA(tag, &color);if(morph) swf_GetRGBA(tag, NULL);}
541 {swf_GetRGB(tag, &color);if(morph) swf_GetRGB(tag, NULL);}
542 DEBUG_ENUMERATE printf("line style %d: %02x%02x%02x%02x \n", t, color.r,color.g,color.b,color.a);
546 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
549 swf_ResetReadBits(tag);
553 case ST_DEFINEBUTTONSOUND: {
555 callback(tag, tag->pos + base, callback_data);
558 callback(tag, tag->pos + base, callback_data);
559 swf_GetU16(tag); //sound id
560 flags = swf_GetU8(tag);
562 swf_GetU32(tag); // in point
564 swf_GetU32(tag); // out points
566 swf_GetU16(tag); // loop count
569 int npoints = swf_GetU8(tag);
571 for(s=0;s<npoints;s++)
580 case ST_DEFINEBUTTONCXFORM:
581 callback(tag, tag->pos + base, callback_data); //button id
584 case ST_EXPORTASSETS: {
585 int num = swf_GetU16(tag);
588 callback(tag, tag->pos + base, callback_data); //button id
589 swf_GetU16(tag); //id
590 while(swf_GetU8(tag)); //name
594 case ST_IMPORTASSETS:
595 case ST_IMPORTASSETS2: {
596 swf_GetString(tag); //count
597 swf_GetU8(tag); //reserved
598 swf_GetU8(tag); //reserved
599 int num = swf_GetU16(tag); //url
602 callback(tag, tag->pos + base, callback_data); //button id
603 swf_GetU16(tag); //id
604 while(swf_GetU8(tag)); //name
608 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
609 case ST_NAMECHARACTER:
610 case ST_DEFINEBINARY:
611 case ST_DEFINEFONTNAME:
612 case ST_GENERATORTEXT:
613 callback(tag, tag->pos + base, callback_data);
616 callback(tag, tag->pos + base, callback_data);
618 case ST_PLACEOBJECT2:
619 // only if placeflaghascharacter
620 if(!(tag->data[0]&2))
622 callback(tag, 3 + base, callback_data);
624 case ST_PLACEOBJECT3:
625 // only if placeflaghascharacter
626 if(!(tag->data[0]&2))
628 callback(tag, 4 + base, callback_data);
630 case ST_REMOVEOBJECT:
631 callback(tag, tag->pos + base, callback_data);
634 callback(tag, tag->pos + base, callback_data);
636 case ST_DEFINESPRITE: {
638 break; // sprite is expanded
640 swf_GetU16(tag); // id
641 swf_GetU16(tag); // framenum
644 U16 flags = swf_GetU16(tag);
647 TAG *tag2 = swf_InsertTag(NULL, id);
650 len = swf_GetU32(tag);
653 tag2->len = tag2->memsize = len;
654 tag2->data = (U8*)rfx_alloc(len);
655 memcpy(tag2->data, &tag->data[tag->pos], len);
656 /* I never saw recursive sprites, but they are (theoretically)
657 possible, so better add base here again */
658 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
660 swf_GetBlock(tag, NULL, len);
664 case ST_DEFINEBUTTON2: // has some font ids in the button records
667 case ST_DEFINEBUTTON: {
668 swf_GetU16(tag); //button id
672 swf_GetU8(tag); //flag
673 offset = swf_GetU16(tag); //offset
677 if(!swf_GetU8(tag)) //flags
679 callback(tag, tag->pos + base, callback_data);
680 swf_GetU16(tag); //char
681 swf_GetU16(tag); //layer
682 swf_ResetReadBits(tag);
683 swf_GetMatrix(tag, NULL);
685 swf_ResetReadBits(tag);
686 swf_GetCXForm(tag, NULL, 1);
692 case ST_DEFINEEDITTEXT: {
694 swf_GetU16(tag); //id
695 swf_GetRect(tag, NULL); //bounding box
696 swf_ResetReadBits(tag);
697 flags1 = swf_GetU8(tag);
698 flags2 = swf_GetU8(tag);
700 callback(tag, tag->pos + base, callback_data);
705 case ST_DEFINETEXT: {
706 int glyphbits, advancebits;
708 id = swf_GetU16(tag); //id
709 swf_GetRect(tag, NULL); //bounding box
710 swf_ResetReadBits(tag);
711 swf_GetMatrix(tag, NULL); //matrix
712 swf_ResetReadBits(tag);
713 glyphbits = swf_GetU8(tag); //glyphbits
714 advancebits = swf_GetU8(tag); //advancebits
718 swf_ResetReadBits(tag);
719 flags = swf_GetBits(tag, 8);
722 swf_ResetReadBits(tag);
723 if(flags & 8) { // hasfont
724 callback(tag, tag->pos + base, callback_data);
725 id = swf_GetU16(tag);
727 if(flags & 4) { // hascolor
728 if(num==1) swf_GetRGB(tag, NULL);
729 else swf_GetRGBA(tag, NULL);
731 if(flags & 2) { //has x offset
732 swf_ResetReadBits(tag);
735 if(flags & 1) { //has y offset
736 swf_ResetReadBits(tag);
739 if(flags & 8) { //has height
740 swf_ResetReadBits(tag);
744 flags = swf_GetBits(tag, 8);
746 swf_ResetReadBits(tag);
747 for(t=0;t<flags;t++) {
748 swf_GetBits(tag, glyphbits);
749 swf_GetBits(tag, advancebits);
754 case ST_DEFINEFONTALIGNZONES:
755 case ST_DEFINESCALINGGRID:
757 case ST_CSMTEXTSETTINGS:
758 case ST_DEFINEFONTINFO:
759 case ST_DEFINEFONTINFO2:
761 callback(tag, tag->pos + base, callback_data);
763 case ST_DEFINEVIDEOSTREAM:
766 case ST_DOINITACTION:
767 callback(tag, tag->pos + base, callback_data);
770 case ST_DEFINEMORPHSHAPE2:
771 case ST_DEFINESHAPE4:
773 case ST_DEFINEMORPHSHAPE:
774 case ST_DEFINESHAPE3:
776 case ST_DEFINESHAPE2:
778 case ST_DEFINESHAPE: {
784 if(tag->id == ST_DEFINEMORPHSHAPE || tag->id==ST_DEFINEMORPHSHAPE2) {
789 id = swf_GetU16(tag); // id;
790 SRECT r={0,0,0,0},r2={0,0,0,0};
791 swf_GetRect(tag, &r); // shape bounds
793 swf_ResetReadBits(tag);
794 swf_GetRect(tag, NULL); // shape bounds2
796 swf_ResetReadBits(tag);
797 swf_GetRect(tag, NULL); // edge bounds1
801 swf_ResetReadBits(tag);
802 swf_GetRect(tag, &r2); // edge bounds
803 U8 flags = swf_GetU8(tag); // flags, &1: contains scaling stroke, &2: contains non-scaling stroke
804 DEBUG_ENUMERATE printf("flags: %02x (1=scaling strokes, 2=non-scaling strokes)\n", flags);
807 swf_GetU32(tag); //offset to endedges
810 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
811 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);
812 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);
814 DEBUG_ENUMERATE printf("style tag pos: %d\n", tag->pos);
815 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
816 DEBUG_ENUMERATE printf("-------\n");
817 swf_ResetReadBits(tag);
818 while(--numshapes>=0) /* morph shapes define two shapes */
820 DEBUG_ENUMERATE printf("shape:%d\n", numshapes);
821 fillbits = swf_GetBits(tag, 4);
822 linebits = swf_GetBits(tag, 4);
823 DEBUG_ENUMERATE printf("fillbits=%d linebits=%d\n", fillbits, linebits);
824 swf_ResetReadBits(tag);
827 flags = swf_GetBits(tag, 1);
828 if(!flags) { //style change
829 flags = swf_GetBits(tag, 5);
833 int n = swf_GetBits(tag, 5);
835 x = swf_GetBits(tag, n); //x
836 y = swf_GetBits(tag, n); //y
837 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
839 if(flags&2) { //fill0
841 fill0 = swf_GetBits(tag, fillbits);
842 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
844 if(flags&4) { //fill1
846 fill1 = swf_GetBits(tag, fillbits);
847 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
849 if(flags&8) { //linestyle
851 line = swf_GetBits(tag, linebits);
852 DEBUG_ENUMERATE printf("linestyle %d\n",line);
855 DEBUG_ENUMERATE printf("more fillstyles\n");
856 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
857 fillbits = swf_GetBits(tag, 4);
858 linebits = swf_GetBits(tag, 4);
861 flags = swf_GetBits(tag, 1);
862 if(flags) { //straight edge
863 int n = swf_GetBits(tag, 4) + 2;
864 if(swf_GetBits(tag, 1)) { //line flag
866 x = swf_GetSBits(tag, n); //delta x
867 y = swf_GetSBits(tag, n); //delta y
868 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
870 int v=swf_GetBits(tag, 1);
872 d = swf_GetSBits(tag, n); //vert/horz
873 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
875 } else { //curved edge
876 int n = swf_GetBits(tag, 4) + 2;
878 x1 = swf_GetSBits(tag, n);
879 y1 = swf_GetSBits(tag, n);
880 x2 = swf_GetSBits(tag, n);
881 y2 = swf_GetSBits(tag, n);
882 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
894 void callbackCount(TAG * t,int pos, void*ptr)
897 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
900 void callbackFillin(TAG * t,int pos, void*ptr)
904 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
907 int swf_GetNumUsedIDs(TAG * t)
910 enumerateUsedIDs(t, 0, callbackCount, &num);
914 void swf_GetUsedIDs(TAG * t, int * positions)
916 int * ptr = positions;
917 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
920 void swf_Relocate (SWF*swf, char*bitmap)
924 memset(slaveids, -1, sizeof(slaveids));
932 if(swf_isDefiningTag(tag))
937 id = swf_GetDefineID(tag); //own id
939 if(!bitmap[id]) { //free
944 for (t=1;t<65536;t++)
954 slaveids[id] = newid;
956 swf_SetDefineID(tag, newid);
959 num = swf_GetNumUsedIDs(tag);
961 ptr = (int*)rfx_alloc(sizeof(int)*num);
962 swf_GetUsedIDs(tag, ptr);
965 int id = GET16(&tag->data[ptr[t]]);
967 fprintf(stderr, "swf_Relocate: Mapping id (%d) never encountered before in %s\n", id,
968 swf_TagGetName(tag));
971 PUT16(&tag->data[ptr[t]], id);
980 void swf_Relocate2(SWF*swf, int*id2id)
985 if(swf_isDefiningTag(tag)) {
986 int id = swf_GetDefineID(tag);
989 swf_SetDefineID(tag, id);
992 int num = swf_GetNumUsedIDs(tag);
996 ptr = (int*)rfx_alloc(sizeof(int)*num);
997 swf_GetUsedIDs(tag, ptr);
999 int id = GET16(&tag->data[ptr[t]]);
1002 PUT16(&tag->data[ptr[t]], id);
1009 void swf_RelocateDepth(SWF*swf, char*bitmap)
1013 tag = swf->firstTag;
1014 for(nr=65535;nr>=0;nr--) {
1018 // now nr is the highest used depth. So we start
1019 // assigning depths at nr+1
1025 /* TODO * clip depths
1028 if(tag->id == ST_PLACEOBJECT2) {
1030 swf_GetPlaceObject(tag, &obj);
1032 int newdepth = obj.clipdepth+nr;
1033 if(newdepth>65535) {
1034 fprintf(stderr, "Couldn't relocate depths: too large values\n");
1037 obj.clipdepth = newdepth;
1038 swf_ResetTag(tag, ST_PLACEOBJECT2);
1039 swf_SetPlaceObject(tag, &obj);
1041 swf_PlaceObjectFree(&obj);
1044 depth = swf_GetDepth(tag);
1046 int newdepth = depth+nr;
1047 if(newdepth>65535) {
1048 fprintf(stderr, "Couldn't relocate depths: too large values\n");
1051 swf_SetDepth(tag, newdepth);
1057 U8 swf_isShapeTag(TAG*tag)
1059 if(tag->id == ST_DEFINESHAPE ||
1060 tag->id == ST_DEFINESHAPE2 ||
1061 tag->id == ST_DEFINESHAPE3 ||
1062 tag->id == ST_DEFINESHAPE4)
1067 U8 swf_isPlaceTag(TAG*tag)
1069 if(tag->id == ST_PLACEOBJECT ||
1070 tag->id == ST_PLACEOBJECT2 ||
1071 tag->id == ST_PLACEOBJECT3)
1075 U8 swf_isTextTag(TAG*tag)
1077 if(tag->id == ST_DEFINETEXT ||
1078 tag->id == ST_DEFINETEXT2)
1083 U8 swf_isFontTag(TAG*tag)
1085 if(tag->id == ST_DEFINEFONT ||
1086 tag->id == ST_DEFINEFONT2 ||
1087 tag->id == ST_DEFINEFONTINFO)
1092 U8 swf_isImageTag(TAG*tag)
1094 if(tag->id == ST_DEFINEBITSJPEG ||
1095 tag->id == ST_DEFINEBITSJPEG2 ||
1096 tag->id == ST_DEFINEBITSJPEG3 ||
1097 tag->id == ST_DEFINEBITSLOSSLESS ||
1098 tag->id == ST_DEFINEBITSLOSSLESS2)
1103 TAG* swf_Concatenate (TAG*list1,TAG*list2)
1105 TAG*tag=0,*lasttag=0;
1107 char depthmap[65536];
1109 memset(bitmap, 0, sizeof(bitmap));
1110 memset(depthmap, 0, sizeof(depthmap));
1111 memset(&swf1, 0, sizeof(swf1));
1112 memset(&swf2, 0, sizeof(swf2));
1114 swf1.firstTag = list1;
1116 swf2.firstTag = list2;
1121 if(!swf_isDefiningTag(tag)) {
1122 int id = swf_GetDefineID(tag);
1125 if(tag->id == ST_PLACEOBJECT ||
1126 tag->id == ST_PLACEOBJECT2) {
1127 int depth = swf_GetDepth(tag);
1128 depthmap[depth] = 1;
1130 if(tag->id == ST_REMOVEOBJECT ||
1131 tag->id == ST_REMOVEOBJECT2) {
1132 int depth = swf_GetDepth(tag);
1133 depthmap[depth] = 0;
1138 swf_Relocate(&swf2, bitmap);
1139 swf_RelocateDepth(&swf2, depthmap);
1140 lasttag->next = swf2.firstTag;
1141 swf2.firstTag->prev = lasttag;
1143 return swf1.firstTag;
1146 static int tagHash(TAG*tag)
1149 unsigned int a = 0x6b973e5a;
1150 /* start at pos 2, as 0 and 1 are the id */
1151 for(t=2;t<tag->len;t++) {
1154 a += tag->data[t]*0xefbc35a5*b*(t+1);
1156 return a&0x7fffffff; //always return positive number
1159 void swf_Optimize(SWF*swf)
1161 const int hash_size = 131072;
1162 char* dontremap = (char*)rfx_calloc(sizeof(char)*65536);
1163 U16* remap = (U16*)rfx_alloc(sizeof(U16)*65536);
1164 TAG* id2tag = (TAG*)rfx_calloc(sizeof(TAG*)*65536);
1165 TAG** hashmap = (TAG**)rfx_calloc(sizeof(TAG*)*hash_size);
1168 for(t=0;t<65536;t++) {
1174 tag = swf->firstTag;
1176 /* make sure we don't remap to this tag,
1177 as it might have different "helper tags"
1178 FIXME: a better way would be to compare
1179 the helper tags, too.
1181 if(swf_isPseudoDefiningTag(tag) &&
1182 tag->id != ST_NAMECHARACTER) {
1183 dontremap[swf_GetDefineID(tag)] = 1;
1187 tag = swf->firstTag;
1189 TAG*next = tag->next;
1192 int num = swf_GetNumUsedIDs(tag);
1193 int*positions = (int*)rfx_alloc(sizeof(int)*num);
1195 swf_GetUsedIDs(tag, positions);
1196 for(t=0;t<num;t++) {
1197 int id = GET16(&tag->data[positions[t]]);
1199 PUT16(&tag->data[positions[t]], id);
1201 rfx_free(positions);
1203 /* now look for previous tags with the same
1205 if(swf_isDefiningTag(tag)) {
1207 int id = swf_GetDefineID(tag);
1208 int hash = tagHash(tag);
1211 while((tag2 = hashmap[hash%hash_size])) {
1212 if(tag2 != (TAG*)0 && tag->len == tag2->len) {
1213 if(memcmp(&tag->data[2],&tag2->data[2],tag->len-2) == 0) {
1221 while(hashmap[hash%hash_size]) hash++;
1222 hashmap[hash%hash_size] = tag;
1224 /* we found two identical tags- remap one
1226 remap[id] = swf_GetDefineID(tag2);
1228 if(tag == swf->firstTag)
1229 swf->firstTag = next;
1231 } else if(swf_isPseudoDefiningTag(tag)) {
1232 int id = swf_GetDefineID(tag);
1234 /* if this tag was remapped, we don't
1235 need the helper tag anymore. Discard
1238 if(tag == swf->firstTag)
1239 swf->firstTag = next;
1246 rfx_free(dontremap);
1252 void swf_SetDefineBBox(TAG * tag, SRECT newbbox)
1256 swf_SetTagPos(tag,0);
1258 switch (swf_GetTagID(tag))
1260 case ST_DEFINESHAPE:
1261 case ST_DEFINESHAPE2:
1262 case ST_DEFINESHAPE3:
1263 case ST_DEFINEEDITTEXT:
1265 case ST_DEFINETEXT2:
1266 case ST_DEFINEVIDEOSTREAM: {
1267 U32 after_bbox_offset = 0, len;
1269 id = swf_GetU16(tag);
1270 swf_GetRect(tag, &b1);
1271 swf_ResetReadBits(tag);
1272 after_bbox_offset = tag->pos;
1273 len = tag->len - after_bbox_offset;
1274 data = (U8*)malloc(len);
1275 memcpy(data, &tag->data[after_bbox_offset], len);
1278 swf_SetRect(tag, &newbbox);
1279 swf_SetBlock(tag, data, len);
1281 tag->pos = tag->readBit = 0;
1285 fprintf(stderr, "rfxswf: Tag %d (%s) has no bbox\n", tag->id, swf_TagGetName(tag));
1289 RGBA swf_GetSWFBackgroundColor(SWF*swf)
1291 TAG*t=swf->firstTag;
1293 color.r = color.b = color.g = 0;
1296 if(t->id == ST_SETBACKGROUNDCOLOR) {
1297 swf_SetTagPos(t, 0);
1298 color.r = swf_GetU8(t);
1299 color.g = swf_GetU8(t);
1300 color.b = swf_GetU8(t);