/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 | 4.81k | static inline Guchar mul255(Guchar x, Guchar y) { |
25 | 4.81k | int z; |
26 | | |
27 | 4.81k | z = (int)x * (int)y; |
28 | 4.81k | return (Guchar)((z + (z >> 8) + 0x80) >> 8); |
29 | 4.81k | } |
30 | | |
31 | | //------------------------------------------------------------------------ |
32 | | // SplashClip |
33 | | //------------------------------------------------------------------------ |
34 | | |
35 | | SplashClip::SplashClip(int hardXMinA, int hardYMinA, |
36 | 248k | int hardXMaxA, int hardYMaxA) { |
37 | 248k | int w; |
38 | | |
39 | 248k | hardXMin = hardXMinA; |
40 | 248k | hardYMin = hardYMinA; |
41 | 248k | hardXMax = hardXMaxA; |
42 | 248k | hardYMax = hardYMaxA; |
43 | 248k | xMin = hardXMin; |
44 | 248k | yMin = hardYMin; |
45 | 248k | xMax = hardXMax; |
46 | 248k | yMax = hardYMax; |
47 | 248k | intBoundsValid = gFalse; |
48 | 248k | paths = NULL; |
49 | 248k | eo = NULL; |
50 | 248k | scanners = NULL; |
51 | 248k | length = size = 0; |
52 | 248k | isSimple = gTrue; |
53 | 248k | prev = NULL; |
54 | 248k | if ((w = hardXMax + 1) <= 0) { |
55 | 0 | w = 1; |
56 | 0 | } |
57 | 248k | buf = (Guchar *)gmalloc(w); |
58 | 248k | } |
59 | | |
60 | 110k | SplashClip::SplashClip(SplashClip *clip) { |
61 | 110k | int w; |
62 | | |
63 | 110k | hardXMin = clip->hardXMin; |
64 | 110k | hardYMin = clip->hardYMin; |
65 | 110k | hardXMax = clip->hardXMax; |
66 | 110k | hardYMax = clip->hardYMax; |
67 | 110k | xMin = clip->xMin; |
68 | 110k | yMin = clip->yMin; |
69 | 110k | xMax = clip->xMax; |
70 | 110k | yMax = clip->yMax; |
71 | 110k | xMinI = clip->xMinI; |
72 | 110k | yMinI = clip->yMinI; |
73 | 110k | xMaxI = clip->xMaxI; |
74 | 110k | yMaxI = clip->yMaxI; |
75 | 110k | intBoundsValid = clip->intBoundsValid; |
76 | 110k | intBoundsStrokeAdjust = clip->intBoundsStrokeAdjust; |
77 | 110k | paths = NULL; |
78 | 110k | eo = NULL; |
79 | 110k | scanners = NULL; |
80 | 110k | length = size = 0; |
81 | 110k | isSimple = clip->isSimple; |
82 | 110k | prev = clip; |
83 | 110k | if ((w = splashCeil(xMax)) <= 0) { |
84 | 47.6k | w = 1; |
85 | 47.6k | } |
86 | 110k | buf = (Guchar *)gmalloc(w); |
87 | 110k | } |
88 | | |
89 | 359k | SplashClip::~SplashClip() { |
90 | 359k | int i; |
91 | | |
92 | 397k | for (i = 0; i < length; ++i) { |
93 | 38.0k | delete scanners[i]; |
94 | 38.0k | delete paths[i]; |
95 | 38.0k | } |
96 | 359k | gfree(paths); |
97 | 359k | gfree(eo); |
98 | 359k | gfree(scanners); |
99 | 359k | gfree(buf); |
100 | 359k | } |
101 | | |
102 | 38.0k | void SplashClip::grow(int nPaths) { |
103 | 38.0k | if (length + nPaths > size) { |
104 | 32.1k | if (size == 0) { |
105 | 32.1k | size = 32; |
106 | 32.1k | } |
107 | 32.1k | while (size < length + nPaths) { |
108 | 16 | size *= 2; |
109 | 16 | } |
110 | 32.1k | paths = (SplashXPath **)greallocn(paths, size, sizeof(SplashXPath *)); |
111 | 32.1k | eo = (Guchar *)greallocn(eo, size, sizeof(Guchar)); |
112 | 32.1k | scanners = (SplashXPathScanner **) |
113 | 32.1k | greallocn(scanners, size, sizeof(SplashXPathScanner *)); |
114 | 32.1k | } |
115 | 38.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 | 95.1k | SplashCoord x1, SplashCoord y1) { |
159 | 95.1k | if (x0 < x1) { |
160 | 26.1k | if (x0 > xMin) { |
161 | 52 | xMin = x0; |
162 | 52 | intBoundsValid = gFalse; |
163 | 52 | } |
164 | 26.1k | if (x1 < xMax) { |
165 | 6.50k | xMax = x1; |
166 | 6.50k | intBoundsValid = gFalse; |
167 | 6.50k | } |
168 | 68.9k | } else { |
169 | 68.9k | if (x1 > xMin) { |
170 | 14.8k | xMin = x1; |
171 | 14.8k | intBoundsValid = gFalse; |
172 | 14.8k | } |
173 | 68.9k | if (x0 < xMax) { |
174 | 10.6k | xMax = x0; |
175 | 10.6k | intBoundsValid = gFalse; |
176 | 10.6k | } |
177 | 68.9k | } |
178 | 95.1k | if (y0 < y1) { |
179 | 40.0k | if (y0 > yMin) { |
180 | 21 | yMin = y0; |
181 | 21 | intBoundsValid = gFalse; |
182 | 21 | } |
183 | 40.0k | if (y1 < yMax) { |
184 | 4.46k | yMax = y1; |
185 | 4.46k | intBoundsValid = gFalse; |
186 | 4.46k | } |
187 | 55.0k | } else { |
188 | 55.0k | if (y1 > yMin) { |
189 | 4.99k | yMin = y1; |
190 | 4.99k | intBoundsValid = gFalse; |
191 | 4.99k | } |
192 | 55.0k | if (y0 < yMax) { |
193 | 6.68k | yMax = y0; |
194 | 6.68k | intBoundsValid = gFalse; |
195 | 6.68k | } |
196 | 55.0k | } |
197 | 95.1k | return splashOk; |
198 | 95.1k | } |
199 | | |
200 | | SplashError SplashClip::clipToPath(SplashPath *path, SplashCoord *matrix, |
201 | | SplashCoord flatness, GBool eoA, |
202 | | GBool enablePathSimplification, |
203 | 141k | SplashStrokeAdjustMode strokeAdjust) { |
204 | 141k | SplashXPath *xPath; |
205 | 141k | SplashCoord t; |
206 | | |
207 | 141k | xPath = new SplashXPath(path, matrix, flatness, gTrue, |
208 | 141k | enablePathSimplification, |
209 | 141k | strokeAdjust, NULL); |
210 | | |
211 | | // check for an empty path |
212 | 141k | if (xPath->length == 0) { |
213 | 8.62k | xMin = yMin = 1; |
214 | 8.62k | xMax = yMax = 0; |
215 | 8.62k | intBoundsValid = gFalse; |
216 | 8.62k | delete xPath; |
217 | 8.62k | return splashOk; |
218 | 8.62k | } |
219 | | |
220 | | // check for a rectangle |
221 | 133k | if (xPath->isRect) { |
222 | 95.1k | clipToRect(xPath->rectX0, xPath->rectY0, xPath->rectX1, xPath->rectY1); |
223 | 95.1k | delete xPath; |
224 | 95.1k | return splashOk; |
225 | 95.1k | } |
226 | | |
227 | 38.0k | grow(1); |
228 | 38.0k | paths[length] = xPath; |
229 | 38.0k | eo[length] = (Guchar)eoA; |
230 | 38.0k | if ((t = xPath->getXMin()) > xMin) { |
231 | 3.52k | xMin = t; |
232 | 3.52k | } |
233 | 38.0k | if ((t = xPath->getYMin()) > yMin) { |
234 | 9.05k | yMin = t; |
235 | 9.05k | } |
236 | 38.0k | if ((t = xPath->getXMax() + 1) < xMax) { |
237 | 3.39k | xMax = t; |
238 | 3.39k | } |
239 | 38.0k | if ((t = xPath->getYMax() + 1) < yMax) { |
240 | 9.16k | yMax = t; |
241 | 9.16k | } |
242 | 38.0k | intBoundsValid = gFalse; |
243 | 38.0k | scanners[length] = new SplashXPathScanner(xPath, eoA, splashFloor(yMin), |
244 | 38.0k | splashCeil(yMax) - 1); |
245 | 38.0k | ++length; |
246 | 38.0k | isSimple = gFalse; |
247 | | |
248 | 38.0k | return splashOk; |
249 | 133k | } |
250 | | |
251 | | SplashClipResult SplashClip::testRect(int rectXMin, int rectYMin, |
252 | | int rectXMax, int rectYMax, |
253 | 1.93M | 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 | 1.93M | 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.24M | updateIntBounds(strokeAdjust); |
267 | 1.24M | if (xMinI > xMaxI || yMinI > yMaxI) { |
268 | 55.7k | return splashClipAllOutside; |
269 | 55.7k | } |
270 | 1.18M | if (rectXMax + 1 <= xMinI || |
271 | 603k | rectXMin >= xMaxI + 1 || |
272 | 266k | rectYMax + 1 <= yMinI || |
273 | 1.16M | rectYMin >= yMaxI + 1) { |
274 | 1.16M | return splashClipAllOutside; |
275 | 1.16M | } |
276 | 25.8k | if (rectXMin >= xMinI && |
277 | 19.7k | rectXMax <= xMaxI && |
278 | 13.8k | rectYMin >= yMinI && |
279 | 8.88k | rectYMax <= yMaxI) { |
280 | 8.20k | return splashClipAllInside; |
281 | 8.20k | } |
282 | 689k | } else { |
283 | 689k | if (xMin >= xMax || yMin >= yMax) { |
284 | 99.3k | return splashClipAllOutside; |
285 | 99.3k | } |
286 | 589k | if ((SplashCoord)(rectXMax + 1) <= xMin || |
287 | 348k | (SplashCoord)rectXMin >= xMax || |
288 | 9.23k | (SplashCoord)(rectYMax + 1) <= yMin || |
289 | 584k | (SplashCoord)rectYMin >= yMax) { |
290 | 584k | return splashClipAllOutside; |
291 | 584k | } |
292 | 5.39k | if (isSimple && |
293 | 1.50k | (SplashCoord)rectXMin >= xMin && |
294 | 424 | (SplashCoord)(rectXMax + 1) <= xMax && |
295 | 368 | (SplashCoord)rectYMin >= yMin && |
296 | 368 | (SplashCoord)(rectYMax + 1) <= yMax) { |
297 | 368 | return splashClipAllInside; |
298 | 368 | } |
299 | 5.39k | } |
300 | 22.6k | return splashClipPartial; |
301 | 1.93M | } |
302 | | |
303 | | void SplashClip::clipSpan(Guchar *line, int y, int x0, int x1, |
304 | 19.6k | SplashStrokeAdjustMode strokeAdjust) { |
305 | 19.6k | SplashClip *clip; |
306 | 19.6k | SplashCoord d; |
307 | 19.6k | int x0a, x1a, x0b, x1b, x, i; |
308 | | |
309 | 19.6k | updateIntBounds(strokeAdjust); |
310 | | |
311 | | //--- clip to the integer rectangle |
312 | | |
313 | 19.6k | if (y < yMinI || y > yMaxI || |
314 | 19.6k | x1 < xMinI || x0 > xMaxI) { |
315 | 0 | memset(line + x0, 0, x1 - x0 + 1); |
316 | 0 | return; |
317 | 0 | } |
318 | | |
319 | 19.6k | if (x0 > xMinI) { |
320 | 220 | x0a = x0; |
321 | 19.4k | } else { |
322 | 19.4k | x0a = xMinI; |
323 | 19.4k | memset(line + x0, 0, x0a - x0); |
324 | 19.4k | } |
325 | | |
326 | 19.6k | if (x1 < xMaxI) { |
327 | 16 | x1a = x1; |
328 | 19.6k | } else { |
329 | 19.6k | x1a = xMaxI; |
330 | 19.6k | memset(line + x1a + 1, 0, x1 - x1a); |
331 | 19.6k | } |
332 | | |
333 | 19.6k | if (x0a > x1a) { |
334 | 0 | return; |
335 | 0 | } |
336 | | |
337 | | //--- clip to the floating point rectangle |
338 | | // (if stroke adjustment is disabled) |
339 | | |
340 | 19.6k | if (strokeAdjust == splashStrokeAdjustOff) { |
341 | | |
342 | | // clip left edge (xMin) |
343 | 1.88k | if (x0a == xMinI) { |
344 | 1.66k | d = (SplashCoord)(xMinI + 1) - xMin; |
345 | 1.66k | line[x0a] = (Guchar)(int)((SplashCoord)line[x0a] * d); |
346 | 1.66k | } |
347 | | |
348 | | // clip right edge (xMax) |
349 | 1.88k | if (x1a == xMaxI) { |
350 | 1.86k | d = xMax - (SplashCoord)xMaxI; |
351 | 1.86k | line[x1a] = (Guchar)(int)((SplashCoord)line[x1a] * d); |
352 | 1.86k | } |
353 | | |
354 | | // clip top edge (yMin) |
355 | 1.88k | if (y == yMinI) { |
356 | 1.14k | d = (SplashCoord)(yMinI + 1) - yMin; |
357 | 3.03k | for (x = x0a; x <= x1a; ++x) { |
358 | 1.88k | line[x] = (Guchar)(int)((SplashCoord)line[x] * d); |
359 | 1.88k | } |
360 | 1.14k | } |
361 | | |
362 | | // clip bottom edge (yMax) |
363 | 1.88k | if (y == yMaxI) { |
364 | 1.15k | d = yMax - (SplashCoord)yMaxI; |
365 | 3.04k | for (x = x0a; x <= x1a; ++x) { |
366 | 1.89k | line[x] = (Guchar)(int)((SplashCoord)line[x] * d); |
367 | 1.89k | } |
368 | 1.15k | } |
369 | 1.88k | } |
370 | | |
371 | 19.6k | if (isSimple) { |
372 | 17.0k | return; |
373 | 17.0k | } |
374 | | |
375 | | //--- clip to the paths |
376 | | |
377 | 5.59k | for (clip = this; clip; clip = clip->prev) { |
378 | 8.79k | for (i = 0; i < clip->length; ++i) { |
379 | 5.87k | clip->scanners[i]->getSpan(buf, y, x0a, x1a, &x0b, &x1b); |
380 | 5.87k | if (x0a < x0b) { |
381 | 339 | memset(line + x0a, 0, x0b - x0a); |
382 | 339 | } |
383 | 10.6k | for (x = x0b; x <= x1b; ++x) { |
384 | 4.81k | line[x] = mul255(line[x], buf[x]); |
385 | 4.81k | } |
386 | 5.87k | if (x1b < x1a) { |
387 | 865 | memset(line + x1b + 1, 0, x1a - x1b); |
388 | 865 | } |
389 | 5.87k | } |
390 | 2.91k | } |
391 | 2.67k | } |
392 | | |
393 | | GBool SplashClip::clipSpanBinary(Guchar *line, int y, int x0, int x1, |
394 | 2.69k | SplashStrokeAdjustMode strokeAdjust) { |
395 | 2.69k | SplashClip *clip; |
396 | 2.69k | int x0a, x1a, x0b, x1b, x, i; |
397 | 2.69k | Guchar any; |
398 | | |
399 | 2.69k | updateIntBounds(strokeAdjust); |
400 | | |
401 | 2.69k | if (y < yMinI || y > yMaxI || |
402 | 2.69k | 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 | 2.69k | if (x0 > xMinI) { |
410 | 0 | x0a = x0; |
411 | 2.69k | } else { |
412 | 2.69k | x0a = xMinI; |
413 | 2.69k | memset(line + x0, 0, x0a - x0); |
414 | 2.69k | } |
415 | | |
416 | 2.69k | if (x1 < xMaxI) { |
417 | 0 | x1a = x1; |
418 | 2.69k | } else { |
419 | 2.69k | x1a = xMaxI; |
420 | 2.69k | memset(line + x1a + 1, 0, x1 - x1a); |
421 | 2.69k | } |
422 | | |
423 | 2.69k | if (x0a > x1a) { |
424 | 0 | return gFalse; |
425 | 0 | } |
426 | | |
427 | 2.69k | if (isSimple) { |
428 | 1.52k | for (x = x0a; x <= x1a; ++x) { |
429 | 1.52k | if (line[x]) { |
430 | 1.52k | return gTrue; |
431 | 1.52k | } |
432 | 1.52k | } |
433 | 0 | return gFalse; |
434 | 1.52k | } |
435 | | |
436 | 1.17k | any = 0; |
437 | 2.52k | for (clip = this; clip; clip = clip->prev) { |
438 | 3.95k | for (i = 0; i < clip->length; ++i) { |
439 | 2.60k | clip->scanners[i]->getSpanBinary(buf, y, x0a, x1a, &x0b, &x1b); |
440 | 2.60k | if (x0a < x0b) { |
441 | 65 | memset(line + x0a, 0, x0b - x0a); |
442 | 65 | } |
443 | 5.07k | for (x = x0b; x <= x1b; ++x) { |
444 | 2.46k | line[x] &= buf[x]; |
445 | 2.46k | any |= line[x]; |
446 | 2.46k | } |
447 | 2.60k | if (x1b < x1a) { |
448 | 110 | memset(line + x1b + 1, 0, x1a - x1b); |
449 | 110 | } |
450 | 2.60k | } |
451 | 1.34k | } |
452 | | |
453 | 1.17k | return any != 0; |
454 | 2.69k | } |
455 | | |
456 | 61.3k | int SplashClip::getXMinI(SplashStrokeAdjustMode strokeAdjust) { |
457 | 61.3k | updateIntBounds(strokeAdjust); |
458 | 61.3k | return xMinI; |
459 | 61.3k | } |
460 | | |
461 | 61.3k | int SplashClip::getXMaxI(SplashStrokeAdjustMode strokeAdjust) { |
462 | 61.3k | updateIntBounds(strokeAdjust); |
463 | 61.3k | return xMaxI; |
464 | 61.3k | } |
465 | | |
466 | 59.8k | int SplashClip::getYMinI(SplashStrokeAdjustMode strokeAdjust) { |
467 | 59.8k | updateIntBounds(strokeAdjust); |
468 | 59.8k | return yMinI; |
469 | 59.8k | } |
470 | | |
471 | 59.8k | int SplashClip::getYMaxI(SplashStrokeAdjustMode strokeAdjust) { |
472 | 59.8k | updateIntBounds(strokeAdjust); |
473 | 59.8k | return yMaxI; |
474 | 59.8k | } |
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.50M | void SplashClip::updateIntBounds(SplashStrokeAdjustMode strokeAdjust) { |
488 | 1.50M | if (intBoundsValid && strokeAdjust == intBoundsStrokeAdjust) { |
489 | 1.49M | return; |
490 | 1.49M | } |
491 | 15.3k | if (strokeAdjust != splashStrokeAdjustOff && isSimple) { |
492 | 13.4k | splashStrokeAdjust(xMin, xMax, &xMinI, &xMaxI, strokeAdjust); |
493 | 13.4k | splashStrokeAdjust(yMin, yMax, &yMinI, &yMaxI, strokeAdjust); |
494 | 13.4k | } else { |
495 | 1.91k | xMinI = splashFloor(xMin); |
496 | 1.91k | yMinI = splashFloor(yMin); |
497 | 1.91k | xMaxI = splashCeil(xMax); |
498 | 1.91k | yMaxI = splashCeil(yMax); |
499 | 1.91k | } |
500 | 15.3k | if (xMinI < hardXMin) { |
501 | 0 | xMinI = hardXMin; |
502 | 0 | } |
503 | 15.3k | if (yMinI < hardYMin) { |
504 | 0 | yMinI = hardYMin; |
505 | 0 | } |
506 | 15.3k | if (xMaxI > hardXMax) { |
507 | 0 | xMaxI = hardXMax; |
508 | 0 | } |
509 | 15.3k | if (yMaxI > hardYMax) { |
510 | 0 | yMaxI = hardYMax; |
511 | 0 | } |
512 | | // the clipping code uses [xMinI, xMaxI] instead of [xMinI, xMaxI) |
513 | 15.3k | --xMaxI; |
514 | 15.3k | --yMaxI; |
515 | 15.3k | intBoundsValid = gTrue; |
516 | 15.3k | intBoundsStrokeAdjust = strokeAdjust; |
517 | 15.3k | } |