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[] =
228 ST_DEFINEMORPHSHAPE2,
238 ST_DEFINEBITSLOSSLESS,
239 ST_DEFINEBITSLOSSLESS2,
245 ST_DEFINEVIDEOSTREAM,
250 // tags which may be used inside a sprite definition
251 static int swf_spritetagids[] =
269 /* tags which add content or information to a character with a given ID */
270 static int swf_pseudodefiningtagids[] =
274 ST_DEFINEFONTALIGNZONES,
276 ST_DEFINEBUTTONCXFORM,
277 ST_DEFINEBUTTONSOUND,
278 ST_DEFINESCALINGGRID,
287 U8 swf_isAllowedSpriteTag(TAG * tag)
291 while(swf_spritetagids[t]>=0)
293 if(swf_spritetagids[t] == id)
300 U8 swf_isDefiningTag(TAG * tag)
304 while(swf_definingtagids[t]>=0)
306 if(swf_definingtagids[t] == id)
313 U8 swf_isPseudoDefiningTag(TAG * tag)
317 while(swf_pseudodefiningtagids[t]>=0)
319 if(swf_pseudodefiningtagids[t] == id)
326 int swf_GetDepth(TAG * t)
330 oldTagPos = swf_GetTagPos(t);
333 switch (swf_GetTagID(t))
334 { case ST_PLACEOBJECT:
335 case ST_REMOVEOBJECT:
337 depth = swf_GetU16(t);
339 case ST_REMOVEOBJECT2:
340 depth = swf_GetU16(t);
342 case ST_PLACEOBJECT2:
343 { U8 flags = swf_GetU8(t);
344 depth = swf_GetU16(t);
346 case ST_PLACEOBJECT3:
347 { U8 flags = swf_GetU8(t);
348 U8 flags2 = swf_GetU8(t);
349 depth = swf_GetU16(t);
353 depth = swf_GetU16(t);
356 swf_SetTagPos(t,oldTagPos);
360 void swf_SetDepth(TAG * t, U16 depth)
362 switch (swf_GetTagID(t))
363 { case ST_PLACEOBJECT:
364 case ST_REMOVEOBJECT:
365 PUT16(t->data, depth);
367 case ST_REMOVEOBJECT2:
368 PUT16(t->data, depth);
370 case ST_PLACEOBJECT2:
371 PUT16(&t->data[1], depth);
374 PUT16(t->data, depth);
377 fprintf(stderr, "rfxswf: Error: tag %d has no depth\n", t->id);
381 char* swf_GetName(TAG * t)
387 oldTagPos = swf_GetTagPos(t);
389 switch(swf_GetTagID(t))
392 name = (char*)&t->data[swf_GetTagPos(t)];
394 case ST_PLACEOBJECT3:
395 case ST_PLACEOBJECT2: {
396 U8 flags = swf_GetU8(t);
397 if(t->id == ST_PLACEOBJECT3)
399 swf_GetU16(t); //depth;
403 swf_GetMatrix(t, &m);
405 swf_GetCXForm(t, &c, 1);
408 if(flags&PF_CLIPDEPTH)
411 swf_ResetReadBits(t);
412 name = (char*)&t->data[swf_GetTagPos(t)];
417 swf_SetTagPos(t,oldTagPos);
421 /* used in enumerateUsedIDs */
422 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
425 int num = swf_GetU8(tag) & 15;
426 if(gradient1) gradient1->num = num;
427 if(gradient2) gradient2->num = num;
430 gradient1->num = num;
431 gradient1->rgba = (RGBA*)rfx_calloc(sizeof(RGBA)*gradient1->num);
432 gradient1->ratios = (U8*)rfx_calloc(sizeof(gradient1->ratios[0])*gradient1->num);
435 gradient2->num = num;
436 gradient2->rgba = (RGBA*)rfx_calloc(sizeof(RGBA)*gradient2->num);
437 gradient2->ratios = (U8*)rfx_calloc(sizeof(gradient2->ratios[0])*gradient2->num);
444 ratio = swf_GetU8(tag);
445 swf_GetRGBA(tag, &color);
447 gradient1->ratios[t] = ratio;
448 gradient1->rgba[t] = color;
451 ratio = swf_GetU8(tag);
452 swf_GetRGBA(tag, &color);
454 gradient2->ratios[t] = ratio;
455 gradient2->rgba[t] = color;
460 #define DEBUG_ENUMERATE if(0)
461 //#define DEBUG_ENUMERATE
463 void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
467 count = swf_GetU8(tag);
468 if(count == 0xff && num>1) // defineshape2,3,4 only
469 count = swf_GetU16(tag);
471 DEBUG_ENUMERATE printf("%d fill styles\n", count);
475 type = swf_GetU8(tag); //type
476 DEBUG_ENUMERATE printf("fill style %d) %02x (tagpos=%d)\n", t, type, tag->pos);
479 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
481 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
483 else if(type == 0x10 || type == 0x12 || type == 0x13)
485 swf_ResetReadBits(tag);
487 swf_GetMatrix(tag, &m);
488 DEBUG_ENUMERATE swf_DumpMatrix(stdout, &m);
490 swf_GetMatrix(tag, NULL);
491 swf_ResetReadBits(tag);
493 swf_GetMorphGradient(tag, NULL, NULL);
496 swf_GetGradient(tag, &g, /*alpha*/ num>=3?1:0);
497 DEBUG_ENUMERATE swf_DumpGradient(stdout, &g);
502 else if(type == 0x40 || type == 0x41 || type == 0x42 || type == 0x43)
504 swf_ResetReadBits(tag);
505 if(tag->data[tag->pos] != 0xff ||
506 tag->data[tag->pos+1] != 0xff)
507 (callback)(tag, tag->pos, callback_data);
510 swf_ResetReadBits(tag);
511 swf_GetMatrix(tag, NULL);
513 swf_GetMatrix(tag, NULL);
516 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x in tag %02x\n",type, tag->id);
519 swf_ResetReadBits(tag);
520 count = swf_GetU8(tag); // line style array
522 count = swf_GetU16(tag);
523 DEBUG_ENUMERATE printf("%d line styles\n", count);
528 width = swf_GetU16(tag);
532 U16 flags = swf_GetU16(tag);
534 swf_GetU16(tag); // miter limit
536 fprintf(stderr, "Filled strokes parsing not yet supported\n");
540 {swf_GetRGBA(tag, &color);if(morph) swf_GetRGBA(tag, NULL);}
542 {swf_GetRGB(tag, &color);if(morph) swf_GetRGB(tag, NULL);}
543 DEBUG_ENUMERATE printf("line style %d: %02x%02x%02x%02x \n", t, color.r,color.g,color.b,color.a);
547 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
550 swf_ResetReadBits(tag);
554 case ST_DEFINEBUTTONSOUND: {
556 callback(tag, tag->pos + base, callback_data);
559 callback(tag, tag->pos + base, callback_data);
560 swf_GetU16(tag); //sound id
561 flags = swf_GetU8(tag);
563 swf_GetU32(tag); // in point
565 swf_GetU32(tag); // out points
567 swf_GetU16(tag); // loop count
570 int npoints = swf_GetU8(tag);
572 for(s=0;s<npoints;s++)
581 case ST_DEFINEBUTTONCXFORM:
582 callback(tag, tag->pos + base, callback_data); //button id
585 case ST_EXPORTASSETS: {
586 int num = swf_GetU16(tag);
589 callback(tag, tag->pos + base, callback_data); //button id
590 swf_GetU16(tag); //id
591 while(swf_GetU8(tag)); //name
595 case ST_IMPORTASSETS:
596 case ST_IMPORTASSETS2: {
597 swf_GetString(tag); //count
598 swf_GetU8(tag); //reserved
599 swf_GetU8(tag); //reserved
600 int num = swf_GetU16(tag); //url
603 callback(tag, tag->pos + base, callback_data); //button id
604 swf_GetU16(tag); //id
605 while(swf_GetU8(tag)); //name
609 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
610 case ST_NAMECHARACTER:
611 case ST_DEFINEBINARY:
612 case ST_DEFINEFONTNAME:
613 case ST_GENERATORTEXT:
614 callback(tag, tag->pos + base, callback_data);
617 callback(tag, tag->pos + base, callback_data);
619 case ST_PLACEOBJECT2:
620 // only if placeflaghascharacter
621 if(!(tag->data[0]&2))
623 callback(tag, 3 + base, callback_data);
625 case ST_PLACEOBJECT3:
626 // only if placeflaghascharacter
627 if(!(tag->data[0]&2))
629 callback(tag, 4 + base, callback_data);
631 case ST_REMOVEOBJECT:
632 callback(tag, tag->pos + base, callback_data);
635 callback(tag, tag->pos + base, callback_data);
637 case ST_DEFINESPRITE: {
639 break; // sprite is expanded
641 swf_GetU16(tag); // id
642 swf_GetU16(tag); // framenum
645 U16 flags = swf_GetU16(tag);
648 TAG *tag2 = swf_InsertTag(NULL, id);
651 len = swf_GetU32(tag);
654 tag2->len = tag2->memsize = len;
655 tag2->data = (U8*)rfx_alloc(len);
656 memcpy(tag2->data, &tag->data[tag->pos], len);
657 /* I never saw recursive sprites, but they are (theoretically)
658 possible, so better add base here again */
659 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
661 swf_GetBlock(tag, NULL, len);
665 case ST_DEFINEBUTTON2: // has some font ids in the button records
668 case ST_DEFINEBUTTON: {
669 swf_GetU16(tag); //button id
673 swf_GetU8(tag); //flag
674 offset = swf_GetU16(tag); //offset
678 if(!swf_GetU8(tag)) //flags
680 callback(tag, tag->pos + base, callback_data);
681 swf_GetU16(tag); //char
682 swf_GetU16(tag); //layer
683 swf_ResetReadBits(tag);
684 swf_GetMatrix(tag, NULL);
686 swf_ResetReadBits(tag);
687 swf_GetCXForm(tag, NULL, 1);
693 case ST_DEFINEEDITTEXT: {
695 swf_GetU16(tag); //id
696 swf_GetRect(tag, NULL); //bounding box
697 swf_ResetReadBits(tag);
698 flags1 = swf_GetU8(tag);
699 flags2 = swf_GetU8(tag);
701 callback(tag, tag->pos + base, callback_data);
706 case ST_DEFINETEXT: {
707 int glyphbits, advancebits;
709 id = swf_GetU16(tag); //id
710 swf_GetRect(tag, NULL); //bounding box
711 swf_ResetReadBits(tag);
712 swf_GetMatrix(tag, NULL); //matrix
713 swf_ResetReadBits(tag);
714 glyphbits = swf_GetU8(tag); //glyphbits
715 advancebits = swf_GetU8(tag); //advancebits
719 swf_ResetReadBits(tag);
720 flags = swf_GetBits(tag, 8);
723 swf_ResetReadBits(tag);
724 if(flags & 8) { // hasfont
725 callback(tag, tag->pos + base, callback_data);
726 id = swf_GetU16(tag);
728 if(flags & 4) { // hascolor
729 if(num==1) swf_GetRGB(tag, NULL);
730 else swf_GetRGBA(tag, NULL);
732 if(flags & 2) { //has x offset
733 swf_ResetReadBits(tag);
736 if(flags & 1) { //has y offset
737 swf_ResetReadBits(tag);
740 if(flags & 8) { //has height
741 swf_ResetReadBits(tag);
745 flags = swf_GetBits(tag, 8);
747 swf_ResetReadBits(tag);
748 for(t=0;t<flags;t++) {
749 swf_GetBits(tag, glyphbits);
750 swf_GetBits(tag, advancebits);
755 case ST_DEFINEFONTALIGNZONES:
756 case ST_DEFINESCALINGGRID:
758 case ST_CSMTEXTSETTINGS:
759 case ST_DEFINEFONTINFO:
760 case ST_DEFINEFONTINFO2:
762 callback(tag, tag->pos + base, callback_data);
764 case ST_DEFINEVIDEOSTREAM:
767 case ST_DOINITACTION:
768 callback(tag, tag->pos + base, callback_data);
771 case ST_DEFINEMORPHSHAPE2:
772 case ST_DEFINESHAPE4:
774 case ST_DEFINEMORPHSHAPE:
775 case ST_DEFINESHAPE3:
777 case ST_DEFINESHAPE2:
779 case ST_DEFINESHAPE: {
785 if(tag->id == ST_DEFINEMORPHSHAPE || tag->id==ST_DEFINEMORPHSHAPE2) {
790 id = swf_GetU16(tag); // id;
791 SRECT r={0,0,0,0},r2={0,0,0,0};
792 swf_GetRect(tag, &r); // shape bounds
794 swf_ResetReadBits(tag);
795 swf_GetRect(tag, NULL); // shape bounds2
797 swf_ResetReadBits(tag);
798 swf_GetRect(tag, NULL); // edge bounds1
802 swf_ResetReadBits(tag);
803 swf_GetRect(tag, &r2); // edge bounds
804 U8 flags = swf_GetU8(tag); // flags, &1: contains scaling stroke, &2: contains non-scaling stroke
805 DEBUG_ENUMERATE printf("flags: %02x (1=scaling strokes, 2=non-scaling strokes)\n", flags);
808 swf_GetU32(tag); //offset to endedges
811 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
812 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);
813 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);
815 DEBUG_ENUMERATE printf("style tag pos: %d\n", tag->pos);
816 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
817 DEBUG_ENUMERATE printf("-------\n");
818 swf_ResetReadBits(tag);
819 while(--numshapes>=0) /* morph shapes define two shapes */
821 DEBUG_ENUMERATE printf("shape:%d\n", numshapes);
822 fillbits = swf_GetBits(tag, 4);
823 linebits = swf_GetBits(tag, 4);
824 DEBUG_ENUMERATE printf("fillbits=%d linebits=%d\n", fillbits, linebits);
825 swf_ResetReadBits(tag);
828 flags = swf_GetBits(tag, 1);
829 if(!flags) { //style change
830 flags = swf_GetBits(tag, 5);
834 int n = swf_GetBits(tag, 5);
836 x = swf_GetBits(tag, n); //x
837 y = swf_GetBits(tag, n); //y
838 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
840 if(flags&2) { //fill0
842 fill0 = swf_GetBits(tag, fillbits);
843 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
845 if(flags&4) { //fill1
847 fill1 = swf_GetBits(tag, fillbits);
848 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
850 if(flags&8) { //linestyle
852 line = swf_GetBits(tag, linebits);
853 DEBUG_ENUMERATE printf("linestyle %d\n",line);
856 DEBUG_ENUMERATE printf("more fillstyles\n");
857 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
858 fillbits = swf_GetBits(tag, 4);
859 linebits = swf_GetBits(tag, 4);
862 flags = swf_GetBits(tag, 1);
863 if(flags) { //straight edge
864 int n = swf_GetBits(tag, 4) + 2;
865 if(swf_GetBits(tag, 1)) { //line flag
867 x = swf_GetSBits(tag, n); //delta x
868 y = swf_GetSBits(tag, n); //delta y
869 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
871 int v=swf_GetBits(tag, 1);
873 d = swf_GetSBits(tag, n); //vert/horz
874 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
876 } else { //curved edge
877 int n = swf_GetBits(tag, 4) + 2;
879 x1 = swf_GetSBits(tag, n);
880 y1 = swf_GetSBits(tag, n);
881 x2 = swf_GetSBits(tag, n);
882 y2 = swf_GetSBits(tag, n);
883 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
895 void callbackCount(TAG * t,int pos, void*ptr)
898 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
901 void callbackFillin(TAG * t,int pos, void*ptr)
905 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
908 int swf_GetNumUsedIDs(TAG * t)
911 enumerateUsedIDs(t, 0, callbackCount, &num);
915 void swf_GetUsedIDs(TAG * t, int * positions)
917 int * ptr = positions;
918 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
921 void swf_Relocate (SWF*swf, char*bitmap)
925 memset(slaveids, -1, sizeof(slaveids));
933 if(swf_isDefiningTag(tag))
938 id = swf_GetDefineID(tag); //own id
940 if(!bitmap[id]) { //free
945 for (t=1;t<65536;t++)
955 slaveids[id] = newid;
957 swf_SetDefineID(tag, newid);
960 num = swf_GetNumUsedIDs(tag);
962 ptr = (int*)rfx_alloc(sizeof(int)*num);
963 swf_GetUsedIDs(tag, ptr);
966 int id = GET16(&tag->data[ptr[t]]);
968 fprintf(stderr, "swf_Relocate: Mapping id (%d) never encountered before in %s\n", id,
969 swf_TagGetName(tag));
972 PUT16(&tag->data[ptr[t]], id);
981 void swf_Relocate2(SWF*swf, int*id2id)
986 if(swf_isDefiningTag(tag)) {
987 int id = swf_GetDefineID(tag);
990 swf_SetDefineID(tag, id);
993 int num = swf_GetNumUsedIDs(tag);
997 ptr = (int*)rfx_alloc(sizeof(int)*num);
998 swf_GetUsedIDs(tag, ptr);
1000 int id = GET16(&tag->data[ptr[t]]);
1003 PUT16(&tag->data[ptr[t]], id);
1010 void swf_RelocateDepth(SWF*swf, char*bitmap)
1014 tag = swf->firstTag;
1015 for(nr=65535;nr>=0;nr--) {
1019 // now nr is the highest used depth. So we start
1020 // assigning depths at nr+1
1026 /* TODO * clip depths
1029 if(tag->id == ST_PLACEOBJECT2) {
1031 swf_GetPlaceObject(tag, &obj);
1033 int newdepth = obj.clipdepth+nr;
1034 if(newdepth>65535) {
1035 fprintf(stderr, "Couldn't relocate depths: too large values\n");
1038 obj.clipdepth = newdepth;
1039 swf_ResetTag(tag, ST_PLACEOBJECT2);
1040 swf_SetPlaceObject(tag, &obj);
1042 swf_PlaceObjectFree(&obj);
1045 depth = swf_GetDepth(tag);
1047 int newdepth = depth+nr;
1048 if(newdepth>65535) {
1049 fprintf(stderr, "Couldn't relocate depths: too large values\n");
1052 swf_SetDepth(tag, newdepth);
1058 U8 swf_isShapeTag(TAG*tag)
1060 if(tag->id == ST_DEFINESHAPE ||
1061 tag->id == ST_DEFINESHAPE2 ||
1062 tag->id == ST_DEFINESHAPE3 ||
1063 tag->id == ST_DEFINESHAPE4)
1068 U8 swf_isPlaceTag(TAG*tag)
1070 if(tag->id == ST_PLACEOBJECT ||
1071 tag->id == ST_PLACEOBJECT2 ||
1072 tag->id == ST_PLACEOBJECT3)
1076 U8 swf_isTextTag(TAG*tag)
1078 if(tag->id == ST_DEFINETEXT ||
1079 tag->id == ST_DEFINETEXT2)
1084 U8 swf_isFontTag(TAG*tag)
1086 if(tag->id == ST_DEFINEFONT ||
1087 tag->id == ST_DEFINEFONT2 ||
1088 tag->id == ST_DEFINEFONTINFO)
1093 U8 swf_isImageTag(TAG*tag)
1095 if(tag->id == ST_DEFINEBITSJPEG ||
1096 tag->id == ST_DEFINEBITSJPEG2 ||
1097 tag->id == ST_DEFINEBITSJPEG3 ||
1098 tag->id == ST_DEFINEBITSLOSSLESS ||
1099 tag->id == ST_DEFINEBITSLOSSLESS2)
1104 TAG* swf_Concatenate (TAG*list1,TAG*list2)
1106 TAG*tag=0,*lasttag=0;
1108 char depthmap[65536];
1110 memset(bitmap, 0, sizeof(bitmap));
1111 memset(depthmap, 0, sizeof(depthmap));
1112 memset(&swf1, 0, sizeof(swf1));
1113 memset(&swf2, 0, sizeof(swf2));
1115 swf1.firstTag = list1;
1117 swf2.firstTag = list2;
1122 if(!swf_isDefiningTag(tag)) {
1123 int id = swf_GetDefineID(tag);
1126 if(tag->id == ST_PLACEOBJECT ||
1127 tag->id == ST_PLACEOBJECT2) {
1128 int depth = swf_GetDepth(tag);
1129 depthmap[depth] = 1;
1131 if(tag->id == ST_REMOVEOBJECT ||
1132 tag->id == ST_REMOVEOBJECT2) {
1133 int depth = swf_GetDepth(tag);
1134 depthmap[depth] = 0;
1139 swf_Relocate(&swf2, bitmap);
1140 swf_RelocateDepth(&swf2, depthmap);
1141 lasttag->next = swf2.firstTag;
1142 swf2.firstTag->prev = lasttag;
1144 return swf1.firstTag;
1147 static int tagHash(TAG*tag)
1150 unsigned int a = 0x6b973e5a;
1151 /* start at pos 2, as 0 and 1 are the id */
1152 for(t=2;t<tag->len;t++) {
1155 a += tag->data[t]*0xefbc35a5*b*(t+1);
1157 return a&0x7fffffff; //always return positive number
1160 void swf_Optimize(SWF*swf)
1162 const int hash_size = 131072;
1163 char* dontremap = (char*)rfx_calloc(sizeof(char)*65536);
1164 U16* remap = (U16*)rfx_alloc(sizeof(U16)*65536);
1165 TAG* id2tag = (TAG*)rfx_calloc(sizeof(TAG*)*65536);
1166 TAG** hashmap = (TAG**)rfx_calloc(sizeof(TAG*)*hash_size);
1169 for(t=0;t<65536;t++) {
1175 tag = swf->firstTag;
1177 /* make sure we don't remap to this tag,
1178 as it might have different "helper tags"
1179 FIXME: a better way would be to compare
1180 the helper tags, too.
1182 if(swf_isPseudoDefiningTag(tag) &&
1183 tag->id != ST_NAMECHARACTER) {
1184 dontremap[swf_GetDefineID(tag)] = 1;
1188 tag = swf->firstTag;
1190 TAG*next = tag->next;
1193 int num = swf_GetNumUsedIDs(tag);
1194 int*positions = (int*)rfx_alloc(sizeof(int)*num);
1196 swf_GetUsedIDs(tag, positions);
1197 for(t=0;t<num;t++) {
1198 int id = GET16(&tag->data[positions[t]]);
1200 PUT16(&tag->data[positions[t]], id);
1202 rfx_free(positions);
1204 /* now look for previous tags with the same
1206 if(swf_isDefiningTag(tag)) {
1208 int id = swf_GetDefineID(tag);
1209 int hash = tagHash(tag);
1212 while((tag2 = hashmap[hash%hash_size])) {
1213 if(tag2 != (TAG*)0 && tag->len == tag2->len) {
1214 if(memcmp(&tag->data[2],&tag2->data[2],tag->len-2) == 0) {
1222 while(hashmap[hash%hash_size]) hash++;
1223 hashmap[hash%hash_size] = tag;
1225 /* we found two identical tags- remap one
1227 remap[id] = swf_GetDefineID(tag2);
1229 if(tag == swf->firstTag)
1230 swf->firstTag = next;
1232 } else if(swf_isPseudoDefiningTag(tag)) {
1233 int id = swf_GetDefineID(tag);
1235 /* if this tag was remapped, we don't
1236 need the helper tag anymore. Discard
1239 if(tag == swf->firstTag)
1240 swf->firstTag = next;
1247 rfx_free(dontremap);
1253 void swf_SetDefineBBox(TAG * tag, SRECT newbbox)
1257 swf_SetTagPos(tag,0);
1259 switch (swf_GetTagID(tag))
1261 case ST_DEFINESHAPE:
1262 case ST_DEFINESHAPE2:
1263 case ST_DEFINESHAPE3:
1264 case ST_DEFINEEDITTEXT:
1266 case ST_DEFINETEXT2:
1267 case ST_DEFINEVIDEOSTREAM: {
1268 U32 after_bbox_offset = 0, len;
1270 id = swf_GetU16(tag);
1271 swf_GetRect(tag, &b1);
1272 swf_ResetReadBits(tag);
1273 after_bbox_offset = tag->pos;
1274 len = tag->len - after_bbox_offset;
1275 data = (U8*)malloc(len);
1276 memcpy(data, &tag->data[after_bbox_offset], len);
1279 swf_SetRect(tag, &newbbox);
1280 swf_SetBlock(tag, data, len);
1282 tag->pos = tag->readBit = 0;
1286 fprintf(stderr, "rfxswf: Tag %d (%s) has no bbox\n", tag->id, swf_TagGetName(tag));
1290 RGBA swf_GetSWFBackgroundColor(SWF*swf)
1292 TAG*t=swf->firstTag;
1294 color.r = color.b = color.g = 0;
1297 if(t->id == ST_SETBACKGROUNDCOLOR) {
1298 swf_SetTagPos(t, 0);
1299 color.r = swf_GetU8(t);
1300 color.g = swf_GetU8(t);
1301 color.b = swf_GetU8(t);