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