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

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

Issue 2510223002: Make CPDF_Dictionary use unique pointers. (Closed)
Patch Set: Plug leaks 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
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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 int32_t num_comps; 90 int32_t num_comps;
91 int32_t bits; 91 int32_t bits;
92 bool color_trans; 92 bool color_trans;
93 if (!CPDF_ModuleMgr::Get()->GetJpegModule()->LoadInfo( 93 if (!CPDF_ModuleMgr::Get()->GetJpegModule()->LoadInfo(
94 pData, size, &width, &height, &num_comps, &bits, &color_trans)) { 94 pData, size, &width, &height, &num_comps, &bits, &color_trans)) {
95 return nullptr; 95 return nullptr;
96 } 96 }
97 97
98 CPDF_Dictionary* pDict = 98 CPDF_Dictionary* pDict =
99 new CPDF_Dictionary(m_pDocument->GetByteStringPool()); 99 new CPDF_Dictionary(m_pDocument->GetByteStringPool());
100 pDict->SetNameFor("Type", "XObject"); 100 pDict->SetNewFor<CPDF_Name>("Type", "XObject");
101 pDict->SetNameFor("Subtype", "Image"); 101 pDict->SetNewFor<CPDF_Name>("Subtype", "Image");
102 pDict->SetIntegerFor("Width", width); 102 pDict->SetNewFor<CPDF_Number>("Width", width);
103 pDict->SetIntegerFor("Height", height); 103 pDict->SetNewFor<CPDF_Number>("Height", height);
104 const FX_CHAR* csname = nullptr; 104 const FX_CHAR* csname = nullptr;
105 if (num_comps == 1) { 105 if (num_comps == 1) {
106 csname = "DeviceGray"; 106 csname = "DeviceGray";
107 } else if (num_comps == 3) { 107 } else if (num_comps == 3) {
108 csname = "DeviceRGB"; 108 csname = "DeviceRGB";
109 } else if (num_comps == 4) { 109 } else if (num_comps == 4) {
110 csname = "DeviceCMYK"; 110 csname = "DeviceCMYK";
111 CPDF_Array* pDecode = new CPDF_Array; 111 CPDF_Array* pDecode = pDict->SetNewFor<CPDF_Array>("Decode");
112 for (int n = 0; n < 4; n++) { 112 for (int n = 0; n < 4; n++) {
113 pDecode->AddNew<CPDF_Number>(1); 113 pDecode->AddNew<CPDF_Number>(1);
114 pDecode->AddNew<CPDF_Number>(0); 114 pDecode->AddNew<CPDF_Number>(0);
115 } 115 }
116 pDict->SetFor("Decode", pDecode);
117 } 116 }
118 pDict->SetNameFor("ColorSpace", csname); 117 pDict->SetNewFor<CPDF_Name>("ColorSpace", csname);
119 pDict->SetIntegerFor("BitsPerComponent", bits); 118 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", bits);
120 pDict->SetNameFor("Filter", "DCTDecode"); 119 pDict->SetNewFor<CPDF_Name>("Filter", "DCTDecode");
121 if (!color_trans) { 120 if (!color_trans) {
122 CPDF_Dictionary* pParms = 121 CPDF_Dictionary* pParms = pDict->SetNewFor<CPDF_Dictionary>("DecodeParms");
123 new CPDF_Dictionary(m_pDocument->GetByteStringPool()); 122 pParms->SetNewFor<CPDF_Number>("ColorTransform", 0);
124 pDict->SetFor("DecodeParms", pParms);
125 pParms->SetIntegerFor("ColorTransform", 0);
126 } 123 }
127 m_bIsMask = false; 124 m_bIsMask = false;
128 m_Width = width; 125 m_Width = width;
129 m_Height = height; 126 m_Height = height;
130 if (!m_pStream) { 127 if (!m_pStream) {
131 m_pOwnedStream = pdfium::MakeUnique<CPDF_Stream>(); 128 m_pOwnedStream = pdfium::MakeUnique<CPDF_Stream>();
132 m_pStream = m_pOwnedStream.get(); 129 m_pStream = m_pOwnedStream.get();
133 } 130 }
134 return pDict; 131 return pDict;
135 } 132 }
(...skipping 23 matching lines...) Expand all
159 int32_t BitmapHeight = pBitmap->GetHeight(); 156 int32_t BitmapHeight = pBitmap->GetHeight();
160 if (BitmapWidth < 1 || BitmapHeight < 1) { 157 if (BitmapWidth < 1 || BitmapHeight < 1) {
161 return; 158 return;
162 } 159 }
163 uint8_t* src_buf = pBitmap->GetBuffer(); 160 uint8_t* src_buf = pBitmap->GetBuffer();
164 int32_t src_pitch = pBitmap->GetPitch(); 161 int32_t src_pitch = pBitmap->GetPitch();
165 int32_t bpp = pBitmap->GetBPP(); 162 int32_t bpp = pBitmap->GetBPP();
166 163
167 CPDF_Dictionary* pDict = 164 CPDF_Dictionary* pDict =
168 new CPDF_Dictionary(m_pDocument->GetByteStringPool()); 165 new CPDF_Dictionary(m_pDocument->GetByteStringPool());
169 pDict->SetNameFor("Type", "XObject"); 166 pDict->SetNewFor<CPDF_Name>("Type", "XObject");
170 pDict->SetNameFor("Subtype", "Image"); 167 pDict->SetNewFor<CPDF_Name>("Subtype", "Image");
171 pDict->SetIntegerFor("Width", BitmapWidth); 168 pDict->SetNewFor<CPDF_Number>("Width", BitmapWidth);
172 pDict->SetIntegerFor("Height", BitmapHeight); 169 pDict->SetNewFor<CPDF_Number>("Height", BitmapHeight);
173 uint8_t* dest_buf = nullptr; 170 uint8_t* dest_buf = nullptr;
174 FX_STRSIZE dest_pitch = 0, dest_size = 0, opType = -1; 171 FX_STRSIZE dest_pitch = 0, dest_size = 0, opType = -1;
175 if (bpp == 1) { 172 if (bpp == 1) {
176 int32_t reset_a = 0, reset_r = 0, reset_g = 0, reset_b = 0; 173 int32_t reset_a = 0, reset_r = 0, reset_g = 0, reset_b = 0;
177 int32_t set_a = 0, set_r = 0, set_g = 0, set_b = 0; 174 int32_t set_a = 0, set_r = 0, set_g = 0, set_b = 0;
178 if (!pBitmap->IsAlphaMask()) { 175 if (!pBitmap->IsAlphaMask()) {
179 ArgbDecode(pBitmap->GetPaletteArgb(0), reset_a, reset_r, reset_g, 176 ArgbDecode(pBitmap->GetPaletteArgb(0), reset_a, reset_r, reset_g,
180 reset_b); 177 reset_b);
181 ArgbDecode(pBitmap->GetPaletteArgb(1), set_a, set_r, set_g, set_b); 178 ArgbDecode(pBitmap->GetPaletteArgb(1), set_a, set_r, set_g, set_b);
182 } 179 }
183 if (set_a == 0 || reset_a == 0) { 180 if (set_a == 0 || reset_a == 0) {
184 pDict->SetFor("ImageMask", new CPDF_Boolean(true)); 181 pDict->SetNewFor<CPDF_Boolean>("ImageMask", true);
185 if (reset_a == 0) { 182 if (reset_a == 0) {
186 CPDF_Array* pArray = new CPDF_Array; 183 CPDF_Array* pArray = pDict->SetNewFor<CPDF_Array>("Decode");
187 pArray->AddNew<CPDF_Number>(1); 184 pArray->AddNew<CPDF_Number>(1);
188 pArray->AddNew<CPDF_Number>(0); 185 pArray->AddNew<CPDF_Number>(0);
189 pDict->SetFor("Decode", pArray);
190 } 186 }
191 } else { 187 } else {
192 CPDF_Array* pCS = new CPDF_Array; 188 CPDF_Array* pCS = pDict->SetNewFor<CPDF_Array>("ColorSpace");
193 pCS->AddNew<CPDF_Name>("Indexed"); 189 pCS->AddNew<CPDF_Name>("Indexed");
194 pCS->AddNew<CPDF_Name>("DeviceRGB"); 190 pCS->AddNew<CPDF_Name>("DeviceRGB");
195 pCS->AddNew<CPDF_Number>(1); 191 pCS->AddNew<CPDF_Number>(1);
196 CFX_ByteString ct; 192 CFX_ByteString ct;
197 FX_CHAR* pBuf = ct.GetBuffer(6); 193 FX_CHAR* pBuf = ct.GetBuffer(6);
198 pBuf[0] = (FX_CHAR)reset_r; 194 pBuf[0] = (FX_CHAR)reset_r;
199 pBuf[1] = (FX_CHAR)reset_g; 195 pBuf[1] = (FX_CHAR)reset_g;
200 pBuf[2] = (FX_CHAR)reset_b; 196 pBuf[2] = (FX_CHAR)reset_b;
201 pBuf[3] = (FX_CHAR)set_r; 197 pBuf[3] = (FX_CHAR)set_r;
202 pBuf[4] = (FX_CHAR)set_g; 198 pBuf[4] = (FX_CHAR)set_g;
203 pBuf[5] = (FX_CHAR)set_b; 199 pBuf[5] = (FX_CHAR)set_b;
204 ct.ReleaseBuffer(6); 200 ct.ReleaseBuffer(6);
205 pCS->AddNew<CPDF_String>(ct, true); 201 pCS->AddNew<CPDF_String>(ct, true);
206 pDict->SetFor("ColorSpace", pCS);
207 } 202 }
208 pDict->SetIntegerFor("BitsPerComponent", 1); 203 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 1);
209 dest_pitch = (BitmapWidth + 7) / 8; 204 dest_pitch = (BitmapWidth + 7) / 8;
210 if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) { 205 if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) {
211 opType = 1; 206 opType = 1;
212 } else { 207 } else {
213 opType = 0; 208 opType = 0;
214 } 209 }
215 } else if (bpp == 8) { 210 } else if (bpp == 8) {
216 int32_t iPalette = pBitmap->GetPaletteSize(); 211 int32_t iPalette = pBitmap->GetPaletteSize();
217 if (iPalette > 0) { 212 if (iPalette > 0) {
218 CPDF_Array* pCS = m_pDocument->NewIndirect<CPDF_Array>(); 213 CPDF_Array* pCS = m_pDocument->NewIndirect<CPDF_Array>();
219 pCS->AddNew<CPDF_Name>("Indexed"); 214 pCS->AddNew<CPDF_Name>("Indexed");
220 pCS->AddNew<CPDF_Name>("DeviceRGB"); 215 pCS->AddNew<CPDF_Name>("DeviceRGB");
221 pCS->AddNew<CPDF_Number>(iPalette - 1); 216 pCS->AddNew<CPDF_Number>(iPalette - 1);
222 uint8_t* pColorTable = FX_Alloc2D(uint8_t, iPalette, 3); 217 uint8_t* pColorTable = FX_Alloc2D(uint8_t, iPalette, 3);
223 uint8_t* ptr = pColorTable; 218 uint8_t* ptr = pColorTable;
224 for (int32_t i = 0; i < iPalette; i++) { 219 for (int32_t i = 0; i < iPalette; i++) {
225 uint32_t argb = pBitmap->GetPaletteArgb(i); 220 uint32_t argb = pBitmap->GetPaletteArgb(i);
226 ptr[0] = (uint8_t)(argb >> 16); 221 ptr[0] = (uint8_t)(argb >> 16);
227 ptr[1] = (uint8_t)(argb >> 8); 222 ptr[1] = (uint8_t)(argb >> 8);
228 ptr[2] = (uint8_t)argb; 223 ptr[2] = (uint8_t)argb;
229 ptr += 3; 224 ptr += 3;
230 } 225 }
231 CPDF_Stream* pCTS = m_pDocument->NewIndirect<CPDF_Stream>( 226 CPDF_Stream* pCTS = m_pDocument->NewIndirect<CPDF_Stream>(
232 pColorTable, iPalette * 3, 227 pColorTable, iPalette * 3,
233 new CPDF_Dictionary(m_pDocument->GetByteStringPool())); 228 new CPDF_Dictionary(m_pDocument->GetByteStringPool()));
234 pCS->AddNew<CPDF_Reference>(m_pDocument, pCTS->GetObjNum()); 229 pCS->AddNew<CPDF_Reference>(m_pDocument, pCTS->GetObjNum());
235 pDict->SetReferenceFor("ColorSpace", m_pDocument, pCS); 230 pDict->SetNewFor<CPDF_Reference>("ColorSpace", m_pDocument,
231 pCS->GetObjNum());
236 } else { 232 } else {
237 pDict->SetNameFor("ColorSpace", "DeviceGray"); 233 pDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceGray");
238 } 234 }
239 pDict->SetIntegerFor("BitsPerComponent", 8); 235 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8);
240 if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) { 236 if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) {
241 dest_pitch = BitmapWidth; 237 dest_pitch = BitmapWidth;
242 opType = 1; 238 opType = 1;
243 } else { 239 } else {
244 opType = 0; 240 opType = 0;
245 } 241 }
246 } else { 242 } else {
247 pDict->SetNameFor("ColorSpace", "DeviceRGB"); 243 pDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceRGB");
248 pDict->SetIntegerFor("BitsPerComponent", 8); 244 pDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8);
249 if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) { 245 if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) {
250 dest_pitch = BitmapWidth * 3; 246 dest_pitch = BitmapWidth * 3;
251 opType = 2; 247 opType = 2;
252 } else { 248 } else {
253 opType = 0; 249 opType = 0;
254 } 250 }
255 } 251 }
256 const CFX_DIBitmap* pMaskBitmap = nullptr; 252 const CFX_DIBitmap* pMaskBitmap = nullptr;
257 bool bDeleteMask = false; 253 bool bDeleteMask = false;
258 if (pBitmap->HasAlpha()) { 254 if (pBitmap->HasAlpha()) {
259 pMaskBitmap = pBitmap->GetAlphaMask(); 255 pMaskBitmap = pBitmap->GetAlphaMask();
260 bDeleteMask = true; 256 bDeleteMask = true;
261 } 257 }
262 if (pMaskBitmap) { 258 if (pMaskBitmap) {
263 int32_t maskWidth = pMaskBitmap->GetWidth(); 259 int32_t maskWidth = pMaskBitmap->GetWidth();
264 int32_t maskHeight = pMaskBitmap->GetHeight(); 260 int32_t maskHeight = pMaskBitmap->GetHeight();
265 uint8_t* mask_buf = nullptr; 261 uint8_t* mask_buf = nullptr;
266 FX_STRSIZE mask_size = 0; 262 FX_STRSIZE mask_size = 0;
267 CPDF_Dictionary* pMaskDict = 263 CPDF_Dictionary* pMaskDict =
268 new CPDF_Dictionary(m_pDocument->GetByteStringPool()); 264 new CPDF_Dictionary(m_pDocument->GetByteStringPool());
269 pMaskDict->SetNameFor("Type", "XObject"); 265 pMaskDict->SetNewFor<CPDF_Name>("Type", "XObject");
270 pMaskDict->SetNameFor("Subtype", "Image"); 266 pMaskDict->SetNewFor<CPDF_Name>("Subtype", "Image");
271 pMaskDict->SetIntegerFor("Width", maskWidth); 267 pMaskDict->SetNewFor<CPDF_Number>("Width", maskWidth);
272 pMaskDict->SetIntegerFor("Height", maskHeight); 268 pMaskDict->SetNewFor<CPDF_Number>("Height", maskHeight);
273 pMaskDict->SetNameFor("ColorSpace", "DeviceGray"); 269 pMaskDict->SetNewFor<CPDF_Name>("ColorSpace", "DeviceGray");
274 pMaskDict->SetIntegerFor("BitsPerComponent", 8); 270 pMaskDict->SetNewFor<CPDF_Number>("BitsPerComponent", 8);
275 if (pMaskBitmap->GetBPP() == 8 && 271 if (pMaskBitmap->GetBPP() == 8 &&
276 (iCompress & PDF_IMAGE_MASK_LOSSY_COMPRESS) != 0) { 272 (iCompress & PDF_IMAGE_MASK_LOSSY_COMPRESS) != 0) {
277 } else if (pMaskBitmap->GetFormat() == FXDIB_1bppMask) { 273 } else if (pMaskBitmap->GetFormat() == FXDIB_1bppMask) {
278 } else { 274 } else {
279 mask_buf = FX_Alloc2D(uint8_t, maskHeight, maskWidth); 275 mask_buf = FX_Alloc2D(uint8_t, maskHeight, maskWidth);
280 mask_size = maskHeight * maskWidth; // Safe since checked alloc returned. 276 mask_size = maskHeight * maskWidth; // Safe since checked alloc returned.
281 for (int32_t a = 0; a < maskHeight; a++) { 277 for (int32_t a = 0; a < maskHeight; a++) {
282 FXSYS_memcpy(mask_buf + a * maskWidth, pMaskBitmap->GetScanline(a), 278 FXSYS_memcpy(mask_buf + a * maskWidth, pMaskBitmap->GetScanline(a),
283 maskWidth); 279 maskWidth);
284 } 280 }
285 } 281 }
286 pMaskDict->SetIntegerFor("Length", mask_size); 282 pMaskDict->SetNewFor<CPDF_Number>("Length", mask_size);
287 pDict->SetReferenceFor( 283 pDict->SetNewFor<CPDF_Reference>(
288 "SMask", m_pDocument, 284 "SMask", m_pDocument,
289 m_pDocument->NewIndirect<CPDF_Stream>(mask_buf, mask_size, pMaskDict)); 285 m_pDocument->NewIndirect<CPDF_Stream>(mask_buf, mask_size, pMaskDict)
286 ->GetObjNum());
290 if (bDeleteMask) 287 if (bDeleteMask)
291 delete pMaskBitmap; 288 delete pMaskBitmap;
292 } 289 }
293 if (opType == 0) { 290 if (opType == 0) {
294 if (iCompress & PDF_IMAGE_LOSSLESS_COMPRESS) { 291 if (iCompress & PDF_IMAGE_LOSSLESS_COMPRESS) {
295 } else { 292 } else {
296 if (pBitmap->GetBPP() == 1) { 293 if (pBitmap->GetBPP() == 1) {
297 } else if (pBitmap->GetBPP() >= 8 && pBitmap->GetPalette()) { 294 } else if (pBitmap->GetBPP() >= 8 && pBitmap->GetPalette()) {
298 CFX_DIBitmap* pNewBitmap = new CFX_DIBitmap(); 295 CFX_DIBitmap* pNewBitmap = new CFX_DIBitmap();
299 pNewBitmap->Copy(pBitmap); 296 pNewBitmap->Copy(pBitmap);
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 } 409 }
413 if (!ret) { 410 if (!ret) {
414 delete m_pDIBSource; 411 delete m_pDIBSource;
415 m_pDIBSource = nullptr; 412 m_pDIBSource = nullptr;
416 return false; 413 return false;
417 } 414 }
418 m_pMask = pSource->DetachMask(); 415 m_pMask = pSource->DetachMask();
419 m_MatteColor = pSource->GetMatteColor(); 416 m_MatteColor = pSource->GetMatteColor();
420 return false; 417 return false;
421 } 418 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698