| 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/fpdf_page/include/cpdf_image.h" | 7 #include "core/fpdfapi/fpdf_page/include/cpdf_image.h" |
| 8 | 8 |
| 9 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" | 9 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
| 10 #include "core/fpdfapi/include/cpdf_modulemgr.h" | 10 #include "core/fpdfapi/include/cpdf_modulemgr.h" |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 pFile->ReadBlock(pData, 0, size); | 146 pFile->ReadBlock(pData, 0, size); |
| 147 pDict = InitJPEG(pData, size); | 147 pDict = InitJPEG(pData, size); |
| 148 FX_Free(pData); | 148 FX_Free(pData); |
| 149 } | 149 } |
| 150 if (!pDict) { | 150 if (!pDict) { |
| 151 return; | 151 return; |
| 152 } | 152 } |
| 153 m_pStream->InitStreamFromFile(pFile, pDict); | 153 m_pStream->InitStreamFromFile(pFile, pDict); |
| 154 } | 154 } |
| 155 | 155 |
| 156 void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, | 156 void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, int32_t iCompress) { |
| 157 int32_t iCompress, | |
| 158 IFX_FileWrite* pFileWrite, | |
| 159 IFX_FileRead* pFileRead, | |
| 160 const CFX_DIBitmap* pMask) { | |
| 161 int32_t BitmapWidth = pBitmap->GetWidth(); | 157 int32_t BitmapWidth = pBitmap->GetWidth(); |
| 162 int32_t BitmapHeight = pBitmap->GetHeight(); | 158 int32_t BitmapHeight = pBitmap->GetHeight(); |
| 163 if (BitmapWidth < 1 || BitmapHeight < 1) { | 159 if (BitmapWidth < 1 || BitmapHeight < 1) { |
| 164 return; | 160 return; |
| 165 } | 161 } |
| 166 uint8_t* src_buf = pBitmap->GetBuffer(); | 162 uint8_t* src_buf = pBitmap->GetBuffer(); |
| 167 int32_t src_pitch = pBitmap->GetPitch(); | 163 int32_t src_pitch = pBitmap->GetPitch(); |
| 168 int32_t bpp = pBitmap->GetBPP(); | 164 int32_t bpp = pBitmap->GetBPP(); |
| 169 | 165 |
| 170 CPDF_Dictionary* pDict = new CPDF_Dictionary; | 166 CPDF_Dictionary* pDict = new CPDF_Dictionary; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 } else { | 251 } else { |
| 256 opType = 0; | 252 opType = 0; |
| 257 } | 253 } |
| 258 } | 254 } |
| 259 const CFX_DIBitmap* pMaskBitmap = NULL; | 255 const CFX_DIBitmap* pMaskBitmap = NULL; |
| 260 FX_BOOL bDeleteMask = FALSE; | 256 FX_BOOL bDeleteMask = FALSE; |
| 261 if (pBitmap->HasAlpha()) { | 257 if (pBitmap->HasAlpha()) { |
| 262 pMaskBitmap = pBitmap->GetAlphaMask(); | 258 pMaskBitmap = pBitmap->GetAlphaMask(); |
| 263 bDeleteMask = TRUE; | 259 bDeleteMask = TRUE; |
| 264 } | 260 } |
| 265 if (!pMaskBitmap && pMask) { | |
| 266 FXDIB_Format maskFormat = pMask->GetFormat(); | |
| 267 if (maskFormat == FXDIB_1bppMask || maskFormat == FXDIB_8bppMask) { | |
| 268 pMaskBitmap = pMask; | |
| 269 } | |
| 270 } | |
| 271 if (pMaskBitmap) { | 261 if (pMaskBitmap) { |
| 272 int32_t maskWidth = pMaskBitmap->GetWidth(); | 262 int32_t maskWidth = pMaskBitmap->GetWidth(); |
| 273 int32_t maskHeight = pMaskBitmap->GetHeight(); | 263 int32_t maskHeight = pMaskBitmap->GetHeight(); |
| 274 uint8_t* mask_buf = NULL; | 264 uint8_t* mask_buf = NULL; |
| 275 FX_STRSIZE mask_size = 0; | 265 FX_STRSIZE mask_size = 0; |
| 276 CPDF_Dictionary* pMaskDict = new CPDF_Dictionary; | 266 CPDF_Dictionary* pMaskDict = new CPDF_Dictionary; |
| 277 pMaskDict->SetAtName("Type", "XObject"); | 267 pMaskDict->SetAtName("Type", "XObject"); |
| 278 pMaskDict->SetAtName("Subtype", "Image"); | 268 pMaskDict->SetAtName("Subtype", "Image"); |
| 279 pMaskDict->SetAtInteger("Width", maskWidth); | 269 pMaskDict->SetAtInteger("Width", maskWidth); |
| 280 pMaskDict->SetAtInteger("Height", maskHeight); | 270 pMaskDict->SetAtInteger("Height", maskHeight); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 293 } | 283 } |
| 294 pMaskDict->SetAtInteger("Length", mask_size); | 284 pMaskDict->SetAtInteger("Length", mask_size); |
| 295 | 285 |
| 296 CPDF_Stream* pMaskStream = new CPDF_Stream(mask_buf, mask_size, pMaskDict); | 286 CPDF_Stream* pMaskStream = new CPDF_Stream(mask_buf, mask_size, pMaskDict); |
| 297 m_pDocument->AddIndirectObject(pMaskStream); | 287 m_pDocument->AddIndirectObject(pMaskStream); |
| 298 pDict->SetAtReference("SMask", m_pDocument, pMaskStream); | 288 pDict->SetAtReference("SMask", m_pDocument, pMaskStream); |
| 299 if (bDeleteMask) { | 289 if (bDeleteMask) { |
| 300 delete pMaskBitmap; | 290 delete pMaskBitmap; |
| 301 } | 291 } |
| 302 } | 292 } |
| 303 FX_BOOL bStream = pFileWrite && pFileRead; | |
| 304 if (opType == 0) { | 293 if (opType == 0) { |
| 305 if (iCompress & PDF_IMAGE_LOSSLESS_COMPRESS) { | 294 if (iCompress & PDF_IMAGE_LOSSLESS_COMPRESS) { |
| 306 } else { | 295 } else { |
| 307 if (pBitmap->GetBPP() == 1) { | 296 if (pBitmap->GetBPP() == 1) { |
| 308 } else if (pBitmap->GetBPP() >= 8 && pBitmap->GetPalette()) { | 297 } else if (pBitmap->GetBPP() >= 8 && pBitmap->GetPalette()) { |
| 309 CFX_DIBitmap* pNewBitmap = new CFX_DIBitmap(); | 298 CFX_DIBitmap* pNewBitmap = new CFX_DIBitmap(); |
| 310 pNewBitmap->Copy(pBitmap); | 299 pNewBitmap->Copy(pBitmap); |
| 311 pNewBitmap->ConvertFormat(FXDIB_Rgb); | 300 pNewBitmap->ConvertFormat(FXDIB_Rgb); |
| 312 SetImage(pNewBitmap, iCompress, pFileWrite, pFileRead); | 301 SetImage(pNewBitmap, iCompress); |
| 313 if (pDict) { | 302 if (pDict) { |
| 314 pDict->Release(); | 303 pDict->Release(); |
| 315 pDict = NULL; | 304 pDict = NULL; |
| 316 } | 305 } |
| 317 FX_Free(dest_buf); | 306 FX_Free(dest_buf); |
| 318 dest_buf = NULL; | 307 dest_buf = NULL; |
| 319 dest_size = 0; | 308 dest_size = 0; |
| 320 delete pNewBitmap; | 309 delete pNewBitmap; |
| 321 return; | 310 return; |
| 322 } | 311 } |
| 323 } | 312 } |
| 324 if (bStream) { | |
| 325 pFileWrite->WriteBlock(dest_buf, dest_size); | |
| 326 FX_Free(dest_buf); | |
| 327 dest_buf = NULL; | |
| 328 } | |
| 329 } else if (opType == 1) { | 313 } else if (opType == 1) { |
| 330 if (!bStream) { | 314 dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight); |
| 331 dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight); | 315 dest_size = dest_pitch * BitmapHeight; // Safe as checked alloc returned. |
| 332 dest_size = | 316 |
| 333 dest_pitch * BitmapHeight; // Safe since checked alloc returned. | |
| 334 } | |
| 335 uint8_t* pDest = dest_buf; | 317 uint8_t* pDest = dest_buf; |
| 336 for (int32_t i = 0; i < BitmapHeight; i++) { | 318 for (int32_t i = 0; i < BitmapHeight; i++) { |
| 337 if (!bStream) { | 319 FXSYS_memcpy(pDest, src_buf, dest_pitch); |
| 338 FXSYS_memcpy(pDest, src_buf, dest_pitch); | 320 pDest += dest_pitch; |
| 339 pDest += dest_pitch; | |
| 340 } else { | |
| 341 pFileWrite->WriteBlock(src_buf, dest_pitch); | |
| 342 } | |
| 343 src_buf += src_pitch; | 321 src_buf += src_pitch; |
| 344 } | 322 } |
| 345 } else if (opType == 2) { | 323 } else if (opType == 2) { |
| 346 if (!bStream) { | 324 dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight); |
| 347 dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight); | 325 dest_size = dest_pitch * BitmapHeight; // Safe as checked alloc returned. |
| 348 dest_size = | 326 |
| 349 dest_pitch * BitmapHeight; // Safe since checked alloc returned. | |
| 350 } else { | |
| 351 dest_buf = FX_Alloc(uint8_t, dest_pitch); | |
| 352 } | |
| 353 uint8_t* pDest = dest_buf; | 327 uint8_t* pDest = dest_buf; |
| 354 int32_t src_offset = 0; | 328 int32_t src_offset = 0; |
| 355 int32_t dest_offset = 0; | 329 int32_t dest_offset = 0; |
| 356 for (int32_t row = 0; row < BitmapHeight; row++) { | 330 for (int32_t row = 0; row < BitmapHeight; row++) { |
| 357 src_offset = row * src_pitch; | 331 src_offset = row * src_pitch; |
| 358 for (int32_t column = 0; column < BitmapWidth; column++) { | 332 for (int32_t column = 0; column < BitmapWidth; column++) { |
| 359 FX_FLOAT alpha = 1; | 333 FX_FLOAT alpha = 1; |
| 360 pDest[dest_offset] = (uint8_t)(src_buf[src_offset + 2] * alpha); | 334 pDest[dest_offset] = (uint8_t)(src_buf[src_offset + 2] * alpha); |
| 361 pDest[dest_offset + 1] = (uint8_t)(src_buf[src_offset + 1] * alpha); | 335 pDest[dest_offset + 1] = (uint8_t)(src_buf[src_offset + 1] * alpha); |
| 362 pDest[dest_offset + 2] = (uint8_t)(src_buf[src_offset] * alpha); | 336 pDest[dest_offset + 2] = (uint8_t)(src_buf[src_offset] * alpha); |
| 363 dest_offset += 3; | 337 dest_offset += 3; |
| 364 src_offset += bpp == 24 ? 3 : 4; | 338 src_offset += bpp == 24 ? 3 : 4; |
| 365 } | 339 } |
| 366 if (bStream) { | 340 |
| 367 pFileWrite->WriteBlock(pDest, dest_pitch); | 341 pDest += dest_pitch; |
| 368 pDest = dest_buf; | |
| 369 } else { | |
| 370 pDest += dest_pitch; | |
| 371 } | |
| 372 dest_offset = 0; | 342 dest_offset = 0; |
| 373 } | 343 } |
| 374 if (bStream) { | |
| 375 FX_Free(dest_buf); | |
| 376 dest_buf = NULL; | |
| 377 } | |
| 378 } | 344 } |
| 379 if (!m_pStream) { | 345 if (!m_pStream) { |
| 380 m_pStream = new CPDF_Stream(NULL, 0, NULL); | 346 m_pStream = new CPDF_Stream(NULL, 0, NULL); |
| 381 } | 347 } |
| 382 if (!bStream) { | 348 m_pStream->InitStream(dest_buf, dest_size, pDict); |
| 383 m_pStream->InitStream(dest_buf, dest_size, pDict); | |
| 384 } else { | |
| 385 pFileWrite->Flush(); | |
| 386 m_pStream->InitStreamFromFile(pFileRead, pDict); | |
| 387 } | |
| 388 m_bIsMask = pBitmap->IsAlphaMask(); | 349 m_bIsMask = pBitmap->IsAlphaMask(); |
| 389 m_Width = BitmapWidth; | 350 m_Width = BitmapWidth; |
| 390 m_Height = BitmapHeight; | 351 m_Height = BitmapHeight; |
| 391 FX_Free(dest_buf); | 352 FX_Free(dest_buf); |
| 392 } | 353 } |
| 393 | 354 |
| 394 void CPDF_Image::ResetCache(CPDF_Page* pPage, const CFX_DIBitmap* pBitmap) { | 355 void CPDF_Image::ResetCache(CPDF_Page* pPage, const CFX_DIBitmap* pBitmap) { |
| 395 pPage->GetRenderCache()->ResetBitmap(m_pStream, pBitmap); | 356 pPage->GetRenderCache()->ResetBitmap(m_pStream, pBitmap); |
| 396 } | 357 } |
| 397 | 358 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 } | 413 } |
| 453 if (!ret) { | 414 if (!ret) { |
| 454 delete m_pDIBSource; | 415 delete m_pDIBSource; |
| 455 m_pDIBSource = nullptr; | 416 m_pDIBSource = nullptr; |
| 456 return FALSE; | 417 return FALSE; |
| 457 } | 418 } |
| 458 m_pMask = pSource->DetachMask(); | 419 m_pMask = pSource->DetachMask(); |
| 459 m_MatteColor = pSource->GetMatteColor(); | 420 m_MatteColor = pSource->GetMatteColor(); |
| 460 return FALSE; | 421 return FALSE; |
| 461 } | 422 } |
| OLD | NEW |