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/page/cpdf_image.h" | 7 #include "core/fpdfapi/page/cpdf_image.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 } | 223 } |
224 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); | 224 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); |
225 dest_pitch = BitmapWidth; | 225 dest_pitch = BitmapWidth; |
226 } else { | 226 } else { |
227 pDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceRGB"); | 227 pDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceRGB"); |
228 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); | 228 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); |
229 dest_pitch = BitmapWidth * 3; | 229 dest_pitch = BitmapWidth * 3; |
230 bCopyWithoutAlpha = false; | 230 bCopyWithoutAlpha = false; |
231 } | 231 } |
232 | 232 |
233 const CFX_DIBitmap* pMaskBitmap = | 233 std::unique_ptr<CFX_DIBitmap> pMaskBitmap; |
234 pBitmap->HasAlpha() ? pBitmap->GetAlphaMask() : nullptr; | 234 if (pBitmap->HasAlpha()) |
| 235 pMaskBitmap = pBitmap->CloneAlphaMask(); |
| 236 |
235 if (pMaskBitmap) { | 237 if (pMaskBitmap) { |
236 int32_t maskWidth = pMaskBitmap->GetWidth(); | 238 int32_t maskWidth = pMaskBitmap->GetWidth(); |
237 int32_t maskHeight = pMaskBitmap->GetHeight(); | 239 int32_t maskHeight = pMaskBitmap->GetHeight(); |
238 uint8_t* mask_buf = nullptr; | 240 uint8_t* mask_buf = nullptr; |
239 FX_STRSIZE mask_size = 0; | 241 FX_STRSIZE mask_size = 0; |
240 auto pMaskDict = | 242 auto pMaskDict = |
241 pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool()); | 243 pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool()); |
242 pMaskDict->SetNewFor<CPDF_Name>("Type", "XObject"); | 244 pMaskDict->SetNewFor<CPDF_Name>("Type", "XObject"); |
243 pMaskDict->SetNewFor<CPDF_Name>("Subtype", "Image"); | 245 pMaskDict->SetNewFor<CPDF_Name>("Subtype", "Image"); |
244 pMaskDict->SetNewFor<CPDF_Number>("Width", maskWidth); | 246 pMaskDict->SetNewFor<CPDF_Number>("Width", maskWidth); |
245 pMaskDict->SetNewFor<CPDF_Number>("Height", maskHeight); | 247 pMaskDict->SetNewFor<CPDF_Number>("Height", maskHeight); |
246 pMaskDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceGray"); | 248 pMaskDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceGray"); |
247 pMaskDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); | 249 pMaskDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); |
248 if (pMaskBitmap->GetFormat() != FXDIB_1bppMask) { | 250 if (pMaskBitmap->GetFormat() != FXDIB_1bppMask) { |
249 mask_buf = FX_Alloc2D(uint8_t, maskHeight, maskWidth); | 251 mask_buf = FX_Alloc2D(uint8_t, maskHeight, maskWidth); |
250 mask_size = maskHeight * maskWidth; // Safe since checked alloc returned. | 252 mask_size = maskHeight * maskWidth; // Safe since checked alloc returned. |
251 for (int32_t a = 0; a < maskHeight; a++) { | 253 for (int32_t a = 0; a < maskHeight; a++) { |
252 FXSYS_memcpy(mask_buf + a * maskWidth, pMaskBitmap->GetScanline(a), | 254 FXSYS_memcpy(mask_buf + a * maskWidth, pMaskBitmap->GetScanline(a), |
253 maskWidth); | 255 maskWidth); |
254 } | 256 } |
255 } | 257 } |
256 pMaskDict->SetNewFor<CPDF_Number>("Length", mask_size); | 258 pMaskDict->SetNewFor<CPDF_Number>("Length", mask_size); |
257 CPDF_Stream* pNewStream = m_pDocument->NewIndirect<CPDF_Stream>( | 259 CPDF_Stream* pNewStream = m_pDocument->NewIndirect<CPDF_Stream>( |
258 mask_buf, mask_size, std::move(pMaskDict)); | 260 mask_buf, mask_size, std::move(pMaskDict)); |
259 pDict->SetNewFor<CPDF_Reference>("SMask", m_pDocument, | 261 pDict->SetNewFor<CPDF_Reference>("SMask", m_pDocument, |
260 pNewStream->GetObjNum()); | 262 pNewStream->GetObjNum()); |
261 delete pMaskBitmap; | |
262 } | 263 } |
263 | 264 |
264 uint8_t* src_buf = pBitmap->GetBuffer(); | 265 uint8_t* src_buf = pBitmap->GetBuffer(); |
265 int32_t src_pitch = pBitmap->GetPitch(); | 266 int32_t src_pitch = pBitmap->GetPitch(); |
266 uint8_t* dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight); | 267 uint8_t* dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight); |
267 // Safe as checked alloc returned. | 268 // Safe as checked alloc returned. |
268 FX_STRSIZE dest_size = dest_pitch * BitmapHeight; | 269 FX_STRSIZE dest_size = dest_pitch * BitmapHeight; |
269 uint8_t* pDest = dest_buf; | 270 uint8_t* pDest = dest_buf; |
270 if (bCopyWithoutAlpha) { | 271 if (bCopyWithoutAlpha) { |
271 for (int32_t i = 0; i < BitmapHeight; i++) { | 272 for (int32_t i = 0; i < BitmapHeight; i++) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 } | 363 } |
363 if (!ret) { | 364 if (!ret) { |
364 delete m_pDIBSource; | 365 delete m_pDIBSource; |
365 m_pDIBSource = nullptr; | 366 m_pDIBSource = nullptr; |
366 return false; | 367 return false; |
367 } | 368 } |
368 m_pMask = pSource->DetachMask(); | 369 m_pMask = pSource->DetachMask(); |
369 m_MatteColor = pSource->GetMatteColor(); | 370 m_MatteColor = pSource->GetMatteColor(); |
370 return false; | 371 return false; |
371 } | 372 } |
OLD | NEW |