04cdf716d966f22be5ebc003c74826412d6e84b4
[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,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 * new = (FILLSTYLE *)rfx_realloc(s->fillstyle.data,(s->fillstyle.n+1)*sizeof(FILLSTYLE));
281     if (!new) return -1;
282     s->fillstyle.data = new;
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
301 int swf_ShapeAddSolidFillStyle(SHAPE * s,RGBA * color)
302 { return swf_ShapeAddFillStyle(s,FILL_SOLID,NULL,color,0,0);
303 }
304
305 int swf_ShapeAddBitmapFillStyle(SHAPE * s,MATRIX * m,U16 id_bitmap,int clip)
306 { return swf_ShapeAddFillStyle(s,clip?FILL_CLIPPED:FILL_TILED,m,NULL,id_bitmap,0);
307 }
308
309 int swf_ShapeAddGradientFillStyle(SHAPE * s,MATRIX * m,GRADIENT* gradient,int radial)
310 { return swf_ShapeAddFillStyle(s,radial?FILL_RADIAL:FILL_LINEAR,m,NULL,0,gradient);
311 }
312
313 int swf_ShapeAddLineStyle(SHAPE * s,U16 width,RGBA * color)
314 { RGBA def;
315   if (!s) return -1;
316   if (!color)
317   { color = &def;
318     def.a = 0xff;
319     def.r = def.g = def.b = 0; 
320   }
321   if (s->linestyle.data)
322   { LINESTYLE * new = (LINESTYLE *)rfx_realloc(s->linestyle.data,(s->linestyle.n+1)*sizeof(LINESTYLE));
323     if (!new) return -1;
324     s->linestyle.data = new;
325   }
326   else
327   { s->linestyle.data = (LINESTYLE *)rfx_alloc(sizeof(LINESTYLE));
328     s->linestyle.n = 0;
329     if (!s->linestyle.data) return -1;
330   }
331   
332   s->linestyle.data[s->linestyle.n].width = width;
333   memcpy(&s->linestyle.data[s->linestyle.n].color,color,sizeof(RGBA));
334
335   return (++s->linestyle.n);
336 }
337
338 int swf_ShapeSetMove(TAG * t,SHAPE * s,S32 x,S32 y)
339 { U8 b;
340   if (!t) return -1;
341   swf_SetBits(t,0,1);
342   swf_SetBits(t,SF_MOVETO,5);
343   
344   b = swf_CountBits(x,0);
345   b = swf_CountBits(y,b);
346   
347   swf_SetBits(t,b,5);
348   swf_SetBits(t,x,b);
349   swf_SetBits(t,y,b);
350
351   return 0;
352 }
353
354 int swf_ShapeSetStyle(TAG * t,SHAPE * s,int line,int fill0,int fill1)
355 { if ((!t)||(!s)) return -1;
356     
357   swf_SetBits(t,0,1);
358   swf_SetBits(t,(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
359
360   if (fill0) swf_SetBits(t,fill0,s->bits.fill);
361   if (fill1) swf_SetBits(t,fill1,s->bits.fill);
362   if (line)  swf_SetBits(t,line ,s->bits.line);
363   
364   return 0;
365 }
366
367 /* TODO: sometimes we want to set fillstyle 0, as that's the empty fill
368    used for line drawings. At the moment, we can't, as 0 fill be considered
369    nonexistent and therefore not set.
370    these defines are a workaround (they also reduce the maximal number of
371    fill styles to 32768)
372  */
373 #define UNDEFINED_COORD 0x7fffffff
374
375 int swf_ShapeSetAll(TAG * t,SHAPE * s,S32 x,S32 y,int line,int fill0,int fill1)
376 { U8 b;
377   U8 hasmove = 0;
378   if ((!t)||(!s)) return -1;
379
380   if(x!=UNDEFINED_COORD || y!=UNDEFINED_COORD)
381       hasmove=1;
382
383   swf_SetBits(t,0,1);
384   swf_SetBits(t,(hasmove?SF_MOVETO:0)|(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
385
386   if(hasmove) {
387     b = swf_CountBits(x,0);
388     b = swf_CountBits(y,b);
389     swf_SetBits(t,b,5);
390     swf_SetBits(t,x,b);
391     swf_SetBits(t,y,b);
392   }
393
394   if (fill0) swf_SetBits(t,fill0,s->bits.fill);
395   if (fill1) swf_SetBits(t,fill1,s->bits.fill);
396   if (line)  swf_SetBits(t,line ,s->bits.line);
397   
398   return 0;
399 }
400
401 int swf_ShapeSetEnd(TAG * t)
402 { if (!t) return -1;
403   swf_SetBits(t,0,6);
404   swf_ResetWriteBits(t);
405   return 0;
406 }
407
408 int swf_ShapeSetLine(TAG * t,SHAPE * s,S32 x,S32 y)
409
410     U8 b;
411     if (!t) return -1;
412    
413     b = swf_CountBits(x,2);
414     b = swf_CountBits(y,b);
415     if (b<2) b=2;
416     if(b >= 18) {
417         if(b >= 18 + 4) {
418             /* do not split into more than 16 segments. If the line is *that* long, something's broken */
419             fprintf(stderr, "Warning: Line to %.2f,%.2f is too long\n", (double)x,(double)y);
420             return -1;
421         } else {
422             /* split line */
423             int x1,y1,x2,y2;
424             if(x>=0) { x1 = x/2;x2 = (x+1)/2;} 
425             else     { x1 = x/2;x2 = (x-1)/2;}
426             if(y>=0) { y1 = y/2;y2 = (y+1)/2;} 
427             else     { y1 = y/2;y2 = (y-1)/2;}
428             swf_ShapeSetLine(t, s, x1,y1);
429             swf_ShapeSetLine(t, s, x2,y2);
430             return 0;
431         }
432     }
433
434     if(x!=0 && y!=0) { //(!s)||((x!=0)&&(y!=0)))
435         swf_SetBits(t,3,2); // Straight Edge
436         swf_SetBits(t, b-2, 4); //Number of Bits in x/y
437         swf_SetBits(t,1,1); // Diagonal
438         swf_SetBits(t,x,b);
439         swf_SetBits(t,y,b);
440     } else if (x==0) {
441         swf_SetBits(t,3,2); // Straight Edge
442         swf_SetBits(t, b-2, 4); //Number of Bits in y
443         swf_SetBits(t,1,2); // Vertical
444         swf_SetBits(t,y,b);
445     } else {
446         swf_SetBits(t,3,2); // Straight Edge
447         swf_SetBits(t, b-2, 4); //Number of Bits in x
448         swf_SetBits(t,0,2); // Horizontal
449         swf_SetBits(t,x,b);
450     }
451     return 0;
452 }
453
454 int swf_ShapeSetCurve(TAG * t,SHAPE * s,S32 x,S32 y,S32 ax,S32 ay)
455
456     U8 b;
457     if (!t) return -1;
458
459     b = swf_CountBits(ax,2);
460     b = swf_CountBits(ay,b);
461     b = swf_CountBits(x,b);
462     b = swf_CountBits(y,b);
463
464     if(b >= 18) {
465           fprintf(stderr, "Bit overflow in swf_ShapeSetCurve- %d (%d,%d,%d,%d)\n", b, ax,ay,x,y);
466           b = 17;
467     }
468
469     swf_SetBits(t,2,2);
470     swf_SetBits(t,b-2,4);
471     swf_SetBits(t,x,b);
472     swf_SetBits(t,y,b);
473     swf_SetBits(t,ax,b);
474     swf_SetBits(t,ay,b);
475     return 0;
476 }
477
478 int swf_ShapeSetCircle(TAG * t,SHAPE * s,S32 x,S32 y,S32 rx,S32 ry)
479 { double C1 = 0.2930;    
480   double C2 = 0.4140;   
481   double begin = 0.7070; 
482
483   if (!t) return -1;
484   
485   swf_ShapeSetMove(t,s,x+begin*rx,y+begin*ry);
486   swf_ShapeSetCurve(t,s, -C1*rx,  C1*ry, -C2*rx,      0);
487   swf_ShapeSetCurve(t,s, -C2*rx,      0, -C1*rx, -C1*ry);
488   swf_ShapeSetCurve(t,s, -C1*rx, -C1*ry,      0, -C2*ry);
489   swf_ShapeSetCurve(t,s,      0, -C2*ry,  C1*rx, -C1*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   
495   return 0;
496 }
497
498 /* todo: merge this with swf_GetSimpleShape */
499 SHAPELINE* swf_ParseShapeData(U8*data, int bits, int fillbits, int linebits)
500 {
501     SHAPELINE _lines;
502     SHAPELINE*lines = &_lines;
503
504     TAG _tag;
505     TAG* tag = &_tag;
506     int fill0 = 0;
507     int fill1 = 0;
508     int line = 0;
509     int x=0,y=0;
510     
511     memset(tag, 0, sizeof(TAG));
512     tag->data = data;
513     tag->len = tag->memsize = (bits+7)/8;
514     tag->pos = 0;
515
516     lines->next = 0;
517     while(1) {
518         int flags;
519         flags = swf_GetBits(tag, 1);
520         if(!flags) { //style change
521             flags = swf_GetBits(tag, 5);
522             if(!flags)
523                 break;
524             if(flags&1) { //move
525                 int n = swf_GetBits(tag, 5); 
526                 x = swf_GetSBits(tag, n); //x
527                 y = swf_GetSBits(tag, n); //y
528             }
529             if(flags&2)
530                 fill0 = swf_GetBits(tag, fillbits); 
531             if(flags&4)
532                 fill1 = swf_GetBits(tag, fillbits); 
533             if(flags&8)
534                 line = swf_GetBits(tag, linebits); 
535             if(flags&16) {
536                 fprintf(stderr, "Additional file styles style change not yet supported\n");
537                 exit(1);
538                 //enumerateUsedIDs_styles(tag, callback, callback_data, num);
539                 fillbits = swf_GetBits(tag, 4);
540                 linebits = swf_GetBits(tag, 4);
541             }
542             if(flags&1) { //move
543                 lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
544                 lines = lines->next;
545                 lines->type = moveTo;
546                 lines->x = x; 
547                 lines->y = y; 
548                 lines->sx = lines->sy = 0;
549                 lines->fillstyle0 = fill0;
550                 lines->fillstyle1 = fill1;
551                 lines->linestyle = line;
552                 lines->next = 0;
553             }
554         } else {
555             flags = swf_GetBits(tag, 1);
556             if(flags) { //straight edge
557                 int n = swf_GetBits(tag, 4) + 2;
558                 if(swf_GetBits(tag, 1)) { //line flag
559                     x += swf_GetSBits(tag, n); //delta x
560                     y += swf_GetSBits(tag, n); //delta y
561                 } else {
562                     int v=swf_GetBits(tag, 1);
563                     int d;
564                     d = swf_GetSBits(tag, n); //vert/horz
565                     if(v) y += d;
566                     else  x += d;
567                 }
568                 lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
569                 lines = lines->next;
570                 lines->type = lineTo;
571                 lines->x = x; 
572                 lines->y = y; 
573                 lines->sx = lines->sy = 0;
574                 lines->fillstyle0 = fill0;
575                 lines->fillstyle1 = fill1;
576                 lines->linestyle = line;
577                 lines->next = 0;
578             } else { //curved edge
579                 int n = swf_GetBits(tag, 4) + 2;
580                 int x1,y1;
581                 x += swf_GetSBits(tag, n);
582                 y += swf_GetSBits(tag, n);
583                 x1 = x;
584                 y1 = y;
585                 x += swf_GetSBits(tag, n);
586                 y += swf_GetSBits(tag, n);
587
588                 lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
589                 lines = lines->next;
590                 lines->type = splineTo;
591                 lines->sx = x1; 
592                 lines->sy = y1; 
593                 lines->x = x; 
594                 lines->y = y; 
595                 lines->fillstyle0 = fill0;
596                 lines->fillstyle1 = fill1;
597                 lines->linestyle = line;
598                 lines->next = 0;
599             }
600         }
601     }
602     return _lines.next;
603 }
604
605 SRECT swf_GetShapeBoundingBox(SHAPE2*shape2)
606 {
607     SRECT r;
608     SHAPELINE*l = shape2->lines;
609     int lastx=0,lasty=0;
610     int valid = 0;
611     r.xmin = r.ymin = SCOORD_MAX;
612     r.xmax = r.ymax = SCOORD_MIN;
613
614     while(l) {
615         int t1;
616         if(l->linestyle>0) {
617             t1 = shape2->linestyles[l->linestyle - 1].width*3/2;
618         } else {
619             t1 = 0;
620         }
621
622         if(l->type == lineTo || l->type == splineTo)
623         {
624             valid = 1;
625             if(lastx - t1 < r.xmin) r.xmin = lastx - t1;
626             if(lasty - t1 < r.ymin) r.ymin = lasty - t1;
627             if(lastx + t1 > r.xmax) r.xmax = lastx + t1;
628             if(lasty + t1 > r.ymax) r.ymax = lasty + t1;
629             if(l->x - t1 < r.xmin) r.xmin = l->x - t1;
630             if(l->y - t1 < r.ymin) r.ymin = l->y - t1;
631             if(l->x + t1 > r.xmax) r.xmax = l->x + t1;
632             if(l->y + t1 > r.ymax) r.ymax = l->y + t1;
633             if(l->type == splineTo) {
634                 if(l->sx - t1 < r.xmin) r.xmin = l->sx - t1;
635                 if(l->sy - t1 < r.ymin) r.ymin = l->sy - t1;
636                 if(l->sx + t1 > r.xmax) r.xmax = l->sx + t1;
637                 if(l->sy + t1 > r.ymax) r.ymax = l->sy + t1;
638             }
639         }
640         lastx = l->x;
641         lasty = l->y;
642         l = l->next;
643     }
644     if(!valid) memset(&r, 0, sizeof(SRECT));
645     return r;
646 }
647
648 void swf_Shape2Free(SHAPE2 * s)
649 {
650     SHAPELINE*line = s->lines;
651     while(line) {
652         SHAPELINE*next = line->next;
653         rfx_free(line);
654         line = next;
655     }
656     if(s->linestyles)
657         rfx_free(s->linestyles);
658     if(s->fillstyles)
659         rfx_free(s->fillstyles);
660     if(s->bbox)
661         rfx_free(s->bbox);
662 }
663
664 SHAPE2* swf_Shape2Clone(SHAPE2 * s)
665 {
666     SHAPELINE*line = s->lines;
667     SHAPELINE*prev = 0;
668     SHAPE2*s2 = rfx_alloc(sizeof(SHAPE2));
669     memcpy(s2,s,sizeof(SHAPE2));
670     s2->linestyles = rfx_alloc(sizeof(LINESTYLE)*s->numlinestyles);
671     memcpy(s2->linestyles, s->linestyles, sizeof(LINESTYLE)*s->numlinestyles);
672     s2->fillstyles = rfx_alloc(sizeof(FILLSTYLE)*s->numfillstyles);
673     memcpy(s2->fillstyles, s->fillstyles, sizeof(FILLSTYLE)*s->numfillstyles);
674
675     while(line) {
676         SHAPELINE*line2 = rfx_alloc(sizeof(SHAPELINE));
677         memcpy(line2, line, sizeof(SHAPELINE));
678         line2->next = 0;
679         if(prev)
680             prev->next = line2;
681         else
682             s2->lines = line2;
683         prev = line2;
684         line = line->next;
685     }
686     if(s->bbox) {
687         s2->bbox = rfx_alloc(sizeof(SRECT));
688         memcpy(s2->bbox, s->bbox, sizeof(SRECT));
689     }
690     return s2;
691 }
692
693 SHAPE2* swf_ShapeToShape2(SHAPE*shape) {
694
695     SHAPE2*shape2 = (SHAPE2*)rfx_calloc(sizeof(SHAPE2));
696     
697     shape2->numlinestyles = shape->linestyle.n;
698     if(shape2->numlinestyles) {
699         shape2->linestyles = (LINESTYLE*)rfx_alloc(sizeof(LINESTYLE)*shape->linestyle.n);
700         memcpy(shape2->linestyles, shape->linestyle.data, sizeof(LINESTYLE)*shape->linestyle.n);
701     }
702     
703     shape2->numfillstyles = shape->fillstyle.n;
704     if(shape2->numfillstyles) {
705         shape2->fillstyles = (FILLSTYLE*)rfx_alloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
706         memcpy(shape2->fillstyles, shape->fillstyle.data, sizeof(FILLSTYLE)*shape->fillstyle.n);
707     }
708
709     shape2->lines = swf_ParseShapeData(shape->data, shape->bitlen, shape->bits.fill, shape->bits.line);
710     shape2->bbox = 0;
711     return shape2;
712 };
713
714 void swf_ShapeSetBitmapRect(TAG*tag, U16 gfxid, int width, int height)
715 {
716     SHAPE*shape;
717     MATRIX m;
718     RGBA rgb;
719     SRECT r;
720     int lines = 0;
721     int ls=0,fs;
722     swf_ShapeNew(&shape);
723     rgb.b = rgb.g = rgb.r = 0xff;
724     if(lines)
725         ls = swf_ShapeAddLineStyle(shape,20,&rgb);  
726     swf_GetMatrix(NULL,&m);
727     m.sx = 20*65536;
728     m.sy = 20*65536;
729
730     fs = swf_ShapeAddBitmapFillStyle(shape,&m,gfxid,0);
731     r.xmin = 0;
732     r.ymin = 0;
733     r.xmax = width*20;
734     r.ymax = height*20;
735     swf_SetRect(tag,&r);
736
737     swf_SetShapeStyles(tag,shape);
738     swf_ShapeCountBits(shape,NULL,NULL);
739     swf_SetShapeBits(tag,shape);
740
741     swf_ShapeSetAll(tag,shape,0,0,lines?ls:0,fs,0);
742
743     swf_ShapeSetLine(tag,shape,width*20,0);
744     swf_ShapeSetLine(tag,shape,0,height*20);
745     swf_ShapeSetLine(tag,shape,-width*20,0);
746     swf_ShapeSetLine(tag,shape,0,-height*20);
747     swf_ShapeSetEnd(tag);
748     swf_ShapeFree(shape);
749 }
750
751 void swf_Shape2ToShape(SHAPE2*shape2, SHAPE*shape)
752 {
753     TAG*tag = swf_InsertTag(0,0);
754     SHAPELINE*l,*next;
755     int newx=0,newy=0,lastx=0,lasty=0,oldls=0,oldfs0=0,oldfs1=0;
756
757     memset(shape, 0, sizeof(SHAPE));
758
759     shape->linestyle.n = shape2->numlinestyles;
760     shape->linestyle.data = (LINESTYLE*)rfx_alloc(sizeof(LINESTYLE)*shape->linestyle.n);
761     memcpy(shape->linestyle.data, shape2->linestyles, sizeof(LINESTYLE)*shape->linestyle.n);
762     
763     shape->fillstyle.n =  shape2->numfillstyles;
764     shape->fillstyle.data = (FILLSTYLE*)rfx_alloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
765     memcpy(shape->fillstyle.data, shape2->fillstyles, sizeof(FILLSTYLE)*shape->fillstyle.n);
766
767     swf_ShapeCountBits(shape,NULL,NULL);
768
769     l = shape2->lines;
770
771     while(l) {
772         int ls=0,fs0=0,fs1=0;
773
774         if(l->type != moveTo) {
775             if(oldls != l->linestyle) {oldls = ls = l->linestyle;if(!ls) ls=0x8000;}
776             if(oldfs0 != l->fillstyle0) {oldfs0 = fs0 = l->fillstyle0;if(!fs0) fs0=0x8000;}
777             if(oldfs1 != l->fillstyle1) {oldfs1 = fs1 = l->fillstyle1;if(!fs1) fs1=0x8000;}
778
779             if(ls || fs0 || fs1 || newx!=0x7fffffff || newy!=0x7fffffff) {
780                 swf_ShapeSetAll(tag,shape,newx,newy,ls,fs0,fs1);
781                 newx = 0x7fffffff;
782                 newy = 0x7fffffff;
783             }
784         }
785
786         if(l->type == lineTo) {
787             swf_ShapeSetLine(tag,shape,l->x-lastx,l->y-lasty);
788         } else if(l->type == splineTo) {
789             swf_ShapeSetCurve(tag,shape, l->sx-lastx,l->sy-lasty, l->x-l->sx,l->y-l->sy);
790         }
791         if(l->type == moveTo) {
792             newx = l->x;
793             newy = l->y;
794         }
795
796         lastx = l->x;
797         lasty = l->y;
798         l = l->next;
799     }
800     swf_ShapeSetEnd(tag);
801     shape->data = tag->data;
802     shape->bitlen = tag->len*8;
803 }
804
805 void swf_SetShape2(TAG*tag, SHAPE2*shape2)
806 {
807     SHAPE shape;
808     swf_Shape2ToShape(shape2, &shape);
809
810     swf_SetRect(tag,shape2->bbox);
811     swf_SetShapeStyles(tag, &shape);
812     //swf_ShapeCountBits(&shape,NULL,NULL); // done in swf_Shape2ToShape()
813     swf_SetShapeBits(tag,&shape);
814
815     swf_SetBlock(tag, shape.data, (shape.bitlen+7)/8);
816 }
817
818 static void parseFillStyleArray(TAG*tag, SHAPE2*shape)
819 {
820     U16 count;
821     int t;
822     int num=0;
823     if(tag->id == ST_DEFINESHAPE)
824         num = 1;
825     else if(tag->id == ST_DEFINESHAPE2)
826         num = 2;
827     else if(tag->id == ST_DEFINESHAPE3)
828         num = 3;
829
830     count = swf_GetU8(tag);
831     if(count == 0xff && num>1) // defineshape2,3 only
832         count = swf_GetU16(tag);
833
834     shape->numfillstyles = count;
835
836     if(shape->numfillstyles) {
837         shape->fillstyles = rfx_alloc(sizeof(FILLSTYLE)*count);
838
839         for(t=0;t<count;t++)
840         {
841             int type;
842             U8*pos;
843             FILLSTYLE*dest = &shape->fillstyles[t];
844             type = swf_GetU8(tag); //type
845             shape->fillstyles[t].type = type;
846             if(type == 0) {
847                 /* plain color */
848                 if(num == 3)
849                     swf_GetRGBA(tag, &dest->color);
850                 else 
851                     swf_GetRGB(tag, &dest->color);
852             }
853             else if(type == 0x10 || type == 0x12)
854             {
855                 /* linear/radial gradient fill */
856                 swf_ResetReadBits(tag);
857                 swf_GetMatrix(tag, &dest->m);
858                 swf_ResetReadBits(tag);
859                 swf_GetGradient(tag, &dest->gradient, num>=3?1:0);
860             }
861             else if(type == 0x40 || type == 0x41)
862             {
863                 /* bitmap fill */
864                 swf_ResetReadBits(tag);
865                 dest->id_bitmap = swf_GetU16(tag); //id
866                 swf_ResetReadBits(tag); //?
867                 swf_GetMatrix(tag, &dest->m);
868             }
869             else {
870                 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
871             }
872         }
873     }
874
875     swf_ResetReadBits(tag);
876     count = swf_GetU8(tag); // line style array
877     if(count == 0xff)
878         count = swf_GetU16(tag);
879
880     //if(verbose) printf("lnum: %d\n", count);
881
882     shape->numlinestyles = count;
883     if(count) {
884         shape->linestyles = rfx_alloc(sizeof(LINESTYLE)*count);
885         /* TODO: should we start with 1 and insert a correct definition of the
886            "built in" linestyle 0? */
887         for(t=0;t<count;t++) 
888         {
889             shape->linestyles[t].width = swf_GetU16(tag);
890             if(num == 3)
891                 swf_GetRGBA(tag, &shape->linestyles[t].color);
892             else
893                 swf_GetRGB(tag, &shape->linestyles[t].color);
894         }
895     }
896     return;
897 }
898
899 void swf_ParseDefineShape(TAG*tag, SHAPE2*shape)
900 {
901     int num = 0, id;
902     U16 fill,line;
903     SRECT r;
904     SRECT r2;
905     SHAPELINE*l;
906     if(tag->id == ST_DEFINESHAPE)
907         num = 1;
908     else if(tag->id == ST_DEFINESHAPE2)
909         num = 2;
910     else if(tag->id == ST_DEFINESHAPE3)
911         num = 3;
912     else {
913         fprintf(stderr, "parseDefineShape must be called with a shape tag");
914     }
915     swf_SetTagPos(tag, 0);
916
917     id = swf_GetU16(tag); //id
918     memset(shape, 0, sizeof(SHAPE2));
919     shape->bbox = rfx_alloc(sizeof(SRECT));
920     swf_GetRect(tag, &r);
921
922     memcpy(shape->bbox, &r, sizeof(SRECT));
923     parseFillStyleArray(tag, shape);
924
925     swf_ResetReadBits(tag); 
926     fill = (U16)swf_GetBits(tag,4);
927     line = (U16)swf_GetBits(tag,4);
928
929     shape->lines = swf_ParseShapeData(&tag->data[tag->pos], (tag->len - tag->pos)*8, fill, line);
930
931     l = shape->lines;
932 }
933
934 void swf_RecodeShapeData(U8*data, int bitlen, int in_bits_fill, int in_bits_line, 
935                          U8**destdata, U32*destbitlen, int out_bits_fill, int out_bits_line)
936 {
937     SHAPE2 s2;
938     SHAPE s;
939     SHAPELINE*line;
940     memset(&s2, 0, sizeof(s2));
941     s2.lines = swf_ParseShapeData(data, bitlen, in_bits_fill, in_bits_line);
942     s2.numfillstyles = out_bits_fill?1<<(out_bits_fill-1):0;
943     s2.numlinestyles = out_bits_line?1<<(out_bits_line-1):0;
944     s2.fillstyles = rfx_calloc(sizeof(FILLSTYLE)*s2.numfillstyles);
945     s2.linestyles = rfx_calloc(sizeof(LINESTYLE)*s2.numlinestyles);
946
947     line = s2.lines;
948     while(line) {
949         if(line->fillstyle0 > s2.numfillstyles) line->fillstyle0 = 0;
950         if(line->fillstyle1 > s2.numfillstyles) line->fillstyle1 = 0;
951         if(line->linestyle > s2.numlinestyles) line->linestyle = 0;
952         line = line->next;
953     }
954
955     swf_Shape2ToShape(&s2,&s);
956
957     free(s2.fillstyles);
958     free(s2.linestyles);
959     free(s.fillstyle.data);
960     free(s.linestyle.data);
961     *destdata = s.data;
962     *destbitlen = s.bitlen;
963 }
964