b7152484c4461e5be9b0e7564f38be5439f6d2c3
[swftools.git] / lib / modules / swfshape.c
1 /* swfshape.c
2
3    shape functions
4       
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
9  
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
23
24 #include "../rfxswf.h"
25
26 #define SF_MOVETO       0x01
27 #define SF_FILL0        0x02
28 #define SF_FILL1        0x04
29 #define SF_LINE         0x08
30 #define SF_NEWSTYLE     0x10
31
32 void swf_ShapeFree(SHAPE * s)
33
34     if(!s)
35         return;
36     if (s->linestyle.data) rfx_free(s->linestyle.data);
37     s->linestyle.data = NULL;
38     s->linestyle.n    = 0;
39     if (s->fillstyle.data) rfx_free(s->fillstyle.data);
40     s->fillstyle.data = NULL;
41     s->fillstyle.n    = 0;
42     if (s->data) rfx_free(s->data);
43     s->data = NULL;
44     rfx_free(s);
45 }
46
47 int swf_ShapeNew(SHAPE * * s)
48
49     SHAPE * sh;
50     if (!s) return -1;
51     sh = (SHAPE *)rfx_calloc(sizeof(SHAPE)); 
52     *s = sh;
53     return 0;
54 }
55
56 int swf_GetSimpleShape(TAG * t,SHAPE * * s) // without Linestyle/Fillstyle Record
57 { SHAPE * sh;
58   int bitl, len;
59   int end;
60   U32 pos;
61   
62   if (FAILED(swf_ShapeNew(s))) return -1;
63   sh = s[0];
64
65   swf_ResetReadBits(t); 
66   sh->bits.fill = (U16)swf_GetBits(t,4);
67   sh->bits.line = (U16)swf_GetBits(t,4);
68   bitl = 0; end = 0; pos = swf_GetTagPos(t);
69
70   while (!end)
71   { int edge = swf_GetBits(t,1); bitl+=1;
72     if (edge)
73     { bitl+=1;
74       if (swf_GetBits(t,1))                 // Line
75       { U16 nbits = swf_GetBits(t,4)+2;
76         bitl+=5;
77
78         if (swf_GetBits(t,1))               // x/y Line
79         { swf_GetBits(t,nbits);
80           swf_GetBits(t,nbits);
81           bitl+=nbits*2;
82         }
83         else                            // hline/vline
84         { swf_GetBits(t,nbits+1);
85           bitl+=nbits+1;
86         }
87       }
88       else                              // Curve
89       { U16 nbits = swf_GetBits(t,4)+2;
90         bitl+=4;
91
92         swf_GetBits(t,nbits);
93         swf_GetBits(t,nbits);
94         swf_GetBits(t,nbits);
95         swf_GetBits(t,nbits);
96
97         bitl+=4*nbits;
98       }
99     }
100     else
101     { U16 flags = swf_GetBits(t,5); bitl+=5;
102       if (flags)
103       {
104         if (flags&SF_MOVETO)
105         { U16 nbits = swf_GetBits(t,5); bitl+=5;
106           swf_GetBits(t,nbits);
107           swf_GetBits(t,nbits);
108           bitl+=2*nbits;
109         }
110         
111         if (flags&SF_FILL0)
112         { swf_GetBits(t,sh->bits.fill);
113           bitl+=sh->bits.fill;
114         }
115         
116         if (flags&SF_FILL1)
117         { swf_GetBits(t,sh->bits.fill);
118           bitl+=sh->bits.fill;
119         }
120
121         if (flags&SF_LINE)
122         { swf_GetBits(t,sh->bits.line);
123           bitl+=sh->bits.line;
124         }
125
126         if (flags&SF_NEWSTYLE)
127         { fprintf(stderr,"RFXSWF: Can't process extended styles in shape.\n");
128         }
129       }
130       else end = 1;
131     }
132   }
133   swf_SetTagPos(t,pos);
134   len = (bitl+7)/8;
135   
136   if (sh->data) rfx_free(sh->data);
137   sh->data = (U8*)rfx_alloc(len);
138   
139   if (sh->data)
140   { sh->bitlen = bitl;
141     swf_GetBlock(t,sh->data,len);
142   }
143   else return -1;
144   
145   return len;
146 }
147
148 int swf_SetSimpleShape(TAG * t,SHAPE * s) // without Linestyle/Fillstyle Record
149 { int l;
150
151   if (!s) return -1;
152   l = (s->bitlen+7)/8;
153
154   if (t)
155   { swf_ResetWriteBits(t);
156
157     swf_SetBits(t,s->bits.fill,4);
158     swf_SetBits(t,s->bits.line,4);
159     swf_SetBlock(t,s->data,l);
160
161     swf_ResetWriteBits(t);
162   }
163   return l+1;
164 }
165
166 int swf_SetFillStyle(TAG * t,FILLSTYLE * f)
167 { if ((!t)||(!f)) return -1;
168   swf_SetU8(t,f->type);
169   
170   switch (f->type)
171   { case FILL_SOLID:
172       if (swf_GetTagID(t)!=ST_DEFINESHAPE3) swf_SetRGB(t,&f->color);
173       else swf_SetRGBA(t,&f->color);
174       break;
175
176     case FILL_TILED:
177     case FILL_CLIPPED:
178       swf_SetU16(t,f->id_bitmap);
179       swf_SetMatrix(t,&f->m);
180       break;
181     case FILL_LINEAR:
182     case FILL_RADIAL:
183       swf_SetMatrix(t,&f->m);
184       swf_SetGradient(t,&f->gradient,/*alpha?*/t->id==ST_DEFINESHAPE3?1:0);
185       break;
186   }
187   
188   return 0;
189 }
190
191 int swf_SetLineStyle(TAG * t,LINESTYLE * l)
192 { if ((!l)||(!t)) return -1;
193   swf_SetU16(t,l->width);
194
195   if (swf_GetTagID(t)!=ST_DEFINESHAPE3) swf_SetRGB(t,&l->color);
196   else swf_SetRGBA(t,&l->color);
197   
198   return 0;
199 }
200
201 int swf_SetShapeStyleCount(TAG * t,U16 n)
202 { if (n>254)
203   { swf_SetU8(t,0xff);
204     swf_SetU16(t,n);
205     return 3;
206   }
207   else
208   { swf_SetU8(t,(U8)n);
209     return 1;
210   }
211 }
212
213 int swf_SetShapeStyles(TAG * t,SHAPE * s)
214 { int i,l;
215   if (!s) return -1;
216
217   l = 0;
218   l += swf_SetShapeStyleCount(t,s->fillstyle.n);
219
220   for (i=0;i<s->fillstyle.n;i++)
221     l+=swf_SetFillStyle(t,&s->fillstyle.data[i]);
222
223   l += swf_SetShapeStyleCount(t,s->linestyle.n);
224
225   for (i=0;i<s->linestyle.n;i++)
226     l+=swf_SetLineStyle(t,&s->linestyle.data[i]);
227
228   return l;
229 }
230
231 int swf_ShapeCountBits(SHAPE * s,U8 * fbits,U8 * lbits)
232 { if (!s) return -1;
233   s->bits.fill = swf_CountUBits(s->fillstyle.n, 0);
234   s->bits.line = swf_CountUBits(s->linestyle.n, 0);
235   if (fbits) fbits[0] = s->bits.fill;
236   if (lbits) lbits[0] = s->bits.line;
237   return 0;    
238 }
239
240 int swf_SetShapeBits(TAG * t,SHAPE * s)
241 { if ((!t)||(!s)) return -1;
242   swf_ResetWriteBits(t);
243   swf_SetBits(t,s->bits.fill,4);
244   swf_SetBits(t,s->bits.line,4);
245   return 0;
246 }
247
248 int swf_SetShapeHeader(TAG * t,SHAPE * s)
249 { int res;
250   res = swf_SetShapeStyles(t,s);
251   if (res>=0) res = swf_ShapeCountBits(s,NULL,NULL);
252   if (res>=0) res = swf_SetShapeBits(t,s);
253   return res;
254 }
255
256 int swf_ShapeAddFillStyle(SHAPE * s,U8 type,MATRIX * m,RGBA * color,U16 id_bitmap, GRADIENT*gradient)
257 { RGBA def_c;
258   MATRIX def_m;    
259   GRADIENT def_g;
260
261   // handle defaults
262   
263   if (!s) return -1;
264   if (!color)
265   { color = &def_c;
266     def_c.a = 0xff;
267     def_c.r = def_c.g = def_c.b = 0;
268   }
269   if (!m)
270   { m = &def_m;
271     swf_GetMatrix(NULL,m);
272   }
273   if(!gradient)
274   {
275     gradient = &def_g;
276     swf_GetGradient(NULL, gradient, 1);
277   }
278
279   // handle memory
280   
281   if (s->fillstyle.data)
282   { FILLSTYLE * xnew = (FILLSTYLE *)rfx_realloc(s->fillstyle.data,(s->fillstyle.n+1)*sizeof(FILLSTYLE));
283     if (!xnew) return -1;
284     s->fillstyle.data = xnew;
285   }
286   else
287   { s->fillstyle.data = (FILLSTYLE *)rfx_alloc(sizeof(FILLSTYLE));
288     s->fillstyle.n = 0;
289     if (!s->fillstyle.data) return -1;
290   }
291
292   // set fillstyle
293   
294   s->fillstyle.data[s->fillstyle.n].type = type; 
295   s->fillstyle.data[s->fillstyle.n].id_bitmap = id_bitmap;
296   memcpy(&s->fillstyle.data[s->fillstyle.n].m,m,sizeof(MATRIX));
297   memcpy(&s->fillstyle.data[s->fillstyle.n].color,color,sizeof(RGBA));
298   memcpy(&s->fillstyle.data[s->fillstyle.n].gradient,gradient,sizeof(GRADIENT));
299           
300   return (++s->fillstyle.n);
301 }
302 int swf_ShapeAddFillStyle2(SHAPE * s,FILLSTYLE*fs)
303 {
304     return swf_ShapeAddFillStyle(s, fs->type, &fs->m, &fs->color, fs->id_bitmap, &fs->gradient);
305 }
306
307 int swf_ShapeAddSolidFillStyle(SHAPE * s,RGBA * color)
308 { return swf_ShapeAddFillStyle(s,FILL_SOLID,NULL,color,0,0);
309 }
310
311 int swf_ShapeAddBitmapFillStyle(SHAPE * s,MATRIX * m,U16 id_bitmap,int clip)
312 { return swf_ShapeAddFillStyle(s,clip?FILL_CLIPPED:FILL_TILED,m,NULL,id_bitmap,0);
313 }
314
315 int swf_ShapeAddGradientFillStyle(SHAPE * s,MATRIX * m,GRADIENT* gradient,int radial)
316 { return swf_ShapeAddFillStyle(s,radial?FILL_RADIAL:FILL_LINEAR,m,NULL,0,gradient);
317 }
318
319 int swf_ShapeAddLineStyle(SHAPE * s,U16 width,RGBA * color)
320 { RGBA def;
321   if (!s) return -1;
322   if (!color)
323   { color = &def;
324     def.a = 0xff;
325     def.r = def.g = def.b = 0; 
326   }
327   if (s->linestyle.data)
328   { LINESTYLE * xnew = (LINESTYLE *)rfx_realloc(s->linestyle.data,(s->linestyle.n+1)*sizeof(LINESTYLE));
329     if (!xnew) return -1;
330     s->linestyle.data = xnew;
331   }
332   else
333   { s->linestyle.data = (LINESTYLE *)rfx_alloc(sizeof(LINESTYLE));
334     s->linestyle.n = 0;
335     if (!s->linestyle.data) return -1;
336   }
337   
338   s->linestyle.data[s->linestyle.n].width = width;
339   memcpy(&s->linestyle.data[s->linestyle.n].color,color,sizeof(RGBA));
340
341   return (++s->linestyle.n);
342 }
343
344 int swf_ShapeSetMove(TAG * t,SHAPE * s,S32 x,S32 y)
345 { U8 b;
346   if (!t) return -1;
347   swf_SetBits(t,0,1);
348   swf_SetBits(t,SF_MOVETO,5);
349   
350   b = swf_CountBits(x,0);
351   b = swf_CountBits(y,b);
352   
353   swf_SetBits(t,b,5);
354   swf_SetBits(t,x,b);
355   swf_SetBits(t,y,b);
356
357   return 0;
358 }
359
360 int swf_ShapeSetStyle(TAG * t,SHAPE * s,int line,int fill0,int fill1)
361 { if ((!t)||(!s)) return -1;
362     
363   swf_SetBits(t,0,1);
364   swf_SetBits(t,(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
365
366   if (fill0) swf_SetBits(t,fill0,s->bits.fill);
367   if (fill1) swf_SetBits(t,fill1,s->bits.fill);
368   if (line)  swf_SetBits(t,line ,s->bits.line);
369   
370   return 0;
371 }
372
373 /* TODO: sometimes we want to set fillstyle 0, as that's the empty fill
374    used for line drawings. At the moment, we can't, as 0 fill be considered
375    nonexistent and therefore not set.
376    these defines are a workaround (they also reduce the maximal number of
377    fill styles to 32768)
378  */
379 #define UNDEFINED_COORD 0x7fffffff
380
381 int swf_ShapeSetAll(TAG * t,SHAPE * s,S32 x,S32 y,int line,int fill0,int fill1)
382 { U8 b;
383   U8 hasmove = 0;
384   if ((!t)||(!s)) return -1;
385
386   if(x!=UNDEFINED_COORD || y!=UNDEFINED_COORD)
387       hasmove=1;
388
389   swf_SetBits(t,0,1);
390   swf_SetBits(t,(hasmove?SF_MOVETO:0)|(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
391
392   if(hasmove) {
393     b = swf_CountBits(x,0);
394     b = swf_CountBits(y,b);
395     swf_SetBits(t,b,5);
396     swf_SetBits(t,x,b);
397     swf_SetBits(t,y,b);
398   }
399
400   if (fill0) swf_SetBits(t,fill0,s->bits.fill);
401   if (fill1) swf_SetBits(t,fill1,s->bits.fill);
402   if (line)  swf_SetBits(t,line ,s->bits.line);
403   
404   return 0;
405 }
406
407 int swf_ShapeSetEnd(TAG * t)
408 { if (!t) return -1;
409   swf_SetBits(t,0,6);
410   swf_ResetWriteBits(t);
411   return 0;
412 }
413
414 int swf_ShapeSetLine(TAG * t,SHAPE * s,S32 x,S32 y)
415
416     U8 b;
417     if (!t) return -1;
418    
419     b = swf_CountBits(x,2);
420     b = swf_CountBits(y,b);
421     if (b<2) b=2;
422     if(b >= 18) {
423         if(b >= 18 + 6) {
424             /* do not split into more than 64 segments. If the line is *that* long, something's broken */
425             fprintf(stderr, "Warning: Line to %.2f,%.2f is too long\n", (double)x,(double)y);
426             return -1;
427         } else {
428             /* split line */
429             int x1,y1,x2,y2;
430             if(x>=0) { x1 = x/2;x2 = (x+1)/2;} 
431             else     { x1 = x/2;x2 = (x-1)/2;}
432             if(y>=0) { y1 = y/2;y2 = (y+1)/2;} 
433             else     { y1 = y/2;y2 = (y-1)/2;}
434             swf_ShapeSetLine(t, s, x1,y1);
435             swf_ShapeSetLine(t, s, x2,y2);
436             return 0;
437         }
438     }
439
440     if(x!=0 && y!=0) { //(!s)||((x!=0)&&(y!=0)))
441         swf_SetBits(t,3,2); // Straight Edge
442         swf_SetBits(t, b-2, 4); //Number of Bits in x/y
443         swf_SetBits(t,1,1); // Diagonal
444         swf_SetBits(t,x,b);
445         swf_SetBits(t,y,b);
446     } else if (x==0) {
447         swf_SetBits(t,3,2); // Straight Edge
448         swf_SetBits(t, b-2, 4); //Number of Bits in y
449         swf_SetBits(t,1,2); // Vertical
450         swf_SetBits(t,y,b);
451     } else {
452         swf_SetBits(t,3,2); // Straight Edge
453         swf_SetBits(t, b-2, 4); //Number of Bits in x
454         swf_SetBits(t,0,2); // Horizontal
455         swf_SetBits(t,x,b);
456     }
457     return 0;
458 }
459
460 int swf_ShapeSetCurve(TAG * t,SHAPE * s,S32 x,S32 y,S32 ax,S32 ay)
461
462     U8 b;
463     if (!t) return -1;
464
465     b = swf_CountBits(ax,2);
466     b = swf_CountBits(ay,b);
467     b = swf_CountBits(x,b);
468     b = swf_CountBits(y,b);
469
470     if(b >= 18) {
471           fprintf(stderr, "Bit overflow in swf_ShapeSetCurve- %d (%d,%d,%d,%d)\n", b, ax,ay,x,y);
472           b = 17;
473     }
474
475     swf_SetBits(t,2,2);
476     swf_SetBits(t,b-2,4);
477     swf_SetBits(t,x,b);
478     swf_SetBits(t,y,b);
479     swf_SetBits(t,ax,b);
480     swf_SetBits(t,ay,b);
481     return 0;
482 }
483
484 int swf_ShapeSetCircle(TAG * t,SHAPE * s,S32 x,S32 y,S32 rx,S32 ry)
485 { double C1 = 0.2930;    
486   double C2 = 0.4140;   
487   double begin = 0.7070; 
488
489   if (!t) return -1;
490   
491   swf_ShapeSetMove(t,s,x+begin*rx,y+begin*ry);
492   swf_ShapeSetCurve(t,s, -C1*rx,  C1*ry, -C2*rx,      0);
493   swf_ShapeSetCurve(t,s, -C2*rx,      0, -C1*rx, -C1*ry);
494   swf_ShapeSetCurve(t,s, -C1*rx, -C1*ry,      0, -C2*ry);
495   swf_ShapeSetCurve(t,s,      0, -C2*ry,  C1*rx, -C1*ry);
496   swf_ShapeSetCurve(t,s,  C1*rx, -C1*ry,  C2*rx,      0);
497   swf_ShapeSetCurve(t,s,  C2*rx,      0,  C1*rx,  C1*ry);
498   swf_ShapeSetCurve(t,s,  C1*rx,  C1*ry,      0,  C2*ry);
499   swf_ShapeSetCurve(t,s,      0,  C2*ry, -C1*rx,  C1*ry);
500   
501   return 0;
502 }
503
504 void dummycallback1(TAG*tag, int x, void*y)
505 {
506 }
507
508 // from swftools.c:
509 void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph);
510
511 static void parseFillStyle(FILLSTYLE*dest, TAG*tag, int num)
512 {
513     int type = swf_GetU8(tag); //type
514     dest->type = type;
515     if(type == 0) {
516         /* plain color */
517         if(num >= 3)
518             swf_GetRGBA(tag, &dest->color);
519         else 
520             swf_GetRGB(tag, &dest->color);
521     }
522     else if(type == 0x10 || type == 0x11 || type == 0x12 || type == 0x13)
523     {
524         /* linear/radial gradient fill */
525         swf_ResetReadBits(tag);
526         swf_GetMatrix(tag, &dest->m);
527         swf_ResetReadBits(tag);
528         swf_GetGradient(tag, &dest->gradient, num>=3?1:0);
529         if(type == 0x13)
530             swf_GetU16(tag);
531     }
532     else if(type == 0x40 || type == 0x41 || type == 0x42 || type == 0x43)
533     {
534         /* bitmap fill */
535         swf_ResetReadBits(tag);
536         dest->id_bitmap = swf_GetU16(tag); //id
537         swf_ResetReadBits(tag); //?
538         swf_GetMatrix(tag, &dest->m);
539     }
540     else {
541         fprintf(stderr, "rfxswf:swfshape.c Unknown fillstyle:0x%02x in tag %02d\n",type, tag->id);
542     }
543 }
544 static int parseFillStyleArray(TAG*tag, SHAPE2*shape)
545 {
546     U16 count;
547     int t;
548     int num=0;
549     int fillstylestart = shape->numfillstyles;
550     int linestylestart = shape->numlinestyles;
551
552     if(tag->id == ST_DEFINESHAPE)
553         num = 1;
554     else if(tag->id == ST_DEFINESHAPE2)
555         num = 2;
556     else if(tag->id == ST_DEFINESHAPE3)
557         num = 3;
558     else if(tag->id == ST_DEFINESHAPE4)
559         num = 4;
560
561     count = swf_GetU8(tag);
562     if(count == 0xff && num>1) // defineshape2,3 only
563         count = swf_GetU16(tag);
564
565     shape->numfillstyles += count;
566     if(shape->numfillstyles) {
567         shape->fillstyles = (FILLSTYLE*)rfx_realloc(shape->fillstyles, sizeof(FILLSTYLE)*shape->numfillstyles);
568
569         for(t=fillstylestart;t<shape->numfillstyles;t++) {
570             parseFillStyle(&shape->fillstyles[t], tag, num);
571         }
572     }
573
574     swf_ResetReadBits(tag);
575     count = swf_GetU8(tag); // line style array
576     if(count == 0xff)
577         count = swf_GetU16(tag);
578
579     shape->numlinestyles += count;
580     if(count) {
581         shape->linestyles = (LINESTYLE*)rfx_realloc(shape->linestyles, sizeof(LINESTYLE)*shape->numlinestyles);
582         /* TODO: should we start with 1 and insert a correct definition of the
583            "built in" linestyle 0? */
584         for(t=linestylestart;t<shape->numlinestyles;t++) 
585         {
586             char fill = 0;
587             shape->linestyles[t].width = swf_GetU16(tag);
588
589             if(num >= 4) {
590                 U16 flags = swf_GetU16(tag);
591                 if((flags & 0x30) == 0x20)
592                     swf_GetU16(tag); // miter limit
593                 if(flags & 0x08) {
594                     fprintf(stderr, "Warning: Filled strokes parsing not yet fully supported\n");
595                     fill = 1;
596                 }
597             }
598
599             if(fill) {
600                 FILLSTYLE f;
601                 parseFillStyle(&f, tag, num);
602                 shape->linestyles[t].color = f.color;
603             } else {
604                 if(num >= 3)
605                     swf_GetRGBA(tag, &shape->linestyles[t].color);
606                 else
607                     swf_GetRGB(tag, &shape->linestyles[t].color);
608             }
609         }
610     }
611     return 1;
612 }
613
614
615 /* todo: merge this with swf_GetSimpleShape */
616 static SHAPELINE* swf_ParseShapeData(U8*data, int bits, int fillbits, int linebits, int version, SHAPE2*shape2)
617 {
618     SHAPELINE _lines;
619     SHAPELINE*lines = &_lines;
620
621     TAG _tag;
622     TAG* tag = &_tag;
623     int fill0 = 0;
624     int fill1 = 0;
625     int line = 0;
626     int x=0,y=0;
627     int linestyleadd=0;
628     int fillstyleadd=0;
629     
630     memset(tag, 0, sizeof(TAG));
631     tag->data = data;
632     tag->len = tag->memsize = (bits+7)/8;
633     tag->pos = 0;
634     tag->id = version==1?ST_DEFINESHAPE:(version==2?ST_DEFINESHAPE2:(version==3?ST_DEFINESHAPE3:ST_DEFINESHAPE4));
635
636     lines->next = 0;
637     while(1) {
638         int flags;
639         flags = swf_GetBits(tag, 1);
640         if(!flags) { //style change
641             flags = swf_GetBits(tag, 5);
642             if(!flags)
643                 break;
644             if(flags&1) { //move
645                 int n = swf_GetBits(tag, 5); 
646                 x = swf_GetSBits(tag, n); //x
647                 y = swf_GetSBits(tag, n); //y
648             }
649             if(flags&2)
650                 fill0 = swf_GetBits(tag, fillbits) + fillstyleadd; 
651             if(flags&4)
652                 fill1 = swf_GetBits(tag, fillbits) + fillstyleadd; 
653             if(flags&8)
654                 line = swf_GetBits(tag, linebits) + linestyleadd; 
655             if(flags&16) {
656                 if(!shape2) {
657                     fprintf(stderr, "rfxswf: Error: Additional fillstyles not supported\n");fflush(stderr);
658                     enumerateUsedIDs_styles(tag, dummycallback1, 0, version, 0);
659                 } else {
660                     linestyleadd = shape2->numlinestyles;
661                     fillstyleadd = shape2->numfillstyles;
662                     if(!parseFillStyleArray(tag, shape2))
663                         return 0;
664                 }
665                 fillbits = swf_GetBits(tag, 4);
666                 linebits = swf_GetBits(tag, 4);
667             }
668             if(flags&1) { //move
669                 lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
670                 lines = lines->next;
671                 lines->type = moveTo;
672                 lines->x = x; 
673                 lines->y = y; 
674                 lines->sx = lines->sy = 0;
675                 lines->fillstyle0 = fill0;
676                 lines->fillstyle1 = fill1;
677                 lines->linestyle = line;
678                 lines->next = 0;
679             }
680         } else {
681             flags = swf_GetBits(tag, 1);
682             if(flags) { //straight edge
683                 int n = swf_GetBits(tag, 4) + 2;
684                 if(swf_GetBits(tag, 1)) { //line flag
685                     x += swf_GetSBits(tag, n); //delta x
686                     y += swf_GetSBits(tag, n); //delta y
687                 } else {
688                     int v=swf_GetBits(tag, 1);
689                     int d;
690                     d = swf_GetSBits(tag, n); //vert/horz
691                     if(v) y += d;
692                     else  x += d;
693                 }
694                 lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
695                 lines = lines->next;
696                 lines->type = lineTo;
697                 lines->x = x; 
698                 lines->y = y; 
699                 lines->sx = lines->sy = 0;
700                 lines->fillstyle0 = fill0;
701                 lines->fillstyle1 = fill1;
702                 lines->linestyle = line;
703                 lines->next = 0;
704             } else { //curved edge
705                 int n = swf_GetBits(tag, 4) + 2;
706                 int x1,y1;
707                 x += swf_GetSBits(tag, n);
708                 y += swf_GetSBits(tag, n);
709                 x1 = x;
710                 y1 = y;
711                 x += swf_GetSBits(tag, n);
712                 y += swf_GetSBits(tag, n);
713
714                 lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
715                 lines = lines->next;
716                 lines->type = splineTo;
717                 lines->sx = x1; 
718                 lines->sy = y1; 
719                 lines->x = x; 
720                 lines->y = y; 
721                 lines->fillstyle0 = fill0;
722                 lines->fillstyle1 = fill1;
723                 lines->linestyle = line;
724                 lines->next = 0;
725             }
726         }
727     }
728     return _lines.next;
729 }
730
731 SRECT swf_GetShapeBoundingBox(SHAPE2*shape2)
732 {
733     SRECT r;
734     SHAPELINE*l = shape2->lines;
735     int lastx=0,lasty=0;
736     int valid = 0;
737     r.xmin = r.ymin = SCOORD_MAX;
738     r.xmax = r.ymax = SCOORD_MIN;
739
740     while(l) {
741         int t1;
742         if(l->linestyle>0) {
743             t1 = shape2->linestyles[l->linestyle - 1].width*3/2;
744         } else {
745             t1 = 0;
746         }
747
748         if(l->type == lineTo || l->type == splineTo)
749         {
750             valid = 1;
751             if(lastx - t1 < r.xmin) r.xmin = lastx - t1;
752             if(lasty - t1 < r.ymin) r.ymin = lasty - t1;
753             if(lastx + t1 > r.xmax) r.xmax = lastx + t1;
754             if(lasty + t1 > r.ymax) r.ymax = lasty + t1;
755             if(l->x - t1 < r.xmin) r.xmin = l->x - t1;
756             if(l->y - t1 < r.ymin) r.ymin = l->y - t1;
757             if(l->x + t1 > r.xmax) r.xmax = l->x + t1;
758             if(l->y + t1 > r.ymax) r.ymax = l->y + t1;
759             if(l->type == splineTo) {
760                 if(l->sx - t1 < r.xmin) r.xmin = l->sx - t1;
761                 if(l->sy - t1 < r.ymin) r.ymin = l->sy - t1;
762                 if(l->sx + t1 > r.xmax) r.xmax = l->sx + t1;
763                 if(l->sy + t1 > r.ymax) r.ymax = l->sy + t1;
764             }
765         }
766         lastx = l->x;
767         lasty = l->y;
768         l = l->next;
769     }
770     if(!valid) memset(&r, 0, sizeof(SRECT));
771     return r;
772 }
773
774 void swf_Shape2Free(SHAPE2 * s)
775 {
776     SHAPELINE*line = s->lines;
777     s->lines = 0;
778     while(line) {
779         SHAPELINE*next = line->next;
780         line->next = 0;
781         rfx_free(line);
782         line = next;
783     }
784
785     if(s->linestyles) {
786         rfx_free(s->linestyles);
787         s->linestyles = 0;
788     }
789     if(s->fillstyles) {
790         rfx_free(s->fillstyles);
791         s->fillstyles = 0;
792     }
793     if(s->bbox) {
794         rfx_free(s->bbox);
795         s->bbox = 0;
796     }
797 }
798
799 SHAPE2* swf_Shape2Clone(SHAPE2 * s)
800 {
801     SHAPELINE*line = s->lines;
802     SHAPELINE*prev = 0;
803     SHAPE2*s2 = (SHAPE2*)rfx_alloc(sizeof(SHAPE2));
804     memcpy(s2,s,sizeof(SHAPE2));
805     s2->linestyles = (LINESTYLE*)rfx_alloc(sizeof(LINESTYLE)*s->numlinestyles);
806     memcpy(s2->linestyles, s->linestyles, sizeof(LINESTYLE)*s->numlinestyles);
807     s2->fillstyles = (FILLSTYLE*)rfx_alloc(sizeof(FILLSTYLE)*s->numfillstyles);
808     memcpy(s2->fillstyles, s->fillstyles, sizeof(FILLSTYLE)*s->numfillstyles);
809
810     while(line) {
811         SHAPELINE*line2 = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
812         memcpy(line2, line, sizeof(SHAPELINE));
813         line2->next = 0;
814         if(prev)
815             prev->next = line2;
816         else
817             s2->lines = line2;
818         prev = line2;
819         line = line->next;
820     }
821     if(s->bbox) {
822         s2->bbox = (SRECT*)rfx_alloc(sizeof(SRECT));
823         memcpy(s2->bbox, s->bbox, sizeof(SRECT));
824     }
825     return s2;
826 }
827
828 SHAPE2* swf_ShapeToShape2(SHAPE*shape) {
829
830     SHAPE2*shape2 = (SHAPE2*)rfx_calloc(sizeof(SHAPE2));
831     
832     shape2->numlinestyles = shape->linestyle.n;
833     if(shape2->numlinestyles) {
834         shape2->linestyles = (LINESTYLE*)rfx_alloc(sizeof(LINESTYLE)*shape->linestyle.n);
835         memcpy(shape2->linestyles, shape->linestyle.data, sizeof(LINESTYLE)*shape->linestyle.n);
836     }
837     
838     shape2->numfillstyles = shape->fillstyle.n;
839     if(shape2->numfillstyles) {
840         shape2->fillstyles = (FILLSTYLE*)rfx_alloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
841         memcpy(shape2->fillstyles, shape->fillstyle.data, sizeof(FILLSTYLE)*shape->fillstyle.n);
842     }
843
844     shape2->lines = swf_ParseShapeData(shape->data, shape->bitlen, shape->bits.fill, shape->bits.line, 1, 0);
845     shape2->bbox = 0;
846     return shape2;
847 };
848
849 void swf_ShapeSetBitmapRect(TAG*tag, U16 gfxid, int width, int height)
850 {
851     SHAPE*shape;
852     MATRIX m;
853     RGBA rgb;
854     SRECT r;
855     int lines = 0;
856     int ls=0,fs;
857     swf_ShapeNew(&shape);
858     rgb.b = rgb.g = rgb.r = 0xff;
859     if(lines)
860         ls = swf_ShapeAddLineStyle(shape,20,&rgb);  
861     swf_GetMatrix(NULL,&m);
862     m.sx = 20*65536;
863     m.sy = 20*65536;
864
865     fs = swf_ShapeAddBitmapFillStyle(shape,&m,gfxid,0);
866     r.xmin = 0;
867     r.ymin = 0;
868     r.xmax = width*20;
869     r.ymax = height*20;
870     swf_SetRect(tag,&r);
871
872     swf_SetShapeStyles(tag,shape);
873     swf_ShapeCountBits(shape,NULL,NULL);
874     swf_SetShapeBits(tag,shape);
875
876     swf_ShapeSetAll(tag,shape,0,0,lines?ls:0,fs,0);
877
878     swf_ShapeSetLine(tag,shape,width*20,0);
879     swf_ShapeSetLine(tag,shape,0,height*20);
880     swf_ShapeSetLine(tag,shape,-width*20,0);
881     swf_ShapeSetLine(tag,shape,0,-height*20);
882     swf_ShapeSetEnd(tag);
883     swf_ShapeFree(shape);
884 }
885
886 void swf_Shape2ToShape(SHAPE2*shape2, SHAPE*shape)
887 {
888     TAG*tag = swf_InsertTag(0,0);
889     SHAPELINE*l;
890     int newx=0,newy=0,lastx=0,lasty=0,oldls=0,oldfs0=0,oldfs1=0;
891
892     memset(shape, 0, sizeof(SHAPE));
893
894     shape->linestyle.n = shape2->numlinestyles;
895     shape->linestyle.data = (LINESTYLE*)rfx_alloc(sizeof(LINESTYLE)*shape->linestyle.n);
896     memcpy(shape->linestyle.data, shape2->linestyles, sizeof(LINESTYLE)*shape->linestyle.n);
897     
898     shape->fillstyle.n =  shape2->numfillstyles;
899     shape->fillstyle.data = (FILLSTYLE*)rfx_alloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
900     memcpy(shape->fillstyle.data, shape2->fillstyles, sizeof(FILLSTYLE)*shape->fillstyle.n);
901
902     swf_ShapeCountBits(shape,NULL,NULL);
903
904     l = shape2->lines;
905
906     while(l) {
907         int ls=0,fs0=0,fs1=0;
908
909         if(l->type != moveTo) {
910             if(oldls != l->linestyle) {oldls = ls = l->linestyle;if(!ls) ls=0x8000;}
911             if(oldfs0 != l->fillstyle0) {oldfs0 = fs0 = l->fillstyle0;if(!fs0) fs0=0x8000;}
912             if(oldfs1 != l->fillstyle1) {oldfs1 = fs1 = l->fillstyle1;if(!fs1) fs1=0x8000;}
913
914             if(ls || fs0 || fs1 || newx!=0x7fffffff || newy!=0x7fffffff) {
915                 swf_ShapeSetAll(tag,shape,newx,newy,ls,fs0,fs1);
916                 newx = 0x7fffffff;
917                 newy = 0x7fffffff;
918             }
919         }
920
921         if(l->type == lineTo) {
922             swf_ShapeSetLine(tag,shape,l->x-lastx,l->y-lasty);
923         } else if(l->type == splineTo) {
924             swf_ShapeSetCurve(tag,shape, l->sx-lastx,l->sy-lasty, l->x-l->sx,l->y-l->sy);
925         }
926         if(l->type == moveTo) {
927             newx = l->x;
928             newy = l->y;
929         }
930
931         lastx = l->x;
932         lasty = l->y;
933         l = l->next;
934     }
935     swf_ShapeSetEnd(tag);
936     shape->data = tag->data;
937     shape->bitlen = tag->len*8;
938     free(tag);
939 }
940
941 void swf_SetShape2(TAG*tag, SHAPE2*shape2)
942 {
943     SHAPE shape;
944     swf_Shape2ToShape(shape2, &shape);
945
946     swf_SetRect(tag,shape2->bbox);
947     swf_SetShapeStyles(tag, &shape);
948     //swf_ShapeCountBits(&shape,NULL,NULL); // done in swf_Shape2ToShape()
949     swf_SetShapeBits(tag,&shape);
950
951     swf_SetBlock(tag, shape.data, (shape.bitlen+7)/8);
952 }
953
954 void swf_ParseDefineShape(TAG*tag, SHAPE2*shape)
955 {
956     int num = 0, id;
957     U16 fill,line;
958     SHAPELINE*l;
959     if(tag->id == ST_DEFINESHAPE)
960         num = 1;
961     else if(tag->id == ST_DEFINESHAPE2)
962         num = 2;
963     else if(tag->id == ST_DEFINESHAPE3)
964         num = 3;
965     else if(tag->id == ST_DEFINESHAPE4)
966         num = 4;
967     else {
968         fprintf(stderr, "parseDefineShape must be called with a shape tag");
969     }
970     swf_SetTagPos(tag, 0);
971
972     id = swf_GetU16(tag); //id
973     memset(shape, 0, sizeof(SHAPE2));
974     shape->bbox = (SRECT*)rfx_alloc(sizeof(SRECT));
975     swf_GetRect(tag, shape->bbox);
976     if(num>=4) {
977         SRECT r2;
978         swf_ResetReadBits(tag);
979         swf_GetRect(tag, &r2); // edge bounds
980         U8 flags = swf_GetU8(tag); // flags, &1: contains scaling stroke, &2: contains non-scaling stroke
981     }
982
983     if(!parseFillStyleArray(tag, shape)) {
984         return;
985     }
986
987     swf_ResetReadBits(tag); 
988     fill = (U16)swf_GetBits(tag,4);
989     line = (U16)swf_GetBits(tag,4);
990     if(!fill && !line) {
991         fprintf(stderr, "fill/line bits are both zero\n");
992     }
993
994     shape->lines = swf_ParseShapeData(&tag->data[tag->pos], (tag->len - tag->pos)*8, fill, line, num, shape);
995
996     l = shape->lines;
997 }
998
999 static void free_lines(SHAPELINE* lines)
1000 {
1001     if (lines->next)
1002         free_lines(lines->next);
1003     free(lines);
1004 }
1005
1006 void swf_RecodeShapeData(U8*data, int bitlen, int in_bits_fill, int in_bits_line, 
1007                          U8**destdata, U32*destbitlen, int out_bits_fill, int out_bits_line)
1008 {
1009     SHAPE2 s2;
1010     SHAPE s;
1011     SHAPELINE*line;
1012     memset(&s2, 0, sizeof(s2));
1013     s2.lines = swf_ParseShapeData(data, bitlen, in_bits_fill, in_bits_line, 1, 0);
1014     s2.numfillstyles = out_bits_fill?1<<(out_bits_fill-1):0;
1015     s2.numlinestyles = out_bits_line?1<<(out_bits_line-1):0;
1016     s2.fillstyles = (FILLSTYLE*)rfx_calloc(sizeof(FILLSTYLE)*s2.numfillstyles);
1017     s2.linestyles = (LINESTYLE*)rfx_calloc(sizeof(LINESTYLE)*s2.numlinestyles);
1018
1019     line = s2.lines;
1020     while(line) {
1021         if(line->fillstyle0 > s2.numfillstyles) line->fillstyle0 = 0;
1022         if(line->fillstyle1 > s2.numfillstyles) line->fillstyle1 = 0;
1023         if(line->linestyle > s2.numlinestyles) line->linestyle = 0;
1024         line = line->next;
1025     }
1026
1027     swf_Shape2ToShape(&s2,&s);
1028
1029     free_lines(s2.lines);
1030     free(s2.fillstyles);
1031     free(s2.linestyles);
1032     free(s.fillstyle.data);
1033     free(s.linestyle.data);
1034     *destdata = s.data;
1035     *destbitlen = s.bitlen;
1036 }
1037