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 file is distributed under the GPL, see file COPYING for details
14 // Matrix & Math tools for SWF files
17 SFIXED RFXSWF_SP(SFIXED a1,SFIXED a2,SFIXED b1,SFIXED b2)
19 a = (S64)a1*(S64)b1+(S64)a2*(S64)b2;
20 return (SFIXED)(a>>16);
22 SFIXED RFXSWF_QFIX(int zaehler,int nenner) // bildet Quotient von zwei INTs in SFIXED
23 { S64 z = zaehler<<16;
24 S64 a = z/(S64)nenner;
29 MATRIX * swf_MatrixJoin(MATRIX * d,MATRIX * s1,MATRIX * s2)
32 if (!s1) return (s2)?(MATRIX *)memcpy(d,s2,sizeof(MATRIX)):NULL;
33 if (!s2) return (MATRIX *)memcpy(d,s1,sizeof(MATRIX));
35 d->tx = s1->tx + s2->tx;
36 d->ty = s1->ty + s2->ty;
38 d->sx = RFXSWF_SP(s1->sx,s1->r1,s2->sx,s2->r0);
39 d->sy = RFXSWF_SP(s1->r0,s1->sy,s2->r1,s2->sy);
40 d->r0 = RFXSWF_SP(s1->r0,s1->sy,s2->sx,s2->r0);
41 d->r1 = RFXSWF_SP(s1->sx,s1->r1,s2->r1,s2->sy);
48 MATRIX * swf_MatrixMapTriangle(MATRIX * m,int dx,int dy,int x0,int y0,
49 int x1,int y1,int x2,int y2)
56 if ((!dx)||(!dy)) return NULL; // check DIV by zero
60 m->sx = RFXSWF_QFIX(dx1,dx);
61 m->sy = RFXSWF_QFIX(dy2,dy);
62 m->r0 = RFXSWF_QFIX(dy1,dx);
63 m->r1 = RFXSWF_QFIX(dx2,dy);
68 void swf_SetDefineID(TAG * tag, U16 newid)
70 int oldlen = tag->len;
72 swf_SetU16(tag, newid); /* set defining ID */
76 U16 swf_GetDefineID(TAG * t)
81 oldTagPos = swf_GetTagPos(t);
84 switch (swf_GetTagID(t))
85 { case ST_DEFINESHAPE:
88 case ST_DEFINEMORPHSHAPE:
89 case ST_DEFINEEDITTEXT:
91 case ST_DEFINEBITSJPEG2:
92 case ST_DEFINEBITSJPEG3:
93 case ST_DEFINEBITSLOSSLESS:
94 case ST_DEFINEBITSLOSSLESS2:
96 case ST_DEFINEBUTTON2:
97 case ST_DEFINEBUTTONCXFORM: //pseudodefine
98 case ST_DEFINEBUTTONSOUND: //pseudodefine
101 case ST_DEFINEFONTINFO: //pseudodefine
102 case ST_DEFINEFONTINFO2: //pseudodefine
106 case ST_DEFINESPRITE:
107 case ST_DEFINEVIDEOSTREAM:
108 case ST_VIDEOFRAME: //pseudodefine
109 case ST_NAMECHARACTER: //pseudodefine
114 swf_SetTagPos(t,oldTagPos);
119 SRECT swf_GetDefineBBox(TAG * t)
125 oldTagPos = swf_GetTagPos(t);
130 switch (swf_GetTagID(t))
131 { case ST_DEFINESHAPE:
132 case ST_DEFINESHAPE2:
133 case ST_DEFINESHAPE3:
134 case ST_DEFINEEDITTEXT:
135 case ST_DEFINEBUTTON:
136 case ST_DEFINEBUTTON2:
139 case ST_DEFINEVIDEOSTREAM:
143 case ST_DEFINEMORPHSHAPE:
147 swf_ExpandRect2(&b1, &b2);
149 case ST_DEFINEBITSLOSSLESS:
150 case ST_DEFINEBITSLOSSLESS2:
152 case ST_DEFINEBITSJPEG2:
153 case ST_DEFINEBITSJPEG3:
158 swf_SetTagPos(t,oldTagPos);
163 U16 swf_GetPlaceID(TAG * t)
168 oldTagPos = swf_GetTagPos(t);
171 switch (swf_GetTagID(t))
172 { case ST_PLACEOBJECT:
173 case ST_REMOVEOBJECT:
174 case ST_FREECHARACTER:
179 case ST_PLACEOBJECT2:
180 { U8 flags = swf_GetU8(t);
181 U16 d = swf_GetU16(t);
182 id = (flags&PF_CHAR)?swf_GetU16(t):id;
187 swf_SetTagPos(t,oldTagPos);
192 static int swf_definingtagids[] =
205 ST_DEFINEBITSLOSSLESS,
206 ST_DEFINEBITSLOSSLESS2,
212 ST_DEFINEVIDEOSTREAM,
216 // tags which may be used inside a sprite definition
217 static int swf_spritetagids[] =
222 ST_REMOVEOBJECT2, //?
233 static int swf_pseudodefiningtagids[] =
237 ST_DEFINEBUTTONCXFORM,
238 ST_DEFINEBUTTONSOUND,
245 U8 swf_isAllowedSpriteTag(TAG * tag)
249 while(swf_spritetagids[t]>=0)
251 if(swf_spritetagids[t] == id)
258 U8 swf_isDefiningTag(TAG * tag)
262 while(swf_definingtagids[t]>=0)
264 if(swf_definingtagids[t] == id)
271 U8 swf_isPseudoDefiningTag(TAG * tag)
275 while(swf_pseudodefiningtagids[t]>=0)
277 if(swf_pseudodefiningtagids[t] == id)
284 U16 swf_GetDepth(TAG * t)
289 oldTagPos = swf_GetTagPos(t);
292 switch (swf_GetTagID(t))
293 { case ST_PLACEOBJECT:
294 case ST_REMOVEOBJECT:
296 depth = swf_GetU16(t);
298 case ST_REMOVEOBJECT2:
299 depth = swf_GetU16(t);
301 case ST_PLACEOBJECT2:
302 { U8 flags = swf_GetU8(t);
303 depth = swf_GetU16(t);
306 swf_SetTagPos(t,oldTagPos);
310 char* swf_GetName(TAG * t)
316 oldTagPos = swf_GetTagPos(t);
318 switch(swf_GetTagID(t))
321 name = &t->data[swf_GetTagPos(t)];
323 case ST_PLACEOBJECT2: {
324 U8 flags = swf_GetU8(t);
325 swf_GetU16(t); //depth;
329 swf_GetMatrix(t, &m);
331 swf_GetCXForm(t, &c, 1);
334 if(flags&PF_CLIPACTION)
337 swf_ResetReadBits(t);
338 name = &t->data[swf_GetTagPos(t)];
343 swf_SetTagPos(t,oldTagPos);
347 /* used in enumerateUsedIDs */
348 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
358 gradient2->num = swf_GetU8(tag);
359 for(t=0;t<gradient1->num;t++)
364 gradient1->ratios[t] = swf_GetU8(tag);
365 swf_GetRGBA(tag, &gradient1->rgba[t]);
366 gradient2->ratios[t] = swf_GetU8(tag);
367 swf_GetRGBA(tag, &gradient2->rgba[t]);
371 #define DEBUG_ENUMERATE if(0)
373 static void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
377 count = swf_GetU8(tag);
378 if(count == 0xff && num>1) // defineshape2,3 only
379 count = swf_GetU16(tag);
385 swf_ResetReadBits(tag);
386 type = swf_GetU8(tag); //type
389 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
391 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
393 else if(type == 0x10 || type == 0x12)
395 swf_ResetReadBits(tag);
396 swf_GetMatrix(tag, NULL);
398 swf_GetMatrix(tag, NULL);
399 swf_ResetReadBits(tag);
401 swf_GetMorphGradient(tag, NULL, NULL);
403 swf_GetGradient(tag, NULL, /*alpha*/ num>=3?1:0);
405 else if(type == 0x40 || type == 0x41)
407 swf_ResetReadBits(tag);
409 if(tag->data[tag->pos] != 0xff ||
410 tag->data[tag->pos+1] != 0xff)
411 (callback)(tag, tag->pos, callback_data);
414 swf_ResetReadBits(tag);
415 swf_GetMatrix(tag, NULL);
417 swf_GetMatrix(tag, NULL);
420 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
423 swf_ResetReadBits(tag);
424 count = swf_GetU8(tag); // line style array
426 count = swf_GetU16(tag);
433 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
435 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
439 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
442 swf_ResetReadBits(tag);
446 case ST_DEFINEBUTTONCXFORM: {
448 callback(tag, tag->pos + base, callback_data);
451 callback(tag, tag->pos + base, callback_data);
452 swf_GetU16(tag); //sound id
453 flags = swf_GetU8(tag);
455 swf_GetU32(tag); // in point
457 swf_GetU32(tag); // out points
459 swf_GetU16(tag); // loop count
462 int npoints = swf_GetU8(tag);
464 for(s=0;s<npoints;s++)
473 case ST_DEFINEBUTTONSOUND:
474 callback(tag, tag->pos + base, callback_data); //button id
477 case ST_EXPORTASSETS: {
478 int num = swf_GetU16(tag);
481 callback(tag, tag->pos + base, callback_data); //button id
482 swf_GetU16(tag); //id
483 while(swf_GetU8(tag)); //name
487 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
488 case ST_NAMECHARACTER:
489 case ST_GENERATORTEXT:
490 callback(tag, tag->pos + base, callback_data);
493 callback(tag, tag->pos + base, callback_data);
495 case ST_PLACEOBJECT2:
496 // only if placeflaghascharacter
497 if(!(tag->data[0]&2))
499 callback(tag, 3 + base, callback_data);
501 case ST_REMOVEOBJECT:
502 callback(tag, tag->pos + base, callback_data);
505 callback(tag, tag->pos + base, callback_data);
507 case ST_DEFINESPRITE: {
509 break; // sprite is expanded
511 swf_GetU16(tag); // id
512 swf_GetU16(tag); // framenum
515 U16 flags = swf_GetU16(tag);
518 TAG *tag2 = swf_InsertTag(NULL, id);
521 len = swf_GetU32(tag);
524 tag2->len = tag2->memsize = len;
525 tag2->data = malloc(len);
526 memcpy(tag2->data, &tag->data[tag->pos], len);
527 /* I never saw recursive sprites, but they are (theoretically)
528 possible, so better add base here again */
529 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
531 swf_GetBlock(tag, NULL, len);
535 case ST_DEFINEBUTTON2: // has some font ids in the button records
538 case ST_DEFINEBUTTON: {
539 swf_GetU16(tag); //button id
543 swf_GetU8(tag); //flag
544 offset = swf_GetU16(tag); //offset
549 if(!swf_GetU8(tag)) //flags
551 callback(tag, tag->pos + base, callback_data);
552 swf_GetU16(tag); //char
553 swf_GetU16(tag); //layer
554 swf_ResetReadBits(tag);
555 swf_GetMatrix(tag, NULL);
557 swf_ResetReadBits(tag);
558 swf_GetCXForm(tag, NULL, 1);
564 case ST_DEFINEEDITTEXT: {
566 swf_GetU16(tag); //id
567 swf_GetRect(tag, NULL); //bounding box
568 swf_ResetReadBits(tag);
569 flags1 = swf_GetU8(tag);
570 flags2 = swf_GetU8(tag);
572 callback(tag, tag->pos + base, callback_data);
577 case ST_DEFINETEXT: {
578 int glyphbits, advancebits;
580 id = swf_GetU16(tag); //id
581 swf_GetRect(tag, NULL); //bounding box
582 swf_ResetReadBits(tag);
583 swf_GetMatrix(tag, NULL); //matrix
584 swf_ResetReadBits(tag);
585 glyphbits = swf_GetU8(tag); //glyphbits
586 advancebits = swf_GetU8(tag); //advancebits
589 swf_ResetReadBits(tag);
590 flags = swf_GetBits(tag, 8);
592 if(flags & 128) // text style record
594 swf_ResetReadBits(tag);
595 if(flags & 8) { // hasfont
596 callback(tag, tag->pos + base, callback_data);
597 id = swf_GetU16(tag);
599 if(flags & 4) { // hascolor
600 if(num==1) swf_GetRGB(tag, NULL);
601 else swf_GetRGBA(tag, NULL);
603 if(flags & 2) { //has x offset
604 swf_ResetReadBits(tag);
607 if(flags & 1) { //has y offset
608 swf_ResetReadBits(tag);
611 if(flags & 8) { //has height
612 swf_ResetReadBits(tag);
615 } else { // glyph record
617 swf_ResetReadBits(tag);
618 for(t=0;t<flags;t++) {
619 swf_GetBits(tag, glyphbits);
620 swf_GetBits(tag, advancebits);
626 case ST_DEFINEFONTINFO:
627 case ST_DEFINEFONTINFO2:
629 callback(tag, tag->pos + base, callback_data);
631 case ST_DEFINEVIDEOSTREAM:
634 case ST_DOINITACTION:
635 callback(tag, tag->pos + base, callback_data);
638 case ST_DEFINEMORPHSHAPE:
639 case ST_DEFINESHAPE3:
641 case ST_DEFINESHAPE2:
643 case ST_DEFINESHAPE: {
649 if(tag->id == ST_DEFINEMORPHSHAPE) {
654 id = swf_GetU16(tag); // id;
655 swf_GetRect(tag, NULL); // bounds
657 swf_ResetReadBits(tag);
658 swf_GetRect(tag, NULL); // bounds2
659 swf_GetU32(tag); //offset to endedges
662 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
664 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
665 DEBUG_ENUMERATE printf("-------\n");
666 while(--numshapes>=0) /* morph shapes define two shapes */
668 DEBUG_ENUMERATE printf("shape:%d\n", numshapes);
669 fillbits = swf_GetBits(tag, 4);
670 linebits = swf_GetBits(tag, 4);
671 DEBUG_ENUMERATE printf("%d %d\n", fillbits, linebits);
672 swf_ResetReadBits(tag);
675 flags = swf_GetBits(tag, 1);
676 if(!flags) { //style change
677 flags = swf_GetBits(tag, 5);
681 int n = swf_GetBits(tag, 5);
683 x = swf_GetBits(tag, n); //x
684 y = swf_GetBits(tag, n); //y
685 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
687 if(flags&2) { //fill0
689 fill0 = swf_GetBits(tag, fillbits);
690 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
692 if(flags&4) { //fill1
694 fill1 = swf_GetBits(tag, fillbits);
695 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
697 if(flags&8) { //linestyle
699 line = swf_GetBits(tag, linebits);
700 DEBUG_ENUMERATE printf("linestyle %d\n",line);
703 DEBUG_ENUMERATE printf("more fillstyles\n");
704 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
705 fillbits = swf_GetBits(tag, 4);
706 linebits = swf_GetBits(tag, 4);
709 flags = swf_GetBits(tag, 1);
710 if(flags) { //straight edge
711 int n = swf_GetBits(tag, 4) + 2;
712 if(swf_GetBits(tag, 1)) { //line flag
714 x = swf_GetSBits(tag, n); //delta x
715 y = swf_GetSBits(tag, n); //delta y
716 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
718 int v=swf_GetBits(tag, 1);
720 d = swf_GetSBits(tag, n); //vert/horz
721 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
723 } else { //curved edge
724 int n = swf_GetBits(tag, 4) + 2;
726 x1 = swf_GetSBits(tag, n);
727 y1 = swf_GetSBits(tag, n);
728 x2 = swf_GetSBits(tag, n);
729 y2 = swf_GetSBits(tag, n);
730 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
742 void callbackCount(TAG * t,int pos, void*ptr)
745 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
748 void callbackFillin(TAG * t,int pos, void*ptr)
752 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
755 int swf_GetNumUsedIDs(TAG * t)
758 enumerateUsedIDs(t, 0, callbackCount, &num);
762 void swf_GetUsedIDs(TAG * t, int * positions)
764 int * ptr = positions;
765 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
768 void swf_Relocate (SWF*swf, char*bitmap)
772 memset(slaveids, -1, sizeof(slaveids));
780 if(swf_isDefiningTag(tag))
785 id = swf_GetDefineID(tag); //own id
787 if(!bitmap[id]) { //free
792 for (t=1;t<65536;t++)
802 slaveids[id] = newid;
804 swf_SetDefineID(tag, newid);
807 num = swf_GetNumUsedIDs(tag);
808 ptr = malloc(sizeof(int)*num);
809 swf_GetUsedIDs(tag, ptr);
812 int id = GET16(&tag->data[ptr[t]]);
814 fprintf(stderr, "swf_Relocate: Mapping id never encountered before: %d\n", id);
818 PUT16(&tag->data[ptr[t]], id);