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 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 } | 240 } |
241 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); | 241 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); |
242 dest_pitch = BitmapWidth; | 242 dest_pitch = BitmapWidth; |
243 } else { | 243 } else { |
244 pDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceRGB"); | 244 pDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceRGB"); |
245 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); | 245 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); |
246 dest_pitch = BitmapWidth * 3; | 246 dest_pitch = BitmapWidth * 3; |
247 bCopyWithoutAlpha = false; | 247 bCopyWithoutAlpha = false; |
248 } | 248 } |
249 | 249 |
250 const CFX_DIBitmap* pMaskBitmap = | 250 std::unique_ptr<CFX_DIBitmap> pMaskBitmap; |
251 pBitmap->HasAlpha() ? pBitmap->GetAlphaMask() : nullptr; | 251 if (pBitmap->HasAlpha()) |
| 252 pMaskBitmap = pBitmap->CloneAlphaMask(); |
| 253 |
252 if (pMaskBitmap) { | 254 if (pMaskBitmap) { |
253 int32_t maskWidth = pMaskBitmap->GetWidth(); | 255 int32_t maskWidth = pMaskBitmap->GetWidth(); |
254 int32_t maskHeight = pMaskBitmap->GetHeight(); | 256 int32_t maskHeight = pMaskBitmap->GetHeight(); |
255 uint8_t* mask_buf = nullptr; | 257 uint8_t* mask_buf = nullptr; |
256 FX_STRSIZE mask_size = 0; | 258 FX_STRSIZE mask_size = 0; |
257 auto pMaskDict = | 259 auto pMaskDict = |
258 pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool()); | 260 pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool()); |
259 pMaskDict->SetNewFor<CPDF_Name>("Type", "XObject"); | 261 pMaskDict->SetNewFor<CPDF_Name>("Type", "XObject"); |
260 pMaskDict->SetNewFor<CPDF_Name>("Subtype", "Image"); | 262 pMaskDict->SetNewFor<CPDF_Name>("Subtype", "Image"); |
261 pMaskDict->SetNewFor<CPDF_Number>("Width", maskWidth); | 263 pMaskDict->SetNewFor<CPDF_Number>("Width", maskWidth); |
262 pMaskDict->SetNewFor<CPDF_Number>("Height", maskHeight); | 264 pMaskDict->SetNewFor<CPDF_Number>("Height", maskHeight); |
263 pMaskDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceGray"); | 265 pMaskDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceGray"); |
264 pMaskDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); | 266 pMaskDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); |
265 if (pMaskBitmap->GetFormat() != FXDIB_1bppMask) { | 267 if (pMaskBitmap->GetFormat() != FXDIB_1bppMask) { |
266 mask_buf = FX_Alloc2D(uint8_t, maskHeight, maskWidth); | 268 mask_buf = FX_Alloc2D(uint8_t, maskHeight, maskWidth); |
267 mask_size = maskHeight * maskWidth; // Safe since checked alloc returned. | 269 mask_size = maskHeight * maskWidth; // Safe since checked alloc returned. |
268 for (int32_t a = 0; a < maskHeight; a++) { | 270 for (int32_t a = 0; a < maskHeight; a++) { |
269 FXSYS_memcpy(mask_buf + a * maskWidth, pMaskBitmap->GetScanline(a), | 271 FXSYS_memcpy(mask_buf + a * maskWidth, pMaskBitmap->GetScanline(a), |
270 maskWidth); | 272 maskWidth); |
271 } | 273 } |
272 } | 274 } |
273 pMaskDict->SetNewFor<CPDF_Number>("Length", mask_size); | 275 pMaskDict->SetNewFor<CPDF_Number>("Length", mask_size); |
274 CPDF_Stream* pNewStream = m_pDocument->NewIndirect<CPDF_Stream>( | 276 CPDF_Stream* pNewStream = m_pDocument->NewIndirect<CPDF_Stream>( |
275 mask_buf, mask_size, std::move(pMaskDict)); | 277 mask_buf, mask_size, std::move(pMaskDict)); |
276 pDict->SetNewFor<CPDF_Reference>("SMask", m_pDocument, | 278 pDict->SetNewFor<CPDF_Reference>("SMask", m_pDocument, |
277 pNewStream->GetObjNum()); | 279 pNewStream->GetObjNum()); |
278 delete pMaskBitmap; | |
279 } | 280 } |
280 | 281 |
281 uint8_t* src_buf = pBitmap->GetBuffer(); | 282 uint8_t* src_buf = pBitmap->GetBuffer(); |
282 int32_t src_pitch = pBitmap->GetPitch(); | 283 int32_t src_pitch = pBitmap->GetPitch(); |
283 uint8_t* dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight); | 284 uint8_t* dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight); |
284 // Safe as checked alloc returned. | 285 // Safe as checked alloc returned. |
285 FX_STRSIZE dest_size = dest_pitch * BitmapHeight; | 286 FX_STRSIZE dest_size = dest_pitch * BitmapHeight; |
286 uint8_t* pDest = dest_buf; | 287 uint8_t* pDest = dest_buf; |
287 if (bCopyWithoutAlpha) { | 288 if (bCopyWithoutAlpha) { |
288 for (int32_t i = 0; i < BitmapHeight; i++) { | 289 for (int32_t i = 0; i < BitmapHeight; i++) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 } | 380 } |
380 if (!ret) { | 381 if (!ret) { |
381 delete m_pDIBSource; | 382 delete m_pDIBSource; |
382 m_pDIBSource = nullptr; | 383 m_pDIBSource = nullptr; |
383 return false; | 384 return false; |
384 } | 385 } |
385 m_pMask = pSource->DetachMask(); | 386 m_pMask = pSource->DetachMask(); |
386 m_MatteColor = pSource->GetMatteColor(); | 387 m_MatteColor = pSource->GetMatteColor(); |
387 return false; | 388 return false; |
388 } | 389 } |
OLD | NEW |