OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 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/page/pageint.h" | 7 #include "core/fpdfapi/page/pageint.h" |
8 | 8 |
9 #include <limits.h> | 9 #include <limits.h> |
10 | 10 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 FX_FLOAT divisor = xmax - xmin; | 77 FX_FLOAT divisor = xmax - xmin; |
78 return ymin + (divisor ? (x - xmin) * (ymax - ymin) / divisor : 0); | 78 return ymin + (divisor ? (x - xmin) * (ymax - ymin) / divisor : 0); |
79 } | 79 } |
80 | 80 |
81 class CPDF_PSFunc : public CPDF_Function { | 81 class CPDF_PSFunc : public CPDF_Function { |
82 public: | 82 public: |
83 CPDF_PSFunc() : CPDF_Function(Type::kType4PostScript) {} | 83 CPDF_PSFunc() : CPDF_Function(Type::kType4PostScript) {} |
84 ~CPDF_PSFunc() override {} | 84 ~CPDF_PSFunc() override {} |
85 | 85 |
86 // CPDF_Function | 86 // CPDF_Function |
87 FX_BOOL v_Init(CPDF_Object* pObj) override; | 87 bool v_Init(CPDF_Object* pObj) override; |
88 FX_BOOL v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const override; | 88 bool v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const override; |
89 | 89 |
90 private: | 90 private: |
91 CPDF_PSEngine m_PS; | 91 CPDF_PSEngine m_PS; |
92 }; | 92 }; |
93 | 93 |
94 FX_BOOL CPDF_PSFunc::v_Init(CPDF_Object* pObj) { | 94 bool CPDF_PSFunc::v_Init(CPDF_Object* pObj) { |
95 CPDF_StreamAcc acc; | 95 CPDF_StreamAcc acc; |
96 acc.LoadAllData(pObj->AsStream(), FALSE); | 96 acc.LoadAllData(pObj->AsStream(), false); |
97 return m_PS.Parse(reinterpret_cast<const FX_CHAR*>(acc.GetData()), | 97 return m_PS.Parse(reinterpret_cast<const FX_CHAR*>(acc.GetData()), |
98 acc.GetSize()); | 98 acc.GetSize()); |
99 } | 99 } |
100 | 100 |
101 FX_BOOL CPDF_PSFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const { | 101 bool CPDF_PSFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const { |
102 CPDF_PSEngine& PS = const_cast<CPDF_PSEngine&>(m_PS); | 102 CPDF_PSEngine& PS = const_cast<CPDF_PSEngine&>(m_PS); |
103 PS.Reset(); | 103 PS.Reset(); |
104 for (uint32_t i = 0; i < m_nInputs; i++) | 104 for (uint32_t i = 0; i < m_nInputs; i++) |
105 PS.Push(inputs[i]); | 105 PS.Push(inputs[i]); |
106 PS.Execute(); | 106 PS.Execute(); |
107 if (PS.GetStackSize() < m_nOutputs) | 107 if (PS.GetStackSize() < m_nOutputs) |
108 return FALSE; | 108 return false; |
109 for (uint32_t i = 0; i < m_nOutputs; i++) | 109 for (uint32_t i = 0; i < m_nOutputs; i++) |
110 results[m_nOutputs - i - 1] = PS.Pop(); | 110 results[m_nOutputs - i - 1] = PS.Pop(); |
111 return TRUE; | 111 return true; |
112 } | 112 } |
113 | 113 |
114 } // namespace | 114 } // namespace |
115 | 115 |
116 class CPDF_PSOP { | 116 class CPDF_PSOP { |
117 public: | 117 public: |
118 explicit CPDF_PSOP(PDF_PSOP op) : m_op(op), m_value(0) { | 118 explicit CPDF_PSOP(PDF_PSOP op) : m_op(op), m_value(0) { |
119 ASSERT(m_op != PSOP_CONST); | 119 ASSERT(m_op != PSOP_CONST); |
120 ASSERT(m_op != PSOP_PROC); | 120 ASSERT(m_op != PSOP_PROC); |
121 } | 121 } |
(...skipping 16 matching lines...) Expand all Loading... |
138 } | 138 } |
139 | 139 |
140 PDF_PSOP GetOp() const { return m_op; } | 140 PDF_PSOP GetOp() const { return m_op; } |
141 | 141 |
142 private: | 142 private: |
143 const PDF_PSOP m_op; | 143 const PDF_PSOP m_op; |
144 const FX_FLOAT m_value; | 144 const FX_FLOAT m_value; |
145 std::unique_ptr<CPDF_PSProc> m_proc; | 145 std::unique_ptr<CPDF_PSProc> m_proc; |
146 }; | 146 }; |
147 | 147 |
148 FX_BOOL CPDF_PSEngine::Execute() { | 148 bool CPDF_PSEngine::Execute() { |
149 return m_MainProc.Execute(this); | 149 return m_MainProc.Execute(this); |
150 } | 150 } |
151 | 151 |
152 CPDF_PSProc::CPDF_PSProc() {} | 152 CPDF_PSProc::CPDF_PSProc() {} |
153 CPDF_PSProc::~CPDF_PSProc() {} | 153 CPDF_PSProc::~CPDF_PSProc() {} |
154 | 154 |
155 FX_BOOL CPDF_PSProc::Execute(CPDF_PSEngine* pEngine) { | 155 bool CPDF_PSProc::Execute(CPDF_PSEngine* pEngine) { |
156 for (size_t i = 0; i < m_Operators.size(); ++i) { | 156 for (size_t i = 0; i < m_Operators.size(); ++i) { |
157 const PDF_PSOP op = m_Operators[i]->GetOp(); | 157 const PDF_PSOP op = m_Operators[i]->GetOp(); |
158 if (op == PSOP_PROC) | 158 if (op == PSOP_PROC) |
159 continue; | 159 continue; |
160 | 160 |
161 if (op == PSOP_CONST) { | 161 if (op == PSOP_CONST) { |
162 pEngine->Push(m_Operators[i]->GetFloatValue()); | 162 pEngine->Push(m_Operators[i]->GetFloatValue()); |
163 continue; | 163 continue; |
164 } | 164 } |
165 | 165 |
166 if (op == PSOP_IF) { | 166 if (op == PSOP_IF) { |
167 if (i == 0 || m_Operators[i - 1]->GetOp() != PSOP_PROC) | 167 if (i == 0 || m_Operators[i - 1]->GetOp() != PSOP_PROC) |
168 return FALSE; | 168 return false; |
169 | 169 |
170 if (static_cast<int>(pEngine->Pop())) | 170 if (static_cast<int>(pEngine->Pop())) |
171 m_Operators[i - 1]->GetProc()->Execute(pEngine); | 171 m_Operators[i - 1]->GetProc()->Execute(pEngine); |
172 } else if (op == PSOP_IFELSE) { | 172 } else if (op == PSOP_IFELSE) { |
173 if (i < 2 || m_Operators[i - 1]->GetOp() != PSOP_PROC || | 173 if (i < 2 || m_Operators[i - 1]->GetOp() != PSOP_PROC || |
174 m_Operators[i - 2]->GetOp() != PSOP_PROC) { | 174 m_Operators[i - 2]->GetOp() != PSOP_PROC) { |
175 return FALSE; | 175 return false; |
176 } | 176 } |
177 size_t offset = static_cast<int>(pEngine->Pop()) ? 2 : 1; | 177 size_t offset = static_cast<int>(pEngine->Pop()) ? 2 : 1; |
178 m_Operators[i - offset]->GetProc()->Execute(pEngine); | 178 m_Operators[i - offset]->GetProc()->Execute(pEngine); |
179 } else { | 179 } else { |
180 pEngine->DoOperator(op); | 180 pEngine->DoOperator(op); |
181 } | 181 } |
182 } | 182 } |
183 return TRUE; | 183 return true; |
184 } | 184 } |
185 | 185 |
186 CPDF_PSEngine::CPDF_PSEngine() { | 186 CPDF_PSEngine::CPDF_PSEngine() { |
187 m_StackCount = 0; | 187 m_StackCount = 0; |
188 } | 188 } |
189 CPDF_PSEngine::~CPDF_PSEngine() {} | 189 CPDF_PSEngine::~CPDF_PSEngine() {} |
190 void CPDF_PSEngine::Push(FX_FLOAT v) { | 190 void CPDF_PSEngine::Push(FX_FLOAT v) { |
191 if (m_StackCount == PSENGINE_STACKSIZE) { | 191 if (m_StackCount == PSENGINE_STACKSIZE) { |
192 return; | 192 return; |
193 } | 193 } |
194 m_Stack[m_StackCount++] = v; | 194 m_Stack[m_StackCount++] = v; |
195 } | 195 } |
196 FX_FLOAT CPDF_PSEngine::Pop() { | 196 FX_FLOAT CPDF_PSEngine::Pop() { |
197 if (m_StackCount == 0) { | 197 if (m_StackCount == 0) { |
198 return 0; | 198 return 0; |
199 } | 199 } |
200 return m_Stack[--m_StackCount]; | 200 return m_Stack[--m_StackCount]; |
201 } | 201 } |
202 FX_BOOL CPDF_PSEngine::Parse(const FX_CHAR* str, int size) { | 202 bool CPDF_PSEngine::Parse(const FX_CHAR* str, int size) { |
203 CPDF_SimpleParser parser((uint8_t*)str, size); | 203 CPDF_SimpleParser parser((uint8_t*)str, size); |
204 CFX_ByteStringC word = parser.GetWord(); | 204 CFX_ByteStringC word = parser.GetWord(); |
205 if (word != "{") { | 205 if (word != "{") { |
206 return FALSE; | 206 return false; |
207 } | 207 } |
208 return m_MainProc.Parse(&parser, 0); | 208 return m_MainProc.Parse(&parser, 0); |
209 } | 209 } |
210 | 210 |
211 FX_BOOL CPDF_PSProc::Parse(CPDF_SimpleParser* parser, int depth) { | 211 bool CPDF_PSProc::Parse(CPDF_SimpleParser* parser, int depth) { |
212 if (depth > kMaxDepth) | 212 if (depth > kMaxDepth) |
213 return FALSE; | 213 return false; |
214 | 214 |
215 while (1) { | 215 while (1) { |
216 CFX_ByteStringC word = parser->GetWord(); | 216 CFX_ByteStringC word = parser->GetWord(); |
217 if (word.IsEmpty()) { | 217 if (word.IsEmpty()) { |
218 return FALSE; | 218 return false; |
219 } | 219 } |
220 if (word == "}") { | 220 if (word == "}") { |
221 return TRUE; | 221 return true; |
222 } | 222 } |
223 if (word == "{") { | 223 if (word == "{") { |
224 std::unique_ptr<CPDF_PSProc> proc(new CPDF_PSProc); | 224 std::unique_ptr<CPDF_PSProc> proc(new CPDF_PSProc); |
225 std::unique_ptr<CPDF_PSOP> op(new CPDF_PSOP(std::move(proc))); | 225 std::unique_ptr<CPDF_PSOP> op(new CPDF_PSOP(std::move(proc))); |
226 m_Operators.push_back(std::move(op)); | 226 m_Operators.push_back(std::move(op)); |
227 if (!m_Operators.back()->GetProc()->Parse(parser, depth + 1)) { | 227 if (!m_Operators.back()->GetProc()->Parse(parser, depth + 1)) { |
228 return FALSE; | 228 return false; |
229 } | 229 } |
230 } else { | 230 } else { |
231 bool found = false; | 231 bool found = false; |
232 for (const PDF_PSOpName& op_name : kPsOpNames) { | 232 for (const PDF_PSOpName& op_name : kPsOpNames) { |
233 if (word == CFX_ByteStringC(op_name.name)) { | 233 if (word == CFX_ByteStringC(op_name.name)) { |
234 std::unique_ptr<CPDF_PSOP> op(new CPDF_PSOP(op_name.op)); | 234 std::unique_ptr<CPDF_PSOP> op(new CPDF_PSOP(op_name.op)); |
235 m_Operators.push_back(std::move(op)); | 235 m_Operators.push_back(std::move(op)); |
236 found = true; | 236 found = true; |
237 break; | 237 break; |
238 } | 238 } |
239 } | 239 } |
240 if (!found) { | 240 if (!found) { |
241 std::unique_ptr<CPDF_PSOP> op(new CPDF_PSOP(FX_atof(word))); | 241 std::unique_ptr<CPDF_PSOP> op(new CPDF_PSOP(FX_atof(word))); |
242 m_Operators.push_back(std::move(op)); | 242 m_Operators.push_back(std::move(op)); |
243 } | 243 } |
244 } | 244 } |
245 } | 245 } |
246 } | 246 } |
247 | 247 |
248 FX_BOOL CPDF_PSEngine::DoOperator(PDF_PSOP op) { | 248 bool CPDF_PSEngine::DoOperator(PDF_PSOP op) { |
249 int i1; | 249 int i1; |
250 int i2; | 250 int i2; |
251 FX_FLOAT d1; | 251 FX_FLOAT d1; |
252 FX_FLOAT d2; | 252 FX_FLOAT d2; |
253 FX_SAFE_INT32 result; | 253 FX_SAFE_INT32 result; |
254 switch (op) { | 254 switch (op) { |
255 case PSOP_ADD: | 255 case PSOP_ADD: |
256 d1 = Pop(); | 256 d1 = Pop(); |
257 d2 = Pop(); | 257 d2 = Pop(); |
258 Push(d1 + d2); | 258 Push(d1 + d2); |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 j -= n; | 467 j -= n; |
468 auto begin_it = std::begin(m_Stack) + m_StackCount - n; | 468 auto begin_it = std::begin(m_Stack) + m_StackCount - n; |
469 auto middle_it = begin_it - j; | 469 auto middle_it = begin_it - j; |
470 auto end_it = std::begin(m_Stack) + m_StackCount; | 470 auto end_it = std::begin(m_Stack) + m_StackCount; |
471 std::rotate(begin_it, middle_it, end_it); | 471 std::rotate(begin_it, middle_it, end_it); |
472 break; | 472 break; |
473 } | 473 } |
474 default: | 474 default: |
475 break; | 475 break; |
476 } | 476 } |
477 return TRUE; | 477 return true; |
478 } | 478 } |
479 | 479 |
480 CPDF_SampledFunc::CPDF_SampledFunc() : CPDF_Function(Type::kType0Sampled) {} | 480 CPDF_SampledFunc::CPDF_SampledFunc() : CPDF_Function(Type::kType0Sampled) {} |
481 | 481 |
482 CPDF_SampledFunc::~CPDF_SampledFunc() {} | 482 CPDF_SampledFunc::~CPDF_SampledFunc() {} |
483 | 483 |
484 FX_BOOL CPDF_SampledFunc::v_Init(CPDF_Object* pObj) { | 484 bool CPDF_SampledFunc::v_Init(CPDF_Object* pObj) { |
485 CPDF_Stream* pStream = pObj->AsStream(); | 485 CPDF_Stream* pStream = pObj->AsStream(); |
486 if (!pStream) | 486 if (!pStream) |
487 return false; | 487 return false; |
488 | 488 |
489 CPDF_Dictionary* pDict = pStream->GetDict(); | 489 CPDF_Dictionary* pDict = pStream->GetDict(); |
490 CPDF_Array* pSize = pDict->GetArrayFor("Size"); | 490 CPDF_Array* pSize = pDict->GetArrayFor("Size"); |
491 CPDF_Array* pEncode = pDict->GetArrayFor("Encode"); | 491 CPDF_Array* pEncode = pDict->GetArrayFor("Encode"); |
492 CPDF_Array* pDecode = pDict->GetArrayFor("Decode"); | 492 CPDF_Array* pDecode = pDict->GetArrayFor("Decode"); |
493 m_nBitsPerSample = pDict->GetIntegerFor("BitsPerSample"); | 493 m_nBitsPerSample = pDict->GetIntegerFor("BitsPerSample"); |
494 if (!IsValidBitsPerSample(m_nBitsPerSample)) | 494 if (!IsValidBitsPerSample(m_nBitsPerSample)) |
495 return FALSE; | 495 return false; |
496 | 496 |
497 m_SampleMax = 0xffffffff >> (32 - m_nBitsPerSample); | 497 m_SampleMax = 0xffffffff >> (32 - m_nBitsPerSample); |
498 m_pSampleStream.reset(new CPDF_StreamAcc); | 498 m_pSampleStream.reset(new CPDF_StreamAcc); |
499 m_pSampleStream->LoadAllData(pStream, FALSE); | 499 m_pSampleStream->LoadAllData(pStream, false); |
500 FX_SAFE_UINT32 nTotalSampleBits = 1; | 500 FX_SAFE_UINT32 nTotalSampleBits = 1; |
501 m_EncodeInfo.resize(m_nInputs); | 501 m_EncodeInfo.resize(m_nInputs); |
502 for (uint32_t i = 0; i < m_nInputs; i++) { | 502 for (uint32_t i = 0; i < m_nInputs; i++) { |
503 m_EncodeInfo[i].sizes = pSize ? pSize->GetIntegerAt(i) : 0; | 503 m_EncodeInfo[i].sizes = pSize ? pSize->GetIntegerAt(i) : 0; |
504 if (!pSize && i == 0) | 504 if (!pSize && i == 0) |
505 m_EncodeInfo[i].sizes = pDict->GetIntegerFor("Size"); | 505 m_EncodeInfo[i].sizes = pDict->GetIntegerFor("Size"); |
506 nTotalSampleBits *= m_EncodeInfo[i].sizes; | 506 nTotalSampleBits *= m_EncodeInfo[i].sizes; |
507 if (pEncode) { | 507 if (pEncode) { |
508 m_EncodeInfo[i].encode_min = pEncode->GetFloatAt(i * 2); | 508 m_EncodeInfo[i].encode_min = pEncode->GetFloatAt(i * 2); |
509 m_EncodeInfo[i].encode_max = pEncode->GetFloatAt(i * 2 + 1); | 509 m_EncodeInfo[i].encode_max = pEncode->GetFloatAt(i * 2 + 1); |
510 } else { | 510 } else { |
511 m_EncodeInfo[i].encode_min = 0; | 511 m_EncodeInfo[i].encode_min = 0; |
512 m_EncodeInfo[i].encode_max = | 512 m_EncodeInfo[i].encode_max = |
513 m_EncodeInfo[i].sizes == 1 ? 1 : (FX_FLOAT)m_EncodeInfo[i].sizes - 1; | 513 m_EncodeInfo[i].sizes == 1 ? 1 : (FX_FLOAT)m_EncodeInfo[i].sizes - 1; |
514 } | 514 } |
515 } | 515 } |
516 nTotalSampleBits *= m_nBitsPerSample; | 516 nTotalSampleBits *= m_nBitsPerSample; |
517 nTotalSampleBits *= m_nOutputs; | 517 nTotalSampleBits *= m_nOutputs; |
518 FX_SAFE_UINT32 nTotalSampleBytes = nTotalSampleBits; | 518 FX_SAFE_UINT32 nTotalSampleBytes = nTotalSampleBits; |
519 nTotalSampleBytes += 7; | 519 nTotalSampleBytes += 7; |
520 nTotalSampleBytes /= 8; | 520 nTotalSampleBytes /= 8; |
521 if (!nTotalSampleBytes.IsValid() || nTotalSampleBytes.ValueOrDie() == 0 || | 521 if (!nTotalSampleBytes.IsValid() || nTotalSampleBytes.ValueOrDie() == 0 || |
522 nTotalSampleBytes.ValueOrDie() > m_pSampleStream->GetSize()) { | 522 nTotalSampleBytes.ValueOrDie() > m_pSampleStream->GetSize()) { |
523 return FALSE; | 523 return false; |
524 } | 524 } |
525 m_DecodeInfo.resize(m_nOutputs); | 525 m_DecodeInfo.resize(m_nOutputs); |
526 for (uint32_t i = 0; i < m_nOutputs; i++) { | 526 for (uint32_t i = 0; i < m_nOutputs; i++) { |
527 if (pDecode) { | 527 if (pDecode) { |
528 m_DecodeInfo[i].decode_min = pDecode->GetFloatAt(2 * i); | 528 m_DecodeInfo[i].decode_min = pDecode->GetFloatAt(2 * i); |
529 m_DecodeInfo[i].decode_max = pDecode->GetFloatAt(2 * i + 1); | 529 m_DecodeInfo[i].decode_max = pDecode->GetFloatAt(2 * i + 1); |
530 } else { | 530 } else { |
531 m_DecodeInfo[i].decode_min = m_pRanges[i * 2]; | 531 m_DecodeInfo[i].decode_min = m_pRanges[i * 2]; |
532 m_DecodeInfo[i].decode_max = m_pRanges[i * 2 + 1]; | 532 m_DecodeInfo[i].decode_max = m_pRanges[i * 2 + 1]; |
533 } | 533 } |
534 } | 534 } |
535 return TRUE; | 535 return true; |
536 } | 536 } |
537 | 537 |
538 FX_BOOL CPDF_SampledFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const { | 538 bool CPDF_SampledFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const { |
539 int pos = 0; | 539 int pos = 0; |
540 CFX_FixedBufGrow<FX_FLOAT, 16> encoded_input_buf(m_nInputs); | 540 CFX_FixedBufGrow<FX_FLOAT, 16> encoded_input_buf(m_nInputs); |
541 FX_FLOAT* encoded_input = encoded_input_buf; | 541 FX_FLOAT* encoded_input = encoded_input_buf; |
542 CFX_FixedBufGrow<uint32_t, 32> int_buf(m_nInputs * 2); | 542 CFX_FixedBufGrow<uint32_t, 32> int_buf(m_nInputs * 2); |
543 uint32_t* index = int_buf; | 543 uint32_t* index = int_buf; |
544 uint32_t* blocksize = index + m_nInputs; | 544 uint32_t* blocksize = index + m_nInputs; |
545 for (uint32_t i = 0; i < m_nInputs; i++) { | 545 for (uint32_t i = 0; i < m_nInputs; i++) { |
546 if (i == 0) | 546 if (i == 0) |
547 blocksize[i] = 1; | 547 blocksize[i] = 1; |
548 else | 548 else |
549 blocksize[i] = blocksize[i - 1] * m_EncodeInfo[i - 1].sizes; | 549 blocksize[i] = blocksize[i - 1] * m_EncodeInfo[i - 1].sizes; |
550 encoded_input[i] = | 550 encoded_input[i] = |
551 PDF_Interpolate(inputs[i], m_pDomains[i * 2], m_pDomains[i * 2 + 1], | 551 PDF_Interpolate(inputs[i], m_pDomains[i * 2], m_pDomains[i * 2 + 1], |
552 m_EncodeInfo[i].encode_min, m_EncodeInfo[i].encode_max); | 552 m_EncodeInfo[i].encode_min, m_EncodeInfo[i].encode_max); |
553 index[i] = std::min((uint32_t)std::max(0.f, encoded_input[i]), | 553 index[i] = std::min((uint32_t)std::max(0.f, encoded_input[i]), |
554 m_EncodeInfo[i].sizes - 1); | 554 m_EncodeInfo[i].sizes - 1); |
555 pos += index[i] * blocksize[i]; | 555 pos += index[i] * blocksize[i]; |
556 } | 556 } |
557 FX_SAFE_INT32 bits_to_output = m_nOutputs; | 557 FX_SAFE_INT32 bits_to_output = m_nOutputs; |
558 bits_to_output *= m_nBitsPerSample; | 558 bits_to_output *= m_nBitsPerSample; |
559 if (!bits_to_output.IsValid()) | 559 if (!bits_to_output.IsValid()) |
560 return FALSE; | 560 return false; |
561 | 561 |
562 FX_SAFE_INT32 bitpos = pos; | 562 FX_SAFE_INT32 bitpos = pos; |
563 bitpos *= bits_to_output.ValueOrDie(); | 563 bitpos *= bits_to_output.ValueOrDie(); |
564 if (!bitpos.IsValid()) | 564 if (!bitpos.IsValid()) |
565 return FALSE; | 565 return false; |
566 | 566 |
567 FX_SAFE_INT32 range_check = bitpos; | 567 FX_SAFE_INT32 range_check = bitpos; |
568 range_check += bits_to_output.ValueOrDie(); | 568 range_check += bits_to_output.ValueOrDie(); |
569 if (!range_check.IsValid()) | 569 if (!range_check.IsValid()) |
570 return FALSE; | 570 return false; |
571 | 571 |
572 const uint8_t* pSampleData = m_pSampleStream->GetData(); | 572 const uint8_t* pSampleData = m_pSampleStream->GetData(); |
573 if (!pSampleData) | 573 if (!pSampleData) |
574 return FALSE; | 574 return false; |
575 | 575 |
576 for (uint32_t j = 0; j < m_nOutputs; j++) { | 576 for (uint32_t j = 0; j < m_nOutputs; j++) { |
577 uint32_t sample = | 577 uint32_t sample = |
578 GetBits32(pSampleData, bitpos.ValueOrDie() + j * m_nBitsPerSample, | 578 GetBits32(pSampleData, bitpos.ValueOrDie() + j * m_nBitsPerSample, |
579 m_nBitsPerSample); | 579 m_nBitsPerSample); |
580 FX_FLOAT encoded = (FX_FLOAT)sample; | 580 FX_FLOAT encoded = (FX_FLOAT)sample; |
581 for (uint32_t i = 0; i < m_nInputs; i++) { | 581 for (uint32_t i = 0; i < m_nInputs; i++) { |
582 if (index[i] == m_EncodeInfo[i].sizes - 1) { | 582 if (index[i] == m_EncodeInfo[i].sizes - 1) { |
583 if (index[i] == 0) | 583 if (index[i] == 0) |
584 encoded = encoded_input[i] * (FX_FLOAT)sample; | 584 encoded = encoded_input[i] * (FX_FLOAT)sample; |
585 } else { | 585 } else { |
586 FX_SAFE_INT32 bitpos2 = blocksize[i]; | 586 FX_SAFE_INT32 bitpos2 = blocksize[i]; |
587 bitpos2 += pos; | 587 bitpos2 += pos; |
588 bitpos2 *= m_nOutputs; | 588 bitpos2 *= m_nOutputs; |
589 bitpos2 += j; | 589 bitpos2 += j; |
590 bitpos2 *= m_nBitsPerSample; | 590 bitpos2 *= m_nBitsPerSample; |
591 if (!bitpos2.IsValid()) | 591 if (!bitpos2.IsValid()) |
592 return FALSE; | 592 return false; |
593 uint32_t sample1 = | 593 uint32_t sample1 = |
594 GetBits32(pSampleData, bitpos2.ValueOrDie(), m_nBitsPerSample); | 594 GetBits32(pSampleData, bitpos2.ValueOrDie(), m_nBitsPerSample); |
595 encoded += (encoded_input[i] - index[i]) * | 595 encoded += (encoded_input[i] - index[i]) * |
596 ((FX_FLOAT)sample1 - (FX_FLOAT)sample); | 596 ((FX_FLOAT)sample1 - (FX_FLOAT)sample); |
597 } | 597 } |
598 } | 598 } |
599 results[j] = | 599 results[j] = |
600 PDF_Interpolate(encoded, 0, (FX_FLOAT)m_SampleMax, | 600 PDF_Interpolate(encoded, 0, (FX_FLOAT)m_SampleMax, |
601 m_DecodeInfo[j].decode_min, m_DecodeInfo[j].decode_max); | 601 m_DecodeInfo[j].decode_min, m_DecodeInfo[j].decode_max); |
602 } | 602 } |
603 return TRUE; | 603 return true; |
604 } | 604 } |
605 | 605 |
606 CPDF_ExpIntFunc::CPDF_ExpIntFunc() | 606 CPDF_ExpIntFunc::CPDF_ExpIntFunc() |
607 : CPDF_Function(Type::kType2ExpotentialInterpolation), | 607 : CPDF_Function(Type::kType2ExpotentialInterpolation), |
608 m_pBeginValues(nullptr), | 608 m_pBeginValues(nullptr), |
609 m_pEndValues(nullptr) {} | 609 m_pEndValues(nullptr) {} |
610 | 610 |
611 CPDF_ExpIntFunc::~CPDF_ExpIntFunc() { | 611 CPDF_ExpIntFunc::~CPDF_ExpIntFunc() { |
612 FX_Free(m_pBeginValues); | 612 FX_Free(m_pBeginValues); |
613 FX_Free(m_pEndValues); | 613 FX_Free(m_pEndValues); |
614 } | 614 } |
615 FX_BOOL CPDF_ExpIntFunc::v_Init(CPDF_Object* pObj) { | 615 bool CPDF_ExpIntFunc::v_Init(CPDF_Object* pObj) { |
616 CPDF_Dictionary* pDict = pObj->GetDict(); | 616 CPDF_Dictionary* pDict = pObj->GetDict(); |
617 if (!pDict) { | 617 if (!pDict) { |
618 return FALSE; | 618 return false; |
619 } | 619 } |
620 CPDF_Array* pArray0 = pDict->GetArrayFor("C0"); | 620 CPDF_Array* pArray0 = pDict->GetArrayFor("C0"); |
621 if (m_nOutputs == 0) { | 621 if (m_nOutputs == 0) { |
622 m_nOutputs = 1; | 622 m_nOutputs = 1; |
623 if (pArray0) { | 623 if (pArray0) { |
624 m_nOutputs = pArray0->GetCount(); | 624 m_nOutputs = pArray0->GetCount(); |
625 } | 625 } |
626 } | 626 } |
627 CPDF_Array* pArray1 = pDict->GetArrayFor("C1"); | 627 CPDF_Array* pArray1 = pDict->GetArrayFor("C1"); |
628 m_pBeginValues = FX_Alloc2D(FX_FLOAT, m_nOutputs, 2); | 628 m_pBeginValues = FX_Alloc2D(FX_FLOAT, m_nOutputs, 2); |
629 m_pEndValues = FX_Alloc2D(FX_FLOAT, m_nOutputs, 2); | 629 m_pEndValues = FX_Alloc2D(FX_FLOAT, m_nOutputs, 2); |
630 for (uint32_t i = 0; i < m_nOutputs; i++) { | 630 for (uint32_t i = 0; i < m_nOutputs; i++) { |
631 m_pBeginValues[i] = pArray0 ? pArray0->GetFloatAt(i) : 0.0f; | 631 m_pBeginValues[i] = pArray0 ? pArray0->GetFloatAt(i) : 0.0f; |
632 m_pEndValues[i] = pArray1 ? pArray1->GetFloatAt(i) : 1.0f; | 632 m_pEndValues[i] = pArray1 ? pArray1->GetFloatAt(i) : 1.0f; |
633 } | 633 } |
634 m_Exponent = pDict->GetFloatFor("N"); | 634 m_Exponent = pDict->GetFloatFor("N"); |
635 m_nOrigOutputs = m_nOutputs; | 635 m_nOrigOutputs = m_nOutputs; |
636 if (m_nOutputs && m_nInputs > INT_MAX / m_nOutputs) { | 636 if (m_nOutputs && m_nInputs > INT_MAX / m_nOutputs) { |
637 return FALSE; | 637 return false; |
638 } | 638 } |
639 m_nOutputs *= m_nInputs; | 639 m_nOutputs *= m_nInputs; |
640 return TRUE; | 640 return true; |
641 } | 641 } |
642 FX_BOOL CPDF_ExpIntFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const { | 642 bool CPDF_ExpIntFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const { |
643 for (uint32_t i = 0; i < m_nInputs; i++) | 643 for (uint32_t i = 0; i < m_nInputs; i++) |
644 for (uint32_t j = 0; j < m_nOrigOutputs; j++) { | 644 for (uint32_t j = 0; j < m_nOrigOutputs; j++) { |
645 results[i * m_nOrigOutputs + j] = | 645 results[i * m_nOrigOutputs + j] = |
646 m_pBeginValues[j] + | 646 m_pBeginValues[j] + |
647 (FX_FLOAT)FXSYS_pow(inputs[i], m_Exponent) * | 647 (FX_FLOAT)FXSYS_pow(inputs[i], m_Exponent) * |
648 (m_pEndValues[j] - m_pBeginValues[j]); | 648 (m_pEndValues[j] - m_pBeginValues[j]); |
649 } | 649 } |
650 return TRUE; | 650 return true; |
651 } | 651 } |
652 | 652 |
653 CPDF_StitchFunc::CPDF_StitchFunc() | 653 CPDF_StitchFunc::CPDF_StitchFunc() |
654 : CPDF_Function(Type::kType3Stitching), | 654 : CPDF_Function(Type::kType3Stitching), |
655 m_pBounds(nullptr), | 655 m_pBounds(nullptr), |
656 m_pEncode(nullptr) {} | 656 m_pEncode(nullptr) {} |
657 | 657 |
658 CPDF_StitchFunc::~CPDF_StitchFunc() { | 658 CPDF_StitchFunc::~CPDF_StitchFunc() { |
659 FX_Free(m_pBounds); | 659 FX_Free(m_pBounds); |
660 FX_Free(m_pEncode); | 660 FX_Free(m_pEncode); |
661 } | 661 } |
662 | 662 |
663 FX_BOOL CPDF_StitchFunc::v_Init(CPDF_Object* pObj) { | 663 bool CPDF_StitchFunc::v_Init(CPDF_Object* pObj) { |
664 CPDF_Dictionary* pDict = pObj->GetDict(); | 664 CPDF_Dictionary* pDict = pObj->GetDict(); |
665 if (!pDict) { | 665 if (!pDict) { |
666 return FALSE; | 666 return false; |
667 } | 667 } |
668 if (m_nInputs != kRequiredNumInputs) { | 668 if (m_nInputs != kRequiredNumInputs) { |
669 return FALSE; | 669 return false; |
670 } | 670 } |
671 CPDF_Array* pArray = pDict->GetArrayFor("Functions"); | 671 CPDF_Array* pArray = pDict->GetArrayFor("Functions"); |
672 if (!pArray) { | 672 if (!pArray) { |
673 return FALSE; | 673 return false; |
674 } | 674 } |
675 uint32_t nSubs = pArray->GetCount(); | 675 uint32_t nSubs = pArray->GetCount(); |
676 if (nSubs == 0) | 676 if (nSubs == 0) |
677 return FALSE; | 677 return false; |
678 m_nOutputs = 0; | 678 m_nOutputs = 0; |
679 for (uint32_t i = 0; i < nSubs; i++) { | 679 for (uint32_t i = 0; i < nSubs; i++) { |
680 CPDF_Object* pSub = pArray->GetDirectObjectAt(i); | 680 CPDF_Object* pSub = pArray->GetDirectObjectAt(i); |
681 if (pSub == pObj) | 681 if (pSub == pObj) |
682 return FALSE; | 682 return false; |
683 std::unique_ptr<CPDF_Function> pFunc(CPDF_Function::Load(pSub)); | 683 std::unique_ptr<CPDF_Function> pFunc(CPDF_Function::Load(pSub)); |
684 if (!pFunc) | 684 if (!pFunc) |
685 return FALSE; | 685 return false; |
686 // Check that the input dimensionality is 1, and that all output | 686 // Check that the input dimensionality is 1, and that all output |
687 // dimensionalities are the same. | 687 // dimensionalities are the same. |
688 if (pFunc->CountInputs() != kRequiredNumInputs) | 688 if (pFunc->CountInputs() != kRequiredNumInputs) |
689 return FALSE; | 689 return false; |
690 if (pFunc->CountOutputs() != m_nOutputs) { | 690 if (pFunc->CountOutputs() != m_nOutputs) { |
691 if (m_nOutputs) | 691 if (m_nOutputs) |
692 return FALSE; | 692 return false; |
693 | 693 |
694 m_nOutputs = pFunc->CountOutputs(); | 694 m_nOutputs = pFunc->CountOutputs(); |
695 } | 695 } |
696 | 696 |
697 m_pSubFunctions.push_back(std::move(pFunc)); | 697 m_pSubFunctions.push_back(std::move(pFunc)); |
698 } | 698 } |
699 m_pBounds = FX_Alloc(FX_FLOAT, nSubs + 1); | 699 m_pBounds = FX_Alloc(FX_FLOAT, nSubs + 1); |
700 m_pBounds[0] = m_pDomains[0]; | 700 m_pBounds[0] = m_pDomains[0]; |
701 pArray = pDict->GetArrayFor("Bounds"); | 701 pArray = pDict->GetArrayFor("Bounds"); |
702 if (!pArray) | 702 if (!pArray) |
703 return FALSE; | 703 return false; |
704 for (uint32_t i = 0; i < nSubs - 1; i++) | 704 for (uint32_t i = 0; i < nSubs - 1; i++) |
705 m_pBounds[i + 1] = pArray->GetFloatAt(i); | 705 m_pBounds[i + 1] = pArray->GetFloatAt(i); |
706 m_pBounds[nSubs] = m_pDomains[1]; | 706 m_pBounds[nSubs] = m_pDomains[1]; |
707 m_pEncode = FX_Alloc2D(FX_FLOAT, nSubs, 2); | 707 m_pEncode = FX_Alloc2D(FX_FLOAT, nSubs, 2); |
708 pArray = pDict->GetArrayFor("Encode"); | 708 pArray = pDict->GetArrayFor("Encode"); |
709 if (!pArray) | 709 if (!pArray) |
710 return FALSE; | 710 return false; |
711 | 711 |
712 for (uint32_t i = 0; i < nSubs * 2; i++) | 712 for (uint32_t i = 0; i < nSubs * 2; i++) |
713 m_pEncode[i] = pArray->GetFloatAt(i); | 713 m_pEncode[i] = pArray->GetFloatAt(i); |
714 return TRUE; | 714 return true; |
715 } | 715 } |
716 | 716 |
717 FX_BOOL CPDF_StitchFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* outputs) const { | 717 bool CPDF_StitchFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* outputs) const { |
718 FX_FLOAT input = inputs[0]; | 718 FX_FLOAT input = inputs[0]; |
719 size_t i; | 719 size_t i; |
720 for (i = 0; i < m_pSubFunctions.size() - 1; i++) { | 720 for (i = 0; i < m_pSubFunctions.size() - 1; i++) { |
721 if (input < m_pBounds[i + 1]) | 721 if (input < m_pBounds[i + 1]) |
722 break; | 722 break; |
723 } | 723 } |
724 input = PDF_Interpolate(input, m_pBounds[i], m_pBounds[i + 1], | 724 input = PDF_Interpolate(input, m_pBounds[i], m_pBounds[i + 1], |
725 m_pEncode[i * 2], m_pEncode[i * 2 + 1]); | 725 m_pEncode[i * 2], m_pEncode[i * 2 + 1]); |
726 int nresults; | 726 int nresults; |
727 m_pSubFunctions[i]->Call(&input, kRequiredNumInputs, outputs, nresults); | 727 m_pSubFunctions[i]->Call(&input, kRequiredNumInputs, outputs, nresults); |
728 return TRUE; | 728 return true; |
729 } | 729 } |
730 | 730 |
731 // static | 731 // static |
732 std::unique_ptr<CPDF_Function> CPDF_Function::Load(CPDF_Object* pFuncObj) { | 732 std::unique_ptr<CPDF_Function> CPDF_Function::Load(CPDF_Object* pFuncObj) { |
733 std::unique_ptr<CPDF_Function> pFunc; | 733 std::unique_ptr<CPDF_Function> pFunc; |
734 if (!pFuncObj) | 734 if (!pFuncObj) |
735 return pFunc; | 735 return pFunc; |
736 | 736 |
737 int iType = -1; | 737 int iType = -1; |
738 if (CPDF_Stream* pStream = pFuncObj->AsStream()) | 738 if (CPDF_Stream* pStream = pFuncObj->AsStream()) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
770 } | 770 } |
771 | 771 |
772 CPDF_Function::CPDF_Function(Type type) | 772 CPDF_Function::CPDF_Function(Type type) |
773 : m_pDomains(nullptr), m_pRanges(nullptr), m_Type(type) {} | 773 : m_pDomains(nullptr), m_pRanges(nullptr), m_Type(type) {} |
774 | 774 |
775 CPDF_Function::~CPDF_Function() { | 775 CPDF_Function::~CPDF_Function() { |
776 FX_Free(m_pDomains); | 776 FX_Free(m_pDomains); |
777 FX_Free(m_pRanges); | 777 FX_Free(m_pRanges); |
778 } | 778 } |
779 | 779 |
780 FX_BOOL CPDF_Function::Init(CPDF_Object* pObj) { | 780 bool CPDF_Function::Init(CPDF_Object* pObj) { |
781 CPDF_Stream* pStream = pObj->AsStream(); | 781 CPDF_Stream* pStream = pObj->AsStream(); |
782 CPDF_Dictionary* pDict = pStream ? pStream->GetDict() : pObj->AsDictionary(); | 782 CPDF_Dictionary* pDict = pStream ? pStream->GetDict() : pObj->AsDictionary(); |
783 | 783 |
784 CPDF_Array* pDomains = pDict->GetArrayFor("Domain"); | 784 CPDF_Array* pDomains = pDict->GetArrayFor("Domain"); |
785 if (!pDomains) | 785 if (!pDomains) |
786 return FALSE; | 786 return false; |
787 | 787 |
788 m_nInputs = pDomains->GetCount() / 2; | 788 m_nInputs = pDomains->GetCount() / 2; |
789 if (m_nInputs == 0) | 789 if (m_nInputs == 0) |
790 return FALSE; | 790 return false; |
791 | 791 |
792 m_pDomains = FX_Alloc2D(FX_FLOAT, m_nInputs, 2); | 792 m_pDomains = FX_Alloc2D(FX_FLOAT, m_nInputs, 2); |
793 for (uint32_t i = 0; i < m_nInputs * 2; i++) { | 793 for (uint32_t i = 0; i < m_nInputs * 2; i++) { |
794 m_pDomains[i] = pDomains->GetFloatAt(i); | 794 m_pDomains[i] = pDomains->GetFloatAt(i); |
795 } | 795 } |
796 CPDF_Array* pRanges = pDict->GetArrayFor("Range"); | 796 CPDF_Array* pRanges = pDict->GetArrayFor("Range"); |
797 m_nOutputs = 0; | 797 m_nOutputs = 0; |
798 if (pRanges) { | 798 if (pRanges) { |
799 m_nOutputs = pRanges->GetCount() / 2; | 799 m_nOutputs = pRanges->GetCount() / 2; |
800 m_pRanges = FX_Alloc2D(FX_FLOAT, m_nOutputs, 2); | 800 m_pRanges = FX_Alloc2D(FX_FLOAT, m_nOutputs, 2); |
801 for (uint32_t i = 0; i < m_nOutputs * 2; i++) | 801 for (uint32_t i = 0; i < m_nOutputs * 2; i++) |
802 m_pRanges[i] = pRanges->GetFloatAt(i); | 802 m_pRanges[i] = pRanges->GetFloatAt(i); |
803 } | 803 } |
804 uint32_t old_outputs = m_nOutputs; | 804 uint32_t old_outputs = m_nOutputs; |
805 if (!v_Init(pObj)) | 805 if (!v_Init(pObj)) |
806 return FALSE; | 806 return false; |
807 if (m_pRanges && m_nOutputs > old_outputs) { | 807 if (m_pRanges && m_nOutputs > old_outputs) { |
808 m_pRanges = FX_Realloc(FX_FLOAT, m_pRanges, m_nOutputs * 2); | 808 m_pRanges = FX_Realloc(FX_FLOAT, m_pRanges, m_nOutputs * 2); |
809 if (m_pRanges) { | 809 if (m_pRanges) { |
810 FXSYS_memset(m_pRanges + (old_outputs * 2), 0, | 810 FXSYS_memset(m_pRanges + (old_outputs * 2), 0, |
811 sizeof(FX_FLOAT) * (m_nOutputs - old_outputs) * 2); | 811 sizeof(FX_FLOAT) * (m_nOutputs - old_outputs) * 2); |
812 } | 812 } |
813 } | 813 } |
814 return TRUE; | 814 return true; |
815 } | 815 } |
816 | 816 |
817 FX_BOOL CPDF_Function::Call(FX_FLOAT* inputs, | 817 bool CPDF_Function::Call(FX_FLOAT* inputs, |
818 uint32_t ninputs, | 818 uint32_t ninputs, |
819 FX_FLOAT* results, | 819 FX_FLOAT* results, |
820 int& nresults) const { | 820 int& nresults) const { |
821 if (m_nInputs != ninputs) { | 821 if (m_nInputs != ninputs) { |
822 return FALSE; | 822 return false; |
823 } | 823 } |
824 nresults = m_nOutputs; | 824 nresults = m_nOutputs; |
825 for (uint32_t i = 0; i < m_nInputs; i++) { | 825 for (uint32_t i = 0; i < m_nInputs; i++) { |
826 if (inputs[i] < m_pDomains[i * 2]) | 826 if (inputs[i] < m_pDomains[i * 2]) |
827 inputs[i] = m_pDomains[i * 2]; | 827 inputs[i] = m_pDomains[i * 2]; |
828 else if (inputs[i] > m_pDomains[i * 2 + 1]) | 828 else if (inputs[i] > m_pDomains[i * 2 + 1]) |
829 inputs[i] = m_pDomains[i * 2] + 1; | 829 inputs[i] = m_pDomains[i * 2] + 1; |
830 } | 830 } |
831 v_Call(inputs, results); | 831 v_Call(inputs, results); |
832 if (m_pRanges) { | 832 if (m_pRanges) { |
833 for (uint32_t i = 0; i < m_nOutputs; i++) { | 833 for (uint32_t i = 0; i < m_nOutputs; i++) { |
834 if (results[i] < m_pRanges[i * 2]) | 834 if (results[i] < m_pRanges[i * 2]) |
835 results[i] = m_pRanges[i * 2]; | 835 results[i] = m_pRanges[i * 2]; |
836 else if (results[i] > m_pRanges[i * 2 + 1]) | 836 else if (results[i] > m_pRanges[i * 2 + 1]) |
837 results[i] = m_pRanges[i * 2 + 1]; | 837 results[i] = m_pRanges[i * 2 + 1]; |
838 } | 838 } |
839 } | 839 } |
840 return TRUE; | 840 return true; |
841 } | 841 } |
842 | 842 |
843 const CPDF_SampledFunc* CPDF_Function::ToSampledFunc() const { | 843 const CPDF_SampledFunc* CPDF_Function::ToSampledFunc() const { |
844 return m_Type == Type::kType0Sampled | 844 return m_Type == Type::kType0Sampled |
845 ? static_cast<const CPDF_SampledFunc*>(this) | 845 ? static_cast<const CPDF_SampledFunc*>(this) |
846 : nullptr; | 846 : nullptr; |
847 } | 847 } |
848 | 848 |
849 const CPDF_ExpIntFunc* CPDF_Function::ToExpIntFunc() const { | 849 const CPDF_ExpIntFunc* CPDF_Function::ToExpIntFunc() const { |
850 return m_Type == Type::kType2ExpotentialInterpolation | 850 return m_Type == Type::kType2ExpotentialInterpolation |
851 ? static_cast<const CPDF_ExpIntFunc*>(this) | 851 ? static_cast<const CPDF_ExpIntFunc*>(this) |
852 : nullptr; | 852 : nullptr; |
853 } | 853 } |
854 | 854 |
855 const CPDF_StitchFunc* CPDF_Function::ToStitchFunc() const { | 855 const CPDF_StitchFunc* CPDF_Function::ToStitchFunc() const { |
856 return m_Type == Type::kType3Stitching | 856 return m_Type == Type::kType3Stitching |
857 ? static_cast<const CPDF_StitchFunc*>(this) | 857 ? static_cast<const CPDF_StitchFunc*>(this) |
858 : nullptr; | 858 : nullptr; |
859 } | 859 } |
OLD | NEW |