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 U16 swf_GetPlaceID(TAG * t)
123 oldTagPos = swf_GetTagPos(t);
126 switch (swf_GetTagID(t))
127 { case ST_PLACEOBJECT:
128 case ST_REMOVEOBJECT:
129 case ST_FREECHARACTER:
134 case ST_PLACEOBJECT2:
135 { U8 flags = swf_GetU8(t);
136 U16 d = swf_GetU16(t);
137 id = (flags&PF_CHAR)?swf_GetU16(t):id;
142 swf_SetTagPos(t,oldTagPos);
147 static int swf_definingtagids[] =
160 ST_DEFINEBITSLOSSLESS,
161 ST_DEFINEBITSLOSSLESS2,
167 ST_DEFINEVIDEOSTREAM,
171 // tags which may be used inside a sprite definition
172 static int swf_spritetagids[] =
177 ST_REMOVEOBJECT2, //?
188 static int swf_pseudodefiningtagids[] =
192 ST_DEFINEBUTTONCXFORM,
193 ST_DEFINEBUTTONSOUND,
199 U8 swf_isAllowedSpriteTag(TAG * tag)
203 while(swf_spritetagids[t]>=0)
205 if(swf_spritetagids[t] == id)
212 U8 swf_isDefiningTag(TAG * tag)
216 while(swf_definingtagids[t]>=0)
218 if(swf_definingtagids[t] == id)
225 U8 swf_isPseudoDefiningTag(TAG * tag)
229 while(swf_pseudodefiningtagids[t]>=0)
231 if(swf_pseudodefiningtagids[t] == id)
238 U16 swf_GetDepth(TAG * t)
243 oldTagPos = swf_GetTagPos(t);
246 switch (swf_GetTagID(t))
247 { case ST_PLACEOBJECT:
248 case ST_REMOVEOBJECT:
250 depth = swf_GetU16(t);
252 case ST_REMOVEOBJECT2:
253 depth = swf_GetU16(t);
255 case ST_PLACEOBJECT2:
256 { U8 flags = swf_GetU8(t);
257 depth = swf_GetU16(t);
260 swf_SetTagPos(t,oldTagPos);
264 char* swf_GetName(TAG * t)
270 oldTagPos = swf_GetTagPos(t);
272 switch(swf_GetTagID(t))
275 name = &t->data[swf_GetTagPos(t)];
277 case ST_PLACEOBJECT2: {
278 U8 flags = swf_GetU8(t);
279 swf_GetU16(t); //depth;
283 swf_GetMatrix(t, &m);
285 swf_GetCXForm(t, &c, 1);
289 swf_ResetReadBits(t);
290 name = &t->data[swf_GetTagPos(t)];
295 swf_SetTagPos(t,oldTagPos);
299 /* used in enumerateUsedIDs */
300 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
310 gradient2->num = swf_GetU8(tag);
311 for(t=0;t<gradient1->num;t++)
316 gradient1->ratios[t] = swf_GetU8(tag);
317 swf_GetRGBA(tag, &gradient1->rgba[t]);
318 gradient2->ratios[t] = swf_GetU8(tag);
319 swf_GetRGBA(tag, &gradient2->rgba[t]);
323 #define DEBUG_ENUMERATE if(0)
325 static void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
329 count = swf_GetU8(tag);
330 if(count == 0xff && num>1) // defineshape2,3 only
331 count = swf_GetU16(tag);
337 swf_ResetReadBits(tag);
338 type = swf_GetU8(tag); //type
341 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
343 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
345 else if(type == 0x10 || type == 0x12)
347 swf_ResetReadBits(tag);
348 swf_GetMatrix(tag, NULL);
350 swf_GetMatrix(tag, NULL);
351 swf_ResetReadBits(tag);
353 swf_GetMorphGradient(tag, NULL, NULL);
355 swf_GetGradient(tag, NULL, /*alpha*/ num>=3?1:0);
357 else if(type == 0x40 || type == 0x41)
359 swf_ResetReadBits(tag);
361 if(tag->data[tag->pos] != 0xff ||
362 tag->data[tag->pos+1] != 0xff)
363 (callback)(tag, tag->pos, callback_data);
366 swf_ResetReadBits(tag);
367 swf_GetMatrix(tag, NULL);
369 swf_GetMatrix(tag, NULL);
372 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
375 swf_ResetReadBits(tag);
376 count = swf_GetU8(tag); // line style array
378 count = swf_GetU16(tag);
385 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
387 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
391 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
394 swf_ResetReadBits(tag);
398 case ST_DEFINEBUTTONCXFORM: {
400 callback(tag, tag->pos + base, callback_data);
403 callback(tag, tag->pos + base, callback_data);
404 swf_GetU16(tag); //sound id
405 flags = swf_GetU8(tag);
407 swf_GetU32(tag); // in point
409 swf_GetU32(tag); // out points
411 swf_GetU16(tag); // loop count
414 int npoints = swf_GetU8(tag);
416 for(s=0;s<npoints;s++)
425 case ST_DEFINEBUTTONSOUND:
426 callback(tag, tag->pos + base, callback_data); //button id
429 case ST_EXPORTASSETS: {
430 int num = swf_GetU16(tag);
433 callback(tag, tag->pos + base, callback_data); //button id
434 swf_GetU16(tag); //id
435 while(swf_GetU8(tag)); //name
439 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
440 case ST_NAMECHARACTER:
441 case ST_GENERATORTEXT:
442 callback(tag, tag->pos + base, callback_data);
445 callback(tag, tag->pos + base, callback_data);
447 case ST_PLACEOBJECT2:
448 // only if placeflaghascharacter
449 if(!(tag->data[0]&2))
451 callback(tag, 3 + base, callback_data);
453 case ST_REMOVEOBJECT:
454 callback(tag, tag->pos + base, callback_data);
457 callback(tag, tag->pos + base, callback_data);
459 case ST_DEFINESPRITE: {
461 break; // sprite is expanded
463 swf_GetU16(tag); // id
464 swf_GetU16(tag); // framenum
467 U16 flags = swf_GetU16(tag);
470 TAG *tag2 = swf_InsertTag(NULL, id);
473 len = swf_GetU32(tag);
476 tag2->len = tag2->memsize = len;
477 tag2->data = malloc(len);
478 memcpy(tag2->data, &tag->data[tag->pos], len);
479 /* I never saw recursive sprites, but they are (theoretically)
480 possible, so better add base here again */
481 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
483 swf_GetBlock(tag, NULL, len);
487 case ST_DEFINEBUTTON2: // has some font ids in the button records
490 case ST_DEFINEBUTTON: {
491 swf_GetU16(tag); //button id
495 swf_GetU8(tag); //flag
496 offset = swf_GetU16(tag); //offset
501 if(!swf_GetU8(tag)) //flags
503 callback(tag, tag->pos + base, callback_data);
504 swf_GetU16(tag); //char
505 swf_GetU16(tag); //layer
506 swf_ResetReadBits(tag);
507 swf_GetMatrix(tag, NULL);
509 swf_ResetReadBits(tag);
510 swf_GetCXForm(tag, NULL, 1);
516 case ST_DEFINEEDITTEXT: {
518 swf_GetU16(tag); //id
519 swf_GetRect(tag, NULL); //bounding box
520 swf_ResetReadBits(tag);
521 flags1 = swf_GetU8(tag);
522 flags2 = swf_GetU8(tag);
524 callback(tag, tag->pos + base, callback_data);
529 case ST_DEFINETEXT: {
530 int glyphbits, advancebits;
532 id = swf_GetU16(tag); //id
533 swf_GetRect(tag, NULL); //bounding box
534 swf_ResetReadBits(tag);
535 swf_GetMatrix(tag, NULL); //matrix
536 swf_ResetReadBits(tag);
537 glyphbits = swf_GetU8(tag); //glyphbits
538 advancebits = swf_GetU8(tag); //advancebits
541 swf_ResetReadBits(tag);
542 flags = swf_GetBits(tag, 8);
544 if(flags & 128) // text style record
546 swf_ResetReadBits(tag);
547 if(flags & 8) { // hasfont
548 callback(tag, tag->pos + base, callback_data);
549 id = swf_GetU16(tag);
551 if(flags & 4) { // hascolor
552 if(num==1) swf_GetRGB(tag, NULL);
553 else swf_GetRGBA(tag, NULL);
555 if(flags & 2) { //has x offset
556 swf_ResetReadBits(tag);
559 if(flags & 1) { //has y offset
560 swf_ResetReadBits(tag);
563 if(flags & 8) { //has height
564 swf_ResetReadBits(tag);
567 } else { // glyph record
569 swf_ResetReadBits(tag);
570 for(t=0;t<flags;t++) {
571 swf_GetBits(tag, glyphbits);
572 swf_GetBits(tag, advancebits);
578 case ST_DEFINEFONTINFO:
579 case ST_DEFINEFONTINFO2:
581 callback(tag, tag->pos + base, callback_data);
583 case ST_DEFINEVIDEOSTREAM:
586 case ST_DOINITACTION:
587 callback(tag, tag->pos + base, callback_data);
590 case ST_DEFINEMORPHSHAPE:
591 case ST_DEFINESHAPE3:
593 case ST_DEFINESHAPE2:
595 case ST_DEFINESHAPE: {
601 if(tag->id == ST_DEFINEMORPHSHAPE) {
606 id = swf_GetU16(tag); // id;
607 swf_GetRect(tag, NULL); // bounds
609 swf_ResetReadBits(tag);
610 swf_GetRect(tag, NULL); // bounds2
611 swf_GetU32(tag); //offset to endedges
614 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
616 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
617 DEBUG_ENUMERATE printf("-------\n");
618 while(--numshapes>=0) /* morph shapes define two shapes */
620 DEBUG_ENUMERATE printf("shape:%d\n", numshapes);
621 fillbits = swf_GetBits(tag, 4);
622 linebits = swf_GetBits(tag, 4);
623 DEBUG_ENUMERATE printf("%d %d\n", fillbits, linebits);
624 swf_ResetReadBits(tag);
627 flags = swf_GetBits(tag, 1);
628 if(!flags) { //style change
629 flags = swf_GetBits(tag, 5);
633 int n = swf_GetBits(tag, 5);
635 x = swf_GetBits(tag, n); //x
636 y = swf_GetBits(tag, n); //y
637 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
639 if(flags&2) { //fill0
641 fill0 = swf_GetBits(tag, fillbits);
642 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
644 if(flags&4) { //fill1
646 fill1 = swf_GetBits(tag, fillbits);
647 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
649 if(flags&8) { //linestyle
651 line = swf_GetBits(tag, linebits);
652 DEBUG_ENUMERATE printf("linestyle %d\n",line);
655 DEBUG_ENUMERATE printf("more fillstyles\n");
656 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
657 fillbits = swf_GetBits(tag, 4);
658 linebits = swf_GetBits(tag, 4);
661 flags = swf_GetBits(tag, 1);
662 if(flags) { //straight edge
663 int n = swf_GetBits(tag, 4) + 2;
664 if(swf_GetBits(tag, 1)) { //line flag
666 x = swf_GetSBits(tag, n); //delta x
667 y = swf_GetSBits(tag, n); //delta y
668 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
670 int v=swf_GetBits(tag, 1);
672 d = swf_GetSBits(tag, n); //vert/horz
673 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
675 } else { //curved edge
676 int n = swf_GetBits(tag, 4) + 2;
678 x1 = swf_GetSBits(tag, n);
679 y1 = swf_GetSBits(tag, n);
680 x2 = swf_GetSBits(tag, n);
681 y2 = swf_GetSBits(tag, n);
682 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
694 void callbackCount(TAG * t,int pos, void*ptr)
697 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
700 void callbackFillin(TAG * t,int pos, void*ptr)
704 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
707 int swf_GetNumUsedIDs(TAG * t)
710 enumerateUsedIDs(t, 0, callbackCount, &num);
714 void swf_GetUsedIDs(TAG * t, int * positions)
716 int * ptr = positions;
717 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
720 void swf_Relocate (SWF*swf, char*bitmap)
724 memset(slaveids, -1, sizeof(slaveids));
732 if(swf_isDefiningTag(tag))
737 id = swf_GetDefineID(tag); //own id
739 if(!bitmap[id]) { //free
744 for (t=1;t<65536;t++)
754 slaveids[id] = newid;
756 swf_SetDefineID(tag, newid);
759 num = swf_GetNumUsedIDs(tag);
760 ptr = malloc(sizeof(int)*num);
761 swf_GetUsedIDs(tag, ptr);
764 int id = GET16(&tag->data[ptr[t]]);
766 fprintf(stderr, "swf_Relocate: Mapping id never encountered before: %d\n", id);
770 PUT16(&tag->data[ptr[t]], id);