xref: /aosp_15_r20/external/pdfium/core/fpdfapi/page/cpdf_stitchfunc.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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   RetainPtr<const CPDF_Dictionary> pDict = pObj->GetDict();
32   if (!pDict)
33     return false;
34 
35   RetainPtr<const CPDF_Array> pFunctionsArray = pDict->GetArrayFor("Functions");
36   if (!pFunctionsArray)
37     return false;
38 
39   RetainPtr<const CPDF_Array> pBoundsArray = pDict->GetArrayFor("Bounds");
40   if (!pBoundsArray)
41     return false;
42 
43   RetainPtr<const CPDF_Array> pEncodeArray = pDict->GetArrayFor("Encode");
44   if (!pEncodeArray)
45     return false;
46 
47   const uint32_t nSubs = fxcrt::CollectionSize<uint32_t>(*pFunctionsArray);
48   if (nSubs == 0)
49     return false;
50 
51   // Check array sizes. The checks are slightly relaxed to allow the "Bounds"
52   // and "Encode" arrays to have more than the required number of elements.
53   {
54     if (pBoundsArray->size() < nSubs - 1)
55       return false;
56 
57     FX_SAFE_UINT32 nExpectedEncodeSize = nSubs;
58     nExpectedEncodeSize *= 2;
59     if (!nExpectedEncodeSize.IsValid())
60       return false;
61 
62     if (pEncodeArray->size() < nExpectedEncodeSize.ValueOrDie())
63       return false;
64   }
65 
66   // Check sub-functions.
67   {
68     absl::optional<uint32_t> nOutputs;
69     for (uint32_t i = 0; i < nSubs; ++i) {
70       RetainPtr<const CPDF_Object> pSub = pFunctionsArray->GetDirectObjectAt(i);
71       if (pSub == pObj)
72         return false;
73 
74       std::unique_ptr<CPDF_Function> pFunc =
75           CPDF_Function::Load(std::move(pSub), pVisited);
76       if (!pFunc)
77         return false;
78 
79       // Check that the input dimensionality is 1, and that all output
80       // dimensionalities are the same.
81       if (pFunc->CountInputs() != kRequiredNumInputs)
82         return false;
83 
84       uint32_t nFuncOutputs = pFunc->CountOutputs();
85       if (nFuncOutputs == 0)
86         return false;
87 
88       if (nOutputs.has_value()) {
89         if (nOutputs != nFuncOutputs)
90           return false;
91       } else {
92         nOutputs = nFuncOutputs;
93       }
94       m_pSubFunctions.push_back(std::move(pFunc));
95     }
96     m_nOutputs = nOutputs.value();
97   }
98 
99   m_bounds.reserve(nSubs + 1);
100   m_bounds.push_back(m_Domains[0]);
101   for (uint32_t i = 0; i < nSubs - 1; i++)
102     m_bounds.push_back(pBoundsArray->GetFloatAt(i));
103   m_bounds.push_back(m_Domains[1]);
104 
105   m_encode = ReadArrayElementsToVector(pEncodeArray.Get(), nSubs * 2);
106   return true;
107 }
108 
v_Call(pdfium::span<const float> inputs,pdfium::span<float> results) const109 bool CPDF_StitchFunc::v_Call(pdfium::span<const float> inputs,
110                              pdfium::span<float> results) const {
111   float input = inputs[0];
112   size_t i;
113   for (i = 0; i < m_pSubFunctions.size() - 1; i++) {
114     if (input < m_bounds[i + 1])
115       break;
116   }
117   input = Interpolate(input, m_bounds[i], m_bounds[i + 1], m_encode[i * 2],
118                       m_encode[i * 2 + 1]);
119   return m_pSubFunctions[i]
120       ->Call(pdfium::make_span(&input, 1), results)
121       .has_value();
122 }
123