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 #ifndef CORE_FPDFAPI_PAGE_CPDF_FUNCTION_H_ 8 #define CORE_FPDFAPI_PAGE_CPDF_FUNCTION_H_ 9 10 #include <memory> 11 #include <optional> 12 #include <set> 13 #include <vector> 14 15 #include "core/fxcrt/retain_ptr.h" 16 #include "core/fxcrt/span.h" 17 18 class CPDF_ExpIntFunc; 19 class CPDF_Object; 20 class CPDF_SampledFunc; 21 class CPDF_StitchFunc; 22 23 class CPDF_Function { 24 public: 25 // Valid values are from ISO 32000-1:2008 spec, table 38. DO NOT CHANGE. 26 enum class Type { 27 kTypeInvalid = -1, 28 kType0Sampled = 0, 29 kType2ExponentialInterpolation = 2, 30 kType3Stitching = 3, 31 kType4PostScript = 4, 32 }; 33 34 static std::unique_ptr<CPDF_Function> Load( 35 RetainPtr<const CPDF_Object> pFuncObj); 36 37 virtual ~CPDF_Function(); 38 39 std::optional<uint32_t> Call(pdfium::span<const float> inputs, 40 pdfium::span<float> results) const; InputCount()41 uint32_t InputCount() const { return m_nInputs; } OutputCount()42 uint32_t OutputCount() const { return m_nOutputs; } GetDomain(int i)43 float GetDomain(int i) const { return m_Domains[i]; } GetRange(int i)44 float GetRange(int i) const { return m_Ranges[i]; } 45 float Interpolate(float x, 46 float xmin, 47 float xmax, 48 float ymin, 49 float ymax) const; 50 51 #if defined(PDF_USE_SKIA) 52 const CPDF_SampledFunc* ToSampledFunc() const; 53 const CPDF_ExpIntFunc* ToExpIntFunc() const; 54 const CPDF_StitchFunc* ToStitchFunc() const; 55 #endif // defined(PDF_USE_SKIA) 56 57 protected: 58 explicit CPDF_Function(Type type); 59 60 using VisitedSet = std::set<RetainPtr<const CPDF_Object>>; 61 static std::unique_ptr<CPDF_Function> Load( 62 RetainPtr<const CPDF_Object> pFuncObj, 63 VisitedSet* pVisited); 64 bool Init(const CPDF_Object* pObj, VisitedSet* pVisited); 65 // `pObj` is guaranteed to be either a dictionary or a stream. 66 virtual bool v_Init(const CPDF_Object* pObj, VisitedSet* pVisited) = 0; 67 virtual bool v_Call(pdfium::span<const float> inputs, 68 pdfium::span<float> results) const = 0; 69 70 const Type m_Type; 71 uint32_t m_nInputs = 0; 72 uint32_t m_nOutputs = 0; 73 std::vector<float> m_Domains; 74 std::vector<float> m_Ranges; 75 }; 76 77 #endif // CORE_FPDFAPI_PAGE_CPDF_FUNCTION_H_ 78