Coverage Report

Created: 2026-04-12 06:43

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.65k
static inline Guchar mul255(Guchar x, Guchar y) {
25
5.65k
  int z;
26
27
5.65k
  z = (int)x * (int)y;
28
5.65k
  return (Guchar)((z + (z >> 8) + 0x80) >> 8);
29
5.65k
}
30
31
//------------------------------------------------------------------------
32
// SplashClip
33
//------------------------------------------------------------------------
34
35
SplashClip::SplashClip(int hardXMinA, int hardYMinA,
36
427k
           int hardXMaxA, int hardYMaxA) {
37
427k
  int w;
38
39
427k
  hardXMin = hardXMinA;
40
427k
  hardYMin = hardYMinA;
41
427k
  hardXMax = hardXMaxA;
42
427k
  hardYMax = hardYMaxA;
43
427k
  xMin = hardXMin;
44
427k
  yMin = hardYMin;
45
427k
  xMax = hardXMax;
46
427k
  yMax = hardYMax;
47
427k
  intBoundsValid = gFalse;
48
427k
  paths = NULL;
49
427k
  eo = NULL;
50
427k
  scanners = NULL;
51
427k
  length = size = 0;
52
427k
  isSimple = gTrue;
53
427k
  prev = NULL;
54
427k
  if ((w = hardXMax + 1) <= 0) {
55
0
    w = 1;
56
0
  }
57
427k
  buf = (Guchar *)gmalloc(w);
58
427k
}
59
60
197k
SplashClip::SplashClip(SplashClip *clip) {
61
197k
  int w;
62
63
197k
  hardXMin = clip->hardXMin;
64
197k
  hardYMin = clip->hardYMin;
65
197k
  hardXMax = clip->hardXMax;
66
197k
  hardYMax = clip->hardYMax;
67
197k
  xMin = clip->xMin;
68
197k
  yMin = clip->yMin;
69
197k
  xMax = clip->xMax;
70
197k
  yMax = clip->yMax;
71
197k
  xMinI = clip->xMinI;
72
197k
  yMinI = clip->yMinI;
73
197k
  xMaxI = clip->xMaxI;
74
197k
  yMaxI = clip->yMaxI;
75
197k
  intBoundsValid = clip->intBoundsValid;
76
197k
  intBoundsStrokeAdjust = clip->intBoundsStrokeAdjust;
77
197k
  paths = NULL;
78
197k
  eo = NULL;
79
197k
  scanners = NULL;
80
197k
  length = size = 0;
81
197k
  isSimple = clip->isSimple;
82
197k
  prev = clip;
83
197k
  if ((w = splashCeil(xMax)) <= 0) {
84
50.8k
    w = 1;
85
50.8k
  }
86
197k
  buf = (Guchar *)gmalloc(w);
87
197k
}
88
89
624k
SplashClip::~SplashClip() {
90
624k
  int i;
91
92
719k
  for (i = 0; i < length; ++i) {
93
94.8k
    delete scanners[i];
94
94.8k
    delete paths[i];
95
94.8k
  }
96
624k
  gfree(paths);
97
624k
  gfree(eo);
98
624k
  gfree(scanners);
99
624k
  gfree(buf);
100
624k
}
101
102
94.8k
void SplashClip::grow(int nPaths) {
103
94.8k
  if (length + nPaths > size) {
104
86.2k
    if (size == 0) {
105
86.2k
      size = 32;
106
86.2k
    }
107
86.2k
    while (size < length + nPaths) {
108
34
      size *= 2;
109
34
    }
110
86.2k
    paths = (SplashXPath **)greallocn(paths, size, sizeof(SplashXPath *));
111
86.2k
    eo = (Guchar *)greallocn(eo, size, sizeof(Guchar));
112
86.2k
    scanners = (SplashXPathScanner **)
113
86.2k
                   greallocn(scanners, size, sizeof(SplashXPathScanner *));
114
86.2k
  }
115
94.8k
}
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
125k
           SplashCoord x1, SplashCoord y1) {
159
125k
  if (x0 < x1) {
160
41.8k
    if (x0 > xMin) {
161
51
      xMin = x0;
162
51
      intBoundsValid = gFalse;
163
51
    }
164
41.8k
    if (x1 < xMax) {
165
5.89k
      xMax = x1;
166
5.89k
      intBoundsValid = gFalse;
167
5.89k
    }
168
83.8k
  } else {
169
83.8k
    if (x1 > xMin) {
170
25.6k
      xMin = x1;
171
25.6k
      intBoundsValid = gFalse;
172
25.6k
    }
173
83.8k
    if (x0 < xMax) {
174
12.6k
      xMax = x0;
175
12.6k
      intBoundsValid = gFalse;
176
12.6k
    }
177
83.8k
  }
178
125k
  if (y0 < y1) {
179
65.4k
    if (y0 > yMin) {
180
17
      yMin = y0;
181
17
      intBoundsValid = gFalse;
182
17
    }
183
65.4k
    if (y1 < yMax) {
184
2.85k
      yMax = y1;
185
2.85k
      intBoundsValid = gFalse;
186
2.85k
    }
187
65.4k
  } else {
188
60.2k
    if (y1 > yMin) {
189
7.13k
      yMin = y1;
190
7.13k
      intBoundsValid = gFalse;
191
7.13k
    }
192
60.2k
    if (y0 < yMax) {
193
6.29k
      yMax = y0;
194
6.29k
      intBoundsValid = gFalse;
195
6.29k
    }
196
60.2k
  }
197
125k
  return splashOk;
198
125k
}
199
200
SplashError SplashClip::clipToPath(SplashPath *path, SplashCoord *matrix,
201
           SplashCoord flatness, GBool eoA,
202
           GBool enablePathSimplification,
203
233k
           SplashStrokeAdjustMode strokeAdjust) {
204
233k
  SplashXPath *xPath;
205
233k
  SplashCoord t;
206
207
233k
  xPath = new SplashXPath(path, matrix, flatness, gTrue,
208
233k
        enablePathSimplification,
209
233k
        strokeAdjust, NULL);
210
211
  // check for an empty path
212
233k
  if (xPath->length == 0) {
213
13.1k
    xMin = yMin = 1;
214
13.1k
    xMax = yMax = 0;
215
13.1k
    intBoundsValid = gFalse;
216
13.1k
    delete xPath;
217
13.1k
    return splashOk;
218
13.1k
  }
219
220
  // check for a rectangle
221
220k
  if (xPath->isRect) {
222
125k
    clipToRect(xPath->rectX0, xPath->rectY0, xPath->rectX1, xPath->rectY1);
223
125k
    delete xPath;
224
125k
    return splashOk;
225
125k
  }
226
227
94.8k
  grow(1);
228
94.8k
  paths[length] = xPath;
229
94.8k
  eo[length] = (Guchar)eoA;
230
94.8k
  if ((t = xPath->getXMin()) > xMin) {
231
3.39k
    xMin = t;
232
3.39k
  }
233
94.8k
  if ((t = xPath->getYMin()) > yMin) {
234
11.6k
    yMin = t;
235
11.6k
  }
236
94.8k
  if ((t = xPath->getXMax() + 1) < xMax) {
237
50.5k
    xMax = t;
238
50.5k
  }
239
94.8k
  if ((t = xPath->getYMax() + 1) < yMax) {
240
57.2k
    yMax = t;
241
57.2k
  }
242
94.8k
  intBoundsValid = gFalse;
243
94.8k
  scanners[length] = new SplashXPathScanner(xPath, eoA, splashFloor(yMin),
244
94.8k
              splashCeil(yMax) - 1);
245
94.8k
  ++length;
246
94.8k
  isSimple = gFalse;
247
248
94.8k
  return splashOk;
249
220k
}
250
251
SplashClipResult SplashClip::testRect(int rectXMin, int rectYMin,
252
              int rectXMax, int rectYMax,
253
1.94M
              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.94M
  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
954k
    updateIntBounds(strokeAdjust);
267
954k
    if (xMinI > xMaxI || yMinI > yMaxI) {
268
19.3k
      return splashClipAllOutside;
269
19.3k
    }
270
934k
    if (rectXMax + 1 <= xMinI ||
271
538k
  rectXMin >= xMaxI + 1 ||
272
302k
  rectYMax + 1 <= yMinI ||
273
901k
  rectYMin >= yMaxI + 1) {
274
901k
      return splashClipAllOutside;
275
901k
    }
276
33.6k
    if (rectXMin >= xMinI &&
277
20.9k
  rectXMax <= xMaxI &&
278
13.9k
  rectYMin >= yMinI &&
279
8.96k
  rectYMax <= yMaxI) {
280
8.36k
      return splashClipAllInside;
281
8.36k
    }
282
992k
  } else {
283
992k
    if (xMin >= xMax || yMin >= yMax) {
284
521k
      return splashClipAllOutside;
285
521k
    }
286
470k
    if ((SplashCoord)(rectXMax + 1) <= xMin ||
287
252k
  (SplashCoord)rectXMin >= xMax ||
288
23.8k
  (SplashCoord)(rectYMax + 1) <= yMin ||
289
459k
  (SplashCoord)rectYMin >= yMax) {
290
459k
      return splashClipAllOutside;
291
459k
    }
292
10.9k
    if (isSimple &&
293
4.57k
  (SplashCoord)rectXMin >= xMin &&
294
3.47k
  (SplashCoord)(rectXMax + 1) <= xMax &&
295
3.41k
  (SplashCoord)rectYMin >= yMin &&
296
577
  (SplashCoord)(rectYMax + 1) <= yMax) {
297
577
      return splashClipAllInside;
298
577
    }
299
10.9k
  }
