Chromium Code Reviews| Index: core/fxge/dib/fx_dib_main.cpp |
| diff --git a/core/fxge/dib/fx_dib_main.cpp b/core/fxge/dib/fx_dib_main.cpp |
| index 83899553a2bacdb98046b996d124557c33197495..593b844486ad7a256b330c31f1ce741863d65836 100644 |
| --- a/core/fxge/dib/fx_dib_main.cpp |
| +++ b/core/fxge/dib/fx_dib_main.cpp |
| @@ -13,6 +13,7 @@ |
| #include <utility> |
| #include "core/fxcodec/fx_codec.h" |
| +#include "core/fxcrt/cfx_maybe_owned.h" |
| #include "core/fxge/cfx_gemodule.h" |
| #include "core/fxge/dib/dib_int.h" |
| #include "core/fxge/ge/cfx_cliprgn.h" |
| @@ -174,15 +175,14 @@ void CFX_DIBitmap::TakeOver(CFX_DIBitmap* pSrcBitmap) { |
| m_Pitch = pSrcBitmap->m_Pitch; |
| } |
| -CFX_DIBitmap* CFX_DIBSource::Clone(const FX_RECT* pClip) const { |
| +std::unique_ptr<CFX_DIBitmap> CFX_DIBSource::Clone(const FX_RECT* pClip) const { |
| FX_RECT rect(0, 0, m_Width, m_Height); |
| if (pClip) { |
| rect.Intersect(*pClip); |
| - if (rect.IsEmpty()) { |
| + if (rect.IsEmpty()) |
| return nullptr; |
| - } |
| } |
| - std::unique_ptr<CFX_DIBitmap> pNewBitmap(new CFX_DIBitmap); |
| + auto pNewBitmap = pdfium::MakeUnique<CFX_DIBitmap>(); |
| if (!pNewBitmap->Create(rect.Width(), rect.Height(), GetFormat())) |
| return nullptr; |
| @@ -202,22 +202,22 @@ CFX_DIBitmap* CFX_DIBSource::Clone(const FX_RECT* pClip) const { |
| } |
| } else { |
| int copy_len = (pNewBitmap->GetWidth() * pNewBitmap->GetBPP() + 7) / 8; |
| - if (m_Pitch < (uint32_t)copy_len) { |
| + if (m_Pitch < (uint32_t)copy_len) |
| copy_len = m_Pitch; |
| - } |
| + |
| for (int row = rect.top; row < rect.bottom; row++) { |
| const uint8_t* src_scan = GetScanline(row) + rect.left * m_bpp / 8; |
| uint8_t* dest_scan = (uint8_t*)pNewBitmap->GetScanline(row - rect.top); |
| FXSYS_memcpy(dest_scan, src_scan, copy_len); |
| } |
| } |
| - return pNewBitmap.release(); |
| + return pNewBitmap; |
| } |
| void CFX_DIBSource::BuildPalette() { |
| - if (m_pPalette) { |
| + if (m_pPalette) |
| return; |
| - } |
| + |
| if (GetBPP() == 1) { |
| m_pPalette.reset(FX_Alloc(uint32_t, 2)); |
| if (IsCmykImage()) { |
| @@ -230,13 +230,11 @@ void CFX_DIBSource::BuildPalette() { |
| } else if (GetBPP() == 8) { |
| m_pPalette.reset(FX_Alloc(uint32_t, 256)); |
| if (IsCmykImage()) { |
| - for (int i = 0; i < 256; i++) { |
| + for (int i = 0; i < 256; i++) |
| m_pPalette.get()[i] = 0xff - i; |
| - } |
| } else { |
| - for (int i = 0; i < 256; i++) { |
| + for (int i = 0; i < 256; i++) |
| m_pPalette.get()[i] = 0xff000000 | (i * 0x10101); |
| - } |
| } |
| } |
| } |
| @@ -663,127 +661,92 @@ bool CFX_DIBSource::CopyAlphaMask(const CFX_DIBSource* pAlphaMask, |
| const int g_ChannelOffset[] = {0, 2, 1, 0, 0, 1, 2, 3, 3}; |
| bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, |
| - const CFX_DIBSource* pSrcBitmap, |
| + CFX_DIBSource* pSrcBitmap, |
| FXDIB_Channel srcChannel) { |
| - if (!m_pBuffer) { |
| + if (!m_pBuffer) |
| return false; |
| - } |
| - CFX_DIBSource* pSrcClone = (CFX_DIBSource*)pSrcBitmap; |
| - CFX_DIBitmap* pDst = this; |
| - int destOffset, srcOffset; |
| + |
| + CFX_MaybeOwned<CFX_DIBSource> pSrcClone(pSrcBitmap); |
| + int srcOffset; |
| if (srcChannel == FXDIB_Alpha) { |
| - if (!pSrcBitmap->HasAlpha() && !pSrcBitmap->IsAlphaMask()) { |
| + if (!pSrcBitmap->HasAlpha() && !pSrcBitmap->IsAlphaMask()) |
| return false; |
| - } |
| + |
| if (pSrcBitmap->GetBPP() == 1) { |
| pSrcClone = pSrcBitmap->CloneConvert(FXDIB_8bppMask); |
| - if (!pSrcClone) { |
| + if (!pSrcClone) |
| return false; |
| - } |
| - } |
| - if (pSrcBitmap->GetFormat() == FXDIB_Argb) { |
| - srcOffset = 3; |
| - } else { |
| - srcOffset = 0; |
| } |
| + srcOffset = pSrcBitmap->GetFormat() == FXDIB_Argb ? 3 : 0; |
| } else { |
| - if (pSrcBitmap->IsAlphaMask()) { |
| + if (pSrcBitmap->IsAlphaMask()) |
| return false; |
| - } |
| + |
| if (pSrcBitmap->GetBPP() < 24) { |
| if (pSrcBitmap->IsCmykImage()) { |
| - pSrcClone = pSrcBitmap->CloneConvert( |
| - (FXDIB_Format)((pSrcBitmap->GetFormat() & 0xff00) | 0x20)); |
| + pSrcClone = pSrcBitmap->CloneConvert(static_cast<FXDIB_Format>( |
| + (pSrcBitmap->GetFormat() & 0xff00) | 0x20)); |
| } else { |
| - pSrcClone = pSrcBitmap->CloneConvert( |
| - (FXDIB_Format)((pSrcBitmap->GetFormat() & 0xff00) | 0x18)); |
| + pSrcClone = pSrcBitmap->CloneConvert(static_cast<FXDIB_Format>( |
| + (pSrcBitmap->GetFormat() & 0xff00) | 0x18)); |
| } |
| - if (!pSrcClone) { |
| + if (!pSrcClone) |
| return false; |
| - } |
| } |
| srcOffset = g_ChannelOffset[srcChannel]; |
| } |
| + int destOffset = 0; |
| if (destChannel == FXDIB_Alpha) { |
| if (IsAlphaMask()) { |
| - if (!ConvertFormat(FXDIB_8bppMask)) { |
| - if (pSrcClone != pSrcBitmap) { |
| - delete pSrcClone; |
| - } |
| + if (!ConvertFormat(FXDIB_8bppMask)) |
| return false; |
| - } |
| - destOffset = 0; |
| } else { |
| - destOffset = 0; |
| - if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyka : FXDIB_Argb)) { |
| - if (pSrcClone != pSrcBitmap) { |
| - delete pSrcClone; |
| - } |
| + if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyka : FXDIB_Argb)) |
| return false; |
| - } |
| - if (GetFormat() == FXDIB_Argb) { |
| + |
| + if (GetFormat() == FXDIB_Argb) |
| destOffset = 3; |
| - } |
| } |
| } else { |
| - if (IsAlphaMask()) { |
| - if (pSrcClone != pSrcBitmap) { |
| - delete pSrcClone; |
| - } |
| + if (IsAlphaMask()) |
| return false; |
| - } |
| + |
| if (GetBPP() < 24) { |
| if (HasAlpha()) { |
| - if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyka : FXDIB_Argb)) { |
| - if (pSrcClone != pSrcBitmap) { |
| - delete pSrcClone; |
| - } |
| + if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyka : FXDIB_Argb)) |
| return false; |
| - } |
| #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| } else if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyk : FXDIB_Rgb32)) { |
| #else |
| } else if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyk : FXDIB_Rgb)) { |
| #endif |
| - if (pSrcClone != pSrcBitmap) { |
| - delete pSrcClone; |
| - } |
| return false; |
| } |
| } |
| destOffset = g_ChannelOffset[destChannel]; |
| } |
| if (srcChannel == FXDIB_Alpha && pSrcClone->m_pAlphaMask) { |
| - CFX_DIBitmap* pAlphaMask = pSrcClone->m_pAlphaMask; |
| + CFX_MaybeOwned<CFX_DIBSource> pAlphaMask(pSrcClone->m_pAlphaMask); |
| if (pSrcClone->GetWidth() != m_Width || |
| pSrcClone->GetHeight() != m_Height) { |
| if (pAlphaMask) { |
| pAlphaMask = pAlphaMask->StretchTo(m_Width, m_Height); |
| - if (!pAlphaMask) { |
| - if (pSrcClone != pSrcBitmap) { |
| - delete pSrcClone; |
| - } |
| + if (!pAlphaMask) |
| return false; |
| - } |
| } |
| } |
| - if (pSrcClone != pSrcBitmap) { |
| - pSrcClone->m_pAlphaMask = nullptr; |
| - delete pSrcClone; |
| - } |
| - pSrcClone = pAlphaMask; |
| + pSrcClone = std::move(pAlphaMask); |
| srcOffset = 0; |
| } else if (pSrcClone->GetWidth() != m_Width || |
| pSrcClone->GetHeight() != m_Height) { |
| - CFX_DIBitmap* pSrcMatched = pSrcClone->StretchTo(m_Width, m_Height); |
| - if (pSrcClone != pSrcBitmap) { |
| - delete pSrcClone; |
| - } |
| - if (!pSrcMatched) { |
| + std::unique_ptr<CFX_DIBitmap> pSrcMatched = |
| + pSrcClone->StretchTo(m_Width, m_Height); |
| + if (!pSrcMatched) |
| return false; |
| - } |
| - pSrcClone = pSrcMatched; |
| + |
| + pSrcClone = std::move(pSrcMatched); |
| } |
| + CFX_DIBitmap* pDst = this; |
| if (destChannel == FXDIB_Alpha && m_pAlphaMask) { |
| pDst = m_pAlphaMask; |
| destOffset = 0; |
| @@ -799,9 +762,6 @@ bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, |
| src_pos += srcBytes; |
| } |
| } |
| - if (pSrcClone != pSrcBitmap && pSrcClone != pSrcBitmap->m_pAlphaMask) { |
| - delete pSrcClone; |
| - } |
| return true; |
| } |
| @@ -864,40 +824,36 @@ bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, int value) { |
| return true; |
| } |
| -bool CFX_DIBitmap::MultiplyAlpha(const CFX_DIBSource* pSrcBitmap) { |
| - if (!m_pBuffer) { |
| +bool CFX_DIBitmap::MultiplyAlpha(CFX_DIBSource* pSrcBitmap) { |
| + if (!m_pBuffer) |
| return false; |
| - } |
| + |
| ASSERT(pSrcBitmap->IsAlphaMask()); |
| - if (!pSrcBitmap->IsAlphaMask()) { |
| + if (!pSrcBitmap->IsAlphaMask()) |
| return false; |
| - } |
| - if (!IsAlphaMask() && !HasAlpha()) { |
| + |
| + if (!IsAlphaMask() && !HasAlpha()) |
| return LoadChannel(FXDIB_Alpha, pSrcBitmap, FXDIB_Alpha); |
| - } |
| - CFX_DIBitmap* pSrcClone = (CFX_DIBitmap*)pSrcBitmap; |
| + |
| + CFX_MaybeOwned<CFX_DIBitmap> pSrcClone( |
| + static_cast<CFX_DIBitmap*>(const_cast<CFX_DIBSource*>(pSrcBitmap))); |
|
npm
2016/12/05 21:57:05
I think you no longer need const_cast, right?
Tom Sepez
2016/12/05 22:01:25
Done.
|
| if (pSrcBitmap->GetWidth() != m_Width || |
| pSrcBitmap->GetHeight() != m_Height) { |
| pSrcClone = pSrcBitmap->StretchTo(m_Width, m_Height); |
| - if (!pSrcClone) { |
| + if (!pSrcClone) |
| return false; |
| - } |
| } |
| if (IsAlphaMask()) { |
| - if (!ConvertFormat(FXDIB_8bppMask)) { |
| - if (pSrcClone != pSrcBitmap) { |
| - delete pSrcClone; |
| - } |
| + if (!ConvertFormat(FXDIB_8bppMask)) |
| return false; |
| - } |
| + |
| for (int row = 0; row < m_Height; row++) { |
| uint8_t* dest_scan = m_pBuffer + m_Pitch * row; |
| uint8_t* src_scan = pSrcClone->m_pBuffer + pSrcClone->m_Pitch * row; |
| if (pSrcClone->GetBPP() == 1) { |
| for (int col = 0; col < m_Width; col++) { |
| - if (!((1 << (7 - col % 8)) & src_scan[col / 8])) { |
| + if (!((1 << (7 - col % 8)) & src_scan[col / 8])) |
| dest_scan[col] = 0; |
| - } |
| } |
| } else { |
| for (int col = 0; col < m_Width; col++) { |
| @@ -908,12 +864,9 @@ bool CFX_DIBitmap::MultiplyAlpha(const CFX_DIBSource* pSrcBitmap) { |
| } |
| } else { |
| if (GetFormat() == FXDIB_Argb) { |
| - if (pSrcClone->GetBPP() == 1) { |
| - if (pSrcClone != pSrcBitmap) { |
| - delete pSrcClone; |
| - } |
| + if (pSrcClone->GetBPP() == 1) |
| return false; |
| - } |
| + |
| for (int row = 0; row < m_Height; row++) { |
| uint8_t* dest_scan = m_pBuffer + m_Pitch * row + 3; |
| uint8_t* src_scan = pSrcClone->m_pBuffer + pSrcClone->m_Pitch * row; |
| @@ -923,12 +876,9 @@ bool CFX_DIBitmap::MultiplyAlpha(const CFX_DIBSource* pSrcBitmap) { |
| } |
| } |
| } else { |
| - m_pAlphaMask->MultiplyAlpha(pSrcClone); |
| + m_pAlphaMask->MultiplyAlpha(pSrcClone.Get()); |
| } |
| } |
| - if (pSrcClone != pSrcBitmap) { |
| - delete pSrcClone; |
| - } |
| return true; |
| } |
| @@ -1476,7 +1426,7 @@ CFX_DIBExtractor::CFX_DIBExtractor(const CFX_DIBSource* pSrc) { |
| m_pBitmap->CopyPalette(pSrc->GetPalette()); |
| m_pBitmap->CopyAlphaMask(pSrc->m_pAlphaMask); |
| } else { |
| - m_pBitmap.reset(pSrc->Clone()); |
| + m_pBitmap = pSrc->Clone(); |
| } |
| } |