Coverage Report

Created: 2026-07-04 07:11

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
60.7k
static inline Guchar mul255(Guchar x, Guchar y) {
25
60.7k
  int z;
26
27
60.7k
  z = (int)x * (int)y;
28
60.7k
  return (Guchar)((z + (z >> 8) + 0x80) >> 8);
29
60.7k
}
30
31
//------------------------------------------------------------------------
32
// SplashClip
33
//------------------------------------------------------------------------
34
35
SplashClip::SplashClip(int hardXMinA, int hardYMinA,
36
194k
           int hardXMaxA, int hardYMaxA) {
37
194k
  int w;
38
39
194k
  hardXMin = hardXMinA;
40
194k
  hardYMin = hardYMinA;
41
194k
  hardXMax = hardXMaxA;
42
194k
  hardYMax = hardYMaxA;
43
194k
  xMin = hardXMin;
44
194k
  yMin = hardYMin;
45
194k
  xMax = hardXMax;
46
194k
  yMax = hardYMax;
47
194k
  intBoundsValid = gFalse;
48
194k
  paths = NULL;
49
194k
  eo = NULL;
50
194k
  scanners = NULL;
51
194k
  length = size = 0;
52
194k
  isSimple = gTrue;
53
194k
  prev = NULL;
54
194k
  if ((w = hardXMax + 1) <= 0) {
55
0
    w = 1;
56
0
  }
57
194k
  buf = (Guchar *)gmalloc(w);
58
194k
}
59
60
200k
SplashClip::SplashClip(SplashClip *clip) {
61
200k
  int w;
62
63
200k
  hardXMin = clip->hardXMin;
64
200k
  hardYMin = clip->hardYMin;
65
200k
  hardXMax = clip->hardXMax;
66
200k
  hardYMax = clip->hardYMax;
67
200k
  xMin = clip->xMin;
68
200k
  yMin = clip->yMin;
69
200k
  xMax = clip->xMax;
70
200k
  yMax = clip->yMax;
71
200k
  xMinI = clip->xMinI;
72
200k
  yMinI = clip->yMinI;
73
200k
  xMaxI = clip->xMaxI;
74
200k
  yMaxI = clip->yMaxI;
75
200k
  intBoundsValid = clip->intBoundsValid;
76
200k
  intBoundsStrokeAdjust = clip->intBoundsStrokeAdjust;
77
200k
  paths = NULL;
78
200k
  eo = NULL;
79
200k
  scanners = NULL;
80
200k
  length = size = 0;
81
200k
  isSimple = clip->isSimple;
82
200k
  prev = clip;
83
200k
  if ((w = splashCeil(xMax)) <= 0) {
84
59.7k
    w = 1;
85
59.7k
  }
86
200k
  buf = (Guchar *)gmalloc(w);
87
200k
}
88
89
394k
SplashClip::~SplashClip() {
90
394k
  int i;
91
92
489k
  for (i = 0; i < length; ++i) {
93
94.4k
    delete scanners[i];
94
94.4k
    delete paths[i];
95
94.4k
  }
96
394k
  gfree(paths);
97
394k
  gfree(eo);
98
394k
  gfree(scanners);
99
394k
  gfree(buf);
100
394k
}
101
102
94.4k
void SplashClip::grow(int nPaths) {
103
94.4k
  if (length + nPaths > size) {
104
85.6k
    if (size == 0) {
105
85.5k
      size = 32;
106
85.5k
    }
107
85.6k
    while (size < length + nPaths) {
108
40
      size *= 2;
109
40
    }
110
85.6k
    paths = (SplashXPath **)greallocn(paths, size, sizeof(SplashXPath *));
111
85.6k
    eo = (Guchar *)greallocn(eo, size, sizeof(Guchar));
112
85.6k
    scanners = (SplashXPathScanner **)
113
85.6k
                   greallocn(scanners, size, sizeof(SplashXPathScanner *));
114
85.6k
  }
115
94.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
130k
           SplashCoord x1, SplashCoord y1) {
159
130k
  if (x0 < x1) {
160
45.4k
    if (x0 > xMin) {
161
463
      xMin = x0;
162
463
      intBoundsValid = gFalse;
163
463
    }
164
45.4k
    if (x1 < xMax) {
165
6.95k
      xMax = x1;
166
6.95k
      intBoundsValid = gFalse;
167
6.95k
    }
168
85.3k
  } else {
169
85.3k
    if (x1 > xMin) {
170
25.1k
      xMin = x1;
171
25.1k
      intBoundsValid = gFalse;
172
25.1k
    }
173
85.3k
    if (x0 < xMax) {
174
13.5k
      xMax = x0;
175
13.5k
      intBoundsValid = gFalse;
176
13.5k
    }
177
85.3k
  }
178
130k
  if (y0 < y1) {
179
71.1k
    if (y0 > yMin) {
180
1.67k
      yMin = y0;
181
1.67k
      intBoundsValid = gFalse;
182
1.67k
    }
183
71.1k
    if (y1 < yMax) {
184
1.80k
      yMax = y1;
185
1.80k
      intBoundsValid = gFalse;
186
1.80k
    }
187
71.1k
  } else {
188
59.7k
    if (y1 > yMin) {
189
7.53k
      yMin = y1;
190
7.53k
      intBoundsValid = gFalse;
191
7.53k
    }
192
59.7k
    if (y0 < yMax) {
193
5.88k
      yMax = y0;
194
5.88k
      intBoundsValid = gFalse;
195
5.88k
    }
196
59.7k
  }
197
130k
  return splashOk;
198
130k
}
199
200
SplashError SplashClip::clipToPath(SplashPath *path, SplashCoord *matrix,
201
           SplashCoord flatness, GBool eoA,
202
           GBool enablePathSimplification,
203
241k
           SplashStrokeAdjustMode strokeAdjust) {
204
241k
  SplashXPath *xPath;
205
241k
  SplashCoord t;
206
207
241k
  xPath = new SplashXPath(path, matrix, flatness, gTrue,
208
241k
        enablePathSimplification,
209
241k
        strokeAdjust, NULL);
210
211
  // check for an empty path
212
241k
  if (xPath->length == 0) {
213
16.3k
    xMin = yMin = 1;
214
16.3k
    xMax = yMax = 0;
215
16.3k
    intBoundsValid = gFalse;
216
16.3k
    delete xPath;
217
16.3k
    return splashOk;
218
16.3k
  }
219
220
  // check for a rectangle
221
225k
  if (xPath->isRect) {
222
130k
    clipToRect(xPath->rectX0, xPath->rectY0, xPath->rectX1, xPath->rectY1);
223
130k
    delete xPath;
224
130k
    return splashOk;
225
130k
  }
226
227
94.4k
  grow(1);
228
94.4k
  paths[length] = xPath;
229
94.4k
  eo[length] = (Guchar)eoA;
230
94.4k
  if ((t = xPath->getXMin()) > xMin) {
231
2.84k
    xMin = t;
232
2.84k
  }
233
94.4k
  if ((t = xPath->getYMin()) > yMin) {
234
12.4k
    yMin = t;
235
12.4k
  }
236
94.4k
  if ((t = xPath->getXMax() + 1) < xMax) {
237
46.2k
    xMax = t;
238
46.2k
  }
239
94.4k
  if ((t = xPath->getYMax() + 1) < yMax) {
240
55.1k
    yMax = t;
241
55.1k
  }
242
94.4k
  intBoundsValid = gFalse;
243
94.4k
  scanners[length] = new SplashXPathScanner(xPath, eoA, splashFloor(yMin),
244
94.4k
              splashCeil(yMax) - 1);
245
94.4k
  ++length;
246
94.4k
  isSimple = gFalse;
247
248
94.4k
  return splashOk;
249
225k
}
250
251
SplashClipResult SplashClip::testRect(int rectXMin, int rectYMin,
252
              int rectXMax, int rectYMax,
253
2.25M
              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.25M
  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.07M
    updateIntBounds(strokeAdjust);
267
1.07M
    if (xMinI > xMaxI || yMinI > yMaxI) {
268
164k
      return splashClipAllOutside;
269
164k
    }
270
910k
    if (rectXMax + 1 <= xMinI ||
271
327k
  rectXMin >= xMaxI + 1 ||
272
148k
  rectYMax + 1 <= yMinI ||
273
880k
  rectYMin >= yMaxI + 1) {
274
880k
      return splashClipAllOutside;
275
880k
    }
276
29.4k
    if (rectXMin >= xMinI &&
277
19.5k
  rectXMax <= xMaxI &&
278
12.3k
  rectYMin >= yMinI &&
279
5.41k
  rectYMax <= yMaxI) {
280
5.16k
      return splashClipAllInside;
281
5.16k
    }
282
1.18M
  } else {
283
1.18M
    if (xMin >= xMax || yMin >= yMax) {
284
516k
      return splashClipAllOutside;
285
516k
    }
286
666k
    if ((SplashCoord)(rectXMax + 1) <= xMin ||
287
230k
  (SplashCoord)rectXMin >= xMax ||
288
24.2k
  (SplashCoord)(rectYMax + 1) <= yMin ||
289
647k
  (SplashCoord)rectYMin >= yMax) {
290
647k
      return splashClipAllOutside;
291
647k
    }
292
18.4k
    if (isSimple &&
293
10.9k
  (SplashCoord)rectXMin >= xMin &&
294
5.34k
  (SplashCoord)(rectXMax + 1) <= xMax &&
295
5.23k
  (SplashCoord)rectYMin >= yMin &&
296
509
  (SplashCoord)(rectYMax + 1) <= yMax) {
297
506
      return splashClipAllInside;
298
506
    }
299
18.4k
  }
