1 // Copyright 2016 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_COLORSPACE_H_ 8 #define CORE_FPDFAPI_PAGE_CPDF_COLORSPACE_H_ 9 10 #include <stddef.h> 11 #include <stdint.h> 12 13 #include <array> 14 #include <set> 15 #include <utility> 16 #include <vector> 17 18 #include "core/fpdfapi/page/cpdf_pattern.h" 19 #include "core/fpdfapi/parser/cpdf_array.h" 20 #include "core/fpdfapi/parser/cpdf_object.h" 21 #include "core/fxcrt/bytestring.h" 22 #include "core/fxcrt/observed_ptr.h" 23 #include "core/fxcrt/retain_ptr.h" 24 #include "core/fxcrt/unowned_ptr.h" 25 #include "third_party/base/span.h" 26 27 class CPDF_Document; 28 class CPDF_IndexedCS; 29 class CPDF_PatternCS; 30 31 constexpr size_t kMaxPatternColorComps = 16; 32 33 class PatternValue { 34 public: 35 PatternValue(); 36 PatternValue(const PatternValue& that); 37 ~PatternValue(); 38 39 void SetComps(pdfium::span<const float> comps); GetComps()40 pdfium::span<const float> GetComps() const { 41 // TODO(tsepez): update span.h from base for implicit std::array ctor. 42 return {m_Comps.data(), m_Comps.size()}; 43 } 44 GetPattern()45 RetainPtr<CPDF_Pattern> GetPattern() const { return m_pRetainedPattern; } SetPattern(RetainPtr<CPDF_Pattern> pPattern)46 void SetPattern(RetainPtr<CPDF_Pattern> pPattern) { 47 m_pRetainedPattern = std::move(pPattern); 48 } 49 50 private: 51 RetainPtr<CPDF_Pattern> m_pRetainedPattern; 52 std::array<float, kMaxPatternColorComps> m_Comps{}; 53 }; 54 55 class CPDF_ColorSpace : public Retainable, public Observable { 56 public: 57 enum class Family { 58 kUnknown = 0, 59 kDeviceGray = 1, 60 kDeviceRGB = 2, 61 kDeviceCMYK = 3, 62 kCalGray = 4, 63 kCalRGB = 5, 64 kLab = 6, 65 kICCBased = 7, 66 kSeparation = 8, 67 kDeviceN = 9, 68 kIndexed = 10, 69 kPattern = 11, 70 }; 71 72 static RetainPtr<CPDF_ColorSpace> GetStockCS(Family family); 73 static RetainPtr<CPDF_ColorSpace> GetStockCSForName(const ByteString& name); 74 static RetainPtr<CPDF_ColorSpace> Load( 75 CPDF_Document* pDoc, 76 const CPDF_Object* pObj, 77 std::set<const CPDF_Object*>* pVisited); 78 79 static RetainPtr<CPDF_ColorSpace> AllocateColorSpaceForID( 80 CPDF_Document* pDocument, 81 uint32_t family_id); 82 83 static uint32_t ComponentsForFamily(Family family); 84 static bool IsValidIccComponents(int components); 85 86 // Should only be called if this colorspace is not a pattern. 87 std::vector<float> CreateBufAndSetDefaultColor() const; 88 89 uint32_t CountComponents() const; GetFamily()90 Family GetFamily() const { return m_Family; } IsSpecial()91 bool IsSpecial() const { 92 return GetFamily() == Family::kSeparation || 93 GetFamily() == Family::kDeviceN || GetFamily() == Family::kIndexed || 94 GetFamily() == Family::kPattern; 95 } 96 97 // Use CPDF_Pattern::GetPatternRGB() instead of GetRGB() for patterns. 98 virtual bool GetRGB(pdfium::span<const float> pBuf, 99 float* R, 100 float* G, 101 float* B) const = 0; 102 103 virtual void GetDefaultValue(int iComponent, 104 float* value, 105 float* min, 106 float* max) const; 107 108 virtual void TranslateImageLine(pdfium::span<uint8_t> dest_span, 109 pdfium::span<const uint8_t> src_span, 110 int pixels, 111 int image_width, 112 int image_height, 113 bool bTransMask) const; 114 virtual void EnableStdConversion(bool bEnabled); 115 virtual bool IsNormal() const; 116 117 // Returns `this` as a CPDF_PatternCS* if `this` is a pattern. 118 virtual const CPDF_PatternCS* AsPatternCS() const; 119 120 // Returns `this` as a CPDF_IndexedCS* if `this` is indexed. 121 virtual const CPDF_IndexedCS* AsIndexedCS() const; 122 123 protected: 124 explicit CPDF_ColorSpace(Family family); 125 ~CPDF_ColorSpace() override; 126 127 // Returns the number of components, or 0 on failure. 128 virtual uint32_t v_Load(CPDF_Document* pDoc, 129 const CPDF_Array* pArray, 130 std::set<const CPDF_Object*>* pVisited) = 0; 131 132 // Stock colorspaces are not loaded normally. This initializes their 133 // components count. 134 void SetComponentsForStockCS(uint32_t nComponents); 135 IsStdConversionEnabled()136 bool IsStdConversionEnabled() const { return m_dwStdConversion != 0; } HasSameArray(const CPDF_Object * pObj)137 bool HasSameArray(const CPDF_Object* pObj) const { return m_pArray == pObj; } 138 139 private: 140 friend class CPDF_CalGray_TranslateImageLine_Test; 141 friend class CPDF_CalRGB_TranslateImageLine_Test; 142 143 static RetainPtr<CPDF_ColorSpace> AllocateColorSpace( 144 ByteStringView bsFamilyName); 145 146 const Family m_Family; 147 uint32_t m_dwStdConversion = 0; 148 uint32_t m_nComponents = 0; 149 RetainPtr<const CPDF_Array> m_pArray; 150 }; 151 152 #endif // CORE_FPDFAPI_PAGE_CPDF_COLORSPACE_H_ 153