Coverage Report

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