Coverage Report

Created: 2026-09-01 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/xpdf/ShadingImage.cc
Line
Count
Source
1
//========================================================================
2
//
3
// ShadingImage.cc
4
//
5
// Copyright 2020 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <math.h>
12
#include "Trace.h"
13
#include "GfxState.h"
14
#include "SplashBitmap.h"
15
#include "SplashPattern.h"
16
#include "SplashPath.h"
17
#include "Splash.h"
18
#include "ShadingImage.h"
19
20
// Max recursive depth for a patch mesh shading fill.
21
3.37M
#define patchMaxDepth 10
22
23
// Max delta allowed in any color component for a patch mesh shading
24
// fill.
25
83.2M
#define patchColorDelta (dblToCol(1 / 256.0))
26
27
SplashBitmap *ShadingImage::generateBitmap(
28
        GfxState *state,
29
        GfxShading *shading,
30
        SplashColorMode mode,
31
        GBool reverseVideo,
32
        Splash *parentSplash,
33
        SplashBitmapMemCache *bitmapMemCache,
34
3.28k
        int *xOut, int *yOut) {
35
3.28k
  switch (shading->getType()) {
36
0
  case 1:
37
0
    return generateFunctionBitmap(state, (GfxFunctionShading *)shading,
38
0
          mode, reverseVideo,
39
0
          parentSplash, bitmapMemCache, xOut, yOut);
40
0
    break;
41
1.56k
  case 2:
42
1.56k
    return generateAxialBitmap(state, (GfxAxialShading *)shading,
43
1.56k
             mode, reverseVideo,
44
1.56k
             parentSplash, bitmapMemCache, xOut, yOut);
45
0
    break;
46
16
  case 3:
47
16
    return generateRadialBitmap(state, (GfxRadialShading *)shading,
48
16
        mode, reverseVideo,
49
16
        parentSplash, bitmapMemCache, xOut, yOut);
50
0
    break;
51
480
  case 4:
52
716
  case 5:
53
716
    return generateGouraudTriangleBitmap(state,
54
716
           (GfxGouraudTriangleShading *)shading,
55
716
           mode, reverseVideo,
56
716
           parentSplash, bitmapMemCache,
57
716
           xOut, yOut);
58
0
    break;
59
128
  case 6:
60
986
  case 7:
61
986
    return generatePatchMeshBitmap(state, (GfxPatchMeshShading *)shading,
62
986
           mode, reverseVideo,
63
986
           parentSplash, bitmapMemCache, xOut, yOut);
64
0
    break;
65
0
  default:
66
0
    return NULL;
67
3.28k
  }
68
3.28k
}
69
70
1.12k
static double max4(double x0, double x1, double x2, double x3) {
71
1.12k
  double t = x0;
72
1.12k
  t = (x1 > t) ? x1 : t;
73
1.12k
  t = (x2 > t) ? x2 : t;
74
1.12k
  t = (x3 > t) ? x3 : t;
75
1.12k
  return t;
76
1.12k
}
77
78
SplashBitmap *ShadingImage::generateFunctionBitmap(
79
        GfxState *state,
80
        GfxFunctionShading *shading,
81
        SplashColorMode mode,
82
        GBool reverseVideo,
83
        Splash *parentSplash,
84
        SplashBitmapMemCache *bitmapMemCache,
85
0
        int *xOut, int *yOut) {
86
  // get the shading parameters
87
0
  double x0, y0, x1, y1;
88
0
  shading->getDomain(&x0, &y0, &x1, &y1);
89
0
  double *patternMat = shading->getMatrix();
90
91
  // get the clip bbox
92
0
  double fxMin, fyMin, fxMax, fyMax;
93
0
  state->getClipBBox(&fxMin, &fyMin, &fxMax, &fyMax);
94
0
  if (fxMin > fxMax || fyMin > fyMax) {
95
0
    return NULL;
96
0
  }
97
98
  // convert to integer coords
99
0
  int xMin = (int)floor(fxMin);
100
0
  int yMin = (int)floor(fyMin);
101
0
  int xMax = (int)floor(fxMax) + 1;
102
0
  int yMax = (int)floor(fyMax) + 1;
103
0
  int bitmapWidth = xMax - xMin;
104
0
  int bitmapHeight = yMax - yMin;
105
106
  // allocate the bitmap
107
0
  traceMessage("function shading fill bitmap");
108
0
  SplashBitmap *bitmap = new SplashBitmap(bitmapWidth, bitmapHeight, 1, mode,
109
0
            gTrue, gTrue, bitmapMemCache);
110
0
  int nComps = splashColorModeNComps[mode];
111
112
  // compute the domain -> device space transform = mat * CTM
113
0
  double *ctm = state->getCTM();
114
0
  double mat[6];
115
0
  mat[0] = patternMat[0] * ctm[0] + patternMat[1] * ctm[2];
116
0
  mat[1] = patternMat[0] * ctm[1] + patternMat[1] * ctm[3];
117
0
  mat[2] = patternMat[2] * ctm[0] + patternMat[3] * ctm[2];
118
0
  mat[3] = patternMat[2] * ctm[1] + patternMat[3] * ctm[3];
119
0
  mat[4] = patternMat[4] * ctm[0] + patternMat[5] * ctm[2] + ctm[4];
120
0
  mat[5] = patternMat[4] * ctm[1] + patternMat[5] * ctm[3] + ctm[5];
121
122
  // compute the device space -> domain transform
123
0
  double det = mat[0] * mat[3] - mat[1] * mat[2];
124
0
  if (fabs(det) < 0.000001) {
125
0
    return NULL;
126
0
  }
127
0
  det = 1 / det;
128
0
  double iMat[6];
129
0
  iMat[0] = mat[3] * det;
130
0
  iMat[1] = -mat[1] * det;
131
0
  iMat[2] = -mat[2] * det;
132
0
  iMat[3] = mat[0] * det;
133
0
  iMat[4] = (mat[2] * mat[5] - mat[3] * mat[4]) * det;
134
0
  iMat[5] = (mat[1] * mat[4] - mat[0] * mat[5]) * det;
135
136
  // fill the bitmap
137
0
  SplashColorPtr dataPtr = bitmap->getDataPtr();
138
0
  Guchar *alphaPtr = bitmap->getAlphaPtr();
139
0
  for (int y = 0; y < bitmapHeight; ++y) {
140
0
    for (int x = 0; x < bitmapWidth; ++x) {
141
142
      // convert coords to the pattern domain
143
0
      double tx = xMin + x + 0.5;
144
0
      double ty = yMin + y + 0.5;
145
0
      double xx = tx * iMat[0] + ty * iMat[2] + iMat[4];
146
0
      double yy = tx * iMat[1] + ty * iMat[3] + iMat[5];
147
148
      // get the color
149
0
      if (xx >= x0 && xx <= x1 && yy >= y0 && yy <= y1) {
150
0
  GfxColor color;
151
0
  shading->getColor(xx, yy, &color);
152
0
  SplashColor sColor;
153
0
  computeShadingColor(state, mode, reverseVideo, &color, sColor);
154
0
  for (int i = 0; i < nComps; ++i) {
155
0
    *dataPtr++ = sColor[i];
156
0
  }
157
0
  *alphaPtr++ = 0xff;
158
0
      } else {
159
0
  dataPtr += nComps;
160
0
  *alphaPtr++ = 0;
161
0
      }
162
0
    }
163
0
  }
164
165
0
  *xOut = xMin;
166
0
  *yOut = yMin;
167
0
  return bitmap;
168
0
}
169
170
SplashBitmap *ShadingImage::generateAxialBitmap(
171
        GfxState *state,
172
        GfxAxialShading *shading,
173
        SplashColorMode mode,
174
        GBool reverseVideo,
175
        Splash *parentSplash,
176
        SplashBitmapMemCache *bitmapMemCache,
177
1.56k
        int *xOut, int *yOut) {
178
  // get the shading parameters
179
1.56k
  double x0, y0, x1, y1;
180
1.56k
  shading->getCoords(&x0, &y0, &x1, &y1);
181
1.56k
  double t0 = shading->getDomain0();
182
1.56k
  double t1 = shading->getDomain1();
183
1.56k
  GBool ext0 = shading->getExtend0();
184
1.56k
  GBool ext1 = shading->getExtend1();
185
1.56k
  double dx = x1 - x0;
186
1.56k
  double dy = y1 - y0;
187
1.56k
  double d = dx * dx + dy * dy;
188
1.56k
  GBool dZero = fabs(d) < 0.0001;
189
1.56k
  if (!dZero) {
190
1.34k
    d = 1 / d;
191
1.34k
  }
192
1.56k
  if (dZero && !ext0 && !ext1) {
193
112
    return NULL;
194
112
  }
195
196
  // get the clip bbox
197
1.45k
  double fxMin, fyMin, fxMax, fyMax;
198
1.45k
  state->getClipBBox(&fxMin, &fyMin, &fxMax, &fyMax);
199
1.45k
  if (fxMin > fxMax || fyMin > fyMax) {
200
344
    return NULL;
201
344
  }
202
203
  // convert to integer coords
204
1.10k
  int xMin = (int)floor(fxMin);
205
1.10k
  int yMin = (int)floor(fyMin);
206
1.10k
  int xMax = (int)floor(fxMax) + 1;
207
1.10k
  int yMax = (int)floor(fyMax) + 1;
208
1.10k
  int bitmapWidth = xMax - xMin;
209
1.10k
  int bitmapHeight = yMax - yMin;
210
211
  // compute the inverse CTM
212
1.10k
  double *ctm = state->getCTM();
213
1.10k
  double det = ctm[0] * ctm[3] - ctm[1] * ctm[2];
214
1.10k
  if (fabs(det) < 1e-10 * max4(fabs(ctm[0]), fabs(ctm[1]),
215
1.10k
             fabs(ctm[2]), fabs(ctm[3]))) {
216
246
    return NULL;
217
246
  }
218
861
  det = 1 / det;
219
861
  double ictm[6];
220
861
  ictm[0] = ctm[3] * det;
221
861
  ictm[1] = -ctm[1] * det;
222
861
  ictm[2] = -ctm[2] * det;
223
861
  ictm[3] = ctm[0] * det;
224
861
  ictm[4] = (ctm[2] * ctm[5] - ctm[3] * ctm[4]) * det;
225
861
  ictm[5] = (ctm[1] * ctm[4] - ctm[0] * ctm[5]) * det;
226
227
  // convert axis endpoints to device space
228
861
  double xx0, yy0, xx1, yy1;
229
861
  state->transform(x0, y0, &xx0, &yy0);
230
861
  state->transform(x1, y1, &xx1, &yy1);
231
232
  // allocate the bitmap
233
861
  traceMessage("axial shading fill bitmap");
234
861
  SplashBitmap *bitmap = new SplashBitmap(bitmapWidth, bitmapHeight, 1, mode,
235
861
            gTrue, gTrue, bitmapMemCache);
236
861
  int nComps = splashColorModeNComps[mode];
237
238
  // special case: zero-length axis
239
861
  if (dZero) {
240
84
    GfxColor color;
241
84
    if (ext0) {
242
84
      shading->getColor(t0, &color);
243
84
    } else {
244
0
      shading->getColor(t1, &color);
245
0
    }
246
84
    SplashColor sColor;
247
84
    computeShadingColor(state, mode, reverseVideo, &color, sColor);
248
84
    SplashColorPtr dataPtr = bitmap->getDataPtr();
249
168
    for (int y = 0; y < bitmapHeight; ++y) {
250
168
      for (int x = 0; x < bitmapWidth; ++x) {
251
336
  for (int i = 0; i < nComps; ++i) {
252
252
    *dataPtr++ = sColor[i];
253
252
  }
254
84
      }
255
84
    }
256
84
    memset(bitmap->getAlphaPtr(), 0xff, (size_t)bitmapWidth * bitmapHeight);
257
258
  // special case: horizontal axis (in device space)
259
777
  } else if (fabs(yy0 - yy1) < 0.01) {
260
320
    for (int x = 0; x < bitmapWidth; ++x) {
261
160
      SplashColorPtr dataPtr = bitmap->getDataPtr() + x * nComps;
262
160
      Guchar *alphaPtr = bitmap->getAlphaPtr() + x;
263
160
      double tx = xMin + x + 0.5;
264
160
      double ty = yMin + 0.5;
265
160
      double xx = tx * ictm[0] + ty * ictm[2] + ictm[4];
266
160
      double yy = tx * ictm[1] + ty * ictm[3] + ictm[5];
267
160
      double s = ((xx - x0) * dx + (yy - y0) * dy) * d;
268
160
      GBool go = gFalse;
269
160
      if (s < 0) {
270
3
  go = ext0;
271
157
      } else if (s > 1) {
272
103
  go = ext1;
273
103
      } else {
274
54
  go = gTrue;
275
54
      }
276
160
      if (go) {
277
107
  GfxColor color;
278
107
  if (s <= 0) {
279
3
    shading->getColor(t0, &color);
280
104
  } else if (s >= 1) {
281
50
    shading->getColor(t1, &color);
282
54
  } else {
283
54
    double t = t0 + s * (t1 - t0);
284
54
    shading->getColor(t, &color);
285
54
  }
286
107
  SplashColor sColor;
287
107
  computeShadingColor(state, mode, reverseVideo, &color, sColor);
288
214
  for (int y = 0; y < bitmapHeight; ++y) {
289
418
    for (int i = 0; i < nComps; ++i) {
290
311
      dataPtr[i] = sColor[i];
291
311
    }
292
107
    dataPtr += bitmap->getRowSize();
293
107
    *alphaPtr = 0xff;
294
107
    alphaPtr += bitmapWidth;
295
107
  }
296
107
      } else {
297
106
  for (int y = 0; y < bitmapHeight; ++y) {
298
53
    *alphaPtr = 0;
299
53
    alphaPtr += bitmapWidth;
300
53
  }
301
53
      }
302
160
    }
303
304
  // special case: vertical axis (in device space)
305
617
  } else if (fabs(xx0 - xx1) < 0.01) {
306
686
    for (int y = 0; y < bitmapHeight; ++y) {
307
343
      SplashColorPtr dataPtr = bitmap->getDataPtr() + y * bitmap->getRowSize();
308
343
      Guchar *alphaPtr = bitmap->getAlphaPtr() + y * bitmapWidth;
309
343
      double tx = xMin + 0.5;
310
343
      double ty = yMin + y + 0.5;
311
343
      double xx = tx * ictm[0] + ty * ictm[2] + ictm[4];
312
343
      double yy = tx * ictm[1] + ty * ictm[3] + ictm[5];
313
343
      double s = ((xx - x0) * dx + (yy - y0) * dy) * d;
314
343
      GBool go = gFalse;
315
343
      if (s < 0) {
316
95
  go = ext0;
317
248
      } else if (s > 1) {
318
95
  go = ext1;
319
153
      } else {
320
153
  go = gTrue;
321
153
      }
322
343
      if (go) {
323
289
  GfxColor color;
324
289
  if (s <= 0) {
325
65
    shading->getColor(t0, &color);
326
224
  } else if (s >= 1) {
327
71
    shading->getColor(t1, &color);
328
153
  } else {
329
153
    double t = t0 + s * (t1 - t0);
330
153
    shading->getColor(t, &color);
331
153
  }
332
289
  SplashColor sColor;
333
289
  computeShadingColor(state, mode, reverseVideo, &color, sColor);
334
578
  for (int x = 0; x < bitmapWidth; ++x) {
335
1.15k
    for (int i = 0; i < nComps; ++i) {
336
867
      dataPtr[i] = sColor[i];
337
867
    }
338
289
    dataPtr += nComps;
339
289
  }
340
289
  memset(alphaPtr, 0xff, bitmapWidth);
341
289
      } else {
342
54
  memset(alphaPtr, 0, bitmapWidth);
343
54
      }
344
343
    }
345
346
  // general case
347
343
  } else {
348
    // pre-compute colors along the axis
349
274
    int nColors = (int)(1.5 * sqrt((xx1 - xx0) * (xx1 - xx0)
350
274
           + (yy1 - yy0) * (yy1 - yy0)));
351
274
    if (nColors < 16) {
352
270
      nColors = 16;
353
270
    } else if (nColors > 1024) {
354
0
      nColors = 1024;
355
0
    }
356
274
    SplashColorPtr sColors = (SplashColorPtr)gmallocn(nColors, nComps);
357
274
    SplashColorPtr sColor = sColors;
358
4.59k
    for (int i = 0; i < nColors; ++i) {
359
4.32k
      double s = (double)i / (double)(nColors - 1);
360
4.32k
      double t = t0 + s * (t1 - t0);
361
4.32k
      GfxColor color;
362
4.32k
      shading->getColor(t, &color);
363
4.32k
      computeShadingColor(state, mode, reverseVideo, &color, sColor);
364
4.32k
      sColor += nComps;
365
4.32k
    }
366
367
274
    SplashColorPtr dataPtr = bitmap->getDataPtr();
368
274
    Guchar *alphaPtr = bitmap->getAlphaPtr();
369
544
    for (int y = 0; y < bitmapHeight; ++y) {
370
540
      for (int x = 0; x < bitmapWidth; ++x) {
371
372
  // convert coords to user space
373
270
  double tx = xMin + x + 0.5;
374
270
  double ty = yMin + y + 0.5;
375
270
  double xx = tx * ictm[0] + ty * ictm[2] + ictm[4];
376
270
  double yy = tx * ictm[1] + ty * ictm[3] + ictm[5];
377
378
  // compute the position along the axis
379
270
  double s = ((xx - x0) * dx + (yy - y0) * dy) * d;
380
270
  GBool go = gFalse;
381
270
  if (s < 0) {
382
77
    go = ext0;
383
193
  } else if (s > 1) {
384
96
    go = ext1;
385
97
  } else {
386
97
    go = gTrue;
387
97
  }
388
270
  if (go) {
389
240
    if (s <= 0) {
390
107
      sColor = sColors;
391
133
    } else if (s >= 1) {
392
94
      sColor = sColors + (nColors - 1) * nComps;
393
94
    } else {
394
39
      int i = (int)((nColors - 1) * s + 0.5);
395
39
      sColor = sColors + i * nComps;
396
39
    }
397
960
    for (int i = 0; i < nComps; ++i) {
398
720
      *dataPtr++ = sColor[i];
399
720
    }
400
240
    *alphaPtr++ = 0xff;
401
240
  } else {
402
30
    dataPtr += nComps;
403
30
    *alphaPtr++ = 0;
404
30
  }
405
270
      }
406
270
    }
407
274
    gfree(sColors);
408
274
  }
409
410
861
  *xOut = xMin;
411
861
  *yOut = yMin;
412
861
  return bitmap;
413
1.10k
}
414
415
SplashBitmap *ShadingImage::generateRadialBitmap(
416
        GfxState *state,
417
        GfxRadialShading *shading,
418
        SplashColorMode mode,
419
        GBool reverseVideo,
420
        Splash *parentSplash,
421
        SplashBitmapMemCache *bitmapMemCache,
422
16
        int *xOut, int *yOut) {
423
  // get the shading parameters
424
16
  double x0, y0, r0, x1, y1, r1;
425
16
  shading->getCoords(&x0, &y0, &r0, &x1, &y1, &r1);
426
16
  double t0 = shading->getDomain0();
427
16
  double t1 = shading->getDomain1();
428
16
  GBool ext0 = shading->getExtend0();
429
16
  GBool ext1 = shading->getExtend1();
430
16
  double h = sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
431
16
  GBool enclosed = fabs(r1 - r0) >= h;
432
433
  // get the clip bbox
434
16
  double fxMin, fyMin, fxMax, fyMax;
435
16
  state->getClipBBox(&fxMin, &fyMin, &fxMax, &fyMax);
436
16
  if (fxMin > fxMax || fyMin > fyMax) {
437
1
    return NULL;
438
1
  }
439
440
  // intersect with shading region (in user space): if the extend
441
  // flags are false (or just the larger extend flag is false, in the
442
  // "enclosed" case), we can use the bbox for the two circles
443
15
  if ((!ext0 && !ext1) ||
444
10
      (enclosed && !(r0 > r1 ? ext0 : ext1))) {
445
10
    double uxMin = (x0 - r0) < (x1 - r1) ? (x0 - r0) : (x1 - r1);
446
10
    double uxMax = (x0 + r0) > (x1 + r1) ? (x0 + r0) : (x1 + r1);
447
10
    double uyMin = (y0 - r0) < (y1 - r1) ? (y0 - r0) : (y1 - r1);
448
10
    double uyMax = (y0 + r0) > (y1 + r1) ? (y0 + r0) : (y1 + r1);
449
10
    double dxMin, dyMin, dxMax, dyMax;
450
10
    transformBBox(state, uxMin, uyMin, uxMax, uyMax,
451
10
      &dxMin, &dyMin, &dxMax, &dyMax);
452
10
    if (dxMin > fxMin) {
453
0
      fxMin = dxMin;
454
0
    }
455
10
    if (dxMax < dxMax) {
456
0
      fxMax = dxMax;
457
0
    }
458
10
    if (dyMin > fyMin) {
459
2
      fyMin = dyMin;
460
2
    }
461
10
    if (dyMax < fyMax) {
462
1
      fyMax = dyMax;
463
1
    }
464
10
    if (fxMin > fxMax || fyMin > fyMax) {
465
1
      return NULL;
466
1
    }
467
10
  }
468
469
  // convert to integer coords
470
14
  int xMin = (int)floor(fxMin);
471
14
  int yMin = (int)floor(fyMin);
472
14
  int xMax = (int)floor(fxMax) + 1;
473
14
  int yMax = (int)floor(fyMax) + 1;
474
14
  int bitmapWidth = xMax - xMin;
475
14
  int bitmapHeight = yMax - yMin;
476
477
  // compute the inverse CTM
478
14
  double *ctm = state->getCTM();
479
14
  double det = ctm[0] * ctm[3] - ctm[1] * ctm[2];
480
14
  if (fabs(det) < 1e-10 * max4(fabs(ctm[0]), fabs(ctm[1]),
481
14
             fabs(ctm[2]), fabs(ctm[3]))) {
482
0
    return NULL;
483
0
  }
484
14
  det = 1 / det;
485
14
  double ictm[6];
486
14
  ictm[0] = ctm[3] * det;
487
14
  ictm[1] = -ctm[1] * det;
488
14
  ictm[2] = -ctm[2] * det;
489
14
  ictm[3] = ctm[0] * det;
490
14
  ictm[4] = (ctm[2] * ctm[5] - ctm[3] * ctm[4]) * det;
491
14
  ictm[5] = (ctm[1] * ctm[4] - ctm[0] * ctm[5]) * det;
492
493
  // allocate the bitmap
494
14
  traceMessage("radial shading fill bitmap");
495
14
  SplashBitmap *bitmap = new SplashBitmap(bitmapWidth, bitmapHeight, 1, mode,
496
14
            gTrue, gTrue, bitmapMemCache);
497
14
  int nComps = splashColorModeNComps[mode];
498
499
  // pre-compute colors along the axis
500
14
  int nColors = (int)sqrt((double)(bitmapWidth * bitmapWidth
501
14
           + bitmapHeight * bitmapHeight));
502
14
  if (nColors < 16) {
503
13
    nColors = 16;
504
13
  } else if (nColors > 1024) {
505
0
    nColors = 1024;
506
0
  }
507
14
  SplashColorPtr sColors = (SplashColorPtr)gmallocn(nColors, nComps);
508
14
  SplashColorPtr sColor = sColors;
509
222
  for (int i = 0; i < nColors; ++i) {
510
208
    double s = (double)i / (double)(nColors - 1);
511
208
    double t = t0 + s * (t1 - t0);
512
208
    GfxColor color;
513
208
    shading->getColor(t, &color);
514
208
    computeShadingColor(state, mode, reverseVideo, &color, sColor);
515
208
    sColor += nComps;
516
208
  }
517
518
  // special case: in the "enclosed" + extended case, we can fill the
519
  // bitmap with the outer color and just render inside the larger
520
  // circle
521
14
  int bxMin, byMin, bxMax, byMax;
522
14
  if (enclosed &&
523
12
      ((r0 > r1 && ext0) || (r1 > r0 && ext1))) {
524
2
    double uxMin, uyMin, uxMax, uyMax;
525
2
    if (r0 > r1) {
526
1
      sColor = sColors;
527
1
      uxMin = x0 - r0;
528
1
      uxMax = x0 + r0;
529
1
      uyMin = y0 - r0;
530
1
      uyMax = y0 + r0;
531
1
    } else {
532
1
      sColor = sColors + (nColors - 1) * nComps;
533
1
      uxMin = x1 - r1;
534
1
      uxMax = x1 + r1;
535
1
      uyMin = y1 - r1;
536
1
      uyMax = y1 + r1;
537
1
    }
538
539
    // convert bbox of larger circle to device space
540
2
    double dxMin, dyMin, dxMax, dyMax;
541
2
    transformBBox(state, uxMin, uyMin, uxMax, uyMax,
542
2
      &dxMin, &dyMin, &dxMax, &dyMax);
543
2
    bxMin = (int)floor(dxMin - xMin);
544
2
    if (bxMin < 0) {
545
2
      bxMin = 0;
546
2
    }
547
2
    byMin = (int)floor(dyMin - yMin);
548
2
    if (byMin < 0) {
549
2
      byMin = 0;
550
2
    }
551
2
    bxMax = (int)floor(dxMax - xMin) + 1;
552
2
    if (bxMax > bitmapWidth) {
553
0
      bxMax = bitmapWidth;
554
0
    }
555
2
    byMax = (int)floor(dyMax - yMin) + 1;
556
2
    if (byMax > bitmapHeight) {
557
0
      byMax = bitmapHeight;
558
0
    }
559
560
    // fill bitmap (except for the rectangle containing the larger circle)
561
2
    SplashColorPtr dataPtr = bitmap->getDataPtr();
562
2
    Guchar *alphaPtr = bitmap->getAlphaPtr();
563
4
    for (int y = 0; y < bitmapHeight; ++y) {
564
4
      for (int x = 0; x < bitmapWidth; ++x) {
565
2
  if (y >= byMin && y < byMax && x >= bxMin && x < bxMax) {
566
0
    dataPtr += nComps;
567
0
    ++alphaPtr;
568
2
  } else {
569
8
    for (int i = 0; i < nComps; ++i) {
570
6
      *dataPtr++ = sColor[i];
571
6
    }
572
2
    *alphaPtr++ = 0xff;
573
2
  }
574
2
      }
575
2
    }
576
577
12
  } else {
578
12
    bxMin = 0;
579
12
    byMin = 0;
580
12
    bxMax = bitmapWidth;
581
12
    byMax = bitmapHeight;
582
12
  }
583
584
  // render the shading into the bitmap
585
14
  double dx = x1 - x0;
586
14
  double dy = y1 - y0;
587
14
  double dr = r1 - r0;
588
14
  double r0dr = r0 * dr;
589
14
  double r02 = r0 * r0;
590
14
  double a = dx * dx + dy * dy - dr * dr;
591
14
  GBool aIsZero;
592
14
  double a2;
593
14
  if (fabs(a) < 0.00001) {
594
7
    aIsZero = gTrue;
595
7
    a2 = 0;
596
7
  } else {
597
7
    aIsZero = gFalse;
598
7
    a2 = 1 / (2 * a);
599
7
  }
600
25
  for (int y = byMin; y < byMax; ++y) {
601
11
    SplashColorPtr dataPtr = bitmap->getDataPtr()
602
11
                             + y * bitmap->getRowSize() + bxMin * nComps;
603
11
    Guchar *alphaPtr = bitmap->getAlphaPtr()
604
11
                       + y * bitmap->getAlphaRowSize() + bxMin;
605
22
    for (int x = bxMin; x < bxMax; ++x) {
606
607
      // convert coords to user space
608
11
      double tx = xMin + x + 0.5;
609
11
      double ty = yMin + y + 0.5;
610
11
      double xx = tx * ictm[0] + ty * ictm[2] + ictm[4];
611
11
      double yy = tx * ictm[1] + ty * ictm[3] + ictm[5];
612
613
      // compute the radius of the circle at x,y
614
11
      double b = 2 * ((xx - x0) * dx + (yy - y0) * dy + r0dr);
615
11
      double c = (xx - x0) * (xx - x0) + (yy - y0) * (yy - y0) - r02;
616
11
      double s = 0;
617
11
      GBool go = gFalse;
618
11
      if (aIsZero) {
619
5
  if (fabs(b) < 0.000001) {
620
1
    if (c <= 0) {
621
0
      if (ext0) {
622
0
        s = 0;
623
0
        go = gTrue;
624
0
      }
625
1
    } else {
626
1
      if (ext1) {
627
0
        s = 1;
628
0
        go = gTrue;
629
0
      }
630
1
    }
631
4
  } else {
632
4
    double s0 = c / b;
633
4
    double rs0 = r0 + s0 * (r1 - r0);
634
4
    if ((s0 >= 0 || ext0) && (s0 <= 1 || ext1) && rs0 >= 0) { 
635
0
      s = s0;
636
0
      go = gTrue;
637
0
    }
638
4
  }
639
6
      } else {
640
6
  double e = b*b - 4*a*c;
641
6
  if (e >= 0) {
642
5
    double es = sqrt(e);
643
5
    double s0 = (b + es) * a2;
644
5
    double s1 = (b - es) * a2;
645
5
    double rs0 = r0 + s0 * (r1 - r0);
646
5
    double rs1 = r0 + s1 * (r1 - r0);
647
5
    if (s0 > s1) {
648
0
      if ((s0 >= 0 || ext0) && (s0 <= 1 || ext1) && rs0 >= 0) {
649
0
        s = s0;
650
0
        go = gTrue;
651
0
      } else if ((s1 >= 0 || ext0) && (s1 <= 1 || ext1) && rs1 >= 0) {
652
0
        s = s1;
653
0
        go = gTrue;
654
0
      }
655
5
    } else {
656
5
      if ((s1 >= 0 || ext0) && (s1 <= 1 || ext1) && rs1 >= 0) {
657
2
        s = s1;
658
2
        go = gTrue;
659
3
      } else if ((s0 >= 0 || ext0) && (s0 <= 1 || ext1) && rs0 >= 0) {
660
1
        s = s0;
661
1
        go = gTrue;
662
1
      }
663
5
    }
664
5
  }
665
6
      }
666
11
      if (!go) {
667
8
  dataPtr += nComps;
668
8
  *alphaPtr++ = 0x00;
669
8
  continue;
670
8
      }
671
3
      if (s <= 0) {
672
0
  sColor = sColors;
673
3
      } else if (s >= 1) {
674
0
  sColor = sColors + (nColors - 1) * nComps;
675
3
      } else {
676
3
  int i = (int)((nColors - 1) * s + 0.5);
677
3
  sColor = sColors + i * nComps;
678
3
      }
679
12
      for (int i = 0; i < nComps; ++i) {
680
9
  *dataPtr++ = sColor[i];
681
9
      }
682
3
      *alphaPtr++ = 0xff;
683
3
    }
684
11
  }
685
686
14
  gfree(sColors);
687
688
14
  *xOut = xMin;
689
14
  *yOut = yMin;
690
14
  return bitmap;
691
14
}
692
693
SplashBitmap *ShadingImage::generateGouraudTriangleBitmap(
694
          GfxState *state,
695
          GfxGouraudTriangleShading *shading,
696
          SplashColorMode mode,
697
          GBool reverseVideo,
698
          Splash *parentSplash,
699
          SplashBitmapMemCache *bitmapMemCache,
700
716
          int *xOut, int *yOut) {
701
  // get the clip bbox
702
716
  double fxMin, fyMin, fxMax, fyMax;
703
716
  state->getClipBBox(&fxMin, &fyMin, &fxMax, &fyMax);
704
716
  if (fxMin > fxMax || fyMin > fyMax) {
705
195
    return NULL;
706
195
  }
707
708
  // get the shading bbox
709
521
  double tx0, ty0, tx1, ty1, dx, dy, txMin, tyMin, txMax, tyMax;
710
521
  shading->getBBox(&tx0, &ty0, &tx1, &ty1);
711
521
  state->transform(tx0, ty0, &dx, &dy);
712
521
  txMin = txMax = dx;
713
521
  tyMin = tyMax = dy;
714
521
  state->transform(tx0, ty1, &dx, &dy);
715
521
  if (dx < txMin) {
716
7
    txMin = dx;
717
514
  } else if (dx > txMax) {
718
20
    txMax = dx;
719
20
  }
720
521
  if (dy < tyMin) {
721
47
    tyMin = dy;
722
474
  } else if (dy > tyMax) {
723
44
    tyMax = dy;
724
44
  }
725
521
  state->transform(tx1, ty0, &dx, &dy);
726
521
  if (dx < txMin) {
727
44
    txMin = dx;
728
477
  } else if (dx > txMax) {
729
200
    txMax = dx;
730
200
  }
731
521
  if (dy < tyMin) {
732
177
    tyMin = dy;
733
344
  } else if (dy > tyMax) {
734
27
    tyMax = dy;
735
27
  }
736
521
  state->transform(tx1, ty1, &dx, &dy);
737
521
  if (dx < txMin) {
738
1
    txMin = dx;
739
520
  } else if (dx > txMax) {
740
6
    txMax = dx;
741
6
  }
742
521
  if (dy < tyMin) {
743
31
    tyMin = dy;
744
490
  } else if (dy > tyMax) {
745
13
    tyMax = dy;
746
13
  }
747
521
  if (txMin > fxMin) {
748
30
    fxMin = txMin;
749
30
  }
750
521
  if (txMax < fxMax) {
751
56
    fxMax = txMax;
752
56
  }
753
521
  if (tyMin > fyMin) {
754
20
    fyMin = tyMin;
755
20
  }
756
521
  if (tyMax < fyMax) {
757
50
    fyMax = tyMax;
758
50
  }
759
521
  if (fxMin > fxMax || fyMin > fyMax) {
760
63
    return NULL;
761
63
  }
762
763
  // convert to integer coords
764
458
  int xMin = (int)floor(fxMin);
765
458
  int yMin = (int)floor(fyMin);
766
458
  int xMax = (int)floor(fxMax) + 1;
767
458
  int yMax = (int)floor(fyMax) + 1;
768
458
  int bitmapWidth = xMax - xMin;
769
458
  int bitmapHeight = yMax - yMin;
770
771
  // allocate the bitmap
772
458
  traceMessage("Gouraud triangle shading fill bitmap");
773
458
  SplashBitmap *bitmap = new SplashBitmap(bitmapWidth, bitmapHeight, 1, mode,
774
458
            gTrue, gTrue, bitmapMemCache);
775
776
  // clear the bitmap
777
458
  memset(bitmap->getDataPtr(), 0, bitmap->getHeight() * bitmap->getRowSize());
778
458
  memset(bitmap->getAlphaPtr(), 0, bitmap->getHeight() * bitmap->getWidth());
779
780
  // draw the triangles
781
656k
  for (int i = 0; i < shading->getNTriangles(); ++i) {
782
655k
    double x0, y0, x1, y1, x2, y2;
783
655k
    double color0[gfxColorMaxComps];
784
655k
    double color1[gfxColorMaxComps];
785
655k
    double color2[gfxColorMaxComps];
786
655k
    shading->getTriangle(i, &x0, &y0, color0,
787
655k
       &x1, &y1, color1,
788
655k
       &x2, &y2, color2);
789
655k
    gouraudFillTriangle(state, bitmap, mode, reverseVideo,
790
655k
      xMin, yMin, xMax, yMax,
791
655k
      x0, y0, color0, x1, y1, color1, x2, y2, color2,
792
655k
      shading);
793
655k
  }
794
795
458
  *xOut = xMin;
796
458
  *yOut = yMin;
797
458
  return bitmap;
798
521
}
799
800
void ShadingImage::gouraudFillTriangle(GfxState *state, SplashBitmap *bitmap,
801
               SplashColorMode mode,
802
               GBool reverseVideo,
803
               int xMin, int yMin, int xMax, int yMax,
804
               double x0, double y0, double *color0,
805
               double x1, double y1, double *color1,
806
               double x2, double y2, double *color2,
807
655k
               GfxGouraudTriangleShading *shading) {
808
655k
  int nShadingComps = shading->getNComps();
809
655k
  int nBitmapComps = splashColorModeNComps[mode];
810
811
  //--- transform the vertices to device space, sort by y
812
655k
  double dx0, dy0, dx1, dy1, dx2, dy2;
813
655k
  state->transform(x0, y0, &dx0, &dy0);
814
655k
  state->transform(x1, y1, &dx1, &dy1);
815
655k
  state->transform(x2, y2, &dx2, &dy2);
816
655k
  if (dy0 > dy1) {
817
95.5k
    double t = dx0;  dx0 = dx1;  dx1 = t;
818
95.5k
    t = dy0;  dy0 = dy1;  dy1 = t;
819
95.5k
    double *tc = color0;  color0 = color1;  color1 = tc;
820
95.5k
  }
821
655k
  if (dy1 > dy2) {
822
133k
    double t = dx1;  dx1 = dx2;  dx2 = t;
823
133k
    t = dy1;  dy1 = dy2;  dy2 = t;
824
133k
    double *tc = color1;  color1 = color2;  color2 = tc;
825
133k
  }
826
655k
  if (dy0 > dy1) {
827
68.4k
    double t = dx0;  dx0 = dx1;  dx1 = t;
828
68.4k
    t = dy0;  dy0 = dy1;  dy1 = t;
829
68.4k
    double *tc = color0;  color0 = color1;  color1 = tc;
830
68.4k
  }
831
832
  //--- y loop
833
655k
  int syMin = (int)floor(dy0);
834
655k
  if (syMin < yMin) {
835
25.4k
    syMin = yMin;
836
25.4k
  }
837
655k
  int syMax = (int)floor(dy2) + 1;
838
655k
  if (syMax > yMax) {
839
13.5k
    syMax = yMax;
840
13.5k
  }
841
1.23M
  for (int sy = syMin; sy < syMax; ++sy) {
842
843
    //--- vertical interpolation
844
578k
    double xx0, xx1;
845
578k
    double cc0[gfxColorMaxComps], cc1[gfxColorMaxComps];
846
578k
    if (sy <= dy0) {
847
340k
      xx0 = xx1 = dx0;
848
1.02M
      for (int i = 0; i < nShadingComps; ++i) {
849
684k
  cc0[i] = cc1[i] = color0[i];
850
684k
      }
851
340k
    } else if (sy >= dy2) {
852
61.9k
      xx0 = xx1 = dx2;
853
174k
      for (int i = 0; i < nShadingComps; ++i) {
854
112k
  cc0[i] = cc1[i] = color2[i];
855
112k
      }
856
175k
    } else {
857
175k
      if (sy <= dy1) {
858
47.6k
  double interp = (sy - dy0) / (dy1 - dy0);
859
47.6k
  xx0 = dx0 + interp * (dx1 - dx0);
860
151k
  for (int i = 0; i < nShadingComps; ++i) {
861
104k
    cc0[i] = color0[i] + interp * (color1[i] - color0[i]);
862
104k
  }
863
128k
      } else {
864
128k
  double interp = (sy - dy1) / (dy2 - dy1);
865
128k
  xx0 = dx1 + interp * (dx2 - dx1);
866
487k
  for (int i = 0; i < nShadingComps; ++i) {
867
359k
    cc0[i] = color1[i] + interp * (color2[i] - color1[i]);
868
359k
  }
869
128k
      }
870
175k
      double interp = (sy - dy0) / (dy2 - dy0);
871
175k
      xx1 = dx0 + interp * (dx2 - dx0);
872
639k
      for (int i = 0; i < nShadingComps; ++i) {
873
463k
  cc1[i] = color0[i] + interp * (color2[i] - color0[i]);
874
463k
      }
875
175k
    }
876
877
    //--- x loop
878
578k
    if (xx0 > xx1) {
879
10.6k
      double t = xx0;  xx0 = xx1;  xx1 = t;
880
33.1k
      for (int i = 0; i < nShadingComps; ++i) {
881
22.5k
  t = cc0[i];  cc0[i] = cc1[i];  cc1[i] = t;
882
22.5k
      }
883
10.6k
    }
884
578k
    int sxMin = (int)floor(xx0);
885
578k
    if (sxMin < xMin) {
886
242k
      sxMin = xMin;
887
242k
    }
888
578k
    int sxMax = (int)floor(xx1) + 1;
889
578k
    if (sxMax > xMax) {
890
184k
      sxMax = xMax;
891
184k
    }
892
578k
    SplashColorPtr dataPtr = bitmap->getDataPtr()
893
578k
                             + (sy - yMin) * bitmap->getRowSize()
894
578k
                             + (sxMin - xMin) * nBitmapComps;
895
578k
    if (sxMin < sxMax) {
896
151k
      Guchar *alphaPtr = bitmap->getAlphaPtr()
897
151k
                   + (sy - yMin) * bitmap->getWidth()
898
151k
                   + (sxMin - xMin);
899
151k
      memset(alphaPtr, 0xff, sxMax - sxMin);
900
151k
    }
901
729k
    for (int sx = sxMin; sx < sxMax; ++sx) {
902
903
      //--- horizontal interpolation
904
151k
      double cc[gfxColorMaxComps];
905
151k
      if (sx <= xx0) {
906
106k
  for (int i = 0; i < nShadingComps; ++i) {
907
68.4k
    cc[i] = cc0[i];
908
68.4k
  }
909
112k
      } else if (sx >= xx1) {
910
28.6k
  for (int i = 0; i < nShadingComps; ++i) {
911
15.7k
    cc[i] = cc1[i];
912
15.7k
  }
913
100k
      } else {
914
400k
  for (int i = 0; i < nShadingComps; ++i) {
915
300k
    double interp = (sx - xx0) / (xx1 - xx0);
916
300k
    cc[i] = cc0[i] + interp * (cc1[i] - cc0[i]);
917
300k
  }
918
100k
      }
919
920
      //--- compute color and set pixel
921
151k
      GfxColor gColor;
922
151k
      shading->getColor(cc, &gColor);
923
151k
      SplashColor sColor;
924
151k
      computeShadingColor(state, mode, reverseVideo, &gColor, sColor);
925
605k
      for (int i = 0; i < nBitmapComps; ++i) {
926
453k
  dataPtr[i] = sColor[i];
927
453k
      }
928
151k
      dataPtr += nBitmapComps;
929
151k
    }
930
578k
  }
931
655k
}
932
933
SplashBitmap *ShadingImage::generatePatchMeshBitmap(
934
          GfxState *state,
935
          GfxPatchMeshShading *shading,
936
          SplashColorMode mode,
937
          GBool reverseVideo,
938
          Splash *parentSplash,
939
          SplashBitmapMemCache *bitmapMemCache,
940
986
          int *xOut, int *yOut) {
941
  // get the clip bbox
942
986
  double fxMin, fyMin, fxMax, fyMax;
943
986
  state->getClipBBox(&fxMin, &fyMin, &fxMax, &fyMax);
944
986
  if (fxMin > fxMax || fyMin > fyMax) {
945
135
    return NULL;
946
135
  }
947
948
  // get the shading bbox
949
851
  double tx0, ty0, tx1, ty1, dx, dy, txMin, tyMin, txMax, tyMax;
950
851
  shading->getBBox(&tx0, &ty0, &tx1, &ty1);
951
851
  state->transform(tx0, ty0, &dx, &dy);
952
851
  txMin = txMax = dx;
953
851
  tyMin = tyMax = dy;
954
851
  state->transform(tx0, ty1, &dx, &dy);
955
851
  if (dx < txMin) {
956
122
    txMin = dx;
957
729
  } else if (dx > txMax) {
958
187
    txMax = dx;
959
187
  }
960
851
  if (dy < tyMin) {
961
274
    tyMin = dy;
962
577
  } else if (dy > tyMax) {
963
325
    tyMax = dy;
964
325
  }
965
851
  state->transform(tx1, ty0, &dx, &dy);
966
851
  if (dx < txMin) {
967
100
    txMin = dx;
968
751
  } else if (dx > txMax) {
969
442
    txMax = dx;
970
442
  }
971
851
  if (dy < tyMin) {
972
138
    tyMin = dy;
973
713
  } else if (dy > tyMax) {
974
95
    tyMax = dy;
975
95
  }
976
851
  state->transform(tx1, ty1, &dx, &dy);
977
851
  if (dx < txMin) {
978
69
    txMin = dx;
979
782
  } else if (dx > txMax) {
980
118
    txMax = dx;
981
118
  }
982
851
  if (dy < tyMin) {
983
81
    tyMin = dy;
984
770
  } else if (dy > tyMax) {
985
106
    tyMax = dy;
986
106
  }
987
851
  if (txMin > fxMin) {
988
396
    fxMin = txMin;
989
396
  }
990
851
  if (txMax < fxMax) {
991
784
    fxMax = txMax;
992
784
  }
993
851
  if (tyMin > fyMin) {
994
54
    fyMin = tyMin;
995
54
  }
996
851
  if (tyMax < fyMax) {
997
644
    fyMax = tyMax;
998
644
  }
999
851
  if (fxMin > fxMax || fyMin > fyMax) {
1000
627
    return NULL;
1001
627
  }
1002
1003
  // convert to integer coords
1004
224
  int xMin = (int)floor(fxMin);
1005
224
  int yMin = (int)floor(fyMin);
1006
224
  int xMax = (int)floor(fxMax) + 1;
1007
224
  int yMax = (int)floor(fyMax) + 1;
1008
224
  int bitmapWidth = xMax - xMin;
1009
224
  int bitmapHeight = yMax - yMin;
1010
1011
  // allocate the bitmap
1012
224
  traceMessage("Gouraud triangle shading fill bitmap");
1013
224
  SplashBitmap *bitmap = new SplashBitmap(bitmapWidth, bitmapHeight, 1, mode,
1014
224
            gTrue, gTrue, bitmapMemCache);
1015
1016
  // allocate a Splash object
1017
  // vector antialiasing is disabled to avoid artifacts along triangle edges
1018
224
  Splash *splash = new Splash(bitmap, gFalse,
1019
224
            parentSplash->getImageCache(),
1020
224
            parentSplash->getScreen());
1021
224
  SplashColor zero;
1022
884
  for (int i = 0; i < splashColorModeNComps[mode]; ++i) {
1023
660
    zero[i] = 0;
1024
660
  }
1025
224
  splash->clear(zero, 0x00);
1026
1027
  // draw the patches
1028
224
  int start;
1029
224
  if (shading->getNPatches() > 128) {
1030
1
    start = 3;
1031
223
  } else if (shading->getNPatches() > 64) {
1032
24
    start = 2;
1033
199
  } else if (shading->getNPatches() > 16) {
1034
8
    start = 1;
1035
191
  } else {
1036
191
    start = 0;
1037
191
  }
1038
2.86k
  for (int i = 0; i < shading->getNPatches(); ++i) {
1039
2.64k
    fillPatch(state, splash, mode, reverseVideo,
1040
2.64k
        xMin, yMin, shading->getPatch(i), shading, start);
1041
2.64k
  }
1042
1043
224
  delete splash;
1044
1045
224
  *xOut = xMin;
1046
224
  *yOut = yMin;
1047
224
  return bitmap;
1048
851
}
1049
1050
void ShadingImage::fillPatch(GfxState *state, Splash *splash,
1051
           SplashColorMode mode, GBool reverseVideo,
1052
           int xMin, int yMin,
1053
           GfxPatch *patch,
1054
           GfxPatchMeshShading *shading,
1055
3.37M
           int depth) {
1056
3.37M
  GfxColor c00;
1057
3.37M
  shading->getColor(patch->color[0][0], &c00);
1058
3.37M
  GBool stop = gFalse;
1059
1060
  // stop subdivision at max depth
1061
3.37M
  if (depth == patchMaxDepth) {
1062
0
    stop = gTrue;
1063
0
  }
1064
1065
  // stop subdivision if colors are close enough
1066
3.37M
  if (!stop) {
1067
3.37M
    int nComps = shading->getColorSpace()->getNComps();
1068
3.37M
    GfxColor c01, c10, c11;
1069
3.37M
    shading->getColor(patch->color[0][1], &c01);
1070
3.37M
    shading->getColor(patch->color[1][0], &c10);
1071
3.37M
    shading->getColor(patch->color[1][1], &c11);
1072
3.37M
    int i;
1073
14.7M
    for (i = 0; i < nComps; ++i) {
1074
12.2M
      if (abs(c00.c[i] - c01.c[i]) > patchColorDelta ||
1075
11.6M
    abs(c01.c[i] - c11.c[i]) > patchColorDelta ||
1076
11.3M
    abs(c11.c[i] - c10.c[i]) > patchColorDelta ||
1077
11.3M
    abs(c10.c[i] - c00.c[i]) > patchColorDelta) {
1078
843k
  break;
1079
843k
      }
1080
12.2M
    }
1081
3.37M
    if (i == nComps) {
1082
2.53M
      stop = gTrue;
1083
2.53M
    }
1084
3.37M
  }
1085
1086
  // stop subdivision if patch is small enough
1087
3.37M
  if (!stop) {
1088
843k
    double xxMin = 0;
1089
843k
    double yyMin = 0;
1090
843k
    double xxMax = 0;
1091
843k
    double yyMax = 0;
1092
4.21M
    for (int j = 0; j < 4; ++j) {
1093
16.8M
      for (int i = 0; i < 4; ++i) {
1094
13.4M
  double xx, yy;
1095
13.4M
  state->transformDelta(patch->x[i][j], patch->y[i][j], &xx, &yy);
1096
13.4M
  if (i == 0 && j == 0) {
1097
843k
    xxMin = xxMax = xx;
1098
843k
    yyMin = yyMax = yy;
1099
12.6M
  } else {
1100
12.6M
    if (xx < xxMin) {
1101
2.87M
      xxMin = xx;
1102
9.77M
    } else if (xx > xxMax) {
1103
2.26M
      xxMax = xx;
1104
2.26M
    }
1105
12.6M
    if (yy < yyMin) {
1106
2.89M
      yyMin = yy;
1107
9.75M
    } else if (yy > yyMax) {
1108
3.40M
      yyMax = yy;
1109
3.40M
    }
1110
12.6M
  }
1111
13.4M
      }
1112
3.37M
    }
1113
843k
    if (xxMax - xxMin < 1 && yyMax - yyMin < 1) {
1114
554
      stop = gTrue;
1115
554
    }
1116
843k
  }
1117
1118
  // draw the patch
1119
3.37M
  if (stop) {
1120
2.53M
    SplashColor sColor;
1121
2.53M
    computeShadingColor(state, mode, reverseVideo, &c00, sColor);
1122
2.53M
    splash->setFillPattern(new SplashSolidColor(sColor));
1123
2.53M
    SplashPath *path = new SplashPath();
1124
2.53M
    double xx0, yy0, xx1, yy1, xx2, yy2, xx3, yy3;
1125
2.53M
    state->transform(patch->x[0][0], patch->y[0][0], &xx0, &yy0);
1126
2.53M
    path->moveTo(xx0 - xMin, yy0 - yMin);
1127
2.53M
    state->transform(patch->x[0][1], patch->y[0][1], &xx1, &yy1);
1128
2.53M
    state->transform(patch->x[0][2], patch->y[0][2], &xx2, &yy2);
1129
2.53M
    state->transform(patch->x[0][3], patch->y[0][3], &xx3, &yy3);
1130
2.53M
    path->curveTo(xx1 - xMin, yy1 - yMin, xx2 - xMin, yy2 - yMin,
1131
2.53M
      xx3 - xMin, yy3 - yMin);
1132
2.53M
    state->transform(patch->x[1][3], patch->y[1][3], &xx1, &yy1);
1133
2.53M
    state->transform(patch->x[2][3], patch->y[2][3], &xx2, &yy2);
1134
2.53M
    state->transform(patch->x[3][3], patch->y[3][3], &xx3, &yy3);
1135
2.53M
    path->curveTo(xx1 - xMin, yy1 - yMin, xx2 - xMin, yy2 - yMin,
1136
2.53M
      xx3 - xMin, yy3 - yMin);
1137
2.53M
    state->transform(patch->x[3][2], patch->y[3][2], &xx1, &yy1);
1138
2.53M
    state->transform(patch->x[3][1], patch->y[3][1], &xx2, &yy2);
1139
2.53M
    state->transform(patch->x[3][0], patch->y[3][0], &xx3, &yy3);
1140
2.53M
    path->curveTo(xx1 - xMin, yy1 - yMin, xx2 - xMin, yy2 - yMin,
1141
2.53M
      xx3 - xMin, yy3 - yMin);
1142
2.53M
    state->transform(patch->x[2][0], patch->y[2][0], &xx1, &yy1);
1143
2.53M
    state->transform(patch->x[1][0], patch->y[1][0], &xx2, &yy2);
1144
2.53M
    path->curveTo(xx1 - xMin, yy1 - yMin, xx2 - xMin, yy2 - yMin,
1145
2.53M
      xx0 - xMin, yy0 - yMin);
1146
2.53M
    path->close();
1147
2.53M
    splash->fill(path, gFalse);
1148
2.53M
    delete path;
1149
1150
  // subdivide the patch
1151
2.53M
  } else {
1152
842k
    double xx[4][8], yy[4][8];
1153
4.21M
    for (int i = 0; i < 4; ++i) {
1154
3.37M
      xx[i][0] = patch->x[i][0];
1155
3.37M
      yy[i][0] = patch->y[i][0];
1156
3.37M
      xx[i][1] = 0.5 * (patch->x[i][0] + patch->x[i][1]);
1157
3.37M
      yy[i][1] = 0.5 * (patch->y[i][0] + patch->y[i][1]);
1158
3.37M
      double xxm = 0.5 * (patch->x[i][1] + patch->x[i][2]);
1159
3.37M
      double yym = 0.5 * (patch->y[i][1] + patch->y[i][2]);
1160
3.37M
      xx[i][6] = 0.5 * (patch->x[i][2] + patch->x[i][3]);
1161
3.37M
      yy[i][6] = 0.5 * (patch->y[i][2] + patch->y[i][3]);
1162
3.37M
      xx[i][2] = 0.5 * (xx[i][1] + xxm);
1163
3.37M
      yy[i][2] = 0.5 * (yy[i][1] + yym);
1164
3.37M
      xx[i][5] = 0.5 * (xxm + xx[i][6]);
1165
3.37M
      yy[i][5] = 0.5 * (yym + yy[i][6]);
1166
3.37M
      xx[i][3] = xx[i][4] = 0.5 * (xx[i][2] + xx[i][5]);
1167
3.37M
      yy[i][3] = yy[i][4] = 0.5 * (yy[i][2] + yy[i][5]);
1168
3.37M
      xx[i][7] = patch->x[i][3];
1169
3.37M
      yy[i][7] = patch->y[i][3];
1170
3.37M
    }
1171
842k
    GfxPatch patch00, patch01, patch10, patch11;
1172
4.21M
    for (int i = 0; i < 4; ++i) {
1173
3.37M
      patch00.x[0][i] = xx[0][i];
1174
3.37M
      patch00.y[0][i] = yy[0][i];
1175
3.37M
      patch00.x[1][i] = 0.5 * (xx[0][i] + xx[1][i]);
1176
3.37M
      patch00.y[1][i] = 0.5 * (yy[0][i] + yy[1][i]);
1177
3.37M
      double xxm = 0.5 * (xx[1][i] + xx[2][i]);
1178
3.37M
      double yym = 0.5 * (yy[1][i] + yy[2][i]);
1179
3.37M
      patch10.x[2][i] = 0.5 * (xx[2][i] + xx[3][i]);
1180
3.37M
      patch10.y[2][i] = 0.5 * (yy[2][i] + yy[3][i]);
1181
3.37M
      patch00.x[2][i] = 0.5 * (patch00.x[1][i] + xxm);
1182
3.37M
      patch00.y[2][i] = 0.5 * (patch00.y[1][i] + yym);
1183
3.37M
      patch10.x[1][i] = 0.5 * (xxm + patch10.x[2][i]);
1184
3.37M
      patch10.y[1][i] = 0.5 * (yym + patch10.y[2][i]);
1185
3.37M
      patch00.x[3][i] = 0.5 * (patch00.x[2][i] + patch10.x[1][i]);
1186
3.37M
      patch00.y[3][i] = 0.5 * (patch00.y[2][i] + patch10.y[1][i]);
1187
3.37M
      patch10.x[0][i] = patch00.x[3][i];
1188
3.37M
      patch10.y[0][i] = patch00.y[3][i];
1189
3.37M
      patch10.x[3][i] = xx[3][i];
1190
3.37M
      patch10.y[3][i] = yy[3][i];
1191
3.37M
    }
1192
4.21M
    for (int i = 4; i < 8; ++i) {
1193
3.37M
      patch01.x[0][i-4] = xx[0][i];
1194
3.37M
      patch01.y[0][i-4] = yy[0][i];
1195
3.37M
      patch01.x[1][i-4] = 0.5 * (xx[0][i] + xx[1][i]);
1196
3.37M
      patch01.y[1][i-4] = 0.5 * (yy[0][i] + yy[1][i]);
1197
3.37M
      double xxm = 0.5 * (xx[1][i] + xx[2][i]);
1198
3.37M
      double yym = 0.5 * (yy[1][i] + yy[2][i]);
1199
3.37M
      patch11.x[2][i-4] = 0.5 * (xx[2][i] + xx[3][i]);
1200
3.37M
      patch11.y[2][i-4] = 0.5 * (yy[2][i] + yy[3][i]);
1201
3.37M
      patch01.x[2][i-4] = 0.5 * (patch01.x[1][i-4] + xxm);
1202
3.37M
      patch01.y[2][i-4] = 0.5 * (patch01.y[1][i-4] + yym);
1203
3.37M
      patch11.x[1][i-4] = 0.5 * (xxm + patch11.x[2][i-4]);
1204
3.37M
      patch11.y[1][i-4] = 0.5 * (yym + patch11.y[2][i-4]);
1205
3.37M
      patch01.x[3][i-4] = 0.5 * (patch01.x[2][i-4] + patch11.x[1][i-4]);
1206
3.37M
      patch01.y[3][i-4] = 0.5 * (patch01.y[2][i-4] + patch11.y[1][i-4]);
1207
3.37M
      patch11.x[0][i-4] = patch01.x[3][i-4];
1208
3.37M
      patch11.y[0][i-4] = patch01.y[3][i-4];
1209
3.37M
      patch11.x[3][i-4] = xx[3][i];
1210
3.37M
      patch11.y[3][i-4] = yy[3][i];
1211
3.37M
    }
1212
4.21M
    for (int i = 0; i < shading->getNComps(); ++i) {
1213
3.37M
      patch00.color[0][0][i] = patch->color[0][0][i];
1214
3.37M
      patch00.color[0][1][i] = 0.5 * (patch->color[0][0][i] +
1215
3.37M
              patch->color[0][1][i]);
1216
3.37M
      patch01.color[0][0][i] = patch00.color[0][1][i];
1217
3.37M
      patch01.color[0][1][i] = patch->color[0][1][i];
1218
3.37M
      patch01.color[1][1][i] = 0.5 * (patch->color[0][1][i] +
1219
3.37M
              patch->color[1][1][i]);
1220
3.37M
      patch11.color[0][1][i] = patch01.color[1][1][i];
1221
3.37M
      patch11.color[1][1][i] = patch->color[1][1][i];
1222
3.37M
      patch11.color[1][0][i] = 0.5 * (patch->color[1][1][i] +
1223
3.37M
              patch->color[1][0][i]);
1224
3.37M
      patch10.color[1][1][i] = patch11.color[1][0][i];
1225
3.37M
      patch10.color[1][0][i] = patch->color[1][0][i];
1226
3.37M
      patch10.color[0][0][i] = 0.5 * (patch->color[1][0][i] +
1227
3.37M
              patch->color[0][0][i]);
1228
3.37M
      patch00.color[1][0][i] = patch10.color[0][0][i];
1229
3.37M
      patch00.color[1][1][i] = 0.5 * (patch00.color[1][0][i] +
1230
3.37M
              patch01.color[1][1][i]);
1231
3.37M
      patch01.color[1][0][i] = patch00.color[1][1][i];
1232
3.37M
      patch11.color[0][0][i] = patch00.color[1][1][i];
1233
3.37M
      patch10.color[0][1][i] = patch00.color[1][1][i];
1234
3.37M
    }
1235
842k
    fillPatch(state, splash, mode, reverseVideo, xMin, yMin, &patch00,
1236
842k
        shading, depth + 1);
1237
842k
    fillPatch(state, splash, mode, reverseVideo, xMin, yMin, &patch10,
1238
842k
        shading, depth + 1);
1239
842k
    fillPatch(state, splash, mode, reverseVideo, xMin, yMin, &patch01,
1240
842k
        shading, depth + 1);
1241
842k
    fillPatch(state, splash, mode, reverseVideo, xMin, yMin, &patch11,
1242
842k
        shading, depth + 1);
1243
842k
  }
1244
3.37M
}
1245
1246
void ShadingImage::computeShadingColor(GfxState *state,
1247
               SplashColorMode mode,
1248
               GBool reverseVideo,
1249
               GfxColor *color,
1250
2.68M
               SplashColorPtr sColor) {
1251
2.68M
  GfxGray gray;
1252
2.68M
  GfxRGB rgb;
1253
2.68M
#if SPLASH_CMYK
1254
2.68M
  GfxCMYK cmyk;
1255
2.68M
#endif
1256
1257
2.68M
  state->setFillColor(color);
1258
2.68M
  switch (mode) {
1259
5
  case splashModeMono8:
1260
5
    state->getFillGray(&gray);
1261
5
    if (reverseVideo) {
1262
0
      gray = gfxColorComp1 - gray;
1263
0
    }
1264
5
    sColor[0] = colToByte(gray);
1265
5
    break;
1266
2.68M
  case splashModeRGB8:
1267
2.68M
    state->getFillRGB(&rgb);
1268
2.68M
    if (reverseVideo) {
1269
0
      rgb.r = gfxColorComp1 - rgb.r;
1270
0
      rgb.g = gfxColorComp1 - rgb.g;
1271
0
      rgb.b = gfxColorComp1 - rgb.b;
1272
0
    }
1273
2.68M
    sColor[0] = colToByte(rgb.r);
1274
2.68M
    sColor[1] = colToByte(rgb.g);
1275
2.68M
    sColor[2] = colToByte(rgb.b);
1276
2.68M
    break;
1277
0
#if SPLASH_CMYK
1278
0
  case splashModeCMYK8:
1279
0
    state->getFillCMYK(&cmyk);
1280
0
    sColor[0] = colToByte(cmyk.c);
1281
0
    sColor[1] = colToByte(cmyk.m);
1282
0
    sColor[2] = colToByte(cmyk.y);
1283
0
    sColor[3] = colToByte(cmyk.k);
1284
0
    break;
1285
0
#endif
1286
0
  case splashModeMono1:
1287
0
  case splashModeBGR8:
1288
    // mode cannot be Mono1 or BGR8
1289
0
    break;
1290
2.68M
  }
1291
2.68M
}
1292
1293
// Transform a user space bbox to a device space bbox.
1294
void ShadingImage::transformBBox(GfxState *state,
1295
         double uxMin, double uyMin,
1296
         double uxMax, double uyMax,
1297
         double *dxMin, double *dyMin,
1298
12
         double *dxMax, double *dyMax) {
1299
12
  double tx, ty;
1300
12
  state->transform(uxMin, uyMin, &tx, &ty);
1301
12
  *dxMin = *dxMax = tx;
1302
12
  *dyMin = *dyMax = ty;
1303
12
  state->transform(uxMin, uyMax, &tx, &ty);
1304
12
  if (tx < *dxMin) {
1305
1
    *dxMin = tx;
1306
11
  } else if (tx > *dxMax) {
1307
0
    *dxMax = tx;
1308
0
  }
1309
12
  if (ty < *dyMin) {
1310
6
    *dyMin = ty;
1311
6
  } else if (ty > *dyMax) {
1312
1
    *dyMax = ty;
1313
1
  }
1314
12
  state->transform(uxMax, uyMin, &tx, &ty);
1315
12
  if (tx < *dxMin) {
1316
0
    *dxMin = tx;
1317
12
  } else if (tx > *dxMax) {
1318
9
    *dxMax = tx;
1319
9
  }
1320
12
  if (ty < *dyMin) {
1321
0
    *dyMin = ty;
1322
12
  } else if (ty > *dyMax) {
1323
0
    *dyMax = ty;
1324
0
  }
1325
12
  state->transform(uxMax, uyMax, &tx, &ty);
1326
12
  if (tx < *dxMin) {
1327
0
    *dxMin = tx;
1328
12
  } else if (tx > *dxMax) {
1329
0
    *dxMax = tx;
1330
0
  }
1331
12
  if (ty < *dyMin) {
1332
0
    *dyMin = ty;
1333
12
  } else if (ty > *dyMax) {
1334
0
    *dyMax = ty;
1335
0
  }
1336
12
}
1337