Coverage Report

Created: 2026-12-01 06:38

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
21.7k
static inline Guchar mul255(Guchar x, Guchar y) {
25
21.7k
  int z;
26
27
21.7k
  z = (int)x * (int)y;
28
21.7k
  return (Guchar)((z + (z >> 8) + 0x80) >> 8);
29
21.7k
}
30
31
//------------------------------------------------------------------------
32
// SplashClip
33
//------------------------------------------------------------------------
34
35
SplashClip::SplashClip(int hardXMinA, int hardYMinA,
36
179k
           int hardXMaxA, int hardYMaxA) {
37
179k
  int w;
38
39
179k
  hardXMin = hardXMinA;
40
179k
  hardYMin = hardYMinA;
41
179k
  hardXMax = hardXMaxA;
42
179k
  hardYMax = hardYMaxA;
43
179k
  xMin = hardXMin;
44
179k
  yMin = hardYMin;
45
179k
  xMax = hardXMax;
46
179k
  yMax = hardYMax;
47
179k
  intBoundsValid = gFalse;
48
179k
  paths = NULL;
49
179k
  eo = NULL;
50
179k
  scanners = NULL;
51
179k
  length = size = 0;
52
179k
  isSimple = gTrue;
53
179k
  prev = NULL;
54
179k
  if ((w = hardXMax + 1) <= 0) {
55
0
    w = 1;
56
0
  }
57
179k
  buf = (Guchar *)gmalloc(w);
58
179k
}
59
60
129k
SplashClip::SplashClip(SplashClip *clip) {
61
129k
  int w;
62
63
129k
  hardXMin = clip->hardXMin;
64
129k
  hardYMin = clip->hardYMin;
65
129k
  hardXMax = clip->hardXMax;
66
129k
  hardYMax = clip->hardYMax;
67
129k
  xMin = clip->xMin;
68
129k
  yMin = clip->yMin;
69
129k
  xMax = clip->xMax;
70
129k
  yMax = clip->yMax;
71
129k
  xMinI = clip->xMinI;
72
129k
  yMinI = clip->yMinI;
73
129k
  xMaxI = clip->xMaxI;
74
129k
  yMaxI = clip->yMaxI;
75
129k
  intBoundsValid = clip->intBoundsValid;
76
129k
  intBoundsStrokeAdjust = clip->intBoundsStrokeAdjust;
77
129k
  paths = NULL;
78
129k
  eo = NULL;
79
129k
  scanners = NULL;
80
129k
  length = size = 0;
81
129k
  isSimple = clip->isSimple;
82
129k
  prev = clip;
83
129k
  if ((w = splashCeil(xMax)) <= 0) {
84
29.3k
    w = 1;
85
29.3k
  }
86
129k
  buf = (Guchar *)gmalloc(w);
87
129k
}
88
89
308k
SplashClip::~SplashClip() {
90
308k
  int i;
91
92
358k
  for (i = 0; i < length; ++i) {
93
50.1k
    delete scanners[i];
94
50.1k
    delete paths[i];
95
50.1k
  }
96
308k
  gfree(paths);
97
308k
  gfree(eo);
98
308k
  gfree(scanners);
99
308k
  gfree(buf);
100
308k
}
101
102
50.2k
void SplashClip::grow(int nPaths) {
103
50.2k
  if (length + nPaths > size) {
104
44.3k
    if (size == 0) {
105
44.3k
      size = 32;
106
44.3k
    }
107
44.4k
    while (size < length + nPaths) {
108
17
      size *= 2;
109
17
    }
110
44.3k
    paths = (SplashXPath **)greallocn(paths, size, sizeof(SplashXPath *));
111
44.3k
    eo = (Guchar *)greallocn(eo, size, sizeof(Guchar));
112
44.3k
    scanners = (SplashXPathScanner **)
113
44.3k
                   greallocn(scanners, size, sizeof(SplashXPathScanner *));
114
44.3k
  }
115
50.2k
}
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
101k
           SplashCoord x1, SplashCoord y1) {
159
101k
  if (x0 < x1) {
160
33.5k
    if (x0 > xMin) {
161
120
      xMin = x0;
162
120
      intBoundsValid = gFalse;
163
120
    }
164
33.5k
    if (x1 < xMax) {
165
4.68k
      xMax = x1;
166
4.68k
      intBoundsValid = gFalse;
167
4.68k
    }
168
67.6k
  } else {
169
67.6k
    if (x1 > xMin) {
170
17.9k
      xMin = x1;
171
17.9k
      intBoundsValid = gFalse;
172
17.9k
    }
173
67.6k
    if (x0 < xMax) {
174
17.7k
      xMax = x0;
175
17.7k
      intBoundsValid = gFalse;
176
17.7k
    }
177
67.6k
  }
178
101k
  if (y0 < y1) {
179
49.2k
    if (y0 > yMin) {
180
10.5k
      yMin = y0;
181
10.5k
      intBoundsValid = gFalse;
182
10.5k
    }
183
49.2k
    if (y1 < yMax) {
184
2.34k
      yMax = y1;
185
2.34k
      intBoundsValid = gFalse;
186
2.34k
    }
187
51.9k
  } else {
188
51.9k
    if (y1 > yMin) {
189
9.00k
      yMin = y1;
190
9.00k
      intBoundsValid = gFalse;
191
9.00k
    }
192
51.9k
    if (y0 < yMax) {
193
11.0k
      yMax = y0;
194
11.0k
      intBoundsValid = gFalse;
195
11.0k
    }
196
51.9k
  }
197
101k
  return splashOk;
198
101k
}
199
200
SplashError SplashClip::clipToPath(SplashPath *path, SplashCoord *matrix,
201
           SplashCoord flatness, GBool eoA,
202
           GBool enablePathSimplification,
203
159k
           SplashStrokeAdjustMode strokeAdjust) {
204
159k
  SplashXPath *xPath;
205
159k
  SplashCoord t;
206
207
159k
  xPath = new SplashXPath(path, matrix, flatness, gTrue,
208
159k
        enablePathSimplification,
209
159k
        strokeAdjust, NULL);
210
211
  // check for an empty path
212
159k
  if (xPath->length == 0) {
213
8.45k
    xMin = yMin = 1;
214
8.45k
    xMax = yMax = 0;
215
8.45k
    intBoundsValid = gFalse;
216
8.45k
    delete xPath;
217
8.45k
    return splashOk;
218
8.45k
  }
219
220
  // check for a rectangle
221
151k
  if (xPath->isRect) {
222
101k
    clipToRect(xPath->rectX0, xPath->rectY0, xPath->rectX1, xPath->rectY1);
223
101k
    delete xPath;
224
101k
    return splashOk;
225
101k
  }
226
227
50.2k
  grow(1);
228
50.2k
  paths[length] = xPath;
229
50.2k
  eo[length] = (Guchar)eoA;
230
50.2k
  if ((t = xPath->getXMin()) > xMin) {
231
3.07k
    xMin = t;
232
3.07k
  }
233
50.2k
  if ((t = xPath->getYMin()) > yMin) {
234
9.14k
    yMin = t;
235
9.14k
  }
236
50.2k
  if ((t = xPath->getXMax() + 1) < xMax) {
237
9.23k
    xMax = t;
238
9.23k
  }
239
50.2k
  if ((t = xPath->getYMax() + 1) < yMax) {
240
18.8k
    yMax = t;
241
18.8k
  }
242
50.2k
  intBoundsValid = gFalse;
243
50.2k
  scanners[length] = new SplashXPathScanner(xPath, eoA, splashFloor(yMin),
244
50.2k
              splashCeil(yMax) - 1);
245
50.2k
  ++length;
246
50.2k
  isSimple = gFalse;
247
248
50.2k
  return splashOk;
249
151k
}
250
251
SplashClipResult SplashClip::testRect(int rectXMin, int rectYMin,
252
              int rectXMax, int rectYMax,
253
2.35M
              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.35M
  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
832k
    updateIntBounds(strokeAdjust);
267
832k
    if (xMinI > xMaxI || yMinI > yMaxI) {
268
14.3k
      return splashClipAllOutside;
269
14.3k
    }
270
818k
    if (rectXMax + 1 <= xMinI ||
271
410k
  rectXMin >= xMaxI + 1 ||
272
194k
  rectYMax + 1 <= yMinI ||
273
791k
  rectYMin >= yMaxI + 1) {
274
791k
      return splashClipAllOutside;
275
791k
    }
276
26.5k
    if (rectXMin >= xMinI &&
277
14.6k
  rectXMax <= xMaxI &&
278
8.31k
  rectYMin >= yMinI &&
279
2.81k
  rectYMax <= yMaxI) {
280
2.61k
      return splashClipAllInside;
281
2.61k
    }
282
1.51M
  } else {
283
1.51M
    if (xMin >= xMax || yMin >= yMax) {
284
995k
      return splashClipAllOutside;
285
995k
    }
286
523k
    if ((SplashCoord)(rectXMax + 1) <= xMin ||
287
252k
  (SplashCoord)rectXMin >= xMax ||
288
25.8k
  (SplashCoord)(rectYMax + 1) <= yMin ||
289
507k
  (SplashCoord)rectYMin >= yMax) {
290
507k
      return splashClipAllOutside;
291
507k
    }
292
15.2k
    if (isSimple &&
293
8.60k
  (SplashCoord)rectXMin >= xMin &&
294
5.79k
  (SplashCoord)(rectXMax + 1) <= xMax &&
295
5.23k
  (SplashCoord)rectYMin >= yMin &&
296
4.47k
  (SplashCoord)(rectYMax + 1) <= yMax) {
297
4.47k
      return splashClipAllInside;
298
4.47k
    }
299
15.2k
  }
