Coverage Report

Created: 2026-08-13 06:56

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.5k
static inline Guchar mul255(Guchar x, Guchar y) {
25
61.5k
  int z;
26
27
61.5k
  z = (int)x * (int)y;
28
61.5k
  return (Guchar)((z + (z >> 8) + 0x80) >> 8);
29
61.5k
}
30
31
//------------------------------------------------------------------------
32
// SplashClip
33
//------------------------------------------------------------------------
34
35
SplashClip::SplashClip(int hardXMinA, int hardYMinA,
36
402k
           int hardXMaxA, int hardYMaxA) {
37
402k
  int w;
38
39
402k
  hardXMin = hardXMinA;
40
402k
  hardYMin = hardYMinA;
41
402k
  hardXMax = hardXMaxA;
42
402k
  hardYMax = hardYMaxA;
43
402k
  xMin = hardXMin;
44
402k
  yMin = hardYMin;
45
402k
  xMax = hardXMax;
46
402k
  yMax = hardYMax;
47
402k
  intBoundsValid = gFalse;
48
402k
  paths = NULL;
49
402k
  eo = NULL;
50
402k
  scanners = NULL;
51
402k
  length = size = 0;
52
402k
  isSimple = gTrue;
53
402k
  prev = NULL;
54
402k
  if ((w = hardXMax + 1) <= 0) {
55
0
    w = 1;
56
0
  }
57
402k
  buf = (Guchar *)gmalloc(w);
58
402k
}
59
60
187k
SplashClip::SplashClip(SplashClip *clip) {
61
187k
  int w;
62
63
187k
  hardXMin = clip->hardXMin;
64
187k
  hardYMin = clip->hardYMin;
65
187k
  hardXMax = clip->hardXMax;
66
187k
  hardYMax = clip->hardYMax;
67
187k
  xMin = clip->xMin;
68
187k
  yMin = clip->yMin;
69
187k
  xMax = clip->xMax;
70
187k
  yMax = clip->yMax;
71
187k
  xMinI = clip->xMinI;
72
187k
  yMinI = clip->yMinI;
73
187k
  xMaxI = clip->xMaxI;
74
187k
  yMaxI = clip->yMaxI;
75
187k
  intBoundsValid = clip->intBoundsValid;
76
187k
  intBoundsStrokeAdjust = clip->intBoundsStrokeAdjust;
77
187k
  paths = NULL;
78
187k
  eo = NULL;
79
187k
  scanners = NULL;
80
187k
  length = size = 0;
81
187k
  isSimple = clip->isSimple;
82
187k
  prev = clip;
83
187k
  if ((w = splashCeil(xMax)) <= 0) {
84
45.4k
    w = 1;
85
45.4k
  }
86
187k
  buf = (Guchar *)gmalloc(w);
87
187k
}
88
89
590k
SplashClip::~SplashClip() {
90
590k
  int i;
91
92
684k
  for (i = 0; i < length; ++i) {
93
94.2k
    delete scanners[i];
94
94.2k
    delete paths[i];
95
94.2k
  }
96
590k
  gfree(paths);
97
590k
  gfree(eo);
98
590k
  gfree(scanners);
99
590k
  gfree(buf);
100
590k
}
101
102
94.3k
void SplashClip::grow(int nPaths) {
103
94.3k
  if (length + nPaths > size) {
104
86.5k
    if (size == 0) {
105
86.5k
      size = 32;
106
86.5k
    }
107
86.5k
    while (size < length + nPaths) {
108
22
      size *= 2;
109
22
    }
110
86.5k
    paths = (SplashXPath **)greallocn(paths, size, sizeof(SplashXPath *));
111
86.5k
    eo = (Guchar *)greallocn(eo, size, sizeof(Guchar));
112
86.5k
    scanners = (SplashXPathScanner **)
113
86.5k
                   greallocn(scanners, size, sizeof(SplashXPathScanner *));
114
86.5k
  }
115
94.3k
}
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
126k
           SplashCoord x1, SplashCoord y1) {
159
126k
  if (x0 < x1) {
160
55.2k
    if (x0 > xMin) {
161
697
      xMin = x0;
162
697
      intBoundsValid = gFalse;
163
697
    }
164
55.2k
    if (x1 < xMax) {
165
10.1k
      xMax = x1;
166
10.1k
      intBoundsValid = gFalse;
167
10.1k
    }
168
70.9k
  } else {
169
70.9k
    if (x1 > xMin) {
170
24.4k
      xMin = x1;
171
24.4k
      intBoundsValid = gFalse;
172
24.4k
    }
173
70.9k
    if (x0 < xMax) {
174
13.3k
      xMax = x0;
175
13.3k
      intBoundsValid = gFalse;
176
13.3k
    }
177
70.9k
  }
178
126k
  if (y0 < y1) {
179
79.8k
    if (y0 > yMin) {
180
10.3k
      yMin = y0;
181
10.3k
      intBoundsValid = gFalse;
182
10.3k
    }
183
79.8k
    if (y1 < yMax) {
184
2.84k
      yMax = y1;
185
2.84k
      intBoundsValid = gFalse;
186
2.84k
    }
187
79.8k
  } else {
188
46.4k
    if (y1 > yMin) {
189
5.42k
      yMin = y1;
190
5.42k
      intBoundsValid = gFalse;
191
5.42k
    }
192
46.4k
    if (y0 < yMax) {
193
8.28k
      yMax = y0;
194
8.28k
      intBoundsValid = gFalse;
195
8.28k
    }
196
46.4k
  }
197
126k
  return splashOk;
198
126k
}
199
200
SplashError SplashClip::clipToPath(SplashPath *path, SplashCoord *matrix,
201
           SplashCoord flatness, GBool eoA,
202
           GBool enablePathSimplification,
203
233k
           SplashStrokeAdjustMode strokeAdjust) {
204
233k
  SplashXPath *xPath;
205
233k
  SplashCoord t;
206
207
233k
  xPath = new SplashXPath(path, matrix, flatness, gTrue,
208
233k
        enablePathSimplification,
209
233k
        strokeAdjust, NULL);
210
211
  // check for an empty path
212
233k
  if (xPath->length == 0) {
213
12.9k
    xMin = yMin = 1;
214
12.9k
    xMax = yMax = 0;
215
12.9k
    intBoundsValid = gFalse;
216
12.9k
    delete xPath;
217
12.9k
    return splashOk;
218
12.9k
  }
219
220
  // check for a rectangle
221
220k
  if (xPath->isRect) {
222
126k
    clipToRect(xPath->rectX0, xPath->rectY0, xPath->rectX1, xPath->rectY1);
223
126k
    delete xPath;
224
126k
    return splashOk;
225
126k
  }
226
227
94.3k
  grow(1);
228
94.3k
  paths[length] = xPath;
229
94.3k
  eo[length] = (Guchar)eoA;
230
94.3k
  if ((t = xPath->getXMin()) > xMin) {
231
2.40k
    xMin = t;
232
2.40k
  }
233
94.3k
  if ((t = xPath->getYMin()) > yMin) {
234
15.8k
    yMin = t;
235
15.8k
  }
236
94.3k
  if ((t = xPath->getXMax() + 1) < xMax) {
237
38.3k
    xMax = t;
238
38.3k
  }
239
94.3k
  if ((t = xPath->getYMax() + 1) < yMax) {
240
47.0k
    yMax = t;
241
47.0k
  }
242
94.3k
  intBoundsValid = gFalse;
243
94.3k
  scanners[length] = new SplashXPathScanner(xPath, eoA, splashFloor(yMin),
244
94.3k
              splashCeil(yMax) - 1);
245
94.3k
  ++length;
246
94.3k
  isSimple = gFalse;
247
248
94.3k
  return splashOk;
249
220k
}
250
251
SplashClipResult SplashClip::testRect(int rectXMin, int rectYMin,
252
              int rectXMax, int rectYMax,
253
2.45M
              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.45M
  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.21M
    updateIntBounds(strokeAdjust);
267
1.21M
    if (xMinI > xMaxI || yMinI > yMaxI) {
268
158k
      return splashClipAllOutside;
269
158k
    }
270
1.05M
    if (rectXMax + 1 <= xMinI ||
271
525k
  rectXMin >= xMaxI + 1 ||
272
251k
  rectYMax + 1 <= yMinI ||
273
1.03M
  rectYMin >= yMaxI + 1) {
274
1.03M
      return splashClipAllOutside;
275
1.03M
    }
276
25.9k
    if (rectXMin >= xMinI &&
277
16.4k
  rectXMax <= xMaxI &&
278
10.7k
  rectYMin >= yMinI &&
279
4.76k
  rectYMax <= yMaxI) {
280
4.13k
      return splashClipAllInside;
281
4.13k
    }
282
1.23M
  } else {
283
1.23M
    if (xMin >= xMax || yMin >= yMax) {
284
718k
      return splashClipAllOutside;
285
718k
    }
286
518k
    if ((SplashCoord)(rectXMax + 1) <= xMin ||
287
189k
  (SplashCoord)rectXMin >= xMax ||
288
44.3k
  (SplashCoord)(rectYMax + 1) <= yMin ||
289
498k
  (SplashCoord)rectYMin >= yMax) {
290
498k
      return splashClipAllOutside;
291
498k
    }
292
20.1k
    if (isSimple &&
293
8.36k
  (SplashCoord)rectXMin >= xMin &&
294
4.06k
  (SplashCoord)(rectXMax + 1) <= xMax &&
295
3.62k
  (SplashCoord)rectYMin >= yMin &&
296
395
  (SplashCoord)(rectYMax + 1) <= yMax) {
297
391
      return splashClipAllInside;
298
391
    }
299
20.1k
  }
