OLD | NEW |
1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/fpdfapi/fpdf_page/include/cpdf_image.h" | 7 #include "core/fpdfapi/fpdf_page/include/cpdf_image.h" |
8 | 8 |
| 9 #include <algorithm> |
| 10 #include <memory> |
| 11 #include <vector> |
| 12 |
9 #include "core/fpdfapi/fpdf_page/include/cpdf_page.h" | 13 #include "core/fpdfapi/fpdf_page/include/cpdf_page.h" |
10 #include "core/fpdfapi/fpdf_page/pageint.h" | 14 #include "core/fpdfapi/fpdf_page/pageint.h" |
11 #include "core/fpdfapi/fpdf_parser/cpdf_boolean.h" | 15 #include "core/fpdfapi/fpdf_parser/cpdf_boolean.h" |
12 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | 16 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
13 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" | 17 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
14 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" | 18 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" |
15 #include "core/fpdfapi/fpdf_render/cpdf_pagerendercache.h" | 19 #include "core/fpdfapi/fpdf_render/cpdf_pagerendercache.h" |
16 #include "core/fpdfapi/fpdf_render/render_int.h" | 20 #include "core/fpdfapi/fpdf_render/render_int.h" |
17 #include "core/fpdfapi/include/cpdf_modulemgr.h" | 21 #include "core/fpdfapi/include/cpdf_modulemgr.h" |
18 #include "core/fxcodec/include/fx_codec.h" | 22 #include "core/fxcodec/include/fx_codec.h" |
19 #include "core/fxge/include/fx_dib.h" | 23 #include "core/fxge/include/fx_dib.h" |
20 | 24 |
21 CPDF_Image::CPDF_Image(CPDF_Document* pDoc) | 25 CPDF_Image::CPDF_Image(CPDF_Document* pDoc) |
| 26 : CPDF_Image(pDoc, nullptr, false) {} |
| 27 |
| 28 CPDF_Image::CPDF_Image(CPDF_Document* pDoc, CPDF_Stream* pStream, bool bInline) |
22 : m_pDIBSource(nullptr), | 29 : m_pDIBSource(nullptr), |
23 m_pMask(nullptr), | 30 m_pMask(nullptr), |
24 m_MatteColor(0), | 31 m_MatteColor(0), |
25 m_pStream(nullptr), | 32 m_pStream(pStream), |
26 m_bInline(FALSE), | 33 m_bInline(bInline), |
27 m_pInlineDict(nullptr), | 34 m_pInlineDict(nullptr), |
| 35 m_Height(0), |
| 36 m_Width(0), |
| 37 m_bIsMask(false), |
| 38 m_bInterpolate(false), |
28 m_pDocument(pDoc), | 39 m_pDocument(pDoc), |
29 m_pOC(nullptr) {} | 40 m_pOC(nullptr) { |
| 41 if (!pStream) |
| 42 return; |
| 43 |
| 44 CPDF_Dictionary* pDict = pStream->GetDict(); |
| 45 if (m_bInline) |
| 46 m_pInlineDict = ToDictionary(pDict->Clone()); |
| 47 |
| 48 m_pOC = pDict->GetDictBy("OC"); |
| 49 m_bIsMask = |
| 50 !pDict->KeyExist("ColorSpace") || pDict->GetIntegerBy("ImageMask"); |
| 51 m_bInterpolate = !!pDict->GetIntegerBy("Interpolate"); |
| 52 m_Height = pDict->GetIntegerBy("Height"); |
| 53 m_Width = pDict->GetIntegerBy("Width"); |
| 54 } |
30 | 55 |
31 CPDF_Image::~CPDF_Image() { | 56 CPDF_Image::~CPDF_Image() { |
32 if (m_bInline) { | 57 if (m_bInline) { |
33 if (m_pStream) | 58 if (m_pStream) |
34 m_pStream->Release(); | 59 m_pStream->Release(); |
35 if (m_pInlineDict) | 60 if (m_pInlineDict) |
36 m_pInlineDict->Release(); | 61 m_pInlineDict->Release(); |
37 } | 62 } |
38 } | 63 } |
39 | 64 |
40 void CPDF_Image::Release() { | |
41 if (m_bInline || (m_pStream && m_pStream->GetObjNum() == 0)) | |
42 delete this; | |
43 } | |
44 | |
45 CPDF_Image* CPDF_Image::Clone() { | 65 CPDF_Image* CPDF_Image::Clone() { |
46 if (m_pStream->GetObjNum()) | 66 if (m_pStream->GetObjNum()) |
47 return m_pDocument->GetPageData()->GetImage(m_pStream); | 67 return m_pDocument->GetPageData()->GetImage(m_pStream); |
48 | 68 |
49 CPDF_Image* pImage = new CPDF_Image(m_pDocument); | 69 CPDF_Image* pImage = |
50 pImage->LoadImageF(ToStream(m_pStream->Clone()), m_bInline); | 70 new CPDF_Image(m_pDocument, ToStream(m_pStream->Clone()), m_bInline); |
51 if (m_bInline) | 71 if (m_bInline) |
52 pImage->SetInlineDict(ToDictionary(m_pInlineDict->Clone(TRUE))); | 72 pImage->SetInlineDict(ToDictionary(m_pInlineDict->Clone(TRUE))); |
53 | 73 |
54 return pImage; | 74 return pImage; |
55 } | 75 } |
56 | 76 |
57 FX_BOOL CPDF_Image::LoadImageF(CPDF_Stream* pStream, FX_BOOL bInline) { | |
58 m_pStream = pStream; | |
59 if (m_bInline && m_pInlineDict) { | |
60 m_pInlineDict->Release(); | |
61 m_pInlineDict = nullptr; | |
62 } | |
63 m_bInline = bInline; | |
64 CPDF_Dictionary* pDict = pStream->GetDict(); | |
65 if (m_bInline) { | |
66 m_pInlineDict = ToDictionary(pDict->Clone()); | |
67 } | |
68 m_pOC = pDict->GetDictBy("OC"); | |
69 m_bIsMask = | |
70 !pDict->KeyExist("ColorSpace") || pDict->GetIntegerBy("ImageMask"); | |
71 m_bInterpolate = pDict->GetIntegerBy("Interpolate"); | |
72 m_Height = pDict->GetIntegerBy("Height"); | |
73 m_Width = pDict->GetIntegerBy("Width"); | |
74 return TRUE; | |
75 } | |
76 | |
77 CPDF_Dictionary* CPDF_Image::InitJPEG(uint8_t* pData, uint32_t size) { | 77 CPDF_Dictionary* CPDF_Image::InitJPEG(uint8_t* pData, uint32_t size) { |
78 int32_t width; | 78 int32_t width; |
79 int32_t height; | 79 int32_t height; |
80 int32_t num_comps; | 80 int32_t num_comps; |
81 int32_t bits; | 81 int32_t bits; |
82 bool color_trans; | 82 bool color_trans; |
83 if (!CPDF_ModuleMgr::Get()->GetJpegModule()->LoadInfo( | 83 if (!CPDF_ModuleMgr::Get()->GetJpegModule()->LoadInfo( |
84 pData, size, &width, &height, &num_comps, &bits, &color_trans)) { | 84 pData, size, &width, &height, &num_comps, &bits, &color_trans)) { |
85 return nullptr; | 85 return nullptr; |
86 } | 86 } |
(...skipping 26 matching lines...) Expand all Loading... |
113 pParms->SetAtInteger("ColorTransform", 0); | 113 pParms->SetAtInteger("ColorTransform", 0); |
114 } | 114 } |
115 m_bIsMask = FALSE; | 115 m_bIsMask = FALSE; |
116 m_Width = width; | 116 m_Width = width; |
117 m_Height = height; | 117 m_Height = height; |
118 if (!m_pStream) | 118 if (!m_pStream) |
119 m_pStream = new CPDF_Stream(nullptr, 0, nullptr); | 119 m_pStream = new CPDF_Stream(nullptr, 0, nullptr); |
120 return pDict; | 120 return pDict; |
121 } | 121 } |
122 | 122 |
123 void CPDF_Image::SetJpegImage(uint8_t* pData, uint32_t size) { | |
124 CPDF_Dictionary* pDict = InitJPEG(pData, size); | |
125 if (!pDict) { | |
126 return; | |
127 } | |
128 m_pStream->InitStream(pData, size, pDict); | |
129 } | |
130 | |
131 void CPDF_Image::SetJpegImage(IFX_FileRead* pFile) { | 123 void CPDF_Image::SetJpegImage(IFX_FileRead* pFile) { |
132 uint32_t size = (uint32_t)pFile->GetSize(); | 124 uint32_t size = (uint32_t)pFile->GetSize(); |
133 if (!size) { | 125 if (!size) |
134 return; | 126 return; |
| 127 |
| 128 uint32_t dwEstimateSize = std::min(size, 8192U); |
| 129 std::vector<uint8_t> data(dwEstimateSize); |
| 130 pFile->ReadBlock(data.data(), 0, dwEstimateSize); |
| 131 CPDF_Dictionary* pDict = InitJPEG(data.data(), dwEstimateSize); |
| 132 if (!pDict && size > dwEstimateSize) { |
| 133 data.resize(size); |
| 134 pFile->ReadBlock(data.data(), 0, size); |
| 135 pDict = InitJPEG(data.data(), size); |
135 } | 136 } |
136 uint32_t dwEstimateSize = size; | 137 if (!pDict) |
137 if (dwEstimateSize > 8192) { | |
138 dwEstimateSize = 8192; | |
139 } | |
140 uint8_t* pData = FX_Alloc(uint8_t, dwEstimateSize); | |
141 pFile->ReadBlock(pData, 0, dwEstimateSize); | |
142 CPDF_Dictionary* pDict = InitJPEG(pData, dwEstimateSize); | |
143 FX_Free(pData); | |
144 if (!pDict && size > dwEstimateSize) { | |
145 pData = FX_Alloc(uint8_t, size); | |
146 pFile->ReadBlock(pData, 0, size); | |
147 pDict = InitJPEG(pData, size); | |
148 FX_Free(pData); | |
149 } | |
150 if (!pDict) { | |
151 return; | 138 return; |
152 } | 139 |
153 m_pStream->InitStreamFromFile(pFile, pDict); | 140 m_pStream->InitStreamFromFile(pFile, pDict); |
154 } | 141 } |
155 | 142 |
156 void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, int32_t iCompress) { | 143 void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, int32_t iCompress) { |
157 int32_t BitmapWidth = pBitmap->GetWidth(); | 144 int32_t BitmapWidth = pBitmap->GetWidth(); |
158 int32_t BitmapHeight = pBitmap->GetHeight(); | 145 int32_t BitmapHeight = pBitmap->GetHeight(); |
159 if (BitmapWidth < 1 || BitmapHeight < 1) { | 146 if (BitmapWidth < 1 || BitmapHeight < 1) { |
160 return; | 147 return; |
161 } | 148 } |
162 uint8_t* src_buf = pBitmap->GetBuffer(); | 149 uint8_t* src_buf = pBitmap->GetBuffer(); |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 } | 400 } |
414 if (!ret) { | 401 if (!ret) { |
415 delete m_pDIBSource; | 402 delete m_pDIBSource; |
416 m_pDIBSource = nullptr; | 403 m_pDIBSource = nullptr; |
417 return FALSE; | 404 return FALSE; |
418 } | 405 } |
419 m_pMask = pSource->DetachMask(); | 406 m_pMask = pSource->DetachMask(); |
420 m_MatteColor = pSource->GetMatteColor(); | 407 m_MatteColor = pSource->GetMatteColor(); |
421 return FALSE; | 408 return FALSE; |
422 } | 409 } |
OLD | NEW |