Coverage Report

Created: 2025-07-11 07:47

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