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     : m_pImage(nullptr), m_pImageOwned(false) {}
17 
~CPDF_ImageObject()18 CPDF_ImageObject::~CPDF_ImageObject() {
19   Release();
20 }
21 
GetType() const22 CPDF_PageObject::Type CPDF_ImageObject::GetType() const {
23   return IMAGE;
24 }
25 
Transform(const CFX_Matrix & matrix)26 void CPDF_ImageObject::Transform(const CFX_Matrix& matrix) {
27   m_Matrix.Concat(matrix);
28   CalcBoundingBox();
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   m_Left = 0;
45   m_Bottom = 0;
46   m_Right = 1.0f;
47   m_Top = 1.0f;
48   m_Matrix.TransformRect(m_Left, m_Right, m_Top, m_Bottom);
49 }
50 
SetOwnedImage(std::unique_ptr<CPDF_Image> pImage)51 void CPDF_ImageObject::SetOwnedImage(std::unique_ptr<CPDF_Image> pImage) {
52   Release();
53   m_pImage = pImage.release();
54   m_pImageOwned = true;
55 }
56 
SetUnownedImage(CPDF_Image * pImage)57 void CPDF_ImageObject::SetUnownedImage(CPDF_Image* pImage) {
58   Release();
59   m_pImage = pImage;
60   m_pImageOwned = false;
61 }
62 
Release()63 void CPDF_ImageObject::Release() {
64   if (m_pImageOwned) {
65     delete m_pImage;
66     m_pImage = nullptr;
67     m_pImageOwned = false;
68     return;
69   }
70 
71   if (!m_pImage)
72     return;
73 
74   CPDF_DocPageData* pPageData = m_pImage->GetDocument()->GetPageData();
75   pPageData->ReleaseImage(m_pImage->GetStream()->GetObjNum());
76   m_pImage = nullptr;
77 }
78