OLD | NEW |
1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/fpdfapi/fpdf_page/cpdf_meshstream.h" | 7 #include "core/fpdfapi/fpdf_page/cpdf_meshstream.h" |
8 | 8 |
9 #include "core/fpdfapi/fpdf_page/include/cpdf_colorspace.h" | 9 #include "core/fpdfapi/fpdf_page/include/cpdf_colorspace.h" |
10 #include "core/fpdfapi/fpdf_page/pageint.h" | 10 #include "core/fpdfapi/fpdf_page/pageint.h" |
11 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | 11 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
12 | 12 |
13 FX_BOOL CPDF_MeshStream::Load(CPDF_Stream* pShadingStream, | 13 CPDF_MeshStream::CPDF_MeshStream( |
14 CPDF_Function** pFuncs, | 14 const std::vector<std::unique_ptr<CPDF_Function>>& funcs, |
15 int nFuncs, | 15 CPDF_ColorSpace* pCS) |
16 CPDF_ColorSpace* pCS) { | 16 : m_funcs(funcs), m_pCS(pCS) {} |
| 17 |
| 18 bool CPDF_MeshStream::Load(CPDF_Stream* pShadingStream) { |
17 m_Stream.LoadAllData(pShadingStream); | 19 m_Stream.LoadAllData(pShadingStream); |
18 m_BitStream.Init(m_Stream.GetData(), m_Stream.GetSize()); | 20 m_BitStream.Init(m_Stream.GetData(), m_Stream.GetSize()); |
19 m_pFuncs = pFuncs; | |
20 m_nFuncs = nFuncs; | |
21 m_pCS = pCS; | |
22 CPDF_Dictionary* pDict = pShadingStream->GetDict(); | 21 CPDF_Dictionary* pDict = pShadingStream->GetDict(); |
23 m_nCoordBits = pDict->GetIntegerBy("BitsPerCoordinate"); | 22 m_nCoordBits = pDict->GetIntegerBy("BitsPerCoordinate"); |
24 m_nCompBits = pDict->GetIntegerBy("BitsPerComponent"); | 23 m_nCompBits = pDict->GetIntegerBy("BitsPerComponent"); |
25 m_nFlagBits = pDict->GetIntegerBy("BitsPerFlag"); | 24 m_nFlagBits = pDict->GetIntegerBy("BitsPerFlag"); |
26 if (!m_nCoordBits || !m_nCompBits) | 25 if (!m_nCoordBits || !m_nCompBits) |
27 return FALSE; | 26 return FALSE; |
28 | 27 |
29 uint32_t nComps = pCS->CountComponents(); | 28 uint32_t nComps = m_pCS->CountComponents(); |
30 if (nComps > 8) | 29 if (nComps > 8) |
31 return FALSE; | 30 return FALSE; |
32 | 31 |
33 m_nComps = nFuncs ? 1 : nComps; | 32 m_nComps = m_funcs.empty() ? nComps : 1; |
34 if (((int)m_nComps < 0) || m_nComps > 8) | |
35 return FALSE; | |
36 | |
37 m_CoordMax = m_nCoordBits == 32 ? -1 : (1 << m_nCoordBits) - 1; | 33 m_CoordMax = m_nCoordBits == 32 ? -1 : (1 << m_nCoordBits) - 1; |
38 m_CompMax = (1 << m_nCompBits) - 1; | 34 m_CompMax = (1 << m_nCompBits) - 1; |
39 CPDF_Array* pDecode = pDict->GetArrayBy("Decode"); | 35 CPDF_Array* pDecode = pDict->GetArrayBy("Decode"); |
40 if (!pDecode || pDecode->GetCount() != 4 + m_nComps * 2) | 36 if (!pDecode || pDecode->GetCount() != 4 + m_nComps * 2) |
41 return FALSE; | 37 return FALSE; |
42 | 38 |
43 m_xmin = pDecode->GetNumberAt(0); | 39 m_xmin = pDecode->GetNumberAt(0); |
44 m_xmax = pDecode->GetNumberAt(1); | 40 m_xmax = pDecode->GetNumberAt(1); |
45 m_ymin = pDecode->GetNumberAt(2); | 41 m_ymin = pDecode->GetNumberAt(2); |
46 m_ymax = pDecode->GetNumberAt(3); | 42 m_ymax = pDecode->GetNumberAt(3); |
47 for (uint32_t i = 0; i < m_nComps; i++) { | 43 for (uint32_t i = 0; i < m_nComps; ++i) { |
48 m_ColorMin[i] = pDecode->GetNumberAt(i * 2 + 4); | 44 m_ColorMin[i] = pDecode->GetNumberAt(i * 2 + 4); |
49 m_ColorMax[i] = pDecode->GetNumberAt(i * 2 + 5); | 45 m_ColorMax[i] = pDecode->GetNumberAt(i * 2 + 5); |
50 } | 46 } |
51 return TRUE; | 47 return TRUE; |
52 } | 48 } |
53 | 49 |
54 uint32_t CPDF_MeshStream::GetFlag() { | 50 uint32_t CPDF_MeshStream::GetFlag() { |
55 return m_BitStream.GetBits(m_nFlagBits) & 0x03; | 51 return m_BitStream.GetBits(m_nFlagBits) & 0x03; |
56 } | 52 } |
57 | 53 |
58 void CPDF_MeshStream::GetCoords(FX_FLOAT& x, FX_FLOAT& y) { | 54 void CPDF_MeshStream::GetCoords(FX_FLOAT& x, FX_FLOAT& y) { |
59 if (m_nCoordBits == 32) { | 55 if (m_nCoordBits == 32) { |
60 x = m_xmin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * | 56 x = m_xmin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * |
61 (m_xmax - m_xmin) / (double)m_CoordMax); | 57 (m_xmax - m_xmin) / (double)m_CoordMax); |
62 y = m_ymin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * | 58 y = m_ymin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * |
63 (m_ymax - m_ymin) / (double)m_CoordMax); | 59 (m_ymax - m_ymin) / (double)m_CoordMax); |
64 } else { | 60 } else { |
65 x = m_xmin + | 61 x = m_xmin + |
66 m_BitStream.GetBits(m_nCoordBits) * (m_xmax - m_xmin) / m_CoordMax; | 62 m_BitStream.GetBits(m_nCoordBits) * (m_xmax - m_xmin) / m_CoordMax; |
67 y = m_ymin + | 63 y = m_ymin + |
68 m_BitStream.GetBits(m_nCoordBits) * (m_ymax - m_ymin) / m_CoordMax; | 64 m_BitStream.GetBits(m_nCoordBits) * (m_ymax - m_ymin) / m_CoordMax; |
69 } | 65 } |
70 } | 66 } |
71 | 67 |
72 void CPDF_MeshStream::GetColor(FX_FLOAT& r, FX_FLOAT& g, FX_FLOAT& b) { | 68 void CPDF_MeshStream::GetColor(FX_FLOAT& r, FX_FLOAT& g, FX_FLOAT& b) { |
73 uint32_t i; | 69 static const int kMaxResults = 8; |
74 FX_FLOAT color_value[8]; | 70 FX_FLOAT color_value[kMaxResults]; |
75 for (i = 0; i < m_nComps; i++) { | 71 for (uint32_t i = 0; i < m_nComps; ++i) { |
76 color_value[i] = m_ColorMin[i] + | 72 color_value[i] = m_ColorMin[i] + |
77 m_BitStream.GetBits(m_nCompBits) * | 73 m_BitStream.GetBits(m_nCompBits) * |
78 (m_ColorMax[i] - m_ColorMin[i]) / m_CompMax; | 74 (m_ColorMax[i] - m_ColorMin[i]) / m_CompMax; |
79 } | 75 } |
80 if (m_nFuncs) { | 76 if (m_funcs.empty()) { |
81 static const int kMaxResults = 8; | |
82 FX_FLOAT result[kMaxResults]; | |
83 int nResults; | |
84 FXSYS_memset(result, 0, sizeof(result)); | |
85 for (uint32_t i = 0; i < m_nFuncs; i++) { | |
86 if (m_pFuncs[i] && m_pFuncs[i]->CountOutputs() <= kMaxResults) { | |
87 m_pFuncs[i]->Call(color_value, 1, result, nResults); | |
88 } | |
89 } | |
90 m_pCS->GetRGB(result, r, g, b); | |
91 } else { | |
92 m_pCS->GetRGB(color_value, r, g, b); | 77 m_pCS->GetRGB(color_value, r, g, b); |
| 78 return; |
93 } | 79 } |
| 80 |
| 81 FX_FLOAT result[kMaxResults]; |
| 82 FXSYS_memset(result, 0, sizeof(result)); |
| 83 int nResults; |
| 84 for (const auto& func : m_funcs) { |
| 85 if (func && func->CountOutputs() <= kMaxResults) |
| 86 func->Call(color_value, 1, result, nResults); |
| 87 } |
| 88 m_pCS->GetRGB(result, r, g, b); |
94 } | 89 } |
95 | 90 |
96 uint32_t CPDF_MeshStream::GetVertex(CPDF_MeshVertex& vertex, | 91 uint32_t CPDF_MeshStream::GetVertex(CPDF_MeshVertex& vertex, |
97 CFX_Matrix* pObject2Bitmap) { | 92 CFX_Matrix* pObject2Bitmap) { |
98 uint32_t flag = GetFlag(); | 93 uint32_t flag = GetFlag(); |
99 GetCoords(vertex.x, vertex.y); | 94 GetCoords(vertex.x, vertex.y); |
100 pObject2Bitmap->Transform(vertex.x, vertex.y); | 95 pObject2Bitmap->Transform(vertex.x, vertex.y); |
101 GetColor(vertex.r, vertex.g, vertex.b); | 96 GetColor(vertex.r, vertex.g, vertex.b); |
102 m_BitStream.ByteAlign(); | 97 m_BitStream.ByteAlign(); |
103 return flag; | 98 return flag; |
104 } | 99 } |
105 | 100 |
106 FX_BOOL CPDF_MeshStream::GetVertexRow(CPDF_MeshVertex* vertex, | 101 FX_BOOL CPDF_MeshStream::GetVertexRow(CPDF_MeshVertex* vertex, |
107 int count, | 102 int count, |
108 CFX_Matrix* pObject2Bitmap) { | 103 CFX_Matrix* pObject2Bitmap) { |
109 for (int i = 0; i < count; i++) { | 104 for (int i = 0; i < count; i++) { |
110 if (m_BitStream.IsEOF()) | 105 if (m_BitStream.IsEOF()) |
111 return FALSE; | 106 return FALSE; |
112 | 107 |
113 GetCoords(vertex[i].x, vertex[i].y); | 108 GetCoords(vertex[i].x, vertex[i].y); |
114 pObject2Bitmap->Transform(vertex[i].x, vertex[i].y); | 109 pObject2Bitmap->Transform(vertex[i].x, vertex[i].y); |
115 GetColor(vertex[i].r, vertex[i].g, vertex[i].b); | 110 GetColor(vertex[i].r, vertex[i].g, vertex[i].b); |
116 m_BitStream.ByteAlign(); | 111 m_BitStream.ByteAlign(); |
117 } | 112 } |
118 return TRUE; | 113 return TRUE; |
119 } | 114 } |
OLD | NEW |