300
42.1k
  return splashClipPartial;
301
2.25M
}
302
303
void SplashClip::clipSpan(Guchar *line, int y, int x0, int x1,
304
33.5k
        SplashStrokeAdjustMode strokeAdjust) {
305
33.5k
  SplashClip *clip;
306
33.5k
  SplashCoord d;
307
33.5k
  int x0a, x1a, x0b, x1b, x, i;
308
309
33.5k
  updateIntBounds(strokeAdjust);
310
311
  //--- clip to the integer rectangle
312
313
33.5k
  if (y < yMinI || y > yMaxI ||
314
33.5k
      x1 < xMinI || x0 > xMaxI) {
315
0
    memset(line + x0, 0, x1 - x0 + 1);
316
0
    return;
317
0
  }
318
319
33.5k
  if (x0 > xMinI) {
320
118
    x0a = x0;
321
33.4k
  } else {
322
33.4k
    x0a = xMinI;
323
33.4k
    memset(line + x0, 0, x0a - x0);
324
33.4k
  }
325
326
33.5k
  if (x1 < xMaxI) {
327
20
    x1a = x1;
328
33.5k
  } else {
329
33.5k
    x1a = xMaxI;
330
33.5k
    memset(line + x1a + 1, 0, x1 - x1a);
331
33.5k
  }
332
333
33.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
33.5k
  if (strokeAdjust == splashStrokeAdjustOff) {
341
342
    // clip left edge (xMin)
343
7.88k
    if (x0a == xMinI) {
344
7.76k
      d = (SplashCoord)(xMinI + 1) - xMin;
345
7.76k
      line[x0a] = (Guchar)(int)((SplashCoord)line[x0a] * d);
346
7.76k
    }
347
348
    // clip right edge (xMax)
349
7.88k
    if (x1a == xMaxI) {
350
7.86k
      d = xMax - (SplashCoord)xMaxI;
351
7.86k
      line[x1a] = (Guchar)(int)((SplashCoord)line[x1a] * d);
352
7.86k
    }
353
354
    // clip top edge (yMin)
355
7.88k
    if (y == yMinI) {
356
7.75k
      d = (SplashCoord)(yMinI + 1) - yMin;
357
15.6k
      for (x = x0a; x <= x1a; ++x) {
358
7.89k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
359
7.89k
      }
360
7.75k
    }
361
362
    // clip bottom edge (yMax)
363
7.88k
    if (y == yMaxI) {
364
7.74k
      d = yMax - (SplashCoord)yMaxI;
365
15.6k
      for (x = x0a; x <= x1a; ++x) {
366
7.89k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
367
7.89k
      }
368
7.74k
    }
369
7.88k
  }
370
371
33.5k
  if (isSimple) {
372
28.2k
    return;
373
28.2k
  }
374
375
  //--- clip to the paths
376
377
11.3k
  for (clip = this; clip; clip = clip->prev) {
378
68.8k
    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
1.83k
  memset(line + x0a, 0, x0b - x0a);
382
1.83k
      }
383
123k
      for (x = x0b; x <= x1b; ++x) {
384
60.7k
  line[x] = mul255(line[x], buf[x]);
385
60.7k
      }
386
62.7k
      if (x1b < x1a) {
387
279
  memset(line + x1b + 1, 0, x1a - x1b);
388
279
      }
389
62.7k
    }
