Coverage Report

Created: 2026-07-16 06:50

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.0k
static inline Guchar mul255(Guchar x, Guchar y) {
25
61.0k
  int z;
26
27
61.0k
  z = (int)x * (int)y;
28
61.0k
  return (Guchar)((z + (z >> 8) + 0x80) >> 8);
29
61.0k
}
30
31
//------------------------------------------------------------------------
32
// SplashClip
33
//------------------------------------------------------------------------
34
35
SplashClip::SplashClip(int hardXMinA, int hardYMinA,
36
276k
           int hardXMaxA, int hardYMaxA) {
37
276k
  int w;
38
39
276k
  hardXMin = hardXMinA;
40
276k
  hardYMin = hardYMinA;
41
276k
  hardXMax = hardXMaxA;
42
276k
  hardYMax = hardYMaxA;
43
276k
  xMin = hardXMin;
44
276k
  yMin = hardYMin;
45
276k
  xMax = hardXMax;
46
276k
  yMax = hardYMax;
47
276k
  intBoundsValid = gFalse;
48
276k
  paths = NULL;
49
276k
  eo = NULL;
50
276k
  scanners = NULL;
51
276k
  length = size = 0;
52
276k
  isSimple = gTrue;
53
276k
  prev = NULL;
54
276k
  if ((w = hardXMax + 1) <= 0) {
55
0
    w = 1;
56
0
  }
57
276k
  buf = (Guchar *)gmalloc(w);
58
276k
}
59
60
159k
SplashClip::SplashClip(SplashClip *clip) {
61
159k
  int w;
62
63
159k
  hardXMin = clip->hardXMin;
64
159k
  hardYMin = clip->hardYMin;
65
159k
  hardXMax = clip->hardXMax;
66
159k
  hardYMax = clip->hardYMax;
67
159k
  xMin = clip->xMin;
68
159k
  yMin = clip->yMin;
69
159k
  xMax = clip->xMax;
70
159k
  yMax = clip->yMax;
71
159k
  xMinI = clip->xMinI;
72
159k
  yMinI = clip->yMinI;
73
159k
  xMaxI = clip->xMaxI;
74
159k
  yMaxI = clip->yMaxI;
75
159k
  intBoundsValid = clip->intBoundsValid;
76
159k
  intBoundsStrokeAdjust = clip->intBoundsStrokeAdjust;
77
159k
  paths = NULL;
78
159k
  eo = NULL;
79
159k
  scanners = NULL;
80
159k
  length = size = 0;
81
159k
  isSimple = clip->isSimple;
82
159k
  prev = clip;
83
159k
  if ((w = splashCeil(xMax)) <= 0) {
84
30.7k
    w = 1;
85
30.7k
  }
86
159k
  buf = (Guchar *)gmalloc(w);
87
159k
}
88
89
435k
SplashClip::~SplashClip() {
90
435k
  int i;
91
92
523k
  for (i = 0; i < length; ++i) {
93
88.1k
    delete scanners[i];
94
88.1k
    delete paths[i];
95
88.1k
  }
96
435k
  gfree(paths);
97
435k
  gfree(eo);
98
435k
  gfree(scanners);
99
435k
  gfree(buf);
100
435k
}
101
102
88.1k
void SplashClip::grow(int nPaths) {
103
88.1k
  if (length + nPaths > size) {
104
79.7k
    if (size == 0) {
105
79.7k
      size = 32;
106
79.7k
    }
107
79.8k
    while (size < length + nPaths) {
108
29
      size *= 2;
109
29
    }
110
79.7k
    paths = (SplashXPath **)greallocn(paths, size, sizeof(SplashXPath *));
111
79.7k
    eo = (Guchar *)greallocn(eo, size, sizeof(Guchar));
112
79.7k
    scanners = (SplashXPathScanner **)
113
79.7k
                   greallocn(scanners, size, sizeof(SplashXPathScanner *));
114
79.7k
  }
115
88.1k
}
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
97.7k
           SplashCoord x1, SplashCoord y1) {
159
97.7k
  if (x0 < x1) {
160
41.8k
    if (x0 > xMin) {
161
766
      xMin = x0;
162
766
      intBoundsValid = gFalse;
163
766
    }
164
41.8k
    if (x1 < xMax) {
165
6.33k
      xMax = x1;
166
6.33k
      intBoundsValid = gFalse;
167
6.33k
    }
168
55.9k
  } else {
169
55.9k
    if (x1 > xMin) {
170
24.3k
      xMin = x1;
171
24.3k
      intBoundsValid = gFalse;
172
24.3k
    }
173
55.9k
    if (x0 < xMax) {
174
10.1k
      xMax = x0;
175
10.1k
      intBoundsValid = gFalse;
176
10.1k
    }
177
55.9k
  }
178
97.7k
  if (y0 < y1) {
179
65.9k
    if (y0 > yMin) {
180
146
      yMin = y0;
181
146
      intBoundsValid = gFalse;
182
146
    }
183
65.9k
    if (y1 < yMax) {
184
3.92k
      yMax = y1;
185
3.92k
      intBoundsValid = gFalse;
186
3.92k
    }
187
65.9k
  } else {
188
31.7k
    if (y1 > yMin) {
189
4.81k
      yMin = y1;
190
4.81k
      intBoundsValid = gFalse;
191
4.81k
    }
192
31.7k
    if (y0 < yMax) {
193
5.33k
      yMax = y0;
194
5.33k
      intBoundsValid = gFalse;
195
5.33k
    }
196
31.7k
  }
197
97.7k
  return splashOk;
198
97.7k
}
199
200
SplashError SplashClip::clipToPath(SplashPath *path, SplashCoord *matrix,
201
           SplashCoord flatness, GBool eoA,
202
           GBool enablePathSimplification,
203
198k
           SplashStrokeAdjustMode strokeAdjust) {
204
198k
  SplashXPath *xPath;
205
198k
  SplashCoord t;
206
207
198k
  xPath = new SplashXPath(path, matrix, flatness, gTrue,
208
198k
        enablePathSimplification,
209
198k
        strokeAdjust, NULL);
210
211
  // check for an empty path
212
198k
  if (xPath->length == 0) {
213
12.5k
    xMin = yMin = 1;
214
12.5k
    xMax = yMax = 0;
215
12.5k
    intBoundsValid = gFalse;
216
12.5k
    delete xPath;
217
12.5k
    return splashOk;
218
12.5k
  }
219
220
  // check for a rectangle
221
185k
  if (xPath->isRect) {
222
97.7k
    clipToRect(xPath->rectX0, xPath->rectY0, xPath->rectX1, xPath->rectY1);
223
97.7k
    delete xPath;
224
97.7k
    return splashOk;
225
97.7k
  }
226
227
88.1k
  grow(1);
228
88.1k
  paths[length] = xPath;
229
88.1k
  eo[length] = (Guchar)eoA;
230
88.1k
  if ((t = xPath->getXMin()) > xMin) {
231
2.51k
    xMin = t;
232
2.51k
  }
233
88.1k
  if ((t = xPath->getYMin()) > yMin) {
234
11.6k
    yMin = t;
235
11.6k
  }
236
88.1k
  if ((t = xPath->getXMax() + 1) < xMax) {
237
44.5k
    xMax = t;
238
44.5k
  }
239
88.1k
  if ((t = xPath->getYMax() + 1) < yMax) {
240
53.2k
    yMax = t;
241
53.2k
  }
242
88.1k
  intBoundsValid = gFalse;
243
88.1k
  scanners[length] = new SplashXPathScanner(xPath, eoA, splashFloor(yMin),
244
88.1k
              splashCeil(yMax) - 1);
245
88.1k
  ++length;
246
88.1k
  isSimple = gFalse;
247
248
88.1k
  return splashOk;
249
185k
}
250
251
SplashClipResult SplashClip::testRect(int rectXMin, int rectYMin,
252
              int rectXMax, int rectYMax,
253
3.70M
              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
3.70M
  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
1.12M
    updateIntBounds(strokeAdjust);
267
1.12M
    if (xMinI > xMaxI || yMinI > yMaxI) {
268
157k
      return splashClipAllOutside;
269
157k
    }
270
968k
    if (rectXMax + 1 <= xMinI ||
271
491k
  rectXMin >= xMaxI + 1 ||
272
247k
  rectYMax + 1 <= yMinI ||
273
937k
  rectYMin >= yMaxI + 1) {
274
937k
      return splashClipAllOutside;
275
937k
    }
276
31.5k
    if (rectXMin >= xMinI &&
277
20.0k
  rectXMax <= xMaxI &&
278
13.4k
  rectYMin >= yMinI &&
279
6.30k
  rectYMax <= yMaxI) {
280
5.43k
      return splashClipAllInside;
281
5.43k
    }
282
2.57M
  } else {
283
2.57M
    if (xMin >= xMax || yMin >= yMax) {
284
1.99M
      return splashClipAllOutside;
285
1.99M
    }
286
585k
    if ((SplashCoord)(rectXMax + 1) <= xMin ||
287
215k
  (SplashCoord)rectXMin >= xMax ||
288
56.0k
  (SplashCoord)(rectYMax + 1) <= yMin ||
289
568k
  (SplashCoord)rectYMin >= yMax) {
290
568k
      return splashClipAllOutside;
291
568k
    }
292
17.5k
    if (isSimple &&
293
9.60k
  (SplashCoord)rectXMin >= xMin &&
294
4.09k
  (SplashCoord)(rectXMax + 1) <= xMax &&
295
4.04k
  (SplashCoord)rectYMin >= yMin &&
296
399
  (SplashCoord)(rectYMax + 1) <= yMax) {
297
396
      return splashClipAllInside;
298
396
    }
299
17.5k
  }
