Debian Codegraph @

libs/xpdf/xpdf-src/splash/SplashClip.cc

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