Coverage Report

Created: 2026-04-04 06:06

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.39k
static inline Guchar mul255(Guchar x, Guchar y) {
25
5.39k
  int z;
26
27
5.39k
  z = (int)x * (int)y;
28
5.39k
  return (Guchar)((z + (z >> 8) + 0x80) >> 8);
29
5.39k
}
30
31
//------------------------------------------------------------------------
32
// SplashClip
33
//------------------------------------------------------------------------
34
35
SplashClip::SplashClip(int hardXMinA, int hardYMinA,
36
383k
           int hardXMaxA, int hardYMaxA) {
37
383k
  int w;
38
39
383k
  hardXMin = hardXMinA;
40
383k
  hardYMin = hardYMinA;
41
383k
  hardXMax = hardXMaxA;
42
383k
  hardYMax = hardYMaxA;
43
383k
  xMin = hardXMin;
44
383k
  yMin = hardYMin;
45
383k
  xMax = hardXMax;
46
383k
  yMax = hardYMax;
47
383k
  intBoundsValid = gFalse;
48
383k
  paths = NULL;
49
383k
  eo = NULL;
50
383k
  scanners = NULL;
51
383k
  length = size = 0;
52
383k
  isSimple = gTrue;
53
383k
  prev = NULL;
54
383k
  if ((w = hardXMax + 1) <= 0) {
55
0
    w = 1;
56
0
  }
57
383k
  buf = (Guchar *)gmalloc(w);
58
383k
}
59
60
190k
SplashClip::SplashClip(SplashClip *clip) {
61
190k
  int w;
62
63
190k
  hardXMin = clip->hardXMin;
64
190k
  hardYMin = clip->hardYMin;
65
190k
  hardXMax = clip->hardXMax;
66
190k
  hardYMax = clip->hardYMax;
67
190k
  xMin = clip->xMin;
68
190k
  yMin = clip->yMin;
69
190k
  xMax = clip->xMax;
70
190k
  yMax = clip->yMax;
71
190k
  xMinI = clip->xMinI;
72
190k
  yMinI = clip->yMinI;
73
190k
  xMaxI = clip->xMaxI;
74
190k
  yMaxI = clip->yMaxI;
75
190k
  intBoundsValid = clip->intBoundsValid;
76
190k
  intBoundsStrokeAdjust = clip->intBoundsStrokeAdjust;
77
190k
  paths = NULL;
78
190k
  eo = NULL;
79
190k
  scanners = NULL;
80
190k
  length = size = 0;
81
190k
  isSimple = clip->isSimple;
82
190k
  prev = clip;
83
190k
  if ((w = splashCeil(xMax)) <= 0) {
84
47.9k
    w = 1;
85
47.9k
  }
86
190k
  buf = (Guchar *)gmalloc(w);
87
190k
}
88
89
573k
SplashClip::~SplashClip() {
90
573k
  int i;
91
92
664k
  for (i = 0; i < length; ++i) {
93
90.8k
    delete scanners[i];
94
90.8k
    delete paths[i];
95
90.8k
  }
96
573k
  gfree(paths);
97
573k
  gfree(eo);
98
573k
  gfree(scanners);
99
573k
  gfree(buf);
100
573k
}
101
102
90.8k
void SplashClip::grow(int nPaths) {
103
90.8k
  if (length + nPaths > size) {
104
83.0k
    if (size == 0) {
105
83.0k
      size = 32;
106
83.0k
    }
107
83.1k
    while (size < length + nPaths) {
108
25
      size *= 2;
109
25
    }
110
83.0k
    paths = (SplashXPath **)greallocn(paths, size, sizeof(SplashXPath *));
111
83.0k
    eo = (Guchar *)greallocn(eo, size, sizeof(Guchar));
112
83.0k
    scanners = (SplashXPathScanner **)
113
83.0k
                   greallocn(scanners, size, sizeof(SplashXPathScanner *));
114
83.0k
  }
115
90.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
121k
           SplashCoord x1, SplashCoord y1) {
159
121k
  if (x0 < x1) {
160
40.2k
    if (x0 > xMin) {
161
48
      xMin = x0;
162
48
      intBoundsValid = gFalse;
163
48
    }
164
40.2k
    if (x1 < xMax) {
165
4.85k
      xMax = x1;
166
4.85k
      intBoundsValid = gFalse;
167
4.85k
    }
168
81.2k
  } else {
169
81.2k
    if (x1 > xMin) {
170
25.3k
      xMin = x1;
171
25.3k
      intBoundsValid = gFalse;
172
25.3k
    }
173
81.2k
    if (x0 < xMax) {
174
11.6k
      xMax = x0;
175
11.6k
      intBoundsValid = gFalse;
176
11.6k
    }
177
81.2k
  }
178
121k
  if (y0 < y1) {
179
63.7k
    if (y0 > yMin) {
180
13
      yMin = y0;
181
13
      intBoundsValid = gFalse;
182
13
    }
183
63.7k
    if (y1 < yMax) {
184
2.58k
      yMax = y1;
185
2.58k
      intBoundsValid = gFalse;
186
2.58k
    }
187
63.7k
  } else {
188
57.7k
    if (y1 > yMin) {
189
6.89k
      yMin = y1;
190
6.89k
      intBoundsValid = gFalse;
191
6.89k
    }
192
57.7k
    if (y0 < yMax) {
193
5.48k
      yMax = y0;
194
5.48k
      intBoundsValid = gFalse;
195
5.48k
    }
196
57.7k
  }
197
121k
  return splashOk;
198
121k
}
199
200
SplashError SplashClip::clipToPath(SplashPath *path, SplashCoord *matrix,
201
           SplashCoord flatness, GBool eoA,
202
           GBool enablePathSimplification,
203
224k
           SplashStrokeAdjustMode strokeAdjust) {
204
224k
  SplashXPath *xPath;
205
224k
  SplashCoord t;
206
207
224k
  xPath = new SplashXPath(path, matrix, flatness, gTrue,
208
224k
        enablePathSimplification,
209
224k
        strokeAdjust, NULL);
210
211
  // check for an empty path
212
224k
  if (xPath->length == 0) {
213
11.8k
    xMin = yMin = 1;
214
11.8k
    xMax = yMax = 0;
215
11.8k
    intBoundsValid = gFalse;
216
11.8k
    delete xPath;
217
11.8k
    return splashOk;
218
11.8k
  }
219
220
  // check for a rectangle
221
212k
  if (xPath->isRect) {
222
121k
    clipToRect(xPath->rectX0, xPath->rectY0, xPath->rectX1, xPath->rectY1);
223
121k
    delete xPath;
224
121k
    return splashOk;
225
121k
  }
226
227
90.8k
  grow(1);
228
90.8k
  paths[length] = xPath;
229
90.8k
  eo[length] = (Guchar)eoA;
230
90.8k
  if ((t = xPath->getXMin()) > xMin) {
231
2.91k
    xMin = t;
232
2.91k
  }
233
90.8k
  if ((t = xPath->getYMin()) > yMin) {
234
11.2k
    yMin = t;
235
11.2k
  }
236
90.8k
  if ((t = xPath->getXMax() + 1) < xMax) {
237
50.1k
    xMax = t;
238
50.1k
  }
239
90.8k
  if ((t = xPath->getYMax() + 1) < yMax) {
240
56.2k
    yMax = t;
241
56.2k
  }
242
90.8k
  intBoundsValid = gFalse;
243
90.8k
  scanners[length] = new SplashXPathScanner(xPath, eoA, splashFloor(yMin),
244
90.8k
              splashCeil(yMax) - 1);
245
90.8k
  ++length;
246
90.8k
  isSimple = gFalse;
247
248
90.8k
  return splashOk;
249
212k
}
250
251
SplashClipResult SplashClip::testRect(int rectXMin, int rectYMin,
252
              int rectXMax, int rectYMax,
253
1.53M
              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.53M
  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
938k
    updateIntBounds(strokeAdjust);
267
938k
    if (xMinI > xMaxI || yMinI > yMaxI) {
268
18.7k
      return splashClipAllOutside;
269
18.7k
    }
270
919k
    if (rectXMax + 1 <= xMinI ||
271
528k
  rectXMin >= xMaxI + 1 ||
272
293k
  rectYMax + 1 <= yMinI ||
273
893k
  rectYMin >= yMaxI + 1) {
274
893k
      return splashClipAllOutside;
275
893k
    }
276
25.7k
    if (rectXMin >= xMinI &&
277
19.3k
  rectXMax <= xMaxI &&
278
12.8k
  rectYMin >= yMinI &&
279
8.41k
  rectYMax <= yMaxI) {
280
7.80k
      return splashClipAllInside;
281
7.80k
    }
282
592k
  } else {
283
592k
    if (xMin >= xMax || yMin >= yMax) {
284
113k
      return splashClipAllOutside;
285
113k
    }
286
479k
    if ((SplashCoord)(rectXMax + 1) <= xMin ||
287
260k
  (SplashCoord)rectXMin >= xMax ||
288
26.0k
  (SplashCoord)(rectYMax + 1) <= yMin ||
289
469k
  (SplashCoord)rectYMin >= yMax) {
290
469k
      return splashClipAllOutside;
291
469k
    }
292
10.1k
    if (isSimple &&
293
5.04k
  (SplashCoord)rectXMin >= xMin &&
294
4.03k
  (SplashCoord)(rectXMax + 1) <= xMax &&
295
3.98k
  (SplashCoord)rectYMin >= yMin &&
296
546
  (SplashCoord)(rectYMax + 1) <= yMax) {
297
546
      return splashClipAllInside;
298
546
    }
299
10.1k
  }
