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 #define DEBUG_ENUMERATE if(0)
296 static void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
300 count = swf_GetU8(tag);
301 if(count == 0xff && num>1) // defineshape2,3 only
302 count = swf_GetU16(tag);
308 swf_ResetReadBits(tag);
309 type = swf_GetU8(tag); //type
312 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
314 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
316 else if(type == 0x10 || type == 0x12)
318 swf_ResetReadBits(tag);
319 swf_GetMatrix(tag, NULL);
321 swf_GetMatrix(tag, NULL);
322 swf_ResetReadBits(tag);
324 swf_GetMorphGradient(tag, NULL, NULL);
326 swf_GetGradient(tag, NULL, /*alpha*/ num>=3?1:0);
328 else if(type == 0x40 || type == 0x41)
330 swf_ResetReadBits(tag);
332 if(tag->data[tag->pos] != 0xff ||
333 tag->data[tag->pos+1] != 0xff)
334 (callback)(tag, tag->pos, callback_data);
337 swf_ResetReadBits(tag);
338 swf_GetMatrix(tag, NULL);
340 swf_GetMatrix(tag, NULL);
343 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
346 swf_ResetReadBits(tag);
347 count = swf_GetU8(tag); // line style array
349 count = swf_GetU16(tag);
356 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
358 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
362 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
365 swf_ResetReadBits(tag);
369 case ST_DEFINEBUTTONCXFORM: {
371 callback(tag, tag->pos + base, callback_data);
374 callback(tag, tag->pos + base, callback_data);
375 swf_GetU16(tag); //sound id
376 flags = swf_GetU8(tag);
378 swf_GetU32(tag); // in point
380 swf_GetU32(tag); // out points
382 swf_GetU16(tag); // loop count
385 int npoints = swf_GetU8(tag);
387 for(s=0;s<npoints;s++)
396 case ST_DEFINEBUTTONSOUND:
397 callback(tag, tag->pos + base, callback_data); //button id
400 case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
401 case ST_NAMECHARACTER:
402 case ST_GENERATORTEXT:
404 callback(tag, tag->pos + base, callback_data);
407 callback(tag, tag->pos + base, callback_data);
409 case ST_PLACEOBJECT2:
410 // only if placeflaghascharacter
411 if(!(tag->data[0]&2))
413 callback(tag, 3 + base, callback_data);
415 case ST_REMOVEOBJECT:
416 callback(tag, tag->pos + base, callback_data);
419 callback(tag, tag->pos + base, callback_data);
421 case ST_DEFINESPRITE: {
423 break; // sprite is expanded
425 swf_GetU16(tag); // id
426 swf_GetU16(tag); // framenum
429 U16 flags = swf_GetU16(tag);
432 TAG *tag2 = swf_InsertTag(NULL, id);
435 len = swf_GetU32(tag);
438 tag2->len = tag2->memsize = len;
439 tag2->data = malloc(len);
440 memcpy(tag2->data, &tag->data[tag->pos], len);
441 /* I never saw recursive sprites, but they are (theoretically)
442 possible, so better add base here again */
443 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
445 swf_GetBlock(tag, NULL, len);
449 case ST_DEFINEBUTTON2: // has some font ids in the button records
452 case ST_DEFINEBUTTON: {
453 swf_GetU16(tag); //button id
457 swf_GetU8(tag); //flag
458 offset = swf_GetU16(tag); //offset
463 if(!swf_GetU8(tag)) //flags
465 callback(tag, tag->pos + base, callback_data);
466 swf_GetU16(tag); //char
467 swf_GetU16(tag); //layer
468 swf_ResetReadBits(tag);
469 swf_GetMatrix(tag, NULL);
471 swf_ResetReadBits(tag);
472 swf_GetCXForm(tag, NULL, 1);
478 case ST_DEFINEEDITTEXT: {
480 swf_GetU16(tag); //id
481 swf_GetRect(tag, NULL); //bounding box
482 swf_ResetReadBits(tag);
483 flags1 = swf_GetU8(tag);
484 flags2 = swf_GetU8(tag);
486 callback(tag, tag->pos + base, callback_data);
491 case ST_DEFINETEXT: {
492 int glyphbits, advancebits;
494 id = swf_GetU16(tag); //id
495 swf_GetRect(tag, NULL); //bounding box
496 swf_ResetReadBits(tag);
497 swf_GetMatrix(tag, NULL); //matrix
498 swf_ResetReadBits(tag);
499 glyphbits = swf_GetU8(tag); //glyphbits
500 advancebits = swf_GetU8(tag); //advancebits
503 swf_ResetReadBits(tag);
504 flags = swf_GetBits(tag, 8);
506 if(flags & 128) // text style record
508 swf_ResetReadBits(tag);
509 if(flags & 8) { // hasfont
510 callback(tag, tag->pos + base, callback_data);
511 id = swf_GetU16(tag);
513 if(flags & 4) { // hascolor
514 if(num==1) swf_GetRGB(tag, NULL);
515 else swf_GetRGBA(tag, NULL);
517 if(flags & 2) { //has x offset
518 swf_ResetReadBits(tag);
521 if(flags & 1) { //has y offset
522 swf_ResetReadBits(tag);
525 if(flags & 8) { //has height
526 swf_ResetReadBits(tag);
529 } else { // glyph record
531 swf_ResetReadBits(tag);
532 for(t=0;t<flags;t++) {
533 swf_GetBits(tag, glyphbits);
534 swf_GetBits(tag, advancebits);
540 case ST_DEFINEFONTINFO:
541 callback(tag, tag->pos + base, callback_data);
544 case ST_DEFINEMORPHSHAPE:
545 case ST_DEFINESHAPE3: // these thingies might have bitmap ids in their fillstyles
547 case ST_DEFINESHAPE2:
549 case ST_DEFINESHAPE: {
554 if(tag->id == ST_DEFINEMORPHSHAPE)
557 id = swf_GetU16(tag); // id;
558 swf_GetRect(tag, NULL); // bounds
560 swf_GetRect(tag, NULL); // bounds2
561 swf_GetU32(tag); //offset to endedges
564 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
566 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
567 DEBUG_ENUMERATE printf("-------\n");
568 while(--morph>=0) /* morph shapes define two shapes */
570 swf_ResetReadBits(tag); //?
571 fillbits = swf_GetBits(tag, 4);
572 linebits = swf_GetBits(tag, 4);
573 DEBUG_ENUMERATE printf("%d %d\n", fillbits, linebits);
574 swf_ResetReadBits(tag);
577 flags = swf_GetBits(tag, 1);
578 if(!flags) { //style change
579 flags = swf_GetBits(tag, 5);
583 int n = swf_GetBits(tag, 5);
585 x = swf_GetBits(tag, n); //x
586 y = swf_GetBits(tag, n); //y
587 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
589 if(flags&2) { //fill0
591 fill0 = swf_GetBits(tag, fillbits);
592 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
594 if(flags&4) { //fill1
596 fill1 = swf_GetBits(tag, fillbits);
597 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
599 if(flags&8) { //linestyle
601 line = swf_GetBits(tag, linebits);
602 DEBUG_ENUMERATE printf("linestyle %d\n",line);
605 DEBUG_ENUMERATE printf("more fillstyles\n");
606 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
607 fillbits = swf_GetBits(tag, 4);
608 linebits = swf_GetBits(tag, 4);
611 flags = swf_GetBits(tag, 1);
612 if(flags) { //straight edge
613 int n = swf_GetBits(tag, 4) + 2;
614 if(swf_GetBits(tag, 1)) { //line flag
616 x = swf_GetSBits(tag, n); //delta x
617 y = swf_GetSBits(tag, n); //delta y
618 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
620 int v=swf_GetBits(tag, 1);
622 d = swf_GetSBits(tag, n); //vert/horz
623 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
625 } else { //curved edge
626 int n = swf_GetBits(tag, 4) + 2;
628 x1 = swf_GetSBits(tag, n);
629 y1 = swf_GetSBits(tag, n);
630 x2 = swf_GetSBits(tag, n);
631 y2 = swf_GetSBits(tag, n);
632 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
644 void callbackCount(TAG * t,int pos, void*ptr)
647 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
650 void callbackFillin(TAG * t,int pos, void*ptr)
654 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
657 int swf_GetNumUsedIDs(TAG * t)
660 enumerateUsedIDs(t, 0, callbackCount, &num);
664 void swf_GetUsedIDs(TAG * t, int * positions)
666 int * ptr = positions;
667 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
670 void swf_Relocate (SWF*swf, char*bitmap)
674 memset(slaveids, -1, sizeof(slaveids));
682 if(swf_isDefiningTag(tag))
687 id = swf_GetDefineID(tag); //own id
689 if(!bitmap[id]) { //free
694 for (t=1;t<65536;t++)
704 slaveids[id] = newid;
706 swf_SetDefineID(tag, newid);
709 num = swf_GetNumUsedIDs(tag);
710 ptr = malloc(sizeof(int)*num);
711 swf_GetUsedIDs(tag, ptr);
714 int id = GET16(&tag->data[ptr[t]]);
716 fprintf(stderr, "swf_Relocate: Mapping id never encountered before: %d\n", id);
720 PUT16(&tag->data[ptr[t]], id);