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