Coverage Report

Created: 2026-06-10 06:33

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