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_imageobject.h" 8 9 #include <utility> 10 11 #include "core/fpdfapi/page/cpdf_docpagedata.h" 12 #include "core/fpdfapi/page/cpdf_image.h" 13 #include "core/fpdfapi/parser/cpdf_stream.h" 14 #include "core/fxcrt/fx_coordinates.h" 15 #include "core/fxge/dib/cfx_dibbase.h" 16 #include "core/fxge/dib/cfx_dibitmap.h" 17 CPDF_ImageObject(int32_t content_stream)18CPDF_ImageObject::CPDF_ImageObject(int32_t content_stream) 19 : CPDF_PageObject(content_stream) {} 20 CPDF_ImageObject()21CPDF_ImageObject::CPDF_ImageObject() : CPDF_ImageObject(kNoContentStream) {} 22 ~CPDF_ImageObject()23CPDF_ImageObject::~CPDF_ImageObject() { 24 MaybePurgeCache(); 25 } 26 GetType() const27CPDF_PageObject::Type CPDF_ImageObject::GetType() const { 28 return Type::kImage; 29 } 30 Transform(const CFX_Matrix & matrix)31void CPDF_ImageObject::Transform(const CFX_Matrix& matrix) { 32 m_Matrix.Concat(matrix); 33 CalcBoundingBox(); 34 SetDirty(true); 35 } 36 IsImage() const37bool CPDF_ImageObject::IsImage() const { 38 return true; 39 } 40 AsImage()41CPDF_ImageObject* CPDF_ImageObject::AsImage() { 42 return this; 43 } 44 AsImage() const45const CPDF_ImageObject* CPDF_ImageObject::AsImage() const { 46 return this; 47 } 48 CalcBoundingBox()49void CPDF_ImageObject::CalcBoundingBox() { 50 static constexpr CFX_FloatRect kRect(0.0f, 0.0f, 1.0f, 1.0f); 51 SetOriginalRect(kRect); 52 SetRect(m_Matrix.TransformRect(kRect)); 53 } 54 SetImage(RetainPtr<CPDF_Image> pImage)55void CPDF_ImageObject::SetImage(RetainPtr<CPDF_Image> pImage) { 56 MaybePurgeCache(); 57 m_pImage = std::move(pImage); 58 } 59 GetImage() const60RetainPtr<CPDF_Image> CPDF_ImageObject::GetImage() const { 61 return m_pImage; 62 } 63 GetIndependentBitmap() const64RetainPtr<CFX_DIBitmap> CPDF_ImageObject::GetIndependentBitmap() const { 65 RetainPtr<CFX_DIBBase> pSource = GetImage()->LoadDIBBase(); 66 67 // Realize() is non-virtual, and can't be overloaded by CPDF_DIB to 68 // return a full-up CPDF_DIB subclass. Instead, it only works upon the 69 // CFX_DIBBase, which is convenient since none of its members point to 70 // objects owned by |this| or the form containing |this|. As a result, 71 // the new bitmap may outlive them, giving the "independent" property 72 // this method is named after. 73 return pSource ? pSource->Realize() : nullptr; 74 } 75 SetImageMatrix(const CFX_Matrix & matrix)76void CPDF_ImageObject::SetImageMatrix(const CFX_Matrix& matrix) { 77 m_Matrix = matrix; 78 CalcBoundingBox(); 79 } 80 MaybePurgeCache()81void CPDF_ImageObject::MaybePurgeCache() { 82 if (!m_pImage) 83 return; 84 85 RetainPtr<const CPDF_Stream> pStream = m_pImage->GetStream(); 86 if (!pStream) 87 return; 88 89 uint32_t objnum = pStream->GetObjNum(); 90 if (!objnum) 91 return; 92 93 auto* pDoc = m_pImage->GetDocument(); 94 CHECK(pDoc); 95 96 m_pImage.Reset(); // Clear my reference before asking the cache. 97 pDoc->MaybePurgeImage(objnum); 98 } 99