• 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_function.h"
8 
9 #include <utility>
10 #include <vector>
11 
12 #include "core/fpdfapi/page/cpdf_expintfunc.h"
13 #include "core/fpdfapi/page/cpdf_psfunc.h"
14 #include "core/fpdfapi/page/cpdf_sampledfunc.h"
15 #include "core/fpdfapi/page/cpdf_stitchfunc.h"
16 #include "core/fpdfapi/parser/cpdf_array.h"
17 #include "core/fpdfapi/parser/cpdf_dictionary.h"
18 #include "core/fpdfapi/parser/cpdf_stream.h"
19 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
20 #include "core/fxcrt/fx_safe_types.h"
21 #include "core/fxcrt/scoped_set_insertion.h"
22 #include "core/fxcrt/stl_util.h"
23 #include "third_party/base/containers/contains.h"
24 #include "third_party/base/cxx17_backports.h"
25 
26 namespace {
27 
IntegerToFunctionType(int iType)28 CPDF_Function::Type IntegerToFunctionType(int iType) {
29   switch (iType) {
30     case 0:
31     case 2:
32     case 3:
33     case 4:
34       return static_cast<CPDF_Function::Type>(iType);
35     default:
36       return CPDF_Function::Type::kTypeInvalid;
37   }
38 }
39 
40 }  // namespace
41 
42 // static
Load(RetainPtr<const CPDF_Object> pFuncObj)43 std::unique_ptr<CPDF_Function> CPDF_Function::Load(
44     RetainPtr<const CPDF_Object> pFuncObj) {
45   VisitedSet visited;
46   return Load(std::move(pFuncObj), &visited);
47 }
48 
49 // static
Load(RetainPtr<const CPDF_Object> pFuncObj,VisitedSet * pVisited)50 std::unique_ptr<CPDF_Function> CPDF_Function::Load(
51     RetainPtr<const CPDF_Object> pFuncObj,
52     VisitedSet* pVisited) {
53   if (!pFuncObj)
54     return nullptr;
55 
56   if (pdfium::Contains(*pVisited, pFuncObj))
57     return nullptr;
58 
59   ScopedSetInsertion<VisitedSet::value_type> insertion(pVisited, pFuncObj);
60 
61   int iType = -1;
62   if (const CPDF_Stream* pStream = pFuncObj->AsStream())
63     iType = pStream->GetDict()->GetIntegerFor("FunctionType");
64   else if (const CPDF_Dictionary* pDict = pFuncObj->AsDictionary())
65     iType = pDict->GetIntegerFor("FunctionType");
66 
67   std::unique_ptr<CPDF_Function> pFunc;
68   Type type = IntegerToFunctionType(iType);
69   if (type == Type::kType0Sampled)
70     pFunc = std::make_unique<CPDF_SampledFunc>();
71   else if (type == Type::kType2ExponentialInterpolation)
72     pFunc = std::make_unique<CPDF_ExpIntFunc>();
73   else if (type == Type::kType3Stitching)
74     pFunc = std::make_unique<CPDF_StitchFunc>();
75   else if (type == Type::kType4PostScript)
76     pFunc = std::make_unique<CPDF_PSFunc>();
77 
78   if (!pFunc || !pFunc->Init(pFuncObj, pVisited))
79     return nullptr;
80 
81   return pFunc;
82 }
83 
CPDF_Function(Type type)84 CPDF_Function::CPDF_Function(Type type) : m_Type(type) {}
85 
86 CPDF_Function::~CPDF_Function() = default;
87 
Init(const CPDF_Object * pObj,VisitedSet * pVisited)88 bool CPDF_Function::Init(const CPDF_Object* pObj, VisitedSet* pVisited) {
89   const CPDF_Stream* pStream = pObj->AsStream();
90   RetainPtr<const CPDF_Dictionary> pDict =
91       pStream ? pStream->GetDict() : pdfium::WrapRetain(pObj->AsDictionary());
92 
93   RetainPtr<const CPDF_Array> pDomains = pDict->GetArrayFor("Domain");
94   if (!pDomains)
95     return false;
96 
97   m_nInputs = fxcrt::CollectionSize<uint32_t>(*pDomains) / 2;
98   if (m_nInputs == 0)
99     return false;
100 
101   size_t nInputs = m_nInputs * 2;
102   m_Domains = ReadArrayElementsToVector(pDomains.Get(), nInputs);
103 
104   RetainPtr<const CPDF_Array> pRanges = pDict->GetArrayFor("Range");
105   m_nOutputs = pRanges ? fxcrt::CollectionSize<uint32_t>(*pRanges) / 2 : 0;
106 
107   // Ranges are required for type 0 and type 4 functions. A non-zero
108   // |m_nOutputs| here implied Ranges meets the requirements.
109   bool bRangeRequired =
110       m_Type == Type::kType0Sampled || m_Type == Type::kType4PostScript;
111   if (bRangeRequired && m_nOutputs == 0)
112     return false;
113 
114   if (m_nOutputs > 0) {
115     size_t nOutputs = m_nOutputs * 2;
116     m_Ranges = ReadArrayElementsToVector(pRanges.Get(), nOutputs);
117   }
118 
119   uint32_t old_outputs = m_nOutputs;
120   if (!v_Init(pObj, pVisited))
121     return false;
122 
123   if (!m_Ranges.empty() && m_nOutputs > old_outputs) {
124     FX_SAFE_SIZE_T nOutputs = m_nOutputs;
125     nOutputs *= 2;
126     m_Ranges.resize(nOutputs.ValueOrDie());
127   }
128   return true;
129 }
130 
Call(pdfium::span<const float> inputs,pdfium::span<float> results) const131 absl::optional<uint32_t> CPDF_Function::Call(
132     pdfium::span<const float> inputs,
133     pdfium::span<float> results) const {
134   if (m_nInputs != inputs.size())
135     return absl::nullopt;
136 
137   std::vector<float> clamped_inputs(m_nInputs);
138   for (uint32_t i = 0; i < m_nInputs; i++) {
139     float domain1 = m_Domains[i * 2];
140     float domain2 = m_Domains[i * 2 + 1];
141     if (domain1 > domain2)
142       return absl::nullopt;
143 
144     clamped_inputs[i] = pdfium::clamp(inputs[i], domain1, domain2);
145   }
146   if (!v_Call(clamped_inputs, results))
147     return absl::nullopt;
148 
149   if (m_Ranges.empty())
150     return m_nOutputs;
151 
152   for (uint32_t i = 0; i < m_nOutputs; i++) {
153     float range1 = m_Ranges[i * 2];
154     float range2 = m_Ranges[i * 2 + 1];
155     if (range1 > range2)
156       return absl::nullopt;
157 
158     results[i] = pdfium::clamp(results[i], range1, range2);
159   }
160   return m_nOutputs;
161 }
162 
163 // See PDF Reference 1.7, page 170.
Interpolate(float x,float xmin,float xmax,float ymin,float ymax) const164 float CPDF_Function::Interpolate(float x,
165                                  float xmin,
166                                  float xmax,
167                                  float ymin,
168                                  float ymax) const {
169   float divisor = xmax - xmin;
170   return ymin + (divisor ? (x - xmin) * (ymax - ymin) / divisor : 0);
171 }
172 
173 #if defined(_SKIA_SUPPORT_)
ToSampledFunc() const174 const CPDF_SampledFunc* CPDF_Function::ToSampledFunc() const {
175   return m_Type == Type::kType0Sampled
176              ? static_cast<const CPDF_SampledFunc*>(this)
177              : nullptr;
178 }
179 
ToExpIntFunc() const180 const CPDF_ExpIntFunc* CPDF_Function::ToExpIntFunc() const {
181   return m_Type == Type::kType2ExponentialInterpolation
182              ? static_cast<const CPDF_ExpIntFunc*>(this)
183              : nullptr;
184 }
185 
ToStitchFunc() const186 const CPDF_StitchFunc* CPDF_Function::ToStitchFunc() const {
187   return m_Type == Type::kType3Stitching
188              ? static_cast<const CPDF_StitchFunc*>(this)
189              : nullptr;
190 }
191 #endif  // defined(_SKIA_SUPPORT_)
192