Coverage Report

Created: 2026-10-31 06:21

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
9.49k
static inline Guchar mul255(Guchar x, Guchar y) {
25
9.49k
  int z;
26
27
9.49k
  z = (int)x * (int)y;
28
9.49k
  return (Guchar)((z + (z >> 8) + 0x80) >> 8);
29
9.49k
}
30
31
//------------------------------------------------------------------------
32
// SplashClip
33
//------------------------------------------------------------------------
34
35
SplashClip::SplashClip(int hardXMinA, int hardYMinA,
36
211k
           int hardXMaxA, int hardYMaxA) {
37
211k
  int w;
38
39
211k
  hardXMin = hardXMinA;
40
211k
  hardYMin = hardYMinA;
41
211k
  hardXMax = hardXMaxA;
42
211k
  hardYMax = hardYMaxA;
43
211k
  xMin = hardXMin;
44
211k
  yMin = hardYMin;
45
211k
  xMax = hardXMax;
46
211k
  yMax = hardYMax;
47
211k
  intBoundsValid = gFalse;
48
211k
  paths = NULL;
49
211k
  eo = NULL;
50
211k
  scanners = NULL;
51
211k
  length = size = 0;
52
211k
  isSimple = gTrue;
53
211k
  prev = NULL;
54
211k
  if ((w = hardXMax + 1) <= 0) {
55
0
    w = 1;
56
0
  }
57
211k
  buf = (Guchar *)gmalloc(w);
58
211k
}
59
60
182k
SplashClip::SplashClip(SplashClip *clip) {
61
182k
  int w;
62
63
182k
  hardXMin = clip->hardXMin;
64
182k
  hardYMin = clip->hardYMin;
65
182k
  hardXMax = clip->hardXMax;
66
182k
  hardYMax = clip->hardYMax;
67
182k
  xMin = clip->xMin;
68
182k
  yMin = clip->yMin;
69
182k
  xMax = clip->xMax;
70
182k
  yMax = clip->yMax;
71
182k
  xMinI = clip->xMinI;
72
182k
  yMinI = clip->yMinI;
73
182k
  xMaxI = clip->xMaxI;
74
182k
  yMaxI = clip->yMaxI;
75
182k
  intBoundsValid = clip->intBoundsValid;
76
182k
  intBoundsStrokeAdjust = clip->intBoundsStrokeAdjust;
77
182k
  paths = NULL;
78
182k
  eo = NULL;
79
182k
  scanners = NULL;
80
182k
  length = size = 0;
81
182k
  isSimple = clip->isSimple;
82
182k
  prev = clip;
83
182k
  if ((w = splashCeil(xMax)) <= 0) {
84
35.4k
    w = 1;
85
35.4k
  }
86
182k
  buf = (Guchar *)gmalloc(w);
87
182k
}
88
89
393k
SplashClip::~SplashClip() {
90
393k
  int i;
91
92
486k
  for (i = 0; i < length; ++i) {
93
93.3k
    delete scanners[i];
94
93.3k
    delete paths[i];
95
93.3k
  }
96
393k
  gfree(paths);
97
393k
  gfree(eo);
98
393k
  gfree(scanners);
99
393k
  gfree(buf);
100
393k
}
101
102
93.3k
void SplashClip::grow(int nPaths) {
103
93.3k
  if (length + nPaths > size) {
104
86.1k
    if (size == 0) {
105
86.1k
      size = 32;
106
86.1k
    }
107
86.1k
    while (size < length + nPaths) {
108
28
      size *= 2;
109
28
    }
110
86.1k
    paths = (SplashXPath **)greallocn(paths, size, sizeof(SplashXPath *));
111
86.1k
    eo = (Guchar *)greallocn(eo, size, sizeof(Guchar));
112
86.1k
    scanners = (SplashXPathScanner **)
113
86.1k
                   greallocn(scanners, size, sizeof(SplashXPathScanner *));
114
86.1k
  }
115
93.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
146k
           SplashCoord x1, SplashCoord y1) {
159
146k
  if (x0 < x1) {
160
67.5k
    if (x0 > xMin) {
161
483
      xMin = x0;
162
483
      intBoundsValid = gFalse;
163
483
    }
164
67.5k
    if (x1 < xMax) {
165
17.4k
      xMax = x1;
166
17.4k
      intBoundsValid = gFalse;
167
17.4k
    }
168
79.0k
  } else {
169
79.0k
    if (x1 > xMin) {
170
28.5k
      xMin = x1;
171
28.5k
      intBoundsValid = gFalse;
172
28.5k
    }
173
79.0k
    if (x0 < xMax) {
174
15.4k
      xMax = x0;
175
15.4k
      intBoundsValid = gFalse;
176
15.4k
    }
177
79.0k
  }
178
146k
  if (y0 < y1) {
179
95.5k
    if (y0 > yMin) {
180
10.5k
      yMin = y0;
181
10.5k
      intBoundsValid = gFalse;
182
10.5k
    }
183
95.5k
    if (y1 < yMax) {
184
2.48k
      yMax = y1;
185
2.48k
      intBoundsValid = gFalse;
186
2.48k
    }
187
95.5k
  } else {
188
50.9k
    if (y1 > yMin) {
189
9.47k
      yMin = y1;
190
9.47k
      intBoundsValid = gFalse;
191
9.47k
    }
192
50.9k
    if (y0 < yMax) {
193
6.89k
      yMax = y0;
194
6.89k
      intBoundsValid = gFalse;
195
6.89k
    }
196
50.9k
  }
197
146k
  return splashOk;
198
146k
}
199
200
SplashError SplashClip::clipToPath(SplashPath *path, SplashCoord *matrix,
201
           SplashCoord flatness, GBool eoA,
202
           GBool enablePathSimplification,
203
250k
           SplashStrokeAdjustMode strokeAdjust) {
204
250k
  SplashXPath *xPath;
205
250k
  SplashCoord t;
206
207
250k
  xPath = new SplashXPath(path, matrix, flatness, gTrue,
208
250k
        enablePathSimplification,
209
250k
        strokeAdjust, NULL);
210
211
  // check for an empty path
212
250k
  if (xPath->length == 0) {
213
10.6k
    xMin = yMin = 1;
214
10.6k
    xMax = yMax = 0;
215
10.6k
    intBoundsValid = gFalse;
216
10.6k
    delete xPath;
217
10.6k
    return splashOk;
218
10.6k
  }
219
220
  // check for a rectangle
221
239k
  if (xPath->isRect) {
222
146k
    clipToRect(xPath->rectX0, xPath->rectY0, xPath->rectX1, xPath->rectY1);
223
146k
    delete xPath;
224
146k
    return splashOk;
225
146k
  }
226
227
93.3k
  grow(1);
228
93.3k
  paths[length] = xPath;
229
93.3k
  eo[length] = (Guchar)eoA;
230
93.3k
  if ((t = xPath->getXMin()) > xMin) {
231
3.18k
    xMin = t;
232
3.18k
  }
233
93.3k
  if ((t = xPath->getYMin()) > yMin) {
234
14.6k
    yMin = t;
235
14.6k
  }
236
93.3k
  if ((t = xPath->getXMax() + 1) < xMax) {
237
31.1k
    xMax = t;
238
31.1k
  }
239
93.3k
  if ((t = xPath->getYMax() + 1) < yMax) {
240
55.1k
    yMax = t;
241
55.1k
  }
242
93.3k
  intBoundsValid = gFalse;
243
93.3k
  scanners[length] = new SplashXPathScanner(xPath, eoA, splashFloor(yMin),
244
93.3k
              splashCeil(yMax) - 1);
245
93.3k
  ++length;
246
93.3k
  isSimple = gFalse;
247
248
93.3k
  return splashOk;
249
239k
}
250
251
SplashClipResult SplashClip::testRect(int rectXMin, int rectYMin,
252
              int rectXMax, int rectYMax,
253
2.32M
              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.32M
  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
815k
    updateIntBounds(strokeAdjust);
267
815k
    if (xMinI > xMaxI || yMinI > yMaxI) {
268
12.3k
      return splashClipAllOutside;
269
12.3k
    }
270
803k
    if (rectXMax + 1 <= xMinI ||
271
382k
  rectXMin >= xMaxI + 1 ||
272
227k
  rectYMax + 1 <= yMinI ||
273
766k
  rectYMin >= yMaxI + 1) {
274
766k
      return splashClipAllOutside;
275
766k
    }
276
36.8k
    if (rectXMin >= xMinI &&
277
27.5k
  rectXMax <= xMaxI &&
278
20.2k
  rectYMin >= yMinI &&
279
4.22k
  rectYMax <= yMaxI) {
280
3.72k
      return splashClipAllInside;
281
3.72k
    }
282
1.50M
  } else {
283
1.50M
    if (xMin >= xMax || yMin >= yMax) {
284
1.26M
      return splashClipAllOutside;
285
1.26M
    }
286
240k
    if ((SplashCoord)(rectXMax + 1) <= xMin ||
287
140k
  (SplashCoord)rectXMin >= xMax ||
288
40.0k
  (SplashCoord)(rectYMax + 1) <= yMin ||
289
219k
  (SplashCoord)rectYMin >= yMax) {
290
219k
      return splashClipAllOutside;
291
219k
    }
292
21.8k
    if (isSimple &&
293
13.0k
  (SplashCoord)rectXMin >= xMin &&
294
7.31k
  (SplashCoord)(rectXMax + 1) <= xMax &&
295
7.00k
  (SplashCoord)rectYMin >= yMin &&
296
161
  (SplashCoord)(rectYMax + 1) <= yMax) {
297
155
      return splashClipAllInside;
298
155
    }
299
21.8k
  }
