• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The PDFium Authors
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/page/cpdf_expintfunc.h"
8 
9 #include <math.h>
10 
11 #include "core/fpdfapi/parser/cpdf_array.h"
12 #include "core/fpdfapi/parser/cpdf_dictionary.h"
13 #include "core/fpdfapi/parser/cpdf_number.h"
14 #include "core/fxcrt/data_vector.h"
15 #include "core/fxcrt/fx_2d_size.h"
16 #include "core/fxcrt/fx_safe_types.h"
17 #include "core/fxcrt/stl_util.h"
18 
CPDF_ExpIntFunc()19 CPDF_ExpIntFunc::CPDF_ExpIntFunc()
20     : CPDF_Function(Type::kType2ExponentialInterpolation) {}
21 
22 CPDF_ExpIntFunc::~CPDF_ExpIntFunc() = default;
23 
v_Init(const CPDF_Object * pObj,VisitedSet * pVisited)24 bool CPDF_ExpIntFunc::v_Init(const CPDF_Object* pObj, VisitedSet* pVisited) {
25   CHECK(pObj->IsDictionary() || pObj->IsStream());
26   RetainPtr<const CPDF_Dictionary> pDict = pObj->GetDict();
27   RetainPtr<const CPDF_Number> pExponent = pDict->GetNumberFor("N");
28   if (!pExponent)
29     return false;
30 
31   m_Exponent = pExponent->GetNumber();
32 
33   RetainPtr<const CPDF_Array> pArray0 = pDict->GetArrayFor("C0");
34   if (pArray0 && m_nOutputs == 0)
35     m_nOutputs = fxcrt::CollectionSize<uint32_t>(*pArray0);
36   if (m_nOutputs == 0)
37     m_nOutputs = 1;
38 
39   RetainPtr<const CPDF_Array> pArray1 = pDict->GetArrayFor("C1");
40   m_BeginValues = DataVector<float>(Fx2DSizeOrDie(m_nOutputs, 2));
41   m_EndValues = DataVector<float>(m_BeginValues.size());
42   for (uint32_t i = 0; i < m_nOutputs; i++) {
43     m_BeginValues[i] = pArray0 ? pArray0->GetFloatAt(i) : 0.0f;
44     m_EndValues[i] = pArray1 ? pArray1->GetFloatAt(i) : 1.0f;
45   }
46 
47   FX_SAFE_UINT32 nOutputs = m_nOutputs;
48   nOutputs *= m_nInputs;
49   if (!nOutputs.IsValid())
50     return false;
51 
52   m_nOrigOutputs = m_nOutputs;
53   m_nOutputs = nOutputs.ValueOrDie();
54   return true;
55 }
56 
v_Call(pdfium::span<const float> inputs,pdfium::span<float> results) const57 bool CPDF_ExpIntFunc::v_Call(pdfium::span<const float> inputs,
58                              pdfium::span<float> results) const {
59   for (uint32_t i = 0; i < m_nInputs; i++) {
60     for (uint32_t j = 0; j < m_nOrigOutputs; j++) {
61       results[i * m_nOrigOutputs + j] =
62           m_BeginValues[j] +
63           powf(inputs[i], m_Exponent) * (m_EndValues[j] - m_BeginValues[j]);
64     }
65   }
66   return true;
67 }
68