Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: core/fpdfapi/page/cpdf_image.cpp

Issue 2584683002: Return unique_ptr from CFX_BinaryBuf::DetachBuffer() (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | core/fpdfapi/page/cpdf_streamparser.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 } 212 }
213 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 1); 213 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 1);
214 dest_pitch = (BitmapWidth + 7) / 8; 214 dest_pitch = (BitmapWidth + 7) / 8;
215 } else if (bpp == 8) { 215 } else if (bpp == 8) {
216 int32_t iPalette = pBitmap->GetPaletteSize(); 216 int32_t iPalette = pBitmap->GetPaletteSize();
217 if (iPalette > 0) { 217 if (iPalette > 0) {
218 CPDF_Array* pCS = m_pDocument->NewIndirect<CPDF_Array>(); 218 CPDF_Array* pCS = m_pDocument->NewIndirect<CPDF_Array>();
219 pCS->AddNew<CPDF_Name>("Indexed"); 219 pCS->AddNew<CPDF_Name>("Indexed");
220 pCS->AddNew<CPDF_Name>("DeviceRGB"); 220 pCS->AddNew<CPDF_Name>("DeviceRGB");
221 pCS->AddNew<CPDF_Number>(iPalette - 1); 221 pCS->AddNew<CPDF_Number>(iPalette - 1);
222 uint8_t* pColorTable = FX_Alloc2D(uint8_t, iPalette, 3); 222 std::unique_ptr<uint8_t, FxFreeDeleter> pColorTable(
223 uint8_t* ptr = pColorTable; 223 FX_Alloc2D(uint8_t, iPalette, 3));
224 uint8_t* ptr = pColorTable.get();
224 for (int32_t i = 0; i < iPalette; i++) { 225 for (int32_t i = 0; i < iPalette; i++) {
225 uint32_t argb = pBitmap->GetPaletteArgb(i); 226 uint32_t argb = pBitmap->GetPaletteArgb(i);
226 ptr[0] = (uint8_t)(argb >> 16); 227 ptr[0] = (uint8_t)(argb >> 16);
227 ptr[1] = (uint8_t)(argb >> 8); 228 ptr[1] = (uint8_t)(argb >> 8);
228 ptr[2] = (uint8_t)argb; 229 ptr[2] = (uint8_t)argb;
229 ptr += 3; 230 ptr += 3;
230 } 231 }
231 auto pNewDict = 232 auto pNewDict =
232 pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool()); 233 pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool());
233 CPDF_Stream* pCTS = m_pDocument->NewIndirect<CPDF_Stream>( 234 CPDF_Stream* pCTS = m_pDocument->NewIndirect<CPDF_Stream>(
234 pColorTable, iPalette * 3, std::move(pNewDict)); 235 std::move(pColorTable), iPalette * 3, std::move(pNewDict));
235 pCS->AddNew<CPDF_Reference>(m_pDocument, pCTS->GetObjNum()); 236 pCS->AddNew<CPDF_Reference>(m_pDocument, pCTS->GetObjNum());
236 pDict->SetNewFor<CPDF_Reference>("ColorSpace", m_pDocument, 237 pDict->SetNewFor<CPDF_Reference>("ColorSpace", m_pDocument,
237 pCS->GetObjNum()); 238 pCS->GetObjNum());
238 } else { 239 } else {
239 pDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceGray"); 240 pDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceGray");
240 } 241 }
241 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); 242 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8);
242 dest_pitch = BitmapWidth; 243 dest_pitch = BitmapWidth;
243 } else { 244 } else {
244 pDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceRGB"); 245 pDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceRGB");
245 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); 246 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8);
246 dest_pitch = BitmapWidth * 3; 247 dest_pitch = BitmapWidth * 3;
247 bCopyWithoutAlpha = false; 248 bCopyWithoutAlpha = false;
248 } 249 }
249 250
250 std::unique_ptr<CFX_DIBitmap> pMaskBitmap; 251 std::unique_ptr<CFX_DIBitmap> pMaskBitmap;
251 if (pBitmap->HasAlpha()) 252 if (pBitmap->HasAlpha())
252 pMaskBitmap = pBitmap->CloneAlphaMask(); 253 pMaskBitmap = pBitmap->CloneAlphaMask();
253 254
254 if (pMaskBitmap) { 255 if (pMaskBitmap) {
255 int32_t maskWidth = pMaskBitmap->GetWidth(); 256 int32_t maskWidth = pMaskBitmap->GetWidth();
256 int32_t maskHeight = pMaskBitmap->GetHeight(); 257 int32_t maskHeight = pMaskBitmap->GetHeight();
257 uint8_t* mask_buf = nullptr; 258 std::unique_ptr<uint8_t, FxFreeDeleter> mask_buf;
258 FX_STRSIZE mask_size = 0; 259 FX_STRSIZE mask_size = 0;
259 auto pMaskDict = 260 auto pMaskDict =
260 pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool()); 261 pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool());
261 pMaskDict->SetNewFor<CPDF_Name>("Type", "XObject"); 262 pMaskDict->SetNewFor<CPDF_Name>("Type", "XObject");
262 pMaskDict->SetNewFor<CPDF_Name>("Subtype", "Image"); 263 pMaskDict->SetNewFor<CPDF_Name>("Subtype", "Image");
263 pMaskDict->SetNewFor<CPDF_Number>("Width", maskWidth); 264 pMaskDict->SetNewFor<CPDF_Number>("Width", maskWidth);
264 pMaskDict->SetNewFor<CPDF_Number>("Height", maskHeight); 265 pMaskDict->SetNewFor<CPDF_Number>("Height", maskHeight);
265 pMaskDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceGray"); 266 pMaskDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceGray");
266 pMaskDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8); 267 pMaskDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8);
267 if (pMaskBitmap->GetFormat() != FXDIB_1bppMask) { 268 if (pMaskBitmap->GetFormat() != FXDIB_1bppMask) {
268 mask_buf = FX_Alloc2D(uint8_t, maskHeight, maskWidth); 269 mask_buf.reset(FX_Alloc2D(uint8_t, maskHeight, maskWidth));
269 mask_size = maskHeight * maskWidth; // Safe since checked alloc returned. 270 mask_size = maskHeight * maskWidth; // Safe since checked alloc returned.
270 for (int32_t a = 0; a < maskHeight; a++) { 271 for (int32_t a = 0; a < maskHeight; a++) {
271 FXSYS_memcpy(mask_buf + a * maskWidth, pMaskBitmap->GetScanline(a), 272 FXSYS_memcpy(mask_buf.get() + a * maskWidth,
272 maskWidth); 273 pMaskBitmap->GetScanline(a), maskWidth);
273 } 274 }
274 } 275 }
275 pMaskDict->SetNewFor<CPDF_Number>("Length", mask_size); 276 pMaskDict->SetNewFor<CPDF_Number>("Length", mask_size);
276 CPDF_Stream* pNewStream = m_pDocument->NewIndirect<CPDF_Stream>( 277 CPDF_Stream* pNewStream = m_pDocument->NewIndirect<CPDF_Stream>(
277 mask_buf, mask_size, std::move(pMaskDict)); 278 std::move(mask_buf), mask_size, std::move(pMaskDict));
278 pDict->SetNewFor<CPDF_Reference>("SMask", m_pDocument, 279 pDict->SetNewFor<CPDF_Reference>("SMask", m_pDocument,
279 pNewStream->GetObjNum()); 280 pNewStream->GetObjNum());
280 } 281 }
281 282
282 uint8_t* src_buf = pBitmap->GetBuffer(); 283 uint8_t* src_buf = pBitmap->GetBuffer();
283 int32_t src_pitch = pBitmap->GetPitch(); 284 int32_t src_pitch = pBitmap->GetPitch();
284 uint8_t* dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight); 285 uint8_t* dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight);
285 // Safe as checked alloc returned. 286 // Safe as checked alloc returned.
286 FX_STRSIZE dest_size = dest_pitch * BitmapHeight; 287 FX_STRSIZE dest_size = dest_pitch * BitmapHeight;
287 uint8_t* pDest = dest_buf; 288 uint8_t* pDest = dest_buf;
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 } 381 }
381 if (!ret) { 382 if (!ret) {
382 delete m_pDIBSource; 383 delete m_pDIBSource;
383 m_pDIBSource = nullptr; 384 m_pDIBSource = nullptr;
384 return false; 385 return false;
385 } 386 }
386 m_pMask = pSource->DetachMask(); 387 m_pMask = pSource->DetachMask();
387 m_MatteColor = pSource->GetMatteColor(); 388 m_MatteColor = pSource->GetMatteColor();
388 return false; 389 return false;
389 } 390 }
OLDNEW
« no previous file with comments | « no previous file | core/fpdfapi/page/cpdf_streamparser.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698