Coverage Report

Created: 2026-08-31 06:45

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
256k
           int hardXMaxA, int hardYMaxA) {
37
256k
  int w;
38
39
256k
  hardXMin = hardXMinA;
40
256k
  hardYMin = hardYMinA;
41
256k
  hardXMax = hardXMaxA;
42
256k
  hardYMax = hardYMaxA;
43
256k
  xMin = hardXMin;
44
256k
  yMin = hardYMin;
45
256k
  xMax = hardXMax;
46
256k
  yMax = hardYMax;
47
256k
  intBoundsValid = gFalse;
48
256k
  paths = NULL;
49
256k
  eo = NULL;
50
256k
  scanners = NULL;
51
256k
  length = size = 0;
52
256k
  isSimple = gTrue;
53
256k
  prev = NULL;
54
256k
  if ((w = hardXMax + 1) <= 0) {
55
0
    w = 1;
56
0
  }
57
256k
  buf = (Guchar *)gmalloc(w);
58
256k
}
59
60
149k
SplashClip::SplashClip(SplashClip *clip) {
61
149k
  int w;
62
63
149k
  hardXMin = clip->hardXMin;
64
149k
  hardYMin = clip->hardYMin;
65
149k
  hardXMax = clip->hardXMax;
66
149k
  hardYMax = clip->hardYMax;
67
149k
  xMin = clip->xMin;
68
149k
  yMin = clip->yMin;
69
149k
  xMax = clip->xMax;
70
149k
  yMax = clip->yMax;
71
149k
  xMinI = clip->xMinI;
72
149k
  yMinI = clip->yMinI;
73
149k
  xMaxI = clip->xMaxI;
74
149k
  yMaxI = clip->yMaxI;
75
149k
  intBoundsValid = clip->intBoundsValid;
76
149k
  intBoundsStrokeAdjust = clip->intBoundsStrokeAdjust;
77
149k
  paths = NULL;
78
149k
  eo = NULL;
79
149k
  scanners = NULL;
80
149k
  length = size = 0;
81
149k
  isSimple = clip->isSimple;
82
149k
  prev = clip;
83
149k
  if ((w = splashCeil(xMax)) <= 0) {
84
24.5k
    w = 1;
85
24.5k
  }
86
149k
  buf = (Guchar *)gmalloc(w);
87
149k
}
88
89
405k
SplashClip::~SplashClip() {
90
405k
  int i;
91
92
483k
  for (i = 0; i < length; ++i) {
93
78.4k
    delete scanners[i];
94
78.4k
    delete paths[i];
95
78.4k
  }
96
405k
  gfree(paths);
97
405k
  gfree(eo);
98
405k
  gfree(scanners);
99
405k
  gfree(buf);
100
405k
}
101
102
78.5k
void SplashClip::grow(int nPaths) {
103
78.5k
  if (length + nPaths > size) {
104
71.0k
    if (size == 0) {
105
70.9k
      size = 32;
106
70.9k
    }
107
71.0k
    while (size < length + nPaths) {
108
22
      size *= 2;
109
22
    }
110
71.0k
    paths = (SplashXPath **)greallocn(paths, size, sizeof(SplashXPath *));
111
71.0k
    eo = (Guchar *)greallocn(eo, size, sizeof(Guchar));
112
71.0k
    scanners = (SplashXPathScanner **)
113
71.0k
                   greallocn(scanners, size, sizeof(SplashXPathScanner *));
114
71.0k
  }
115
78.5k
}
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
96.5k
           SplashCoord x1, SplashCoord y1) {
159
96.5k
  if (x0 < x1) {
160
47.8k
    if (x0 > xMin) {
161
588
      xMin = x0;
162
588
      intBoundsValid = gFalse;
163
588
    }
164
47.8k
    if (x1 < xMax) {
165
7.51k
      xMax = x1;
166
7.51k
      intBoundsValid = gFalse;
167
7.51k
    }
168
48.7k
  } else {
169
48.7k
    if (x1 > xMin) {
170
24.0k
      xMin = x1;
171
24.0k
      intBoundsValid = gFalse;
172
24.0k
    }
173
48.7k
    if (x0 < xMax) {
174
8.22k
      xMax = x0;
175
8.22k
      intBoundsValid = gFalse;
176
8.22k
    }
177
48.7k
  }
178
96.5k
  if (y0 < y1) {
179
70.4k
    if (y0 > yMin) {
180
30.7k
      yMin = y0;
181
30.7k
      intBoundsValid = gFalse;
182
30.7k
    }
183
70.4k
    if (y1 < yMax) {
184
2.19k
      yMax = y1;
185
2.19k
      intBoundsValid = gFalse;
186
2.19k
    }
187
70.4k
  } else {
188
26.1k
    if (y1 > yMin) {
189
4.36k
      yMin = y1;
190
4.36k
      intBoundsValid = gFalse;
191
4.36k
    }
192
26.1k
    if (y0 < yMax) {
193
4.15k
      yMax = y0;
194
4.15k
      intBoundsValid = gFalse;
195
4.15k
    }
196
26.1k
  }
197
96.5k
  return splashOk;
198
96.5k
}
199
200
SplashError SplashClip::clipToPath(SplashPath *path, SplashCoord *matrix,
201
           SplashCoord flatness, GBool eoA,
202
           GBool enablePathSimplification,
203
187k
           SplashStrokeAdjustMode strokeAdjust) {
204
187k
  SplashXPath *xPath;
205
187k
  SplashCoord t;
206
207
187k
  xPath = new SplashXPath(path, matrix, flatness, gTrue,
208
187k
        enablePathSimplification,
209
187k
        strokeAdjust, NULL);
210
211
  // check for an empty path
212
187k
  if (xPath->length == 0) {
213
12.1k
    xMin = yMin = 1;
214
12.1k
    xMax = yMax = 0;
215
12.1k
    intBoundsValid = gFalse;
216
12.1k
    delete xPath;
217
12.1k
    return splashOk;
218
12.1k
  }
219
220
  // check for a rectangle
221
175k
  if (xPath->isRect) {
222
96.5k
    clipToRect(xPath->rectX0, xPath->rectY0, xPath->rectX1, xPath->rectY1);
223
96.5k
    delete xPath;
224
96.5k
    return splashOk;
225
96.5k
  }
226
227
78.5k
  grow(1);
228
78.5k
  paths[length] = xPath;
229
78.5k
  eo[length] = (Guchar)eoA;
230
78.5k
  if ((t = xPath->getXMin()) > xMin) {
231
1.97k
    xMin = t;
232
1.97k
  }
233
78.5k
  if ((t = xPath->getYMin()) > yMin) {
234
10.5k
    yMin = t;
235
10.5k
  }
236
78.5k
  if ((t = xPath->getXMax() + 1) < xMax) {
237
39.0k
    xMax = t;
238
39.0k
  }
239
78.5k
  if ((t = xPath->getYMax() + 1) < yMax) {
240
46.8k
    yMax = t;
241
46.8k
  }
242
78.5k
  intBoundsValid = gFalse;
243
78.5k
  scanners[length] = new SplashXPathScanner(xPath, eoA, splashFloor(yMin),
244
78.5k
              splashCeil(yMax) - 1);
245
78.5k
  ++length;
246
78.5k
  isSimple = gFalse;
247
248
78.5k
  return splashOk;
249
175k
}
250
251
SplashClipResult SplashClip::testRect(int rectXMin, int rectYMin,
252
              int rectXMax, int rectYMax,
253
2.91M
              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.91M
  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.54M
    updateIntBounds(strokeAdjust);
267
1.54M
    if (xMinI > xMaxI || yMinI > yMaxI) {
268
6.24k
      return splashClipAllOutside;
269
6.24k
    }
270
1.53M
    if (rectXMax + 1 <= xMinI ||
271
723k
  rectXMin >= xMaxI + 1 ||
272
353k
  rectYMax + 1 <= yMinI ||
273
1.51M
  rectYMin >= yMaxI + 1) {
274
1.51M
      return splashClipAllOutside;
275
1.51M
    }
276
26.1k
    if (rectXMin >= xMinI &&
277
16.1k
  rectXMax <= xMaxI &&
278
11.1k
  rectYMin >= yMinI &&
279
5.83k
  rectYMax <= yMaxI) {
280
5.52k
      return splashClipAllInside;
281
5.52k
    }
282
1.37M
  } else {
283
1.37M
    if (xMin >= xMax || yMin >= yMax) {
284
783k
      return splashClipAllOutside;
285
783k
    }
286
588k
    if ((SplashCoord)(rectXMax + 1) <= xMin ||
287
201k
  (SplashCoord)rectXMin >= xMax ||
288
26.4k
  (SplashCoord)(rectYMax + 1) <= yMin ||
289
573k
  (SplashCoord)rectYMin >= yMax) {
290
573k
      return splashClipAllOutside;
291
573k
    }
292
14.5k
    if (isSimple &&
293
6.68k
  (SplashCoord)rectXMin >= xMin &&
294
2.48k
  (SplashCoord)(rectXMax + 1) <= xMax &&
295
2.32k
  (SplashCoord)rectYMin >= yMin &&
296
780
  (SplashCoord)(rectYMax + 1) <= yMax) {
297
776
      return splashClipAllInside;
298
776
    }
299
14.5k
  }
