1 // Copyright 2014 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 #include "core/fpdfapi/page/cpdf_patterncs.h" 8 9 #include "core/fpdfapi/page/cpdf_docpagedata.h" 10 #include "core/fpdfapi/parser/cpdf_array.h" 11 #include "core/fpdfapi/parser/cpdf_document.h" 12 13 CPDF_PatternCS::CPDF_PatternCS(CPDF_Document* pDoc) 14 : CPDF_ColorSpace(pDoc, PDFCS_PATTERN), 15 m_pBaseCS(nullptr), 16 m_pCountedBaseCS(nullptr) {} 17 18 CPDF_PatternCS::~CPDF_PatternCS() { 19 CPDF_ColorSpace* pCS = m_pCountedBaseCS ? m_pCountedBaseCS->get() : nullptr; 20 if (pCS && m_pDocument) { 21 auto* pPageData = m_pDocument->GetPageData(); 22 if (pPageData) 23 pPageData->ReleaseColorSpace(pCS->GetArray()); 24 } 25 } 26 27 void CPDF_PatternCS::InitializeStockPattern() { 28 SetComponentsForStockCS(1); 29 } 30 31 uint32_t CPDF_PatternCS::v_Load(CPDF_Document* pDoc, 32 CPDF_Array* pArray, 33 std::set<CPDF_Object*>* pVisited) { 34 CPDF_Object* pBaseCS = pArray->GetDirectObjectAt(1); 35 if (pBaseCS == m_pArray) 36 return 0; 37 38 CPDF_DocPageData* pDocPageData = pDoc->GetPageData(); 39 m_pBaseCS = pDocPageData->GetColorSpaceGuarded(pBaseCS, nullptr, pVisited); 40 if (!m_pBaseCS) 41 return 1; 42 43 if (m_pBaseCS->GetFamily() == PDFCS_PATTERN) 44 return 0; 45 46 m_pCountedBaseCS = pDocPageData->FindColorSpacePtr(m_pBaseCS->GetArray()); 47 if (m_pBaseCS->CountComponents() > kMaxPatternColorComps) 48 return 0; 49 50 return m_pBaseCS->CountComponents() + 1; 51 } 52 53 bool CPDF_PatternCS::GetRGB(float* pBuf, float* R, float* G, float* B) const { 54 if (m_pBaseCS) { 55 ASSERT(m_pBaseCS->GetFamily() != PDFCS_PATTERN); 56 PatternValue* pvalue = reinterpret_cast<PatternValue*>(pBuf); 57 if (m_pBaseCS->GetRGB(pvalue->m_Comps, R, G, B)) 58 return true; 59 } 60 *R = 0.75f; 61 *G = 0.75f; 62 *B = 0.75f; 63 return false; 64 } 65