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 <memory>
11#include <set>
12
13#include "core/fpdfapi/page/cpdf_pattern.h"
14#include "core/fxcrt/fx_string.h"
15#include "core/fxcrt/fx_system.h"
16#include "core/fxcrt/unowned_ptr.h"
17
18#define PDFCS_DEVICEGRAY 1
19#define PDFCS_DEVICERGB 2
20#define PDFCS_DEVICECMYK 3
21#define PDFCS_CALGRAY 4
22#define PDFCS_CALRGB 5
23#define PDFCS_LAB 6
24#define PDFCS_ICCBASED 7
25#define PDFCS_SEPARATION 8
26#define PDFCS_DEVICEN 9
27#define PDFCS_INDEXED 10
28#define PDFCS_PATTERN 11
29
30class CPDF_Array;
31class CPDF_Document;
32class CPDF_Object;
33
34constexpr size_t kMaxPatternColorComps = 16;
35
36struct PatternValue {
37  CPDF_Pattern* m_pPattern;
38  CPDF_CountedPattern* m_pCountedPattern;
39  int m_nComps;
40  float m_Comps[kMaxPatternColorComps];
41};
42
43class CPDF_ColorSpace {
44 public:
45  static CPDF_ColorSpace* GetStockCS(int Family);
46  static CPDF_ColorSpace* ColorspaceFromName(const ByteString& name);
47  static std::unique_ptr<CPDF_ColorSpace> Load(CPDF_Document* pDoc,
48                                               CPDF_Object* pCSObj);
49  static std::unique_ptr<CPDF_ColorSpace> Load(
50      CPDF_Document* pDoc,
51      CPDF_Object* pCSObj,
52      std::set<CPDF_Object*>* pVisited);
53
54  void Release();
55
56  int GetBufSize() const;
57  float* CreateBuf();
58  void GetDefaultColor(float* buf) const;
59  uint32_t CountComponents() const;
60  int GetFamily() const { return m_Family; }
61  bool IsSpecial() const {
62    return GetFamily() == PDFCS_SEPARATION || GetFamily() == PDFCS_DEVICEN ||
63           GetFamily() == PDFCS_INDEXED || GetFamily() == PDFCS_PATTERN;
64  }
65
66  virtual void GetDefaultValue(int iComponent,
67                               float* value,
68                               float* min,
69                               float* max) const;
70
71  virtual bool GetRGB(float* pBuf, float* R, float* G, float* B) const = 0;
72
73  virtual void TranslateImageLine(uint8_t* dest_buf,
74                                  const uint8_t* src_buf,
75                                  int pixels,
76                                  int image_width,
77                                  int image_height,
78                                  bool bTransMask) const;
79  virtual void EnableStdConversion(bool bEnabled);
80
81  CPDF_Array* GetArray() const { return m_pArray.Get(); }
82  CPDF_Document* GetDocument() const { return m_pDocument.Get(); }
83
84 protected:
85  CPDF_ColorSpace(CPDF_Document* pDoc, int family);
86  virtual ~CPDF_ColorSpace();
87
88  // Returns the number of components, or 0 on failure.
89  virtual uint32_t v_Load(CPDF_Document* pDoc,
90                          CPDF_Array* pArray,
91                          std::set<CPDF_Object*>* pVisited) = 0;
92
93  // Stock colorspaces are not loaded normally. This initializes their
94  // components count.
95  void SetComponentsForStockCS(uint32_t nComponents);
96
97  UnownedPtr<CPDF_Document> const m_pDocument;
98  UnownedPtr<CPDF_Array> m_pArray;
99  const int m_Family;
100  uint32_t m_dwStdConversion = 0;
101
102 private:
103  uint32_t m_nComponents = 0;
104};
105using CPDF_CountedColorSpace = CPDF_CountedObject<CPDF_ColorSpace>;
106
107namespace std {
108
109// Make std::unique_ptr<CPDF_ColorSpace> call Release() rather than
110// simply deleting the object.
111template <>
112struct default_delete<CPDF_ColorSpace> {
113  void operator()(CPDF_ColorSpace* pColorSpace) const {
114    if (pColorSpace)
115      pColorSpace->Release();
116  }
117};
118
119}  // namespace std
120
121#endif  // CORE_FPDFAPI_PAGE_CPDF_COLORSPACE_H_
122