| 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 namespace { | 13 namespace { |
| 14 | 14 |
| 15 // See PDF Reference 1.7, page 315, table 4.32. (Also table 4.33 and 4.34) | 15 // See PDF Reference 1.7, page 315, table 4.32. (Also table 4.33 and 4.34) |
| 16 bool ShouldCheckBPC(ShadingType type) { |
| 17 switch (type) { |
| 18 case kFreeFormGouraudTriangleMeshShading: |
| 19 case kLatticeFormGouraudTriangleMeshShading: |
| 20 case kCoonsPatchMeshShading: |
| 21 case kTensorProductPatchMeshShading: |
| 22 return true; |
| 23 default: |
| 24 return false; |
| 25 } |
| 26 } |
| 27 |
| 28 // Same references as ShouldCheckBPC() above. |
| 16 bool IsValidBitsPerComponent(uint32_t x) { | 29 bool IsValidBitsPerComponent(uint32_t x) { |
| 17 switch (x) { | 30 switch (x) { |
| 18 case 1: | 31 case 1: |
| 19 case 2: | 32 case 2: |
| 20 case 4: | 33 case 4: |
| 21 case 8: | 34 case 8: |
| 22 case 12: | 35 case 12: |
| 23 case 16: | 36 case 16: |
| 24 return true; | 37 return true; |
| 25 default: | 38 default: |
| 26 return false; | 39 return false; |
| 27 } | 40 } |
| 28 } | 41 } |
| 29 | 42 |
| 30 // Same references as IsValidBitsPerComponent() above. | 43 // Same references as ShouldCheckBPC() above. |
| 31 bool IsValidBitsPerCoordinate(uint32_t x) { | 44 bool IsValidBitsPerCoordinate(uint32_t x) { |
| 32 switch (x) { | 45 switch (x) { |
| 33 case 1: | 46 case 1: |
| 34 case 2: | 47 case 2: |
| 35 case 4: | 48 case 4: |
| 36 case 8: | 49 case 8: |
| 37 case 12: | 50 case 12: |
| 38 case 16: | 51 case 16: |
| 39 case 24: | 52 case 24: |
| 40 case 32: | 53 case 32: |
| (...skipping 30 matching lines...) Expand all Loading... |
| 71 } // namespace | 84 } // namespace |
| 72 | 85 |
| 73 CPDF_MeshStream::CPDF_MeshStream( | 86 CPDF_MeshStream::CPDF_MeshStream( |
| 74 ShadingType type, | 87 ShadingType type, |
| 75 const std::vector<std::unique_ptr<CPDF_Function>>& funcs, | 88 const std::vector<std::unique_ptr<CPDF_Function>>& funcs, |
| 76 CPDF_Stream* pShadingStream, | 89 CPDF_Stream* pShadingStream, |
| 77 CPDF_ColorSpace* pCS) | 90 CPDF_ColorSpace* pCS) |
| 78 : m_type(type), | 91 : m_type(type), |
| 79 m_funcs(funcs), | 92 m_funcs(funcs), |
| 80 m_pShadingStream(pShadingStream), | 93 m_pShadingStream(pShadingStream), |
| 81 m_pCS(pCS) {} | 94 m_pCS(pCS), |
| 95 m_nCoordBits(0), |
| 96 m_nComponentBits(0), |
| 97 m_nFlagBits(0), |
| 98 m_nComponents(0), |
| 99 m_CoordMax(0), |
| 100 m_ComponentMax(0), |
| 101 m_xmin(0), |
| 102 m_xmax(0), |
| 103 m_ymin(0), |
| 104 m_ymax(0) { |
| 105 memset(&m_ColorMin, 0, sizeof(m_ColorMin)); |
| 106 memset(&m_ColorMax, 0, sizeof(m_ColorMax)); |
| 107 } |
| 82 | 108 |
| 83 bool CPDF_MeshStream::Load() { | 109 bool CPDF_MeshStream::Load() { |
| 84 m_Stream.LoadAllData(m_pShadingStream); | 110 m_Stream.LoadAllData(m_pShadingStream); |
| 85 m_BitStream.Init(m_Stream.GetData(), m_Stream.GetSize()); | 111 m_BitStream.Init(m_Stream.GetData(), m_Stream.GetSize()); |
| 86 CPDF_Dictionary* pDict = m_pShadingStream->GetDict(); | 112 CPDF_Dictionary* pDict = m_pShadingStream->GetDict(); |
| 87 m_nCoordBits = pDict->GetIntegerBy("BitsPerCoordinate"); | 113 m_nCoordBits = pDict->GetIntegerBy("BitsPerCoordinate"); |
| 88 if (!IsValidBitsPerCoordinate(m_nCoordBits)) | 114 m_nComponentBits = pDict->GetIntegerBy("BitsPerComponent"); |
| 89 return false; | 115 if (ShouldCheckBPC(m_type)) { |
| 90 | 116 if (!IsValidBitsPerCoordinate(m_nCoordBits)) |
| 91 m_nCompBits = pDict->GetIntegerBy("BitsPerComponent"); | 117 return false; |
| 92 if (!IsValidBitsPerComponent(m_nCompBits)) | 118 if (!IsValidBitsPerComponent(m_nComponentBits)) |
| 93 return false; | 119 return false; |
| 120 } |
| 94 | 121 |
| 95 m_nFlagBits = pDict->GetIntegerBy("BitsPerFlag"); | 122 m_nFlagBits = pDict->GetIntegerBy("BitsPerFlag"); |
| 96 if (ShouldCheckBitsPerFlag(m_type) && !IsValidBitsPerFlag(m_nFlagBits)) | 123 if (ShouldCheckBitsPerFlag(m_type) && !IsValidBitsPerFlag(m_nFlagBits)) |
| 97 return false; | 124 return false; |
| 98 | 125 |
| 99 uint32_t nComps = m_pCS->CountComponents(); | 126 uint32_t nComponents = m_pCS->CountComponents(); |
| 100 if (nComps > 8) | 127 if (nComponents > kMaxComponents) |
| 101 return false; | 128 return false; |
| 102 | 129 |
| 103 m_nComps = m_funcs.empty() ? nComps : 1; | 130 m_nComponents = m_funcs.empty() ? nComponents : 1; |
| 104 m_CoordMax = m_nCoordBits == 32 ? -1 : (1 << m_nCoordBits) - 1; | |
| 105 m_CompMax = (1 << m_nCompBits) - 1; | |
| 106 CPDF_Array* pDecode = pDict->GetArrayBy("Decode"); | 131 CPDF_Array* pDecode = pDict->GetArrayBy("Decode"); |
| 107 if (!pDecode || pDecode->GetCount() != 4 + m_nComps * 2) | 132 if (!pDecode || pDecode->GetCount() != 4 + m_nComponents * 2) |
| 108 return false; | 133 return false; |
| 109 | 134 |
| 110 m_xmin = pDecode->GetNumberAt(0); | 135 m_xmin = pDecode->GetNumberAt(0); |
| 111 m_xmax = pDecode->GetNumberAt(1); | 136 m_xmax = pDecode->GetNumberAt(1); |
| 112 m_ymin = pDecode->GetNumberAt(2); | 137 m_ymin = pDecode->GetNumberAt(2); |
| 113 m_ymax = pDecode->GetNumberAt(3); | 138 m_ymax = pDecode->GetNumberAt(3); |
| 114 for (uint32_t i = 0; i < m_nComps; ++i) { | 139 for (uint32_t i = 0; i < m_nComponents; ++i) { |
| 115 m_ColorMin[i] = pDecode->GetNumberAt(i * 2 + 4); | 140 m_ColorMin[i] = pDecode->GetNumberAt(i * 2 + 4); |
| 116 m_ColorMax[i] = pDecode->GetNumberAt(i * 2 + 5); | 141 m_ColorMax[i] = pDecode->GetNumberAt(i * 2 + 5); |
| 117 } | 142 } |
| 143 |
| 144 if (ShouldCheckBPC(m_type)) { |
| 145 m_CoordMax = m_nCoordBits == 32 ? -1 : (1 << m_nCoordBits) - 1; |
| 146 m_ComponentMax = (1 << m_nComponentBits) - 1; |
| 147 } |
| 118 return true; | 148 return true; |
| 119 } | 149 } |
| 120 | 150 |
| 121 uint32_t CPDF_MeshStream::GetFlag() { | 151 uint32_t CPDF_MeshStream::GetFlag() { |
| 122 ASSERT(ShouldCheckBitsPerFlag(m_type)); | 152 ASSERT(ShouldCheckBitsPerFlag(m_type)); |
| 123 return m_BitStream.GetBits(m_nFlagBits) & 0x03; | 153 return m_BitStream.GetBits(m_nFlagBits) & 0x03; |
| 124 } | 154 } |
| 125 | 155 |
| 126 void CPDF_MeshStream::GetCoords(FX_FLOAT& x, FX_FLOAT& y) { | 156 void CPDF_MeshStream::GetCoords(FX_FLOAT& x, FX_FLOAT& y) { |
| 157 ASSERT(ShouldCheckBPC(m_type)); |
| 127 if (m_nCoordBits == 32) { | 158 if (m_nCoordBits == 32) { |
| 128 x = m_xmin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * | 159 x = m_xmin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * |
| 129 (m_xmax - m_xmin) / (double)m_CoordMax); | 160 (m_xmax - m_xmin) / (double)m_CoordMax); |
| 130 y = m_ymin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * | 161 y = m_ymin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * |
| 131 (m_ymax - m_ymin) / (double)m_CoordMax); | 162 (m_ymax - m_ymin) / (double)m_CoordMax); |
| 132 } else { | 163 } else { |
| 133 x = m_xmin + | 164 x = m_xmin + |
| 134 m_BitStream.GetBits(m_nCoordBits) * (m_xmax - m_xmin) / m_CoordMax; | 165 m_BitStream.GetBits(m_nCoordBits) * (m_xmax - m_xmin) / m_CoordMax; |
| 135 y = m_ymin + | 166 y = m_ymin + |
| 136 m_BitStream.GetBits(m_nCoordBits) * (m_ymax - m_ymin) / m_CoordMax; | 167 m_BitStream.GetBits(m_nCoordBits) * (m_ymax - m_ymin) / m_CoordMax; |
| 137 } | 168 } |
| 138 } | 169 } |
| 139 | 170 |
| 140 void CPDF_MeshStream::GetColor(FX_FLOAT& r, FX_FLOAT& g, FX_FLOAT& b) { | 171 void CPDF_MeshStream::GetColor(FX_FLOAT& r, FX_FLOAT& g, FX_FLOAT& b) { |
| 141 FX_FLOAT color_value[kMaxResults]; | 172 ASSERT(ShouldCheckBPC(m_type)); |
| 142 for (uint32_t i = 0; i < m_nComps; ++i) { | 173 FX_FLOAT color_value[kMaxComponents]; |
| 174 for (uint32_t i = 0; i < m_nComponents; ++i) { |
| 143 color_value[i] = m_ColorMin[i] + | 175 color_value[i] = m_ColorMin[i] + |
| 144 m_BitStream.GetBits(m_nCompBits) * | 176 m_BitStream.GetBits(m_nComponentBits) * |
| 145 (m_ColorMax[i] - m_ColorMin[i]) / m_CompMax; | 177 (m_ColorMax[i] - m_ColorMin[i]) / m_ComponentMax; |
| 146 } | 178 } |
| 147 if (m_funcs.empty()) { | 179 if (m_funcs.empty()) { |
| 148 m_pCS->GetRGB(color_value, r, g, b); | 180 m_pCS->GetRGB(color_value, r, g, b); |
| 149 return; | 181 return; |
| 150 } | 182 } |
| 151 | 183 |
| 152 FX_FLOAT result[kMaxResults]; | 184 FX_FLOAT result[kMaxComponents]; |
| 153 FXSYS_memset(result, 0, sizeof(result)); | 185 FXSYS_memset(result, 0, sizeof(result)); |
| 154 int nResults; | 186 int nResults; |
| 155 for (const auto& func : m_funcs) { | 187 for (const auto& func : m_funcs) { |
| 156 if (func && func->CountOutputs() <= kMaxResults) | 188 if (func && func->CountOutputs() <= kMaxComponents) |
| 157 func->Call(color_value, 1, result, nResults); | 189 func->Call(color_value, 1, result, nResults); |
| 158 } | 190 } |
| 159 m_pCS->GetRGB(result, r, g, b); | 191 m_pCS->GetRGB(result, r, g, b); |
| 160 } | 192 } |
| 161 | 193 |
| 162 uint32_t CPDF_MeshStream::GetVertex(CPDF_MeshVertex& vertex, | 194 uint32_t CPDF_MeshStream::GetVertex(CPDF_MeshVertex& vertex, |
| 163 CFX_Matrix* pObject2Bitmap) { | 195 CFX_Matrix* pObject2Bitmap) { |
| 164 uint32_t flag = GetFlag(); | 196 uint32_t flag = GetFlag(); |
| 165 GetCoords(vertex.x, vertex.y); | 197 GetCoords(vertex.x, vertex.y); |
| 166 pObject2Bitmap->Transform(vertex.x, vertex.y); | 198 pObject2Bitmap->Transform(vertex.x, vertex.y); |
| 167 GetColor(vertex.r, vertex.g, vertex.b); | 199 GetColor(vertex.r, vertex.g, vertex.b); |
| 168 m_BitStream.ByteAlign(); | 200 m_BitStream.ByteAlign(); |
| 169 return flag; | 201 return flag; |
| 170 } | 202 } |
| 171 | 203 |
| 172 FX_BOOL CPDF_MeshStream::GetVertexRow(CPDF_MeshVertex* vertex, | 204 FX_BOOL CPDF_MeshStream::GetVertexRow(CPDF_MeshVertex* vertex, |
| 173 int count, | 205 int count, |
| 174 CFX_Matrix* pObject2Bitmap) { | 206 CFX_Matrix* pObject2Bitmap) { |
| 175 for (int i = 0; i < count; i++) { | 207 for (int i = 0; i < count; i++) { |
| 176 if (m_BitStream.IsEOF()) | 208 if (m_BitStream.IsEOF()) |
| 177 return FALSE; | 209 return FALSE; |
| 178 | 210 |
| 179 GetCoords(vertex[i].x, vertex[i].y); | 211 GetCoords(vertex[i].x, vertex[i].y); |
| 180 pObject2Bitmap->Transform(vertex[i].x, vertex[i].y); | 212 pObject2Bitmap->Transform(vertex[i].x, vertex[i].y); |
| 181 GetColor(vertex[i].r, vertex[i].g, vertex[i].b); | 213 GetColor(vertex[i].r, vertex[i].g, vertex[i].b); |
| 182 m_BitStream.ByteAlign(); | 214 m_BitStream.ByteAlign(); |
| 183 } | 215 } |
| 184 return TRUE; | 216 return TRUE; |
| 185 } | 217 } |
| OLD | NEW |