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

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

Issue 2520133002: Remove some WrapUnique() calls by returing unique_ptrs (Closed)
Patch Set: rebase Created 4 years, 1 month 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 | « core/fpdfapi/page/cpdf_image.h ('k') | core/fpdfapi/page/cpdf_streamcontentparser.h » ('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>
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 #include "core/fpdfapi/cpdf_modulemgr.h" 14 #include "core/fpdfapi/cpdf_modulemgr.h"
15 #include "core/fpdfapi/page/cpdf_page.h" 15 #include "core/fpdfapi/page/cpdf_page.h"
16 #include "core/fpdfapi/parser/cpdf_array.h" 16 #include "core/fpdfapi/parser/cpdf_array.h"
17 #include "core/fpdfapi/parser/cpdf_boolean.h" 17 #include "core/fpdfapi/parser/cpdf_boolean.h"
18 #include "core/fpdfapi/parser/cpdf_dictionary.h" 18 #include "core/fpdfapi/parser/cpdf_dictionary.h"
19 #include "core/fpdfapi/parser/cpdf_document.h" 19 #include "core/fpdfapi/parser/cpdf_document.h"
20 #include "core/fpdfapi/parser/cpdf_name.h" 20 #include "core/fpdfapi/parser/cpdf_name.h"
21 #include "core/fpdfapi/parser/cpdf_number.h" 21 #include "core/fpdfapi/parser/cpdf_number.h"
22 #include "core/fpdfapi/parser/cpdf_reference.h" 22 #include "core/fpdfapi/parser/cpdf_reference.h"
23 #include "core/fpdfapi/parser/cpdf_stream.h" 23 #include "core/fpdfapi/parser/cpdf_stream.h"
24 #include "core/fpdfapi/parser/cpdf_string.h" 24 #include "core/fpdfapi/parser/cpdf_string.h"
25 #include "core/fpdfapi/render/cpdf_pagerendercache.h" 25 #include "core/fpdfapi/render/cpdf_pagerendercache.h"
26 #include "core/fpdfapi/render/render_int.h" 26 #include "core/fpdfapi/render/render_int.h"
27 #include "core/fxcodec/fx_codec.h" 27 #include "core/fxcodec/fx_codec.h"
28 #include "core/fxge/fx_dib.h" 28 #include "core/fxge/fx_dib.h"
29 #include "third_party/base/numerics/safe_conversions.h"
29 #include "third_party/base/ptr_util.h" 30 #include "third_party/base/ptr_util.h"
30 31
31 CPDF_Image::CPDF_Image(CPDF_Document* pDoc) : m_pDocument(pDoc) {} 32 CPDF_Image::CPDF_Image(CPDF_Document* pDoc) : m_pDocument(pDoc) {}
32 33
33 CPDF_Image::CPDF_Image(CPDF_Document* pDoc, 34 CPDF_Image::CPDF_Image(CPDF_Document* pDoc,
34 std::unique_ptr<CPDF_Stream> pStream) 35 std::unique_ptr<CPDF_Stream> pStream)
35 : m_bIsInline(true), 36 : m_bIsInline(true),
36 m_pDocument(pDoc), 37 m_pDocument(pDoc),
37 m_pStream(pStream.get()), 38 m_pStream(pStream.get()),
38 m_pOwnedStream(std::move(pStream)) { 39 m_pOwnedStream(std::move(pStream)) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 } 80 }
80 81
81 void CPDF_Image::ConvertStreamToIndirectObject() { 82 void CPDF_Image::ConvertStreamToIndirectObject() {
82 if (!m_pStream->IsInline()) 83 if (!m_pStream->IsInline())
83 return; 84 return;
84 85
85 ASSERT(m_pOwnedStream); 86 ASSERT(m_pOwnedStream);
86 m_pDocument->AddIndirectObject(std::move(m_pOwnedStream)); 87 m_pDocument->AddIndirectObject(std::move(m_pOwnedStream));
87 } 88 }
88 89
89 CPDF_Dictionary* CPDF_Image::InitJPEG(uint8_t* pData, uint32_t size) { 90 std::unique_ptr<CPDF_Dictionary> CPDF_Image::InitJPEG(uint8_t* pData,
91 uint32_t size) {
90 int32_t width; 92 int32_t width;
91 int32_t height; 93 int32_t height;
92 int32_t num_comps; 94 int32_t num_comps;
93 int32_t bits; 95 int32_t bits;
94 bool color_trans; 96 bool color_trans;
95 if (!CPDF_ModuleMgr::Get()->GetJpegModule()->LoadInfo( 97 if (!CPDF_ModuleMgr::Get()->GetJpegModule()->LoadInfo(
96 pData, size, &width, &height, &num_comps, &bits, &color_trans)) { 98 pData, size, &width, &height, &num_comps, &bits, &color_trans)) {
97 return nullptr; 99 return nullptr;
98 } 100 }
99 101
100 CPDF_Dictionary* pDict = 102 auto pDict =
101 new CPDF_Dictionary(m_pDocument->GetByteStringPool()); 103 pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool());
102 pDict->SetNewFor<CPDF_Name>("Type", "XObject"); 104 pDict->SetNewFor<CPDF_Name>("Type", "XObject");
103 pDict->SetNewFor<CPDF_Name>("Subtype", "Image"); 105 pDict->SetNewFor<CPDF_Name>("Subtype", "Image");
104 pDict->SetNewFor<CPDF_Number>("Width", width); 106 pDict->SetNewFor<CPDF_Number>("Width", width);
105 pDict->SetNewFor<CPDF_Number>("Height", height); 107 pDict->SetNewFor<CPDF_Number>("Height", height);
106 const FX_CHAR* csname = nullptr; 108 const FX_CHAR* csname = nullptr;
107 if (num_comps == 1) { 109 if (num_comps == 1) {
108 csname = "DeviceGray"; 110 csname = "DeviceGray";
109 } else if (num_comps == 3) { 111 } else if (num_comps == 3) {
110 csname = "DeviceRGB"; 112 csname = "DeviceRGB";
111 } else if (num_comps == 4) { 113 } else if (num_comps == 4) {
(...skipping 15 matching lines...) Expand all
127 m_Width = width; 129 m_Width = width;
128 m_Height = height; 130 m_Height = height;
129 if (!m_pStream) { 131 if (!m_pStream) {
130 m_pOwnedStream = pdfium::MakeUnique<CPDF_Stream>(); 132 m_pOwnedStream = pdfium::MakeUnique<CPDF_Stream>();
131 m_pStream = m_pOwnedStream.get(); 133 m_pStream = m_pOwnedStream.get();
132 } 134 }
133 return pDict; 135 return pDict;
134 } 136 }
135 137
136 void CPDF_Image::SetJpegImage(IFX_SeekableReadStream* pFile) { 138 void CPDF_Image::SetJpegImage(IFX_SeekableReadStream* pFile) {
137 uint32_t size = (uint32_t)pFile->GetSize(); 139 uint32_t size = pdfium::base::checked_cast<uint32_t>(pFile->GetSize());
138 if (!size) 140 if (!size)
139 return; 141 return;
140 142
141 uint32_t dwEstimateSize = std::min(size, 8192U); 143 uint32_t dwEstimateSize = std::min(size, 8192U);
142 std::vector<uint8_t> data(dwEstimateSize); 144 std::vector<uint8_t> data(dwEstimateSize);
143 pFile->ReadBlock(data.data(), 0, dwEstimateSize); 145 if (!pFile->ReadBlock(data.data(), 0, dwEstimateSize))
144 CPDF_Dictionary* pDict = InitJPEG(data.data(), dwEstimateSize); 146 return;
147
148 std::unique_ptr<CPDF_Dictionary> pDict =
149 InitJPEG(data.data(), dwEstimateSize);
145 if (!pDict && size > dwEstimateSize) { 150 if (!pDict && size > dwEstimateSize) {
146 data.resize(size); 151 data.resize(size);
147 pFile->ReadBlock(data.data(), 0, size); 152 pFile->ReadBlock(data.data(), 0, size);
148 pDict = InitJPEG(data.data(), size); 153 pDict = InitJPEG(data.data(), size);
149 } 154 }
150 if (!pDict) 155 if (!pDict)
151 return; 156 return;
152 157
153 m_pStream->InitStreamFromFile(pFile, pdfium::WrapUnique(pDict)); 158 m_pStream->InitStreamFromFile(pFile, std::move(pDict));
154 } 159 }
155 160
156 void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap) { 161 void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap) {
157 int32_t BitmapWidth = pBitmap->GetWidth(); 162 int32_t BitmapWidth = pBitmap->GetWidth();
158 int32_t BitmapHeight = pBitmap->GetHeight(); 163 int32_t BitmapHeight = pBitmap->GetHeight();
159 if (BitmapWidth < 1 || BitmapHeight < 1) 164 if (BitmapWidth < 1 || BitmapHeight < 1)
160 return; 165 return;
161 166
162 auto pDict = 167 auto pDict =
163 pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool()); 168 pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool());
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 } 381 }
377 if (!ret) { 382 if (!ret) {
378 delete m_pDIBSource; 383 delete m_pDIBSource;
379 m_pDIBSource = nullptr; 384 m_pDIBSource = nullptr;
380 return false; 385 return false;
381 } 386 }
382 m_pMask = pSource->DetachMask(); 387 m_pMask = pSource->DetachMask();
383 m_MatteColor = pSource->GetMatteColor(); 388 m_MatteColor = pSource->GetMatteColor();
384 return false; 389 return false;
385 } 390 }
OLDNEW
« no previous file with comments | « core/fpdfapi/page/cpdf_image.h ('k') | core/fpdfapi/page/cpdf_streamcontentparser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698