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_NAMECHARACTER: //pseudodefine
113 swf_SetTagPos(t,oldTagPos);
118 SRECT swf_GetDefineBBox(TAG * t)
124 oldTagPos = swf_GetTagPos(t);
129 switch (swf_GetTagID(t))
130 { case ST_DEFINESHAPE:
131 case ST_DEFINESHAPE2:
132 case ST_DEFINESHAPE3:
133 case ST_DEFINEEDITTEXT:
134 case ST_DEFINEBUTTON:
135 case ST_DEFINEBUTTON2:
138 case ST_DEFINEVIDEOSTREAM:
142 case ST_DEFINEMORPHSHAPE:
146 swf_ExpandRect2(&b1, &b2);
148 case ST_DEFINEBITSLOSSLESS:
149 case ST_DEFINEBITSLOSSLESS2:
151 case ST_DEFINEBITSJPEG2:
152 case ST_DEFINEBITSJPEG3:
157 swf_SetTagPos(t,oldTagPos);
162 U16 swf_GetPlaceID(TAG * t)
167 oldTagPos = swf_GetTagPos(t);
170 switch (swf_GetTagID(t))
171 { case ST_PLACEOBJECT:
172 case ST_REMOVEOBJECT:
173 case ST_FREECHARACTER:
178 case ST_PLACEOBJECT2:
179 { U8 flags = swf_GetU8(t);
180 U16 d = swf_GetU16(t);
181 id = (flags&PF_CHAR)?swf_GetU16(t):id;
186 swf_SetTagPos(t,oldTagPos);
191 static int swf_definingtagids[] =
204 ST_DEFINEBITSLOSSLESS,
205 ST_DEFINEBITSLOSSLESS2,
211 ST_DEFINEVIDEOSTREAM,
215 // tags which may be used inside a sprite definition
216 static int swf_spritetagids[] =
221 ST_REMOVEOBJECT2, //?
232 static int swf_pseudodefiningtagids[] =
236 ST_DEFINEBUTTONCXFORM,
237 ST_DEFINEBUTTONSOUND,
243 U8 swf_isAllowedSpriteTag(TAG * tag)
247 while(swf_spritetagids[t]>=0)
249 if(swf_spritetagids[t] == id)
256 U8 swf_isDefiningTag(TAG * tag)
260 while(swf_definingtagids[t]>=0)
262 if(swf_definingtagids[t] == id)
269 U8 swf_isPseudoDefiningTag(TAG * tag)
273 while(swf_pseudodefiningtagids[t]>=0)
275 if(swf_pseudodefiningtagids[t] == id)
282 U16 swf_GetDepth(TAG * t)
287 oldTagPos = swf_GetTagPos(t);
290 switch (swf_GetTagID(t))
291 { case ST_PLACEOBJECT:
292 case ST_REMOVEOBJECT:
294 depth = swf_GetU16(t);
296 case ST_REMOVEOBJECT2:
297 depth = swf_GetU16(t);
299 case ST_PLACEOBJECT2:
300 { U8 flags = swf_GetU8(t);
301 depth = swf_GetU16(t);
304 swf_SetTagPos(t,oldTagPos);
308 char* swf_GetName(TAG * t)
314 oldTagPos = swf_GetTagPos(t);
316 switch(swf_GetTagID(t))
319 name = &t->data[swf_GetTagPos(t)];
321 case ST_PLACEOBJECT2: {
322 U8 flags = swf_GetU8(t);
323 swf_GetU16(t); //depth;
327 swf_GetMatrix(t, &m);
329 swf_GetCXForm(t, &c, 1);
332 if(flags&PF_CLIPACTION)
335 swf_ResetReadBits(t);
336 name = &t->data[swf_GetTagPos(t)];
341 swf_SetTagPos(t,oldTagPos);
345 /* used in enumerateUsedIDs */
346 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
356 gradient2->num = swf_GetU8(tag);
357 for(t=0;t<gradient1->num;t++)
362 gradient1->ratios[t] = swf_GetU8(tag);
363 swf_GetRGBA(tag, &gradient1->rgba[t]);
364 gradient2->ratios[t] = swf_GetU8(tag);
365 swf_GetRGBA(tag, &gradient2->rgba[t]);
369 #define DEBUG_ENUMERATE if(0)
371 static void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
375 count = swf_GetU8(tag);
376 if(count == 0xff && num>1) // defineshape2,3 only
377 count = swf_GetU16(tag);
383 swf_ResetReadBits(tag);
384 type = swf_GetU8(tag); //type
387 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
389 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
391 else if(type == 0x10 || type == 0x12)
393 swf_ResetReadBits(tag);
394 swf_GetMatrix(tag, NULL);
396 swf_GetMatrix(tag, NULL);
397 swf_ResetReadBits(tag);
399 swf_GetMorphGradient(tag, NULL, NULL);
401 swf_GetGradient(tag, NULL, /*alpha*/ num>=3?1:0);
403 else if(type == 0x40 || type == 0x41)
405 swf_ResetReadBits(tag);
407 if(tag->data[tag->pos] != 0xff ||
408 tag->data[tag->pos+1] != 0xff)
409 (callback)(tag, tag->pos, callback_data);
412 swf_ResetReadBits(tag);
413 swf_GetMatrix(tag, NULL);
415 swf_GetMatrix(tag, NULL);
418 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
421 swf_ResetReadBits(tag);
422 count = swf_GetU8(tag); // line style array
424 count = swf_GetU16(tag);
431 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
433 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
437 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
440 swf_ResetReadBits(tag);
444 case ST_DEFINEBUTTONCXFORM: {
446 callback(tag, tag->pos + base, callback_data);
449 callback(tag, tag->pos + base, callback_data);
450 swf_GetU16(tag); //sound id
451 flags = swf_GetU8(tag);
453 swf_GetU32(tag); // in point
455 swf_GetU32(tag); // out points
457 swf_GetU16(tag); // loop count
460 int npoints = swf_GetU8(tag);
462 for(s=0;s<npoints;s++)
471 case ST_DEFINEBUTTONSOUND:
472 callback(tag, tag->pos + base, callback_data); //button id
475 case ST_EXPORTASSETS: {
476 int num = swf_GetU16(tag);
479 callback(tag, tag->pos + base, callback_data); //button id
480 swf_GetU16(tag); //id
481 while(swf_GetU8(tag)); //name
485 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
486 case ST_NAMECHARACTER:
487 case ST_GENERATORTEXT:
488 callback(tag, tag->pos + base, callback_data);
491 callback(tag, tag->pos + base, callback_data);
493 case ST_PLACEOBJECT2:
494 // only if placeflaghascharacter
495 if(!(tag->data[0]&2))
497 callback(tag, 3 + base, callback_data);
499 case ST_REMOVEOBJECT:
500 callback(tag, tag->pos + base, callback_data);
503 callback(tag, tag->pos + base, callback_data);
505 case ST_DEFINESPRITE: {
507 break; // sprite is expanded
509 swf_GetU16(tag); // id
510 swf_GetU16(tag); // framenum
513 U16 flags = swf_GetU16(tag);
516 TAG *tag2 = swf_InsertTag(NULL, id);
519 len = swf_GetU32(tag);
522 tag2->len = tag2->memsize = len;
523 tag2->data = malloc(len);
524 memcpy(tag2->data, &tag->data[tag->pos], len);
525 /* I never saw recursive sprites, but they are (theoretically)
526 possible, so better add base here again */
527 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
529 swf_GetBlock(tag, NULL, len);
533 case ST_DEFINEBUTTON2: // has some font ids in the button records
536 case ST_DEFINEBUTTON: {
537 swf_GetU16(tag); //button id
541 swf_GetU8(tag); //flag
542 offset = swf_GetU16(tag); //offset
547 if(!swf_GetU8(tag)) //flags
549 callback(tag, tag->pos + base, callback_data);
550 swf_GetU16(tag); //char
551 swf_GetU16(tag); //layer
552 swf_ResetReadBits(tag);
553 swf_GetMatrix(tag, NULL);
555 swf_ResetReadBits(tag);
556 swf_GetCXForm(tag, NULL, 1);
562 case ST_DEFINEEDITTEXT: {
564 swf_GetU16(tag); //id
565 swf_GetRect(tag, NULL); //bounding box
566 swf_ResetReadBits(tag);
567 flags1 = swf_GetU8(tag);
568 flags2 = swf_GetU8(tag);
570 callback(tag, tag->pos + base, callback_data);
575 case ST_DEFINETEXT: {
576 int glyphbits, advancebits;
578 id = swf_GetU16(tag); //id
579 swf_GetRect(tag, NULL); //bounding box
580 swf_ResetReadBits(tag);
581 swf_GetMatrix(tag, NULL); //matrix
582 swf_ResetReadBits(tag);
583 glyphbits = swf_GetU8(tag); //glyphbits
584 advancebits = swf_GetU8(tag); //advancebits
587 swf_ResetReadBits(tag);
588 flags = swf_GetBits(tag, 8);
590 if(flags & 128) // text style record
592 swf_ResetReadBits(tag);
593 if(flags & 8) { // hasfont
594 callback(tag, tag->pos + base, callback_data);
595 id = swf_GetU16(tag);
597 if(flags & 4) { // hascolor
598 if(num==1) swf_GetRGB(tag, NULL);
599 else swf_GetRGBA(tag, NULL);
601 if(flags & 2) { //has x offset
602 swf_ResetReadBits(tag);
605 if(flags & 1) { //has y offset
606 swf_ResetReadBits(tag);
609 if(flags & 8) { //has height
610 swf_ResetReadBits(tag);
613 } else { // glyph record
615 swf_ResetReadBits(tag);
616 for(t=0;t<flags;t++) {
617 swf_GetBits(tag, glyphbits);
618 swf_GetBits(tag, advancebits);
624 case ST_DEFINEFONTINFO:
625 case ST_DEFINEFONTINFO2:
627 callback(tag, tag->pos + base, callback_data);
629 case ST_DEFINEVIDEOSTREAM:
632 case ST_DOINITACTION:
633 callback(tag, tag->pos + base, callback_data);
636 case ST_DEFINEMORPHSHAPE:
637 case ST_DEFINESHAPE3:
639 case ST_DEFINESHAPE2:
641 case ST_DEFINESHAPE: {
647 if(tag->id == ST_DEFINEMORPHSHAPE) {
652 id = swf_GetU16(tag); // id;
653 swf_GetRect(tag, NULL); // bounds
655 swf_ResetReadBits(tag);
656 swf_GetRect(tag, NULL); // bounds2
657 swf_GetU32(tag); //offset to endedges
660 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
662 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
663 DEBUG_ENUMERATE printf("-------\n");
664 while(--numshapes>=0) /* morph shapes define two shapes */
666 DEBUG_ENUMERATE printf("shape:%d\n", numshapes);
667 fillbits = swf_GetBits(tag, 4);
668 linebits = swf_GetBits(tag, 4);
669 DEBUG_ENUMERATE printf("%d %d\n", fillbits, linebits);
670 swf_ResetReadBits(tag);
673 flags = swf_GetBits(tag, 1);
674 if(!flags) { //style change
675 flags = swf_GetBits(tag, 5);
679 int n = swf_GetBits(tag, 5);
681 x = swf_GetBits(tag, n); //x
682 y = swf_GetBits(tag, n); //y
683 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
685 if(flags&2) { //fill0
687 fill0 = swf_GetBits(tag, fillbits);
688 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
690 if(flags&4) { //fill1
692 fill1 = swf_GetBits(tag, fillbits);
693 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
695 if(flags&8) { //linestyle
697 line = swf_GetBits(tag, linebits);
698 DEBUG_ENUMERATE printf("linestyle %d\n",line);
701 DEBUG_ENUMERATE printf("more fillstyles\n");
702 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
703 fillbits = swf_GetBits(tag, 4);
704 linebits = swf_GetBits(tag, 4);
707 flags = swf_GetBits(tag, 1);
708 if(flags) { //straight edge
709 int n = swf_GetBits(tag, 4) + 2;
710 if(swf_GetBits(tag, 1)) { //line flag
712 x = swf_GetSBits(tag, n); //delta x
713 y = swf_GetSBits(tag, n); //delta y
714 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
716 int v=swf_GetBits(tag, 1);
718 d = swf_GetSBits(tag, n); //vert/horz
719 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
721 } else { //curved edge
722 int n = swf_GetBits(tag, 4) + 2;
724 x1 = swf_GetSBits(tag, n);
725 y1 = swf_GetSBits(tag, n);
726 x2 = swf_GetSBits(tag, n);
727 y2 = swf_GetSBits(tag, n);
728 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
740 void callbackCount(TAG * t,int pos, void*ptr)
743 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
746 void callbackFillin(TAG * t,int pos, void*ptr)
750 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
753 int swf_GetNumUsedIDs(TAG * t)
756 enumerateUsedIDs(t, 0, callbackCount, &num);
760 void swf_GetUsedIDs(TAG * t, int * positions)
762 int * ptr = positions;
763 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
766 void swf_Relocate (SWF*swf, char*bitmap)
770 memset(slaveids, -1, sizeof(slaveids));
778 if(swf_isDefiningTag(tag))
783 id = swf_GetDefineID(tag); //own id
785 if(!bitmap[id]) { //free
790 for (t=1;t<65536;t++)
800 slaveids[id] = newid;
802 swf_SetDefineID(tag, newid);
805 num = swf_GetNumUsedIDs(tag);
806 ptr = malloc(sizeof(int)*num);
807 swf_GetUsedIDs(tag, ptr);
810 int id = GET16(&tag->data[ptr[t]]);
812 fprintf(stderr, "swf_Relocate: Mapping id never encountered before: %d\n", id);
816 PUT16(&tag->data[ptr[t]], id);