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 #include "core/fpdfapi/page/cpdf_imageobject.h"
8 
9 #include <memory>
10 
11 #include "core/fpdfapi/page/cpdf_docpagedata.h"
12 #include "core/fpdfapi/page/cpdf_image.h"
13 #include "core/fpdfapi/parser/cpdf_document.h"
14 
CPDF_ImageObject()15 CPDF_ImageObject::CPDF_ImageObject() {}
16 
~CPDF_ImageObject()17 CPDF_ImageObject::~CPDF_ImageObject() {
18   MaybePurgeCache();
19 }
20 
GetType() const21 CPDF_PageObject::Type CPDF_ImageObject::GetType() const {
22   return IMAGE;
23 }
24 
Transform(const CFX_Matrix & matrix)25 void CPDF_ImageObject::Transform(const CFX_Matrix& matrix) {
26   m_Matrix.Concat(matrix);
27   CalcBoundingBox();
28   SetDirty(true);
29 }
30 
IsImage() const31 bool CPDF_ImageObject::IsImage() const {
32   return true;
33 }
34 
AsImage()35 CPDF_ImageObject* CPDF_ImageObject::AsImage() {
36   return this;
37 }
38 
AsImage() const39 const CPDF_ImageObject* CPDF_ImageObject::AsImage() const {
40   return this;
41 }
42 
CalcBoundingBox()43 void CPDF_ImageObject::CalcBoundingBox() {
44   std::tie(m_Left, m_Right, m_Top, m_Bottom) =
45       m_Matrix.TransformRect(0.f, 1.f, 1.f, 0.f);
46 }
47 
SetImage(const RetainPtr<CPDF_Image> & pImage)48 void CPDF_ImageObject::SetImage(const RetainPtr<CPDF_Image>& pImage) {
49   MaybePurgeCache();
50   m_pImage = pImage;
51 }
52 
MaybePurgeCache()53 void CPDF_ImageObject::MaybePurgeCache() {
54   if (!m_pImage)
55     return;
56 
57   CPDF_Document* pDocument = m_pImage->GetDocument();
58   if (!pDocument)
59     return;
60 
61   CPDF_DocPageData* pPageData = pDocument->GetPageData();
62   if (!pPageData)
63     return;
64 
65   CPDF_Stream* pStream = m_pImage->GetStream();
66   if (!pStream)
67     return;
68 
69   uint32_t objnum = pStream->GetObjNum();
70   if (!objnum)
71     return;
72 
73   m_pImage.Reset();  // Clear my reference before asking the cache.
74   pPageData->MaybePurgeImage(objnum);
75 }
76