From 72c1bda4efaf0121e13b794cb886acd9806b6c89 Mon Sep 17 00:00:00 2001 From: tsepez Date: Wed, 14 Dec 2016 14:15:14 -0800 Subject: Return unique_ptr from GetAlphaMask. Rename GetAlphaMask => CloneAlphaMask since it does more than get. Rename CopyAlphaMask => SetAlphaMask, as it copies IN, not OUT. ditto for CopyPalette => SetPalette. BUG= Review-Url: https://codereview.chromium.org/2572243002 --- core/fpdfapi/page/cpdf_image.cpp | 7 ++- core/fpdfapi/render/cpdf_imagerenderer.cpp | 42 +++++++------ core/fxge/dib/fx_dib_convert.cpp | 25 ++++---- core/fxge/dib/fx_dib_main.cpp | 99 +++++++++++++++--------------- core/fxge/dib/fx_dib_transform.cpp | 2 +- core/fxge/fx_dib.h | 12 ++-- 6 files changed, 98 insertions(+), 89 deletions(-) diff --git a/core/fpdfapi/page/cpdf_image.cpp b/core/fpdfapi/page/cpdf_image.cpp index feafde3b36..97969551c6 100644 --- a/core/fpdfapi/page/cpdf_image.cpp +++ b/core/fpdfapi/page/cpdf_image.cpp @@ -247,8 +247,10 @@ void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap) { bCopyWithoutAlpha = false; } - const CFX_DIBitmap* pMaskBitmap = - pBitmap->HasAlpha() ? pBitmap->GetAlphaMask() : nullptr; + std::unique_ptr pMaskBitmap; + if (pBitmap->HasAlpha()) + pMaskBitmap = pBitmap->CloneAlphaMask(); + if (pMaskBitmap) { int32_t maskWidth = pMaskBitmap->GetWidth(); int32_t maskHeight = pMaskBitmap->GetHeight(); @@ -275,7 +277,6 @@ void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap) { mask_buf, mask_size, std::move(pMaskDict)); pDict->SetNewFor("SMask", m_pDocument, pNewStream->GetObjNum()); - delete pMaskBitmap; } uint8_t* src_buf = pBitmap->GetBuffer(); diff --git a/core/fpdfapi/render/cpdf_imagerenderer.cpp b/core/fpdfapi/render/cpdf_imagerenderer.cpp index 5ca7ea842f..a5b1238eaf 100644 --- a/core/fpdfapi/render/cpdf_imagerenderer.cpp +++ b/core/fpdfapi/render/cpdf_imagerenderer.cpp @@ -25,6 +25,7 @@ #include "core/fpdfapi/render/cpdf_transferfunc.h" #include "core/fpdfapi/render/render_int.h" #include "core/fpdfdoc/cpdf_occontext.h" +#include "core/fxcrt/cfx_maybe_owned.h" #include "core/fxcrt/fx_safe_types.h" #include "core/fxge/cfx_fxgedevice.h" #include "core/fxge/cfx_pathdata.h" @@ -499,34 +500,37 @@ bool CPDF_ImageRenderer::StartBitmapAlpha() { FXFILL_WINDING); return false; } - const CFX_DIBSource* pAlphaMask = - m_pDIBSource->IsAlphaMask() ? m_pDIBSource : m_pDIBSource->GetAlphaMask(); + CFX_MaybeOwned pAlphaMask; + if (m_pDIBSource->IsAlphaMask()) + pAlphaMask = const_cast(m_pDIBSource); + else + pAlphaMask = m_pDIBSource->CloneAlphaMask(); + if (FXSYS_fabs(m_ImageMatrix.b) >= 0.5f || FXSYS_fabs(m_ImageMatrix.c) >= 0.5f) { - int left, top; - std::unique_ptr pTransformed( - pAlphaMask->TransformTo(&m_ImageMatrix, left, top)); + int left; + int top; + std::unique_ptr pTransformed = + pAlphaMask->TransformTo(&m_ImageMatrix, left, top); if (!pTransformed) return true; m_pRenderStatus->m_pDevice->SetBitMask( pTransformed.get(), left, top, ArgbEncode(0xff, m_BitmapAlpha, m_BitmapAlpha, m_BitmapAlpha)); - } else { - CFX_FloatRect image_rect_f = m_ImageMatrix.GetUnitRect(); - FX_RECT image_rect = image_rect_f.GetOuterRect(); - int dest_width = - m_ImageMatrix.a > 0 ? image_rect.Width() : -image_rect.Width(); - int dest_height = - m_ImageMatrix.d > 0 ? -image_rect.Height() : image_rect.Height(); - int left = dest_width > 0 ? image_rect.left : image_rect.right; - int top = dest_height > 0 ? image_rect.top : image_rect.bottom; - m_pRenderStatus->m_pDevice->StretchBitMask( - pAlphaMask, left, top, dest_width, dest_height, - ArgbEncode(0xff, m_BitmapAlpha, m_BitmapAlpha, m_BitmapAlpha)); + return false; } - if (m_pDIBSource != pAlphaMask) - delete pAlphaMask; + CFX_FloatRect image_rect_f = m_ImageMatrix.GetUnitRect(); + FX_RECT image_rect = image_rect_f.GetOuterRect(); + int dest_width = + m_ImageMatrix.a > 0 ? image_rect.Width() : -image_rect.Width(); + int dest_height = + m_ImageMatrix.d > 0 ? -image_rect.Height() : image_rect.Height(); + int left = dest_width > 0 ? image_rect.left : image_rect.right; + int top = dest_height > 0 ? image_rect.top : image_rect.bottom; + m_pRenderStatus->m_pDevice->StretchBitMask( + pAlphaMask.Get(), left, top, dest_width, dest_height, + ArgbEncode(0xff, m_BitmapAlpha, m_BitmapAlpha, m_BitmapAlpha)); return false; } diff --git a/core/fxge/dib/fx_dib_convert.cpp b/core/fxge/dib/fx_dib_convert.cpp index b1362462c3..aad3f343e2 100644 --- a/core/fxge/dib/fx_dib_convert.cpp +++ b/core/fxge/dib/fx_dib_convert.cpp @@ -8,6 +8,7 @@ #include #include "core/fxcodec/fx_codec.h" +#include "core/fxcrt/cfx_maybe_owned.h" #include "core/fxge/fx_dib.h" #include "third_party/base/ptr_util.h" @@ -791,26 +792,26 @@ std::unique_ptr CFX_DIBSource::CloneConvert( if (!pClone->Create(m_Width, m_Height, dest_format)) return nullptr; - CFX_DIBitmap* pSrcAlpha = nullptr; + CFX_MaybeOwned pSrcAlpha; if (HasAlpha()) { - pSrcAlpha = (GetFormat() == FXDIB_Argb) ? GetAlphaMask() : m_pAlphaMask; + if (GetFormat() == FXDIB_Argb) + pSrcAlpha = CloneAlphaMask(); + else + pSrcAlpha = m_pAlphaMask; + if (!pSrcAlpha) return nullptr; } - bool ret = true; if (dest_format & 0x0200) { if (dest_format == FXDIB_Argb) { - ret = pSrcAlpha ? pClone->LoadChannel(FXDIB_Alpha, pSrcAlpha, FXDIB_Alpha) - : pClone->LoadChannel(FXDIB_Alpha, 0xff); + ret = pSrcAlpha + ? pClone->LoadChannel(FXDIB_Alpha, pSrcAlpha.Get(), FXDIB_Alpha) + : pClone->LoadChannel(FXDIB_Alpha, 0xff); } else { - ret = pClone->CopyAlphaMask(pSrcAlpha); + ret = pClone->SetAlphaMask(pSrcAlpha.Get()); } } - if (pSrcAlpha && pSrcAlpha != m_pAlphaMask) { - delete pSrcAlpha; - pSrcAlpha = nullptr; - } if (!ret) return nullptr; @@ -820,7 +821,7 @@ std::unique_ptr CFX_DIBSource::CloneConvert( return nullptr; } if (pal_8bpp) - pClone->CopyPalette(pal_8bpp.get()); + pClone->SetPalette(pal_8bpp.get()); return pClone; } @@ -867,7 +868,7 @@ bool CFX_DIBitmap::ConvertFormat(FXDIB_Format dest_format) { } } else if (dest_format & 0x0200) { if (src_format == FXDIB_Argb) { - pAlphaMask = GetAlphaMask(); + pAlphaMask = CloneAlphaMask().release(); if (!pAlphaMask) { FX_Free(dest_buf); return false; diff --git a/core/fxge/dib/fx_dib_main.cpp b/core/fxge/dib/fx_dib_main.cpp index 310c62fcc5..f90cbc1a91 100644 --- a/core/fxge/dib/fx_dib_main.cpp +++ b/core/fxge/dib/fx_dib_main.cpp @@ -128,17 +128,17 @@ bool CFX_DIBitmap::Create(int width, } bool CFX_DIBitmap::Copy(const CFX_DIBSource* pSrc) { - if (m_pBuffer) { + if (m_pBuffer) return false; - } - if (!Create(pSrc->GetWidth(), pSrc->GetHeight(), pSrc->GetFormat())) { + + if (!Create(pSrc->GetWidth(), pSrc->GetHeight(), pSrc->GetFormat())) return false; - } - CopyPalette(pSrc->GetPalette()); - CopyAlphaMask(pSrc->m_pAlphaMask); - for (int row = 0; row < pSrc->GetHeight(); row++) { + + SetPalette(pSrc->GetPalette()); + SetAlphaMask(pSrc->m_pAlphaMask); + for (int row = 0; row < pSrc->GetHeight(); row++) FXSYS_memcpy(m_pBuffer + row * m_Pitch, pSrc->GetScanline(row), m_Pitch); - } + return true; } @@ -186,8 +186,8 @@ std::unique_ptr CFX_DIBSource::Clone(const FX_RECT* pClip) const { if (!pNewBitmap->Create(rect.Width(), rect.Height(), GetFormat())) return nullptr; - pNewBitmap->CopyPalette(m_pPalette.get()); - pNewBitmap->CopyAlphaMask(m_pAlphaMask, pClip); + pNewBitmap->SetPalette(m_pPalette.get()); + pNewBitmap->SetAlphaMask(m_pAlphaMask, pClip); if (GetBPP() == 1 && rect.left % 8 != 0) { int left_shift = rect.left % 32; int right_shift = 32 - left_shift; @@ -572,18 +572,17 @@ bool CFX_DIBitmap::TransferMask(int dest_left, return true; } -void CFX_DIBSource::CopyPalette(const uint32_t* pSrc) { +void CFX_DIBSource::SetPalette(const uint32_t* pSrc) { static const uint32_t kPaletteSize = 256; - if (!pSrc || GetBPP() > 8) { m_pPalette.reset(); - } else { - uint32_t pal_size = 1 << GetBPP(); - if (!m_pPalette) - m_pPalette.reset(FX_Alloc(uint32_t, pal_size)); - pal_size = std::min(pal_size, kPaletteSize); - FXSYS_memcpy(m_pPalette.get(), pSrc, pal_size * sizeof(uint32_t)); + return; } + uint32_t pal_size = 1 << GetBPP(); + if (!m_pPalette) + m_pPalette.reset(FX_Alloc(uint32_t, pal_size)); + pal_size = std::min(pal_size, kPaletteSize); + FXSYS_memcpy(m_pPalette.get(), pSrc, pal_size * sizeof(uint32_t)); } void CFX_DIBSource::GetPalette(uint32_t* pal, int alpha) const { @@ -606,23 +605,23 @@ void CFX_DIBSource::GetPalette(uint32_t* pal, int alpha) const { } } -CFX_DIBitmap* CFX_DIBSource::GetAlphaMask(const FX_RECT* pClip) const { +std::unique_ptr CFX_DIBSource::CloneAlphaMask( + const FX_RECT* pClip) const { ASSERT(GetFormat() == FXDIB_Argb); FX_RECT rect(0, 0, m_Width, m_Height); if (pClip) { rect.Intersect(*pClip); - if (rect.IsEmpty()) { + if (rect.IsEmpty()) return nullptr; - } } - CFX_DIBitmap* pMask = new CFX_DIBitmap; - if (!pMask->Create(rect.Width(), rect.Height(), FXDIB_8bppMask)) { - delete pMask; + auto pMask = pdfium::MakeUnique(); + if (!pMask->Create(rect.Width(), rect.Height(), FXDIB_8bppMask)) return nullptr; - } + for (int row = rect.top; row < rect.bottom; row++) { const uint8_t* src_scan = GetScanline(row) + rect.left * 4 + 3; - uint8_t* dest_scan = (uint8_t*)pMask->GetScanline(row - rect.top); + uint8_t* dest_scan = + const_cast(pMask->GetScanline(row - rect.top)); for (int col = rect.left; col < rect.right; col++) { *dest_scan++ = *src_scan; src_scan += 4; @@ -631,30 +630,30 @@ CFX_DIBitmap* CFX_DIBSource::GetAlphaMask(const FX_RECT* pClip) const { return pMask; } -bool CFX_DIBSource::CopyAlphaMask(const CFX_DIBSource* pAlphaMask, - const FX_RECT* pClip) { - if (!HasAlpha() || GetFormat() == FXDIB_Argb) { +bool CFX_DIBSource::SetAlphaMask(const CFX_DIBSource* pAlphaMask, + const FX_RECT* pClip) { + if (!HasAlpha() || GetFormat() == FXDIB_Argb) return false; + + if (!pAlphaMask) { + m_pAlphaMask->Clear(0xff000000); + return true; } - if (pAlphaMask) { - FX_RECT rect(0, 0, pAlphaMask->m_Width, pAlphaMask->m_Height); - if (pClip) { - rect.Intersect(*pClip); - if (rect.IsEmpty() || rect.Width() != m_Width || - rect.Height() != m_Height) { - return false; - } - } else { - if (pAlphaMask->m_Width != m_Width || pAlphaMask->m_Height != m_Height) { - return false; - } + FX_RECT rect(0, 0, pAlphaMask->m_Width, pAlphaMask->m_Height); + if (pClip) { + rect.Intersect(*pClip); + if (rect.IsEmpty() || rect.Width() != m_Width || + rect.Height() != m_Height) { + return false; } - for (int row = 0; row < m_Height; row++) - FXSYS_memcpy((void*)m_pAlphaMask->GetScanline(row), - pAlphaMask->GetScanline(row + rect.top) + rect.left, - m_pAlphaMask->m_Pitch); } else { - m_pAlphaMask->Clear(0xff000000); + if (pAlphaMask->m_Width != m_Width || pAlphaMask->m_Height != m_Height) + return false; + } + for (int row = 0; row < m_Height; row++) { + FXSYS_memcpy(const_cast(m_pAlphaMask->GetScanline(row)), + pAlphaMask->GetScanline(row + rect.top) + rect.left, + m_pAlphaMask->m_Pitch); } return true; } @@ -1349,7 +1348,7 @@ CFX_DIBitmap* CFX_DIBSource::FlipImage(bool bXFlip, bool bYFlip) const { delete pFlipped; return nullptr; } - pFlipped->CopyPalette(m_pPalette.get()); + pFlipped->SetPalette(m_pPalette.get()); uint8_t* pDestBuffer = pFlipped->GetBuffer(); int Bpp = m_bpp / 8; for (int row = 0; row < m_Height; row++) { @@ -1423,8 +1422,8 @@ CFX_DIBExtractor::CFX_DIBExtractor(const CFX_DIBSource* pSrc) { m_pBitmap.reset(); return; } - m_pBitmap->CopyPalette(pSrc->GetPalette()); - m_pBitmap->CopyAlphaMask(pSrc->m_pAlphaMask); + m_pBitmap->SetPalette(pSrc->GetPalette()); + m_pBitmap->SetAlphaMask(pSrc->m_pAlphaMask); } else { m_pBitmap = pSrc->Clone(); } @@ -1642,6 +1641,6 @@ bool CFX_BitmapStorer::SetInfo(int width, return false; } if (pSrcPalette) - m_pBitmap->CopyPalette(pSrcPalette); + m_pBitmap->SetPalette(pSrcPalette); return true; } diff --git a/core/fxge/dib/fx_dib_transform.cpp b/core/fxge/dib/fx_dib_transform.cpp index dacc0aa393..e56e7fccbb 100644 --- a/core/fxge/dib/fx_dib_transform.cpp +++ b/core/fxge/dib/fx_dib_transform.cpp @@ -194,7 +194,7 @@ CFX_DIBitmap* CFX_DIBSource::SwapXY(bool bXFlip, delete pTransBitmap; return nullptr; } - pTransBitmap->CopyPalette(m_pPalette.get()); + pTransBitmap->SetPalette(m_pPalette.get()); int dest_pitch = pTransBitmap->GetPitch(); uint8_t* dest_buf = pTransBitmap->GetBuffer(); int row_start = bXFlip ? m_Height - dest_clip.right : dest_clip.left; diff --git a/core/fxge/fx_dib.h b/core/fxge/fx_dib.h index ed2b47f9c1..c083bf732f 100644 --- a/core/fxge/fx_dib.h +++ b/core/fxge/fx_dib.h @@ -213,7 +213,8 @@ class CFX_DIBSource { SetPaletteEntry(index, color); } - void CopyPalette(const uint32_t* pSrcPal); + // Copies into internally-owned palette. + void SetPalette(const uint32_t* pSrcPal); std::unique_ptr Clone(const FX_RECT* pClip = nullptr) const; std::unique_ptr CloneConvert(FXDIB_Format format) const; @@ -229,9 +230,12 @@ class CFX_DIBSource { uint32_t flags = 0, const FX_RECT* pClip = nullptr) const; - CFX_DIBitmap* GetAlphaMask(const FX_RECT* pClip = nullptr) const; - bool CopyAlphaMask(const CFX_DIBSource* pAlphaMask, - const FX_RECT* pClip = nullptr); + std::unique_ptr CloneAlphaMask( + const FX_RECT* pClip = nullptr) const; + + // Copies into internally-owned mask. + bool SetAlphaMask(const CFX_DIBSource* pAlphaMask, + const FX_RECT* pClip = nullptr); CFX_DIBitmap* SwapXY(bool bXFlip, bool bYFlip, -- cgit v1.2.3