dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 1 | // Copyright 2016 PDFium Authors. All rights reserved. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | 95e854f | 2015-06-13 00:58:06 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 7 | #include "core/fpdfapi/page/cpdf_image.h" |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 8 | |
thestig | dc359b0 | 2016-08-09 15:46:20 -0700 | [diff] [blame] | 9 | #include <algorithm> |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 10 | #include <memory> |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 11 | #include <utility> |
thestig | dc359b0 | 2016-08-09 15:46:20 -0700 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Lei Zhang | 2617056 | 2018-04-17 17:01:52 +0000 | [diff] [blame^] | 14 | #include "constants/stream_dict_common.h" |
dsinclair | 39c62fd | 2016-09-29 12:49:17 -0700 | [diff] [blame] | 15 | #include "core/fpdfapi/cpdf_modulemgr.h" |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 16 | #include "core/fpdfapi/page/cpdf_page.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 17 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 18 | #include "core/fpdfapi/parser/cpdf_boolean.h" |
tsepez | 4e4d1a6 | 2016-10-13 15:56:53 -0700 | [diff] [blame] | 19 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 20 | #include "core/fpdfapi/parser/cpdf_document.h" |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 21 | #include "core/fpdfapi/parser/cpdf_name.h" |
| 22 | #include "core/fpdfapi/parser/cpdf_number.h" |
| 23 | #include "core/fpdfapi/parser/cpdf_reference.h" |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 24 | #include "core/fpdfapi/parser/cpdf_stream.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 25 | #include "core/fpdfapi/parser/cpdf_string.h" |
Nicolas Pena | 0ef7ba0 | 2017-01-04 16:18:57 -0500 | [diff] [blame] | 26 | #include "core/fpdfapi/render/cpdf_dibsource.h" |
dsinclair | 69d9c68 | 2016-10-04 12:18:35 -0700 | [diff] [blame] | 27 | #include "core/fpdfapi/render/cpdf_pagerendercache.h" |
Lei Zhang | 7acd826 | 2017-09-19 14:34:37 -0700 | [diff] [blame] | 28 | #include "core/fxcodec/codec/ccodec_jpegmodule.h" |
Dan Sinclair | bcd1e70 | 2017-08-31 13:19:18 -0400 | [diff] [blame] | 29 | #include "core/fxcrt/fx_stream.h" |
Lei Zhang | 6e59fb5 | 2018-01-18 19:03:58 +0000 | [diff] [blame] | 30 | #include "core/fxge/dib/cfx_dibitmap.h" |
dsinclair | 74a34fc | 2016-09-29 16:41:42 -0700 | [diff] [blame] | 31 | #include "core/fxge/fx_dib.h" |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 32 | #include "third_party/base/numerics/safe_conversions.h" |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 33 | #include "third_party/base/ptr_util.h" |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 34 | |
tsepez | 4e4d1a6 | 2016-10-13 15:56:53 -0700 | [diff] [blame] | 35 | CPDF_Image::CPDF_Image(CPDF_Document* pDoc) : m_pDocument(pDoc) {} |
thestig | dc359b0 | 2016-08-09 15:46:20 -0700 | [diff] [blame] | 36 | |
tsepez | 33fdebc | 2016-11-04 11:38:40 -0700 | [diff] [blame] | 37 | CPDF_Image::CPDF_Image(CPDF_Document* pDoc, |
| 38 | std::unique_ptr<CPDF_Stream> pStream) |
Tom Sepez | bb1ee53 | 2018-01-30 18:10:01 +0000 | [diff] [blame] | 39 | : m_bIsInline(true), m_pDocument(pDoc), m_pStream(std::move(pStream)) { |
tsepez | 9fd0c63 | 2016-11-23 14:34:58 -0800 | [diff] [blame] | 40 | ASSERT(m_pStream.IsOwned()); |
Tom Sepez | bb1ee53 | 2018-01-30 18:10:01 +0000 | [diff] [blame] | 41 | FinishInitialization(m_pStream->GetDict()); |
thestig | dc359b0 | 2016-08-09 15:46:20 -0700 | [diff] [blame] | 42 | } |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 43 | |
tsepez | 4e4d1a6 | 2016-10-13 15:56:53 -0700 | [diff] [blame] | 44 | CPDF_Image::CPDF_Image(CPDF_Document* pDoc, uint32_t dwStreamObjNum) |
| 45 | : m_pDocument(pDoc), |
Tom Sepez | bb1ee53 | 2018-01-30 18:10:01 +0000 | [diff] [blame] | 46 | m_pStream(ToStream(pDoc->GetIndirectObject(dwStreamObjNum))) { |
tsepez | 9fd0c63 | 2016-11-23 14:34:58 -0800 | [diff] [blame] | 47 | ASSERT(!m_pStream.IsOwned()); |
Tom Sepez | bb1ee53 | 2018-01-30 18:10:01 +0000 | [diff] [blame] | 48 | FinishInitialization(m_pStream->GetDict()); |
tsepez | 4e4d1a6 | 2016-10-13 15:56:53 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | CPDF_Image::~CPDF_Image() {} |
| 52 | |
Lei Zhang | aca3efc | 2018-03-16 20:27:04 +0000 | [diff] [blame] | 53 | void CPDF_Image::FinishInitialization(CPDF_Dictionary* pStreamDict) { |
| 54 | m_pOC = pStreamDict->GetDictFor("OC"); |
| 55 | m_bIsMask = !pStreamDict->KeyExist("ColorSpace") || |
| 56 | pStreamDict->GetIntegerFor("ImageMask"); |
| 57 | m_bInterpolate = !!pStreamDict->GetIntegerFor("Interpolate"); |
| 58 | m_Height = pStreamDict->GetIntegerFor("Height"); |
| 59 | m_Width = pStreamDict->GetIntegerFor("Width"); |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 60 | } |
| 61 | |
tsepez | 137a344 | 2016-11-14 15:03:55 -0800 | [diff] [blame] | 62 | void CPDF_Image::ConvertStreamToIndirectObject() { |
| 63 | if (!m_pStream->IsInline()) |
| 64 | return; |
| 65 | |
tsepez | 9fd0c63 | 2016-11-23 14:34:58 -0800 | [diff] [blame] | 66 | ASSERT(m_pStream.IsOwned()); |
| 67 | m_pDocument->AddIndirectObject(m_pStream.Release()); |
tsepez | 137a344 | 2016-11-14 15:03:55 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Lei Zhang | 731526e | 2017-12-11 21:28:38 +0000 | [diff] [blame] | 70 | CPDF_Dictionary* CPDF_Image::GetDict() const { |
| 71 | return m_pStream ? m_pStream->GetDict() : nullptr; |
| 72 | } |
| 73 | |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 74 | std::unique_ptr<CPDF_Dictionary> CPDF_Image::InitJPEG(uint8_t* pData, |
| 75 | uint32_t size) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 76 | int32_t width; |
| 77 | int32_t height; |
| 78 | int32_t num_comps; |
| 79 | int32_t bits; |
thestig | b0fcfad | 2016-06-06 17:54:29 -0700 | [diff] [blame] | 80 | bool color_trans; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 81 | if (!CPDF_ModuleMgr::Get()->GetJpegModule()->LoadInfo( |
thestig | b0fcfad | 2016-06-06 17:54:29 -0700 | [diff] [blame] | 82 | pData, size, &width, &height, &num_comps, &bits, &color_trans)) { |
| 83 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 84 | } |
thestig | b0fcfad | 2016-06-06 17:54:29 -0700 | [diff] [blame] | 85 | |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 86 | auto pDict = |
| 87 | pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool()); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 88 | pDict->SetNewFor<CPDF_Name>("Type", "XObject"); |
| 89 | pDict->SetNewFor<CPDF_Name>("Subtype", "Image"); |
| 90 | pDict->SetNewFor<CPDF_Number>("Width", width); |
| 91 | pDict->SetNewFor<CPDF_Number>("Height", height); |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 92 | const char* csname = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 93 | if (num_comps == 1) { |
| 94 | csname = "DeviceGray"; |
| 95 | } else if (num_comps == 3) { |
| 96 | csname = "DeviceRGB"; |
| 97 | } else if (num_comps == 4) { |
| 98 | csname = "DeviceCMYK"; |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 99 | CPDF_Array* pDecode = pDict->SetNewFor<CPDF_Array>("Decode"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 100 | for (int n = 0; n < 4; n++) { |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 101 | pDecode->AddNew<CPDF_Number>(1); |
| 102 | pDecode->AddNew<CPDF_Number>(0); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 103 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 104 | } |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 105 | pDict->SetNewFor<CPDF_Name>("ColorSpace", csname); |
| 106 | pDict->SetNewFor<CPDF_Number>("BitsPerComponent", bits); |
| 107 | pDict->SetNewFor<CPDF_Name>("Filter", "DCTDecode"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 108 | if (!color_trans) { |
Lei Zhang | 2617056 | 2018-04-17 17:01:52 +0000 | [diff] [blame^] | 109 | CPDF_Dictionary* pParms = |
| 110 | pDict->SetNewFor<CPDF_Dictionary>(pdfium::stream::kDecodeParms); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 111 | pParms->SetNewFor<CPDF_Number>("ColorTransform", 0); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 112 | } |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 113 | m_bIsMask = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 114 | m_Width = width; |
| 115 | m_Height = height; |
tsepez | 9fd0c63 | 2016-11-23 14:34:58 -0800 | [diff] [blame] | 116 | if (!m_pStream) |
| 117 | m_pStream = pdfium::MakeUnique<CPDF_Stream>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 118 | return pDict; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 119 | } |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 120 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 121 | void CPDF_Image::SetJpegImage(const RetainPtr<IFX_SeekableReadStream>& pFile) { |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 122 | uint32_t size = pdfium::base::checked_cast<uint32_t>(pFile->GetSize()); |
thestig | dc359b0 | 2016-08-09 15:46:20 -0700 | [diff] [blame] | 123 | if (!size) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 124 | return; |
thestig | dc359b0 | 2016-08-09 15:46:20 -0700 | [diff] [blame] | 125 | |
| 126 | uint32_t dwEstimateSize = std::min(size, 8192U); |
| 127 | std::vector<uint8_t> data(dwEstimateSize); |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 128 | if (!pFile->ReadBlock(data.data(), 0, dwEstimateSize)) |
| 129 | return; |
| 130 | |
| 131 | std::unique_ptr<CPDF_Dictionary> pDict = |
| 132 | InitJPEG(data.data(), dwEstimateSize); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 133 | if (!pDict && size > dwEstimateSize) { |
thestig | dc359b0 | 2016-08-09 15:46:20 -0700 | [diff] [blame] | 134 | data.resize(size); |
| 135 | pFile->ReadBlock(data.data(), 0, size); |
| 136 | pDict = InitJPEG(data.data(), size); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 137 | } |
thestig | dc359b0 | 2016-08-09 15:46:20 -0700 | [diff] [blame] | 138 | if (!pDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 139 | return; |
thestig | dc359b0 | 2016-08-09 15:46:20 -0700 | [diff] [blame] | 140 | |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 141 | m_pStream->InitStreamFromFile(pFile, std::move(pDict)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 142 | } |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 143 | |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 144 | void CPDF_Image::SetJpegImageInline( |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 145 | const RetainPtr<IFX_SeekableReadStream>& pFile) { |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 146 | uint32_t size = pdfium::base::checked_cast<uint32_t>(pFile->GetSize()); |
| 147 | if (!size) |
| 148 | return; |
| 149 | |
| 150 | std::vector<uint8_t> data(size); |
| 151 | if (!pFile->ReadBlock(data.data(), 0, size)) |
| 152 | return; |
| 153 | |
| 154 | std::unique_ptr<CPDF_Dictionary> pDict = InitJPEG(data.data(), size); |
| 155 | if (!pDict) |
| 156 | return; |
| 157 | |
| 158 | m_pStream->InitStream(&(data[0]), size, std::move(pDict)); |
| 159 | } |
| 160 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 161 | void CPDF_Image::SetImage(const RetainPtr<CFX_DIBitmap>& pBitmap) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 162 | int32_t BitmapWidth = pBitmap->GetWidth(); |
| 163 | int32_t BitmapHeight = pBitmap->GetHeight(); |
thestig | 4ccdb14 | 2016-11-21 15:09:23 -0800 | [diff] [blame] | 164 | if (BitmapWidth < 1 || BitmapHeight < 1) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 165 | return; |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 166 | |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 167 | auto pDict = |
| 168 | pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool()); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 169 | pDict->SetNewFor<CPDF_Name>("Type", "XObject"); |
| 170 | pDict->SetNewFor<CPDF_Name>("Subtype", "Image"); |
| 171 | pDict->SetNewFor<CPDF_Number>("Width", BitmapWidth); |
| 172 | pDict->SetNewFor<CPDF_Number>("Height", BitmapHeight); |
thestig | 4ccdb14 | 2016-11-21 15:09:23 -0800 | [diff] [blame] | 173 | |
| 174 | const int32_t bpp = pBitmap->GetBPP(); |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 175 | size_t dest_pitch = 0; |
thestig | 4ccdb14 | 2016-11-21 15:09:23 -0800 | [diff] [blame] | 176 | bool bCopyWithoutAlpha = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 177 | if (bpp == 1) { |
thestig | 4ccdb14 | 2016-11-21 15:09:23 -0800 | [diff] [blame] | 178 | int32_t reset_a = 0; |
| 179 | int32_t reset_r = 0; |
| 180 | int32_t reset_g = 0; |
| 181 | int32_t reset_b = 0; |
| 182 | int32_t set_a = 0; |
| 183 | int32_t set_r = 0; |
| 184 | int32_t set_g = 0; |
| 185 | int32_t set_b = 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | if (!pBitmap->IsAlphaMask()) { |
Nicolas Pena | ddfc3dc | 2017-04-20 15:29:25 -0400 | [diff] [blame] | 187 | std::tie(reset_a, reset_r, reset_g, reset_b) = |
| 188 | ArgbDecode(pBitmap->GetPaletteArgb(0)); |
| 189 | std::tie(set_a, set_r, set_g, set_b) = |
| 190 | ArgbDecode(pBitmap->GetPaletteArgb(1)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 191 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 192 | if (set_a == 0 || reset_a == 0) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 193 | pDict->SetNewFor<CPDF_Boolean>("ImageMask", true); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 194 | if (reset_a == 0) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 195 | CPDF_Array* pArray = pDict->SetNewFor<CPDF_Array>("Decode"); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 196 | pArray->AddNew<CPDF_Number>(1); |
| 197 | pArray->AddNew<CPDF_Number>(0); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 198 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 199 | } else { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 200 | CPDF_Array* pCS = pDict->SetNewFor<CPDF_Array>("ColorSpace"); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 201 | pCS->AddNew<CPDF_Name>("Indexed"); |
| 202 | pCS->AddNew<CPDF_Name>("DeviceRGB"); |
| 203 | pCS->AddNew<CPDF_Number>(1); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 204 | ByteString ct; |
Tom Sepez | 3d523e3 | 2018-04-16 17:28:17 +0000 | [diff] [blame] | 205 | char* pBuf = ct.GetBuffer(6); |
| 206 | pBuf[0] = (char)reset_r; |
| 207 | pBuf[1] = (char)reset_g; |
| 208 | pBuf[2] = (char)reset_b; |
| 209 | pBuf[3] = (char)set_r; |
| 210 | pBuf[4] = (char)set_g; |
| 211 | pBuf[5] = (char)set_b; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 212 | ct.ReleaseBuffer(6); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 213 | pCS->AddNew<CPDF_String>(ct, true); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 214 | } |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 215 | pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 1); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 216 | dest_pitch = (BitmapWidth + 7) / 8; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 217 | } else if (bpp == 8) { |
| 218 | int32_t iPalette = pBitmap->GetPaletteSize(); |
| 219 | if (iPalette > 0) { |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 220 | CPDF_Array* pCS = m_pDocument->NewIndirect<CPDF_Array>(); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 221 | pCS->AddNew<CPDF_Name>("Indexed"); |
| 222 | pCS->AddNew<CPDF_Name>("DeviceRGB"); |
| 223 | pCS->AddNew<CPDF_Number>(iPalette - 1); |
tsepez | 47fb8c0 | 2016-12-15 13:51:34 -0800 | [diff] [blame] | 224 | std::unique_ptr<uint8_t, FxFreeDeleter> pColorTable( |
| 225 | FX_Alloc2D(uint8_t, iPalette, 3)); |
| 226 | uint8_t* ptr = pColorTable.get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 227 | for (int32_t i = 0; i < iPalette; i++) { |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 228 | uint32_t argb = pBitmap->GetPaletteArgb(i); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 229 | ptr[0] = (uint8_t)(argb >> 16); |
| 230 | ptr[1] = (uint8_t)(argb >> 8); |
| 231 | ptr[2] = (uint8_t)argb; |
| 232 | ptr += 3; |
| 233 | } |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 234 | auto pNewDict = |
| 235 | pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool()); |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 236 | CPDF_Stream* pCTS = m_pDocument->NewIndirect<CPDF_Stream>( |
tsepez | 47fb8c0 | 2016-12-15 13:51:34 -0800 | [diff] [blame] | 237 | std::move(pColorTable), iPalette * 3, std::move(pNewDict)); |
Tom Sepez | 4734512 | 2017-05-24 14:07:18 -0700 | [diff] [blame] | 238 | pCS->AddNew<CPDF_Reference>(m_pDocument.Get(), pCTS->GetObjNum()); |
| 239 | pDict->SetNewFor<CPDF_Reference>("ColorSpace", m_pDocument.Get(), |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 240 | pCS->GetObjNum()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 241 | } else { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 242 | pDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceGray"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 243 | } |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 244 | pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); |
thestig | 4ccdb14 | 2016-11-21 15:09:23 -0800 | [diff] [blame] | 245 | dest_pitch = BitmapWidth; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 246 | } else { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 247 | pDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceRGB"); |
| 248 | pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); |
thestig | 4ccdb14 | 2016-11-21 15:09:23 -0800 | [diff] [blame] | 249 | dest_pitch = BitmapWidth * 3; |
| 250 | bCopyWithoutAlpha = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 251 | } |
thestig | 4ccdb14 | 2016-11-21 15:09:23 -0800 | [diff] [blame] | 252 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 253 | RetainPtr<CFX_DIBitmap> pMaskBitmap; |
tsepez | 72c1bda | 2016-12-14 14:15:14 -0800 | [diff] [blame] | 254 | if (pBitmap->HasAlpha()) |
| 255 | pMaskBitmap = pBitmap->CloneAlphaMask(); |
| 256 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 257 | if (pMaskBitmap) { |
| 258 | int32_t maskWidth = pMaskBitmap->GetWidth(); |
| 259 | int32_t maskHeight = pMaskBitmap->GetHeight(); |
tsepez | 47fb8c0 | 2016-12-15 13:51:34 -0800 | [diff] [blame] | 260 | std::unique_ptr<uint8_t, FxFreeDeleter> mask_buf; |
Ryan Harrison | aa3a9cd | 2017-08-29 16:39:44 -0400 | [diff] [blame] | 261 | int32_t mask_size = 0; |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 262 | auto pMaskDict = |
| 263 | pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool()); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 264 | pMaskDict->SetNewFor<CPDF_Name>("Type", "XObject"); |
| 265 | pMaskDict->SetNewFor<CPDF_Name>("Subtype", "Image"); |
| 266 | pMaskDict->SetNewFor<CPDF_Number>("Width", maskWidth); |
| 267 | pMaskDict->SetNewFor<CPDF_Number>("Height", maskHeight); |
| 268 | pMaskDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceGray"); |
| 269 | pMaskDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); |
thestig | 4ccdb14 | 2016-11-21 15:09:23 -0800 | [diff] [blame] | 270 | if (pMaskBitmap->GetFormat() != FXDIB_1bppMask) { |
tsepez | 47fb8c0 | 2016-12-15 13:51:34 -0800 | [diff] [blame] | 271 | mask_buf.reset(FX_Alloc2D(uint8_t, maskHeight, maskWidth)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 272 | mask_size = maskHeight * maskWidth; // Safe since checked alloc returned. |
| 273 | for (int32_t a = 0; a < maskHeight; a++) { |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 274 | memcpy(mask_buf.get() + a * maskWidth, pMaskBitmap->GetScanline(a), |
| 275 | maskWidth); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 278 | pMaskDict->SetNewFor<CPDF_Number>("Length", mask_size); |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 279 | CPDF_Stream* pNewStream = m_pDocument->NewIndirect<CPDF_Stream>( |
tsepez | 47fb8c0 | 2016-12-15 13:51:34 -0800 | [diff] [blame] | 280 | std::move(mask_buf), mask_size, std::move(pMaskDict)); |
Tom Sepez | 4734512 | 2017-05-24 14:07:18 -0700 | [diff] [blame] | 281 | pDict->SetNewFor<CPDF_Reference>("SMask", m_pDocument.Get(), |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 282 | pNewStream->GetObjNum()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 283 | } |
dsinclair | 65ea394 | 2016-03-25 09:16:34 -0700 | [diff] [blame] | 284 | |
thestig | 4ccdb14 | 2016-11-21 15:09:23 -0800 | [diff] [blame] | 285 | uint8_t* src_buf = pBitmap->GetBuffer(); |
| 286 | int32_t src_pitch = pBitmap->GetPitch(); |
| 287 | uint8_t* dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight); |
| 288 | // Safe as checked alloc returned. |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 289 | size_t dest_size = dest_pitch * BitmapHeight; |
thestig | 4ccdb14 | 2016-11-21 15:09:23 -0800 | [diff] [blame] | 290 | uint8_t* pDest = dest_buf; |
| 291 | if (bCopyWithoutAlpha) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 292 | for (int32_t i = 0; i < BitmapHeight; i++) { |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 293 | memcpy(pDest, src_buf, dest_pitch); |
dsinclair | 65ea394 | 2016-03-25 09:16:34 -0700 | [diff] [blame] | 294 | pDest += dest_pitch; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 295 | src_buf += src_pitch; |
| 296 | } |
thestig | 4ccdb14 | 2016-11-21 15:09:23 -0800 | [diff] [blame] | 297 | } else { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 298 | int32_t src_offset = 0; |
| 299 | int32_t dest_offset = 0; |
| 300 | for (int32_t row = 0; row < BitmapHeight; row++) { |
| 301 | src_offset = row * src_pitch; |
| 302 | for (int32_t column = 0; column < BitmapWidth; column++) { |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 303 | float alpha = 1; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 304 | pDest[dest_offset] = (uint8_t)(src_buf[src_offset + 2] * alpha); |
| 305 | pDest[dest_offset + 1] = (uint8_t)(src_buf[src_offset + 1] * alpha); |
| 306 | pDest[dest_offset + 2] = (uint8_t)(src_buf[src_offset] * alpha); |
| 307 | dest_offset += 3; |
| 308 | src_offset += bpp == 24 ? 3 : 4; |
| 309 | } |
dsinclair | 65ea394 | 2016-03-25 09:16:34 -0700 | [diff] [blame] | 310 | |
| 311 | pDest += dest_pitch; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 312 | dest_offset = 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 313 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 314 | } |
tsepez | 9fd0c63 | 2016-11-23 14:34:58 -0800 | [diff] [blame] | 315 | if (!m_pStream) |
| 316 | m_pStream = pdfium::MakeUnique<CPDF_Stream>(); |
| 317 | |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 318 | m_pStream->InitStream(dest_buf, dest_size, std::move(pDict)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 319 | m_bIsMask = pBitmap->IsAlphaMask(); |
| 320 | m_Width = BitmapWidth; |
| 321 | m_Height = BitmapHeight; |
Lei Zhang | da180e9 | 2015-08-14 22:22:13 -0700 | [diff] [blame] | 322 | FX_Free(dest_buf); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 323 | } |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 324 | |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 325 | void CPDF_Image::ResetCache(CPDF_Page* pPage, |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 326 | const RetainPtr<CFX_DIBitmap>& pBitmap) { |
| 327 | RetainPtr<CPDF_Image> pHolder(this); |
Tom Sepez | 258909c | 2017-05-23 14:31:00 -0700 | [diff] [blame] | 328 | pPage->GetRenderCache()->ResetBitmap(pHolder, pBitmap); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 329 | } |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 330 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 331 | RetainPtr<CFX_DIBSource> CPDF_Image::LoadDIBSource() const { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 332 | auto source = pdfium::MakeRetain<CPDF_DIBSource>(); |
Tom Sepez | 4734512 | 2017-05-24 14:07:18 -0700 | [diff] [blame] | 333 | if (!source->Load(m_pDocument.Get(), m_pStream.Get())) |
tsepez | 5bed98c | 2016-12-14 13:54:33 -0800 | [diff] [blame] | 334 | return nullptr; |
Nicolas Pena | 48f776f | 2017-01-05 13:22:17 -0500 | [diff] [blame] | 335 | |
Lei Zhang | 1330ebb | 2018-03-05 15:16:37 +0000 | [diff] [blame] | 336 | if (!source->IsJBigImage()) |
| 337 | return source; |
| 338 | |
| 339 | CPDF_DIBSource::LoadState ret = CPDF_DIBSource::LoadState::kContinue; |
| 340 | while (ret == CPDF_DIBSource::LoadState::kContinue) |
| 341 | ret = source->ContinueLoadDIBSource(nullptr); |
| 342 | return ret == CPDF_DIBSource::LoadState::kSuccess ? source : nullptr; |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 343 | } |
| 344 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 345 | RetainPtr<CFX_DIBSource> CPDF_Image::DetachBitmap() { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 346 | return std::move(m_pDIBSource); |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 347 | } |
| 348 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 349 | RetainPtr<CFX_DIBSource> CPDF_Image::DetachMask() { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 350 | return std::move(m_pMask); |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 351 | } |
| 352 | |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 353 | bool CPDF_Image::StartLoadDIBSource(CPDF_Dictionary* pFormResource, |
| 354 | CPDF_Dictionary* pPageResource, |
| 355 | bool bStdCS, |
| 356 | uint32_t GroupFamily, |
| 357 | bool bLoadMask) { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 358 | auto source = pdfium::MakeRetain<CPDF_DIBSource>(); |
Lei Zhang | 40482e6 | 2018-03-05 15:03:37 +0000 | [diff] [blame] | 359 | CPDF_DIBSource::LoadState ret = source->StartLoadDIBSource( |
| 360 | m_pDocument.Get(), m_pStream.Get(), true, pFormResource, pPageResource, |
| 361 | bStdCS, GroupFamily, bLoadMask); |
| 362 | if (ret == CPDF_DIBSource::LoadState::kFail) { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 363 | m_pDIBSource.Reset(); |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 364 | return false; |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 365 | } |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 366 | m_pDIBSource = source; |
Lei Zhang | 40482e6 | 2018-03-05 15:03:37 +0000 | [diff] [blame] | 367 | if (ret == CPDF_DIBSource::LoadState::kContinue) |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 368 | return true; |
| 369 | |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 370 | m_pMask = source->DetachMask(); |
| 371 | m_MatteColor = source->GetMatteColor(); |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 372 | return false; |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 373 | } |
| 374 | |
Dan Sinclair | a32145f | 2018-03-06 18:53:05 +0000 | [diff] [blame] | 375 | bool CPDF_Image::Continue(PauseIndicatorIface* pPause) { |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 376 | RetainPtr<CPDF_DIBSource> pSource = m_pDIBSource.As<CPDF_DIBSource>(); |
Lei Zhang | 40482e6 | 2018-03-05 15:03:37 +0000 | [diff] [blame] | 377 | CPDF_DIBSource::LoadState ret = pSource->ContinueLoadDIBSource(pPause); |
| 378 | if (ret == CPDF_DIBSource::LoadState::kContinue) |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 379 | return true; |
| 380 | |
Lei Zhang | 40482e6 | 2018-03-05 15:03:37 +0000 | [diff] [blame] | 381 | if (ret == CPDF_DIBSource::LoadState::kSuccess) { |
Lei Zhang | b058365 | 2018-03-05 14:46:57 +0000 | [diff] [blame] | 382 | m_pMask = pSource->DetachMask(); |
| 383 | m_MatteColor = pSource->GetMatteColor(); |
| 384 | } else { |
| 385 | m_pDIBSource.Reset(); |
| 386 | } |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 387 | return false; |
dan sinclair | 61b2fc7 | 2016-03-23 19:21:44 -0400 | [diff] [blame] | 388 | } |