1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
#include <stdlib.h>
#include <memory.h>
#include <math.h>
#include "renderpoly.h"
typedef struct _renderpoint
{
double x;
segment_dir_t dir;
edgestyle_t*fs;
int polygon_nr;
} renderpoint_t;
typedef struct _renderline
{
renderpoint_t*points;
int size;
int num;
} renderline_t;
typedef struct _renderbuf
{
intbbox_t bbox;
int width;
int height;
double zoom;
renderline_t*lines;
} renderbuf_t;
static inline void add_pixel(renderbuf_t*buf, double x, int y, segment_dir_t dir, edgestyle_t*fs, int polygon_nr)
{
renderpoint_t p;
p.x = x;
p.dir = dir;
p.fs = fs;
p.polygon_nr = polygon_nr;
if(y >= buf->bbox.ymax || y < buf->bbox.ymin)
return;
renderline_t*l = &buf->lines[y-buf->bbox.ymin];
if(l->num == l->size) {
l->size += 32;
l->points = (renderpoint_t*)rfx_realloc(l->points, l->size * sizeof(renderpoint_t));
}
l->points[l->num] = p;
l->num++;
}
#define CUT 0.5
static void add_line(renderbuf_t*buf, double x1, double y1, double x2, double y2, edgestyle_t*fs, segment_dir_t dir, int polygon_nr)
{
x1 *= buf->zoom;
y1 *= buf->zoom;
x2 *= buf->zoom;
y2 *= buf->zoom;
double diffx, diffy;
double ny1, ny2, stepx;
if(y2 < y1) {
dir ^= DIR_UP^DIR_DOWN;
double x,y;
x = x1;x1 = x2;x2=x;
y = y1;y1 = y2;y2=y;
}
diffx = x2 - x1;
diffy = y2 - y1;
ny1 = floor(y1)+CUT;
ny2 = floor(y2)+CUT;
if(ny1 < y1) {
ny1 = floor(y1) + 1.0 + CUT;
}
if(ny2 >= y2) {
ny2 = floor(y2) - 1.0 + CUT;
}
if(ny1 > ny2)
return;
stepx = diffx/diffy;
x1 = x1 + (ny1-y1)*stepx;
x2 = x2 + (ny2-y2)*stepx;
int posy=floor(ny1);
int endy=floor(ny2);
double posx=0;
double startx = x1;
//printf("line %d from %f to %f dir=%s\n", polygon_nr, y1, y2, dir==DIR_UP?"up":"down");
while(posy<=endy) {
double xx = startx + posx;
add_pixel(buf, xx, posy, dir, fs, polygon_nr);
posx+=stepx;
posy++;
}
}
static int compare_renderpoints(const void * _a, const void * _b)
{
renderpoint_t*a = (renderpoint_t*)_a;
renderpoint_t*b = (renderpoint_t*)_b;
if(a->x < b->x) return -1;
if(a->x > b->x) return 1;
return 0;
}
static void fill_bitwise(unsigned char*line, int x1, int x2)
{
int p1 = x1>>3;
int p2 = x2>>3;
int b1 = 0xff >> (x1&7);
int b2 = 0xff << (1+7-(x2&7));
if(p1==p2) {
line[p1] |= b1&b2;
} else {
line[p1] |= b1;
memset(&line[p1+1], 255, p2-(p1+1));
line[p2] = b2;
}
}
unsigned char* render_polygon(gfxpoly_t*polygon, intbbox_t*bbox, double zoom, windrule_t*rule, windcontext_t*context)
{
renderbuf_t _buf, *buf=&_buf;
buf->width = (bbox->xmax - bbox->xmin);
buf->height = (bbox->ymax - bbox->ymin);
buf->bbox = *bbox;
buf->zoom = zoom * polygon->gridsize;
int width8 = (buf->width+7) >> 3;
char bleeding = 0;
unsigned char* image = (unsigned char*)malloc(width8*buf->height);
memset(image, 0, width8*buf->height);
buf->lines = (renderline_t*)rfx_alloc(buf->height*sizeof(renderline_t));
int y;
for(y=0;y<buf->height;y++) {
memset(&buf->lines[y], 0, sizeof(renderline_t));
buf->lines[y].points = 0;
buf->lines[y].num = 0;
}
int polygon_nr = 0;
int s,t;
gfxpolystroke_t*stroke = polygon->strokes;
for(;stroke;stroke=stroke->next) {
for(t=0;t<stroke->num_points-1;t++) {
point_t a = stroke->points[t];
point_t b = stroke->points[t+1];
add_line(buf, a.x, a.y, b.x, b.y, stroke->fs, stroke->dir, polygon_nr);
}
}
for(y=0;y<buf->height;y++) {
renderpoint_t*points = buf->lines[y].points;
unsigned char*line = &image[width8*y];
int n;
int num = buf->lines[y].num;
qsort(points, num, sizeof(renderpoint_t), compare_renderpoints);
int lastx = 0;
windstate_t fill = rule->start(context);
for(n=0;n<num;n++) {
renderpoint_t*p = &points[n];
int x = (int)(p->x - bbox->xmin);
if(x < lastx)
x = lastx;
if(x > buf->width)
x = buf->width;
if(fill.is_filled && lastx<x) {
fill_bitwise(line, lastx, x);
}
fill = rule->add(context, fill, p->fs, p->dir, p->polygon_nr);
lastx = x;
}
if(fill.is_filled && lastx!=buf->width) {
/* we're bleeding, fill over padding, too. */
fprintf(stderr, "Polygon %p is bleeding in line %d\n", polygon, y);
fill_bitwise(line, lastx, width8*8);
assert(line[width8-1]&0x01);
bleeding = 1;
exit(1);
}
}
for(y=0;y<buf->height;y++) {
if(buf->lines[y].points) {
free(buf->lines[y].points);
}
memset(&buf->lines[y], 0, sizeof(renderline_t));
}
if(bleeding) {
assert(!bitmap_ok(bbox, image));
}
free(buf->lines);buf->lines=0;
return image;
}
#define MAX_WIDTH 8192
#define MAX_HEIGHT 4096
static inline double max(double a, double b) {return a>b?a:b;}
static inline double min(double a, double b) {return a<b?a:b;}
static int adjust_x(int xmin, int xmax)
{
xmax += 8;
while(((xmax - xmin)&0x07) != 0x04)
xmax++;
return xmax;
}
intbbox_t intbbox_new(int x1, int y1, int x2, int y2)
{
intbbox_t b;
b.xmin = x1;
b.ymin = y1;
b.xmax = x2;
b.ymax = y2;
b.xmax = adjust_x(b.xmin, b.xmax);
b.width = b.xmax - b.xmin;
b.height = b.ymax - b.ymin;
return b;
}
intbbox_t intbbox_from_polygon(gfxpoly_t*polygon, double zoom)
{
intbbox_t b = {0,0,0,0};
double g = zoom*polygon->gridsize;
if(polygon->strokes && polygon->strokes->num_points) {
b.xmin = polygon->strokes->points[0].x*g;
b.ymin = polygon->strokes->points[0].y*g;
b.xmax = polygon->strokes->points[0].x*g;
b.ymax = polygon->strokes->points[0].y*g;
}
int s,t;
gfxpolystroke_t*stroke = polygon->strokes;
for(;stroke;stroke=stroke->next) {
for(t=0;t<stroke->num_points;t++) {
point_t p = stroke->points[t];
int x1 = floor(p.x);
int y1 = floor(p.y);
int x2 = ceil(p.x);
int y2 = ceil(p.y);
if(x1 < b.xmin) b.xmin = x1;
if(y1 < b.ymin) b.ymin = y1;
if(x2 > b.xmax) b.xmax = x2;
if(y2 > b.ymax) b.ymax = y2;
}
}
if(b.xmax > (int)(MAX_WIDTH*zoom))
b.xmax = (int)(MAX_WIDTH*zoom);
if(b.ymax > (int)(MAX_HEIGHT*zoom))
b.ymax = (int)(MAX_HEIGHT*zoom);
if(b.xmin < -(int)(MAX_WIDTH*zoom))
b.xmin = -(int)(MAX_WIDTH*zoom);
if(b.ymin < -(int)(MAX_HEIGHT*zoom))
b.ymin = -(int)(MAX_HEIGHT*zoom);
if(b.xmin > b.xmax)
b.xmin = b.xmax;
if(b.ymin > b.ymax)
b.ymin = b.ymax;
b.xmax = adjust_x(b.xmin, b.xmax);
b.width = b.xmax - b.xmin;
b.height = b.ymax - b.ymin;
return b;
}
#define B11111000 0xf8
#define B01111100 0x7c
#define B00111110 0x3e
#define B00011111 0x1f
#define B11100000 0xe0
#define B01110000 0x70
#define B00111000 0x38
#define B00011100 0x1c
#define B00001110 0x0e
#define B00000111 0x07
#define B10000000 0x80
#define B01000000 0x40
#define B00100000 0x20
#define B00010000 0x10
#define B00001000 0x08
#define B00000100 0x04
#define B00000010 0x02
#define B00000001 0x01
int bitmap_ok(intbbox_t*bbox, unsigned char*data)
{
int y;
int width8 = (bbox->width+7) >> 3;
for(y=0;y<bbox->height;y++) {
if(data[width8-1]&0x01)
return 0; //bleeding
data += width8;
}
return 1;
}
int compare_bitmaps(intbbox_t*bbox, unsigned char*data1, unsigned char*data2)
{
if(!data1 || !data2)
return 0;
int x,y;
int height = bbox->height;
int width = bbox->width;
int width8 = (width+7) >> 3;
unsigned char*l1 = &data1[width8*2];
unsigned char*l2 = &data2[width8*2];
for(y=2;y<height-2;y++) {
for(x=0;x<width8;x++) {
unsigned char a1 = l1[x-width8*2] & l1[x-width8] & l1[x] &
l1[x+width8] & l1[x+width8*2];
unsigned char b1 = l2[x-width8*2] & l2[x-width8] & l2[x] &
l2[x+width8] & l2[x+width8*2];
unsigned char a2 = l1[x-width8*2] | l1[x-width8] | l1[x] |
l1[x+width8] | l1[x+width8*2];
unsigned char b2 = l2[x-width8*2] | l2[x-width8] | l2[x] |
l2[x+width8] | l2[x+width8*2];
char fail = 0;
if((a1&B11111000)==B11111000 && (b2&B11111000)==0) fail=2;
if((a1&B01111100)==B01111100 && (b2&B01111100)==0) fail=3;
if((a1&B00111110)==B00111110 && (b2&B00111110)==0) fail=4;
if((a1&B00011111)==B00011111 && (b2&B00011111)==0) fail=5;
if((b1&B11111000)==B11111000 && (a2&B11111000)==0) fail=2;
if((b1&B01111100)==B01111100 && (a2&B01111100)==0) fail=3;
if((b1&B00111110)==B00111110 && (a2&B00111110)==0) fail=4;
if((b1&B00011111)==B00011111 && (a2&B00011111)==0) fail=5;
if(fail) {
return 0;
}
}
l1 += width8;
l2 += width8;
}
return 1;
}
#include "../png.h"
void save_two_bitmaps(intbbox_t*b, unsigned char*data1, unsigned char*data2, char*filename)
{
int width8 = (b->width+7) >> 3;
unsigned char*data = malloc(b->width*b->height*4*4);
unsigned char*p = data;
int x,y;
unsigned char*b1 = data1;
unsigned char*b2 = data2;
compare_bitmaps(b, data1, data2);
//# define MARK ((abs(x-badx)<3 && abs(y-bady)<3)*255)
#define MARK 0
for(y=0;y<b->height;y++) {
for(x=0;x<b->width;x++) {
unsigned char c1 = (b1[x>>3]&(0x80>>(x&7)))?255:0;
unsigned char c2 = (b2[x>>3]&(0x80>>(x&7)))?255:0;
*p++ = 255;
*p++ = c1^c2;
*p++ = c1^c2;
*p++ = MARK;
}
for(x=0;x<b->width;x++) {
unsigned char c = (b2[x>>3]&(0x80>>(x&7)))?255:0;
*p++ = 255;
*p++ = c;
*p++ = c;
*p++ = MARK;
}
b1 += width8;
b2 += width8;
}
b1 = data1;
for(y=0;y<b->height;y++) {
for(x=0;x<b->width;x++) {
unsigned char c = (b1[x>>3]&(0x80>>(x&7)))?255:0;
*p++ = 255;
*p++ = c;
*p++ = c;
*p++ = MARK;
}
for(x=0;x<b->width;x++) {
*p++ = 255;
*p++ = 0;
*p++ = 0;
*p++ = 0;
}
b1 += width8;
}
png_write(filename, data, b->width*2, b->height*2);
free(data);
}
|