300
43.3k
  return splashClipPartial;
301
3.70M
}
302
303
void SplashClip::clipSpan(Guchar *line, int y, int x0, int x1,
304
35.6k
        SplashStrokeAdjustMode strokeAdjust) {
305
35.6k
  SplashClip *clip;
306
35.6k
  SplashCoord d;
307
35.6k
  int x0a, x1a, x0b, x1b, x, i;
308
309
35.6k
  updateIntBounds(strokeAdjust);
310
311
  //--- clip to the integer rectangle
312
313
35.6k
  if (y < yMinI || y > yMaxI ||
314
35.6k
      x1 < xMinI || x0 > xMaxI) {
315
0
    memset(line + x0, 0, x1 - x0 + 1);
316
0
    return;
317
0
  }
318
319
35.6k
  if (x0 > xMinI) {
320
126
    x0a = x0;
321
35.5k
  } else {
322
35.5k
    x0a = xMinI;
323
35.5k
    memset(line + x0, 0, x0a - x0);
324
35.5k
  }
325
326
35.6k
  if (x1 < xMaxI) {
327
36
    x1a = x1;
328
35.6k
  } else {
329
35.6k
    x1a = xMaxI;
330
35.6k
    memset(line + x1a + 1, 0, x1 - x1a);
331
35.6k
  }
332
333
35.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
35.6k
  if (strokeAdjust == splashStrokeAdjustOff) {
341
342
    // clip left edge (xMin)
343
7.80k
    if (x0a == xMinI) {
344
7.67k
      d = (SplashCoord)(xMinI + 1) - xMin;
345
7.67k
      line[x0a] = (Guchar)(int)((SplashCoord)line[x0a] * d);
346
7.67k
    }
347
348
    // clip right edge (xMax)
349
7.80k
    if (x1a == xMaxI) {
350
7.76k
      d = xMax - (SplashCoord)xMaxI;
351
7.76k
      line[x1a] = (Guchar)(int)((SplashCoord)line[x1a] * d);
352
7.76k
    }
353
354
    // clip top edge (yMin)
355
7.80k
    if (y == yMinI) {
356
7.65k
      d = (SplashCoord)(yMinI + 1) - yMin;
357
15.4k
      for (x = x0a; x <= x1a; ++x) {
358
7.81k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
359
7.81k
      }
360
7.65k
    }
361
362
    // clip bottom edge (yMax)
363
7.80k
    if (y == yMaxI) {
364
7.64k
      d = yMax - (SplashCoord)yMaxI;
365
15.4k
      for (x = x0a; x <= x1a; ++x) {
366
7.78k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
367
7.78k
      }
368
7.64k
    }
369
7.80k
  }
370
371
35.6k
  if (isSimple) {
372
29.9k
    return;
373
29.9k
  }
374
375
  //--- clip to the paths
376
377
11.8k
  for (clip = this; clip; clip = clip->prev) {
378
68.5k
    for (i = 0; i < clip->length; ++i) {
379
62.4k
      clip->scanners[i]->getSpan(buf, y, x0a, x1a, &x0b, &x1b);
380
62.4k
      if (x0a < x0b) {
381
1.27k
  memset(line + x0a, 0, x0b - x0a);
382
1.27k
      }
383
123k
      for (x = x0b; x <= x1b; ++x) {
384
61.0k
  line[x] = mul255(line[x], buf[x]);
385
61.0k
      }
386
62.4k
      if (x1b < x1a) {
387
505
  memset(line + x1b + 1, 0, x1a - x1b);
388
505
      }
389
62.4k
    }
390
6.16k
  }
391
5.67k
}
392
393
GBool SplashClip::clipSpanBinary(Guchar *line, int y, int x0, int x1,
394
6.08k
         SplashStrokeAdjustMode strokeAdjust) {
395
6.08k
  SplashClip *clip;
396
6.08k
  int x0a, x1a, x0b, x1b, x, i;
397
6.08k
  Guchar any;
398
399
6.08k
  updateIntBounds(strokeAdjust);
400
401
6.08k
  if (y < yMinI || y > yMaxI ||
402
6.08k
      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.08k
  if (x0 > xMinI) {
410
0
    x0a = x0;
411
6.08k
  } else {
412
6.08k
    x0a = xMinI;
413
6.08k
    memset(line + x0, 0, x0a - x0);
414
6.08k
  }
415
416
6.08k
  if (x1 < xMaxI) {
417
0
    x1a = x1;
418
6.08k
  } else {
419
6.08k
    x1a = xMaxI;
420
6.08k
    memset(line + x1a + 1, 0, x1 - x1a);
421
6.08k
  }
422
423
6.08k
  if (x0a > x1a) {
424
0
    return gFalse;
425
0
  }
426
427
6.08k
  if (isSimple) {
428
4.71k
    for (x = x0a; x <= x1a; ++x) {
429
4.71k
      if (line[x]) {
430
4.71k
  return gTrue;
431
4.71k
      }
432
4.71k
    }
433
0
    return gFalse;
434
4.71k
  }
435
436
1.36k
  any = 0;
437
2.94k
  for (clip = this; clip; clip = clip->prev) {
438
5.89k
    for (i = 0; i < clip->length; ++i) {
439
4.31k
      clip->scanners[i]->getSpanBinary(buf, y, x0a, x1a, &x0b, &x1b);
440
4.31k
      if (x0a < x0b) {
441
90
  memset(line + x0a, 0, x0b - x0a);
442
90
      }
443
8.43k
      for (x = x0b; x <= x1b; ++x) {
444
4.11k
  line[x] &= buf[x];
445
4.11k
  any |= line[x];
446
4.11k
      }
447
4.31k
      if (x1b < x1a) {
448
196
  memset(line + x1b + 1, 0, x1a - x1b);
449
196
      }
450
4.31k
    }
451
1.58k
  }
452
453
1.36k
  return any != 0;
454
6.08k
}
455
456
85.4k
int SplashClip::getXMinI(SplashStrokeAdjustMode strokeAdjust) {
457
85.4k
  updateIntBounds(strokeAdjust);
458
85.4k
  return xMinI;
459
85.4k
}
460
461
85.4k
int SplashClip::getXMaxI(SplashStrokeAdjustMode strokeAdjust) {
462
85.4k
  updateIntBounds(strokeAdjust);
463
85.4k
  return xMaxI;
464
85.4k
}
465
466
83.5k
int SplashClip::getYMinI(SplashStrokeAdjustMode strokeAdjust) {
467
83.5k
  updateIntBounds(strokeAdjust);
468
83.5k
  return yMinI;
469
83.5k
}
470
471
83.5k
int SplashClip::getYMaxI(SplashStrokeAdjustMode strokeAdjust) {
472
83.5k
  updateIntBounds(strokeAdjust);
473
83.5k
  return yMaxI;
474
83.5k
}
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.50M
void SplashClip::updateIntBounds(SplashStrokeAdjustMode strokeAdjust) {
488
1.50M
  if (intBoundsValid && strokeAdjust == intBoundsStrokeAdjust) {
489
1.48M
    return;
490
1.48M
  }
491
23.2k
  if (strokeAdjust != splashStrokeAdjustOff && isSimple) {
492
18.1k
    splashStrokeAdjust(xMin, xMax, &xMinI, &xMaxI, strokeAdjust);
493
18.1k
    splashStrokeAdjust(yMin, yMax, &yMinI, &yMaxI, strokeAdjust);
494
18.1k
  } else {
495
5.12k
    xMinI = splashFloor(xMin);
496
5.12k
    yMinI = splashFloor(yMin);
497
5.12k
    xMaxI = splashCeil(xMax);
498
5.12k
    yMaxI = splashCeil(yMax);
499
5.12k
  }
500
23.2k
  if (xMinI < hardXMin) {
501
8
    xMinI = hardXMin;
502
8
  }
503
23.2k
  if (yMinI < hardYMin) {
504
7
    yMinI = hardYMin;
505
7
  }
506
23.2k
  if (xMaxI > hardXMax) {
507
3
    xMaxI = hardXMax;
508
3
  }
509
23.2k
  if (yMaxI > hardYMax) {
510
3
    yMaxI = hardYMax;
511
3
  }
512
  // the clipping code uses [xMinI, xMaxI] instead of [xMinI, xMaxI)
513
23.2k
  --xMaxI;
514
23.2k
  --yMaxI;
515
23.2k
  intBoundsValid = gTrue;
516
23.2k
  intBoundsStrokeAdjust = strokeAdjust;
517
23.2k
}