300
54.8k
  return splashClipPartial;
301
2.32M
}
302
303
void SplashClip::clipSpan(Guchar *line, int y, int x0, int x1,
304
40.8k
        SplashStrokeAdjustMode strokeAdjust) {
305
40.8k
  SplashClip *clip;
306
40.8k
  SplashCoord d;
307
40.8k
  int x0a, x1a, x0b, x1b, x, i;
308
309
40.8k
  updateIntBounds(strokeAdjust);
310
311
  //--- clip to the integer rectangle
312
313
40.8k
  if (y < yMinI || y > yMaxI ||
314
40.8k
      x1 < xMinI || x0 > xMaxI) {
315
0
    memset(line + x0, 0, x1 - x0 + 1);
316
0
    return;
317
0
  }
318
319
40.8k
  if (x0 > xMinI) {
320
154
    x0a = x0;
321
40.6k
  } else {
322
40.6k
    x0a = xMinI;
323
40.6k
    memset(line + x0, 0, x0a - x0);
324
40.6k
  }
325
326
40.8k
  if (x1 < xMaxI) {
327
68
    x1a = x1;
328
40.7k
  } else {
329
40.7k
    x1a = xMaxI;
330
40.7k
    memset(line + x1a + 1, 0, x1 - x1a);
331
40.7k
  }
332
333
40.8k
  if (x0a > x1a) {
334
0
    return;
335
0
  }
336
337
  //--- clip to the floating point rectangle
338
  //    (if stroke adjustment is disabled)
339
340
40.8k
  if (strokeAdjust == splashStrokeAdjustOff) {
341
342
    // clip left edge (xMin)
343
4.19k
    if (x0a == xMinI) {
344
4.04k
      d = (SplashCoord)(xMinI + 1) - xMin;
345
4.04k
      line[x0a] = (Guchar)(int)((SplashCoord)line[x0a] * d);
346
4.04k
    }
347
348
    // clip right edge (xMax)
349
4.19k
    if (x1a == xMaxI) {
350
4.13k
      d = xMax - (SplashCoord)xMaxI;
351
4.13k
      line[x1a] = (Guchar)(int)((SplashCoord)line[x1a] * d);
352
4.13k
    }
353
354
    // clip top edge (yMin)
355
4.19k
    if (y == yMinI) {
356
4.01k
      d = (SplashCoord)(yMinI + 1) - yMin;
357
8.51k
      for (x = x0a; x <= x1a; ++x) {
358
4.50k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
359
4.50k
      }
360
4.01k
    }
361
362
    // clip bottom edge (yMax)
363
4.19k
    if (y == yMaxI) {
364
3.99k
      d = yMax - (SplashCoord)yMaxI;
365
8.37k
      for (x = x0a; x <= x1a; ++x) {
366
4.37k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
367
4.37k
      }
368
3.99k
    }
369
4.19k
  }
370
371
40.8k
  if (isSimple) {
372
35.1k
    return;
373
35.1k
  }
374
375
  //--- clip to the paths
376
377
12.1k
  for (clip = this; clip; clip = clip->prev) {
378
22.0k
    for (i = 0; i < clip->length; ++i) {
379
15.4k
      clip->scanners[i]->getSpan(buf, y, x0a, x1a, &x0b, &x1b);
380
15.4k
      if (x0a < x0b) {
381
5.89k
  memset(line + x0a, 0, x0b - x0a);
382
5.89k
      }
383
24.9k
      for (x = x0b; x <= x1b; ++x) {
384
9.49k
  line[x] = mul255(line[x], buf[x]);
385
9.49k
      }
386
15.4k
      if (x1b < x1a) {
387
5.93k
  memset(line + x1b + 1, 0, x1a - x1b);
388
5.93k
      }
389
15.4k
    }
390
6.52k
  }
391
5.64k
}
392
393
GBool SplashClip::clipSpanBinary(Guchar *line, int y, int x0, int x1,
394
13.2k
         SplashStrokeAdjustMode strokeAdjust) {
395
13.2k
  SplashClip *clip;
396
13.2k
  int x0a, x1a, x0b, x1b, x, i;
397
13.2k
  Guchar any;
398
399
13.2k
  updateIntBounds(strokeAdjust);
400
401
13.2k
  if (y < yMinI || y > yMaxI ||
402
13.2k
      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
13.2k
  if (x0 > xMinI) {
410
0
    x0a = x0;
411
13.2k
  } else {
412
13.2k
    x0a = xMinI;
413
13.2k
    memset(line + x0, 0, x0a - x0);
414
13.2k
  }
415
416
13.2k
  if (x1 < xMaxI) {
417
0
    x1a = x1;
418
13.2k
  } else {
419
13.2k
    x1a = xMaxI;
420
13.2k
    memset(line + x1a + 1, 0, x1 - x1a);
421
13.2k
  }
422
423
13.2k
  if (x0a > x1a) {
424
0
    return gFalse;
425
0
  }
426
427
13.2k
  if (isSimple) {
428
10.3k
    for (x = x0a; x <= x1a; ++x) {
429
10.3k
      if (line[x]) {
430
10.3k
  return gTrue;
431
10.3k
      }
432
10.3k
    }
433
0
    return gFalse;
434
10.3k
  }
435
436
2.87k
  any = 0;
437
5.85k
  for (clip = this; clip; clip = clip->prev) {
438
23.2k
    for (i = 0; i < clip->length; ++i) {
439
20.3k
      clip->scanners[i]->getSpanBinary(buf, y, x0a, x1a, &x0b, &x1b);
440
20.3k
      if (x0a < x0b) {
441
12.3k
  memset(line + x0a, 0, x0b - x0a);
442
12.3k
      }
443
28.2k
      for (x = x0b; x <= x1b; ++x) {
444
7.98k
  line[x] &= buf[x];
445
7.98k
  any |= line[x];
446
7.98k
      }
447
20.3k
      if (x1b < x1a) {
448
12.3k
  memset(line + x1b + 1, 0, x1a - x1b);
449
12.3k
      }
450
20.3k
    }
451
2.98k
  }
452
453
2.87k
  return any != 0;
454
13.2k
}
455
456
106k
int SplashClip::getXMinI(SplashStrokeAdjustMode strokeAdjust) {
457
106k
  updateIntBounds(strokeAdjust);
458
106k
  return xMinI;
459
106k
}
460
461
106k
int SplashClip::getXMaxI(SplashStrokeAdjustMode strokeAdjust) {
462
106k
  updateIntBounds(strokeAdjust);
463
106k
  return xMaxI;
464
106k
}
465
466
104k
int SplashClip::getYMinI(SplashStrokeAdjustMode strokeAdjust) {
467
104k
  updateIntBounds(strokeAdjust);
468
104k
  return yMinI;
469
104k
}
470
471
104k
int SplashClip::getYMaxI(SplashStrokeAdjustMode strokeAdjust) {
472
104k
  updateIntBounds(strokeAdjust);
473
104k
  return yMaxI;
474
104k
}
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.29M
void SplashClip::updateIntBounds(SplashStrokeAdjustMode strokeAdjust) {
488
1.29M
  if (intBoundsValid && strokeAdjust == intBoundsStrokeAdjust) {
489
1.26M
    return;
490
1.26M
  }
491
26.2k
  if (strokeAdjust != splashStrokeAdjustOff && isSimple) {
492
20.7k
    splashStrokeAdjust(xMin, xMax, &xMinI, &xMaxI, strokeAdjust);
493
20.7k
    splashStrokeAdjust(yMin, yMax, &yMinI, &yMaxI, strokeAdjust);
494
20.7k
  } else {
495
5.45k
    xMinI = splashFloor(xMin);
496
5.45k
    yMinI = splashFloor(yMin);
497
5.45k
    xMaxI = splashCeil(xMax);
498
5.45k
    yMaxI = splashCeil(yMax);
499
5.45k
  }
500
26.2k
  if (xMinI < hardXMin) {
501
6
    xMinI = hardXMin;
502
6
  }
503
26.2k
  if (yMinI < hardYMin) {
504
6
    yMinI = hardYMin;
505
6
  }
506
26.2k
  if (xMaxI > hardXMax) {
507
6
    xMaxI = hardXMax;
508
6
  }
509
26.2k
  if (yMaxI > hardYMax) {
510
3
    yMaxI = hardYMax;
511
3
  }
512
  // the clipping code uses [xMinI, xMaxI] instead of [xMinI, xMaxI)
513
26.2k
  --xMaxI;
514
26.2k
  --yMaxI;
515
26.2k
  intBoundsValid = gTrue;
516
26.2k
  intBoundsStrokeAdjust = strokeAdjust;
517
26.2k
}