1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../../include/fpdfapi/fpdf_render.h"
8 #include "../../../include/fpdfapi/fpdf_pageobj.h"
9 #include "../../../include/fxge/fx_ge.h"
10 #include "../fpdf_page/pageint.h"
11 #include "render_int.h"
12 #define SHADING_STEPS 256
13 static void _DrawAxialShading(CFX_DIBitmap* pBitmap, CFX_AffineMatrix* pObject2Bitmap,
14 CPDF_Dictionary* pDict, CPDF_Function** pFuncs, int nFuncs,
15 CPDF_ColorSpace* pCS, int alpha)
17 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
18 CPDF_Array* pCoords = pDict->GetArray(FX_BSTRC("Coords"));
19 if (pCoords == NULL) {
22 FX_FLOAT start_x = pCoords->GetNumber(0);
23 FX_FLOAT start_y = pCoords->GetNumber(1);
24 FX_FLOAT end_x = pCoords->GetNumber(2);
25 FX_FLOAT end_y = pCoords->GetNumber(3);
26 FX_FLOAT t_min = 0, t_max = 1.0f;
27 CPDF_Array* pArray = pDict->GetArray(FX_BSTRC("Domain"));
29 t_min = pArray->GetNumber(0);
30 t_max = pArray->GetNumber(1);
32 FX_BOOL bStartExtend = FALSE, bEndExtend = FALSE;
33 pArray = pDict->GetArray(FX_BSTRC("Extend"));
35 bStartExtend = pArray->GetInteger(0);
36 bEndExtend = pArray->GetInteger(1);
38 int width = pBitmap->GetWidth();
39 int height = pBitmap->GetHeight();
40 FX_FLOAT x_span = end_x - start_x;
41 FX_FLOAT y_span = end_y - start_y;
42 FX_FLOAT axis_len_square = FXSYS_Mul(x_span, x_span) + FXSYS_Mul(y_span, y_span);
43 CFX_AffineMatrix matrix;
44 matrix.SetReverse(*pObject2Bitmap);
45 int total_results = 0;
46 for (int j = 0; j < nFuncs; j ++) {
48 total_results += pFuncs[j]->CountOutputs();
51 if (pCS->CountComponents() > total_results) {
52 total_results = pCS->CountComponents();
54 CFX_FixedBufGrow<FX_FLOAT, 16> result_array(total_results);
55 FX_FLOAT* pResults = result_array;
56 FXSYS_memset32(pResults, 0, total_results * sizeof(FX_FLOAT));
57 FX_DWORD rgb_array[SHADING_STEPS];
58 for (int i = 0; i < SHADING_STEPS; i ++) {
59 FX_FLOAT input = (t_max - t_min) * i / SHADING_STEPS + t_min;
61 for (int j = 0; j < nFuncs; j ++) {
64 if (pFuncs[j]->Call(&input, 1, pResults + offset, nresults)) {
69 FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
70 pCS->GetRGB(pResults, R, G, B);
71 rgb_array[i] = FXARGB_TODIB(FXARGB_MAKE(alpha, FXSYS_round(R * 255), FXSYS_round(G * 255), FXSYS_round(B * 255)));
73 int pitch = pBitmap->GetPitch();
74 for (int row = 0; row < height; row ++) {
75 FX_DWORD* dib_buf = (FX_DWORD*)(pBitmap->GetBuffer() + row * pitch);
76 for (int column = 0; column < width; column ++) {
77 FX_FLOAT x = (FX_FLOAT)column, y = (FX_FLOAT)row;
78 matrix.Transform(x, y);
79 FX_FLOAT scale = FXSYS_Div(FXSYS_Mul(x - start_x, x_span) + FXSYS_Mul(y - start_y, y_span), axis_len_square);
80 int index = (FX_INT32)(scale * (SHADING_STEPS - 1));
86 } else if (index >= SHADING_STEPS) {
90 index = SHADING_STEPS - 1;
92 dib_buf[column] = rgb_array[index];
96 static void _DrawRadialShading(CFX_DIBitmap* pBitmap, CFX_AffineMatrix* pObject2Bitmap,
97 CPDF_Dictionary* pDict, CPDF_Function** pFuncs, int nFuncs,
98 CPDF_ColorSpace* pCS, int alpha)
100 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
101 CPDF_Array* pCoords = pDict->GetArray(FX_BSTRC("Coords"));
102 if (pCoords == NULL) {
105 FX_FLOAT start_x = pCoords->GetNumber(0);
106 FX_FLOAT start_y = pCoords->GetNumber(1);
107 FX_FLOAT start_r = pCoords->GetNumber(2);
108 FX_FLOAT end_x = pCoords->GetNumber(3);
109 FX_FLOAT end_y = pCoords->GetNumber(4);
110 FX_FLOAT end_r = pCoords->GetNumber(5);
111 CFX_AffineMatrix matrix;
112 matrix.SetReverse(*pObject2Bitmap);
113 FX_FLOAT t_min = 0, t_max = 1.0f;
114 CPDF_Array* pArray = pDict->GetArray(FX_BSTRC("Domain"));
116 t_min = pArray->GetNumber(0);
117 t_max = pArray->GetNumber(1);
119 FX_BOOL bStartExtend = FALSE, bEndExtend = FALSE;
120 pArray = pDict->GetArray(FX_BSTRC("Extend"));
122 bStartExtend = pArray->GetInteger(0);
123 bEndExtend = pArray->GetInteger(1);
125 int total_results = 0;
126 for (int j = 0; j < nFuncs; j ++) {
128 total_results += pFuncs[j]->CountOutputs();
131 if (pCS->CountComponents() > total_results) {
132 total_results = pCS->CountComponents();
134 CFX_FixedBufGrow<FX_FLOAT, 16> result_array(total_results);
135 FX_FLOAT* pResults = result_array;
136 FXSYS_memset32(pResults, 0, total_results * sizeof(FX_FLOAT));
137 FX_DWORD rgb_array[SHADING_STEPS];
138 for (int i = 0; i < SHADING_STEPS; i ++) {
139 FX_FLOAT input = (t_max - t_min) * i / SHADING_STEPS + t_min;
141 for (int j = 0; j < nFuncs; j ++) {
144 if (pFuncs[j]->Call(&input, 1, pResults + offset, nresults)) {
149 FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
150 pCS->GetRGB(pResults, R, G, B);
151 rgb_array[i] = FXARGB_TODIB(FXARGB_MAKE(alpha, FXSYS_round(R * 255), FXSYS_round(G * 255), FXSYS_round(B * 255)));
153 FX_FLOAT a = FXSYS_Mul(start_x - end_x, start_x - end_x) +
154 FXSYS_Mul(start_y - end_y, start_y - end_y) - FXSYS_Mul(start_r - end_r, start_r - end_r);
155 int width = pBitmap->GetWidth();
156 int height = pBitmap->GetHeight();
157 int pitch = pBitmap->GetPitch();
158 FX_BOOL bDecreasing = FALSE;
159 if (start_r > end_r) {
160 int length = (int)FXSYS_sqrt((FXSYS_Mul(start_x - end_x, start_x - end_x) + FXSYS_Mul(start_y - end_y, start_y - end_y)));
161 if (length < start_r - end_r) {
165 for (int row = 0; row < height; row ++) {
166 FX_DWORD* dib_buf = (FX_DWORD*)(pBitmap->GetBuffer() + row * pitch);
167 for (int column = 0; column < width; column ++) {
168 FX_FLOAT x = (FX_FLOAT)column, y = (FX_FLOAT)row;
169 matrix.Transform(x, y);
170 FX_FLOAT b = -2 * (FXSYS_Mul(x - start_x, end_x - start_x) + FXSYS_Mul(y - start_y, end_y - start_y) +
171 FXSYS_Mul(start_r, end_r - start_r));
172 FX_FLOAT c = FXSYS_Mul(x - start_x, x - start_x) + FXSYS_Mul(y - start_y, y - start_y) -
173 FXSYS_Mul(start_r, start_r);
176 s = FXSYS_Div(-c, b);
178 FX_FLOAT b2_4ac = FXSYS_Mul(b, b) - 4 * FXSYS_Mul(a, c);
182 FX_FLOAT root = FXSYS_sqrt(b2_4ac);
185 s1 = FXSYS_Div(-b - root, 2 * a);
186 s2 = FXSYS_Div(-b + root, 2 * a);
188 s2 = FXSYS_Div(-b - root, 2 * a);
189 s1 = FXSYS_Div(-b + root, 2 * a);
192 if (s1 >= 0 || bStartExtend) {
198 if (s2 <= 1.0f || bEndExtend) {
204 if ((start_r + s * (end_r - start_r)) < 0) {
208 int index = (FX_INT32)(s * (SHADING_STEPS - 1));
215 if (index >= SHADING_STEPS) {
219 index = SHADING_STEPS - 1;
221 dib_buf[column] = rgb_array[index];
225 static void _DrawFuncShading(CFX_DIBitmap* pBitmap, CFX_AffineMatrix* pObject2Bitmap,
226 CPDF_Dictionary* pDict, CPDF_Function** pFuncs, int nFuncs,
227 CPDF_ColorSpace* pCS, int alpha)
229 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
230 CPDF_Array* pDomain = pDict->GetArray(FX_BSTRC("Domain"));
231 FX_FLOAT xmin = 0, ymin = 0, xmax = 1.0f, ymax = 1.0f;
233 xmin = pDomain->GetNumber(0);
234 xmax = pDomain->GetNumber(1);
235 ymin = pDomain->GetNumber(2);
236 ymax = pDomain->GetNumber(3);
238 CFX_AffineMatrix mtDomain2Target = pDict->GetMatrix(FX_BSTRC("Matrix"));
239 CFX_AffineMatrix matrix, reverse_matrix;
240 matrix.SetReverse(*pObject2Bitmap);
241 reverse_matrix.SetReverse(mtDomain2Target);
242 matrix.Concat(reverse_matrix);
243 int width = pBitmap->GetWidth();
244 int height = pBitmap->GetHeight();
245 int pitch = pBitmap->GetPitch();
246 int total_results = 0;
247 for (int j = 0; j < nFuncs; j ++) {
249 total_results += pFuncs[j]->CountOutputs();
252 if (pCS->CountComponents() > total_results) {
253 total_results = pCS->CountComponents();
255 CFX_FixedBufGrow<FX_FLOAT, 16> result_array(total_results);
256 FX_FLOAT* pResults = result_array;
257 FXSYS_memset32(pResults, 0, total_results * sizeof(FX_FLOAT));
258 for (int row = 0; row < height; row ++) {
259 FX_DWORD* dib_buf = (FX_DWORD*)(pBitmap->GetBuffer() + row * pitch);
260 for (int column = 0; column < width; column ++) {
261 FX_FLOAT x = (FX_FLOAT)column, y = (FX_FLOAT)row;
262 matrix.Transform(x, y);
263 if (x < xmin || x > xmax || y < ymin || y > ymax) {
270 for (int j = 0; j < nFuncs; j ++) {
273 if (pFuncs[j]->Call(input, 2, pResults + offset, nresults)) {
278 FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
279 pCS->GetRGB(pResults, R, G, B);
280 dib_buf[column] = FXARGB_TODIB(FXARGB_MAKE(alpha, (FX_INT32)(R * 255), (FX_INT32)(G * 255), (FX_INT32)(B * 255)));
284 FX_BOOL _GetScanlineIntersect(int y, FX_FLOAT x1, FX_FLOAT y1, FX_FLOAT x2, FX_FLOAT y2, FX_FLOAT& x)
290 if (y < y1 || y > y2) {
294 if (y < y2 || y > y1) {
298 x = x1 + FXSYS_MulDiv(x2 - x1, y - y1, y2 - y1);
301 static void _DrawGouraud(CFX_DIBitmap* pBitmap, int alpha, CPDF_MeshVertex triangle[3])
303 FX_FLOAT min_y = triangle[0].y, max_y = triangle[0].y;
304 for (int i = 1; i < 3; i ++) {
305 if (min_y > triangle[i].y) {
306 min_y = triangle[i].y;
308 if (max_y < triangle[i].y) {
309 max_y = triangle[i].y;
312 if (min_y == max_y) {
315 int min_yi = (int)FXSYS_floor(min_y), max_yi = (int)FXSYS_ceil(max_y);
319 if (max_yi >= pBitmap->GetHeight()) {
320 max_yi = pBitmap->GetHeight() - 1;
322 for (int y = min_yi; y <= max_yi; y ++) {
324 FX_FLOAT inter_x[3], r[3], g[3], b[3];
325 for (int i = 0; i < 3; i ++) {
326 CPDF_MeshVertex& vertex1 = triangle[i];
327 CPDF_MeshVertex& vertex2 = triangle[(i + 1) % 3];
328 FX_BOOL bIntersect = _GetScanlineIntersect(y, vertex1.x, vertex1.y,
329 vertex2.x, vertex2.y, inter_x[nIntersects]);
333 r[nIntersects] = vertex1.r + FXSYS_MulDiv(vertex2.r - vertex1.r, y - vertex1.y, vertex2.y - vertex1.y);
334 g[nIntersects] = vertex1.g + FXSYS_MulDiv(vertex2.g - vertex1.g, y - vertex1.y, vertex2.y - vertex1.y);
335 b[nIntersects] = vertex1.b + FXSYS_MulDiv(vertex2.b - vertex1.b, y - vertex1.y, vertex2.y - vertex1.y);
338 if (nIntersects != 2) {
341 int min_x, max_x, start_index, end_index;
342 if (inter_x[0] < inter_x[1]) {
343 min_x = (int)FXSYS_floor(inter_x[0]);
344 max_x = (int)FXSYS_ceil(inter_x[1]);
348 min_x = (int)FXSYS_floor(inter_x[1]);
349 max_x = (int)FXSYS_ceil(inter_x[0]);
353 int start_x = min_x, end_x = max_x;
357 if (end_x > pBitmap->GetWidth()) {
358 end_x = pBitmap->GetWidth();
360 FX_LPBYTE dib_buf = pBitmap->GetBuffer() + y * pBitmap->GetPitch() + start_x * 4;
361 FX_FLOAT r_unit = (r[end_index] - r[start_index]) / (max_x - min_x);
362 FX_FLOAT g_unit = (g[end_index] - g[start_index]) / (max_x - min_x);
363 FX_FLOAT b_unit = (b[end_index] - b[start_index]) / (max_x - min_x);
364 FX_FLOAT R = r[start_index] + (start_x - min_x) * r_unit;
365 FX_FLOAT G = g[start_index] + (start_x - min_x) * g_unit;
366 FX_FLOAT B = b[start_index] + (start_x - min_x) * b_unit;
367 for (int x = start_x; x < end_x; x ++) {
371 FXARGB_SETDIB(dib_buf, FXARGB_MAKE(alpha, (FX_INT32)(R * 255), (FX_INT32)(G * 255), (FX_INT32)(B * 255)));
376 static void _DrawFreeGouraudShading(CFX_DIBitmap* pBitmap, CFX_AffineMatrix* pObject2Bitmap,
377 CPDF_Stream* pShadingStream, CPDF_Function** pFuncs, int nFuncs,
378 CPDF_ColorSpace* pCS, int alpha)
380 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
381 if (pShadingStream->GetType() != PDFOBJ_STREAM) {
384 CPDF_MeshStream stream;
385 if (!stream.Load(pShadingStream, pFuncs, nFuncs, pCS)) {
388 CPDF_MeshVertex triangle[3];
389 FXSYS_memset32(triangle, 0, sizeof(triangle));
391 while (!stream.m_BitStream.IsEOF()) {
392 CPDF_MeshVertex vertex;
393 FX_DWORD flag = stream.GetVertex(vertex, pObject2Bitmap);
395 triangle[0] = vertex;
396 for (int j = 1; j < 3; j ++) {
397 stream.GetVertex(triangle[j], pObject2Bitmap);
401 triangle[0] = triangle[1];
403 triangle[1] = triangle[2];
404 triangle[2] = vertex;
406 _DrawGouraud(pBitmap, alpha, triangle);
409 static void _DrawLatticeGouraudShading(CFX_DIBitmap* pBitmap, CFX_AffineMatrix* pObject2Bitmap,
410 CPDF_Stream* pShadingStream, CPDF_Function** pFuncs, int nFuncs,
411 CPDF_ColorSpace* pCS, int alpha)
413 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
414 if (pShadingStream->GetType() != PDFOBJ_STREAM) {
417 int row_verts = pShadingStream->GetDict()->GetInteger("VerticesPerRow");
421 CPDF_MeshStream stream;
422 if (!stream.Load(pShadingStream, pFuncs, nFuncs, pCS)) {
425 CPDF_MeshVertex* vertex = FX_Alloc(CPDF_MeshVertex, row_verts * 2);
426 if (!stream.GetVertexRow(vertex, row_verts, pObject2Bitmap)) {
432 CPDF_MeshVertex* last_row = vertex + last_index * row_verts;
433 CPDF_MeshVertex* this_row = vertex + (1 - last_index) * row_verts;
434 if (!stream.GetVertexRow(this_row, row_verts, pObject2Bitmap)) {
438 CPDF_MeshVertex triangle[3];
439 for (int i = 1; i < row_verts; i ++) {
440 triangle[0] = last_row[i];
441 triangle[1] = this_row[i - 1];
442 triangle[2] = last_row[i - 1];
443 _DrawGouraud(pBitmap, alpha, triangle);
444 triangle[2] = this_row[i];
445 _DrawGouraud(pBitmap, alpha, triangle);
447 last_index = 1 - last_index;
451 struct Coon_BezierCoeff {
453 void FromPoints(float p0, float p1, float p2, float p3)
455 a = -p0 + 3 * p1 - 3 * p2 + p3;
456 b = 3 * p0 - 6 * p1 + 3 * p2;
457 c = -3 * p0 + 3 * p1;
460 Coon_BezierCoeff first_half()
462 Coon_BezierCoeff result;
469 Coon_BezierCoeff second_half()
471 Coon_BezierCoeff result;
473 result.b = 3 * a / 8 + b / 4;
474 result.c = 3 * a / 8 + b / 2 + c / 2;
475 result.d = a / 8 + b / 4 + c / 2 + d;
478 void GetPoints(float p[4])
482 p[2] = b / 3 - p[0] + 2 * p[1];
483 p[3] = a + p[0] - 3 * p[1] + 3 * p[2];
485 void GetPointsReverse(float p[4])
489 p[1] = b / 3 - p[3] + 2 * p[2];
490 p[0] = a + p[3] - 3 * p[2] + 3 * p[1];
492 void BezierInterpol(Coon_BezierCoeff& C1, Coon_BezierCoeff& C2, Coon_BezierCoeff& D1, Coon_BezierCoeff& D2)
494 a = (D1.a + D2.a) / 2;
495 b = (D1.b + D2.b) / 2;
496 c = (D1.c + D2.c) / 2 - (C1.a / 8 + C1.b / 4 + C1.c / 2) + (C2.a / 8 + C2.b / 4) + (-C1.d + D2.d) / 2 - (C2.a + C2.b) / 2;
497 d = C1.a / 8 + C1.b / 4 + C1.c / 2 + C1.d;
501 float dis = a + b + c;
502 return dis < 0 ? -dis : dis;
506 Coon_BezierCoeff x, y;
507 void FromPoints(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3)
509 x.FromPoints(x0, x1, x2, x3);
510 y.FromPoints(y0, y1, y2, y3);
512 Coon_Bezier first_half()
515 result.x = x.first_half();
516 result.y = y.first_half();
519 Coon_Bezier second_half()
522 result.x = x.second_half();
523 result.y = y.second_half();
526 void BezierInterpol(Coon_Bezier& C1, Coon_Bezier& C2, Coon_Bezier& D1, Coon_Bezier& D2)
528 x.BezierInterpol(C1.x, C2.x, D1.x, D2.x);
529 y.BezierInterpol(C1.y, C2.y, D1.y, D2.y);
531 void GetPoints(FX_PATHPOINT* pPoints)
536 for (i = 0; i < 4; i ++) {
537 pPoints[i].m_PointX = p[i];
540 for (i = 0; i < 4; i ++) {
541 pPoints[i].m_PointY = p[i];
544 void GetPointsReverse(FX_PATHPOINT* pPoints)
548 x.GetPointsReverse(p);
549 for (i = 0; i < 4; i ++) {
550 pPoints[i].m_PointX = p[i];
552 y.GetPointsReverse(p);
553 for (i = 0; i < 4; i ++) {
554 pPoints[i].m_PointY = p[i];
559 return x.Distance() + y.Distance();
562 static int _BiInterpol(int c0, int c1, int c2, int c3, int x, int y, int x_scale, int y_scale)
564 int x1 = c0 + (c3 - c0) * x / x_scale;
565 int x2 = c1 + (c2 - c1) * x / x_scale;
566 return x1 + (x2 - x1) * y / y_scale;
571 FXSYS_memset32(comp, 0, sizeof(int) * 3);
574 void BiInterpol(Coon_Color colors[4], int x, int y, int x_scale, int y_scale)
576 for (int i = 0; i < 3; i ++)
577 comp[i] = _BiInterpol(colors[0].comp[i], colors[1].comp[i], colors[2].comp[i], colors[3].comp[i],
578 x, y, x_scale, y_scale);
580 int Distance(Coon_Color& o)
583 max = FXSYS_abs(comp[0] - o.comp[0]);
584 diff = FXSYS_abs(comp[1] - o.comp[1]);
588 diff = FXSYS_abs(comp[2] - o.comp[2]);
595 struct CPDF_PatchDrawer {
596 Coon_Color patch_colors[4];
599 CFX_RenderDevice* pDevice;
602 void Draw(int x_scale, int y_scale, int left, int bottom, Coon_Bezier C1, Coon_Bezier C2, Coon_Bezier D1, Coon_Bezier D2)
604 FX_BOOL bSmall = C1.Distance() < 2 && C2.Distance() < 2 && D1.Distance() < 2 && D2.Distance() < 2;
605 Coon_Color div_colors[4];
606 int d_bottom, d_left, d_top, d_right;
607 div_colors[0].BiInterpol(patch_colors, left, bottom, x_scale, y_scale);
609 div_colors[1].BiInterpol(patch_colors, left, bottom + 1, x_scale, y_scale);
610 div_colors[2].BiInterpol(patch_colors, left + 1, bottom + 1, x_scale, y_scale);
611 div_colors[3].BiInterpol(patch_colors, left + 1, bottom, x_scale, y_scale);
612 d_bottom = div_colors[3].Distance(div_colors[0]);
613 d_left = div_colors[1].Distance(div_colors[0]);
614 d_top = div_colors[1].Distance(div_colors[2]);
615 d_right = div_colors[2].Distance(div_colors[3]);
617 #define COONCOLOR_THRESHOLD 4
618 if (bSmall || (d_bottom < COONCOLOR_THRESHOLD && d_left < COONCOLOR_THRESHOLD &&
619 d_top < COONCOLOR_THRESHOLD && d_right < COONCOLOR_THRESHOLD)) {
620 FX_PATHPOINT* pPoints = path.GetPoints();
621 C1.GetPoints(pPoints);
622 D2.GetPoints(pPoints + 3);
623 C2.GetPointsReverse(pPoints + 6);
624 D1.GetPointsReverse(pPoints + 9);
625 int fillFlags = FXFILL_WINDING | FXFILL_FULLCOVER;
626 if (fill_mode & RENDER_NOPATHSMOOTH) {
627 fillFlags |= FXFILL_NOPATHSMOOTH;
629 pDevice->DrawPath(&path, NULL, NULL, FXARGB_MAKE(alpha, div_colors[0].comp[0], div_colors[0].comp[1], div_colors[0].comp[2]), 0, fillFlags);
631 if (d_bottom < COONCOLOR_THRESHOLD && d_top < COONCOLOR_THRESHOLD) {
633 m1.BezierInterpol(D1, D2, C1, C2);
636 Draw(x_scale, y_scale, left, bottom, C1, m1, D1.first_half(), D2.first_half());
637 Draw(x_scale, y_scale, left, bottom + 1, m1, C2, D1.second_half(), D2.second_half());
638 } else if (d_left < COONCOLOR_THRESHOLD && d_right < COONCOLOR_THRESHOLD) {
640 m2.BezierInterpol(C1, C2, D1, D2);
643 Draw(x_scale, y_scale, left, bottom, C1.first_half(), C2.first_half(), D1, m2);
644 Draw(x_scale, y_scale, left + 1, bottom, C1.second_half(), C2.second_half(), m2, D2);
647 m1.BezierInterpol(D1, D2, C1, C2);
648 m2.BezierInterpol(C1, C2, D1, D2);
649 Coon_Bezier m1f = m1.first_half();
650 Coon_Bezier m1s = m1.second_half();
651 Coon_Bezier m2f = m2.first_half();
652 Coon_Bezier m2s = m2.second_half();
657 Draw(x_scale, y_scale, left, bottom, C1.first_half(), m1f, D1.first_half(), m2f);
658 Draw(x_scale, y_scale, left, bottom + 1, m1f, C2.first_half(), D1.second_half(), m2s);
659 Draw(x_scale, y_scale, left + 1, bottom, C1.second_half(), m1s, m2f, D2.first_half());
660 Draw(x_scale, y_scale, left + 1, bottom + 1, m1s, C2.second_half(), m2s, D2.second_half());
666 FX_BOOL _CheckCoonTensorPara(const CPDF_MeshStream &stream)
668 FX_BOOL bCoorBits = ( stream.m_nCoordBits== 1 ||
669 stream.m_nCoordBits == 2 ||
670 stream.m_nCoordBits == 4 ||
671 stream.m_nCoordBits == 8 ||
672 stream.m_nCoordBits == 12 ||
673 stream.m_nCoordBits == 16 ||
674 stream.m_nCoordBits == 24 ||
675 stream.m_nCoordBits == 32 );
677 FX_BOOL bCompBits = ( stream.m_nCompBits == 1 ||
678 stream.m_nCompBits == 2 ||
679 stream.m_nCompBits == 4 ||
680 stream.m_nCompBits == 8 ||
681 stream.m_nCompBits == 12 ||
682 stream.m_nCompBits == 16 );
684 FX_BOOL bFlagBits = ( stream.m_nFlagBits == 2 ||
685 stream.m_nFlagBits == 4 ||
686 stream.m_nFlagBits == 8 );
688 return bCoorBits && bCompBits && bFlagBits;
691 static void _DrawCoonPatchMeshes(FX_BOOL bTensor, CFX_DIBitmap* pBitmap, CFX_AffineMatrix* pObject2Bitmap,
692 CPDF_Stream* pShadingStream, CPDF_Function** pFuncs, int nFuncs,
693 CPDF_ColorSpace* pCS, int fill_mode, int alpha)
695 ASSERT(pBitmap->GetFormat() == FXDIB_Argb);
696 if (pShadingStream->GetType() != PDFOBJ_STREAM) {
699 CFX_FxgeDevice device;
700 device.Attach(pBitmap);
701 CPDF_MeshStream stream;
702 if (!stream.Load(pShadingStream, pFuncs, nFuncs, pCS)) {
706 if (!_CheckCoonTensorPara(stream)) {
710 CPDF_PatchDrawer patch;
712 patch.pDevice = &device;
713 patch.fill_mode = fill_mode;
714 patch.path.SetPointCount(13);
715 FX_PATHPOINT* pPoints = patch.path.GetPoints();
716 pPoints[0].m_Flag = FXPT_MOVETO;
717 for (int i = 1; i < 13; i ++) {
718 pPoints[i].m_Flag = FXPT_BEZIERTO;
720 CFX_FloatPoint coords[16];
721 for (int i = 0; i < 16; i ++) {
722 coords[i].Set(0.0f, 0.0f);
725 int point_count = bTensor ? 16 : 12;
726 while (!stream.m_BitStream.IsEOF()) {
727 FX_DWORD flag = stream.GetFlag();
728 int iStartPoint = 0, iStartColor = 0, i = 0;
732 CFX_FloatPoint tempCoords[4];
733 for (i = 0; i < 4; i ++) {
734 tempCoords[i] = coords[(flag * 3 + i) % 12];
736 FXSYS_memcpy32(coords, tempCoords, sizeof(CFX_FloatPoint) * 4);
737 Coon_Color tempColors[2];
738 tempColors[0] = patch.patch_colors[flag];
739 tempColors[1] = patch.patch_colors[(flag + 1) % 4];
740 FXSYS_memcpy32(patch.patch_colors, tempColors, sizeof(Coon_Color) * 2);
742 for (i = iStartPoint; i < point_count; i ++) {
743 stream.GetCoords(coords[i].x, coords[i].y);
744 pObject2Bitmap->Transform(coords[i].x, coords[i].y);
746 for (i = iStartColor; i < 4; i ++) {
747 FX_FLOAT r=0.0f, g=0.0f, b=0.0f;
748 stream.GetColor(r, g, b);
749 patch.patch_colors[i].comp[0] = (FX_INT32)(r * 255);
750 patch.patch_colors[i].comp[1] = (FX_INT32)(g * 255);
751 patch.patch_colors[i].comp[2] = (FX_INT32)(b * 255);
753 CFX_FloatRect bbox = CFX_FloatRect::GetBBox(coords, point_count);
754 if (bbox.right <= 0 || bbox.left >= (FX_FLOAT)pBitmap->GetWidth() || bbox.top <= 0 ||
755 bbox.bottom >= (FX_FLOAT)pBitmap->GetHeight()) {
758 Coon_Bezier C1, C2, D1, D2;
759 C1.FromPoints(coords[0].x, coords[0].y, coords[11].x, coords[11].y, coords[10].x, coords[10].y,
760 coords[9].x, coords[9].y);
761 C2.FromPoints(coords[3].x, coords[3].y, coords[4].x, coords[4].y, coords[5].x, coords[5].y,
762 coords[6].x, coords[6].y);
763 D1.FromPoints(coords[0].x, coords[0].y, coords[1].x, coords[1].y, coords[2].x, coords[2].y,
764 coords[3].x, coords[3].y);
765 D2.FromPoints(coords[9].x, coords[9].y, coords[8].x, coords[8].y, coords[7].x, coords[7].y,
766 coords[6].x, coords[6].y);
767 patch.Draw(1, 1, 0, 0, C1, C2, D1, D2);
770 void CPDF_RenderStatus::DrawShading(CPDF_ShadingPattern* pPattern, CFX_AffineMatrix* pMatrix,
771 FX_RECT& clip_rect, int alpha, FX_BOOL bAlphaMode)
773 CPDF_Function** pFuncs = pPattern->m_pFunctions;
774 int nFuncs = pPattern->m_nFuncs;
775 CPDF_Dictionary* pDict = pPattern->m_pShadingObj->GetDict();
776 CPDF_ColorSpace* pColorSpace = pPattern->m_pCS;
777 if (pColorSpace == NULL) {
780 FX_ARGB background = 0;
781 if (!pPattern->m_bShadingObj && pPattern->m_pShadingObj->GetDict()->KeyExist(FX_BSTRC("Background"))) {
782 CPDF_Array* pBackColor = pPattern->m_pShadingObj->GetDict()->GetArray(FX_BSTRC("Background"));
783 if (pBackColor && pBackColor->GetCount() >= (FX_DWORD)pColorSpace->CountComponents()) {
784 CFX_FixedBufGrow<FX_FLOAT, 16> comps(pColorSpace->CountComponents());
785 for (int i = 0; i < pColorSpace->CountComponents(); i ++) {
786 comps[i] = pBackColor->GetNumber(i);
788 FX_FLOAT R = 0.0f, G = 0.0f, B = 0.0f;
789 pColorSpace->GetRGB(comps, R, G, B);
790 background = ArgbEncode(255, (FX_INT32)(R * 255), (FX_INT32)(G * 255), (FX_INT32)(B * 255));
793 if (pDict->KeyExist(FX_BSTRC("BBox"))) {
794 CFX_FloatRect rect = pDict->GetRect(FX_BSTRC("BBox"));
795 rect.Transform(pMatrix);
796 clip_rect.Intersect(rect.GetOutterRect());
798 CPDF_DeviceBuffer buffer;
799 buffer.Initialize(m_pContext, m_pDevice, &clip_rect, m_pCurObj, 150);
800 CFX_AffineMatrix FinalMatrix = *pMatrix;
801 FinalMatrix.Concat(*buffer.GetMatrix());
802 CFX_DIBitmap* pBitmap = buffer.GetBitmap();
803 if (pBitmap->GetBuffer() == NULL) {
806 pBitmap->Clear(background);
807 int fill_mode = m_Options.m_Flags;
808 switch (pPattern->m_ShadingType) {
810 _DrawFuncShading(pBitmap, &FinalMatrix, pDict, pFuncs, nFuncs, pColorSpace, alpha);
813 _DrawAxialShading(pBitmap, &FinalMatrix, pDict, pFuncs, nFuncs, pColorSpace, alpha);
816 _DrawRadialShading(pBitmap, &FinalMatrix, pDict, pFuncs, nFuncs, pColorSpace, alpha);
819 _DrawFreeGouraudShading(pBitmap, &FinalMatrix, (CPDF_Stream*)pPattern->m_pShadingObj,
820 pFuncs, nFuncs, pColorSpace, alpha);
824 _DrawLatticeGouraudShading(pBitmap, &FinalMatrix, (CPDF_Stream*)pPattern->m_pShadingObj,
825 pFuncs, nFuncs, pColorSpace, alpha);
830 _DrawCoonPatchMeshes(pPattern->m_ShadingType - 6, pBitmap, &FinalMatrix, (CPDF_Stream*)pPattern->m_pShadingObj,
831 pFuncs, nFuncs, pColorSpace, fill_mode, alpha);
836 pBitmap->LoadChannel(FXDIB_Red, pBitmap, FXDIB_Alpha);
838 if (m_Options.m_ColorMode == RENDER_COLOR_GRAY) {
839 pBitmap->ConvertColorScale(m_Options.m_ForeColor, m_Options.m_BackColor);
841 buffer.OutputToDevice();
843 void CPDF_RenderStatus::DrawShadingPattern(CPDF_ShadingPattern* pattern, CPDF_PageObject* pPageObj, const CFX_AffineMatrix* pObj2Device, FX_BOOL bStroke)
845 if (!pattern->Load()) {
848 m_pDevice->SaveState();
849 if (pPageObj->m_Type == PDFPAGE_PATH) {
850 if (!SelectClipPath((CPDF_PathObject*)pPageObj, pObj2Device, bStroke)) {
851 m_pDevice->RestoreState();
854 } else if (pPageObj->m_Type == PDFPAGE_IMAGE) {
855 FX_RECT rect = pPageObj->GetBBox(pObj2Device);
856 m_pDevice->SetClip_Rect(&rect);
861 if (GetObjectClippedRect(pPageObj, pObj2Device, FALSE, rect)) {
862 m_pDevice->RestoreState();
865 CFX_AffineMatrix matrix = pattern->m_Pattern2Form;
866 matrix.Concat(*pObj2Device);
867 GetScaledMatrix(matrix);
868 int alpha = pPageObj->m_GeneralState.GetAlpha(bStroke);
869 DrawShading(pattern, &matrix, rect, alpha, m_Options.m_ColorMode == RENDER_COLOR_ALPHA);
870 m_pDevice->RestoreState();
872 FX_BOOL CPDF_RenderStatus::ProcessShading(CPDF_ShadingObject* pShadingObj, const CFX_AffineMatrix* pObj2Device)
874 FX_RECT rect = pShadingObj->GetBBox(pObj2Device);
875 FX_RECT clip_box = m_pDevice->GetClipBox();
876 rect.Intersect(clip_box);
877 if (rect.IsEmpty()) {
880 CFX_AffineMatrix matrix = pShadingObj->m_Matrix;
881 matrix.Concat(*pObj2Device);
882 DrawShading(pShadingObj->m_pShading, &matrix, rect, pShadingObj->m_GeneralState.GetAlpha(FALSE),
883 m_Options.m_ColorMode == RENDER_COLOR_ALPHA);
884 #ifdef _FPDFAPI_MINI_
886 DitherObjectArea(pShadingObj, pObj2Device);
891 static CFX_DIBitmap* DrawPatternBitmap(CPDF_Document* pDoc, CPDF_PageRenderCache* pCache,
892 CPDF_TilingPattern* pPattern, const CFX_AffineMatrix* pObject2Device,
893 int width, int height, int flags)
895 CFX_DIBitmap* pBitmap = FX_NEW CFX_DIBitmap;
896 if (!pBitmap->Create(width, height, pPattern->m_bColored ? FXDIB_Argb : FXDIB_8bppMask)) {
900 CFX_FxgeDevice bitmap_device;
901 bitmap_device.Attach(pBitmap);
903 CFX_FloatRect cell_bbox = pPattern->m_BBox;
904 pPattern->m_Pattern2Form.TransformRect(cell_bbox);
905 pObject2Device->TransformRect(cell_bbox);
906 CFX_FloatRect bitmap_rect(0.0f, 0.0f, (FX_FLOAT)width, (FX_FLOAT)height);
907 CFX_AffineMatrix mtAdjust;
908 mtAdjust.MatchRect(bitmap_rect, cell_bbox);
909 CFX_AffineMatrix mtPattern2Bitmap = *pObject2Device;
910 mtPattern2Bitmap.Concat(mtAdjust);
911 CPDF_RenderOptions options;
912 if (!pPattern->m_bColored) {
913 options.m_ColorMode = RENDER_COLOR_ALPHA;
915 flags |= RENDER_FORCE_HALFTONE;
916 options.m_Flags = flags;
917 CPDF_RenderContext context;
918 context.Create(pDoc, pCache, NULL);
919 context.DrawObjectList(&bitmap_device, pPattern->m_pForm, &mtPattern2Bitmap, &options);
922 void CPDF_RenderStatus::DrawTilingPattern(CPDF_TilingPattern* pPattern, CPDF_PageObject* pPageObj, const CFX_AffineMatrix* pObj2Device, FX_BOOL bStroke)
924 if (!pPattern->Load()) {
927 m_pDevice->SaveState();
928 if (pPageObj->m_Type == PDFPAGE_PATH) {
929 if (!SelectClipPath((CPDF_PathObject*)pPageObj, pObj2Device, bStroke)) {
930 m_pDevice->RestoreState();
933 } else if (pPageObj->m_Type == PDFPAGE_IMAGE) {
934 FX_RECT rect = pPageObj->GetBBox(pObj2Device);
935 m_pDevice->SetClip_Rect(&rect);
939 FX_RECT clip_box = m_pDevice->GetClipBox();
940 if (clip_box.IsEmpty()) {
941 m_pDevice->RestoreState();
944 CFX_Matrix dCTM = m_pDevice->GetCTM();
945 FX_FLOAT sa = FXSYS_fabs(dCTM.a);
946 FX_FLOAT sd = FXSYS_fabs(dCTM.d);
947 clip_box.right = clip_box.left + (FX_INT32)FXSYS_ceil(clip_box.Width() * sa);
948 clip_box.bottom = clip_box.top + (FX_INT32)FXSYS_ceil(clip_box.Height() * sd);
949 CFX_AffineMatrix mtPattern2Device = pPattern->m_Pattern2Form;
950 mtPattern2Device.Concat(*pObj2Device);
951 GetScaledMatrix(mtPattern2Device);
952 FX_BOOL bAligned = FALSE;
953 if (pPattern->m_BBox.left == 0 && pPattern->m_BBox.bottom == 0 &&
954 pPattern->m_BBox.right == pPattern->m_XStep && pPattern->m_BBox.top == pPattern->m_YStep &&
955 (mtPattern2Device.IsScaled() || mtPattern2Device.Is90Rotated())) {
958 CFX_FloatRect cell_bbox = pPattern->m_BBox;
959 mtPattern2Device.TransformRect(cell_bbox);
960 int width = (int)FXSYS_ceil(cell_bbox.Width());
961 int height = (int)FXSYS_ceil(cell_bbox.Height());
968 int min_col, max_col, min_row, max_row;
969 CFX_AffineMatrix mtDevice2Pattern;
970 mtDevice2Pattern.SetReverse(mtPattern2Device);
971 CFX_FloatRect clip_box_p(clip_box);
972 clip_box_p.Transform(&mtDevice2Pattern);
973 min_col = (int)FXSYS_ceil(FXSYS_Div(clip_box_p.left - pPattern->m_BBox.right, pPattern->m_XStep));
974 max_col = (int)FXSYS_floor(FXSYS_Div(clip_box_p.right - pPattern->m_BBox.left, pPattern->m_XStep));
975 min_row = (int)FXSYS_ceil(FXSYS_Div(clip_box_p.bottom - pPattern->m_BBox.top, pPattern->m_YStep));
976 max_row = (int)FXSYS_floor(FXSYS_Div(clip_box_p.top - pPattern->m_BBox.bottom, pPattern->m_YStep));
977 if (width > clip_box.Width() || height > clip_box.Height() || width * height > clip_box.Width() * clip_box.Height()) {
978 CPDF_GraphicStates* pStates = NULL;
979 if (!pPattern->m_bColored) {
980 pStates = CloneObjStates(pPageObj, bStroke);
982 CPDF_Dictionary* pFormResource = NULL;
983 if (pPattern->m_pForm->m_pFormDict) {
984 pFormResource = pPattern->m_pForm->m_pFormDict->GetDict(FX_BSTRC("Resources"));
986 for (int col = min_col; col <= max_col; col ++)
987 for (int row = min_row; row <= max_row; row ++) {
988 FX_FLOAT orig_x, orig_y;
989 orig_x = col * pPattern->m_XStep;
990 orig_y = row * pPattern->m_YStep;
991 mtPattern2Device.Transform(orig_x, orig_y);
992 CFX_AffineMatrix matrix = *pObj2Device;
993 matrix.Translate(orig_x - mtPattern2Device.e, orig_y - mtPattern2Device.f);
994 m_pDevice->SaveState();
995 CPDF_RenderStatus status;
996 status.Initialize(m_Level + 1, m_pContext, m_pDevice, NULL, NULL, this, pStates, &m_Options,
997 pPattern->m_pForm->m_Transparency, m_bDropObjects, pFormResource);
998 status.RenderObjectList(pPattern->m_pForm, &matrix);
999 m_pDevice->RestoreState();
1001 m_pDevice->RestoreState();
1008 int orig_x = FXSYS_round(mtPattern2Device.e);
1009 int orig_y = FXSYS_round(mtPattern2Device.f);
1010 min_col = (clip_box.left - orig_x) / width;
1011 if (clip_box.left < orig_x) {
1014 max_col = (clip_box.right - orig_x) / width;
1015 if (clip_box.right <= orig_x) {
1018 min_row = (clip_box.top - orig_y) / height;
1019 if (clip_box.top < orig_y) {
1022 max_row = (clip_box.bottom - orig_y) / height;
1023 if (clip_box.bottom <= orig_y) {
1027 FX_FLOAT left_offset = cell_bbox.left - mtPattern2Device.e;
1028 FX_FLOAT top_offset = cell_bbox.bottom - mtPattern2Device.f;
1029 CFX_DIBitmap* pPatternBitmap = NULL;
1030 if (width * height < 16) {
1031 CFX_DIBitmap* pEnlargedBitmap = DrawPatternBitmap(m_pContext->m_pDocument, m_pContext->m_pPageCache, pPattern, pObj2Device, 8, 8, m_Options.m_Flags);
1032 pPatternBitmap = pEnlargedBitmap->StretchTo(width, height);
1033 delete pEnlargedBitmap;
1035 pPatternBitmap = DrawPatternBitmap(m_pContext->m_pDocument, m_pContext->m_pPageCache, pPattern, pObj2Device, width, height, m_Options.m_Flags);
1037 if (pPatternBitmap == NULL) {
1038 m_pDevice->RestoreState();
1041 if (m_Options.m_ColorMode == RENDER_COLOR_GRAY) {
1042 pPatternBitmap->ConvertColorScale(m_Options.m_ForeColor, m_Options.m_BackColor);
1044 FX_ARGB fill_argb = GetFillArgb(pPageObj);
1045 int clip_width = clip_box.right - clip_box.left;
1046 int clip_height = clip_box.bottom - clip_box.top;
1047 CFX_DIBitmap screen;
1048 if (!screen.Create(clip_width, clip_height, FXDIB_Argb)) {
1052 FX_DWORD* src_buf = (FX_DWORD*)pPatternBitmap->GetBuffer();
1053 for (int col = min_col; col <= max_col; col ++) {
1054 for (int row = min_row; row <= max_row; row ++) {
1055 int start_x, start_y;
1057 start_x = FXSYS_round(mtPattern2Device.e) + col * width - clip_box.left;
1058 start_y = FXSYS_round(mtPattern2Device.f) + row * height - clip_box.top;
1060 FX_FLOAT orig_x = col * pPattern->m_XStep;
1061 FX_FLOAT orig_y = row * pPattern->m_YStep;
1062 mtPattern2Device.Transform(orig_x, orig_y);
1063 start_x = FXSYS_round(orig_x + left_offset) - clip_box.left;
1064 start_y = FXSYS_round(orig_y + top_offset) - clip_box.top;
1066 if (width == 1 && height == 1) {
1067 if (start_x < 0 || start_x >= clip_box.Width() || start_y < 0 || start_y >= clip_box.Height()) {
1070 FX_DWORD* dest_buf = (FX_DWORD*)(screen.GetBuffer() + screen.GetPitch() * start_y + start_x * 4);
1071 if (pPattern->m_bColored) {
1072 *dest_buf = *src_buf;
1074 *dest_buf = (*(FX_LPBYTE)src_buf << 24) | (fill_argb & 0xffffff);
1077 if (pPattern->m_bColored) {
1078 screen.CompositeBitmap(start_x, start_y, width, height, pPatternBitmap, 0, 0);
1080 screen.CompositeMask(start_x, start_y, width, height, pPatternBitmap, fill_argb, 0, 0);
1085 CompositeDIBitmap(&screen, clip_box.left, clip_box.top, 0, 255, FXDIB_BLEND_NORMAL, FALSE);
1086 m_pDevice->RestoreState();
1087 delete pPatternBitmap;
1089 void CPDF_RenderStatus::DrawPathWithPattern(CPDF_PathObject* pPathObj, const CFX_AffineMatrix* pObj2Device, CPDF_Color* pColor, FX_BOOL bStroke)
1091 CPDF_Pattern* pattern = pColor->GetPattern();
1092 if (pattern == NULL) {
1095 if(pattern->m_PatternType == PATTERN_TILING) {
1096 DrawTilingPattern((CPDF_TilingPattern*)pattern, pPathObj, pObj2Device, bStroke);
1098 DrawShadingPattern((CPDF_ShadingPattern*)pattern, pPathObj, pObj2Device, bStroke);
1101 void CPDF_RenderStatus::ProcessPathPattern(CPDF_PathObject* pPathObj, const CFX_AffineMatrix* pObj2Device, int& filltype, FX_BOOL& bStroke)
1103 FX_BOOL bPattern = FALSE;
1105 CPDF_Color& FillColor = *pPathObj->m_ColorState.GetFillColor();
1106 if(FillColor.m_pCS && FillColor.m_pCS->GetFamily() == PDFCS_PATTERN) {
1107 DrawPathWithPattern(pPathObj, pObj2Device, &FillColor, FALSE);
1113 CPDF_Color& StrokeColor = *pPathObj->m_ColorState.GetStrokeColor();
1114 if(StrokeColor.m_pCS && StrokeColor.m_pCS->GetFamily() == PDFCS_PATTERN) {
1115 DrawPathWithPattern(pPathObj, pObj2Device, &StrokeColor, TRUE);
1120 #ifdef _FPDFAPI_MINI_
1121 if (bPattern && m_DitherBits) {
1122 DitherObjectArea(pPathObj, pObj2Device);