300
27.5k
  return splashClipPartial;
301
1.53M
}
302
303
void SplashClip::clipSpan(Guchar *line, int y, int x0, int x1,
304
19.6k
        SplashStrokeAdjustMode strokeAdjust) {
305
19.6k
  SplashClip *clip;
306
19.6k
  SplashCoord d;
307
19.6k
  int x0a, x1a, x0b, x1b, x, i;
308
309
19.6k
  updateIntBounds(strokeAdjust);
310
311
  //--- clip to the integer rectangle
312
313
19.6k
  if (y < yMinI || y > yMaxI ||
314
19.6k
      x1 < xMinI || x0 > xMaxI) {
315
0
    memset(line + x0, 0, x1 - x0 + 1);
316
0
    return;
317
0
  }
318
319
19.6k
  if (x0 > xMinI) {
320
313
    x0a = x0;
321
19.2k
  } else {
322
19.2k
    x0a = xMinI;
323
19.2k
    memset(line + x0, 0, x0a - x0);
324
19.2k
  }
325
326
19.6k
  if (x1 < xMaxI) {
327
38
    x1a = x1;
328
19.5k
  } else {
329
19.5k
    x1a = xMaxI;
330
19.5k
    memset(line + x1a + 1, 0, x1 - x1a);
331
19.5k
  }
332
333
19.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
19.6k
  if (strokeAdjust == splashStrokeAdjustOff) {
341
342
    // clip left edge (xMin)
343
2.00k
    if (x0a == xMinI) {
344
1.68k
      d = (SplashCoord)(xMinI + 1) - xMin;
345
1.68k
      line[x0a] = (Guchar)(int)((SplashCoord)line[x0a] * d);
346
1.68k
    }
347
348
    // clip right edge (xMax)
349
2.00k
    if (x1a == xMaxI) {
350
1.96k
      d = xMax - (SplashCoord)xMaxI;
351
1.96k
      line[x1a] = (Guchar)(int)((SplashCoord)line[x1a] * d);
352
1.96k
    }
353
354
    // clip top edge (yMin)
355
2.00k
    if (y == yMinI) {
356
1.46k
      d = (SplashCoord)(yMinI + 1) - yMin;
357
3.44k
      for (x = x0a; x <= x1a; ++x) {
358
1.97k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
359
1.97k
      }
360
1.46k
    }
361
362
    // clip bottom edge (yMax)
363
2.00k
    if (y == yMaxI) {
364
1.46k
      d = yMax - (SplashCoord)yMaxI;
365
3.43k
      for (x = x0a; x <= x1a; ++x) {
366
1.97k
  line[x] = (Guchar)(int)((SplashCoord)line[x] * d);
367
1.97k
      }
368
1.46k
    }
369
2.00k
  }
370
371
19.6k
  if (isSimple) {
372
16.7k
    return;
373
16.7k
  }
374
375
  //--- clip to the paths
376
377
6.27k
  for (clip = this; clip; clip = clip->prev) {
378
24.7k
    for (i = 0; i < clip->length; ++i) {
379
21.3k
      clip->scanners[i]->getSpan(buf, y, x0a, x1a, &x0b, &x1b);
380
21.3k
      if (x0a < x0b) {
381
15.8k
  memset(line + x0a, 0, x0b - x0a);
382
15.8k
      }
383
26.7k
      for (x = x0b; x <= x1b; ++x) {
384
5.39k
  line[x] = mul255(line[x], buf[x]);
385
5.39k
      }
386
21.3k
      if (x1b < x1a) {
387
288
  memset(line + x1b + 1, 0, x1a - x1b);
388
288
      }
389
21.3k
    }
390
3.42k
  }
391
2.85k
}
392
393
GBool SplashClip::clipSpanBinary(Guchar *line, int y, int x0, int x1,
394
6.27k
         SplashStrokeAdjustMode strokeAdjust) {
395
6.27k
  SplashClip *clip;
396
6.27k
  int x0a, x1a, x0b, x1b, x, i;
397
6.27k
  Guchar any;
398
399
6.27k
  updateIntBounds(strokeAdjust);
400
401
6.27k
  if (y < yMinI || y > yMaxI ||
402
6.27k
      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.27k
  if (x0 > xMinI) {
410
0
    x0a = x0;
411
6.27k
  } else {
412
6.27k
    x0a = xMinI;
413
6.27k
    memset(line + x0, 0, x0a - x0);
414
6.27k
  }
415
416
6.27k
  if (x1 < xMaxI) {
417
0
    x1a = x1;
418
6.27k
  } else {
419
6.27k
    x1a = xMaxI;
420
6.27k
    memset(line + x1a + 1, 0, x1 - x1a);
421
6.27k
  }
422
423
6.27k
  if (x0a > x1a) {
424
0
    return gFalse;
425
0
  }
426
427
6.27k
  if (isSimple) {
428
4.69k
    for (x = x0a; x <= x1a; ++x) {
429
4.69k
      if (line[x]) {
430
4.69k
  return gTrue;
431
4.69k
      }
432
4.69k
    }
433
0
    return gFalse;
434
4.69k
  }
435
436
1.57k
  any = 0;
437
3.37k
  for (clip = this; clip; clip = clip->prev) {
438
4.61k
    for (i = 0; i < clip->length; ++i) {
439
2.81k
      clip->scanners[i]->getSpanBinary(buf, y, x0a, x1a, &x0b, &x1b);
440
2.81k
      if (x0a < x0b) {
441
67
  memset(line + x0a, 0, x0b - x0a);
442
67
      }
443
5.48k
      for (x = x0b; x <= x1b; ++x) {
444
2.67k
  line[x] &= buf[x];
445
2.67k
  any |= line[x];
446
2.67k
      }
447
2.81k
      if (x1b < x1a) {
448
120
  memset(line + x1b + 1, 0, x1a - x1b);
449
120
      }
450
2.81k
    }
451
1.79k
  }
452
453
1.57k
  return any != 0;
454
6.27k
}
455
456
65.0k
int SplashClip::getXMinI(SplashStrokeAdjustMode strokeAdjust) {
457
65.0k
  updateIntBounds(strokeAdjust);
458
65.0k
  return xMinI;
459
65.0k
}
460
461
65.0k
int SplashClip::getXMaxI(SplashStrokeAdjustMode strokeAdjust) {
462
65.0k
  updateIntBounds(strokeAdjust);
463
65.0k
  return xMaxI;
464
65.0k
}
465
466
63.2k
int SplashClip::getYMinI(SplashStrokeAdjustMode strokeAdjust) {
467
63.2k
  updateIntBounds(strokeAdjust);
468
63.2k
  return yMinI;
469
63.2k
}
470
471
63.2k
int SplashClip::getYMaxI(SplashStrokeAdjustMode strokeAdjust) {
472
63.2k
  updateIntBounds(strokeAdjust);
473
63.2k
  return yMaxI;
474
63.2k
}
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.22M
void SplashClip::updateIntBounds(SplashStrokeAdjustMode strokeAdjust) {
488
1.22M
  if (intBoundsValid && strokeAdjust == intBoundsStrokeAdjust) {
489
1.20M
    return;
490
1.20M
  }
491
16.5k
  if (strokeAdjust != splashStrokeAdjustOff && isSimple) {
492
14.2k
    splashStrokeAdjust(xMin, xMax, &xMinI, &xMaxI, strokeAdjust);
493
14.2k
    splashStrokeAdjust(yMin, yMax, &yMinI, &yMaxI, strokeAdjust);
494
14.2k
  } else {
495
2.31k
    xMinI = splashFloor(xMin);
496
2.31k
    yMinI = splashFloor(yMin);
497
2.31k
    xMaxI = splashCeil(xMax);
498
2.31k
    yMaxI = splashCeil(yMax);
499
2.31k
  }
500
16.5k
  if (xMinI < hardXMin) {
501
0
    xMinI = hardXMin;
502
0
  }
503
16.5k
  if (yMinI < hardYMin) {
504
0
    yMinI = hardYMin;
505
0
  }
506
16.5k
  if (xMaxI > hardXMax) {
507
0
    xMaxI = hardXMax;
508
0
  }
509
16.5k
  if (yMaxI > hardYMax) {
510
0
    yMaxI = hardYMax;
511
0
  }
512
  // the clipping code uses [xMinI, xMaxI] instead of [xMinI, xMaxI)
513
16.5k
  --xMaxI;
514
16.5k
  --yMaxI;
515
16.5k
  intBoundsValid = gTrue;
516
16.5k
  intBoundsStrokeAdjust = strokeAdjust;
517
16.5k
}