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