Coverage Report

Created: 2026-06-22 07:09

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