fixed format warnings
[swftools.git] / lib / devices / render.c
1 /* render.c
2
3    Part of the swftools package.
4
5    Copyright (c) 2005/2006/2007 Matthias Kramm <kramm@quiss.org> 
6  
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <math.h>
24 #include <memory.h>
25 #include "../gfxdevice.h"
26 #include "../gfxtools.h"
27 #include "../mem.h"
28 #define PNG_INLINE_EXPORTS
29 #include "../types.h"
30 #include "../png.c"
31 #include "../log.h"
32 #include "render.h"
33
34 typedef gfxcolor_t RGBA;
35
36 typedef struct _renderpoint
37 {
38     float x;
39 } renderpoint_t;
40
41 typedef struct _renderline
42 {
43     renderpoint_t*points;
44     int size;
45     int num;
46 } renderline_t;
47
48 typedef struct _internal_result {
49     gfximage_t img;
50     struct _internal_result*next;
51     char palette;
52 } internal_result_t;
53
54 typedef struct _clipbuffer {
55     U32*data;
56     struct _clipbuffer*next;
57 } clipbuffer_t;
58
59 typedef struct _internal {
60     int width;
61     int height;
62     int width2;
63     int height2;
64     int bitwidth;
65     int multiply;
66     int antialize;
67     int zoom;
68     int ymin, ymax;
69     int fillwhite;
70
71     char palette;
72
73     RGBA* img;
74
75     clipbuffer_t*clipbuf;
76
77     renderline_t*lines;
78
79     internal_result_t*results;
80     internal_result_t*result_next;
81 } internal_t;
82
83 typedef enum {filltype_solid,filltype_clip,filltype_bitmap,filltype_gradient} filltype_t;
84
85 typedef struct _fillinfo {
86     filltype_t type;
87     gfxcolor_t*color;
88     gfximage_t*image;
89     gfxmatrix_t*matrix;
90     gfxcxform_t*cxform;
91     RGBA*gradient;
92     char linear_or_radial;
93 } fillinfo_t;
94
95
96 static inline void add_pixel(internal_t*i, float x, int y)
97 {
98     renderpoint_t p;
99
100     if(x >= i->width2 || y >= i->height2 || y<0) return;
101     p.x = x;
102     if(y<i->ymin) i->ymin = y;
103     if(y>i->ymax) i->ymax = y;
104
105     renderline_t*l = &i->lines[y];
106
107     if(l->num == l->size) {
108         l->size += 32;
109         l->points = (renderpoint_t*)rfx_realloc(l->points, l->size * sizeof(renderpoint_t));
110     }
111     l->points[l->num] = p;
112     l->num++;
113 }
114
115 /* set this to 0.777777 or something if the "both fillstyles set while not inside shape"
116    problem appears to often */
117 #define CUT 0.5
118
119 #define INT(x) ((int)((x)+16)-16)
120
121 static void add_line(gfxdevice_t*dev , double x1, double y1, double x2, double y2)
122 {
123     internal_t*i = (internal_t*)dev->internal;
124     double diffx, diffy;
125     double ny1, ny2, stepx;
126 /*    if(DEBUG&4) {
127         int l = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
128         printf(" l[%d - %.2f/%.2f -> %.2f/%.2f]\n", l, x1/20.0, y1/20.0, x2/20.0, y2/20.0);
129     }*/
130
131     if(y2 < y1) {
132         double x;
133         double y;
134         x = x1;x1 = x2;x2=x;
135         y = y1;y1 = y2;y2=y;
136     }
137     
138     diffx = x2 - x1;
139     diffy = y2 - y1;
140     
141     ny1 = INT(y1)+CUT;
142     ny2 = INT(y2)+CUT;
143
144     if(ny1 < y1) {
145         ny1 = INT(y1) + 1.0 + CUT;
146     }
147     if(ny2 >= y2) {
148         ny2 = INT(y2) - 1.0 + CUT;
149     }
150
151     if(ny1 > ny2)
152         return;
153
154     stepx = diffx/diffy;
155     x1 = x1 + (ny1-y1)*stepx;
156     x2 = x2 + (ny2-y2)*stepx;
157
158     {
159         int posy=INT(ny1);
160         int endy=INT(ny2);
161         double posx=0;
162         double startx = x1;
163
164         while(posy<=endy) {
165             float xx = (float)(startx + posx);
166             add_pixel(i, xx ,posy);
167             posx+=stepx;
168             posy++;
169         }
170     }
171 }
172 #define PI 3.14159265358979
173 static void add_solidline(gfxdevice_t*dev, double x1, double y1, double x2, double y2, double width)
174 {
175     /* TODO: handle cap styles */
176
177     internal_t*i = (internal_t*)dev->internal;
178
179     double dx = x2-x1;
180     double dy = y2-y1;
181     double sd;
182     double d;
183
184     int t;
185     int segments;
186     double lastx,lasty;
187     double vx,vy;
188     double xx,yy;
189   
190     /* Make sure the line is always at least one pixel wide */
191 #ifdef LINEMODE1
192     /* That's what Macromedia's Player does at least at zoom level >= 1.  */
193     width += 1.0;
194 #else
195     /* That's what Macromedia's Player seems to do at zoom level 0.  */
196     /* TODO: needs testing */
197
198     /* TODO: how does this interact with scaling? */
199     if(width * i->multiply < 1.0)
200         width = 1.0 / i->multiply;
201 #endif
202
203     sd = (double)dx*(double)dx+(double)dy*(double)dy;
204     d = sqrt(sd);
205
206     if(!dx && !dy) {
207         vx = 1;
208         vy = 0;
209     } else {
210         vx = ( dy/d);
211         vy = (-dx/d);
212     }
213
214     segments = width/2;
215     if(segments < 2)
216         segments = 2;
217
218     segments = 8;
219
220     vx=vx*width*0.5;
221     vy=vy*width*0.5;
222
223     xx = x2+vx;
224     yy = y2+vy;
225     add_line(dev, x1+vx, y1+vy, xx, yy);
226     lastx = xx;
227     lasty = yy;
228     for(t=1;t<segments;t++) {
229         double s = sin(t*PI/segments);
230         double c = cos(t*PI/segments);
231         xx = (x2 + vx*c - vy*s);
232         yy = (y2 + vx*s + vy*c);
233         add_line(dev, lastx, lasty, xx, yy);
234         lastx = xx;
235         lasty = yy;
236     }
237     
238     xx = (x2-vx);
239     yy = (y2-vy);
240     add_line(dev, lastx, lasty, xx, yy);
241     lastx = xx;
242     lasty = yy;
243     xx = (x1-vx);
244     yy = (y1-vy);
245     add_line(dev, lastx, lasty, xx, yy);
246     lastx = xx;
247     lasty = yy;
248     for(t=1;t<segments;t++) {
249         double s = sin(t*PI/segments);
250         double c = cos(t*PI/segments);
251         xx = (x1 - vx*c + vy*s);
252         yy = (y1 - vx*s - vy*c);
253         add_line(dev, lastx, lasty, xx, yy);
254         lastx = xx;
255         lasty = yy;
256     }
257     add_line(dev, lastx, lasty, (x1+vx), (y1+vy));
258 }
259
260 static int compare_renderpoints(const void * _a, const void * _b)
261 {
262     renderpoint_t*a = (renderpoint_t*)_a;
263     renderpoint_t*b = (renderpoint_t*)_b;
264     if(a->x < b->x) return -1;
265     if(a->x > b->x) return 1;
266     return 0;
267 }
268
269 static void fill_line_solid(RGBA*line, U32*z, int y, int x1, int x2, RGBA col)
270 {
271     int x = x1;
272
273     U32 bit = 1<<(x1&31);
274     int bitpos = (x1/32);
275
276     if(col.a!=255) {
277         int ainv = 255-col.a;
278         col.r = (col.r*col.a)>>8;
279         col.g = (col.g*col.a)>>8;
280         col.b = (col.b*col.a)>>8;
281         do {
282             if(z[bitpos]&bit) {
283                 line[x].r = ((line[x].r*ainv)>>8)+col.r;
284                 line[x].g = ((line[x].g*ainv)>>8)+col.g;
285                 line[x].b = ((line[x].b*ainv)>>8)+col.b;
286                 //line[x].a = 255;
287                 line[x].a = ((line[x].a*ainv)>>8)+col.a;
288             }
289             bit <<= 1;
290             if(!bit) {
291                 bit = 1;bitpos++;
292             }
293         } while(++x<x2);
294     } else {
295         do {
296             if(z[bitpos]&bit) {
297                 line[x] = col;
298             }
299             bit <<= 1;
300             if(!bit) {
301                 bit = 1;bitpos++;
302             }
303         } while(++x<x2);
304     }
305 }
306
307 static void fill_line_bitmap(RGBA*line, U32*z, int y, int x1, int x2, fillinfo_t*info)
308 {
309     int x = x1;
310
311     gfxmatrix_t*m = info->matrix;
312     gfximage_t*b = info->image;
313     
314     if(!b || !b->width || !b->height) {
315         gfxcolor_t red = {255,255,0,0};
316         fill_line_solid(line, z, y, x1, x2, red);
317         return;
318     }
319     
320     double det = m->m00*m->m11 - m->m01*m->m10;
321     if(fabs(det) < 0.0005) { 
322         /* x direction equals y direction- the image is invisible */
323         return;
324     }
325     det = 1.0/det;
326     double xx1 =  (  (-m->tx) * m->m11 - (y - m->ty) * m->m10) * det;
327     double yy1 =  (- (-m->tx) * m->m01 + (y - m->ty) * m->m00) * det;
328     double xinc1 = m->m11 * det;
329     double yinc1 = m->m01 * det;
330     
331     U32 bit = 1<<(x1&31);
332     int bitpos = (x1/32);
333
334     do {
335         if(z[bitpos]&bit) {
336             RGBA col;
337             int xx = (int)(xx1 + x * xinc1);
338             int yy = (int)(yy1 - x * yinc1);
339             int ainv;
340
341             if(info->linear_or_radial) {
342                 if(xx<0) xx=0;
343                 if(xx>=b->width) xx = b->width-1;
344                 if(yy<0) yy=0;
345                 if(yy>=b->height) yy = b->height-1;
346             } else {
347                 xx %= b->width;
348                 yy %= b->height;
349                 if(xx<0) xx += b->width;
350                 if(yy<0) yy += b->height;
351             }
352
353             col = b->data[yy*b->width+xx];
354             ainv = 255-col.a;
355
356             /* needs bitmap with premultiplied alpha */
357             line[x].r = ((line[x].r*ainv)>>8)+col.r;
358             line[x].g = ((line[x].g*ainv)>>8)+col.g;
359             line[x].b = ((line[x].b*ainv)>>8)+col.b;
360             line[x].a = 255;
361         }
362         bit <<= 1;
363         if(!bit) {
364             bit = 1;bitpos++;
365         }
366     } while(++x<x2);
367 }
368
369 static void fill_line_gradient(RGBA*line, U32*z, int y, int x1, int x2, fillinfo_t*info)
370 {
371     int x = x1;
372
373     gfxmatrix_t*m = info->matrix;
374     RGBA*g= info->gradient;
375     
376     double det = m->m00*m->m11 - m->m01*m->m10;
377     if(fabs(det) < 0.0005) { 
378         /* x direction equals y direction */
379         return;
380     }
381     
382     det = 1.0/det;
383     double xx1 =  (  (-m->tx) * m->m11 - (y - m->ty) * m->m10) * det;
384     double yy1 =  (- (-m->tx) * m->m01 + (y - m->ty) * m->m00) * det;
385     double xinc1 = m->m11 * det;
386     double yinc1 = m->m01 * det;
387     
388     U32 bit = 1<<(x1&31);
389     int bitpos = (x1/32);
390
391     do {
392         if(z[bitpos]&bit) {
393             RGBA col;
394             int ainv;
395
396             int pos = 0;
397             if(info->linear_or_radial) {
398                 double xx = xx1 + x * xinc1;
399                 double yy = yy1 + y * yinc1;
400                 double r = sqrt(xx*xx + yy*yy);
401                 if(r>1) r = 1;
402                 pos = (int)(r*255.999);
403             } else {
404                 double r = xx1 + x * xinc1;
405                 if(r>1) r = 1;
406                 if(r<-1) r = -1;
407                 pos = (int)((r+1)*127.999);
408             }
409             col = g[pos];
410             ainv = 255-col.a;
411
412             /* needs bitmap with premultiplied alpha */
413             line[x].r = ((line[x].r*ainv)/255)+col.r;
414             line[x].g = ((line[x].g*ainv)/255)+col.g;
415             line[x].b = ((line[x].b*ainv)/255)+col.b;
416             line[x].a = 255;
417         }
418         bit <<= 1;
419         if(!bit) {
420             bit = 1;bitpos++;
421         }
422     } while(++x<x2);
423 }
424
425 static void fill_line_clip(RGBA*line, U32*z, int y, int x1, int x2)
426 {
427     int x = x1;
428
429     U32 bit = 1<<(x1&31);
430     int bitpos = (x1/32);
431
432     do {
433         z[bitpos]|=bit;
434         bit <<= 1;
435         if(!bit) {
436             bit = 1;bitpos++;
437         }
438     } while(++x<x2);
439 }
440
441 void fill_line(gfxdevice_t*dev, RGBA*line, U32*zline, int y, int startx, int endx, fillinfo_t*fill)
442 {
443     if(fill->type == filltype_solid)
444         fill_line_solid(line, zline, y, startx, endx, *fill->color);
445     else if(fill->type == filltype_clip)
446         fill_line_clip(line, zline, y, startx, endx);
447     else if(fill->type == filltype_bitmap)
448         fill_line_bitmap(line, zline, y, startx, endx, fill);
449     else if(fill->type == filltype_gradient)
450         fill_line_gradient(line, zline, y, startx, endx, fill);
451 }
452
453 void fill(gfxdevice_t*dev, fillinfo_t*fill)
454 {
455     internal_t*i = (internal_t*)dev->internal;
456     int y;
457     U32 clipdepth = 0;
458     for(y=i->ymin;y<=i->ymax;y++) {
459         renderpoint_t*points = i->lines[y].points;
460         RGBA*line = &i->img[i->width2*y];
461         U32*zline = &i->clipbuf->data[i->bitwidth*y];
462
463         int n;
464         int num = i->lines[y].num;
465         int lastx;
466         qsort(points, num, sizeof(renderpoint_t), compare_renderpoints);
467
468         for(n=0;n<num;n++) {
469             renderpoint_t*p = &points[n];
470             renderpoint_t*next= n<num-1?&points[n+1]:0;
471             int startx = p->x;
472             int endx = next?next->x:i->width2;
473             if(endx > i->width2)
474                 endx = i->width2;
475             if(startx < 0)
476                 startx = 0;
477             if(endx < 0)
478                 endx = 0;
479
480             if(!(n&1))
481                 fill_line(dev, line, zline, y, startx, endx, fill);
482
483             lastx = endx;
484             if(endx == i->width2)
485                 break;
486         }
487         if(fill->type == filltype_clip) {
488             if(i->clipbuf->next) {
489                 U32*line2 = &i->clipbuf->next->data[i->bitwidth*y];
490                 int x;
491                 for(x=0;x<i->bitwidth;x++)
492                     zline[x] &= line2[x];
493             }
494         }
495
496         i->lines[y].num = 0;
497     }
498 }
499
500 void fill_solid(gfxdevice_t*dev, gfxcolor_t* color)
501 {
502     fillinfo_t info;
503     memset(&info, 0, sizeof(info));
504     info.type = filltype_solid;
505     info.color = color;
506     fill(dev, &info);
507 }
508
509 int render_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
510 {
511     internal_t*i = (internal_t*)dev->internal;
512     if(!strcmp(key, "antialize") || !strcmp(key, "antialise")) {
513         i->antialize = atoi(value);
514         i->zoom = i->antialize * i->multiply;
515         return 1;
516     } else if(!strcmp(key, "multiply")) {
517         i->multiply = atoi(value);
518         i->zoom = i->antialize * i->multiply;
519         fprintf(stderr, "Warning: multiply not implemented yet\n");
520         return 1;
521     } else if(!strcmp(key, "fillwhite")) {
522         i->fillwhite = atoi(value);
523         return 1;
524     } else if(!strcmp(key, "palette")) {
525         i->palette = atoi(value);
526         return 1;
527     }
528     return 0;
529 }
530
531 void newclip(struct _gfxdevice*dev)
532 {
533     internal_t*i = (internal_t*)dev->internal;
534     
535     clipbuffer_t*c = (clipbuffer_t*)rfx_calloc(sizeof(clipbuffer_t));
536     c->data = (U32*)rfx_calloc(sizeof(U32) * i->bitwidth * i->height2);
537     c->next = i->clipbuf;
538     i->clipbuf = c;
539     if(c->next)
540         memcpy(c->data, c->next->data, i->bitwidth*i->height2);
541     else
542         memset(c->data, 0, sizeof(U32)*i->bitwidth*i->height2);
543 }
544
545 void endclip(struct _gfxdevice*dev, char removelast)
546 {
547     internal_t*i = (internal_t*)dev->internal;
548    
549     /* test for at least one cliplevel (the one we created ourselves) */
550     if(!i->clipbuf || (!i->clipbuf->next && !removelast)) {
551         fprintf(stderr, "endclip without any active clip buffers\n");
552         return;
553     }
554
555     clipbuffer_t*c = i->clipbuf;
556     i->clipbuf = i->clipbuf->next;
557     c->next = 0;
558     free(c->data);c->data = 0;
559     free(c);
560 }
561
562 void render_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
563 {
564     internal_t*i = (internal_t*)dev->internal;
565     double x,y;
566     
567     /*if(cap_style != gfx_capRound || joint_style != gfx_joinRound) {
568         fprintf(stderr, "Warning: cap/joint style != round not yet supported\n");
569     }*/
570
571     while(line) {
572         if(line->type == gfx_moveTo) {
573         } else if(line->type == gfx_lineTo) {
574             double x1=x*i->zoom,y1=y*i->zoom;
575             double x3=line->x*i->zoom,y3=line->y*i->zoom;
576             add_solidline(dev, x1, y1, x3, y3, width * i->zoom);
577             fill_solid(dev, color);
578         } else if(line->type == gfx_splineTo) {
579             int t,parts;
580             double xx,yy;
581            
582             double x1=x*i->zoom,y1=y*i->zoom;
583             double x2=line->sx*i->zoom,y2=line->sy*i->zoom;
584             double x3=line->x*i->zoom,y3=line->y*i->zoom;
585             
586             double c = abs(x3-2*x2+x1) + abs(y3-2*y2+y1);
587             xx=x1;
588             yy=y1;
589
590             parts = (int)(sqrt(c)/3);
591             if(!parts) parts = 1;
592
593             for(t=1;t<=parts;t++) {
594                 double nx = (double)(t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts);
595                 double ny = (double)(t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts);
596                 
597                 add_solidline(dev, xx, yy, nx, ny, width * i->zoom);
598                 fill_solid(dev, color);
599                 xx = nx;
600                 yy = ny;
601             }
602         }
603         x = line->x;
604         y = line->y;
605         line = line->next;
606     }
607 }
608
609 static void draw_line(gfxdevice_t*dev, gfxline_t*line)
610 {
611     internal_t*i = (internal_t*)dev->internal;
612     double x,y;
613
614     while(line)
615     {
616         int x1,y1,x2,y2,x3,y3;
617
618         if(line->type == gfx_moveTo) {
619         } else if(line->type == gfx_lineTo) {
620             double x1=x*i->zoom,y1=y*i->zoom;
621             double x3=line->x*i->zoom,y3=line->y*i->zoom;
622             
623             add_line(dev, x1, y1, x3, y3);
624         } else if(line->type == gfx_splineTo) {
625             int c,t,parts,qparts;
626             double xx,yy;
627             
628             double x1=x*i->zoom,y1=y*i->zoom;
629             double x2=line->sx*i->zoom,y2=line->sy*i->zoom;
630             double x3=line->x*i->zoom,y3=line->y*i->zoom;
631             
632             c = abs(x3-2*x2+x1) + abs(y3-2*y2+y1);
633             xx=x1;
634             yy=y1;
635
636             parts = (int)(sqrt(c));
637             if(!parts) parts = 1;
638
639             for(t=1;t<=parts;t++) {
640                 double nx = (double)(t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts);
641                 double ny = (double)(t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts);
642                 
643                 add_line(dev, xx, yy, nx, ny);
644                 xx = nx;
645                 yy = ny;
646             }
647         }
648         x = line->x;
649         y = line->y;
650         line = line->next;
651     }
652 }
653
654 void render_startclip(struct _gfxdevice*dev, gfxline_t*line)
655 {
656     internal_t*i = (internal_t*)dev->internal;
657     fillinfo_t info;
658     memset(&info, 0, sizeof(info));
659     newclip(dev);
660     info.type = filltype_clip;
661     draw_line(dev, line);
662     fill(dev, &info);
663 }
664
665 void render_endclip(struct _gfxdevice*dev)
666 {
667     internal_t*i = (internal_t*)dev->internal;
668     endclip(dev, 0);
669 }
670
671 void render_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
672 {
673     internal_t*i = (internal_t*)dev->internal;
674
675     draw_line(dev, line);
676     fill_solid(dev, color);
677 }
678
679 void render_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
680 {
681     internal_t*i = (internal_t*)dev->internal;
682
683     gfxmatrix_t m2 = *matrix;
684
685     draw_line(dev, line);
686
687     fillinfo_t info;
688     memset(&info, 0, sizeof(info));
689     info.type = filltype_bitmap;
690     info.image = img;
691     info.matrix = &m2;
692     info.cxform = cxform;
693
694     m2.m00 *= i->zoom; m2.m01 *= i->zoom; m2.tx *= i->zoom;
695     m2.m10 *= i->zoom; m2.m11 *= i->zoom; m2.ty *= i->zoom;
696
697     fill(dev, &info);
698 }
699
700 void render_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
701 {
702     internal_t*i = (internal_t*)dev->internal;
703     
704     gfxmatrix_t m2 = *matrix;
705
706     draw_line(dev, line);
707
708     RGBA g[256];
709     fillinfo_t info;
710     memset(&info, 0, sizeof(info));
711     info.type = filltype_gradient;
712     info.gradient = g;
713     info.matrix = &m2;
714
715     m2.m00 *= i->zoom; m2.m01 *= i->zoom; m2.tx *= i->zoom;
716     m2.m10 *= i->zoom; m2.m11 *= i->zoom; m2.ty *= i->zoom;
717
718     info.linear_or_radial = type == gfxgradient_radial;
719
720     int pos = 0;
721     gfxcolor_t color = {0,0,0,0};
722     pos=0;
723     while(gradient) {
724         int nextpos = gradient->pos*256;
725         int t;
726         if(nextpos>256) {
727             msg("<error> Invalid gradient- contains values > 1.0");
728             return;
729         }
730         
731         gfxcolor_t nextcolor = gradient->color;
732         if(nextpos!=pos) {
733             double p0 = 1.0;
734             double p1 = 0.0;
735             double step = 1.0/(nextpos-pos);
736             int t;
737             for(t=pos;t<nextpos;t++) {
738                 g[t].r = color.r*p0 + nextcolor.r*p1;
739                 g[t].g = color.g*p0 + nextcolor.g*p1;
740                 g[t].b = color.b*p0 + nextcolor.b*p1;
741                 g[t].a = color.a*p0 + nextcolor.a*p1;
742                 p0 -= step;
743                 p1 += step;
744             }
745         }
746         color=nextcolor;
747
748         pos = nextpos;
749         gradient = gradient->next;
750     }
751     if(pos!=256) {
752         msg("<error> Invalid gradient- doesn't end with 1.0");
753     }
754
755     fill(dev, &info);
756 }
757
758 void render_addfont(struct _gfxdevice*dev, gfxfont_t*font)
759 {
760 }
761
762 void render_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
763 {
764     internal_t*i = (internal_t*)dev->internal;
765     if(!font)
766         return;
767
768     /* align characters to whole pixels */
769     matrix->tx = (int)(matrix->tx * i->antialize) / i->antialize;
770     matrix->ty = (int)(matrix->ty * i->antialize) / i->antialize;
771
772     gfxglyph_t*glyph = &font->glyphs[glyphnr];
773     gfxline_t*line2 = gfxline_clone(glyph->line);
774     gfxline_transform(line2, matrix);
775     draw_line(dev, line2);
776     fill_solid(dev, color);
777     gfxline_free(line2);
778     
779     return;
780 }
781
782 void render_result_write(gfxresult_t*r, int filedesc)
783 {
784     internal_result_t*i= (internal_result_t*)r->internal;
785 }
786 int render_result_save(gfxresult_t*r, const char*filename)
787 {
788     internal_result_t*i= (internal_result_t*)r->internal;
789     if(!i) {
790         return 0; // no pages drawn
791     }
792     if(i->next) {
793         int nr=0;
794         char filenamebuf[256];
795         char*origname = strdup(filename);
796         int l = strlen(origname);
797         if(l>3 && strchr("gG",origname[l-1]) && strchr("nN",filename[l-2]) &&
798                 strchr("pP",origname[l-3]) && filename[l-4]=='.') {
799             origname[l-4] = 0;
800         }
801         while(i->next) {
802             sprintf(filenamebuf, "%s.%d.png", origname, nr);
803             if(!i->palette) {
804                 writePNG(filename, (unsigned char*)i->img.data, i->img.width, i->img.height);
805             } else {
806                 writePalettePNG(filename, (unsigned char*)i->img.data, i->img.width, i->img.height);
807             }
808             nr++;
809         }
810         free(origname);
811     } else {
812         if(!i->palette) {
813             writePNG(filename, (unsigned char*)i->img.data, i->img.width, i->img.height);
814         } else {
815             writePalettePNG(filename, (unsigned char*)i->img.data, i->img.width, i->img.height);
816         }
817     }
818     return 1;
819 }
820 char*gfximage_asXPM(gfximage_t*img, int depth)
821 {
822     int d= 256/depth;
823     char*str = (char*)malloc(img->width*img->height*4 + 500 + 16*depth*depth*depth);
824     char*p = str;
825     p+= sprintf(p, "static char *noname[] = {\n\"%d %d 262144 3\",\n", img->width, img->height);
826     int r,g,b;
827     for(r=0;r<depth;r++)
828     for(g=0;g<depth;g++)
829     for(b=0;b<depth;b++) {
830         p += sprintf(p, "\"%c%c%c c #%02x%02x%02x\",\n", r+32,g+32,b+32, r*d,g*d,b*d);
831     }
832     int y;
833     for(y=0;y<img->height;y++)  {
834         p+=sprintf(p, "\"");
835         gfxcolor_t*col = &img->data[y*img->height];
836         int x;
837         for(x=0;x<img->width;x++) {
838             p+=sprintf(p, "%c%c%c", 32+(col->r/d), 32+(col->g/d), 32+(col->b/d));
839         }
840         p+=sprintf(p, "\",\n");
841     }
842     *p = 0;
843     return p;
844 }
845 void*render_result_get(gfxresult_t*r, const char*name)
846 {
847     internal_result_t*i= (internal_result_t*)r->internal;
848     if(!strncmp(name,"xpm",3)) {
849         int pagenr = atoi(&name[3]);
850         if(pagenr<0)
851             pagenr=0;
852         while(pagenr>0) {
853             i = i->next;
854             if(!i)
855                 return 0;
856             pagenr--;
857         }
858         return gfximage_asXPM(&i->img, 64);
859     } else if(!strncmp(name,"page",4)) {
860         int pagenr = atoi(&name[4]);
861         if(pagenr<0)
862             pagenr=0;
863         while(pagenr>0) {
864             i = i->next;
865             if(!i)
866                 return 0;
867             pagenr--;
868         }
869         return &i->img;
870     }
871     return 0;
872 }
873 void render_result_destroy(gfxresult_t*r)
874 {
875     internal_result_t*i= (internal_result_t*)r->internal;
876     r->internal = 0;
877     while(i) {
878         internal_result_t*next = i->next;
879         free(i->img.data);i->img.data = 0;
880
881         /* FIXME memleak
882            the following rfx_free causes a segfault on WIN32 machines,
883            if executed */
884         //rfx_free(i);
885
886         i = next;
887     }
888     rfx_free(r);
889 }
890
891 gfxresult_t* render_finish(struct _gfxdevice*dev)
892 {
893     internal_t*i = (internal_t*)dev->internal;
894     
895     gfxresult_t* res = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
896     
897     res->internal = i->results;i->results = 0;
898     res->write = render_result_write;
899     res->save = render_result_save;
900     res->get = render_result_get;
901     res->destroy = render_result_destroy;
902
903     free(dev->internal); dev->internal = 0; i = 0;
904
905     return res;
906 }
907
908 void render_startpage(struct _gfxdevice*dev, int width, int height)
909 {
910     internal_t*i = (internal_t*)dev->internal;
911     int y;
912
913     if(i->width2 || i->height2) {
914         fprintf(stderr, "Error: startpage() called twice (no endpage()?)\n");
915         exit(1);
916     }
917     
918     i->width = width*i->multiply;
919     i->height = height*i->multiply;
920     i->width2 = width*i->zoom;
921     i->height2 = height*i->zoom;
922     i->bitwidth = (i->width2+31)/32;
923
924     i->lines = (renderline_t*)rfx_alloc(i->height2*sizeof(renderline_t));
925     for(y=0;y<i->height2;y++) {
926         memset(&i->lines[y], 0, sizeof(renderline_t));
927         i->lines[y].points = 0;
928         i->lines[y].num = 0;
929     }
930     i->img = (RGBA*)rfx_calloc(sizeof(RGBA)*i->width2*i->height2);
931     if(i->fillwhite) {
932         memset(i->img, 0xff, sizeof(RGBA)*i->width2*i->height2);
933     }
934
935     i->ymin = 0x7fffffff;
936     i->ymax = -0x80000000;
937
938
939     /* initialize initial clipping field, which doesn't clip anything yet */
940     newclip(dev);
941     memset(i->clipbuf->data, 255, sizeof(U32)*i->bitwidth*i->height2);
942 }
943
944 static void store_image(internal_t*i, internal_result_t*ir)
945 {
946     ir->img.data = (gfxcolor_t*)malloc(i->width*i->height*sizeof(gfxcolor_t));
947     ir->img.width = i->width;
948     ir->img.height = i->height;
949
950     gfxcolor_t*dest = ir->img.data;
951
952     if(i->antialize <= 1) /* no antializing */ {
953         int y;
954         for(y=0;y<i->height;y++) {
955             RGBA*line = &i->img[y*i->width];
956             memcpy(&dest[y*i->width], line, sizeof(RGBA)*i->width);
957         }
958     } else {
959         RGBA**lines = (RGBA**)rfx_calloc(sizeof(RGBA*)*i->antialize);
960         int q = i->antialize*i->antialize;
961         int ypos = 0;
962         int y;
963         int y2=0;
964         for(y=0;y<i->height2;y++) {
965             int n;
966             ypos = y % i->antialize;
967             lines[ypos] = &i->img[y*i->width2];
968             if(ypos == i->antialize-1) {
969                 RGBA*out = &dest[(y2++)*i->width];
970                 int x;
971                 int r,g,b,a;
972                 for(x=0;x<i->width;x++) {
973                     int xpos = x*i->antialize;
974                     int yp;
975                     U32 r=0,g=0,b=0,a=0;
976                     for(yp=0;yp<i->antialize;yp++) {
977                         RGBA*lp = &lines[yp][xpos];
978                         int xp;
979                         for(xp=0;xp<i->antialize;xp++) {
980                             RGBA*p = &lp[xp];
981                             r += p->r;
982                             g += p->g;
983                             b += p->b;
984                             a += p->a;
985                         }
986                     }
987                     out[x].r = r / q;
988                     out[x].g = g / q;
989                     out[x].b = b / q;
990                     out[x].a = a / q;
991                 }
992             }
993         }
994         rfx_free(lines);
995     }
996 }
997
998 void render_endpage(struct _gfxdevice*dev)
999 {
1000     internal_t*i = (internal_t*)dev->internal;
1001     
1002     if(!i->width2 || !i->height2) {
1003         fprintf(stderr, "Error: endpage() called without corresponding startpage()\n");
1004         exit(1);
1005     }
1006
1007     endclip(dev, 1);
1008     int unclosed = 0;
1009     while(i->clipbuf) {
1010         endclip(dev, 1);
1011         unclosed++;
1012     }
1013
1014     if(unclosed) {
1015         fprintf(stderr, "Warning: %d unclosed clip(s) while processing endpage()\n", unclosed);
1016     }
1017     
1018     internal_result_t*ir= (internal_result_t*)rfx_calloc(sizeof(internal_result_t));
1019     ir->palette = i->palette;
1020
1021     int y,x;
1022
1023     store_image(i, ir);
1024
1025     ir->next = 0;
1026     if(i->result_next) {
1027         i->result_next->next = ir;
1028     }
1029     if(!i->results) {
1030         i->results = ir;
1031     }
1032     i->result_next = ir;
1033
1034     for(y=0;y<i->height2;y++) {
1035         rfx_free(i->lines[y].points); i->lines[y].points = 0;
1036     }
1037     rfx_free(i->lines);i->lines=0;
1038
1039     if(i->img) {rfx_free(i->img);i->img = 0;}
1040
1041     i->width2 = 0;
1042     i->height2 = 0;
1043 }
1044
1045 void render_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
1046 {
1047     /* not supported for this output device */
1048 }
1049
1050 void gfxdevice_render_init(gfxdevice_t*dev)
1051 {
1052     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
1053     memset(dev, 0, sizeof(gfxdevice_t));
1054     
1055     dev->name = "render";
1056
1057     dev->internal = i;
1058     
1059     i->width = 0;
1060     i->width2 = 0;
1061     i->height = 0;
1062     i->height2 = 0;
1063     i->antialize = 1;
1064     i->multiply = 1;
1065     i->zoom = 1;
1066
1067     dev->setparameter = render_setparameter;
1068     dev->startpage = render_startpage;
1069     dev->startclip = render_startclip;
1070     dev->endclip = render_endclip;
1071     dev->stroke = render_stroke;
1072     dev->fill = render_fill;
1073     dev->fillbitmap = render_fillbitmap;
1074     dev->fillgradient = render_fillgradient;
1075     dev->addfont = render_addfont;
1076     dev->drawchar = render_drawchar;
1077     dev->drawlink = render_drawlink;
1078     dev->endpage = render_endpage;
1079     dev->finish = render_finish;
1080 }
1081
1082
1083 gfxdevice_t* gfxdevice_render_new()
1084 {
1085     gfxdevice_t* d = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
1086     gfxdevice_render_init(d);
1087     return d;
1088 }