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 "pageint.h"
8 
9 #include "core/include/fpdfapi/fpdf_page.h"
10 #include "core/include/fpdfapi/fpdf_pageobj.h"
11 
CPDF_ImageObject()12 CPDF_ImageObject::CPDF_ImageObject() {
13   m_pImage = NULL;
14   m_Type = PDFPAGE_IMAGE;
15 }
~CPDF_ImageObject()16 CPDF_ImageObject::~CPDF_ImageObject() {
17   if (!m_pImage) {
18     return;
19   }
20   if (m_pImage->IsInline() ||
21       (m_pImage->GetStream() && m_pImage->GetStream()->GetObjNum() == 0)) {
22     delete m_pImage;
23   } else {
24     m_pImage->GetDocument()->GetPageData()->ReleaseImage(m_pImage->GetStream());
25   }
26 }
CopyData(const CPDF_PageObject * pSrc)27 void CPDF_ImageObject::CopyData(const CPDF_PageObject* pSrc) {
28   const CPDF_ImageObject* pSrcObj = (const CPDF_ImageObject*)pSrc;
29   if (m_pImage) {
30     m_pImage->Release();
31   }
32   m_pImage = pSrcObj->m_pImage->Clone();
33   m_Matrix = pSrcObj->m_Matrix;
34 }
Transform(const CFX_Matrix & matrix)35 void CPDF_ImageObject::Transform(const CFX_Matrix& matrix) {
36   m_Matrix.Concat(matrix);
37   CalcBoundingBox();
38 }
CalcBoundingBox()39 void CPDF_ImageObject::CalcBoundingBox() {
40   m_Left = m_Bottom = 0;
41   m_Right = m_Top = 1.0f;
42   m_Matrix.TransformRect(m_Left, m_Right, m_Top, m_Bottom);
43 }
Release()44 void CPDF_Image::Release() {
45   if (m_bInline || (m_pStream && m_pStream->GetObjNum() == 0)) {
46     delete this;
47   }
48 }
Clone()49 CPDF_Image* CPDF_Image::Clone() {
50   if (m_pStream->GetObjNum())
51     return m_pDocument->GetPageData()->GetImage(m_pStream);
52 
53   CPDF_Image* pImage = new CPDF_Image(m_pDocument);
54   pImage->LoadImageF(ToStream(m_pStream->CPDF_Object::Clone()), m_bInline);
55   if (m_bInline)
56     pImage->SetInlineDict(ToDictionary(m_pInlineDict->Clone(TRUE)));
57 
58   return pImage;
59 }
CPDF_Image(CPDF_Document * pDoc)60 CPDF_Image::CPDF_Image(CPDF_Document* pDoc) {
61   m_pDocument = pDoc;
62   m_pStream = NULL;
63   m_pOC = NULL;
64   m_bInline = FALSE;
65   m_pInlineDict = NULL;
66   m_pDIBSource = NULL;
67   m_pMask = NULL;
68   m_MatteColor = 0;
69 }
~CPDF_Image()70 CPDF_Image::~CPDF_Image() {
71   if (m_bInline) {
72     if (m_pStream) {
73       m_pStream->Release();
74     }
75     if (m_pInlineDict) {
76       m_pInlineDict->Release();
77     }
78   }
79 }
LoadImageF(CPDF_Stream * pStream,FX_BOOL bInline)80 FX_BOOL CPDF_Image::LoadImageF(CPDF_Stream* pStream, FX_BOOL bInline) {
81   m_pStream = pStream;
82   if (m_bInline && m_pInlineDict) {
83     m_pInlineDict->Release();
84     m_pInlineDict = NULL;
85   }
86   m_bInline = bInline;
87   CPDF_Dictionary* pDict = pStream->GetDict();
88   if (m_bInline) {
89     m_pInlineDict = ToDictionary(pDict->Clone());
90   }
91   m_pOC = pDict->GetDict("OC");
92   m_bIsMask = !pDict->KeyExist("ColorSpace") || pDict->GetInteger("ImageMask");
93   m_bInterpolate = pDict->GetInteger("Interpolate");
94   m_Height = pDict->GetInteger("Height");
95   m_Width = pDict->GetInteger("Width");
96   return TRUE;
97 }
98