Coverage Report

Created: 2026-09-03 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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.8k
static inline Guchar mul255(Guchar x, Guchar y) {
25
61.8k
  int z;
26
27
61.8k
  z = (int)x * (int)y;
28
61.8k
  return (Guchar)((z + (z >> 8) + 0x80) >> 8);
29
61.8k
}
30
31
//------------------------------------------------------------------------
32
// SplashClip
33
//------------------------------------------------------------------------
34
35
SplashClip::SplashClip(int hardXMinA, int hardYMinA,
36
273k
           int hardXMaxA, int hardYMaxA) {
37
273k
  int w;
38
39
273k
  hardXMin = hardXMinA;
40
273k
  hardYMin = hardYMinA;
41
273k
  hardXMax = hardXMaxA;
42
273k
  hardYMax = hardYMaxA;
43
273k
  xMin = hardXMin;
44
273k
  yMin = hardYMin;
45
273k
  xMax = hardXMax;
46
273k
  yMax = hardYMax;
47
273k
  intBoundsValid = gFalse;
48
273k
  paths = NULL;
49
273k
  eo = NULL;
50
273k
  scanners = NULL;
51
273k
  length = size = 0;
52
273k
  isSimple = gTrue;
53
273k
  prev = NULL;
54
273k
  if ((w = hardXMax + 1) <= 0) {
55
0
    w = 1;
56
0
  }
57
273k
  buf = (Guchar *)gmalloc(w);
58
273k
}
59
60
175k
SplashClip::SplashClip(SplashClip *clip) {
61
175k
  int w;
62
63
175k
  hardXMin = clip->hardXMin;
64
175k
  hardYMin = clip->hardYMin;
65
175k
  hardXMax = clip->hardXMax;
66
175k
  hardYMax = clip->hardYMax;
67
175k
  xMin = clip->xMin;
68
175k
  yMin = clip->yMin;
69
175k
  xMax = clip->xMax;
70
175k
  yMax = clip->yMax;
71
175k
  xMinI = clip->xMinI;
72
175k
  yMinI = clip->yMinI;
73
175k
  xMaxI = clip->xMaxI;
74
175k
  yMaxI = clip->yMaxI;
75
175k
  intBoundsValid = clip->intBoundsValid;
76
175k
  intBoundsStrokeAdjust = clip->intBoundsStrokeAdjust;
77
175k
  paths = NULL;
78
175k
  eo = NULL;
79
175k
  scanners = NULL;
80
175k
  length = size = 0;
81
175k
  isSimple = clip->isSimple;
82
175k
  prev = clip;
83
175k
  if ((w = splashCeil(xMax)) <= 0) {
84
25.1k
    w = 1;
85
25.1k
  }
86
175k
  buf = (Guchar *)gmalloc(w);
87
175k
}
88
89
448k
SplashClip::~SplashClip() {
90
448k
  int i;
91
92
530k
  for (i = 0; i < length; ++i) {
93
81.6k
    delete scanners[i];
94
81.6k
    delete paths[i];
95
81.6k
  }
96
448k
  gfree(paths);
97
448k
  gfree(eo);
98
448k
  gfree(scanners);
99
448k
  gfree(buf);
100
448k
}
101
102
81.7k
void SplashClip::grow(int nPaths) {
103
81.7k
  if (length + nPaths > size) {
104
74.1k
    if (size == 0) {
105
74.0k
      size = 32;
106
74.0k
    }
107
74.1k
    while (size < length + nPaths) {
108
22
      size *= 2;
109
22
    }
110
74.1k
    paths = (SplashXPath **)greallocn(paths, size, sizeof(SplashXPath *));
111
74.1k
    eo = (Guchar *)greallocn(eo, size, sizeof(Guchar));
112
74.1k
    scanners = (SplashXPathScanner **)
113
74.1k
                   greallocn(scanners, size, sizeof(SplashXPathScanner *));
114
74.1k
  }
115
81.7k
}
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
121k
           SplashCoord x1, SplashCoord y1) {
159
121k
  if (x0 < x1) {
160
61.9k
    if (x0 > xMin) {
161
596
      xMin = x0;
162
596
      intBoundsValid = gFalse;
163
596
    }
164
61.9k
    if (x1 < xMax) {
165
9.85k
      xMax = x1;
166
9.85k
      intBoundsValid = gFalse;
167
9.85k
    }
168
61.9k
  } else {
169
60.0k
    if (x1 > xMin) {
170
34.4k
      xMin = x1;
171
34.4k
      intBoundsValid = gFalse;
172
34.4k
    }
173
60.0k
    if (x0 < xMax) {
174
8.71k
      xMax = x0;
175
8.71k
      intBoundsValid = gFalse;
176
8.71k
    }
177
60.0k
  }
178
121k
  if (y0 < y1) {
179
94.9k
    if (y0 > yMin) {
180
51.0k
      yMin = y0;
181
51.0k
      intBoundsValid = gFalse;
182
51.0k
    }
183
94.9k
    if (y1 < yMax) {
184
3.53k
      yMax = y1;
185
3.53k
      intBoundsValid = gFalse;
186
3.53k
    }
187
94.9k
  } else {
188
27.0k
    if (y1 > yMin) {
189
4.72k
      yMin = y1;
190
4.72k
      intBoundsValid = gFalse;
191
4.72k
    }
192
27.0k
    if (y0 < yMax) {
193
4.23k
      yMax = y0;
194
4.23k
      intBoundsValid = gFalse;
195
4.23k
    }
196
27.0k
  }
197
121k
  return splashOk;
198
121k
}
199
200
SplashError SplashClip::clipToPath(SplashPath *path, SplashCoord *matrix,
201
           SplashCoord flatness, GBool eoA,
202
           GBool enablePathSimplification,
203
216k
           SplashStrokeAdjustMode strokeAdjust) {
204
216k
  SplashXPath *xPath;
205
216k
  SplashCoord t;
206
207
216k
  xPath = new SplashXPath(path, matrix, flatness, gTrue,
208
216k
        enablePathSimplification,
209
216k
        strokeAdjust, NULL);
210
211
  // check for an empty path
212
216k
  if (xPath->length == 0) {
213
12.3k
    xMin = yMin = 1;
214
12.3k
    xMax = yMax = 0;
215
12.3k
    intBoundsValid = gFalse;
216
12.3k
    delete xPath;
217
12.3k
    return splashOk;
218
12.3k
  }
219
220
  // check for a rectangle
221
203k
  if (xPath->isRect) {
222
121k
    clipToRect(xPath->rectX0, xPath->rectY0, xPath->rectX1, xPath->rectY1);
223
121k
    delete xPath;
224
121k
    return splashOk;
225
121k
  }
226
227
81.7k
  grow(1);
228
81.7k
  paths[length] = xPath;
229
81.7k
  eo[length] = (Guchar)eoA;
230
81.7k
  if ((t = xPath->getXMin()) > xMin) {
231
2.03k
    xMin = t;
232
2.03k
  }
233
81.7k
  if ((t = xPath->getYMin()) > yMin) {
234
12.0k
    yMin = t;
235
12.0k
  }
236
81.7k
  if ((t = xPath->getXMax() + 1) < xMax) {
237
39.5k
    xMax = t;
238
39.5k
  }
239
81.7k
  if ((t = xPath->getYMax() + 1) < yMax) {
240
47.6k
    yMax = t;
241
47.6k
  }
242
81.7k
  intBoundsValid = gFalse;
243
81.7k
  scanners[length] = new SplashXPathScanner(xPath, eoA, splashFloor(yMin),
244
81.7k
              splashCeil(yMax) - 1);
245
81.7k
  ++length;
246
81.7k
  isSimple = gFalse;
247
248
81.7k
  return splashOk;
249
203k
}
250
251
SplashClipResult SplashClip::testRect(int rectXMin, int rectYMin,
252
              int rectXMax, int rectYMax,
253
2.87M
              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.87M
  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.50M
    updateIntBounds(strokeAdjust);
267
1.50M
    if (xMinI > xMaxI || yMinI > yMaxI) {
268
6.32k
      return splashClipAllOutside;
269
6.32k
    }
270
1.49M
    if (rectXMax + 1 <= xMinI ||
271
729k
  rectXMin >= xMaxI + 1 ||
272
359k
  rectYMax + 1 <= yMinI ||
273
1.47M
  rectYMin >= yMaxI + 1) {
274
1.47M
      return splashClipAllOutside;
275
1.47M
    }
276
27.4k
    if (rectXMin >= xMinI &&
277
16.5k
  rectXMax <= xMaxI &&
278
11.2k
  rectYMin >= yMinI &&
279
5.97k
  rectYMax <= yMaxI) {
280
5.66k
      return splashClipAllInside;
281
5.66k
    }
282
1.37M
  } else {
283
1.37M
    if (xMin >= xMax || yMin >= yMax) {
284
784k
      return splashClipAllOutside;
285
784k
    }
286
589k
    if ((SplashCoord)(rectXMax + 1) <= xMin ||
287
205k
  (SplashCoord)rectXMin >= xMax ||
288
26.7k
  (SplashCoord)(rectYMax + 1) <= yMin ||
289
575k
  (SplashCoord)rectYMin >= yMax) {
290
575k
      return splashClipAllOutside;
291
575k
    }
292
14.8k
    if (isSimple &&
293
6.98k
  (SplashCoord)rectXMin >= xMin &&
294
2.66k
  (SplashCoord)(rectXMax + 1) <= xMax &&
295
2.50k
  (SplashCoord)rectYMin >= yMin &&
296
787
  (SplashCoord)(rectYMax + 1) <= yMax) {
297
783
      return splashClipAllInside;
298
783
    }
299
14.8k
  }
