• 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 <algorithm>
10 #include <utility>
11 #include <vector>
12 
13 #include "core/fpdfapi/page/cpdf_expintfunc.h"
14 #include "core/fpdfapi/page/cpdf_psfunc.h"
15 #include "core/fpdfapi/page/cpdf_sampledfunc.h"
16 #include "core/fpdfapi/page/cpdf_stitchfunc.h"
17 #include "core/fpdfapi/parser/cpdf_array.h"
18 #include "core/fpdfapi/parser/cpdf_dictionary.h"
19 #include "core/fpdfapi/parser/cpdf_stream.h"
20 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
21 #include "core/fxcrt/containers/contains.h"
22 #include "core/fxcrt/fx_safe_types.h"
23 #include "core/fxcrt/scoped_set_insertion.h"
24 #include "core/fxcrt/stl_util.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 std::optional<uint32_t> CPDF_Function::Call(pdfium::span<const float> inputs,
132                                             pdfium::span<float> results) const {
133   if (m_nInputs != inputs.size())
134     return std::nullopt;
135 
136   std::vector<float> clamped_inputs(m_nInputs);
137   for (uint32_t i = 0; i < m_nInputs; i++) {
138     float domain1 = m_Domains[i * 2];
139     float domain2 = m_Domains[i * 2 + 1];
140     if (domain1 > domain2)
141       return std::nullopt;
142 
143     clamped_inputs[i] = std::clamp(inputs[i], domain1, domain2);
144   }
145   if (!v_Call(clamped_inputs, results))
146     return std::nullopt;
147 
148   if (m_Ranges.empty())
149     return m_nOutputs;
150 
151   for (uint32_t i = 0; i < m_nOutputs; i++) {
152     float range1 = m_Ranges[i * 2];
153     float range2 = m_Ranges[i * 2 + 1];
154     if (range1 > range2)
155       return std::nullopt;
156 
157     results[i] = std::clamp(results[i], range1, range2);
158   }
159   return m_nOutputs;
160 }
161 
162 // See PDF Reference 1.7, page 170.
Interpolate(float x,float xmin,float xmax,float ymin,float ymax) const163 float CPDF_Function::Interpolate(float x,
164                                  float xmin,
165                                  float xmax,
166                                  float ymin,
167                                  float ymax) const {
168   float divisor = xmax - xmin;
169   return ymin + (divisor ? (x - xmin) * (ymax - ymin) / divisor : 0);
170 }
171 
172 #if defined(PDF_USE_SKIA)
ToSampledFunc() const173 const CPDF_SampledFunc* CPDF_Function::ToSampledFunc() const {
174   return m_Type == Type::kType0Sampled
175              ? static_cast<const CPDF_SampledFunc*>(this)
176              : nullptr;
177 }
178 
ToExpIntFunc() const179 const CPDF_ExpIntFunc* CPDF_Function::ToExpIntFunc() const {
180   return m_Type == Type::kType2ExponentialInterpolation
181              ? static_cast<const CPDF_ExpIntFunc*>(this)
182              : nullptr;
183 }
184 
ToStitchFunc() const185 const CPDF_StitchFunc* CPDF_Function::ToStitchFunc() const {
186   return m_Type == Type::kType3Stitching
187              ? static_cast<const CPDF_StitchFunc*>(this)
188              : nullptr;
189 }
190 #endif  // defined(PDF_USE_SKIA)
191