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
|
#include <fitz.h>
static inline void
addspan(unsigned char *list, int x0, int x1, int xofs, int hs)
{
int x0pix, x0sub;
int x1pix, x1sub;
if (x0 == x1)
return;
/* x between 0 and width of bbox */
x0 -= xofs;
x1 -= xofs;
x0pix = x0 / hs;
x0sub = x0 % hs;
x1pix = x1 / hs;
x1sub = x1 % hs;
if (x0pix == x1pix)
{
list[x0pix] += x1sub - x0sub;
list[x0pix+1] += x0sub - x1sub;
}
else
{
list[x0pix] += hs - x0sub;
list[x0pix+1] += x0sub;
list[x1pix] += x1sub - hs;
list[x1pix+1] += -x1sub;
}
}
static inline void
nonzerowinding(fz_ael *ael, unsigned char *list, int xofs, int hs)
{
int winding = 0;
int x = 0;
int i;
for (i = 0; i < ael->len; i++)
{
if (!winding && (winding + ael->edges[i]->ydir))
x = ael->edges[i]->x;
if (winding && !(winding + ael->edges[i]->ydir))
addspan(list, x, ael->edges[i]->x, xofs, hs);
winding += ael->edges[i]->ydir;
}
}
static inline void
evenodd(fz_ael *ael, unsigned char *list, int xofs, int hs)
{
int even = 0;
int x = 0;
int i;
for (i = 0; i < ael->len; i++)
{
if (!even)
x = ael->edges[i]->x;
else
addspan(list, x, ael->edges[i]->x, xofs, hs);
even = !even;
}
}
static void toalpha(unsigned char *list, int n)
{
int d = 0;
while (n--)
{
d += *list;
*list++ = d;
}
}
fz_error *
fz_scanconvert(fz_gel *gel, fz_ael *ael, int eofill, int y0, int y1,
void (*blitfunc)(int,int,int,unsigned char*,void*), void *blitdata)
{
fz_error *error;
unsigned char *deltas;
int y, e;
int yd, yc;
int xmin = fz_idiv(gel->xmin, gel->hs);
int xmax = fz_idiv(gel->xmax, gel->hs) + 1;
int xofs = xmin * gel->hs;
int hs = gel->hs;
int vs = gel->vs;
if (gel->len == 0)
return nil;
deltas = fz_malloc(xmax - xmin + 1);
if (!deltas)
return fz_outofmem;
memset(deltas, 0, xmax - xmin + 1);
e = 0;
y = gel->edges[0].y;
yc = fz_idiv(y, vs);
yd = yc;
while (ael->len > 0 || e < gel->len)
{
yc = fz_idiv(y, vs);
if (yc != yd) {
if (yd >= y0 && yd < y1)
{
toalpha(deltas, xmax - xmin);
blitfunc(yd, xmin, xmax - xmin, deltas, blitdata);
memset(deltas, 0, xmax - xmin + 1);
}
}
yd = yc;
error = fz_insertael(ael, gel, y, &e);
if (error) {
fz_free(deltas);
return error;
}
if (yd >= y0 && yd < y1)
{
if (eofill)
evenodd(ael, deltas, xofs, hs);
else
nonzerowinding(ael, deltas, xofs, hs);
}
fz_advanceael(ael);
if (ael->len > 0)
y ++;
else if (e < gel->len)
y = gel->edges[e].y;
}
if (yd >= y0 && yd < y1)
{
toalpha(deltas, xmax - xmin);
blitfunc(yd, xmin, xmax - xmin, deltas, blitdata);
}
fz_free(deltas);
return nil;
}
|