4 #include "renderpoly.h"
6 typedef struct _renderpoint
11 edge_t*e; //only for debugging
15 typedef struct _renderline
22 typedef struct _renderbuf
31 static inline void add_pixel(renderbuf_t*buf, double x, int y, segment_dir_t dir, fillstyle_t*fs, int polygon_nr, edge_t*e)
38 p.polygon_nr = polygon_nr;
40 if(y >= buf->bbox.ymax || y < buf->bbox.ymin)
43 renderline_t*l = &buf->lines[y-buf->bbox.ymin];
45 if(l->num == l->size) {
47 l->points = (renderpoint_t*)rfx_realloc(l->points, l->size * sizeof(renderpoint_t));
49 l->points[l->num] = p;
53 static void add_line(renderbuf_t*buf, double x1, double y1, double x2, double y2, fillstyle_t*fs, int polygon_nr, edge_t*e)
60 double ny1, ny2, stepx;
61 segment_dir_t dir = DIR_DOWN;
74 ny1 = floor(y1) + 1.0 + CUT;
77 ny2 = floor(y2) - 1.0 + CUT;
83 x1 = x1 + (ny1-y1)*stepx;
84 x2 = x2 + (ny2-y2)*stepx;
92 double xx = startx + posx;
93 add_pixel(buf, xx, posy, dir, fs, polygon_nr, e);
99 static int compare_renderpoints(const void * _a, const void * _b)
101 renderpoint_t*a = (renderpoint_t*)_a;
102 renderpoint_t*b = (renderpoint_t*)_b;
103 if(a->x < b->x) return -1;
104 if(a->x > b->x) return 1;
108 static void fill_bitwise(unsigned char*line, int x1, int x2)
112 int b1 = 0xff >> (x1&7);
113 int b2 = 0xff << (1+7-(x2&7));
118 memset(&line[p1+1], 255, p2-(p1+1));
123 unsigned char* render_polygon(gfxpoly_t*polygon, intbbox_t*bbox, double zoom, windrule_t*rule, windcontext_t*context)
125 renderbuf_t _buf, *buf=&_buf;
126 buf->width = (bbox->xmax - bbox->xmin);
127 buf->height = (bbox->ymax - bbox->ymin);
129 buf->zoom = zoom * polygon->gridsize;
130 int width8 = (buf->width+7) >> 3;
131 unsigned char* image = (unsigned char*)malloc(width8*buf->height);
132 memset(image, 0, width8*buf->height);
134 buf->lines = (renderline_t*)rfx_alloc(buf->height*sizeof(renderline_t));
136 for(y=0;y<buf->height;y++) {
137 memset(&buf->lines[y], 0, sizeof(renderline_t));
138 buf->lines[y].points = 0;
139 buf->lines[y].num = 0;
144 for(e=polygon->edges;e;e=e->next) {
145 add_line(buf, e->a.x, e->a.y, e->b.x, e->b.y, e->style, polygon_nr, e);
148 for(y=0;y<buf->height;y++) {
149 renderpoint_t*points = buf->lines[y].points;
150 unsigned char*line = &image[width8*y];
152 int num = buf->lines[y].num;
153 qsort(points, num, sizeof(renderpoint_t), compare_renderpoints);
156 windstate_t fill = rule->start(context);
158 renderpoint_t*p = &points[n];
159 int x = (int)(p->x - bbox->xmin);
166 if(fill.is_filled && lastx<x) {
167 fill_bitwise(line, lastx, x);
169 fill = rule->add(context, fill, p->fs, p->dir, p->polygon_nr);
172 if(fill.is_filled && lastx!=buf->width) {
173 /* we're bleeding, fill over padding, too. */
174 fprintf(stderr, "Polygon %08x is bleeding in line %d\n", polygon, y);
175 fill_bitwise(line, lastx, width8*8);
179 for(y=0;y<buf->height;y++) {
180 if(buf->lines[y].points) {
181 free(buf->lines[y].points);
183 memset(&buf->lines[y], 0, sizeof(renderline_t));
185 free(buf->lines);buf->lines=0;
189 #define MAX_WIDTH 8192
190 #define MAX_HEIGHT 4096
192 static inline max(double a, double b) {return a>b?a:b;}
193 static inline min(double a, double b) {return a<b?a:b;}
195 intbbox_t intbbox_new(int x1, int y1, int x2, int y2)
207 intbbox_t intbbox_from_polygon(gfxpoly_t*polygon, double zoom)
210 intbbox_t b = {0,0,0,0};
211 edge_t*e = polygon->edges;
213 double g = zoom*polygon->gridsize;
221 for(e=polygon->edges;e;e=e->next) {
222 double x_min = min(e->a.x,e->b.x)*g;
223 double y_min = min(e->a.y,e->b.y)*g;
224 double x_max = max(e->a.x,e->b.x)*g;
225 double y_max = max(e->a.y,e->b.y)*g;
226 int x1 = floor(x_min);
227 int y1 = floor(y_min);
228 int x2 = ceil(x_max);
229 int y2 = ceil(y_max);
230 if(x1 < b.xmin) b.xmin = x1;
231 if(y1 < b.ymin) b.ymin = y1;
232 if(x2 > b.xmax) b.xmax = x2;
233 if(y2 > b.ymax) b.ymax = y2;
236 if(b.xmax > (int)(MAX_WIDTH*zoom))
237 b.xmax = (int)(MAX_WIDTH*zoom);
238 if(b.ymax > (int)(MAX_HEIGHT*zoom))
239 b.ymax = (int)(MAX_HEIGHT*zoom);
240 if(b.xmin < -(int)(MAX_WIDTH*zoom))
241 b.xmin = -(int)(MAX_WIDTH*zoom);
242 if(b.ymin < -(int)(MAX_HEIGHT*zoom))
243 b.ymin = -(int)(MAX_HEIGHT*zoom);
251 while(((b.xmax - b.xmin)&0x07) != 0x04)
254 b.width = b.xmax - b.xmin;
255 b.height = b.ymax - b.ymin;
259 #define B11111000 0xf8
260 #define B01111100 0x7c
261 #define B00111110 0x3e
262 #define B00011111 0x1f
263 #define B11100000 0xe0
264 #define B01110000 0x70
265 #define B00111000 0x38
266 #define B00011100 0x1c
267 #define B00001110 0x0e
268 #define B00000111 0x07
269 #define B10000000 0x80
270 #define B01000000 0x40
271 #define B00100000 0x20
272 #define B00010000 0x10
273 #define B00001000 0x08
274 #define B00000100 0x04
275 #define B00000010 0x02
276 #define B00000001 0x01
278 int bitmap_ok(intbbox_t*bbox, unsigned char*data)
281 int width8 = (bbox->width+7) >> 3;
282 if((bbox->width&7) == 0)
284 for(y=0;y<bbox->height;y++) {
285 if(data[width8-1]&0x01)
292 int compare_bitmaps(intbbox_t*bbox, unsigned char*data1, unsigned char*data2)
297 int height = bbox->height;
298 int width = bbox->width;
299 int width8 = (width+7) >> 3;
300 unsigned char*l1 = &data1[width8*2];
301 unsigned char*l2 = &data2[width8*2];
302 for(y=2;y<height-2;y++) {
303 for(x=0;x<width8;x++) {
304 unsigned char a1 = l1[x-width8*2] & l1[x-width8] & l1[x] &
305 l1[x+width8] & l1[x+width8*2];
306 unsigned char b1 = l2[x-width8*2] & l2[x-width8] & l2[x] &
307 l2[x+width8] & l2[x+width8*2];
308 unsigned char a2 = l1[x-width8*2] | l1[x-width8] | l1[x] |
309 l1[x+width8] | l1[x+width8*2];
310 unsigned char b2 = l2[x-width8*2] | l2[x-width8] | l2[x] |
311 l2[x+width8] | l2[x+width8*2];
314 if((a1&B11111000)==B11111000 && (b2&B11111000)==0) fail=2;
315 if((a1&B01111100)==B01111100 && (b2&B01111100)==0) fail=3;
316 if((a1&B00111110)==B00111110 && (b2&B00111110)==0) fail=4;
317 if((a1&B00011111)==B00011111 && (b2&B00011111)==0) fail=5;
318 if((b1&B11111000)==B11111000 && (a2&B11111000)==0) fail=2;
319 if((b1&B01111100)==B01111100 && (a2&B01111100)==0) fail=3;
320 if((b1&B00111110)==B00111110 && (a2&B00111110)==0) fail=4;
321 if((b1&B00011111)==B00011111 && (a2&B00011111)==0) fail=5;
333 void save_two_bitmaps(intbbox_t*b, unsigned char*data1, unsigned char*data2, char*filename)
335 int width8 = (b->width+7) >> 3;
336 unsigned char*data = malloc(b->width*b->height*4*4);
337 unsigned char*p = data;
339 unsigned char*b1 = data1;
340 unsigned char*b2 = data2;
341 compare_bitmaps(b, data1, data2);
342 //# define MARK ((abs(x-badx)<3 && abs(y-bady)<3)*255)
344 for(y=0;y<b->height;y++) {
345 for(x=0;x<b->width;x++) {
346 unsigned char c1 = (b1[x>>3]&(0x80>>(x&7)))?255:0;
347 unsigned char c2 = (b2[x>>3]&(0x80>>(x&7)))?255:0;
353 for(x=0;x<b->width;x++) {
354 unsigned char c = (b2[x>>3]&(0x80>>(x&7)))?255:0;
364 for(y=0;y<b->height;y++) {
365 for(x=0;x<b->width;x++) {
366 unsigned char c = (b1[x>>3]&(0x80>>(x&7)))?255:0;
372 for(x=0;x<b->width;x++) {
380 writePNG(filename, data, b->width*2, b->height*2);