• 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_stitchfunc.h"
8 
9 #include <utility>
10 
11 #include "core/fpdfapi/parser/cpdf_array.h"
12 #include "core/fpdfapi/parser/cpdf_dictionary.h"
13 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
14 #include "core/fxcrt/fx_safe_types.h"
15 #include "core/fxcrt/stl_util.h"
16 
17 namespace {
18 
19 constexpr uint32_t kRequiredNumInputs = 1;
20 
21 }  // namespace
22 
CPDF_StitchFunc()23 CPDF_StitchFunc::CPDF_StitchFunc() : CPDF_Function(Type::kType3Stitching) {}
24 
25 CPDF_StitchFunc::~CPDF_StitchFunc() = default;
26 
v_Init(const CPDF_Object * pObj,VisitedSet * pVisited)27 bool CPDF_StitchFunc::v_Init(const CPDF_Object* pObj, VisitedSet* pVisited) {
28   if (m_nInputs != kRequiredNumInputs)
29     return false;
30 
31   CHECK(pObj->IsDictionary() || pObj->IsStream());
32   RetainPtr<const CPDF_Dictionary> pDict = pObj->GetDict();
33   RetainPtr<const CPDF_Array> pFunctionsArray = pDict->GetArrayFor("Functions");
34   if (!pFunctionsArray)
35     return false;
36 
37   RetainPtr<const CPDF_Array> pBoundsArray = pDict->GetArrayFor("Bounds");
38   if (!pBoundsArray)
39     return false;
40 
41   RetainPtr<const CPDF_Array> pEncodeArray = pDict->GetArrayFor("Encode");
42   if (!pEncodeArray)
43     return false;
44 
45   const uint32_t nSubs = fxcrt::CollectionSize<uint32_t>(*pFunctionsArray);
46   if (nSubs == 0)
47     return false;
48 
49   // Check array sizes. The checks are slightly relaxed to allow the "Bounds"
50   // and "Encode" arrays to have more than the required number of elements.
51   {
52     if (pBoundsArray->size() < nSubs - 1)
53       return false;
54 
55     FX_SAFE_UINT32 nExpectedEncodeSize = nSubs;
56     nExpectedEncodeSize *= 2;
57     if (!nExpectedEncodeSize.IsValid())
58       return false;
59 
60     if (pEncodeArray->size() < nExpectedEncodeSize.ValueOrDie())
61       return false;
62   }
63 
64   // Check sub-functions.
65   {
66     std::optional<uint32_t> nOutputs;
67     for (uint32_t i = 0; i < nSubs; ++i) {
68       RetainPtr<const CPDF_Object> pSub = pFunctionsArray->GetDirectObjectAt(i);
69       if (pSub == pObj)
70         return false;
71 
72       std::unique_ptr<CPDF_Function> pFunc =
73           CPDF_Function::Load(std::move(pSub), pVisited);
74       if (!pFunc)
75         return false;
76 
77       // Check that the input dimensionality is 1, and that all output
78       // dimensionalities are the same.
79       if (pFunc->InputCount() != kRequiredNumInputs) {
80         return false;
81       }
82 
83       uint32_t nFuncOutputs = pFunc->OutputCount();
84       if (nFuncOutputs == 0)
85         return false;
86 
87       if (nOutputs.has_value()) {
88         if (nOutputs != nFuncOutputs)
89           return false;
90       } else {
91         nOutputs = nFuncOutputs;
92       }
93       m_pSubFunctions.push_back(std::move(pFunc));
94     }
95     m_nOutputs = nOutputs.value();
96   }
97 
98   m_bounds.reserve(nSubs + 1);
99   m_bounds.push_back(m_Domains[0]);
100   for (uint32_t i = 0; i < nSubs - 1; i++)
101     m_bounds.push_back(pBoundsArray->GetFloatAt(i));
102   m_bounds.push_back(m_Domains[1]);
103 
104   m_encode = ReadArrayElementsToVector(pEncodeArray.Get(), nSubs * 2);
105   return true;
106 }
107 
v_Call(pdfium::span<const float> inputs,pdfium::span<float> results) const108 bool CPDF_StitchFunc::v_Call(pdfium::span<const float> inputs,
109                              pdfium::span<float> results) const {
110   float input = inputs[0];
111   size_t i;
112   for (i = 0; i + 1 < m_pSubFunctions.size(); i++) {
113     if (input < m_bounds[i + 1])
114       break;
115   }
116   input = Interpolate(input, m_bounds[i], m_bounds[i + 1], m_encode[i * 2],
117                       m_encode[i * 2 + 1]);
118   return m_pSubFunctions[i]
119       ->Call(pdfium::span_from_ref(input), results)
120       .has_value();
121 }
122