xref: /aosp_15_r20/external/pdfium/core/fpdfapi/page/cpdf_color.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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 #include "core/fpdfapi/page/cpdf_color.h"
8 
9 #include <utility>
10 
11 #include "core/fpdfapi/page/cpdf_patterncs.h"
12 #include "third_party/base/check.h"
13 
14 CPDF_Color::CPDF_Color() = default;
15 
CPDF_Color(const CPDF_Color & that)16 CPDF_Color::CPDF_Color(const CPDF_Color& that) {
17   *this = that;
18 }
19 
20 CPDF_Color::~CPDF_Color() = default;
21 
IsPattern() const22 bool CPDF_Color::IsPattern() const {
23   return m_pCS && IsPatternInternal();
24 }
25 
IsPatternInternal() const26 bool CPDF_Color::IsPatternInternal() const {
27   return m_pCS->GetFamily() == CPDF_ColorSpace::Family::kPattern;
28 }
29 
SetColorSpace(RetainPtr<CPDF_ColorSpace> colorspace)30 void CPDF_Color::SetColorSpace(RetainPtr<CPDF_ColorSpace> colorspace) {
31   m_pCS = std::move(colorspace);
32   if (IsPatternInternal()) {
33     m_Buffer.clear();
34     m_pValue = std::make_unique<PatternValue>();
35   } else {
36     m_Buffer = m_pCS->CreateBufAndSetDefaultColor();
37     m_pValue.reset();
38   }
39 }
40 
SetValueForNonPattern(std::vector<float> values)41 void CPDF_Color::SetValueForNonPattern(std::vector<float> values) {
42   DCHECK(!IsPatternInternal());
43   DCHECK(m_pCS->CountComponents() <= values.size());
44   m_Buffer = std::move(values);
45 }
46 
SetValueForPattern(RetainPtr<CPDF_Pattern> pattern,pdfium::span<float> values)47 void CPDF_Color::SetValueForPattern(RetainPtr<CPDF_Pattern> pattern,
48                                     pdfium::span<float> values) {
49   if (values.size() > kMaxPatternColorComps)
50     return;
51 
52   if (!IsPattern()) {
53     SetColorSpace(
54         CPDF_ColorSpace::GetStockCS(CPDF_ColorSpace::Family::kPattern));
55   }
56   m_pValue->SetPattern(std::move(pattern));
57   m_pValue->SetComps(values);
58 }
59 
operator =(const CPDF_Color & that)60 CPDF_Color& CPDF_Color::operator=(const CPDF_Color& that) {
61   if (this == &that)
62     return *this;
63 
64   m_Buffer = that.m_Buffer;
65   m_pValue =
66       that.m_pValue ? std::make_unique<PatternValue>(*that.m_pValue) : nullptr;
67   m_pCS = that.m_pCS;
68   return *this;
69 }
70 
CountComponents() const71 uint32_t CPDF_Color::CountComponents() const {
72   return m_pCS->CountComponents();
73 }
74 
IsColorSpaceRGB() const75 bool CPDF_Color::IsColorSpaceRGB() const {
76   return m_pCS ==
77          CPDF_ColorSpace::GetStockCS(CPDF_ColorSpace::Family::kDeviceRGB);
78 }
79 
GetRGB(int * R,int * G,int * B) const80 bool CPDF_Color::GetRGB(int* R, int* G, int* B) const {
81   float r = 0.0f;
82   float g = 0.0f;
83   float b = 0.0f;
84   bool result = false;
85   if (IsPatternInternal()) {
86     if (m_pValue) {
87       const CPDF_PatternCS* pPatternCS = m_pCS->AsPatternCS();
88       result = pPatternCS->GetPatternRGB(*m_pValue, &r, &g, &b);
89     }
90   } else {
91     if (!m_Buffer.empty())
92       result = m_pCS->GetRGB(m_Buffer, &r, &g, &b);
93   }
94   if (!result)
95     return false;
96 
97   *R = static_cast<int32_t>(r * 255 + 0.5f);
98   *G = static_cast<int32_t>(g * 255 + 0.5f);
99   *B = static_cast<int32_t>(b * 255 + 0.5f);
100   return true;
101 }
102 
GetPattern() const103 RetainPtr<CPDF_Pattern> CPDF_Color::GetPattern() const {
104   DCHECK(IsPattern());
105   return m_pValue ? m_pValue->GetPattern() : nullptr;
106 }
107