From 846fb0301a146319826a04c4a6e2fafadf92813a Mon Sep 17 00:00:00 2001 From: Nicolas Pena Date: Mon, 16 Apr 2018 20:25:17 +0000 Subject: Simplify CFX_DIBitmap::TransferBitmap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This CL changes CFX_DIBitmap::TransferBitmap so that the exception cases are handled before, and early returns are used to reduce nesting. Change-Id: I733adce5de15d345ee160a8b250133836e9d2873 Reviewed-on: https://pdfium-review.googlesource.com/30770 Commit-Queue: Nicolás Peña Moreno Reviewed-by: Henrique Nakashima Reviewed-by: dsinclair --- core/fxge/dib/cfx_dibitmap.cpp | 113 +++++++++++++++++++++++++++-------------- core/fxge/dib/cfx_dibitmap.h | 22 ++++++++ 2 files changed, 97 insertions(+), 38 deletions(-) diff --git a/core/fxge/dib/cfx_dibitmap.cpp b/core/fxge/dib/cfx_dibitmap.cpp index 9fa9776df0..15e821cf11 100644 --- a/core/fxge/dib/cfx_dibitmap.cpp +++ b/core/fxge/dib/cfx_dibitmap.cpp @@ -197,50 +197,87 @@ bool CFX_DIBitmap::TransferBitmap(int dest_left, FXDIB_Format dest_format = GetFormat(); FXDIB_Format src_format = pSrcBitmap->GetFormat(); - if (dest_format == src_format) { - if (GetBPP() == 1) { - for (int row = 0; row < height; row++) { - uint8_t* dest_scan = m_pBuffer.Get() + (dest_top + row) * m_Pitch; - const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row); - for (int col = 0; col < width; col++) { - if (src_scan[(src_left + col) / 8] & - (1 << (7 - (src_left + col) % 8))) { - dest_scan[(dest_left + col) / 8] |= 1 - << (7 - (dest_left + col) % 8); - } else { - dest_scan[(dest_left + col) / 8] &= - ~(1 << (7 - (dest_left + col) % 8)); - } - } - } - } else { - int Bpp = GetBPP() / 8; - for (int row = 0; row < height; row++) { - uint8_t* dest_scan = - m_pBuffer.Get() + (dest_top + row) * m_Pitch + dest_left * Bpp; - const uint8_t* src_scan = - pSrcBitmap->GetScanline(src_top + row) + src_left * Bpp; - memcpy(dest_scan, src_scan, width * Bpp); - } - } - } else { - if (m_pPalette) - return false; + if (dest_format != src_format) { + return TransferWithUnequalFormats(dest_format, dest_left, dest_top, width, + height, pSrcBitmap, src_left, src_top); + } - if (m_bpp == 8) - dest_format = FXDIB_8bppMask; + if (GetBPP() != 1) { + TransferWithMultipleBPP(dest_left, dest_top, width, height, pSrcBitmap, + src_left, src_top); + return true; + } - uint8_t* dest_buf = - m_pBuffer.Get() + dest_top * m_Pitch + dest_left * GetBPP() / 8; - std::unique_ptr d_plt; - if (!ConvertBuffer(dest_format, dest_buf, m_Pitch, width, height, - pSrcBitmap, src_left, src_top, &d_plt)) { - return false; - } + TransferEqualFormatsOneBPP(dest_left, dest_top, width, height, pSrcBitmap, + src_left, src_top); + return true; +} + +bool CFX_DIBitmap::TransferWithUnequalFormats( + FXDIB_Format dest_format, + int dest_left, + int dest_top, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + if (m_pPalette) + return false; + + if (m_bpp == 8) + dest_format = FXDIB_8bppMask; + + uint8_t* dest_buf = + m_pBuffer.Get() + dest_top * m_Pitch + dest_left * GetBPP() / 8; + std::unique_ptr d_plt; + if (!ConvertBuffer(dest_format, dest_buf, m_Pitch, width, height, pSrcBitmap, + src_left, src_top, &d_plt)) { + return false; } return true; } +void CFX_DIBitmap::TransferWithMultipleBPP( + int dest_left, + int dest_top, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + int Bpp = GetBPP() / 8; + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = + m_pBuffer.Get() + (dest_top + row) * m_Pitch + dest_left * Bpp; + const uint8_t* src_scan = + pSrcBitmap->GetScanline(src_top + row) + src_left * Bpp; + memcpy(dest_scan, src_scan, width * Bpp); + } +} + +void CFX_DIBitmap::TransferEqualFormatsOneBPP( + int dest_left, + int dest_top, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = m_pBuffer.Get() + (dest_top + row) * m_Pitch; + const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row); + for (int col = 0; col < width; ++col) { + int src_idx = src_left + col; + int dest_idx = dest_left + col; + if (src_scan[(src_idx) / 8] & (1 << (7 - (src_idx) % 8))) + dest_scan[(dest_idx) / 8] |= 1 << (7 - (dest_idx) % 8); + else + dest_scan[(dest_idx) / 8] &= ~(1 << (7 - (dest_idx) % 8)); + } + } +} + bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, const RetainPtr& pSrcBitmap, FXDIB_Channel srcChannel) { diff --git a/core/fxge/dib/cfx_dibitmap.h b/core/fxge/dib/cfx_dibitmap.h index 00a145af68..105a22d76e 100644 --- a/core/fxge/dib/cfx_dibitmap.h +++ b/core/fxge/dib/cfx_dibitmap.h @@ -124,6 +124,28 @@ class CFX_DIBitmap : public CFX_DIBSource { private: void ConvertBGRColorScale(uint32_t forecolor, uint32_t backcolor); void ConvertCMYKColorScale(uint32_t forecolor, uint32_t backcolor); + bool TransferWithUnequalFormats(FXDIB_Format dest_format, + int dest_left, + int dest_top, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top); + void TransferWithMultipleBPP(int dest_left, + int dest_top, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top); + void TransferEqualFormatsOneBPP(int dest_left, + int dest_top, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top); }; #endif // CORE_FXGE_DIB_CFX_DIBITMAP_H_ -- cgit v1.2.3