300
34.3k
  return splashClipPartial;
301
2.91M
}
302
303
void SplashClip::clipSpan(Guchar *line, int y, int x0, int x1,
304
28.1k
        SplashStrokeAdjustMode strokeAdjust) {
305
28.1k
  SplashClip *clip;
306
28.1k
  SplashCoord d;
307
28.1k
  int x0a, x1a, x0b, x1b, x, i;
308
309
28.1k
  updateIntBounds(strokeAdjust);
310
311
  //--- clip to the integer rectangle
312
313
28.1k
  if (y < yMinI || y > yMaxI ||
314
28.1k
      x1 < xMinI || x0 > xMaxI) {
315
0
    memset(line + x0, 0, x1 - x0 + 1);
316
0
    return;
317
0
  }
318
319
28.1k
  if (x0 > xMinI) {
320
120
    x0a = x0;
321
28.0k
  } else {
322
28.0k
    x0a = xMinI;
323
28.0k
    memset(line + x0, 0, x0a - x0);
324
28.0k
  }
325
326
28.1k
  if (x1 < xMaxI) {
327
34
    x1a = x1;
328
28.0k
  } else {
329
28.0k
    x1a = xMaxI;
330
28.0k
    memset(line + x1a + 1, 0, x1 - x1a);
331
28.0k
  }
332
333
28.1k
  if (x0a > x1a) {
334
0
    return;
335
0
  }
336
337
  //--- clip to the floating point rectangle
338
  //    (if stroke adjustment is disabled)
339
340
28.1k
  if (strokeAdjust == splashStrokeAdjustOff) {
341
342
    // clip left edge (xMin)
343
6.69k
    if (x0a == xMinI) {
344
6.57k
      d = (SplashCoord)(xMinI + 1) - xMin;
345
6.57k
      line[x0a] = (Guchar)(int)((SplashCoord)line[x0a] * d);
346
6.57k
    }
347
348
    // clip right edge (xMax)
349
6.69k
    if (x1a == xMaxI) {
350
6.65k
      d = xMax - (SplashCoord)xMaxI;
351
6.65k
      line[x1a] = (Guchar)(int)((SplashCoord)line[x1a] * d);
352
6.65k
    }
353
354
    // clip top edge (yMin)
355
6.69k
    if (y == yMinI) {
356
6.55k
      d = (SplashCoord)(yMinI + 1) - yMin;
357
13.2k
      for (x = x0a; x <= x1a; ++x) {
358
6.73k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
359
6.73k
      }
360
6.55k
    }
361
362
    // clip bottom edge (yMax)
363
6.69k
    if (y == yMaxI) {
364
6.54k
      d = yMax - (SplashCoord)yMaxI;
365
13.2k
      for (x = x0a; x <= x1a; ++x) {
366
6.71k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
367
6.71k
      }
368
6.54k
    }
369
6.69k
  }
370
371
28.1k
  if (isSimple) {
372
22.9k
    return;
373
22.9k
  }
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
784
  memset(line + x0a, 0, x0b - x0a);
382
784
      }
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
966
  memset(line + x1b + 1, 0, x1a - x1b);
