• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "core/fpdfapi/parser/cpdf_stream.h"
15 #include "core/fxge/dib/cfx_dibbase.h"
16 #include "core/fxge/dib/cfx_dibitmap.h"
17 
CPDF_ImageObject(int32_t content_stream)18 CPDF_ImageObject::CPDF_ImageObject(int32_t content_stream)
19     : CPDF_PageObject(content_stream) {}
20 
CPDF_ImageObject()21 CPDF_ImageObject::CPDF_ImageObject() : CPDF_ImageObject(kNoContentStream) {}
22 
~CPDF_ImageObject()23 CPDF_ImageObject::~CPDF_ImageObject() {
24   MaybePurgeCache();
25 }
26 
GetType() const27 CPDF_PageObject::Type CPDF_ImageObject::GetType() const {
28   return IMAGE;
29 }
30 
Transform(const CFX_Matrix & matrix)31 void CPDF_ImageObject::Transform(const CFX_Matrix& matrix) {
32   m_Matrix.Concat(matrix);
33   CalcBoundingBox();
34   SetDirty(true);
35 }
36 
IsImage() const37 bool CPDF_ImageObject::IsImage() const {
38   return true;
39 }
40 
AsImage()41 CPDF_ImageObject* CPDF_ImageObject::AsImage() {
42   return this;
43 }
44 
AsImage() const45 const CPDF_ImageObject* CPDF_ImageObject::AsImage() const {
46   return this;
47 }
48 
CalcBoundingBox()49 void CPDF_ImageObject::CalcBoundingBox() {
50   static constexpr CFX_FloatRect kRect(0.0f, 0.0f, 1.0f, 1.0f);
51   SetRect(m_Matrix.TransformRect(kRect));
52 }
53 
SetImage(const RetainPtr<CPDF_Image> & pImage)54 void CPDF_ImageObject::SetImage(const RetainPtr<CPDF_Image>& pImage) {
55   MaybePurgeCache();
56   m_pImage = pImage;
57 }
58 
GetImage() const59 RetainPtr<CPDF_Image> CPDF_ImageObject::GetImage() const {
60   return m_pImage;
61 }
62 
GetIndependentBitmap() const63 RetainPtr<CFX_DIBitmap> CPDF_ImageObject::GetIndependentBitmap() const {
64   RetainPtr<CFX_DIBBase> pSource = GetImage()->LoadDIBBase();
65 
66   // Clone() is non-virtual, and can't be overloaded by CPDF_DIB to
67   // return a clone of the subclass as one would typically expect from a
68   // such a method. Instead, it only clones the CFX_DIBBase, none of whose
69   // members point to objects owned by |this| or the form containing |this|.
70   // As a result, the clone may outlive them.
71   return pSource ? pSource->Clone(nullptr) : nullptr;
72 }
73 
MaybePurgeCache()74 void CPDF_ImageObject::MaybePurgeCache() {
75   if (!m_pImage)
76     return;
77 
78   auto* pPageData = CPDF_DocPageData::FromDocument(m_pImage->GetDocument());
79   if (!pPageData)
80     return;
81 
82   CPDF_Stream* pStream = m_pImage->GetStream();
83   if (!pStream)
84     return;
85 
86   uint32_t objnum = pStream->GetObjNum();
87   if (!objnum)
88     return;
89 
90   m_pImage.Reset();  // Clear my reference before asking the cache.
91   pPageData->MaybePurgeImage(objnum);
92 }
93