• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 PDFium Authors. All rights reserved.
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 <array>
11 #include <memory>
12 #include <set>
13 #include <vector>
14 
15 #include "core/fpdfapi/page/cpdf_pattern.h"
16 #include "core/fxcrt/fx_string.h"
17 #include "core/fxcrt/fx_system.h"
18 #include "core/fxcrt/observed_ptr.h"
19 #include "core/fxcrt/retain_ptr.h"
20 #include "core/fxcrt/unowned_ptr.h"
21 #include "third_party/base/span.h"
22 
23 #define PDFCS_DEVICEGRAY 1
24 #define PDFCS_DEVICERGB 2
25 #define PDFCS_DEVICECMYK 3
26 #define PDFCS_CALGRAY 4
27 #define PDFCS_CALRGB 5
28 #define PDFCS_LAB 6
29 #define PDFCS_ICCBASED 7
30 #define PDFCS_SEPARATION 8
31 #define PDFCS_DEVICEN 9
32 #define PDFCS_INDEXED 10
33 #define PDFCS_PATTERN 11
34 
35 class CPDF_Array;
36 class CPDF_Document;
37 class CPDF_Object;
38 class CPDF_PatternCS;
39 
40 constexpr size_t kMaxPatternColorComps = 16;
41 
42 class PatternValue {
43  public:
44   PatternValue();
45   PatternValue(const PatternValue& that);
46   ~PatternValue();
47 
48   void SetComps(pdfium::span<const float> comps);
GetComps()49   pdfium::span<const float> GetComps() const {
50     // TODO(tsepez): update span.h from base for implicit std::array ctor.
51     return {m_Comps.data(), m_Comps.size()};
52   }
53 
GetPattern()54   CPDF_Pattern* GetPattern() const { return m_pRetainedPattern.Get(); }
SetPattern(const RetainPtr<CPDF_Pattern> & pPattern)55   void SetPattern(const RetainPtr<CPDF_Pattern>& pPattern) {
56     m_pRetainedPattern = pPattern;
57   }
58 
59  private:
60   RetainPtr<CPDF_Pattern> m_pRetainedPattern;
61   std::array<float, kMaxPatternColorComps> m_Comps;
62 };
63 
64 class CPDF_ColorSpace : public Retainable, public Observable {
65  public:
66   static RetainPtr<CPDF_ColorSpace> GetStockCS(int Family);
67   static RetainPtr<CPDF_ColorSpace> ColorspaceFromName(const ByteString& name);
68   static RetainPtr<CPDF_ColorSpace> Load(CPDF_Document* pDoc,
69                                          CPDF_Object* pObj);
70   static RetainPtr<CPDF_ColorSpace> Load(
71       CPDF_Document* pDoc,
72       const CPDF_Object* pObj,
73       std::set<const CPDF_Object*>* pVisited);
74   static uint32_t ComponentsForFamily(int family);
75   static bool IsValidIccComponents(int components);
76 
GetArray()77   const CPDF_Array* GetArray() const { return m_pArray.Get(); }
GetDocument()78   CPDF_Document* GetDocument() const { return m_pDocument.Get(); }
79 
80   // Should only be called if this colorspace is not a pattern.
81   std::vector<float> CreateBufAndSetDefaultColor() const;
82 
83   uint32_t CountComponents() const;
GetFamily()84   int GetFamily() const { return m_Family; }
IsSpecial()85   bool IsSpecial() const {
86     return GetFamily() == PDFCS_SEPARATION || GetFamily() == PDFCS_DEVICEN ||
87            GetFamily() == PDFCS_INDEXED || GetFamily() == PDFCS_PATTERN;
88   }
89 
90   virtual bool GetRGB(const float* pBuf,
91                       float* R,
92                       float* G,
93                       float* B) const = 0;
94 
95   virtual void GetDefaultValue(int iComponent,
96                                float* value,
97                                float* min,
98                                float* max) const;
99   virtual void TranslateImageLine(uint8_t* dest_buf,
100                                   const uint8_t* src_buf,
101                                   int pixels,
102                                   int image_width,
103                                   int image_height,
104                                   bool bTransMask) const;
105   virtual void EnableStdConversion(bool bEnabled);
106   virtual bool IsNormal() const;
107 
108   // Returns |this| as a CPDF_PatternCS* if |this| is a pattern.
109   virtual CPDF_PatternCS* AsPatternCS();
110   virtual const CPDF_PatternCS* AsPatternCS() const;
111 
112   // Use instead of GetRGB() for patterns.
113   virtual bool GetPatternRGB(const PatternValue& value,
114                              float* R,
115                              float* G,
116                              float* B) const;
117 
118  protected:
119   CPDF_ColorSpace(CPDF_Document* pDoc, int family);
120   ~CPDF_ColorSpace() override;
121 
122   // Returns the number of components, or 0 on failure.
123   virtual uint32_t v_Load(CPDF_Document* pDoc,
124                           const CPDF_Array* pArray,
125                           std::set<const CPDF_Object*>* pVisited) = 0;
126 
127   // Stock colorspaces are not loaded normally. This initializes their
128   // components count.
129   void SetComponentsForStockCS(uint32_t nComponents);
130 
131   UnownedPtr<CPDF_Document> const m_pDocument;
132   RetainPtr<const CPDF_Array> m_pArray;
133   const int m_Family;
134   uint32_t m_dwStdConversion = 0;
135 
136  private:
137   uint32_t m_nComponents = 0;
138 };
139 
140 #endif  // CORE_FPDFAPI_PAGE_CPDF_COLORSPACE_H_
141