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
105 case ST_DEFINESPRITE:
106 case ST_NAMECHARACTER: //pseudodefine
111 swf_SetTagPos(t,oldTagPos);
116 U16 swf_GetPlaceID(TAG * t)
121 oldTagPos = swf_GetTagPos(t);
124 switch (swf_GetTagID(t))
125 { case ST_PLACEOBJECT:
126 case ST_REMOVEOBJECT:
127 case ST_FREECHARACTER:
132 case ST_PLACEOBJECT2:
133 { U8 flags = swf_GetU8(t);
134 U16 d = swf_GetU16(t);
135 id = (flags&PF_CHAR)?swf_GetU16(t):id;
140 swf_SetTagPos(t,oldTagPos);
145 static int swf_definingtagids[] =
158 ST_DEFINEBITSLOSSLESS,
159 ST_DEFINEBITSLOSSLESS2,
168 // tags which may be used inside a sprite definition
169 static int swf_spritetagids[] =
174 ST_REMOVEOBJECT2, //?
185 static int swf_pseudodefiningtagids[] =
188 ST_DEFINEBUTTONCXFORM,
189 ST_DEFINEBUTTONSOUND,
194 U8 swf_isAllowedSpriteTag(TAG * tag)
198 while(swf_spritetagids[t]>=0)
200 if(swf_spritetagids[t] == id)
207 U8 swf_isDefiningTag(TAG * tag)
211 while(swf_definingtagids[t]>=0)
213 if(swf_definingtagids[t] == id)
220 U8 swf_isPseudoDefiningTag(TAG * tag)
224 while(swf_pseudodefiningtagids[t]>=0)
226 if(swf_pseudodefiningtagids[t] == id)
233 U16 swf_GetDepth(TAG * t)
238 oldTagPos = swf_GetTagPos(t);
241 switch (swf_GetTagID(t))
242 { case ST_PLACEOBJECT:
243 case ST_REMOVEOBJECT:
245 depth = swf_GetU16(t);
247 case ST_REMOVEOBJECT2:
248 depth = swf_GetU16(t);
250 case ST_PLACEOBJECT2:
251 { U8 flags = swf_GetU8(t);
252 depth = swf_GetU16(t);
255 swf_SetTagPos(t,oldTagPos);
259 char* swf_GetName(TAG * t)
265 oldTagPos = swf_GetTagPos(t);
267 switch(swf_GetTagID(t))
270 name = &t->data[swf_GetTagPos(t)];
272 case ST_PLACEOBJECT2: {
273 U8 flags = swf_GetU8(t);
274 swf_GetU16(t); //depth;
278 swf_GetMatrix(t, &m);
280 swf_GetCXForm(t, &c, 1);
284 swf_ResetReadBits(t);
285 name = &t->data[swf_GetTagPos(t)];
290 swf_SetTagPos(t,oldTagPos);
294 /* used in enumerateUsedIDs */
295 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
305 gradient2->num = swf_GetU8(tag);
306 for(t=0;t<gradient1->num;t++)
311 gradient1->ratios[t] = swf_GetU8(tag);
312 swf_GetRGBA(tag, &gradient1->rgba[t]);
313 gradient2->ratios[t] = swf_GetU8(tag);
314 swf_GetRGBA(tag, &gradient2->rgba[t]);
318 #define DEBUG_ENUMERATE if(0)
320 static void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
324 count = swf_GetU8(tag);
325 if(count == 0xff && num>1) // defineshape2,3 only
326 count = swf_GetU16(tag);
332 swf_ResetReadBits(tag);
333 type = swf_GetU8(tag); //type
336 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
338 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
340 else if(type == 0x10 || type == 0x12)
342 swf_ResetReadBits(tag);
343 swf_GetMatrix(tag, NULL);
345 swf_GetMatrix(tag, NULL);
346 swf_ResetReadBits(tag);
348 swf_GetMorphGradient(tag, NULL, NULL);
350 swf_GetGradient(tag, NULL, /*alpha*/ num>=3?1:0);
352 else if(type == 0x40 || type == 0x41)
354 swf_ResetReadBits(tag);
356 if(tag->data[tag->pos] != 0xff ||
357 tag->data[tag->pos+1] != 0xff)
358 (callback)(tag, tag->pos, callback_data);
361 swf_ResetReadBits(tag);
362 swf_GetMatrix(tag, NULL);
364 swf_GetMatrix(tag, NULL);
367 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
370 swf_ResetReadBits(tag);
371 count = swf_GetU8(tag); // line style array
373 count = swf_GetU16(tag);
380 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
382 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
386 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
389 swf_ResetReadBits(tag);
393 case ST_DEFINEBUTTONCXFORM: {
395 callback(tag, tag->pos + base, callback_data);
398 callback(tag, tag->pos + base, callback_data);
399 swf_GetU16(tag); //sound id
400 flags = swf_GetU8(tag);
402 swf_GetU32(tag); // in point
404 swf_GetU32(tag); // out points
406 swf_GetU16(tag); // loop count
409 int npoints = swf_GetU8(tag);
411 for(s=0;s<npoints;s++)
420 case ST_DEFINEBUTTONSOUND:
421 callback(tag, tag->pos + base, callback_data); //button id
424 case ST_EXPORTASSETS: {
425 int num = swf_GetU16(tag);
428 callback(tag, tag->pos + base, callback_data); //button id
429 swf_GetU16(tag); //id
430 while(swf_GetU8(tag)); //name
434 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
435 case ST_NAMECHARACTER:
436 case ST_GENERATORTEXT:
438 callback(tag, tag->pos + base, callback_data);
441 callback(tag, tag->pos + base, callback_data);
443 case ST_PLACEOBJECT2:
444 // only if placeflaghascharacter
445 if(!(tag->data[0]&2))
447 callback(tag, 3 + base, callback_data);
449 case ST_REMOVEOBJECT:
450 callback(tag, tag->pos + base, callback_data);
453 callback(tag, tag->pos + base, callback_data);
455 case ST_DEFINESPRITE: {
457 break; // sprite is expanded
459 swf_GetU16(tag); // id
460 swf_GetU16(tag); // framenum
463 U16 flags = swf_GetU16(tag);
466 TAG *tag2 = swf_InsertTag(NULL, id);
469 len = swf_GetU32(tag);
472 tag2->len = tag2->memsize = len;
473 tag2->data = malloc(len);
474 memcpy(tag2->data, &tag->data[tag->pos], len);
475 /* I never saw recursive sprites, but they are (theoretically)
476 possible, so better add base here again */
477 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
479 swf_GetBlock(tag, NULL, len);
483 case ST_DEFINEBUTTON2: // has some font ids in the button records
486 case ST_DEFINEBUTTON: {
487 swf_GetU16(tag); //button id
491 swf_GetU8(tag); //flag
492 offset = swf_GetU16(tag); //offset
497 if(!swf_GetU8(tag)) //flags
499 callback(tag, tag->pos + base, callback_data);
500 swf_GetU16(tag); //char
501 swf_GetU16(tag); //layer
502 swf_ResetReadBits(tag);
503 swf_GetMatrix(tag, NULL);
505 swf_ResetReadBits(tag);
506 swf_GetCXForm(tag, NULL, 1);
512 case ST_DEFINEEDITTEXT: {
514 swf_GetU16(tag); //id
515 swf_GetRect(tag, NULL); //bounding box
516 swf_ResetReadBits(tag);
517 flags1 = swf_GetU8(tag);
518 flags2 = swf_GetU8(tag);
520 callback(tag, tag->pos + base, callback_data);
525 case ST_DEFINETEXT: {
526 int glyphbits, advancebits;
528 id = swf_GetU16(tag); //id
529 swf_GetRect(tag, NULL); //bounding box
530 swf_ResetReadBits(tag);
531 swf_GetMatrix(tag, NULL); //matrix
532 swf_ResetReadBits(tag);
533 glyphbits = swf_GetU8(tag); //glyphbits
534 advancebits = swf_GetU8(tag); //advancebits
537 swf_ResetReadBits(tag);
538 flags = swf_GetBits(tag, 8);
540 if(flags & 128) // text style record
542 swf_ResetReadBits(tag);
543 if(flags & 8) { // hasfont
544 callback(tag, tag->pos + base, callback_data);
545 id = swf_GetU16(tag);
547 if(flags & 4) { // hascolor
548 if(num==1) swf_GetRGB(tag, NULL);
549 else swf_GetRGBA(tag, NULL);
551 if(flags & 2) { //has x offset
552 swf_ResetReadBits(tag);
555 if(flags & 1) { //has y offset
556 swf_ResetReadBits(tag);
559 if(flags & 8) { //has height
560 swf_ResetReadBits(tag);
563 } else { // glyph record
565 swf_ResetReadBits(tag);
566 for(t=0;t<flags;t++) {
567 swf_GetBits(tag, glyphbits);
568 swf_GetBits(tag, advancebits);
574 case ST_DEFINEFONTINFO:
575 callback(tag, tag->pos + base, callback_data);
578 //case ST_DEFINEMORPHSHAPE: /* disabled for now (doesn't work) */
580 case ST_DEFINESHAPE3: // these thingies might have bitmap ids in their fillstyles
582 case ST_DEFINESHAPE2:
584 case ST_DEFINESHAPE: {
589 if(tag->id == ST_DEFINEMORPHSHAPE)
592 id = swf_GetU16(tag); // id;
593 swf_GetRect(tag, NULL); // bounds
595 swf_GetRect(tag, NULL); // bounds2
596 swf_GetU32(tag); //offset to endedges
599 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
601 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
602 DEBUG_ENUMERATE printf("-------\n");
603 while(--morph>=0) /* morph shapes define two shapes */
605 fillbits = swf_GetBits(tag, 4);
606 linebits = swf_GetBits(tag, 4);
607 DEBUG_ENUMERATE printf("%d %d\n", fillbits, linebits);
608 swf_ResetReadBits(tag);
611 flags = swf_GetBits(tag, 1);
612 if(!flags) { //style change
613 flags = swf_GetBits(tag, 5);
617 int n = swf_GetBits(tag, 5);
619 x = swf_GetBits(tag, n); //x
620 y = swf_GetBits(tag, n); //y
621 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
623 if(flags&2) { //fill0
625 fill0 = swf_GetBits(tag, fillbits);
626 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
628 if(flags&4) { //fill1
630 fill1 = swf_GetBits(tag, fillbits);
631 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
633 if(flags&8) { //linestyle
635 line = swf_GetBits(tag, linebits);
636 DEBUG_ENUMERATE printf("linestyle %d\n",line);
639 DEBUG_ENUMERATE printf("more fillstyles\n");
640 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
641 fillbits = swf_GetBits(tag, 4);
642 linebits = swf_GetBits(tag, 4);
645 flags = swf_GetBits(tag, 1);
646 if(flags) { //straight edge
647 int n = swf_GetBits(tag, 4) + 2;
648 if(swf_GetBits(tag, 1)) { //line flag
650 x = swf_GetSBits(tag, n); //delta x
651 y = swf_GetSBits(tag, n); //delta y
652 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
654 int v=swf_GetBits(tag, 1);
656 d = swf_GetSBits(tag, n); //vert/horz
657 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
659 } else { //curved edge
660 int n = swf_GetBits(tag, 4) + 2;
662 x1 = swf_GetSBits(tag, n);
663 y1 = swf_GetSBits(tag, n);
664 x2 = swf_GetSBits(tag, n);
665 y2 = swf_GetSBits(tag, n);
666 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
678 void callbackCount(TAG * t,int pos, void*ptr)
681 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
684 void callbackFillin(TAG * t,int pos, void*ptr)
688 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
691 int swf_GetNumUsedIDs(TAG * t)
694 enumerateUsedIDs(t, 0, callbackCount, &num);
698 void swf_GetUsedIDs(TAG * t, int * positions)
700 int * ptr = positions;
701 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
704 void swf_Relocate (SWF*swf, char*bitmap)
708 memset(slaveids, -1, sizeof(slaveids));
716 if(swf_isDefiningTag(tag))
721 id = swf_GetDefineID(tag); //own id
723 if(!bitmap[id]) { //free
728 for (t=1;t<65536;t++)
738 slaveids[id] = newid;
740 swf_SetDefineID(tag, newid);
743 num = swf_GetNumUsedIDs(tag);
744 ptr = malloc(sizeof(int)*num);
745 swf_GetUsedIDs(tag, ptr);
748 int id = GET16(&tag->data[ptr[t]]);
750 fprintf(stderr, "swf_Relocate: Mapping id never encountered before: %d\n", id);
754 PUT16(&tag->data[ptr[t]], id);