5 Extension module for the rfxswf library.
6 Part of the swftools package.
8 Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
10 This file is distributed under the GPL, see file COPYING for details
14 #define SF_MOVETO 0x01
18 #define SF_NEWSTYLE 0x10
20 #define FILL_SOLID 0x00
21 #define FILL_LINEAR 0x10 // Gradient
22 #define FILL_RADIAL 0x12
23 #define FILL_TILED 0x40 // Bitmap
24 #define FILL_CLIPPED 0x41
26 void swf_ShapeFree(SHAPE * s)
28 { if (s->linestyle.data) free(s->linestyle.data);
29 s->linestyle.data = NULL;
31 if (s->fillstyle.data) free(s->fillstyle.data);
32 s->fillstyle.data = NULL;
34 if (s->data) free(s->data);
40 int swf_ShapeNew(SHAPE * * s)
43 sh = (SHAPE *)malloc(sizeof(SHAPE)); s[0] = sh;
44 if (sh) memset(sh,0x00,sizeof(SHAPE));
48 int swf_GetSimpleShape(TAG * t,SHAPE * * s) // without Linestyle/Fillstyle Record
54 if (FAILED(swf_ShapeNew(s))) return -1;
58 sh->bits.fill = (U16)swf_GetBits(t,4);
59 sh->bits.line = (U16)swf_GetBits(t,4);
60 bitl = 0; end = 0; pos = swf_GetTagPos(t);
63 { int edge = swf_GetBits(t,1); bitl+=1;
66 if (swf_GetBits(t,1)) // Line
67 { U16 nbits = swf_GetBits(t,4)+2;
70 if (swf_GetBits(t,1)) // x/y Line
71 { swf_GetBits(t,nbits);
76 { swf_GetBits(t,nbits+1);
81 { U16 nbits = swf_GetBits(t,4)+2;
93 { U16 flags = swf_GetBits(t,5); bitl+=5;
97 { U16 nbits = swf_GetBits(t,5); bitl+=5;
104 { swf_GetBits(t,sh->bits.fill);
109 { swf_GetBits(t,sh->bits.fill);
114 { swf_GetBits(t,sh->bits.line);
118 if (flags&SF_NEWSTYLE)
119 { fprintf(stderr,"RFXSWF: Can't process extended styles in shape.\n");
125 swf_SetTagPos(t,pos);
128 if (sh->data) free(sh->data);
129 sh->data = (U8*)malloc(len);
133 swf_GetBlock(t,sh->data,len);
140 int swf_SetSimpleShape(TAG * t,SHAPE * s) // without Linestyle/Fillstyle Record
147 { swf_ResetWriteBits(t);
149 swf_SetBits(t,s->bits.fill,4);
150 swf_SetBits(t,s->bits.line,4);
151 swf_SetBlock(t,s->data,l);
153 swf_ResetWriteBits(t);
158 int swf_SetFillStyle(TAG * t,FILLSTYLE * f)
159 { if ((!t)||(!f)) return -1;
160 swf_SetU8(t,f->type);
166 if (swf_GetTagID(t)!=ST_DEFINESHAPE3) swf_SetRGB(t,&f->color);
167 else swf_SetRGBA(t,&f->color);
172 swf_SetU16(t,f->id_bitmap);
173 swf_SetMatrix(t,&f->m);
180 int swf_SetLineStyle(TAG * t,LINESTYLE * l)
181 { if ((!l)||(!t)) return -1;
182 swf_SetU16(t,l->width);
184 if (swf_GetTagID(t)!=ST_DEFINESHAPE3) swf_SetRGB(t,&l->color);
185 else swf_SetRGBA(t,&l->color);
190 int swf_SetShapeStyleCount(TAG * t,U16 n)
202 int swf_SetShapeStyles(TAG * t,SHAPE * s)
207 l += swf_SetShapeStyleCount(t,s->fillstyle.n);
209 for (i=0;i<s->fillstyle.n;i++)
210 l+=swf_SetFillStyle(t,&s->fillstyle.data[i]);
212 l += swf_SetShapeStyleCount(t,s->linestyle.n);
214 for (i=0;i<s->linestyle.n;i++)
215 l+=swf_SetLineStyle(t,&s->linestyle.data[i]);
220 int swf_ShapeCountBits(SHAPE * s,U8 * fbits,U8 * lbits)
223 s->bits.fill = swf_CountBits(s->fillstyle.n,0);
224 s->bits.line = swf_CountBits(s->linestyle.n,0);
226 if (fbits) fbits[0] = s->bits.fill;
227 if (lbits) lbits[0] = s->bits.line;
232 int swf_SetShapeBits(TAG * t,SHAPE * s)
233 { if ((!t)||(!s)) return -1;
234 swf_ResetWriteBits(t);
235 swf_SetBits(t,s->bits.fill,4);
236 swf_SetBits(t,s->bits.line,4);
240 int swf_SetShapeHeader(TAG * t,SHAPE * s)
242 res = swf_SetShapeStyles(t,s);
243 if (res>=0) res = swf_ShapeCountBits(s,NULL,NULL);
244 if (res>=0) res = swf_SetShapeBits(t,s);
248 int swf_ShapeExport(int handle,SHAPE * s) // without Linestyle/Fillstyle Record
255 if (write(handle,s,sizeof(SHAPE))!=sizeof(SHAPE)) return -1;
257 // Fillstyle, Linestyle ...
260 { int ll = (s->bitlen+7)/8;
263 if (write(handle,s->data,ll)!=ll) return -1;
269 int swf_ShapeImport(int handle,SHAPE * * shape)
272 if (handle<0) return -1;
274 s = (SHAPE *)malloc(sizeof(SHAPE)); shape[0] = s;
277 if (read(handle,s,sizeof(SHAPE))!=sizeof(SHAPE))
284 { int ll = (s->bitlen+7)/8;
285 s->data = (U8*)malloc(ll);
291 if (read(handle,s->data,ll)!=ll)
302 int swf_ShapeAddFillStyle(SHAPE * s,U8 type,MATRIX * m,RGBA * color,U16 id_bitmap)
312 def_c.r = def_c.g = def_c.b = 0;
316 swf_GetMatrix(NULL,m);
321 if (s->fillstyle.data)
322 { FILLSTYLE * new = (FILLSTYLE *)realloc(s->fillstyle.data,(s->fillstyle.n+1)*sizeof(FILLSTYLE));
324 s->fillstyle.data = new;
327 { s->fillstyle.data = (FILLSTYLE *)malloc(sizeof(FILLSTYLE));
329 if (!s->fillstyle.data) return -1;
332 // set fillstyle (no gradients yet!)
334 s->fillstyle.data[s->fillstyle.n].type = type;
335 s->fillstyle.data[s->fillstyle.n].id_bitmap = id_bitmap;
336 memcpy(&s->fillstyle.data[s->fillstyle.n].m,m,sizeof(MATRIX));
337 memcpy(&s->fillstyle.data[s->fillstyle.n].color,color,sizeof(RGBA));
339 return (++s->fillstyle.n);
342 int swf_ShapeAddSolidFillStyle(SHAPE * s,RGBA * color)
343 { return swf_ShapeAddFillStyle(s,FILL_SOLID,NULL,color,0);
346 int swf_ShapeAddBitmapFillStyle(SHAPE * s,MATRIX * m,U16 id_bitmap,int clip)
347 { return swf_ShapeAddFillStyle(s,clip?FILL_CLIPPED:FILL_TILED,m,NULL,id_bitmap);
350 int swf_ShapeAddLineStyle(SHAPE * s,U16 width,RGBA * color)
356 def.r = def.g = def.b = 0;
358 if (s->linestyle.data)
359 { LINESTYLE * new = (LINESTYLE *)realloc(s->linestyle.data,(s->linestyle.n+1)*sizeof(LINESTYLE));
361 s->linestyle.data = new;
364 { s->linestyle.data = (LINESTYLE *)malloc(sizeof(LINESTYLE));
366 if (!s->linestyle.data) return -1;
369 s->linestyle.data[s->linestyle.n].width = width;
370 memcpy(&s->linestyle.data[s->linestyle.n].color,color,sizeof(RGBA));
372 return (++s->linestyle.n);
375 int swf_ShapeSetMove(TAG * t,SHAPE * s,S32 x,S32 y)
379 swf_SetBits(t,SF_MOVETO,5);
381 b = swf_CountBits(x,0);
382 b = swf_CountBits(y,b);
391 int swf_ShapeSetStyle(TAG * t,SHAPE * s,U16 line,U16 fill0,U16 fill1)
392 { if ((!t)||(!s)) return -1;
395 swf_SetBits(t,(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
397 if (fill0) swf_SetBits(t,fill0,s->bits.fill);
398 if (fill1) swf_SetBits(t,fill1,s->bits.fill);
399 if (line) swf_SetBits(t,line ,s->bits.line);
404 /* TODO: sometimes we want to set fillstyle 0, as that's the empty fill
405 used for line drawings. At the moment, we can't, as 0 fill be considered
406 nonexistent and therefore not set.
407 these defines are a workaround (they also reduce the maximal number of
408 fill styles to 32768)
410 #define FILL_RESET 0x8000
411 #define LINE_RESET 0x8000
413 int swf_ShapeSetAll(TAG * t,SHAPE * s,S32 x,S32 y,U16 line,U16 fill0,U16 fill1)
415 if ((!t)||(!s)) return -1;
418 swf_SetBits(t,SF_MOVETO|(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
420 b = swf_CountBits(x,0);
421 b = swf_CountBits(y,b);
426 if (fill0) swf_SetBits(t,fill0,s->bits.fill);
427 if (fill1) swf_SetBits(t,fill1,s->bits.fill);
428 if (line) swf_SetBits(t,line ,s->bits.line);
433 int swf_ShapeSetEnd(TAG * t)
436 swf_ResetWriteBits(t);
440 int swf_ShapeSetLine(TAG * t,SHAPE * s,S32 x,S32 y)
443 swf_SetBits(t,3,2); // Straight Edge
445 if ((!s)||((x!=0)&&(y!=0)))
446 { b = swf_CountBits(x,2);
447 b = swf_CountBits(y,b);
449 swf_SetBits(t, b-2, 4);
457 { b = swf_CountBits(y,2);
460 swf_SetBits(t, b-2, 4);
465 { b = swf_CountBits(x,2);
468 swf_SetBits(t, b-2, 4);
475 int swf_ShapeSetCurve(TAG * t,SHAPE * s,S32 x,S32 y,S32 ax,S32 ay)
481 b = swf_CountBits(ax,2);
482 b = swf_CountBits(ay,b);
483 b = swf_CountBits(x,b);
484 b = swf_CountBits(y,b);
486 swf_SetBits(t,b-2,4);
495 int swf_ShapeSetCircle(TAG * t,SHAPE * s,S32 x,S32 y,S32 rx,S32 ry)
496 { double C1 = 0.2930;
498 double begin = 0.7070;
502 swf_ShapeSetMove(t,s,x+begin*rx,y+begin*ry);
503 swf_ShapeSetCurve(t,s, -C1*rx, C1*ry, -C2*rx, 0);
504 swf_ShapeSetCurve(t,s, -C2*rx, 0, -C1*rx, -C1*ry);
505 swf_ShapeSetCurve(t,s, -C1*rx, -C1*ry, 0, -C2*ry);
506 swf_ShapeSetCurve(t,s, 0, -C2*ry, C1*rx, -C1*ry);
507 swf_ShapeSetCurve(t,s, C1*rx, -C1*ry, C2*rx, 0);
508 swf_ShapeSetCurve(t,s, C2*rx, 0, C1*rx, C1*ry);
509 swf_ShapeSetCurve(t,s, C1*rx, C1*ry, 0, C2*ry);
510 swf_ShapeSetCurve(t,s, 0, C2*ry, -C1*rx, C1*ry);
515 /* todo: merge this with swf_GetSimpleShape */
516 SHAPELINE* swf_ParseShapeData(U8*data, int bits, int fillbits, int linebits)
519 SHAPELINE*lines = &_lines;
528 memset(tag, 0, sizeof(TAG));
530 tag->len = tag->memsize = (bits+7)/8;
536 flags = swf_GetBits(tag, 1);
537 if(!flags) { //style change
538 flags = swf_GetBits(tag, 5);
542 fill0 = swf_GetBits(tag, fillbits);
544 fill1 = swf_GetBits(tag, fillbits);
546 line = swf_GetBits(tag, linebits);
549 //enumerateUsedIDs_styles(tag, callback, callback_data, num);
550 fillbits = swf_GetBits(tag, 4);
551 linebits = swf_GetBits(tag, 4);
554 int n = swf_GetBits(tag, 5);
555 x = swf_GetSBits(tag, n); //x
556 y = swf_GetSBits(tag, n); //y
558 lines->next = (SHAPELINE*)malloc(sizeof(SHAPELINE));
560 lines->type = moveTo;
563 lines->sx = lines->sy = 0;
564 lines->fillstyle0 = fill0;
565 lines->fillstyle1 = fill1;
566 lines->linestyle = line;
570 flags = swf_GetBits(tag, 1);
571 if(flags) { //straight edge
572 int n = swf_GetBits(tag, 4) + 2;
573 if(swf_GetBits(tag, 1)) { //line flag
574 x += swf_GetSBits(tag, n); //delta x
575 y += swf_GetSBits(tag, n); //delta y
577 int v=swf_GetBits(tag, 1);
579 d = swf_GetSBits(tag, n); //vert/horz
583 lines->next = (SHAPELINE*)malloc(sizeof(SHAPELINE));
585 lines->type = lineTo;
588 lines->sx = lines->sy = 0;
589 lines->fillstyle0 = fill0;
590 lines->fillstyle1 = fill1;
591 lines->linestyle = line;
593 } else { //curved edge
594 int n = swf_GetBits(tag, 4) + 2;
596 x += swf_GetSBits(tag, n);
597 y += swf_GetSBits(tag, n);
600 x += swf_GetSBits(tag, n);
601 y += swf_GetSBits(tag, n);
603 lines->next = (SHAPELINE*)malloc(sizeof(SHAPELINE));
605 lines->type = splineTo;
610 lines->fillstyle0 = fill0;
611 lines->fillstyle1 = fill1;
612 lines->linestyle = line;
620 SRECT swf_GetShapeBoundingBox(SHAPELINE*shape)
623 int SCOORD_MAX = 0x7fffffff;
624 int SCOORD_MIN = -0x80000000;
625 r.xmin = r.ymin = SCOORD_MAX;
626 r.xmax = r.ymax = SCOORD_MIN;
628 memset(&r, 0, sizeof(SRECT));
632 if(shape->x < r.xmin) r.xmin = shape->x;
633 if(shape->y < r.ymin) r.ymin = shape->y;
634 if(shape->x > r.xmax) r.xmax = shape->x;
635 if(shape->y > r.ymax) r.ymax = shape->y;
636 if(shape->type == splineTo) {
637 if(shape->sx < r.xmin) r.xmin = shape->sx;
638 if(shape->sy < r.ymin) r.ymin = shape->sy;
639 if(shape->sx > r.xmax) r.xmax = shape->sx;
640 if(shape->sy > r.ymax) r.ymax = shape->sy;
647 void swf_Shape2Free(SHAPE2 * s)
649 SHAPELINE*line = s->lines;
651 SHAPELINE*next = line->next;
663 SHAPE2* swf_ShapeToShape2(SHAPE*shape) {
665 SHAPE2*shape2 = (SHAPE2*)malloc(sizeof(SHAPE2));
667 shape2->numlinestyles = shape->linestyle.n;
668 shape2->linestyles = (LINESTYLE*)malloc(sizeof(LINESTYLE)*shape->linestyle.n);
669 memcpy(shape2->linestyles, shape->linestyle.data, sizeof(LINESTYLE)*shape->linestyle.n);
671 shape2->numfillstyles = shape->fillstyle.n;
672 shape2->fillstyles = (FILLSTYLE*)malloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
673 memcpy(shape2->fillstyles, shape->fillstyle.data, sizeof(FILLSTYLE)*shape->fillstyle.n);
675 shape2->lines = swf_ParseShapeData(shape->data, shape->bitlen, shape->bits.fill, shape->bits.line);
680 /*LINESTYLE* parseFillStyleArray(TAG*tag)
685 if(tag->id == ST_DEFINESHAPE)
687 else if(tag->id == ST_DEFINESHAPE2)
689 else if(tag->id == ST_DEFINESHAPE3)
693 count = swf_GetU8(tag);
694 if(count == 0xff && num>1) // defineshape2,3 only
695 count = swf_GetU16(tag);
701 swf_ResetReadBits(tag);
702 type = swf_GetU8(tag); //type
705 swf_GetRGBA(tag, NULL);
707 swf_GetRGB(tag, NULL);
709 else if(type == 0x10 || type == 0x12)
711 swf_ResetReadBits(tag);
712 swf_GetMatrix(tag, NULL);
713 swf_ResetReadBits(tag);
714 swf_GetGradient(tag, NULL, alpha num>=3?1:0);
716 else if(type == 0x40 || type == 0x41)
718 swf_ResetReadBits(tag);
720 if(tag->data[tag->pos] != 0xff ||
721 tag->data[tag->pos+1] != 0xff)
722 //(callback)(tag, tag->pos, callback_data);
725 swf_ResetReadBits(tag);
726 swf_GetMatrix(tag, NULL);
727 swf_GetMatrix(tag, NULL);
730 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
733 swf_ResetReadBits(tag);
734 count = swf_GetU8(tag); // line style array
736 count = swf_GetU16(tag);
742 swf_GetRGBA(tag, NULL);
744 swf_GetRGB(tag, NULL);
748 /*SHAPE2* parseDefineShape(TAG*tag)
753 if(tag->id == ST_DEFINESHAPE)
755 else if(tag->id == ST_DEFINESHAPE2)
757 else if(tag->id == ST_DEFINESHAPE3)
763 shape = (SHAPE2*)malloc(sizeof(SHAPE2));
765 // LINESTYLE * linestyles;
766 // int numlinestyles;
767 // FILLSTYLE* fillstyles;
768 // int numfillstyles;
769 // struct _SHAPELINE * lines;
770 // SRECT* bbox; // may be NULL
773 swf_ResetReadBits(t);
774 fill = (U16)swf_GetBits(t,4);
775 line = (U16)swf_GetBits(t,4);
788 SHAPE* swf_Shape2ToShape(SHAPE2*shape)
791 fprintf(stderr, "Not implemented yet!\n");
796 int swf_SetShape2(TAG*tag, SHAPE2*shape)
799 fprintf(stderr, "Not implemented yet!\n");