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:
443 callback(tag, tag->pos + base, callback_data);
446 callback(tag, tag->pos + base, callback_data);
448 case ST_PLACEOBJECT2:
449 // only if placeflaghascharacter
450 if(!(tag->data[0]&2))
452 callback(tag, 3 + base, callback_data);
454 case ST_REMOVEOBJECT:
455 callback(tag, tag->pos + base, callback_data);
458 callback(tag, tag->pos + base, callback_data);
460 case ST_DEFINESPRITE: {
462 break; // sprite is expanded
464 swf_GetU16(tag); // id
465 swf_GetU16(tag); // framenum
468 U16 flags = swf_GetU16(tag);
471 TAG *tag2 = swf_InsertTag(NULL, id);
474 len = swf_GetU32(tag);
477 tag2->len = tag2->memsize = len;
478 tag2->data = malloc(len);
479 memcpy(tag2->data, &tag->data[tag->pos], len);
480 /* I never saw recursive sprites, but they are (theoretically)
481 possible, so better add base here again */
482 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
484 swf_GetBlock(tag, NULL, len);
488 case ST_DEFINEBUTTON2: // has some font ids in the button records
491 case ST_DEFINEBUTTON: {
492 swf_GetU16(tag); //button id
496 swf_GetU8(tag); //flag
497 offset = swf_GetU16(tag); //offset
502 if(!swf_GetU8(tag)) //flags
504 callback(tag, tag->pos + base, callback_data);
505 swf_GetU16(tag); //char
506 swf_GetU16(tag); //layer
507 swf_ResetReadBits(tag);
508 swf_GetMatrix(tag, NULL);
510 swf_ResetReadBits(tag);
511 swf_GetCXForm(tag, NULL, 1);
517 case ST_DEFINEEDITTEXT: {
519 swf_GetU16(tag); //id
520 swf_GetRect(tag, NULL); //bounding box
521 swf_ResetReadBits(tag);
522 flags1 = swf_GetU8(tag);
523 flags2 = swf_GetU8(tag);
525 callback(tag, tag->pos + base, callback_data);
530 case ST_DEFINETEXT: {
531 int glyphbits, advancebits;
533 id = swf_GetU16(tag); //id
534 swf_GetRect(tag, NULL); //bounding box
535 swf_ResetReadBits(tag);
536 swf_GetMatrix(tag, NULL); //matrix
537 swf_ResetReadBits(tag);
538 glyphbits = swf_GetU8(tag); //glyphbits
539 advancebits = swf_GetU8(tag); //advancebits
542 swf_ResetReadBits(tag);
543 flags = swf_GetBits(tag, 8);
545 if(flags & 128) // text style record
547 swf_ResetReadBits(tag);
548 if(flags & 8) { // hasfont
549 callback(tag, tag->pos + base, callback_data);
550 id = swf_GetU16(tag);
552 if(flags & 4) { // hascolor
553 if(num==1) swf_GetRGB(tag, NULL);
554 else swf_GetRGBA(tag, NULL);
556 if(flags & 2) { //has x offset
557 swf_ResetReadBits(tag);
560 if(flags & 1) { //has y offset
561 swf_ResetReadBits(tag);
564 if(flags & 8) { //has height
565 swf_ResetReadBits(tag);
568 } else { // glyph record
570 swf_ResetReadBits(tag);
571 for(t=0;t<flags;t++) {
572 swf_GetBits(tag, glyphbits);
573 swf_GetBits(tag, advancebits);
579 case ST_DEFINEFONTINFO:
580 case ST_DEFINEFONTINFO2:
582 callback(tag, tag->pos + base, callback_data);
584 case ST_DEFINEVIDEOSTREAM:
587 case ST_DOINITACTION:
588 callback(tag, tag->pos + base, callback_data);
591 case ST_DEFINEMORPHSHAPE:
592 case ST_DEFINESHAPE3:
594 case ST_DEFINESHAPE2:
596 case ST_DEFINESHAPE: {
602 if(tag->id == ST_DEFINEMORPHSHAPE) {
607 id = swf_GetU16(tag); // id;
608 swf_GetRect(tag, NULL); // bounds
610 swf_ResetReadBits(tag);
611 swf_GetRect(tag, NULL); // bounds2
612 swf_GetU32(tag); //offset to endedges
615 DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
617 enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
618 DEBUG_ENUMERATE printf("-------\n");
619 while(--numshapes>=0) /* morph shapes define two shapes */
621 DEBUG_ENUMERATE printf("shape:%d\n", numshapes);
622 fillbits = swf_GetBits(tag, 4);
623 linebits = swf_GetBits(tag, 4);
624 DEBUG_ENUMERATE printf("%d %d\n", fillbits, linebits);
625 swf_ResetReadBits(tag);
628 flags = swf_GetBits(tag, 1);
629 if(!flags) { //style change
630 flags = swf_GetBits(tag, 5);
634 int n = swf_GetBits(tag, 5);
636 x = swf_GetBits(tag, n); //x
637 y = swf_GetBits(tag, n); //y
638 DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
640 if(flags&2) { //fill0
642 fill0 = swf_GetBits(tag, fillbits);
643 DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
645 if(flags&4) { //fill1
647 fill1 = swf_GetBits(tag, fillbits);
648 DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
650 if(flags&8) { //linestyle
652 line = swf_GetBits(tag, linebits);
653 DEBUG_ENUMERATE printf("linestyle %d\n",line);
656 DEBUG_ENUMERATE printf("more fillstyles\n");
657 enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
658 fillbits = swf_GetBits(tag, 4);
659 linebits = swf_GetBits(tag, 4);
662 flags = swf_GetBits(tag, 1);
663 if(flags) { //straight edge
664 int n = swf_GetBits(tag, 4) + 2;
665 if(swf_GetBits(tag, 1)) { //line flag
667 x = swf_GetSBits(tag, n); //delta x
668 y = swf_GetSBits(tag, n); //delta y
669 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
671 int v=swf_GetBits(tag, 1);
673 d = swf_GetSBits(tag, n); //vert/horz
674 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
676 } else { //curved edge
677 int n = swf_GetBits(tag, 4) + 2;
679 x1 = swf_GetSBits(tag, n);
680 y1 = swf_GetSBits(tag, n);
681 x2 = swf_GetSBits(tag, n);
682 y2 = swf_GetSBits(tag, n);
683 DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
695 void callbackCount(TAG * t,int pos, void*ptr)
698 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
701 void callbackFillin(TAG * t,int pos, void*ptr)
705 DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
708 int swf_GetNumUsedIDs(TAG * t)
711 enumerateUsedIDs(t, 0, callbackCount, &num);
715 void swf_GetUsedIDs(TAG * t, int * positions)
717 int * ptr = positions;
718 enumerateUsedIDs(t, 0, callbackFillin, &ptr);
721 void swf_Relocate (SWF*swf, char*bitmap)
725 memset(slaveids, -1, sizeof(slaveids));
733 if(swf_isDefiningTag(tag))
738 id = swf_GetDefineID(tag); //own id
740 if(!bitmap[id]) { //free
745 for (t=1;t<65536;t++)
755 slaveids[id] = newid;
757 swf_SetDefineID(tag, newid);
760 num = swf_GetNumUsedIDs(tag);
761 ptr = malloc(sizeof(int)*num);
762 swf_GetUsedIDs(tag, ptr);
765 int id = GET16(&tag->data[ptr[t]]);
767 fprintf(stderr, "swf_Relocate: Mapping id never encountered before: %d\n", id);
771 PUT16(&tag->data[ptr[t]], id);