| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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. | |
| 4 | |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
| 6 | |
| 7 #include "core/fpdfapi/fpdf_page/cpdf_meshstream.h" | |
| 8 | |
| 9 #include "core/fpdfapi/fpdf_page/cpdf_colorspace.h" | |
| 10 #include "core/fpdfapi/fpdf_page/pageint.h" | |
| 11 #include "core/fpdfapi/fpdf_parser/cpdf_array.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 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. | |
| 29 bool IsValidBitsPerComponent(uint32_t x) { | |
| 30 switch (x) { | |
| 31 case 1: | |
| 32 case 2: | |
| 33 case 4: | |
| 34 case 8: | |
| 35 case 12: | |
| 36 case 16: | |
| 37 return true; | |
| 38 default: | |
| 39 return false; | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 // Same references as ShouldCheckBPC() above. | |
| 44 bool IsValidBitsPerCoordinate(uint32_t x) { | |
| 45 switch (x) { | |
| 46 case 1: | |
| 47 case 2: | |
| 48 case 4: | |
| 49 case 8: | |
| 50 case 12: | |
| 51 case 16: | |
| 52 case 24: | |
| 53 case 32: | |
| 54 return true; | |
| 55 default: | |
| 56 return false; | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 // See PDF Reference 1.7, page 315, table 4.32. (Also table 4.34) | |
| 61 bool ShouldCheckBitsPerFlag(ShadingType type) { | |
| 62 switch (type) { | |
| 63 case kFreeFormGouraudTriangleMeshShading: | |
| 64 case kCoonsPatchMeshShading: | |
| 65 case kTensorProductPatchMeshShading: | |
| 66 return true; | |
| 67 default: | |
| 68 return false; | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 // Same references as ShouldCheckBitsPerFlag() above. | |
| 73 bool IsValidBitsPerFlag(uint32_t x) { | |
| 74 switch (x) { | |
| 75 case 2: | |
| 76 case 4: | |
| 77 case 8: | |
| 78 return true; | |
| 79 default: | |
| 80 return false; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 } // namespace | |
| 85 | |
| 86 CPDF_MeshStream::CPDF_MeshStream( | |
| 87 ShadingType type, | |
| 88 const std::vector<std::unique_ptr<CPDF_Function>>& funcs, | |
| 89 CPDF_Stream* pShadingStream, | |
| 90 CPDF_ColorSpace* pCS) | |
| 91 : m_type(type), | |
| 92 m_funcs(funcs), | |
| 93 m_pShadingStream(pShadingStream), | |
| 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 } | |
| 108 | |
| 109 bool CPDF_MeshStream::Load() { | |
| 110 m_Stream.LoadAllData(m_pShadingStream); | |
| 111 m_BitStream.Init(m_Stream.GetData(), m_Stream.GetSize()); | |
| 112 CPDF_Dictionary* pDict = m_pShadingStream->GetDict(); | |
| 113 m_nCoordBits = pDict->GetIntegerFor("BitsPerCoordinate"); | |
| 114 m_nComponentBits = pDict->GetIntegerFor("BitsPerComponent"); | |
| 115 if (ShouldCheckBPC(m_type)) { | |
| 116 if (!IsValidBitsPerCoordinate(m_nCoordBits)) | |
| 117 return false; | |
| 118 if (!IsValidBitsPerComponent(m_nComponentBits)) | |
| 119 return false; | |
| 120 } | |
| 121 | |
| 122 m_nFlagBits = pDict->GetIntegerFor("BitsPerFlag"); | |
| 123 if (ShouldCheckBitsPerFlag(m_type) && !IsValidBitsPerFlag(m_nFlagBits)) | |
| 124 return false; | |
| 125 | |
| 126 uint32_t nComponents = m_pCS->CountComponents(); | |
| 127 if (nComponents > kMaxComponents) | |
| 128 return false; | |
| 129 | |
| 130 m_nComponents = m_funcs.empty() ? nComponents : 1; | |
| 131 CPDF_Array* pDecode = pDict->GetArrayFor("Decode"); | |
| 132 if (!pDecode || pDecode->GetCount() != 4 + m_nComponents * 2) | |
| 133 return false; | |
| 134 | |
| 135 m_xmin = pDecode->GetNumberAt(0); | |
| 136 m_xmax = pDecode->GetNumberAt(1); | |
| 137 m_ymin = pDecode->GetNumberAt(2); | |
| 138 m_ymax = pDecode->GetNumberAt(3); | |
| 139 for (uint32_t i = 0; i < m_nComponents; ++i) { | |
| 140 m_ColorMin[i] = pDecode->GetNumberAt(i * 2 + 4); | |
| 141 m_ColorMax[i] = pDecode->GetNumberAt(i * 2 + 5); | |
| 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 } | |
| 148 return true; | |
| 149 } | |
| 150 | |
| 151 uint32_t CPDF_MeshStream::GetFlag() { | |
| 152 ASSERT(ShouldCheckBitsPerFlag(m_type)); | |
| 153 return m_BitStream.GetBits(m_nFlagBits) & 0x03; | |
| 154 } | |
| 155 | |
| 156 void CPDF_MeshStream::GetCoords(FX_FLOAT& x, FX_FLOAT& y) { | |
| 157 ASSERT(ShouldCheckBPC(m_type)); | |
| 158 if (m_nCoordBits == 32) { | |
| 159 x = m_xmin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * | |
| 160 (m_xmax - m_xmin) / (double)m_CoordMax); | |
| 161 y = m_ymin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * | |
| 162 (m_ymax - m_ymin) / (double)m_CoordMax); | |
| 163 } else { | |
| 164 x = m_xmin + | |
| 165 m_BitStream.GetBits(m_nCoordBits) * (m_xmax - m_xmin) / m_CoordMax; | |
| 166 y = m_ymin + | |
| 167 m_BitStream.GetBits(m_nCoordBits) * (m_ymax - m_ymin) / m_CoordMax; | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 void CPDF_MeshStream::GetColor(FX_FLOAT& r, FX_FLOAT& g, FX_FLOAT& b) { | |
| 172 ASSERT(ShouldCheckBPC(m_type)); | |
| 173 FX_FLOAT color_value[kMaxComponents]; | |
| 174 for (uint32_t i = 0; i < m_nComponents; ++i) { | |
| 175 color_value[i] = m_ColorMin[i] + | |
| 176 m_BitStream.GetBits(m_nComponentBits) * | |
| 177 (m_ColorMax[i] - m_ColorMin[i]) / m_ComponentMax; | |
| 178 } | |
| 179 if (m_funcs.empty()) { | |
| 180 m_pCS->GetRGB(color_value, r, g, b); | |
| 181 return; | |
| 182 } | |
| 183 | |
| 184 FX_FLOAT result[kMaxComponents]; | |
| 185 FXSYS_memset(result, 0, sizeof(result)); | |
| 186 int nResults; | |
| 187 for (const auto& func : m_funcs) { | |
| 188 if (func && func->CountOutputs() <= kMaxComponents) | |
| 189 func->Call(color_value, 1, result, nResults); | |
| 190 } | |
| 191 m_pCS->GetRGB(result, r, g, b); | |
| 192 } | |
| 193 | |
| 194 uint32_t CPDF_MeshStream::GetVertex(CPDF_MeshVertex& vertex, | |
| 195 CFX_Matrix* pObject2Bitmap) { | |
| 196 uint32_t flag = GetFlag(); | |
| 197 GetCoords(vertex.x, vertex.y); | |
| 198 pObject2Bitmap->Transform(vertex.x, vertex.y); | |
| 199 GetColor(vertex.r, vertex.g, vertex.b); | |
| 200 m_BitStream.ByteAlign(); | |
| 201 return flag; | |
| 202 } | |
| 203 | |
| 204 FX_BOOL CPDF_MeshStream::GetVertexRow(CPDF_MeshVertex* vertex, | |
| 205 int count, | |
| 206 CFX_Matrix* pObject2Bitmap) { | |
| 207 for (int i = 0; i < count; i++) { | |
| 208 if (m_BitStream.IsEOF()) | |
| 209 return FALSE; | |
| 210 | |
| 211 GetCoords(vertex[i].x, vertex[i].y); | |
| 212 pObject2Bitmap->Transform(vertex[i].x, vertex[i].y); | |
| 213 GetColor(vertex[i].r, vertex[i].g, vertex[i].b); | |
| 214 m_BitStream.ByteAlign(); | |
| 215 } | |
| 216 return TRUE; | |
| 217 } | |
| OLD | NEW |