300
35.6k
  return splashClipPartial;
301
1.94M
}
302
303
void SplashClip::clipSpan(Guchar *line, int y, int x0, int x1,
304
27.0k
        SplashStrokeAdjustMode strokeAdjust) {
305
27.0k
  SplashClip *clip;
306
27.0k
  SplashCoord d;
307
27.0k
  int x0a, x1a, x0b, x1b, x, i;
308
309
27.0k
  updateIntBounds(strokeAdjust);
310
311
  //--- clip to the integer rectangle
312
313
27.0k
  if (y < yMinI || y > yMaxI ||
314
27.0k
      x1 < xMinI || x0 > xMaxI) {
315
0
    memset(line + x0, 0, x1 - x0 + 1);
316
0
    return;
317
0
  }
318
319
27.0k
  if (x0 > xMinI) {
320
313
    x0a = x0;
321
26.7k
  } else {
322
26.7k
    x0a = xMinI;
323
26.7k
    memset(line + x0, 0, x0a - x0);
324
26.7k
  }
325
326
27.0k
  if (x1 < xMaxI) {
327
34
    x1a = x1;
328
26.9k
  } else {
329
26.9k
    x1a = xMaxI;
330
26.9k
    memset(line + x1a + 1, 0, x1 - x1a);
331
26.9k
  }
332
333
27.0k
  if (x0a > x1a) {
334
0
    return;
335
0
  }
336
337
  //--- clip to the floating point rectangle
338
  //    (if stroke adjustment is disabled)
339
340
27.0k
  if (strokeAdjust == splashStrokeAdjustOff) {
341
342
    // clip left edge (xMin)
343
2.46k
    if (x0a == xMinI) {
344
2.15k
      d = (SplashCoord)(xMinI + 1) - xMin;
345
2.15k
      line[x0a] = (Guchar)(int)((SplashCoord)line[x0a] * d);
346
2.15k
    }
347
348
    // clip right edge (xMax)
349
2.46k
    if (x1a == xMaxI) {
350
2.43k
      d = xMax - (SplashCoord)xMaxI;
351
2.43k
      line[x1a] = (Guchar)(int)((SplashCoord)line[x1a] * d);
352
2.43k
    }
353
354
    // clip top edge (yMin)
355
2.46k
    if (y == yMinI) {
356
1.93k
      d = (SplashCoord)(yMinI + 1) - yMin;
357
4.37k
      for (x = x0a; x <= x1a; ++x) {
358
2.44k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
359
2.44k
      }
360
1.93k
    }
361
362
    // clip bottom edge (yMax)
363
2.46k
    if (y == yMaxI) {
364
1.92k
      d = yMax - (SplashCoord)yMaxI;
365
4.36k
      for (x = x0a; x <= x1a; ++x) {
366
2.43k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
367
2.43k
      }
368
1.92k
    }
369
2.46k
  }
370
371
27.0k
  if (isSimple) {
372
23.5k
    return;
373
23.5k
  }
374
375
  //--- clip to the paths
376
377
7.64k
  for (clip = this; clip; clip = clip->prev) {
378
49.2k
    for (i = 0; i < clip->length; ++i) {
379
45.1k
      clip->scanners[i]->getSpan(buf, y, x0a, x1a, &x0b, &x1b);
380
45.1k
      if (x0a < x0b) {
381
39.3k
  memset(line + x0a, 0, x0b - x0a);
382
39.3k
      }
383
50.7k
      for (x = x0b; x <= x1b; ++x) {
384
5.65k
  line[x] = mul255(line[x], buf[x]);
385
5.65k
      }
386
45.1k
      if (x1b < x1a) {
387
265
  memset(line + x1b + 1, 0, x1a - x1b);
388
265
      }
389
45.1k
    }
390
4.12k
  }
391
3.52k
}
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
4.60k
    for (x = x0a; x <= x1a; ++x) {
429
4.60k
      if (line[x]) {
430
4.60k
  return gTrue;
431
4.60k
      }
432
4.60k
    }
