/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 | 7.17k | static inline Guchar mul255(Guchar x, Guchar y) { |
25 | 7.17k | int z; |
26 | | |
27 | 7.17k | z = (int)x * (int)y; |
28 | 7.17k | return (Guchar)((z + (z >> 8) + 0x80) >> 8); |
29 | 7.17k | } |
30 | | |
31 | | //------------------------------------------------------------------------ |
32 | | // SplashClip |
33 | | //------------------------------------------------------------------------ |
34 | | |
35 | | SplashClip::SplashClip(int hardXMinA, int hardYMinA, |
36 | 216k | int hardXMaxA, int hardYMaxA) { |
37 | 216k | int w; |
38 | | |
39 | 216k | hardXMin = hardXMinA; |
40 | 216k | hardYMin = hardYMinA; |
41 | 216k | hardXMax = hardXMaxA; |
42 | 216k | hardYMax = hardYMaxA; |
43 | 216k | xMin = hardXMin; |
44 | 216k | yMin = hardYMin; |
45 | 216k | xMax = hardXMax; |
46 | 216k | yMax = hardYMax; |
47 | 216k | intBoundsValid = gFalse; |
48 | 216k | paths = NULL; |
49 | 216k | eo = NULL; |
50 | 216k | scanners = NULL; |
51 | 216k | length = size = 0; |
52 | 216k | isSimple = gTrue; |
53 | 216k | prev = NULL; |
54 | 216k | if ((w = hardXMax + 1) <= 0) { |
55 | 0 | w = 1; |
56 | 0 | } |
57 | 216k | buf = (Guchar *)gmalloc(w); |
58 | 216k | } |
59 | | |
60 | 128k | SplashClip::SplashClip(SplashClip *clip) { |
61 | 128k | int w; |
62 | | |
63 | 128k | hardXMin = clip->hardXMin; |
64 | 128k | hardYMin = clip->hardYMin; |
65 | 128k | hardXMax = clip->hardXMax; |
66 | 128k | hardYMax = clip->hardYMax; |
67 | 128k | xMin = clip->xMin; |
68 | 128k | yMin = clip->yMin; |
69 | 128k | xMax = clip->xMax; |
70 | 128k | yMax = clip->yMax; |
71 | 128k | xMinI = clip->xMinI; |
72 | 128k | yMinI = clip->yMinI; |
73 | 128k | xMaxI = clip->xMaxI; |
74 | 128k | yMaxI = clip->yMaxI; |
75 | 128k | intBoundsValid = clip->intBoundsValid; |
76 | 128k | intBoundsStrokeAdjust = clip->intBoundsStrokeAdjust; |
77 | 128k | paths = NULL; |
78 | 128k | eo = NULL; |
79 | 128k | scanners = NULL; |
80 | 128k | length = size = 0; |
81 | 128k | isSimple = clip->isSimple; |
82 | 128k | prev = clip; |
83 | 128k | if ((w = splashCeil(xMax)) <= 0) { |
84 | 54.8k | w = 1; |
85 | 54.8k | } |
86 | 128k | buf = (Guchar *)gmalloc(w); |
87 | 128k | } |
88 | | |
89 | 345k | SplashClip::~SplashClip() { |
90 | 345k | int i; |
91 | | |
92 | 396k | for (i = 0; i < length; ++i) { |
93 | 50.6k | delete scanners[i]; |
94 | 50.6k | delete paths[i]; |
95 | 50.6k | } |
96 | 345k | gfree(paths); |
97 | 345k | gfree(eo); |
98 | 345k | gfree(scanners); |
99 | 345k | gfree(buf); |
100 | 345k | } |
101 | | |
102 | 50.6k | void SplashClip::grow(int nPaths) { |
103 | 50.6k | if (length + nPaths > size) { |
104 | 42.5k | if (size == 0) { |
105 | 42.4k | size = 32; |
106 | 42.4k | } |
107 | 42.5k | while (size < length + nPaths) { |
108 | 21 | size *= 2; |
109 | 21 | } |
110 | 42.5k | paths = (SplashXPath **)greallocn(paths, size, sizeof(SplashXPath *)); |
111 | 42.5k | eo = (Guchar *)greallocn(eo, size, sizeof(Guchar)); |
112 | 42.5k | scanners = (SplashXPathScanner **) |
113 | 42.5k | greallocn(scanners, size, sizeof(SplashXPathScanner *)); |
114 | 42.5k | } |
115 | 50.6k | } |
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 | 113k | SplashCoord x1, SplashCoord y1) { |
159 | 113k | if (x0 < x1) { |
160 | 37.3k | if (x0 > xMin) { |
161 | 78 | xMin = x0; |
162 | 78 | intBoundsValid = gFalse; |
163 | 78 | } |
164 | 37.3k | if (x1 < xMax) { |
165 | 11.7k | xMax = x1; |
166 | 11.7k | intBoundsValid = gFalse; |
167 | 11.7k | } |
168 | 76.3k | } else { |
169 | 76.3k | if (x1 > xMin) { |
170 | 15.1k | xMin = x1; |
171 | 15.1k | intBoundsValid = gFalse; |
172 | 15.1k | } |
173 | 76.3k | if (x0 < xMax) { |
174 | 12.8k | xMax = x0; |
175 | 12.8k | intBoundsValid = gFalse; |
176 | 12.8k | } |
177 | 76.3k | } |
178 | 113k | if (y0 < y1) { |
179 | 52.9k | if (y0 > yMin) { |
180 | 22 | yMin = y0; |
181 | 22 | intBoundsValid = gFalse; |
182 | 22 | } |
183 | 52.9k | if (y1 < yMax) { |
184 | 6.16k | yMax = y1; |
185 | 6.16k | intBoundsValid = gFalse; |
186 | 6.16k | } |
187 | 60.7k | } else { |
188 | 60.7k | if (y1 > yMin) { |
189 | 5.63k | yMin = y1; |
190 | 5.63k | intBoundsValid = gFalse; |
191 | 5.63k | } |
192 | 60.7k | if (y0 < yMax) { |
193 | 7.95k | yMax = y0; |
194 | 7.95k | intBoundsValid = gFalse; |
195 | 7.95k | } |
196 | 60.7k | } |
197 | 113k | return splashOk; |
198 | 113k | } |
199 | | |
200 | | SplashError SplashClip::clipToPath(SplashPath *path, SplashCoord *matrix, |
201 | | SplashCoord flatness, GBool eoA, |
202 | | GBool enablePathSimplification, |
203 | 176k | SplashStrokeAdjustMode strokeAdjust) { |
204 | 176k | SplashXPath *xPath; |
205 | 176k | SplashCoord t; |
206 | | |
207 | 176k | xPath = new SplashXPath(path, matrix, flatness, gTrue, |
208 | 176k | enablePathSimplification, |
209 | 176k | strokeAdjust, NULL); |
210 | | |
211 | | // check for an empty path |
212 | 176k | if (xPath->length == 0) { |
213 | 11.8k | xMin = yMin = 1; |
214 | 11.8k | xMax = yMax = 0; |
215 | 11.8k | intBoundsValid = gFalse; |
216 | 11.8k | delete xPath; |
217 | 11.8k | return splashOk; |
218 | 11.8k | } |
219 | | |
220 | | // check for a rectangle |
221 | 164k | if (xPath->isRect) { |
222 | 113k | clipToRect(xPath->rectX0, xPath->rectY0, xPath->rectX1, xPath->rectY1); |
223 | 113k | delete xPath; |
224 | 113k | return splashOk; |
225 | 113k | } |
226 | | |
227 | 50.6k | grow(1); |
228 | 50.6k | paths[length] = xPath; |
229 | 50.6k | eo[length] = (Guchar)eoA; |
230 | 50.6k | if ((t = xPath->getXMin()) > xMin) { |
231 | 3.41k | xMin = t; |
232 | 3.41k | } |
233 | 50.6k | if ((t = xPath->getYMin()) > yMin) { |
234 | 12.8k | yMin = t; |
235 | 12.8k | } |
236 | 50.6k | if ((t = xPath->getXMax() + 1) < xMax) { |
237 | 4.97k | xMax = t; |
238 | 4.97k | } |
239 | 50.6k | if ((t = xPath->getYMax() + 1) < yMax) { |
240 | 13.7k | yMax = t; |
241 | 13.7k | } |
242 | 50.6k | intBoundsValid = gFalse; |
243 | 50.6k | scanners[length] = new SplashXPathScanner(xPath, eoA, splashFloor(yMin), |
244 | 50.6k | splashCeil(yMax) - 1); |
245 | 50.6k | ++length; |
246 | 50.6k | isSimple = gFalse; |
247 | | |
248 | 50.6k | return splashOk; |
249 | 164k | } |
250 | | |
251 | | SplashClipResult SplashClip::testRect(int rectXMin, int rectYMin, |
252 | | int rectXMax, int rectYMax, |
253 | 2.26M | 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.26M | 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.34M | updateIntBounds(strokeAdjust); |
267 | 1.34M | if (xMinI > xMaxI || yMinI > yMaxI) { |
268 | 31.3k | return splashClipAllOutside; |
269 | 31.3k | } |
270 | 1.31M | if (rectXMax + 1 <= xMinI || |
271 | 739k | rectXMin >= xMaxI + 1 || |
272 | 477k | rectYMax + 1 <= yMinI || |
273 | 1.28M | rectYMin >= yMaxI + 1) { |
274 | 1.28M | return splashClipAllOutside; |
275 | 1.28M | } |
276 | 28.8k | if (rectXMin >= xMinI && |
277 | 22.3k | rectXMax <= xMaxI && |
278 | 15.9k | rectYMin >= yMinI && |
279 | 6.57k | rectYMax <= yMaxI) { |
280 | 5.81k | return splashClipAllInside; |
281 | 5.81k | } |
282 | 923k | } else { |
283 | 923k | if (xMin >= xMax || yMin >= yMax) { |
284 | 232k | return splashClipAllOutside; |
285 | 232k | } |
286 | 691k | if ((SplashCoord)(rectXMax + 1) <= xMin || |
287 | 433k | (SplashCoord)rectXMin >= xMax || |
288 | 79.7k | (SplashCoord)(rectYMax + 1) <= yMin || |
289 | 682k | (SplashCoord)rectYMin >= yMax) { |
290 | 682k | return splashClipAllOutside; |
291 | 682k | } |
292 | 8.97k | if (isSimple && |
293 | 2.53k | (SplashCoord)rectXMin >= xMin && |
294 | 913 | (SplashCoord)(rectXMax + 1) <= xMax && |
295 | 616 | (SplashCoord)rectYMin >= yMin && |
296 | 470 | (SplashCoord)(rectYMax + 1) <= yMax) { |
297 | 470 | return splashClipAllInside; |
298 | 470 | } |
299 | 8.97k | } |
300 | 31.5k | return splashClipPartial; |
301 | 2.26M | } |
302 | | |
303 | | void SplashClip::clipSpan(Guchar *line, int y, int x0, int x1, |
304 | 26.6k | SplashStrokeAdjustMode strokeAdjust) { |
305 | 26.6k | SplashClip *clip; |
306 | 26.6k | SplashCoord d; |
307 | 26.6k | int x0a, x1a, x0b, x1b, x, i; |
308 | | |
309 | 26.6k | updateIntBounds(strokeAdjust); |
310 | | |
311 | | //--- clip to the integer rectangle |
312 | | |
313 | 26.6k | if (y < yMinI || y > yMaxI || |
314 | 26.6k | x1 < xMinI || x0 > xMaxI) { |
315 | 0 | memset(line + x0, 0, x1 - x0 + 1); |
316 | 0 | return; |
317 | 0 | } |
318 | | |
319 | 26.6k | if (x0 > xMinI) { |
320 | 522 | x0a = x0; |
321 | 26.1k | } else { |
322 | 26.1k | x0a = xMinI; |
323 | 26.1k | memset(line + x0, 0, x0a - x0); |
324 | 26.1k | } |
325 | | |
326 | 26.6k | if (x1 < xMaxI) { |
327 | 276 | x1a = x1; |
328 | 26.4k | } else { |
329 | 26.4k | x1a = xMaxI; |
330 | 26.4k | memset(line + x1a + 1, 0, x1 - x1a); |
331 | 26.4k | } |
332 | | |
333 | 26.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 | 26.6k | if (strokeAdjust == splashStrokeAdjustOff) { |
341 | | |
342 | | // clip left edge (xMin) |
343 | 2.82k | if (x0a == xMinI) { |
344 | 2.29k | d = (SplashCoord)(xMinI + 1) - xMin; |
345 | 2.29k | line[x0a] = (Guchar)(int)((SplashCoord)line[x0a] * d); |
346 | 2.29k | } |
347 | | |
348 | | // clip right edge (xMax) |
349 | 2.82k | if (x1a == xMaxI) { |
350 | 2.54k | d = xMax - (SplashCoord)xMaxI; |
351 | 2.54k | line[x1a] = (Guchar)(int)((SplashCoord)line[x1a] * d); |
352 | 2.54k | } |
353 | | |
354 | | // clip top edge (yMin) |
355 | 2.82k | if (y == yMinI) { |
356 | 1.74k | d = (SplashCoord)(yMinI + 1) - yMin; |
357 | 4.14k | for (x = x0a; x <= x1a; ++x) { |
358 | 2.40k | line[x] = (Guchar)(int)((SplashCoord)line[x] * d); |
359 | 2.40k | } |
360 | 1.74k | } |
361 | | |
362 | | // clip bottom edge (yMax) |
363 | 2.82k | if (y == yMaxI) { |
364 | 1.75k | d = yMax - (SplashCoord)yMaxI; |
365 | 4.34k | for (x = x0a; x <= x1a; ++x) { |
366 | 2.59k | line[x] = (Guchar)(int)((SplashCoord)line[x] * d); |
367 | 2.59k | } |
368 | 1.75k | } |
369 | 2.82k | } |
370 | | |
371 | 26.6k | if (isSimple) { |
372 | 22.4k | return; |
373 | 22.4k | } |
374 | | |
375 | | //--- clip to the paths |
376 | | |
377 | 8.79k | for (clip = this; clip; clip = clip->prev) { |
378 | 13.2k | for (i = 0; i < clip->length; ++i) { |
379 | 8.70k | clip->scanners[i]->getSpan(buf, y, x0a, x1a, &x0b, &x1b); |
380 | 8.70k | if (x0a < x0b) { |
381 | 511 | memset(line + x0a, 0, x0b - x0a); |
382 | 511 | } |
383 | 15.8k | for (x = x0b; x <= x1b; ++x) { |
384 | 7.17k | line[x] = mul255(line[x], buf[x]); |
385 | 7.17k | } |
386 | 8.70k | if (x1b < x1a) { |
387 | 1.17k | memset(line + x1b + 1, 0, x1a - x1b); |
388 | 1.17k | } |
389 | 8.70k | } |
390 | 4.56k | } |
391 | 4.23k | } |
392 | | |
393 | | GBool SplashClip::clipSpanBinary(Guchar *line, int y, int x0, int x1, |
394 | 4.49k | SplashStrokeAdjustMode strokeAdjust) { |
395 | 4.49k | SplashClip *clip; |
396 | 4.49k | int x0a, x1a, x0b, x1b, x, i; |
397 | 4.49k | Guchar any; |
398 | | |
399 | 4.49k | updateIntBounds(strokeAdjust); |
400 | | |
401 | 4.49k | if (y < yMinI || y > yMaxI || |
402 | 4.49k | 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.49k | if (x0 > xMinI) { |
410 | 0 | x0a = x0; |
411 | 4.49k | } else { |
412 | 4.49k | x0a = xMinI; |
413 | 4.49k | memset(line + x0, 0, x0a - x0); |
414 | 4.49k | } |
415 | | |
416 | 4.49k | if (x1 < xMaxI) { |
417 | 0 | x1a = x1; |
418 | 4.49k | } else { |
419 | 4.49k | x1a = xMaxI; |
420 | 4.49k | memset(line + x1a + 1, 0, x1 - x1a); |
421 | 4.49k | } |
422 | | |
423 | 4.49k | if (x0a > x1a) { |
424 | 0 | return gFalse; |
425 | 0 | } |
426 | | |
427 | 4.49k | if (isSimple) { |
428 | 2.23k | for (x = x0a; x <= x1a; ++x) { |
429 | 2.23k | if (line[x]) { |
430 | 2.23k | return gTrue; |
431 | 2.23k | } |
432 | 2.23k | } |
433 | 0 | return gFalse; |
434 | 2.23k | } |
435 | | |
436 | 2.26k | any = 0; |
437 | 4.68k | for (clip = this; clip; clip = clip->prev) { |
438 | 8.24k | for (i = 0; i < clip->length; ++i) { |
439 | 5.82k | clip->scanners[i]->getSpanBinary(buf, y, x0a, x1a, &x0b, &x1b); |
440 | 5.82k | if (x0a < x0b) { |
441 | 113 | memset(line + x0a, 0, x0b - x0a); |
442 | 113 | } |
443 | 11.3k | for (x = x0b; x <= x1b; ++x) { |
444 | 5.51k | line[x] &= buf[x]; |
445 | 5.51k | any |= line[x]; |
446 | 5.51k | } |
447 | 5.82k | if (x1b < x1a) { |
448 | 267 | memset(line + x1b + 1, 0, x1a - x1b); |
449 | 267 | } |
450 | 5.82k | } |
451 | 2.42k | } |
452 | | |
453 | 2.26k | return any != 0; |
454 | 4.49k | } |
455 | | |
456 | 73.6k | int SplashClip::getXMinI(SplashStrokeAdjustMode strokeAdjust) { |
457 | 73.6k | updateIntBounds(strokeAdjust); |
458 | 73.6k | return xMinI; |
459 | 73.6k | } |
460 | | |
461 | 73.6k | int SplashClip::getXMaxI(SplashStrokeAdjustMode strokeAdjust) { |
462 | 73.6k | updateIntBounds(strokeAdjust); |
463 | 73.6k | return xMaxI; |
464 | 73.6k | } |
465 | | |
466 | 72.2k | int SplashClip::getYMinI(SplashStrokeAdjustMode strokeAdjust) { |
467 | 72.2k | updateIntBounds(strokeAdjust); |
468 | 72.2k | return yMinI; |
469 | 72.2k | } |
470 | | |
471 | 72.2k | int SplashClip::getYMaxI(SplashStrokeAdjustMode strokeAdjust) { |
472 | 72.2k | updateIntBounds(strokeAdjust); |
473 | 72.2k | return yMaxI; |
474 | 72.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.66M | void SplashClip::updateIntBounds(SplashStrokeAdjustMode strokeAdjust) { |
488 | 1.66M | if (intBoundsValid && strokeAdjust == intBoundsStrokeAdjust) { |
489 | 1.64M | return; |
490 | 1.64M | } |
491 | 18.2k | if (strokeAdjust != splashStrokeAdjustOff && isSimple) { |
492 | 15.2k | splashStrokeAdjust(xMin, xMax, &xMinI, &xMaxI, strokeAdjust); |
493 | 15.2k | splashStrokeAdjust(yMin, yMax, &yMinI, &yMaxI, strokeAdjust); |
494 | 15.2k | } else { |
495 | 2.98k | xMinI = splashFloor(xMin); |
496 | 2.98k | yMinI = splashFloor(yMin); |
497 | 2.98k | xMaxI = splashCeil(xMax); |
498 | 2.98k | yMaxI = splashCeil(yMax); |
499 | 2.98k | } |
500 | 18.2k | if (xMinI < hardXMin) { |
501 | 0 | xMinI = hardXMin; |
502 | 0 | } |
503 | 18.2k | if (yMinI < hardYMin) { |
504 | 0 | yMinI = hardYMin; |
505 | 0 | } |
506 | 18.2k | if (xMaxI > hardXMax) { |
507 | 0 | xMaxI = hardXMax; |
508 | 0 | } |
509 | 18.2k | if (yMaxI > hardYMax) { |
510 | 0 | yMaxI = hardYMax; |
511 | 0 | } |
512 | | // the clipping code uses [xMinI, xMaxI] instead of [xMinI, xMaxI) |
513 | 18.2k | --xMaxI; |
514 | 18.2k | --yMaxI; |
515 | 18.2k | intBoundsValid = gTrue; |
516 | 18.2k | intBoundsStrokeAdjust = strokeAdjust; |
517 | 18.2k | } |