300
41.5k
  return splashClipPartial;
301
2.45M
}
302
303
void SplashClip::clipSpan(Guchar *line, int y, int x0, int x1,
304
30.3k
        SplashStrokeAdjustMode strokeAdjust) {
305
30.3k
  SplashClip *clip;
306
30.3k
  SplashCoord d;
307
30.3k
  int x0a, x1a, x0b, x1b, x, i;
308
309
30.3k
  updateIntBounds(strokeAdjust);
310
311
  //--- clip to the integer rectangle
312
313
30.3k
  if (y < yMinI || y > yMaxI ||
314
30.3k
      x1 < xMinI || x0 > xMaxI) {
315
0
    memset(line + x0, 0, x1 - x0 + 1);
316
0
    return;
317
0
  }
318
319
30.3k
  if (x0 > xMinI) {
320
110
    x0a = x0;
321
30.2k
  } else {
322
30.2k
    x0a = xMinI;
323
30.2k
    memset(line + x0, 0, x0a - x0);
324
30.2k
  }
325
326
30.3k
  if (x1 < xMaxI) {
327
24
    x1a = x1;
328
30.3k
  } else {
329
30.3k
    x1a = xMaxI;
330
30.3k
    memset(line + x1a + 1, 0, x1 - x1a);
331
30.3k
  }
332
333
30.3k
  if (x0a > x1a) {
334
0
    return;
335
0
  }
336
337
  //--- clip to the floating point rectangle
338
  //    (if stroke adjustment is disabled)
339
340
30.3k
  if (strokeAdjust == splashStrokeAdjustOff) {
341
342
    // clip left edge (xMin)
343
6.94k
    if (x0a == xMinI) {
344
6.83k
      d = (SplashCoord)(xMinI + 1) - xMin;
345
6.83k
      line[x0a] = (Guchar)(int)((SplashCoord)line[x0a] * d);
346
6.83k
    }
347
348
    // clip right edge (xMax)
349
6.94k
    if (x1a == xMaxI) {
350
6.92k
      d = xMax - (SplashCoord)xMaxI;
351
6.92k
      line[x1a] = (Guchar)(int)((SplashCoord)line[x1a] * d);
352
6.92k
    }
353
354
    // clip top edge (yMin)
355
6.94k
    if (y == yMinI) {
356
6.82k
      d = (SplashCoord)(yMinI + 1) - yMin;
357
13.8k
      for (x = x0a; x <= x1a; ++x) {
358
6.99k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
359
6.99k
      }
360
6.82k
    }
361
362
    // clip bottom edge (yMax)
363
6.94k
    if (y == yMaxI) {
364
6.81k
      d = yMax - (SplashCoord)yMaxI;
365
13.7k
      for (x = x0a; x <= x1a; ++x) {
366
6.97k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
367
6.97k
      }
368
6.81k
    }
369
6.94k
  }
370
371
30.3k
  if (isSimple) {
372
24.9k
    return;
373
24.9k
  }
374
375
  //--- clip to the paths
376
377
11.6k
  for (clip = this; clip; clip = clip->prev) {
378
68.0k
    for (i = 0; i < clip->length; ++i) {
379
61.7k
      clip->scanners[i]->getSpan(buf, y, x0a, x1a, &x0b, &x1b);
380
61.7k
      if (x0a < x0b) {
381
32
  memset(line + x0a, 0, x0b - x0a);
382
32
      }
383
123k
      for (x = x0b; x <= x1b; ++x) {
384
61.5k
  line[x] = mul255(line[x], buf[x]);
385
61.5k
      }
386
61.7k
      if (x1b < x1a) {
387
258
  memset(line + x1b + 1, 0, x1a - x1b);
388
258
      }
389
61.7k
    }
390
6.25k
  }
391
5.39k
}
392
393
GBool SplashClip::clipSpanBinary(Guchar *line, int y, int x0, int x1,
394
9.52k
         SplashStrokeAdjustMode strokeAdjust) {
395
9.52k
  SplashClip *clip;
396
9.52k
  int x0a, x1a, x0b, x1b, x, i;
397
9.52k
  Guchar any;
398
399
9.52k
  updateIntBounds(strokeAdjust);
400
401
9.52k
  if (y < yMinI || y > yMaxI ||
402
9.52k
      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
9.52k
  if (x0 > xMinI) {
410
0
    x0a = x0;
411
9.52k
  } else {
412
9.52k
    x0a = xMinI;
413
9.52k
    memset(line + x0, 0, x0a - x0);
414
9.52k
  }
415
416
9.52k
  if (x1 < xMaxI) {
417
0
    x1a = x1;
418
9.52k
  } else {
419
9.52k
    x1a = xMaxI;
420
9.52k
    memset(line + x1a + 1, 0, x1 - x1a);
421
9.52k
  }
422
423
9.52k
  if (x0a > x1a) {
424
0
    return gFalse;
425
0
  }
426
427
9.52k
  if (isSimple) {
428
4.07k
    for (x = x0a; x <= x1a; ++x) {
429
4.07k
      if (line[x]) {
430
4.07k
  return gTrue;
431
4.07k
      }
432
4.07k
    }
433
0
    return gFalse;
434
4.07k
  }
435
436
5.44k
  any = 0;
437
15.1k
  for (clip = this; clip; clip = clip->prev) {
438
20.6k
    for (i = 0; i < clip->length; ++i) {
439
11.0k
      clip->scanners[i]->getSpanBinary(buf, y, x0a, x1a, &x0b, &x1b);
440
11.0k
      if (x0a < x0b) {
441
96
  memset(line + x0a, 0, x0b - x0a);
442
96
      }
443
21.7k
      for (x = x0b; x <= x1b; ++x) {
444
10.7k
  line[x] &= buf[x];
445
10.7k
  any |= line[x];
446
10.7k
      }
447
11.0k
      if (x1b < x1a) {
448
221
  memset(line + x1b + 1, 0, x1a - x1b);
449
221
      }
450
11.0k
    }
451
9.65k
  }
452
453
5.44k
  return any != 0;
454
9.52k
}
455
456
77.2k
int SplashClip::getXMinI(SplashStrokeAdjustMode strokeAdjust) {
457
77.2k
  updateIntBounds(strokeAdjust);
458
77.2k
  return xMinI;
459
77.2k
}
460
461
77.2k
int SplashClip::getXMaxI(SplashStrokeAdjustMode strokeAdjust) {
462
77.2k
  updateIntBounds(strokeAdjust);
463
77.2k
  return xMaxI;
464
77.2k
}
465
466
76.3k
int SplashClip::getYMinI(SplashStrokeAdjustMode strokeAdjust) {
467
76.3k
  updateIntBounds(strokeAdjust);
468
76.3k
  return yMinI;
469
76.3k
}
470
471
76.3k
int SplashClip::getYMaxI(SplashStrokeAdjustMode strokeAdjust) {
472
76.3k
  updateIntBounds(strokeAdjust);
473
76.3k
  return yMaxI;
474
76.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.56M
void SplashClip::updateIntBounds(SplashStrokeAdjustMode strokeAdjust) {
488
1.56M
  if (intBoundsValid && strokeAdjust == intBoundsStrokeAdjust) {
489
1.54M
    return;
490
1.54M
  }
491
24.6k
  if (strokeAdjust != splashStrokeAdjustOff && isSimple) {
492
17.1k
    splashStrokeAdjust(xMin, xMax, &xMinI, &xMaxI, strokeAdjust);
493
17.1k
    splashStrokeAdjust(yMin, yMax, &yMinI, &yMaxI, strokeAdjust);
494
17.1k
  } else {
495
7.50k
    xMinI = splashFloor(xMin);
496
7.50k
    yMinI = splashFloor(yMin);
497
7.50k
    xMaxI = splashCeil(xMax);
498
7.50k
    yMaxI = splashCeil(yMax);
499
7.50k
  }
500
24.6k
  if (xMinI < hardXMin) {
501
6
    xMinI = hardXMin;
502
6
  }
503
24.6k
  if (yMinI < hardYMin) {
504
5
    yMinI = hardYMin;
505
5
  }
506
24.6k
  if (xMaxI > hardXMax) {
507
12
    xMaxI = hardXMax;
508
12
  }
509
24.6k
  if (yMaxI > hardYMax) {
510
8
    yMaxI = hardYMax;
511
8
  }
512
  // the clipping code uses [xMinI, xMaxI] instead of [xMinI, xMaxI)
513
24.6k
  --xMaxI;
514
24.6k
  --yMaxI;
515
24.6k
  intBoundsValid = gTrue;
516
24.6k
  intBoundsStrokeAdjust = strokeAdjust;
517
24.6k
}