390
6.04k
  }
391
5.26k
}
392
393
GBool SplashClip::clipSpanBinary(Guchar *line, int y, int x0, int x1,
394
6.47k
         SplashStrokeAdjustMode strokeAdjust) {
395
6.47k
  SplashClip *clip;
396
6.47k
  int x0a, x1a, x0b, x1b, x, i;
397
6.47k
  Guchar any;
398
399
6.47k
  updateIntBounds(strokeAdjust);
400
401
6.47k
  if (y < yMinI || y > yMaxI ||
402
6.47k
      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
6.47k
  if (x0 > xMinI) {
410
0
    x0a = x0;
411
6.47k
  } else {
412
6.47k
    x0a = xMinI;
413
6.47k
    memset(line + x0, 0, x0a - x0);
414
6.47k
  }
415
416
6.47k
  if (x1 < xMaxI) {
417
0
    x1a = x1;
418
6.47k
  } else {
419
6.47k
    x1a = xMaxI;
420
6.47k
    memset(line + x1a + 1, 0, x1 - x1a);
421
6.47k
  }
422
423
6.47k
  if (x0a > x1a) {
424
0
    return gFalse;
425
0
  }
426
427
6.47k
  if (isSimple) {
428
5.33k
    for (x = x0a; x <= x1a; ++x) {
429
5.33k
      if (line[x]) {
430
5.33k
  return gTrue;
431
5.33k
      }
432
5.33k
    }
433
0
    return gFalse;
434
5.33k
  }
435
436
1.13k
  any = 0;
437
2.39k
  for (clip = this; clip; clip = clip->prev) {
438
5.11k
    for (i = 0; i < clip->length; ++i) {
439
3.85k
      clip->scanners[i]->getSpanBinary(buf, y, x0a, x1a, &x0b, &x1b);
440
3.85k
      if (x0a < x0b) {
441
147
  memset(line + x0a, 0, x0b - x0a);
442
147
      }
443
7.40k
      for (x = x0b; x <= x1b; ++x) {
444
3.55k
  line[x] &= buf[x];
445
3.55k
  any |= line[x];
446
3.55k
      }
447
3.85k
      if (x1b < x1a) {
448
192
  memset(line + x1b + 1, 0, x1a - x1b);
449
192
      }
450
3.85k
    }
451
1.26k
  }
452
453
1.13k
  return any != 0;
454
6.47k
}
455
456
83.8k
int SplashClip::getXMinI(SplashStrokeAdjustMode strokeAdjust) {
457
83.8k
  updateIntBounds(strokeAdjust);
458
83.8k
  return xMinI;
459
83.8k
}
460
461
83.8k
int SplashClip::getXMaxI(SplashStrokeAdjustMode strokeAdjust) {
462
83.8k
  updateIntBounds(strokeAdjust);
463
83.8k
  return xMaxI;
464
83.8k
}
465
466
82.7k
int SplashClip::getYMinI(SplashStrokeAdjustMode strokeAdjust) {
467
82.7k
  updateIntBounds(strokeAdjust);
468
82.7k
  return yMinI;
469
82.7k
}
470
471
82.7k
int SplashClip::getYMaxI(SplashStrokeAdjustMode strokeAdjust) {
472
82.7k
  updateIntBounds(strokeAdjust);
473
82.7k
  return yMaxI;
474
82.7k
}
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.44M
void SplashClip::updateIntBounds(SplashStrokeAdjustMode strokeAdjust) {
488
1.44M
  if (intBoundsValid && strokeAdjust == intBoundsStrokeAdjust) {
489
1.42M
    return;
490
1.42M
  }
491
23.2k
  if (strokeAdjust != splashStrokeAdjustOff && isSimple) {
492
18.2k
    splashStrokeAdjust(xMin, xMax, &xMinI, &xMaxI, strokeAdjust);
493
18.2k
    splashStrokeAdjust(yMin, yMax, &yMinI, &yMaxI, strokeAdjust);
494
18.2k
  } else {
495
5.00k
    xMinI = splashFloor(xMin);
496
5.00k
    yMinI = splashFloor(yMin);
497
5.00k
    xMaxI = splashCeil(xMax);
498
5.00k
    yMaxI = splashCeil(yMax);
499
5.00k
  }
500
23.2k
  if (xMinI < hardXMin) {
501
7
    xMinI = hardXMin;
502
7
  }
503
23.2k
  if (yMinI < hardYMin) {
504
3
    yMinI = hardYMin;
505
3
  }
506
23.2k
  if (xMaxI > hardXMax) {
507
3
    xMaxI = hardXMax;
508
3
  }
509
23.2k
  if (yMaxI > hardYMax) {
510
4
    yMaxI = hardYMax;
511
4
  }
512
  // the clipping code uses [xMinI, xMaxI] instead of [xMinI, xMaxI)
513
23.2k
  --xMaxI;
514
23.2k
  --yMaxI;
515
23.2k
  intBoundsValid = gTrue;
516
23.2k
  intBoundsStrokeAdjust = strokeAdjust;
517
23.2k
}