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 U16 swf_GetDefineID(TAG * t)
73 oldTagPos = swf_GetTagPos(t);
76 switch (swf_GetTagID(t))
77 { case ST_DEFINESHAPE:
80 case ST_DEFINEMORPHSHAPE:
81 case ST_DEFINEEDITTEXT:
83 case ST_DEFINEBITSJPEG2:
84 case ST_DEFINEBITSJPEG3:
85 case ST_DEFINEBITSLOSSLESS:
86 case ST_DEFINEBITSLOSSLESS2:
88 case ST_DEFINEBUTTON2:
89 case ST_DEFINEBUTTONCXFORM: //pseudodefine
90 case ST_DEFINEBUTTONSOUND: //pseudodefine
93 case ST_DEFINEFONTINFO: //pseudodefine
98 case ST_NAMECHARACTER: //pseudodefine
103 swf_SetTagPos(t,oldTagPos);
108 U16 swf_GetPlaceID(TAG * t)
113 oldTagPos = swf_GetTagPos(t);
116 switch (swf_GetTagID(t))
117 { case ST_PLACEOBJECT:
118 case ST_REMOVEOBJECT:
123 case ST_PLACEOBJECT2:
124 { U8 flags = swf_GetU8(t);
125 U16 d = swf_GetU16(t);
126 id = (flags&PF_CHAR)?swf_GetU16(t):id;
131 swf_SetTagPos(t,oldTagPos);
136 static int swf_definingtagids[] =
149 ST_DEFINEBITSLOSSLESS,
150 ST_DEFINEBITSLOSSLESS2,
159 // tags which may be used inside a sprite definition
160 static int swf_spritetagids[] =
165 ST_REMOVEOBJECT2, //?
176 static int swf_pseudodefiningtagids[] =
179 ST_DEFINEBUTTONCXFORM,
180 ST_DEFINEBUTTONSOUND,
185 U8 swf_isAllowedSpriteTag(TAG * tag)
189 while(swf_spritetagids[t]>=0)
191 if(swf_spritetagids[t] == id)
198 U8 swf_isDefiningTag(TAG * tag)
202 while(swf_definingtagids[t]>=0)
204 if(swf_definingtagids[t] == id)
211 U8 swf_isPseudoDefiningTag(TAG * tag)
215 while(swf_pseudodefiningtagids[t]>=0)
217 if(swf_pseudodefiningtagids[t] == id)
224 U16 swf_GetDepth(TAG * t)
229 oldTagPos = swf_GetTagPos(t);
232 switch (swf_GetTagID(t))
233 { case ST_PLACEOBJECT:
234 case ST_REMOVEOBJECT:
236 depth = swf_GetU16(t);
238 case ST_REMOVEOBJECT2:
239 depth = swf_GetU16(t);
241 case ST_PLACEOBJECT2:
242 { U8 flags = swf_GetU8(t);
243 depth = swf_GetU16(t);
246 swf_SetTagPos(t,oldTagPos);
250 char* swf_GetName(TAG * t)
256 oldTagPos = swf_GetTagPos(t);
258 switch(swf_GetTagID(t))
261 name = &t->data[swf_GetTagPos(t)];
263 case ST_PLACEOBJECT2: {
264 U8 flags = swf_GetU8(t);
265 swf_GetU16(t); //depth;
269 swf_GetMatrix(t, &m);
271 swf_GetCXForm(t, &c, 1);
275 swf_ResetReadBits(t);
276 name = &t->data[swf_GetTagPos(t)];
281 swf_SetTagPos(t,oldTagPos);
285 void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num)
289 count = swf_GetU8(tag);
290 if(count == 0xff && num>1) // defineshape2,3 only
291 count = swf_GetU16(tag);
297 swf_ResetReadBits(tag);
298 type = swf_GetU8(tag); //type
301 swf_GetRGBA(tag, NULL);
303 swf_GetRGB(tag, NULL);
305 else if(type == 0x10 || type == 0x12)
307 swf_ResetReadBits(tag);
308 swf_GetMatrix(tag, NULL);
309 swf_ResetReadBits(tag);
310 swf_GetGradient(tag, NULL, /*alpha*/ num>=3?1:0);
312 else if(type == 0x40 || type == 0x41)
314 swf_ResetReadBits(tag);
316 if(tag->data[tag->pos] != 0xff ||
317 tag->data[tag->pos+1] != 0xff)
318 (callback)(tag, tag->pos, callback_data);
321 swf_ResetReadBits(tag);
322 swf_GetMatrix(tag, NULL);
325 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
328 swf_ResetReadBits(tag);
329 count = swf_GetU8(tag); // line style array
331 count = swf_GetU16(tag);
336 swf_GetRGBA(tag, NULL);
338 swf_GetRGB(tag, NULL);
342 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
345 swf_ResetReadBits(tag);
349 case ST_DEFINEBUTTONCXFORM: {
351 callback(tag, tag->pos + base, callback_data);
354 callback(tag, tag->pos + base, callback_data);
355 swf_GetU16(tag); //sound id
356 flags = swf_GetU8(tag);
358 swf_GetU32(tag); // in point
360 swf_GetU32(tag); // out points
362 swf_GetU16(tag); // loop count
365 int npoints = swf_GetU8(tag);
367 for(s=0;s<npoints;s++)
376 case ST_DEFINEBUTTONSOUND:
377 callback(tag, tag->pos + base, callback_data); //button id
380 callback(tag, tag->pos + base, callback_data);
382 case ST_PLACEOBJECT2:
383 // only if placeflaghascharacter
384 if(!(tag->data[0]&2))
386 callback(tag, 3 + base, callback_data);
388 case ST_REMOVEOBJECT:
389 callback(tag, tag->pos + base, callback_data);
392 callback(tag, tag->pos + base, callback_data);
394 case ST_DEFINESPRITE: {
396 break; // sprite is expanded
398 swf_GetU16(tag); // id
399 swf_GetU16(tag); // framenum
402 U16 flags = swf_GetU16(tag);
405 TAG *tag2 = swf_InsertTag(NULL, id);
408 len = swf_GetU32(tag);
411 tag2->len = tag2->memsize = len;
412 tag2->data = malloc(len);
413 memcpy(tag2->data, &tag->data[tag->pos], len);
414 /* I never saw recursive sprites, but they are (theoretically)
415 possible, so better add base here again */
416 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
418 swf_GetBlock(tag, NULL, len);
422 case ST_DEFINEBUTTON2: // has some font ids in the button records
425 case ST_DEFINEBUTTON: {
426 swf_GetU16(tag); //button id
430 swf_GetU8(tag); //flag
431 offset = swf_GetU16(tag); //offset
436 if(!swf_GetU8(tag)) //flags
438 callback(tag, tag->pos + base, callback_data);
439 swf_GetU16(tag); //char
440 swf_GetU16(tag); //layer
441 swf_ResetReadBits(tag);
442 swf_GetMatrix(tag, NULL);
444 swf_ResetReadBits(tag);
445 swf_GetCXForm(tag, NULL, 1);
451 case ST_DEFINEEDITTEXT: {
453 swf_GetU16(tag); //id
454 swf_GetRect(tag, NULL); //bounding box
455 swf_ResetReadBits(tag);
456 flags1 = swf_GetU8(tag);
457 flags2 = swf_GetU8(tag);
459 callback(tag, tag->pos + base, callback_data);
464 case ST_DEFINETEXT: {
465 int glyphbits, advancebits;
467 id = swf_GetU16(tag); //id
468 swf_GetRect(tag, NULL); //bounding box
469 swf_ResetReadBits(tag);
470 swf_GetMatrix(tag, NULL); //matrix
471 swf_ResetReadBits(tag);
472 glyphbits = swf_GetU8(tag); //glyphbits
473 advancebits = swf_GetU8(tag); //advancebits
476 swf_ResetReadBits(tag);
477 flags = swf_GetBits(tag, 8);
479 if(flags & 128) // text style record
481 swf_ResetReadBits(tag);
482 if(flags & 8) { // hasfont
483 callback(tag, tag->pos + base, callback_data);
484 id = swf_GetU16(tag);
486 if(flags & 4) { // hascolor
487 if(num==1) swf_GetRGB(tag, NULL);
488 else swf_GetRGBA(tag, NULL);
490 if(flags & 2) { //has x offset
491 swf_ResetReadBits(tag);
494 if(flags & 1) { //has y offset
495 swf_ResetReadBits(tag);
498 if(flags & 8) { //has height
499 swf_ResetReadBits(tag);
502 } else { // glyph record
504 swf_ResetReadBits(tag);
505 for(t=0;t<flags;t++) {
506 swf_GetBits(tag, glyphbits);
507 swf_GetBits(tag, advancebits);
513 case ST_DEFINEFONTINFO:
514 callback(tag, tag->pos + base, callback_data);
517 case ST_DEFINESHAPE3: // these thingies might have bitmap ids in their fillstyles
519 case ST_DEFINESHAPE2:
521 case ST_DEFINESHAPE: {
525 id = swf_GetU16(tag); // id;
526 swf_GetRect(tag, NULL); // bounds
528 enumerateUsedIDs_styles(tag, callback, callback_data, num);
529 fillbits = swf_GetBits(tag, 4);
530 linebits = swf_GetBits(tag, 4);
531 swf_ResetReadBits(tag);
534 flags = swf_GetBits(tag, 1);
535 if(!flags) { //style change
536 flags = swf_GetBits(tag, 5);
540 int n = swf_GetBits(tag, 5);
541 swf_GetBits(tag, n); //x
542 swf_GetBits(tag, n); //y
544 if(flags&2) { //fill0
545 swf_GetBits(tag, fillbits);
547 if(flags&4) { //fill1
548 swf_GetBits(tag, fillbits);
550 if(flags&8) { //linestyle
551 swf_GetBits(tag, linebits);
554 enumerateUsedIDs_styles(tag, callback, callback_data, num);
555 fillbits = swf_GetBits(tag, 4);
556 linebits = swf_GetBits(tag, 4);
559 flags = swf_GetBits(tag, 1);
560 if(flags) { //straight edge
561 int n = swf_GetBits(tag, 4) + 2;
562 if(swf_GetBits(tag, 1)) { //line flag
563 swf_GetBits(tag, n); //delta x
564 swf_GetBits(tag, n); //delta y
566 int v=swf_GetBits(tag, 1);
567 swf_GetBits(tag, n); //vert/horz
569 } else { //curved edge
570 int n = swf_GetBits(tag, 4) + 2;
585 void callbackCount(TAG * t,int pos, void*ptr)
590 void callbackFillin(TAG * t,int pos, void*ptr)
596 int swf_GetNumUsedIDs(TAG * t)
599 enumerateUsedIDs(t, 0, callbackCount, &num);
603 void swf_GetUsedIDs(TAG * t, int * positions)
605 int * ptr = positions;
606 enumerateUsedIDs(t, 0, callbackFillin, &ptr);