Coverage Report

Created: 2026-03-15 06:39

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