Coverage Report

Created: 2026-03-31 07:04

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