Coverage Report

Created: 2026-04-04 06:06

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