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