388
966
      }
389
62.7k
    }
390
5.78k
  }
391
5.20k
}
392
393
GBool SplashClip::clipSpanBinary(Guchar *line, int y, int x0, int x1,
394
4.41k
         SplashStrokeAdjustMode strokeAdjust) {
395
4.41k
  SplashClip *clip;
396
4.41k
  int x0a, x1a, x0b, x1b, x, i;
397
4.41k
  Guchar any;
398
399
4.41k
  updateIntBounds(strokeAdjust);
400
401
4.41k
  if (y < yMinI || y > yMaxI ||
402
4.41k
      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.41k
  if (x0 > xMinI) {
410
0
    x0a = x0;
411
4.41k
  } else {
412
4.41k
    x0a = xMinI;
413
4.41k
    memset(line + x0, 0, x0a - x0);
414
4.41k
  }
415
416
4.41k
  if (x1 < xMaxI) {
417
0
    x1a = x1;
418
4.41k
  } else {
419
4.41k
    x1a = xMaxI;
420
4.41k
    memset(line + x1a + 1, 0, x1 - x1a);
421
4.41k
  }
422
423
4.41k
  if (x0a > x1a) {
424
0
    return gFalse;
425
0
  }
426
427
4.41k
  if (isSimple) {
428
2.71k
    for (x = x0a; x <= x1a; ++x) {
429
2.71k
      if (line[x]) {
430
2.71k
  return gTrue;
431
2.71k
      }
432
2.71k
    }
433
0
    return gFalse;
434
2.71k
  }
435
436
1.69k
  any = 0;
437
3.55k
  for (clip = this; clip; clip = clip->prev) {
438
9.72k
    for (i = 0; i < clip->length; ++i) {
439
7.85k
      clip->scanners[i]->getSpanBinary(buf, y, x0a, x1a, &x0b, &x1b);
440
7.85k
      if (x0a < x0b) {
441
73
  memset(line + x0a, 0, x0b - x0a);
442
73
      }
443
15.2k
      for (x = x0b; x <= x1b; ++x) {
444
7.37k
  line[x] &= buf[x];
445
7.37k
  any |= line[x];
446
7.37k
      }
447
7.85k
      if (x1b < x1a) {
448
461
  memset(line + x1b + 1, 0, x1a - x1b);
449
461
      }
450
7.85k
    }
451
1.86k
  }
452
453
1.69k
  return any != 0;
454
4.41k
}
455
456
70.6k
int SplashClip::getXMinI(SplashStrokeAdjustMode strokeAdjust) {
457
70.6k
  updateIntBounds(strokeAdjust);
458
70.6k
  return xMinI;
459
70.6k
}
460
461
70.6k
int SplashClip::getXMaxI(SplashStrokeAdjustMode strokeAdjust) {
462
70.6k
  updateIntBounds(strokeAdjust);
463
70.6k
  return xMaxI;
464
70.6k
}
465
466
69.2k
int SplashClip::getYMinI(SplashStrokeAdjustMode strokeAdjust) {
467
69.2k
  updateIntBounds(strokeAdjust);
468
69.2k
  return yMinI;
469
69.2k
}
470
471
69.2k
int SplashClip::getYMaxI(SplashStrokeAdjustMode strokeAdjust) {
472
69.2k
  updateIntBounds(strokeAdjust);
473
69.2k
  return yMaxI;
474
69.2k
}
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.85M
void SplashClip::updateIntBounds(SplashStrokeAdjustMode strokeAdjust) {
488
1.85M
  if (intBoundsValid && strokeAdjust == intBoundsStrokeAdjust) {
489
1.83M
    return;
490
1.83M
  }
491
19.1k
  if (strokeAdjust != splashStrokeAdjustOff && isSimple) {
492
15.5k
    splashStrokeAdjust(xMin, xMax, &xMinI, &xMaxI, strokeAdjust);
493
15.5k
    splashStrokeAdjust(yMin, yMax, &yMinI, &yMaxI, strokeAdjust);
494
15.5k
  } else {
495
3.56k
    xMinI = splashFloor(xMin);
496
3.56k
    yMinI = splashFloor(yMin);
497
3.56k
    xMaxI = splashCeil(xMax);
498
3.56k
    yMaxI = splashCeil(yMax);
499
3.56k
  }
500
19.1k
  if (xMinI < hardXMin) {
501
4
    xMinI = hardXMin;
502
4
  }
503
19.1k
  if (yMinI < hardYMin) {
504
6
    yMinI = hardYMin;
505
6
  }
506
19.1k
  if (xMaxI > hardXMax) {
507
4
    xMaxI = hardXMax;
508
4
  }
509
19.1k
  if (yMaxI > hardYMax) {
510
6
    yMaxI = hardYMax;
511
6
  }
512
  // the clipping code uses [xMinI, xMaxI] instead of [xMinI, xMaxI)
513
19.1k
  --xMaxI;
514
19.1k
  --yMaxI;
515
19.1k
  intBoundsValid = gTrue;
516
19.1k
  intBoundsStrokeAdjust = strokeAdjust;
517
19.1k
}