300
35.8k
  return splashClipPartial;
301
2.87M
}
302
303
void SplashClip::clipSpan(Guchar *line, int y, int x0, int x1,
304
29.5k
        SplashStrokeAdjustMode strokeAdjust) {
305
29.5k
  SplashClip *clip;
306
29.5k
  SplashCoord d;
307
29.5k
  int x0a, x1a, x0b, x1b, x, i;
308
309
29.5k
  updateIntBounds(strokeAdjust);
310
311
  //--- clip to the integer rectangle
312
313
29.5k
  if (y < yMinI || y > yMaxI ||
314
29.5k
      x1 < xMinI || x0 > xMaxI) {
315
0
    memset(line + x0, 0, x1 - x0 + 1);
316
0
    return;
317
0
  }
318
319
29.5k
  if (x0 > xMinI) {
320
320
    x0a = x0;
321
29.2k
  } else {
322
29.2k
    x0a = xMinI;
323
29.2k
    memset(line + x0, 0, x0a - x0);
324
29.2k
  }
325
326
29.5k
  if (x1 < xMaxI) {
327
34
    x1a = x1;
328
29.5k
  } else {
329
29.5k
    x1a = xMaxI;
330
29.5k
    memset(line + x1a + 1, 0, x1 - x1a);
331
29.5k
  }
332
333
29.5k
  if (x0a > x1a) {
334
0
    return;
335
0
  }
336
337
  //--- clip to the floating point rectangle
338
  //    (if stroke adjustment is disabled)
339
340
29.5k
  if (strokeAdjust == splashStrokeAdjustOff) {
341
342
    // clip left edge (xMin)
343
7.01k
    if (x0a == xMinI) {
344
6.69k
      d = (SplashCoord)(xMinI + 1) - xMin;
345
6.69k
      line[x0a] = (Guchar)(int)((SplashCoord)line[x0a] * d);
346
6.69k
    }
347
348
    // clip right edge (xMax)
349
7.01k
    if (x1a == xMaxI) {
350
6.98k
      d = xMax - (SplashCoord)xMaxI;
351
6.98k
      line[x1a] = (Guchar)(int)((SplashCoord)line[x1a] * d);
352
6.98k
    }
353
354
    // clip top edge (yMin)
355
7.01k
    if (y == yMinI) {
356
6.68k
      d = (SplashCoord)(yMinI + 1) - yMin;
357
13.7k
      for (x = x0a; x <= x1a; ++x) {
358
7.05k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
359
7.05k
      }
360
6.68k
    }
361
362
    // clip bottom edge (yMax)
363
7.01k
    if (y == yMaxI) {
364
6.67k
      d = yMax - (SplashCoord)yMaxI;
365
13.7k
      for (x = x0a; x <= x1a; ++x) {
366
7.03k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
367
7.03k
      }
368
6.67k
    }
369
7.01k
  }
370
371
29.5k
  if (isSimple) {
372
24.3k
    return;
373
24.3k
  }
374
375
  //--- clip to the paths
376
377
10.9k
  for (clip = this; clip; clip = clip->prev) {
378
68.5k
    for (i = 0; i < clip->length; ++i) {
379
62.7k
      clip->scanners[i]->getSpan(buf, y, x0a, x1a, &x0b, &x1b);
380
62.7k
      if (x0a < x0b) {
381
785
  memset(line + x0a, 0, x0b - x0a);
382
785
      }
383
124k
      for (x = x0b; x <= x1b; ++x) {
384
61.8k
  line[x] = mul255(line[x], buf[x]);
385
61.8k
      }
386
62.7k
      if (x1b < x1a) {
387
967
  memset(line + x1b + 1, 0, x1a - x1b);
388
967
      }
389
62.7k
    }
390
5.76k
  }
391
5.19k
}
392
393
GBool SplashClip::clipSpanBinary(Guchar *line, int y, int x0, int x1,
394
4.57k
         SplashStrokeAdjustMode strokeAdjust) {
395
4.57k
  SplashClip *clip;
396
4.57k
  int x0a, x1a, x0b, x1b, x, i;
397
4.57k
  Guchar any;
398
399
4.57k
  updateIntBounds(strokeAdjust);
400
401
4.57k
  if (y < yMinI || y > yMaxI ||
402
4.57k
      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.57k
  if (x0 > xMinI) {
410
0
    x0a = x0;
411
4.57k
  } else {
412
4.57k
    x0a = xMinI;
413
4.57k
    memset(line + x0, 0, x0a - x0);
414
4.57k
  }
415
416
4.57k
  if (x1 < xMaxI) {
417
0
    x1a = x1;
418
4.57k
  } else {
419
4.57k
    x1a = xMaxI;
420
4.57k
    memset(line + x1a + 1, 0, x1 - x1a);
421
4.57k
  }
422
423
4.57k
  if (x0a > x1a) {
424
0
    return gFalse;
425
0
  }
426
427
4.57k
  if (isSimple) {
428
2.88k
    for (x = x0a; x <= x1a; ++x) {
429
2.88k
      if (line[x]) {
430
2.88k
  return gTrue;
431
2.88k
      }
432
2.88k
    }
433
0
    return gFalse;
434
2.88k
  }
435
436
1.68k
  any = 0;
437
3.54k
  for (clip = this; clip; clip = clip->prev) {
438
9.68k
    for (i = 0; i < clip->length; ++i) {
439
7.82k
      clip->scanners[i]->getSpanBinary(buf, y, x0a, x1a, &x0b, &x1b);
440
7.82k
      if (x0a < x0b) {
441
75
  memset(line + x0a, 0, x0b - x0a);
442
75
      }
443
15.1k
      for (x = x0b; x <= x1b; ++x) {
444
7.33k
  line[x] &= buf[x];
445
7.33k
  any |= line[x];
446
7.33k
      }
447
7.82k
      if (x1b < x1a) {
448
463
  memset(line + x1b + 1, 0, x1a - x1b);
449
463
      }
450
7.82k
    }
451
1.85k
  }
452
453
1.68k
  return any != 0;
454
4.57k
}
455
456
74.0k
int SplashClip::getXMinI(SplashStrokeAdjustMode strokeAdjust) {
457
74.0k
  updateIntBounds(strokeAdjust);
458
74.0k
  return xMinI;
459
74.0k
}
460
461
74.0k
int SplashClip::getXMaxI(SplashStrokeAdjustMode strokeAdjust) {
462
74.0k
  updateIntBounds(strokeAdjust);
463
74.0k
  return xMaxI;
464
74.0k
}
465
466
72.6k
int SplashClip::getYMinI(SplashStrokeAdjustMode strokeAdjust) {
467
72.6k
  updateIntBounds(strokeAdjust);
468
72.6k
  return yMinI;
469
72.6k
}
470
471
72.6k
int SplashClip::getYMaxI(SplashStrokeAdjustMode strokeAdjust) {
472
72.6k
  updateIntBounds(strokeAdjust);
473
72.6k
  return yMaxI;
474
72.6k
}
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.83M
void SplashClip::updateIntBounds(SplashStrokeAdjustMode strokeAdjust) {
488
1.83M
  if (intBoundsValid && strokeAdjust == intBoundsStrokeAdjust) {
489
1.81M
    return;
490
1.81M
  }
491
20.5k
  if (strokeAdjust != splashStrokeAdjustOff && isSimple) {
492
16.8k
    splashStrokeAdjust(xMin, xMax, &xMinI, &xMaxI, strokeAdjust);
493
16.8k
    splashStrokeAdjust(yMin, yMax, &yMinI, &yMaxI, strokeAdjust);
494
16.8k
  } else {
495
3.69k
    xMinI = splashFloor(xMin);
496
3.69k
    yMinI = splashFloor(yMin);
497
3.69k
    xMaxI = splashCeil(xMax);
498
3.69k
    yMaxI = splashCeil(yMax);
499
3.69k
  }
500
20.5k
  if (xMinI < hardXMin) {
501
4
    xMinI = hardXMin;
502
4
  }
503
20.5k
  if (yMinI < hardYMin) {
504
6
    yMinI = hardYMin;
505
6
  }
506
20.5k
  if (xMaxI > hardXMax) {
507
4
    xMaxI = hardXMax;
508
4
  }
509
20.5k
  if (yMaxI > hardYMax) {
510
6
    yMaxI = hardYMax;
511
6
  }
512
  // the clipping code uses [xMinI, xMaxI] instead of [xMinI, xMaxI)
513
20.5k
  --xMaxI;
514
20.5k
  --yMaxI;
515
20.5k
  intBoundsValid = gTrue;
516
20.5k
  intBoundsStrokeAdjust = strokeAdjust;
517
20.5k
}