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