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
|
/*
* Copyright © 2016 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include "ao.h"
#include "ao_draw.h"
#include "ao_draw_int.h"
#define ao_mask(x,w) (ao_right(AO_ALLONES,(x) & AO_MASK) & \
ao_left(AO_ALLONES,(FB_UNIT - ((x)+(w))) & AO_MASK))
/* out of clip region codes */
#define OUT_LEFT 0x08
#define OUT_RIGHT 0x04
#define OUT_ABOVE 0x02
#define OUT_BELOW 0x01
/* major axis for bresenham's line */
#define X_AXIS 0
#define Y_AXIS 1
/*
* Line clipping. Clip to the box, bringing the coordinates forward while
* preserving the actual slope and error
*
*
* X major line, clip X:
*
* adjust_x = -x;
*
* e += adjust_x * e1;
*
* adjust_y = (e + -e3-1) / -e3;
*
* e -= adjust_y / -e3;
*
* X major line, clip Y:
*
* adjust_y = -y;
*
* e -= adjust_y / -e3;
*
* adjust_x = e / e1;
*/
static void
ao_bres(const struct ao_bitmap *dst_bitmap,
int16_t signdx,
int16_t signdy,
int16_t axis,
int16_t x1,
int16_t y1,
int16_t e,
int16_t e1,
int16_t e3,
int16_t len,
uint32_t and,
uint32_t xor)
{
int16_t stride = dst_bitmap->stride;
uint32_t *dst = dst_bitmap->base;
uint32_t mask0, mask;
mask0 = 1;
if (signdx < 0)
mask0 = ao_right(1, AO_UNIT - 1);
if (signdy < 0)
stride = -stride;
dst = dst + y1 * stride + (x1 >> AO_SHIFT);
mask = ao_right(1, x1 & AO_MASK);
while (len--) {
/* clip each point */
*dst = ao_do_mask_rrop(*dst, and, xor, mask);
if (axis == X_AXIS) {
if (signdx < 0)
mask = ao_left(mask, 1);
else
mask = ao_right(mask, 1);
if (!mask) {
dst += signdx;
mask = mask0;
}
e += e1;
if (e >= 0) {
dst += stride;
e += e3;
}
} else {
dst += stride;
e += e1;
if (e >= 0) {
if (signdx < 0)
mask = ao_left(mask, 1);
else
mask = ao_right(mask, 1);
if (!mask) {
dst += signdx;
mask = mask0;
}
e += e3;
}
}
}
}
struct ao_cc {
int16_t major;
int16_t minor;
int16_t sign_major;
int16_t sign_minor;
int16_t e;
int16_t e1;
int16_t e3;
int8_t first;
};
/* line clipping box */
struct ao_cbox {
int16_t maj1, min1;
int16_t maj2, min2;
};
/* -b <= a, so we need to make a bigger */
static int16_t
div_ceil(int32_t a, int16_t b) {
return (a + b + b - 1) / b - 1;
}
static int16_t
div_floor_plus_one(int32_t a, int16_t b) {
return (a + b) / b;
}
static int8_t
ao_clip_line(struct ao_cc *c, struct ao_cbox *b)
{
int32_t adjust_major = 0, adjust_minor = 0;
/* Clip major axis */
if (c->major < b->maj1) {
if (c->sign_major <= 0)
return FALSE;
adjust_major = b->maj1 - c->major;
} else if (c->major >= b->maj2) {
if (c->sign_major >= 0)
return FALSE;
adjust_major = c->major - (b->maj2-1);
}
/* Clip minor axis */
if (c->minor < b->min1) {
if (c->sign_minor <= 0)
return FALSE;
adjust_minor = b->min1 - c->minor;
} else if (c->minor >= b->min2) {
if (c->sign_minor >= 0)
return FALSE;
adjust_minor = c->minor - (b->min2-1);
}
/* If unclipped, we're done */
if (adjust_major == 0 && adjust_minor == 0)
return TRUE;
/* See how much minor adjustment would happen during
* a major clip. This is a bit tricky because line drawing
* isn't symmetrical when the line passes exactly between
* two pixels, we have to pick which one gets drawn
*/
int32_t adj_min;
if (!c->first)
adj_min = div_ceil(c->e + adjust_major * c->e1, -c->e3);
else
adj_min = div_floor_plus_one(c->e + adjust_major * c->e1, -c->e3);
if (adj_min < adjust_minor) {
if (c->first)
adjust_major = div_ceil(c->e - adjust_minor * c->e3, c->e1);
else
adjust_major = div_floor_plus_one(c->e - adjust_minor * c->e3, c->e1);
} else {
adjust_minor = adj_min;
}
c->e += adjust_major * c->e1 + adjust_minor * c->e3;
c->major += c->sign_major * adjust_major;
c->minor += c->sign_minor * adjust_minor;
return TRUE;
}
void
ao_line(const struct ao_bitmap *dst,
int16_t x1,
int16_t y1,
int16_t x2,
int16_t y2,
uint32_t fill,
uint8_t rop)
{
int16_t adx, ady;
int16_t e, e1, e2, e3;
int16_t signdx = 1, signdy = 1;
int16_t axis;
int16_t len;
struct ao_cc clip_1, clip_2;
struct ao_cbox cbox;
if ((adx = x2 - x1) < 0) {
adx = -adx;
signdx = -1;
}
if ((ady = y2 - y1) < 0) {
ady = -ady;
signdy = -1;
}
if (adx > ady) {
axis = X_AXIS;
e1 = ady << 1;
e2 = e1 - (adx << 1);
e = e1 - adx;
clip_1.major = x1;
clip_1.minor = y1;
clip_2.major = x2;
clip_2.minor = y2;
clip_1.sign_major = signdx;
clip_1.sign_minor = signdy;
cbox.maj1 = 0;
cbox.maj2 = dst->width;
cbox.min1 = 0;
cbox.min2 = dst->height;
} else {
axis = Y_AXIS;
e1 = adx << 1;
e2 = e1 - (ady << 1);
e = e1 - ady;
clip_1.major = y1;
clip_1.minor = x1;
clip_2.major = y2;
clip_2.minor = x2;
clip_1.sign_major = signdy;
clip_1.sign_minor = signdx;
cbox.maj1 = 0;
cbox.maj2 = dst->height;
cbox.min1 = 0;
cbox.min2 = dst->width;
}
e3 = e2 - e1;
e = e - e1;
clip_1.first = TRUE;
clip_2.first = FALSE;
clip_2.e = clip_1.e = e;
clip_2.e1 = clip_1.e1 = e1;
clip_2.e3 = clip_1.e3 = e3;
clip_2.sign_major = -clip_1.sign_major;
clip_2.sign_minor = -clip_1.sign_minor;
if (!ao_clip_line(&clip_1, &cbox))
return;
if (!ao_clip_line(&clip_2, &cbox))
return;
len = clip_1.sign_major * (clip_2.major - clip_1.major) + clip_2.first;
if (len <= 0)
return;
if (adx > ady) {
x1 = clip_1.major;
y1 = clip_1.minor;
} else {
x1 = clip_1.minor;
y1 = clip_1.major;
}
ao_bres(dst,
signdx,
signdy,
axis,
x1,
y1,
clip_1.e, e1, e3, len,
ao_and(rop, fill),
ao_xor(rop, fill));
}
|