• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The PDFium Authors
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_imageloader.h"
8 
9 #include <utility>
10 
11 #include "core/fpdfapi/page/cpdf_dib.h"
12 #include "core/fpdfapi/page/cpdf_image.h"
13 #include "core/fpdfapi/page/cpdf_imageobject.h"
14 #include "core/fpdfapi/page/cpdf_pageimagecache.h"
15 #include "core/fpdfapi/page/cpdf_transferfunc.h"
16 #include "core/fxge/dib/cfx_dibitmap.h"
17 #include "third_party/base/check.h"
18 
19 CPDF_ImageLoader::CPDF_ImageLoader() = default;
20 
21 CPDF_ImageLoader::~CPDF_ImageLoader() = default;
22 
Start(const CPDF_ImageObject * pImage,CPDF_PageImageCache * pPageImageCache,const CPDF_Dictionary * pFormResource,const CPDF_Dictionary * pPageResource,bool bStdCS,CPDF_ColorSpace::Family eFamily,bool bLoadMask,const CFX_Size & max_size_required)23 bool CPDF_ImageLoader::Start(const CPDF_ImageObject* pImage,
24                              CPDF_PageImageCache* pPageImageCache,
25                              const CPDF_Dictionary* pFormResource,
26                              const CPDF_Dictionary* pPageResource,
27                              bool bStdCS,
28                              CPDF_ColorSpace::Family eFamily,
29                              bool bLoadMask,
30                              const CFX_Size& max_size_required) {
31   m_pCache = pPageImageCache;
32   m_pImageObject = pImage;
33   bool ret;
34   if (m_pCache) {
35     ret = m_pCache->StartGetCachedBitmap(m_pImageObject->GetImage(),
36                                          pFormResource, pPageResource, bStdCS,
37                                          eFamily, bLoadMask, max_size_required);
38   } else {
39     ret = m_pImageObject->GetImage()->StartLoadDIBBase(
40         pFormResource, pPageResource, bStdCS, eFamily, bLoadMask,
41         max_size_required);
42   }
43   if (!ret)
44     HandleFailure();
45   return ret;
46 }
47 
Continue(PauseIndicatorIface * pPause)48 bool CPDF_ImageLoader::Continue(PauseIndicatorIface* pPause) {
49   bool ret = m_pCache ? m_pCache->Continue(pPause)
50                       : m_pImageObject->GetImage()->Continue(pPause);
51   if (!ret)
52     HandleFailure();
53   return ret;
54 }
55 
TranslateImage(RetainPtr<CPDF_TransferFunc> pTransferFunc)56 RetainPtr<CFX_DIBBase> CPDF_ImageLoader::TranslateImage(
57     RetainPtr<CPDF_TransferFunc> pTransferFunc) {
58   DCHECK(pTransferFunc);
59   DCHECK(!pTransferFunc->GetIdentity());
60   m_pBitmap = pTransferFunc->TranslateImage(std::move(m_pBitmap));
61   if (m_bCached && m_pMask)
62     m_pMask = m_pMask->Realize();
63   m_bCached = false;
64   return m_pBitmap;
65 }
66 
HandleFailure()67 void CPDF_ImageLoader::HandleFailure() {
68   if (m_pCache) {
69     m_bCached = true;
70     m_pBitmap = m_pCache->DetachCurBitmap();
71     m_pMask = m_pCache->DetachCurMask();
72     m_MatteColor = m_pCache->GetCurMatteColor();
73     return;
74   }
75   RetainPtr<CPDF_Image> pImage = m_pImageObject->GetImage();
76   m_bCached = false;
77   m_pBitmap = pImage->DetachBitmap();
78   m_pMask = pImage->DetachMask();
79   m_MatteColor = pImage->GetMatteColor();
80 }
81