/src/xpdf-4.06/splash/SplashClip.cc
Line | Count | Source |
1 | | //======================================================================== |
2 | | // |
3 | | // SplashClip.cc |
4 | | // |
5 | | // Copyright 2003-2013 Glyph & Cog, LLC |
6 | | // |
7 | | //======================================================================== |
8 | | |
9 | | #include <aconf.h> |
10 | | |
11 | | #include <stdlib.h> |
12 | | #include <string.h> |
13 | | #include "gmem.h" |
14 | | #include "gmempp.h" |
15 | | #include "SplashErrorCodes.h" |
16 | | #include "SplashPath.h" |
17 | | #include "SplashXPath.h" |
18 | | #include "SplashXPathScanner.h" |
19 | | #include "SplashClip.h" |
20 | | |
21 | | //------------------------------------------------------------------------ |
22 | | |
23 | | // Compute x * y / 255, where x and y are in [0, 255]. |
24 | 61.7k | static inline Guchar mul255(Guchar x, Guchar y) { |
25 | 61.7k | int z; |
26 | | |
27 | 61.7k | z = (int)x * (int)y; |
28 | 61.7k | return (Guchar)((z + (z >> 8) + 0x80) >> 8); |
29 | 61.7k | } |
30 | | |
31 | | //------------------------------------------------------------------------ |
32 | | // SplashClip |
33 | | //------------------------------------------------------------------------ |
34 | | |
35 | | SplashClip::SplashClip(int hardXMinA, int hardYMinA, |
36 | 335k | int hardXMaxA, int hardYMaxA) { |
37 | 335k | int w; |
38 | | |
39 | 335k | hardXMin = hardXMinA; |
40 | 335k | hardYMin = hardYMinA; |
41 | 335k | hardXMax = hardXMaxA; |
42 | 335k | hardYMax = hardYMaxA; |
43 | 335k | xMin = hardXMin; |
44 | 335k | yMin = hardYMin; |
45 | 335k | xMax = hardXMax; |
46 | 335k | yMax = hardYMax; |
47 | 335k | intBoundsValid = gFalse; |
48 | 335k | paths = NULL; |
49 | 335k | eo = NULL; |
50 | 335k | scanners = NULL; |
51 | 335k | length = size = 0; |
52 | 335k | isSimple = gTrue; |
53 | 335k | prev = NULL; |
54 | 335k | if ((w = hardXMax + 1) <= 0) { |
55 | 0 | w = 1; |
56 | 0 | } |
57 | 335k | buf = (Guchar *)gmalloc(w); |
58 | 335k | } |
59 | | |
60 | 147k | SplashClip::SplashClip(SplashClip *clip) { |
61 | 147k | int w; |
62 | | |
63 | 147k | hardXMin = clip->hardXMin; |
64 | 147k | hardYMin = clip->hardYMin; |
65 | 147k | hardXMax = clip->hardXMax; |
66 | 147k | hardYMax = clip->hardYMax; |
67 | 147k | xMin = clip->xMin; |
68 | 147k | yMin = clip->yMin; |
69 | 147k | xMax = clip->xMax; |
70 | 147k | yMax = clip->yMax; |
71 | 147k | xMinI = clip->xMinI; |
72 | 147k | yMinI = clip->yMinI; |
73 | 147k | xMaxI = clip->xMaxI; |
74 | 147k | yMaxI = clip->yMaxI; |
75 | 147k | intBoundsValid = clip->intBoundsValid; |
76 | 147k | intBoundsStrokeAdjust = clip->intBoundsStrokeAdjust; |
77 | 147k | paths = NULL; |
78 | 147k | eo = NULL; |
79 | 147k | scanners = NULL; |
80 | 147k | length = size = 0; |
81 | 147k | isSimple = clip->isSimple; |
82 | 147k | prev = clip; |
83 | 147k | if ((w = splashCeil(xMax)) <= 0) { |
84 | 41.0k | w = 1; |
85 | 41.0k | } |
86 | 147k | buf = (Guchar *)gmalloc(w); |
87 | 147k | } |
88 | | |
89 | 482k | SplashClip::~SplashClip() { |
90 | 482k | int i; |
91 | | |
92 | 568k | for (i = 0; i < length; ++i) { |
93 | 86.0k | delete scanners[i]; |
94 | 86.0k | delete paths[i]; |
95 | 86.0k | } |
96 | 482k | gfree(paths); |
97 | 482k | gfree(eo); |
98 | 482k | gfree(scanners); |
99 | 482k | gfree(buf); |
100 | 482k | } |
101 | | |
102 | 86.0k | void SplashClip::grow(int nPaths) { |
103 | 86.0k | if (length + nPaths > size) { |
104 | 78.7k | if (size == 0) { |
105 | 78.6k | size = 32; |
106 | 78.6k | } |
107 | 78.7k | while (size < length + nPaths) { |
108 | 38 | size *= 2; |
109 | 38 | } |
110 | 78.7k | paths = (SplashXPath **)greallocn(paths, size, sizeof(SplashXPath *)); |
111 | 78.7k | eo = (Guchar *)greallocn(eo, size, sizeof(Guchar)); |
112 | 78.7k | scanners = (SplashXPathScanner **) |
113 | 78.7k | greallocn(scanners, size, sizeof(SplashXPathScanner *)); |
114 | 78.7k | } |
115 | 86.0k | } |
116 | | |
117 | | void SplashClip::resetToRect(SplashCoord x0, SplashCoord y0, |
118 | 0 | SplashCoord x1, SplashCoord y1) { |
119 | 0 | int w, i; |
120 | |
|
121 | 0 | for (i = 0; i < length; ++i) { |
122 | 0 | delete paths[i]; |
123 | 0 | delete scanners[i]; |
124 | 0 | } |
125 | 0 | gfree(paths); |
126 | 0 | gfree(eo); |
127 | 0 | gfree(scanners); |
128 | 0 | gfree(buf); |
129 | 0 | paths = NULL; |
130 | 0 | eo = NULL; |
131 | 0 | scanners = NULL; |
132 | 0 | length = size = 0; |
133 | 0 | isSimple = gTrue; |
134 | 0 | prev = NULL; |
135 | |
|
136 | 0 | if (x0 < x1) { |
137 | 0 | xMin = x0; |
138 | 0 | xMax = x1; |
139 | 0 | } else { |
140 | 0 | xMin = x1; |
141 | 0 | xMax = x0; |
142 | 0 | } |
143 | 0 | if (y0 < y1) { |
144 | 0 | yMin = y0; |
145 | 0 | yMax = y1; |
146 | 0 | } else { |
147 | 0 | yMin = y1; |
148 | 0 | yMax = y0; |
149 | 0 | } |
150 | 0 | intBoundsValid = gFalse; |
151 | 0 | if ((w = splashCeil(xMax)) <= 0) { |
152 | 0 | w = 1; |
153 | 0 | } |
154 | 0 | buf = (Guchar *)gmalloc(w); |
155 | 0 | } |
156 | | |
157 | | SplashError SplashClip::clipToRect(SplashCoord x0, SplashCoord y0, |
158 | 82.5k | SplashCoord x1, SplashCoord y1) { |
159 | 82.5k | if (x0 < x1) { |
160 | 29.4k | if (x0 > xMin) { |
161 | 51 | xMin = x0; |
162 | 51 | intBoundsValid = gFalse; |
163 | 51 | } |
164 | 29.4k | if (x1 < xMax) { |
165 | 5.60k | xMax = x1; |
166 | 5.60k | intBoundsValid = gFalse; |
167 | 5.60k | } |
168 | 53.1k | } else { |
169 | 53.1k | if (x1 > xMin) { |
170 | 13.4k | xMin = x1; |
171 | 13.4k | intBoundsValid = gFalse; |
172 | 13.4k | } |
173 | 53.1k | if (x0 < xMax) { |
174 | 10.9k | xMax = x0; |
175 | 10.9k | intBoundsValid = gFalse; |
176 | 10.9k | } |
177 | 53.1k | } |
178 | 82.5k | if (y0 < y1) { |
179 | 43.1k | if (y0 > yMin) { |
180 | 9 | yMin = y0; |
181 | 9 | intBoundsValid = gFalse; |
182 | 9 | } |
183 | 43.1k | if (y1 < yMax) { |
184 | 3.42k | yMax = y1; |
185 | 3.42k | intBoundsValid = gFalse; |
186 | 3.42k | } |
187 | 43.1k | } else { |
188 | 39.4k | if (y1 > yMin) { |
189 | 5.46k | yMin = y1; |
190 | 5.46k | intBoundsValid = gFalse; |
191 | 5.46k | } |
192 | 39.4k | if (y0 < yMax) { |
193 | 5.11k | yMax = y0; |
194 | 5.11k | intBoundsValid = gFalse; |
195 | 5.11k | } |
196 | 39.4k | } |
197 | 82.5k | return splashOk; |
198 | 82.5k | } |
199 | | |
200 | | SplashError SplashClip::clipToPath(SplashPath *path, SplashCoord *matrix, |
201 | | SplashCoord flatness, GBool eoA, |
202 | | GBool enablePathSimplification, |
203 | 179k | SplashStrokeAdjustMode strokeAdjust) { |
204 | 179k | SplashXPath *xPath; |
205 | 179k | SplashCoord t; |
206 | | |
207 | 179k | xPath = new SplashXPath(path, matrix, flatness, gTrue, |
208 | 179k | enablePathSimplification, |
209 | 179k | strokeAdjust, NULL); |
210 | | |
211 | | // check for an empty path |
212 | 179k | if (xPath->length == 0) { |
213 | 11.3k | xMin = yMin = 1; |
214 | 11.3k | xMax = yMax = 0; |
215 | 11.3k | intBoundsValid = gFalse; |
216 | 11.3k | delete xPath; |
217 | 11.3k | return splashOk; |
218 | 11.3k | } |
219 | | |
220 | | // check for a rectangle |
221 | 168k | if (xPath->isRect) { |
222 | 82.5k | clipToRect(xPath->rectX0, xPath->rectY0, xPath->rectX1, xPath->rectY1); |
223 | 82.5k | delete xPath; |
224 | 82.5k | return splashOk; |
225 | 82.5k | } |
226 | | |
227 | 86.0k | grow(1); |
228 | 86.0k | paths[length] = xPath; |
229 | 86.0k | eo[length] = (Guchar)eoA; |
230 | 86.0k | if ((t = xPath->getXMin()) > xMin) { |
231 | 2.42k | xMin = t; |
232 | 2.42k | } |
233 | 86.0k | if ((t = xPath->getYMin()) > yMin) { |
234 | 9.80k | yMin = t; |
235 | 9.80k | } |
236 | 86.0k | if ((t = xPath->getXMax() + 1) < xMax) { |
237 | 49.5k | xMax = t; |
238 | 49.5k | } |
239 | 86.0k | if ((t = xPath->getYMax() + 1) < yMax) { |
240 | 55.5k | yMax = t; |
241 | 55.5k | } |
242 | 86.0k | intBoundsValid = gFalse; |
243 | 86.0k | scanners[length] = new SplashXPathScanner(xPath, eoA, splashFloor(yMin), |
244 | 86.0k | splashCeil(yMax) - 1); |
245 | 86.0k | ++length; |
246 | 86.0k | isSimple = gFalse; |
247 | | |
248 | 86.0k | return splashOk; |
249 | 168k | } |
250 | | |
251 | | SplashClipResult SplashClip::testRect(int rectXMin, int rectYMin, |
252 | | int rectXMax, int rectYMax, |
253 | 2.86M | SplashStrokeAdjustMode strokeAdjust) { |
254 | | // In general, this function tests the rectangle: |
255 | | // x = [rectXMin, rectXMax + 1) (note: coords are ints) |
256 | | // y = [rectYMin, rectYMax + 1) |
257 | | // against the clipping region: |
258 | | // x = [xMin, xMax) (note: coords are fp) |
259 | | // y = [yMin, yMax) |
260 | | |
261 | 2.86M | if (strokeAdjust != splashStrokeAdjustOff && isSimple) { |
262 | | // special case for stroke adjustment with a simple clipping |
263 | | // rectangle -- the clipping region is: |
264 | | // x = [xMinI, xMaxI + 1) |
265 | | // y = [yMinI, yMaxI + 1) |
266 | 1.32M | updateIntBounds(strokeAdjust); |
267 | 1.32M | if (xMinI > xMaxI || yMinI > yMaxI) { |
268 | 288k | return splashClipAllOutside; |
269 | 288k | } |
270 | 1.03M | if (rectXMax + 1 <= xMinI || |
271 | 443k | rectXMin >= xMaxI + 1 || |
272 | 271k | rectYMax + 1 <= yMinI || |
273 | 1.01M | rectYMin >= yMaxI + 1) { |
274 | 1.01M | return splashClipAllOutside; |
275 | 1.01M | } |
276 | 21.8k | if (rectXMin >= xMinI && |
277 | 16.2k | rectXMax <= xMaxI && |
278 | 11.3k | rectYMin >= yMinI && |
279 | 6.38k | rectYMax <= yMaxI) { |
280 | 5.48k | return splashClipAllInside; |
281 | 5.48k | } |
282 | 1.53M | } else { |
283 | 1.53M | if (xMin >= xMax || yMin >= yMax) { |
284 | 911k | return splashClipAllOutside; |
285 | 911k | } |
286 | 627k | if ((SplashCoord)(rectXMax + 1) <= xMin || |
287 | 219k | (SplashCoord)rectXMin >= xMax || |
288 | 25.2k | (SplashCoord)(rectYMax + 1) <= yMin || |
289 | 615k | (SplashCoord)rectYMin >= yMax) { |
290 | 615k | return splashClipAllOutside; |
291 | 615k | } |
292 | 12.7k | if (isSimple && |
293 | 7.07k | (SplashCoord)rectXMin >= xMin && |
294 | 2.72k | (SplashCoord)(rectXMax + 1) <= xMax && |
295 | 2.70k | (SplashCoord)rectYMin >= yMin && |
296 | 261 | (SplashCoord)(rectYMax + 1) <= yMax) { |
297 | 258 | return splashClipAllInside; |
298 | 258 | } |
299 | 12.7k | } |
300 | 28.8k | return splashClipPartial; |
301 | 2.86M | } |
302 | | |
303 | | void SplashClip::clipSpan(Guchar *line, int y, int x0, int x1, |
304 | 23.2k | SplashStrokeAdjustMode strokeAdjust) { |
305 | 23.2k | SplashClip *clip; |
306 | 23.2k | SplashCoord d; |
307 | 23.2k | int x0a, x1a, x0b, x1b, x, i; |
308 | | |
309 | 23.2k | updateIntBounds(strokeAdjust); |
310 | | |
311 | | //--- clip to the integer rectangle |
312 | | |
313 | 23.2k | if (y < yMinI || y > yMaxI || |
314 | 23.2k | x1 < xMinI || x0 > xMaxI) { |
315 | 0 | memset(line + x0, 0, x1 - x0 + 1); |
316 | 0 | return; |
317 | 0 | } |
318 | | |
319 | 23.2k | if (x0 > xMinI) { |
320 | 320 | x0a = x0; |
321 | 22.8k | } else { |
322 | 22.8k | x0a = xMinI; |
323 | 22.8k | memset(line + x0, 0, x0a - x0); |
324 | 22.8k | } |
325 | | |
326 | 23.2k | if (x1 < xMaxI) { |
327 | 16 | x1a = x1; |
328 | 23.1k | } else { |
329 | 23.1k | x1a = xMaxI; |
330 | 23.1k | memset(line + x1a + 1, 0, x1 - x1a); |
331 | 23.1k | } |
332 | | |
333 | 23.2k | if (x0a > x1a) { |
334 | 0 | return; |
335 | 0 | } |
336 | | |
337 | | //--- clip to the floating point rectangle |
338 | | // (if stroke adjustment is disabled) |
339 | | |
340 | 23.2k | if (strokeAdjust == splashStrokeAdjustOff) { |
341 | | |
342 | | // clip left edge (xMin) |
343 | 6.81k | if (x0a == xMinI) { |
344 | 6.49k | d = (SplashCoord)(xMinI + 1) - xMin; |
345 | 6.49k | line[x0a] = (Guchar)(int)((SplashCoord)line[x0a] * d); |
346 | 6.49k | } |
347 | | |
348 | | // clip right edge (xMax) |
349 | 6.81k | if (x1a == xMaxI) { |
350 | 6.79k | d = xMax - (SplashCoord)xMaxI; |
351 | 6.79k | line[x1a] = (Guchar)(int)((SplashCoord)line[x1a] * d); |
352 | 6.79k | } |
353 | | |
354 | | // clip top edge (yMin) |
355 | 6.81k | if (y == yMinI) { |
356 | 6.39k | d = (SplashCoord)(yMinI + 1) - yMin; |
357 | 13.3k | for (x = x0a; x <= x1a; ++x) { |
358 | 6.92k | line[x] = (Guchar)(int)((SplashCoord)line[x] * d); |
359 | 6.92k | } |
360 | 6.39k | } |
361 | | |
362 | | // clip bottom edge (yMax) |
363 | 6.81k | if (y == yMaxI) { |
364 | 6.38k | d = yMax - (SplashCoord)yMaxI; |
365 | 13.2k | for (x = x0a; x <= x1a; ++x) { |
366 | 6.81k | line[x] = (Guchar)(int)((SplashCoord)line[x] * d); |
367 | 6.81k | } |
368 | 6.38k | } |
369 | 6.81k | } |
370 | | |
371 | 23.2k | if (isSimple) { |
372 | 18.9k | return; |
373 | 18.9k | } |
374 | | |
375 | | //--- clip to the paths |
376 | | |
377 | 9.11k | for (clip = this; clip; clip = clip->prev) { |
378 | 68.5k | for (i = 0; i < clip->length; ++i) { |
379 | 63.7k | clip->scanners[i]->getSpan(buf, y, x0a, x1a, &x0b, &x1b); |
380 | 63.7k | if (x0a < x0b) { |
381 | 1.90k | memset(line + x0a, 0, x0b - x0a); |
382 | 1.90k | } |
383 | 125k | for (x = x0b; x <= x1b; ++x) { |
384 | 61.7k | line[x] = mul255(line[x], buf[x]); |
385 | 61.7k | } |
386 | 63.7k | if (x1b < x1a) { |
387 | 382 | memset(line + x1b + 1, 0, x1a - x1b); |
388 | 382 | } |
389 | 63.7k | } |
390 | 4.84k | } |
391 | 4.27k | } |
392 | | |
393 | | GBool SplashClip::clipSpanBinary(Guchar *line, int y, int x0, int x1, |
394 | 4.59k | SplashStrokeAdjustMode strokeAdjust) { |
395 | 4.59k | SplashClip *clip; |
396 | 4.59k | int x0a, x1a, x0b, x1b, x, i; |
397 | 4.59k | Guchar any; |
398 | | |
399 | 4.59k | updateIntBounds(strokeAdjust); |
400 | | |
401 | 4.59k | if (y < yMinI || y > yMaxI || |
402 | 4.59k | x1 < xMinI || x0 > xMaxI) { |
403 | 0 | if (x0 <= x1) { |
404 | 0 | memset(line + x0, 0, x1 - x0 + 1); |
405 | 0 | } |
406 | 0 | return gFalse; |
407 | 0 | } |
408 | | |
409 | 4.59k | if (x0 > xMinI) { |
410 | 0 | x0a = x0; |
411 | 4.59k | } else { |
412 | 4.59k | x0a = xMinI; |
413 | 4.59k | memset(line + x0, 0, x0a - x0); |
414 | 4.59k | } |
415 | | |
416 | 4.59k | if (x1 < xMaxI) { |
417 | 0 | x1a = x1; |
418 | 4.59k | } else { |
419 | 4.59k | x1a = xMaxI; |
420 | 4.59k | memset(line + x1a + 1, 0, x1 - x1a); |
421 | 4.59k | } |
422 | | |
423 | 4.59k | if (x0a > x1a) { |
424 | 0 | return gFalse; |
425 | 0 | } |
426 | | |
427 | 4.59k | if (isSimple) { |
428 | 3.64k | for (x = x0a; x <= x1a; ++x) { |
429 | 3.64k | if (line[x]) { |
430 | 3.64k | return gTrue; |
431 | 3.64k | } |
432 | 3.64k | } |
433 | 0 | return gFalse; |
434 | 3.64k | } |
435 | | |
436 | 952 | any = 0; |
437 | 2.12k | for (clip = this; clip; clip = clip->prev) { |
438 | 4.61k | for (i = 0; i < clip->length; ++i) { |
439 | 3.44k | clip->scanners[i]->getSpanBinary(buf, y, x0a, x1a, &x0b, &x1b); |
440 | 3.44k | if (x0a < x0b) { |
441 | 82 | memset(line + x0a, 0, x0b - x0a); |
442 | 82 | } |
443 | 6.72k | for (x = x0b; x <= x1b; ++x) { |
444 | 3.28k | line[x] &= buf[x]; |
445 | 3.28k | any |= line[x]; |
446 | 3.28k | } |
447 | 3.44k | if (x1b < x1a) { |
448 | 148 | memset(line + x1b + 1, 0, x1a - x1b); |
449 | 148 | } |
450 | 3.44k | } |
451 | 1.16k | } |
452 | | |
453 | 952 | return any != 0; |
454 | 4.59k | } |
455 | | |
456 | 60.5k | int SplashClip::getXMinI(SplashStrokeAdjustMode strokeAdjust) { |
457 | 60.5k | updateIntBounds(strokeAdjust); |
458 | 60.5k | return xMinI; |
459 | 60.5k | } |
460 | | |
461 | 60.5k | int SplashClip::getXMaxI(SplashStrokeAdjustMode strokeAdjust) { |
462 | 60.5k | updateIntBounds(strokeAdjust); |
463 | 60.5k | return xMaxI; |
464 | 60.5k | } |
465 | | |
466 | 59.2k | int SplashClip::getYMinI(SplashStrokeAdjustMode strokeAdjust) { |
467 | 59.2k | updateIntBounds(strokeAdjust); |
468 | 59.2k | return yMinI; |
469 | 59.2k | } |
470 | | |
471 | 59.2k | int SplashClip::getYMaxI(SplashStrokeAdjustMode strokeAdjust) { |
472 | 59.2k | updateIntBounds(strokeAdjust); |
473 | 59.2k | return yMaxI; |
474 | 59.2k | } |
475 | | |
476 | 0 | int SplashClip::getNumPaths() { |
477 | 0 | SplashClip *clip; |
478 | 0 | int n; |
479 | |
|
480 | 0 | n = 0; |
481 | 0 | for (clip = this; clip; clip = clip->prev) { |
482 | 0 | n += clip->length; |
483 | 0 | } |
484 | 0 | return n; |
485 | 0 | } |
486 | | |
487 | 1.59M | void SplashClip::updateIntBounds(SplashStrokeAdjustMode strokeAdjust) { |
488 | 1.59M | if (intBoundsValid && strokeAdjust == intBoundsStrokeAdjust) { |
489 | 1.57M | return; |
490 | 1.57M | } |
491 | 14.3k | if (strokeAdjust != splashStrokeAdjustOff && isSimple) { |
492 | 11.7k | splashStrokeAdjust(xMin, xMax, &xMinI, &xMaxI, strokeAdjust); |
493 | 11.7k | splashStrokeAdjust(yMin, yMax, &yMinI, &yMaxI, strokeAdjust); |
494 | 11.7k | } else { |
495 | 2.59k | xMinI = splashFloor(xMin); |
496 | 2.59k | yMinI = splashFloor(yMin); |
497 | 2.59k | xMaxI = splashCeil(xMax); |
498 | 2.59k | yMaxI = splashCeil(yMax); |
499 | 2.59k | } |
500 | 14.3k | if (xMinI < hardXMin) { |
501 | 0 | xMinI = hardXMin; |
502 | 0 | } |
503 | 14.3k | if (yMinI < hardYMin) { |
504 | 0 | yMinI = hardYMin; |
505 | 0 | } |
506 | 14.3k | if (xMaxI > hardXMax) { |
507 | 0 | xMaxI = hardXMax; |
508 | 0 | } |
509 | 14.3k | if (yMaxI > hardYMax) { |
510 | 0 | yMaxI = hardYMax; |
511 | 0 | } |
512 | | // the clipping code uses [xMinI, xMaxI] instead of [xMinI, xMaxI) |
513 | 14.3k | --xMaxI; |
514 | 14.3k | --yMaxI; |
515 | 14.3k | intBoundsValid = gTrue; |
516 | 14.3k | intBoundsStrokeAdjust = strokeAdjust; |
517 | 14.3k | } |