Coverage Report

Created: 2026-10-17 07:11

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