300
34.7k
  return splashClipPartial;
301
2.35M
}
302
303
void SplashClip::clipSpan(Guchar *line, int y, int x0, int x1,
304
26.6k
        SplashStrokeAdjustMode strokeAdjust) {
305
26.6k
  SplashClip *clip;
306
26.6k
  SplashCoord d;
307
26.6k
  int x0a, x1a, x0b, x1b, x, i;
308
309
26.6k
  updateIntBounds(strokeAdjust);
310
311
  //--- clip to the integer rectangle
312
313
26.6k
  if (y < yMinI || y > yMaxI ||
314
26.6k
      x1 < xMinI || x0 > xMaxI) {
315
0
    memset(line + x0, 0, x1 - x0 + 1);
316
0
    return;
317
0
  }
318
319
26.6k
  if (x0 > xMinI) {
320
326
    x0a = x0;
321
26.3k
  } else {
322
26.3k
    x0a = xMinI;
323
26.3k
    memset(line + x0, 0, x0a - x0);
324
26.3k
  }
325
326
26.6k
  if (x1 < xMaxI) {
327
352
    x1a = x1;
328
26.2k
  } else {
329
26.2k
    x1a = xMaxI;
330
26.2k
    memset(line + x1a + 1, 0, x1 - x1a);
331
26.2k
  }
332
333
26.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
26.6k
  if (strokeAdjust == splashStrokeAdjustOff) {
341
342
    // clip left edge (xMin)
343
2.82k
    if (x0a == xMinI) {
344
2.49k
      d = (SplashCoord)(xMinI + 1) - xMin;
345
2.49k
      line[x0a] = (Guchar)(int)((SplashCoord)line[x0a] * d);
346
2.49k
    }
347
348
    // clip right edge (xMax)
349
2.82k
    if (x1a == xMaxI) {
350
2.46k
      d = xMax - (SplashCoord)xMaxI;
351
2.46k
      line[x1a] = (Guchar)(int)((SplashCoord)line[x1a] * d);
352
2.46k
    }
353
354
    // clip top edge (yMin)
355
2.82k
    if (y == yMinI) {
356
2.15k
      d = (SplashCoord)(yMinI + 1) - yMin;
357
4.51k
      for (x = x0a; x <= x1a; ++x) {
358
2.35k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
359
2.35k
      }
360
2.15k
    }
361
362
    // clip bottom edge (yMax)
363
2.82k
    if (y == yMaxI) {
364
2.16k
      d = yMax - (SplashCoord)yMaxI;
365
4.83k
      for (x = x0a; x <= x1a; ++x) {
366
2.66k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
367
2.66k
      }
368
2.16k
    }
369
2.82k
  }
370
371
26.6k
  if (isSimple) {
372
22.4k
    return;
373
22.4k
  }
374
375
  //--- clip to the paths
376
377
8.75k
  for (clip = this; clip; clip = clip->prev) {
378
27.0k
    for (i = 0; i < clip->length; ++i) {
379
22.4k
      clip->scanners[i]->getSpan(buf, y, x0a, x1a, &x0b, &x1b);
380
22.4k
      if (x0a < x0b) {
381
619
  memset(line + x0a, 0, x0b - x0a);
382
619
      }
383
44.2k
      for (x = x0b; x <= x1b; ++x) {
384
21.7k
  line[x] = mul255(line[x], buf[x]);
385
21.7k
      }
386
22.4k
      if (x1b < x1a) {
387
682
  memset(line + x1b + 1, 0, x1a - x1b);
388
682
      }
389
22.4k
    }
390
4.53k
  }
391
4.21k
}
392
393
GBool SplashClip::clipSpanBinary(Guchar *line, int y, int x0, int x1,
394
6.25k
         SplashStrokeAdjustMode strokeAdjust) {
395
6.25k
  SplashClip *clip;
396
6.25k
  int x0a, x1a, x0b, x1b, x, i;
397
6.25k
  Guchar any;
398
399
6.25k
  updateIntBounds(strokeAdjust);
400
401
6.25k
  if (y < yMinI || y > yMaxI ||
402
6.25k
      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.25k
  if (x0 > xMinI) {
410
0
    x0a = x0;
411
6.25k
  } else {
412
6.25k
    x0a = xMinI;
413
6.25k
    memset(line + x0, 0, x0a - x0);
414
6.25k
  }
415
416
6.25k
  if (x1 < xMaxI) {
417
0
    x1a = x1;
418
6.25k
  } else {
419
6.25k
    x1a = xMaxI;
420
6.25k
    memset(line + x1a + 1, 0, x1 - x1a);
421
6.25k
  }
422
423
6.25k
  if (x0a > x1a) {
424
0
    return gFalse;
425
0
  }
426
427
6.25k
  if (isSimple) {
428
3.73k
    for (x = x0a; x <= x1a; ++x) {
429
3.73k
      if (line[x]) {
430
3.73k
  return gTrue;
431
3.73k
      }
432
3.73k
    }
433
0
    return gFalse;
434
3.73k
  }
435
436
2.52k
  any = 0;
437
5.09k
  for (clip = this; clip; clip = clip->prev) {
438
14.3k
    for (i = 0; i < clip->length; ++i) {
439
11.8k
      clip->scanners[i]->getSpanBinary(buf, y, x0a, x1a, &x0b, &x1b);
440
11.8k
      if (x0a < x0b) {
441
511
  memset(line + x0a, 0, x0b - x0a);
442
511
      }
443
23.1k
      for (x = x0b; x <= x1b; ++x) {
444
11.2k
  line[x] &= buf[x];
445
11.2k
  any |= line[x];
446
11.2k
      }
447
11.8k
      if (x1b < x1a) {
448
501
  memset(line + x1b + 1, 0, x1a - x1b);
449
501
      }
450
11.8k
    }
451
2.57k
  }
452
453
2.52k
  return any != 0;
454
6.25k
}
455
456
74.9k
int SplashClip::getXMinI(SplashStrokeAdjustMode strokeAdjust) {
457
74.9k
  updateIntBounds(strokeAdjust);
458
74.9k
  return xMinI;
459
74.9k
}
460
461
74.9k
int SplashClip::getXMaxI(SplashStrokeAdjustMode strokeAdjust) {
462
74.9k
  updateIntBounds(strokeAdjust);
463
74.9k
  return xMaxI;
464
74.9k
}
465
466
73.0k
int SplashClip::getYMinI(SplashStrokeAdjustMode strokeAdjust) {
467
73.0k
  updateIntBounds(strokeAdjust);
468
73.0k
  return yMinI;
469
73.0k
}
470
471
73.0k
int SplashClip::getYMaxI(SplashStrokeAdjustMode strokeAdjust) {
472
73.0k
  updateIntBounds(strokeAdjust);
473
73.0k
  return yMaxI;
474
73.0k
}
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.16M
void SplashClip::updateIntBounds(SplashStrokeAdjustMode strokeAdjust) {
488
1.16M
  if (intBoundsValid && strokeAdjust == intBoundsStrokeAdjust) {
489
1.14M
    return;
490
1.14M
  }
491
19.8k
  if (strokeAdjust != splashStrokeAdjustOff && isSimple) {
492
16.7k
    splashStrokeAdjust(xMin, xMax, &xMinI, &xMaxI, strokeAdjust);
493
16.7k
    splashStrokeAdjust(yMin, yMax, &yMinI, &yMaxI, strokeAdjust);
494
16.7k
  } else {
495
3.14k
    xMinI = splashFloor(xMin);
496
3.14k
    yMinI = splashFloor(yMin);
497
3.14k
    xMaxI = splashCeil(xMax);
498
3.14k
    yMaxI = splashCeil(yMax);
499
3.14k
  }
500
19.8k
  if (xMinI < hardXMin) {
501
14
    xMinI = hardXMin;
502
14
  }
503
19.8k
  if (yMinI < hardYMin) {
504
11
    yMinI = hardYMin;
505
11
  }
506
19.8k
  if (xMaxI > hardXMax) {
507
7
    xMaxI = hardXMax;
508
7
  }
509
19.8k
  if (yMaxI > hardYMax) {
510
5
    yMaxI = hardYMax;
511
5
  }
512
  // the clipping code uses [xMinI, xMaxI] instead of [xMinI, xMaxI)
513
19.8k
  --xMaxI;
514
19.8k
  --yMaxI;
515
19.8k
  intBoundsValid = gTrue;
516
19.8k
  intBoundsStrokeAdjust = strokeAdjust;
517
19.8k
}