433
0
    return gFalse;
434
4.60k
  }
435
436
1.59k
  any = 0;
437
3.44k
  for (clip = this; clip; clip = clip->prev) {
438
4.70k
    for (i = 0; i < clip->length; ++i) {
439
2.85k
      clip->scanners[i]->getSpanBinary(buf, y, x0a, x1a, &x0b, &x1b);
440
2.85k
      if (x0a < x0b) {
441
61
  memset(line + x0a, 0, x0b - x0a);
442
61
      }
443
5.57k
      for (x = x0b; x <= x1b; ++x) {
444
2.72k
  line[x] &= buf[x];
445
2.72k
  any |= line[x];
446
2.72k
      }
447
2.85k
      if (x1b < x1a) {
448
106
  memset(line + x1b + 1, 0, x1a - x1b);
449
106
      }
450
2.85k
    }
451
1.84k
  }
452
453
1.59k
  return any != 0;
454
6.19k
}
455
456
81.6k
int SplashClip::getXMinI(SplashStrokeAdjustMode strokeAdjust) {
457
81.6k
  updateIntBounds(strokeAdjust);
458
81.6k
  return xMinI;
459
81.6k
}
460
461
81.6k
int SplashClip::getXMaxI(SplashStrokeAdjustMode strokeAdjust) {
462
81.6k
  updateIntBounds(strokeAdjust);
463
81.6k
  return xMaxI;
464
81.6k
}
465
466
79.8k
int SplashClip::getYMinI(SplashStrokeAdjustMode strokeAdjust) {
467
79.8k
  updateIntBounds(strokeAdjust);
468
79.8k
  return yMinI;
469
79.8k
}
470
471
79.8k
int SplashClip::getYMaxI(SplashStrokeAdjustMode strokeAdjust) {
472
79.8k
  updateIntBounds(strokeAdjust);
473
79.8k
  return yMaxI;
474
79.8k
}
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.31M
void SplashClip::updateIntBounds(SplashStrokeAdjustMode strokeAdjust) {
488
1.31M
  if (intBoundsValid && strokeAdjust == intBoundsStrokeAdjust) {
489
1.28M
    return;
490
1.28M
  }
491
24.2k
  if (strokeAdjust != splashStrokeAdjustOff && isSimple) {
492
21.3k
    splashStrokeAdjust(xMin, xMax, &xMinI, &xMaxI, strokeAdjust);
493
21.3k
    splashStrokeAdjust(yMin, yMax, &yMinI, &yMaxI, strokeAdjust);
494
21.3k
  } else {
495
2.86k
    xMinI = splashFloor(xMin);
496
2.86k
    yMinI = splashFloor(yMin);
497
2.86k
    xMaxI = splashCeil(xMax);
498
2.86k
    yMaxI = splashCeil(yMax);
499
2.86k
  }
500
24.2k
  if (xMinI < hardXMin) {
501
0
    xMinI = hardXMin;
502
0
  }
503
24.2k
  if (yMinI < hardYMin) {
504
0
    yMinI = hardYMin;
505
0
  }
506
24.2k
  if (xMaxI > hardXMax) {
507
0
    xMaxI = hardXMax;
508
0
  }
509
24.2k
  if (yMaxI > hardYMax) {
510
0
    yMaxI = hardYMax;
511
0
  }
512
  // the clipping code uses [xMinI, xMaxI] instead of [xMinI, xMaxI)
513
24.2k
  --xMaxI;
514
24.2k
  --yMaxI;
515
24.2k
  intBoundsValid = gTrue;
516
24.2k
  intBoundsStrokeAdjust = strokeAdjust;
517
24.2k
}