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

Unified Diff: core/fxge/dib/fx_dib_main.cpp

Issue 2534953004: Return unique_ptrs from CFX_DIBitmap::Clone(). (Closed)
Patch Set: win again Created 4 years 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 side-by-side diff with in-line comments
Download patch
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..b3576635a0bfb7cd03f2fcd30c537919daf63baf 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;
@@ -211,13 +211,13 @@ CFX_DIBitmap* CFX_DIBSource::Clone(const FX_RECT* pClip) const {
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);
- }
}
}
}
@@ -665,31 +663,26 @@ const int g_ChannelOffset[] = {0, 2, 1, 0, 0, 1, 2, 3, 3};
bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel,
const 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(
+ const_cast<CFX_DIBSource*>(pSrcBitmap));
dsinclair 2016/12/05 20:41:26 Should we remove the const from the signature?
Tom Sepez 2016/12/05 21:38:13 Ok. Not too many ripples ...
+ 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(
@@ -698,92 +691,63 @@ bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel,
pSrcClone = pSrcBitmap->CloneConvert(
(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 +763,6 @@ bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel,
src_pos += srcBytes;
}
}
- if (pSrcClone != pSrcBitmap && pSrcClone != pSrcBitmap->m_pAlphaMask) {
- delete pSrcClone;
- }
return true;
}
@@ -865,31 +826,28 @@ bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, int value) {
}
bool CFX_DIBitmap::MultiplyAlpha(const CFX_DIBSource* pSrcBitmap) {
- if (!m_pBuffer) {
+ 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 20:37:23 Why 2 casts?
Tom Sepez 2016/12/05 21:38:13 Because a const_cast can't change type, and a stat
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;
@@ -908,12 +866,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 +878,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 +1428,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();
}
}

Powered by Google App Engine
This is Rietveld 408576698