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:
108 case ST_DEFINEVIDEOSTREAM:
109 case ST_VIDEOFRAME: //pseudodefine
110 case ST_NAMECHARACTER: //pseudodefine
115 swf_SetTagPos(t,oldTagPos);
120 SRECT swf_GetDefineBBox(TAG * t)
126 oldTagPos = swf_GetTagPos(t);
131 switch (swf_GetTagID(t))
132 { case ST_DEFINESHAPE:
133 case ST_DEFINESHAPE2:
134 case ST_DEFINESHAPE3:
135 case ST_DEFINEEDITTEXT:
136 case ST_DEFINEBUTTON:
137 case ST_DEFINEBUTTON2:
140 case ST_DEFINEVIDEOSTREAM:
144 case ST_DEFINEMORPHSHAPE:
148 swf_ExpandRect2(&b1, &b2);
150 case ST_DEFINEBITSLOSSLESS:
151 case ST_DEFINEBITSLOSSLESS2:
153 case ST_DEFINEBITSJPEG2:
154 case ST_DEFINEBITSJPEG3:
159 swf_SetTagPos(t,oldTagPos);
164 U16 swf_GetPlaceID(TAG * t)
169 oldTagPos = swf_GetTagPos(t);
172 switch (swf_GetTagID(t))
173 { case ST_PLACEOBJECT:
174 case ST_REMOVEOBJECT:
175 case ST_FREECHARACTER:
180 case ST_PLACEOBJECT2:
181 { U8 flags = swf_GetU8(t);
182 U16 d = swf_GetU16(t);
183 id = (flags&PF_CHAR)?swf_GetU16(t):id;
188 swf_SetTagPos(t,oldTagPos);
193 static int swf_definingtagids[] =
206 ST_DEFINEBITSLOSSLESS,
207 ST_DEFINEBITSLOSSLESS2,
213 ST_DEFINEVIDEOSTREAM,
217 // tags which may be used inside a sprite definition
218 static int swf_spritetagids[] =
223 ST_REMOVEOBJECT2, //?
234 static int swf_pseudodefiningtagids[] =
238 ST_DEFINEBUTTONCXFORM,
239 ST_DEFINEBUTTONSOUND,
246 U8 swf_isAllowedSpriteTag(TAG * tag)
250 while(swf_spritetagids[t]>=0)
252 if(swf_spritetagids[t] == id)
259 U8 swf_isDefiningTag(TAG * tag)
263 while(swf_definingtagids[t]>=0)
265 if(swf_definingtagids[t] == id)
272 U8 swf_isPseudoDefiningTag(TAG * tag)
276 while(swf_pseudodefiningtagids[t]>=0)
278 if(swf_pseudodefiningtagids[t] == id)
285 U16 swf_GetDepth(TAG * t)
290 oldTagPos = swf_GetTagPos(t);
293 switch (swf_GetTagID(t))
294 { case ST_PLACEOBJECT:
295 case ST_REMOVEOBJECT:
297 depth = swf_GetU16(t);
299 case ST_REMOVEOBJECT2:
300 depth = swf_GetU16(t);
302 case ST_PLACEOBJECT2:
303 { U8 flags = swf_GetU8(t);
304 depth = swf_GetU16(t);
307 swf_SetTagPos(t,oldTagPos);
311 char* swf_GetName(TAG * t)
317 oldTagPos = swf_GetTagPos(t);
319 switch(swf_GetTagID(t))
322 name = &t->data[swf_GetTagPos(t)];
324 case ST_PLACEOBJECT2: {
325 U8 flags = swf_GetU8(t);
326 swf_GetU16(t); //depth;
330 swf_GetMatrix(t, &m);
332 swf_GetCXForm(t, &c, 1);
335 if(flags&PF_CLIPACTION)
338 swf_ResetReadBits(t);
339 name = &t->data[swf_GetTagPos(t)];
344 swf_SetTagPos(t,oldTagPos);
348 /* used in enumerateUsedIDs */
349 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
359 gradient2->num = swf_GetU8(tag);
360 for(t=0;t<gradient1->num;t++)
365 gradient1->ratios[t] = swf_GetU8(tag);
366 swf_GetRGBA(tag, &gradient1->rgba[t]);
367 gradient2->ratios[t] = swf_GetU8(tag);
368 swf_GetRGBA(tag, &gradient2->rgba[t]);
372 #define DEBUG_ENUMERATE if(0)
374 static void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
378 count = swf_GetU8(tag);
379 if(count == 0xff && num>1) // defineshape2,3 only
380 count = swf_GetU16(tag);
386 swf_ResetReadBits(tag);
387 type = swf_GetU8(tag); //type
390 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
392 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
394 else if(type == 0x10 || type == 0x12)
396 swf_ResetReadBits(tag);
397 swf_GetMatrix(tag, NULL);
399 swf_GetMatrix(tag, NULL);
400 swf_ResetReadBits(tag);
402 swf_GetMorphGradient(tag, NULL, NULL);
404 swf_GetGradient(tag, NULL, /*alpha*/ num>=3?1:0);
406 else if(type == 0x40 || type == 0x41)
408 swf_ResetReadBits(tag);
410 if(tag->data[tag->pos] != 0xff ||
411 tag->data[tag->pos+1] != 0xff)
412 (callback)(tag, tag->pos, callback_data);
415 swf_ResetReadBits(tag);
416 swf_GetMatrix(tag, NULL);
418 swf_GetMatrix(tag, NULL);
421 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
424 swf_ResetReadBits(tag);
425 count = swf_GetU8(tag); // line style array
427 count = swf_GetU16(tag);
434 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
436 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
440 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
443 swf_ResetReadBits(tag);
447 case ST_DEFINEBUTTONCXFORM: {
449 callback(tag, tag->pos + base, callback_data);
452 callback(tag, tag->pos + base, callback_data);
453 swf_GetU16(tag); //sound id
454 flags = swf_GetU8(tag);
456 swf_GetU32(tag); // in point
458 swf_GetU32(tag); // out points
460 swf_GetU16(tag); // loop count
463 int npoints = swf_GetU8(tag);
465 for(s=0;s<npoints;s++)
474 case ST_DEFINEBUTTONSOUND:
475 callback(tag, tag->pos + base, callback_data); //button id
478 case ST_EXPORTASSETS: {
479 int num = swf_GetU16(tag);
482 callback(tag, tag->pos + base, callback_data); //button id
483 swf_GetU16(tag); //id
484 while(swf_GetU8(tag)); //name
488 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
489 case ST_NAMECHARACTER:
490 case ST_GENERATORTEXT:
491 callback(tag, tag->pos + base, callback_data);
494 callback(tag, tag->pos + base, callback_data);
496 case ST_PLACEOBJECT2:
497 // only if placeflaghascharacter
498 if(!(tag->data[0]&2))
500 callback(tag, 3 + base, callback_data);
502 case ST_REMOVEOBJECT:
503 callback(tag, tag->pos + base, callback_data);
506 callback(tag, tag->pos + base, callback_data);
508 case ST_DEFINESPRITE: {
510 break; // sprite is expanded
512 swf_GetU16(tag); // id
513 swf_GetU16(tag); // framenum
516 U16 flags = swf_GetU16(tag);
519 TAG *tag2 = swf_InsertTag(NULL, id);
522 len = swf_GetU32(tag);
525 tag2->len = tag2->memsize = len;
526 tag2->data = malloc(len);
527 memcpy(tag2->data, &tag->data[tag->pos], len);
528 /* I never saw recursive sprites, but they are (theoretically)
529 possible, so better add base here again */
530 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
532 swf_GetBlock(tag, NULL, len);
536 case ST_DEFINEBUTTON2: // has some font ids in the button records
539 case ST_DEFINEBUTTON: {
540 swf_GetU16(tag); //button id
544 swf_GetU8(tag); //flag
545 offset = swf_GetU16(tag); //offset
550 if(!swf_GetU8(tag)) //flags
552 callback(tag, tag->pos + base, callback_data);
553 swf_GetU16(tag); //char
554 swf_GetU16(tag); //layer
555 swf_ResetReadBits(tag);
556 swf_GetMatrix(tag, NULL);
558 swf_ResetReadBits(tag);
559 swf_GetCXForm(tag, NULL, 1);
565 case ST_DEFINEEDITTEXT: {
567 swf_GetU16(tag); //id
568 swf_GetRect(tag, NULL); //bounding box
569 swf_ResetReadBits(tag);
570 flags1 = swf_GetU8(tag);
571 flags2 = swf_GetU8(tag);
573 callback(tag, tag->pos + base, callback_data);
578 case ST_DEFINETEXT: {
579 int glyphbits, advancebits;
581 id = swf_GetU16(tag); //id
582 swf_GetRect(tag, NULL); //bounding box
583 swf_ResetReadBits(tag);
584 swf_GetMatrix(tag, NULL); //matrix
585 swf_ResetReadBits(tag);
586 glyphbits = swf_GetU8(tag); //glyphbits
587 advancebits = swf_GetU8(tag); //advancebits
590 swf_ResetReadBits(tag);
591 flags = swf_GetBits(tag, 8);
593 if(flags & 128) // text style record
595 swf_ResetReadBits(tag);
596 if(flags & 8) { // hasfont
597 callback(tag, tag->pos + base, callback_data);
598 id = swf_GetU16(tag);
600 if(flags & 4) { // hascolor
601 if(num==1) swf_GetRGB(tag, NULL);
602 else swf_GetRGBA(tag, NULL);
604 if(flags & 2) { //has x offset
605 swf_ResetReadBits(tag);
608 if(flags & 1) { //has y offset
609 swf_ResetReadBits(tag);
612 if(flags & 8) { //has height
613 swf_ResetReadBits(tag);
616 } else { // glyph record
618 swf_ResetReadBits(tag);
619 for(t=0;t<flags;t++) {
620 swf_GetBits(tag, glyphbits);
621 swf_GetBits(tag, advancebits);
627 case ST_DEFINEFONTINFO:
628 case ST_DEFINEFONTINFO2:
630 callback(tag, tag->pos + base, callback_data);
632 case ST_DEFINEVIDEOSTREAM:
635 case ST_DOINITACTION:
636 callback(tag, tag->pos + base, callback_data);
639 case ST_DEFINEMORPHSHAPE:
640 case ST_DEFINESHAPE3:
642 case ST_DEFINESHAPE2:
644 case ST_DEFINESHAPE: {
650 if(tag->id == ST_DEFINEMORPHSHAPE) {
655 id = swf_GetU16(tag); // id;
656 swf_GetRect(tag, NULL); // bounds
658 swf_ResetReadBits(tag);
659 swf_GetRect(tag, NULL); // bounds2
660 swf_GetU32(tag); //offset to endedges
663 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
665 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
666 DEBUG_ENUMERATE printf("-------\n");
667 while(--numshapes>=0) /* morph shapes define two shapes */
669 DEBUG_ENUMERATE printf("shape:%d\n", numshapes);
670 fillbits = swf_GetBits(tag, 4);
671 linebits = swf_GetBits(tag, 4);
672 DEBUG_ENUMERATE printf("%d %d\n", fillbits, linebits);
673 swf_ResetReadBits(tag);
676 flags = swf_GetBits(tag, 1);
677 if(!flags) { //style change
678 flags = swf_GetBits(tag, 5);
682 int n = swf_GetBits(tag, 5);
684 x = swf_GetBits(tag, n); //x
685 y = swf_GetBits(tag, n); //y
686 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
688 if(flags&2) { //fill0
690 fill0 = swf_GetBits(tag, fillbits);
691 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
693 if(flags&4) { //fill1
695 fill1 = swf_GetBits(tag, fillbits);
696 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
698 if(flags&8) { //linestyle
700 line = swf_GetBits(tag, linebits);
701 DEBUG_ENUMERATE printf("linestyle %d\n",line);
704 DEBUG_ENUMERATE printf("more fillstyles\n");
705 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
706 fillbits = swf_GetBits(tag, 4);
707 linebits = swf_GetBits(tag, 4);
710 flags = swf_GetBits(tag, 1);
711 if(flags) { //straight edge
712 int n = swf_GetBits(tag, 4) + 2;
713 if(swf_GetBits(tag, 1)) { //line flag
715 x = swf_GetSBits(tag, n); //delta x
716 y = swf_GetSBits(tag, n); //delta y
717 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
719 int v=swf_GetBits(tag, 1);
721 d = swf_GetSBits(tag, n); //vert/horz
722 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
724 } else { //curved edge
725 int n = swf_GetBits(tag, 4) + 2;
727 x1 = swf_GetSBits(tag, n);
728 y1 = swf_GetSBits(tag, n);
729 x2 = swf_GetSBits(tag, n);
730 y2 = swf_GetSBits(tag, n);
731 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
743 void callbackCount(TAG * t,int pos, void*ptr)
746 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
749 void callbackFillin(TAG * t,int pos, void*ptr)
753 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
756 int swf_GetNumUsedIDs(TAG * t)
759 enumerateUsedIDs(t, 0, callbackCount, &num);
763 void swf_GetUsedIDs(TAG * t, int * positions)
765 int * ptr = positions;
766 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
769 void swf_Relocate (SWF*swf, char*bitmap)
773 memset(slaveids, -1, sizeof(slaveids));
781 if(swf_isDefiningTag(tag))
786 id = swf_GetDefineID(tag); //own id
788 if(!bitmap[id]) { //free
793 for (t=1;t<65536;t++)
803 slaveids[id] = newid;
805 swf_SetDefineID(tag, newid);
808 num = swf_GetNumUsedIDs(tag);
809 ptr = malloc(sizeof(int)*num);
810 swf_GetUsedIDs(tag, ptr);
813 int id = GET16(&tag->data[ptr[t]]);
815 fprintf(stderr, "swf_Relocate: Mapping id never encountered before: %d\n", id);
819 PUT16(&tag->data[ptr[t]], id);