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