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_DEFINEEDITTEXT:
161 case ST_DEFINEVIDEOSTREAM:
165 case ST_DEFINEMORPHSHAPE:
169 swf_ExpandRect2(&b1, &b2);
171 case ST_DEFINEBITSLOSSLESS:
172 case ST_DEFINEBITSLOSSLESS2:
174 case ST_DEFINEBITSJPEG2:
175 case ST_DEFINEBITSJPEG3:
180 swf_SetTagPos(t,oldTagPos);
185 U16 swf_GetPlaceID(TAG * t)
190 oldTagPos = swf_GetTagPos(t);
193 switch (swf_GetTagID(t))
194 { case ST_PLACEOBJECT:
195 case ST_REMOVEOBJECT:
196 case ST_FREECHARACTER:
201 case ST_PLACEOBJECT2:
202 { U8 flags = swf_GetU8(t);
203 U16 d = swf_GetU16(t);
204 id = (flags&PF_CHAR)?swf_GetU16(t):id;
206 case ST_PLACEOBJECT3:
207 { U8 flags = swf_GetU8(t);
208 U8 flags2 = swf_GetU8(t);
209 U16 d = swf_GetU16(t);
210 id = (flags&PF_CHAR)?swf_GetU16(t):id;
215 swf_SetTagPos(t,oldTagPos);
220 static int swf_definingtagids[] =
235 ST_DEFINEBITSLOSSLESS,
236 ST_DEFINEBITSLOSSLESS2,
242 ST_DEFINEVIDEOSTREAM,
246 // tags which may be used inside a sprite definition
247 static int swf_spritetagids[] =
264 /* tags which add content or information to a character with a given ID */
265 static int swf_pseudodefiningtagids[] =
269 ST_DEFINEFONTALIGNZONES,
270 ST_DEFINEBUTTONCXFORM,
271 ST_DEFINEBUTTONSOUND,
272 ST_DEFINESCALINGGRID,
281 U8 swf_isAllowedSpriteTag(TAG * tag)
285 while(swf_spritetagids[t]>=0)
287 if(swf_spritetagids[t] == id)
294 U8 swf_isDefiningTag(TAG * tag)
298 while(swf_definingtagids[t]>=0)
300 if(swf_definingtagids[t] == id)
307 U8 swf_isPseudoDefiningTag(TAG * tag)
311 while(swf_pseudodefiningtagids[t]>=0)
313 if(swf_pseudodefiningtagids[t] == id)
320 int swf_GetDepth(TAG * t)
324 oldTagPos = swf_GetTagPos(t);
327 switch (swf_GetTagID(t))
328 { case ST_PLACEOBJECT:
329 case ST_REMOVEOBJECT:
331 depth = swf_GetU16(t);
333 case ST_REMOVEOBJECT2:
334 depth = swf_GetU16(t);
336 case ST_PLACEOBJECT2:
337 { U8 flags = swf_GetU8(t);
338 depth = swf_GetU16(t);
340 case ST_PLACEOBJECT3:
341 { U8 flags = swf_GetU8(t);
342 U8 flags2 = swf_GetU8(t);
343 depth = swf_GetU16(t);
347 depth = swf_GetU16(t);
350 swf_SetTagPos(t,oldTagPos);
354 void swf_SetDepth(TAG * t, U16 depth)
356 switch (swf_GetTagID(t))
357 { case ST_PLACEOBJECT:
358 case ST_REMOVEOBJECT:
359 PUT16(t->data, depth);
361 case ST_REMOVEOBJECT2:
362 PUT16(t->data, depth);
364 case ST_PLACEOBJECT2:
365 PUT16(&t->data[1], depth);
368 PUT16(t->data, depth);
371 fprintf(stderr, "rfxswf: Error: tag %d has no depth\n", t->id);
375 char* swf_GetName(TAG * t)
381 oldTagPos = swf_GetTagPos(t);
383 switch(swf_GetTagID(t))
386 name = &t->data[swf_GetTagPos(t)];
388 case ST_PLACEOBJECT3:
389 case ST_PLACEOBJECT2: {
390 U8 flags = swf_GetU8(t);
391 if(t->id == ST_PLACEOBJECT3)
393 swf_GetU16(t); //depth;
397 swf_GetMatrix(t, &m);
399 swf_GetCXForm(t, &c, 1);
402 if(flags&PF_CLIPDEPTH)
405 swf_ResetReadBits(t);
406 name = &t->data[swf_GetTagPos(t)];
411 swf_SetTagPos(t,oldTagPos);
415 /* used in enumerateUsedIDs */
416 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
419 int num = swf_GetU8(tag) & 15;
420 if(gradient1) gradient1->num = num;
421 if(gradient2) gradient2->num = num;
424 gradient1->num = num;
425 gradient1->rgba = rfx_calloc(sizeof(RGBA)*gradient1->num);
426 gradient1->ratios = rfx_calloc(sizeof(gradient1->ratios[0])*gradient1->num);
429 gradient2->num = num;
430 gradient2->rgba = rfx_calloc(sizeof(RGBA)*gradient2->num);
431 gradient2->ratios = rfx_calloc(sizeof(gradient2->ratios[0])*gradient2->num);
438 ratio = swf_GetU8(tag);
439 swf_GetRGBA(tag, &color);
441 gradient1->ratios[t] = ratio;
442 gradient1->rgba[t] = color;
445 ratio = swf_GetU8(tag);
446 swf_GetRGBA(tag, &color);
448 gradient2->ratios[t] = ratio;
449 gradient2->rgba[t] = color;
454 #define DEBUG_ENUMERATE if(0)
455 //#define DEBUG_ENUMERATE
457 void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
461 count = swf_GetU8(tag);
462 if(count == 0xff && num>1) // defineshape2,3,4 only
463 count = swf_GetU16(tag);
465 DEBUG_ENUMERATE printf("%d fill styles\n", count);
470 type = swf_GetU8(tag); //type
471 DEBUG_ENUMERATE printf("fill style %d) %02x (tagpos=%d)\n", t, type, tag->pos);
474 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
476 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
478 else if(type == 0x10 || type == 0x12 || type == 0x13)
480 swf_ResetReadBits(tag);
482 swf_GetMatrix(tag, &m);
483 DEBUG_ENUMERATE swf_DumpMatrix(stdout, &m);
485 swf_GetMatrix(tag, NULL);
486 swf_ResetReadBits(tag);
488 swf_GetMorphGradient(tag, NULL, NULL);
491 swf_GetGradient(tag, &g, /*alpha*/ num>=3?1:0);
492 DEBUG_ENUMERATE swf_DumpGradient(stdout, &g);
497 else if(type == 0x40 || type == 0x41 || type == 0x42 || type == 0x43)
499 swf_ResetReadBits(tag);
500 if(tag->data[tag->pos] != 0xff ||
501 tag->data[tag->pos+1] != 0xff)
502 (callback)(tag, tag->pos, callback_data);
505 swf_ResetReadBits(tag);
506 swf_GetMatrix(tag, NULL);
508 swf_GetMatrix(tag, NULL);
511 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x in tag %02x\n",type, tag->id);
514 swf_ResetReadBits(tag);
515 count = swf_GetU8(tag); // line style array
517 count = swf_GetU16(tag);
518 DEBUG_ENUMERATE printf("%d line styles\n", count);
523 width = swf_GetU16(tag);
527 U16 flags = swf_GetU16(tag);
529 swf_GetU16(tag); // miter limit
531 fprintf(stderr, "Filled strokes parsing not yet supported\n");
535 {swf_GetRGBA(tag, &color);if(morph) swf_GetRGBA(tag, NULL);}
537 {swf_GetRGB(tag, &color);if(morph) swf_GetRGB(tag, NULL);}
538 DEBUG_ENUMERATE printf("line style %d: %02x%02x%02x%02x \n", t, color.r,color.g,color.b,color.a);
542 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
545 swf_ResetReadBits(tag);
549 case ST_DEFINEBUTTONSOUND: {
551 callback(tag, tag->pos + base, callback_data);
554 callback(tag, tag->pos + base, callback_data);
555 swf_GetU16(tag); //sound id
556 flags = swf_GetU8(tag);
558 swf_GetU32(tag); // in point
560 swf_GetU32(tag); // out points
562 swf_GetU16(tag); // loop count
565 int npoints = swf_GetU8(tag);
567 for(s=0;s<npoints;s++)
576 case ST_DEFINEBUTTONCXFORM:
577 callback(tag, tag->pos + base, callback_data); //button id
580 case ST_EXPORTASSETS: {
581 int num = swf_GetU16(tag);
584 callback(tag, tag->pos + base, callback_data); //button id
585 swf_GetU16(tag); //id
586 while(swf_GetU8(tag)); //name
590 case ST_IMPORTASSETS:
591 case ST_IMPORTASSETS2: {
592 swf_GetString(tag); //count
593 swf_GetU8(tag); //reserved
594 swf_GetU8(tag); //reserved
595 int num = swf_GetU16(tag); //url
598 callback(tag, tag->pos + base, callback_data); //button id
599 swf_GetU16(tag); //id
600 while(swf_GetU8(tag)); //name
604 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
605 case ST_NAMECHARACTER:
606 case ST_GENERATORTEXT:
607 callback(tag, tag->pos + base, callback_data);
610 callback(tag, tag->pos + base, callback_data);
612 case ST_PLACEOBJECT2:
613 // only if placeflaghascharacter
614 if(!(tag->data[0]&2))
616 callback(tag, 3 + base, callback_data);
618 case ST_PLACEOBJECT3:
619 // only if placeflaghascharacter
620 if(!(tag->data[0]&2))
622 callback(tag, 4 + base, callback_data);
624 case ST_REMOVEOBJECT:
625 callback(tag, tag->pos + base, callback_data);
628 callback(tag, tag->pos + base, callback_data);
630 case ST_DEFINESPRITE: {
632 break; // sprite is expanded
634 swf_GetU16(tag); // id
635 swf_GetU16(tag); // framenum
638 U16 flags = swf_GetU16(tag);
641 TAG *tag2 = swf_InsertTag(NULL, id);
644 len = swf_GetU32(tag);
647 tag2->len = tag2->memsize = len;
648 tag2->data = rfx_alloc(len);
649 memcpy(tag2->data, &tag->data[tag->pos], len);
650 /* I never saw recursive sprites, but they are (theoretically)
651 possible, so better add base here again */
652 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
654 swf_GetBlock(tag, NULL, len);
658 case ST_DEFINEBUTTON2: // has some font ids in the button records
661 case ST_DEFINEBUTTON: {
662 swf_GetU16(tag); //button id
666 swf_GetU8(tag); //flag
667 offset = swf_GetU16(tag); //offset
672 if(!swf_GetU8(tag)) //flags
674 callback(tag, tag->pos + base, callback_data);
675 swf_GetU16(tag); //char
676 swf_GetU16(tag); //layer
677 swf_ResetReadBits(tag);
678 swf_GetMatrix(tag, NULL);
680 swf_ResetReadBits(tag);
681 swf_GetCXForm(tag, NULL, 1);
687 case ST_DEFINEEDITTEXT: {
689 swf_GetU16(tag); //id
690 swf_GetRect(tag, NULL); //bounding box
691 swf_ResetReadBits(tag);
692 flags1 = swf_GetU8(tag);
693 flags2 = swf_GetU8(tag);
695 callback(tag, tag->pos + base, callback_data);
700 case ST_DEFINETEXT: {
701 int glyphbits, advancebits;
703 id = swf_GetU16(tag); //id
704 swf_GetRect(tag, NULL); //bounding box
705 swf_ResetReadBits(tag);
706 swf_GetMatrix(tag, NULL); //matrix
707 swf_ResetReadBits(tag);
708 glyphbits = swf_GetU8(tag); //glyphbits
709 advancebits = swf_GetU8(tag); //advancebits
713 swf_ResetReadBits(tag);
714 flags = swf_GetBits(tag, 8);
717 swf_ResetReadBits(tag);
718 if(flags & 8) { // hasfont
719 callback(tag, tag->pos + base, callback_data);
720 id = swf_GetU16(tag);
722 if(flags & 4) { // hascolor
723 if(num==1) swf_GetRGB(tag, NULL);
724 else swf_GetRGBA(tag, NULL);
726 if(flags & 2) { //has x offset
727 swf_ResetReadBits(tag);
730 if(flags & 1) { //has y offset
731 swf_ResetReadBits(tag);
734 if(flags & 8) { //has height
735 swf_ResetReadBits(tag);
739 flags = swf_GetBits(tag, 8);
741 swf_ResetReadBits(tag);
742 for(t=0;t<flags;t++) {
743 swf_GetBits(tag, glyphbits);
744 swf_GetBits(tag, advancebits);
749 case ST_DEFINEFONTALIGNZONES:
750 case ST_DEFINESCALINGGRID:
752 case ST_CSMTEXTSETTINGS:
753 case ST_DEFINEFONTINFO:
754 case ST_DEFINEFONTINFO2:
756 callback(tag, tag->pos + base, callback_data);
758 case ST_DEFINEVIDEOSTREAM:
761 case ST_DOINITACTION:
762 callback(tag, tag->pos + base, callback_data);
765 case ST_DEFINEMORPHSHAPE2:
766 case ST_DEFINESHAPE4:
768 case ST_DEFINEMORPHSHAPE:
769 case ST_DEFINESHAPE3:
771 case ST_DEFINESHAPE2:
773 case ST_DEFINESHAPE: {
779 if(tag->id == ST_DEFINEMORPHSHAPE || tag->id==ST_DEFINEMORPHSHAPE2) {
784 id = swf_GetU16(tag); // id;
785 SRECT r={0,0,0,0},r2={0,0,0,0};
786 swf_GetRect(tag, &r); // shape bounds
788 swf_ResetReadBits(tag);
789 swf_GetRect(tag, NULL); // shape bounds2
791 swf_ResetReadBits(tag);
792 swf_GetRect(tag, NULL); // edge bounds1
796 swf_ResetReadBits(tag);
797 swf_GetRect(tag, &r2); // edge bounds
798 U8 flags = swf_GetU8(tag); // flags, &1: contains scaling stroke, &2: contains non-scaling stroke
799 DEBUG_ENUMERATE printf("flags: %02x (1=scaling strokes, 2=non-scaling strokes)\n", flags);
802 swf_GetU32(tag); //offset to endedges
805 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
806 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);
807 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);
809 DEBUG_ENUMERATE printf("style tag pos: %d\n", tag->pos);
810 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
811 DEBUG_ENUMERATE printf("-------\n");
812 swf_ResetReadBits(tag);
813 while(--numshapes>=0) /* morph shapes define two shapes */
815 DEBUG_ENUMERATE printf("shape:%d\n", numshapes);
816 fillbits = swf_GetBits(tag, 4);
817 linebits = swf_GetBits(tag, 4);
818 DEBUG_ENUMERATE printf("fillbits=%d linebits=%d\n", fillbits, linebits);
819 swf_ResetReadBits(tag);
822 flags = swf_GetBits(tag, 1);
823 if(!flags) { //style change
824 flags = swf_GetBits(tag, 5);
828 int n = swf_GetBits(tag, 5);
830 x = swf_GetBits(tag, n); //x
831 y = swf_GetBits(tag, n); //y
832 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
834 if(flags&2) { //fill0
836 fill0 = swf_GetBits(tag, fillbits);
837 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
839 if(flags&4) { //fill1
841 fill1 = swf_GetBits(tag, fillbits);
842 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
844 if(flags&8) { //linestyle
846 line = swf_GetBits(tag, linebits);
847 DEBUG_ENUMERATE printf("linestyle %d\n",line);
850 DEBUG_ENUMERATE printf("more fillstyles\n");
851 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
852 fillbits = swf_GetBits(tag, 4);
853 linebits = swf_GetBits(tag, 4);
856 flags = swf_GetBits(tag, 1);
857 if(flags) { //straight edge
858 int n = swf_GetBits(tag, 4) + 2;
859 if(swf_GetBits(tag, 1)) { //line flag
861 x = swf_GetSBits(tag, n); //delta x
862 y = swf_GetSBits(tag, n); //delta y
863 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
865 int v=swf_GetBits(tag, 1);
867 d = swf_GetSBits(tag, n); //vert/horz
868 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
870 } else { //curved edge
871 int n = swf_GetBits(tag, 4) + 2;
873 x1 = swf_GetSBits(tag, n);
874 y1 = swf_GetSBits(tag, n);
875 x2 = swf_GetSBits(tag, n);
876 y2 = swf_GetSBits(tag, n);
877 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
889 void callbackCount(TAG * t,int pos, void*ptr)
892 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
895 void callbackFillin(TAG * t,int pos, void*ptr)
899 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
902 int swf_GetNumUsedIDs(TAG * t)
905 enumerateUsedIDs(t, 0, callbackCount, &num);
909 void swf_GetUsedIDs(TAG * t, int * positions)
911 int * ptr = positions;
912 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
915 void swf_Relocate (SWF*swf, char*bitmap)
919 memset(slaveids, -1, sizeof(slaveids));
927 if(swf_isDefiningTag(tag))
932 id = swf_GetDefineID(tag); //own id
934 if(!bitmap[id]) { //free
939 for (t=1;t<65536;t++)
949 slaveids[id] = newid;
951 swf_SetDefineID(tag, newid);
954 num = swf_GetNumUsedIDs(tag);
956 ptr = rfx_alloc(sizeof(int)*num);
957 swf_GetUsedIDs(tag, ptr);
960 int id = GET16(&tag->data[ptr[t]]);
962 fprintf(stderr, "swf_Relocate: Mapping id (%d) never encountered before in %s\n", id,
963 swf_TagGetName(tag));
967 PUT16(&tag->data[ptr[t]], id);
975 void swf_Relocate2(SWF*swf, int*id2id)
980 if(swf_isDefiningTag(tag)) {
981 int id = swf_GetDefineID(tag);
984 swf_SetDefineID(tag, id);
987 int num = swf_GetNumUsedIDs(tag);
991 ptr = rfx_alloc(sizeof(int)*num);
992 swf_GetUsedIDs(tag, ptr);
994 int id = GET16(&tag->data[ptr[t]]);
997 PUT16(&tag->data[ptr[t]], id);
1004 void swf_RelocateDepth(SWF*swf, char*bitmap)
1008 tag = swf->firstTag;
1009 for(nr=65535;nr>=0;nr--) {
1013 // now nr is the highest used depth. So we start
1014 // assigning depths at nr+1
1020 /* TODO * clip depths
1023 if(tag->id == ST_PLACEOBJECT2) {
1025 swf_GetPlaceObject(tag, &obj);
1027 int newdepth = obj.clipdepth+nr;
1028 if(newdepth>65535) {
1029 fprintf(stderr, "Couldn't relocate depths: too large values\n");
1032 obj.clipdepth = newdepth;
1033 swf_ResetTag(tag, ST_PLACEOBJECT2);
1034 swf_SetPlaceObject(tag, &obj);
1036 swf_PlaceObjectFree(&obj);
1039 depth = swf_GetDepth(tag);
1041 int newdepth = depth+nr;
1042 if(newdepth>65535) {
1043 fprintf(stderr, "Couldn't relocate depths: too large values\n");
1046 swf_SetDepth(tag, newdepth);
1052 U8 swf_isShapeTag(TAG*tag)
1054 if(tag->id == ST_DEFINESHAPE ||
1055 tag->id == ST_DEFINESHAPE2 ||
1056 tag->id == ST_DEFINESHAPE3 ||
1057 tag->id == ST_DEFINESHAPE4)
1062 U8 swf_isPlaceTag(TAG*tag)
1064 if(tag->id == ST_PLACEOBJECT ||
1065 tag->id == ST_PLACEOBJECT2 ||
1066 tag->id == ST_PLACEOBJECT3)
1070 U8 swf_isTextTag(TAG*tag)
1072 if(tag->id == ST_DEFINETEXT ||
1073 tag->id == ST_DEFINETEXT2)
1078 U8 swf_isFontTag(TAG*tag)
1080 if(tag->id == ST_DEFINEFONT ||
1081 tag->id == ST_DEFINEFONT2 ||
1082 tag->id == ST_DEFINEFONTINFO)
1087 U8 swf_isImageTag(TAG*tag)
1089 if(tag->id == ST_DEFINEBITSJPEG ||
1090 tag->id == ST_DEFINEBITSJPEG2 ||
1091 tag->id == ST_DEFINEBITSJPEG3 ||
1092 tag->id == ST_DEFINEBITSLOSSLESS ||
1093 tag->id == ST_DEFINEBITSLOSSLESS2)
1098 TAG* swf_Concatenate (TAG*list1,TAG*list2)
1100 TAG*tag=0,*lasttag=0;
1102 char depthmap[65536];
1104 memset(bitmap, 0, sizeof(bitmap));
1105 memset(depthmap, 0, sizeof(depthmap));
1106 memset(&swf1, 0, sizeof(swf1));
1107 memset(&swf2, 0, sizeof(swf2));
1109 swf1.firstTag = list1;
1111 swf2.firstTag = list2;
1116 if(!swf_isDefiningTag(tag)) {
1117 int id = swf_GetDefineID(tag);
1120 if(tag->id == ST_PLACEOBJECT ||
1121 tag->id == ST_PLACEOBJECT2) {
1122 int depth = swf_GetDepth(tag);
1123 depthmap[depth] = 1;
1125 if(tag->id == ST_REMOVEOBJECT ||
1126 tag->id == ST_REMOVEOBJECT2) {
1127 int depth = swf_GetDepth(tag);
1128 depthmap[depth] = 0;
1133 swf_Relocate(&swf2, bitmap);
1134 swf_RelocateDepth(&swf2, depthmap);
1135 lasttag->next = swf2.firstTag;
1136 swf2.firstTag->prev = lasttag;
1138 return swf1.firstTag;
1141 static int tagHash(TAG*tag)
1144 unsigned int a = 0x6b973e5a;
1145 /* start at pos 2, as 0 and 1 are the id */
1146 for(t=2;t<tag->len;t++) {
1149 a += tag->data[t]*0xefbc35a5*b*(t+1);
1151 return a&0x7fffffff; //always return positive number
1154 void swf_Optimize(SWF*swf)
1156 const int hash_size = 131072;
1157 char* dontremap = rfx_calloc(sizeof(char)*65536);
1158 U16* remap = rfx_alloc(sizeof(U16)*65536);
1159 TAG* id2tag = rfx_calloc(sizeof(TAG*)*65536);
1160 TAG** hashmap = rfx_calloc(sizeof(TAG*)*hash_size);
1163 for(t=0;t<65536;t++) {
1169 tag = swf->firstTag;
1171 /* make sure we don't remap to this tag,
1172 as it might have different "helper tags"
1173 FIXME: a better way would be to compare
1174 the helper tags, too.
1176 if(swf_isPseudoDefiningTag(tag) &&
1177 tag->id != ST_NAMECHARACTER) {
1178 dontremap[swf_GetDefineID(tag)] = 1;
1182 tag = swf->firstTag;
1184 TAG*next = tag->next;
1187 int num = swf_GetNumUsedIDs(tag);
1188 int*positions = rfx_alloc(sizeof(int)*num);
1190 swf_GetUsedIDs(tag, positions);
1191 for(t=0;t<num;t++) {
1192 int id = GET16(&tag->data[positions[t]]);
1194 PUT16(&tag->data[positions[t]], id);
1196 rfx_free(positions);
1198 /* now look for previous tags with the same
1200 if(swf_isDefiningTag(tag)) {
1202 int id = swf_GetDefineID(tag);
1203 int hash = tagHash(tag);
1206 while((tag2 = hashmap[hash%hash_size])) {
1207 if(tag2 != (TAG*)0 && tag->len == tag2->len) {
1208 if(memcmp(&tag->data[2],&tag2->data[2],tag->len-2) == 0) {
1216 while(hashmap[hash%hash_size]) hash++;
1217 hashmap[hash%hash_size] = tag;
1219 /* we found two identical tags- remap one
1221 remap[id] = swf_GetDefineID(tag2);
1223 if(tag == swf->firstTag)
1224 swf->firstTag = next;
1226 } else if(swf_isPseudoDefiningTag(tag)) {
1227 int id = swf_GetDefineID(tag);
1229 /* if this tag was remapped, we don't
1230 need the helper tag anymore. Discard
1233 if(tag == swf->firstTag)
1234 swf->firstTag = next;
1241 rfx_free(dontremap);
1247 void swf_SetDefineBBox(TAG * tag, SRECT newbbox)
1251 swf_SetTagPos(tag,0);
1253 switch (swf_GetTagID(tag))
1255 case ST_DEFINESHAPE:
1256 case ST_DEFINESHAPE2:
1257 case ST_DEFINESHAPE3:
1258 case ST_DEFINEEDITTEXT:
1260 case ST_DEFINETEXT2:
1261 case ST_DEFINEVIDEOSTREAM: {
1262 U32 after_bbox_offset = 0, len;
1264 id = swf_GetU16(tag);
1265 swf_GetRect(tag, &b1);
1266 swf_ResetReadBits(tag);
1267 after_bbox_offset = tag->pos;
1268 len = tag->len - after_bbox_offset;
1270 memcpy(data, &tag->data[after_bbox_offset], len);
1273 swf_SetRect(tag, &newbbox);
1274 swf_SetBlock(tag, data, len);
1276 tag->pos = tag->readBit = 0;
1280 fprintf(stderr, "rfxswf: Tag %d (%s) has no bbox\n", tag->id, swf_TagGetName(tag));
1284 RGBA swf_GetSWFBackgroundColor(SWF*swf)
1286 TAG*t=swf->firstTag;
1288 color.r = color.b = color.g = 0;
1291 if(t->id == ST_SETBACKGROUNDCOLOR) {
1292 swf_SetTagPos(t, 0);
1293 color.r = swf_GetU8(t);
1294 color.g = swf_GetU8(t);
1295 color.b = swf_GetU8(t);