From 3f41851972a18762164d7110a82da3baed564b80 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 6 Feb 2015 16:44:10 -0800 Subject: Cleanup parts of fpdf_render_loadimage.cpp. - Add functions to do pitch calculation. - Delete dead code. - Don't bother checking pointers before deleting them. - Don't bother setting pointers to NULL in dtors. R=tsepez@chromium.org Review URL: https://codereview.chromium.org/890883006 --- .../fpdfapi/fpdf_render/fpdf_render_loadimage.cpp | 195 ++++++++------------- core/src/fpdfapi/fpdf_render/render_int.h | 13 +- 2 files changed, 78 insertions(+), 130 deletions(-) diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp index f938628f55..45d0078e8a 100644 --- a/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp +++ b/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp @@ -13,7 +13,9 @@ #include "../fpdf_page/pageint.h" #include "render_int.h" -static unsigned int _GetBits8(FX_LPCBYTE pData, int bitpos, int nbits) +namespace { + +unsigned int _GetBits8(FX_LPCBYTE pData, int bitpos, int nbits) { unsigned int byte = pData[bitpos / 8]; if (nbits == 8) { @@ -29,6 +31,32 @@ static unsigned int _GetBits8(FX_LPCBYTE pData, int bitpos, int nbits) } return 0; } + +FX_SAFE_DWORD CalculatePitch8(FX_DWORD bpc, + FX_DWORD components, + int width, + int height) +{ + FX_SAFE_DWORD pitch = bpc; + pitch *= components; + pitch *= width; + pitch += 7; + pitch /= 8; + pitch *= height; + return pitch; +} + +FX_SAFE_DWORD CalculatePitch32(int bpp, int width) +{ + FX_SAFE_DWORD pitch = bpp; + pitch *= width; + pitch += 31; + pitch /= 8; + return pitch; +} + +} // namespace + CFX_DIBSource* CPDF_Image::LoadDIBSource(CFX_DIBSource** ppMask, FX_DWORD* pMatteColor, FX_BOOL bStdCS, FX_DWORD GroupFamily, FX_BOOL bLoadMask) const { CPDF_DIBSource* pSource = FX_NEW CPDF_DIBSource; @@ -112,21 +140,15 @@ CPDF_DIBSource::CPDF_DIBSource() } CPDF_DIBSource::~CPDF_DIBSource() { - if (m_pStreamAcc) { - delete m_pStreamAcc; - } + delete m_pStreamAcc; if (m_pMaskedLine) { FX_Free(m_pMaskedLine); } if (m_pLineBuf) { FX_Free(m_pLineBuf); } - if (m_pCachedBitmap) { - delete m_pCachedBitmap; - } - if (m_pDecoder) { - delete m_pDecoder; - } + delete m_pCachedBitmap; + delete m_pDecoder; if (m_pCompData) { FX_Free(m_pCompData); } @@ -137,12 +159,8 @@ CPDF_DIBSource::~CPDF_DIBSource() if (m_pJbig2Context) { ICodec_Jbig2Module* pJbig2Moudle = CPDF_ModuleMgr::Get()->GetJbig2Module(); pJbig2Moudle->DestroyJbig2Context(m_pJbig2Context); - m_pJbig2Context = NULL; - } - if (m_pGlobalStream) { - delete m_pGlobalStream; } - m_pGlobalStream = NULL; + delete m_pGlobalStream; } CFX_DIBitmap* CPDF_DIBSource::GetBitmap() const { @@ -182,12 +200,8 @@ FX_BOOL CPDF_DIBSource::Load(CPDF_Document* pDoc, const CPDF_Stream* pStream, CP if (m_bpc == 0 || m_nComponents == 0) { return FALSE; } - FX_SAFE_DWORD src_pitch = m_bpc; - src_pitch *= m_nComponents; - src_pitch *= m_Width; - src_pitch += 7; - src_pitch /= 8; - src_pitch *= m_Height; + FX_SAFE_DWORD src_pitch = + CalculatePitch8(m_bpc, m_nComponents, m_Width, m_Height); if (!src_pitch.IsValid()) { return FALSE; } @@ -215,10 +229,7 @@ FX_BOOL CPDF_DIBSource::Load(CPDF_Document* pDoc, const CPDF_Stream* pStream, CP } else { m_bpp = 24; } - FX_SAFE_DWORD pitch = m_Width; - pitch *= m_bpp; - pitch += 31; - pitch /= 8; + FX_SAFE_DWORD pitch = CalculatePitch32(m_bpp, m_Width); if (!pitch.IsValid()) { return FALSE; } @@ -230,10 +241,7 @@ FX_BOOL CPDF_DIBSource::Load(CPDF_Document* pDoc, const CPDF_Stream* pStream, CP if (m_bColorKey) { m_bpp = 32; m_AlphaFlag = 2; - pitch = m_Width; - pitch *= m_bpp; - pitch += 31; - pitch /= 8; + FX_SAFE_DWORD pitch = CalculatePitch32(m_bpp, m_Width); if (!pitch.IsValid()) { return FALSE; } @@ -265,10 +273,7 @@ int CPDF_DIBSource::ContinueToLoadMask() if (!m_bpc || !m_nComponents) { return 0; } - FX_SAFE_DWORD pitch = m_Width; - pitch *= m_bpp; - pitch += 31; - pitch /= 8; + FX_SAFE_DWORD pitch = CalculatePitch32(m_bpp, m_Width); if (!pitch.IsValid()) { return 0; } @@ -280,10 +285,7 @@ int CPDF_DIBSource::ContinueToLoadMask() if (m_bColorKey) { m_bpp = 32; m_AlphaFlag = 2; - pitch = m_Width; - pitch *= m_bpp; - pitch += 31; - pitch /= 8; + FX_SAFE_DWORD pitch = CalculatePitch32(m_bpp, m_Width); if (!pitch.IsValid()) { return 0; } @@ -317,12 +319,8 @@ int CPDF_DIBSource::StartLoadDIBSource(CPDF_Document* pDoc, const CPDF_Stream* p if (m_bpc == 0 || m_nComponents == 0) { return 0; } - FX_SAFE_DWORD src_pitch = m_bpc; - src_pitch *= m_nComponents; - src_pitch *= m_Width; - src_pitch += 7; - src_pitch /= 8; - src_pitch *= m_Height; + FX_SAFE_DWORD src_pitch = + CalculatePitch8(m_bpc, m_nComponents, m_Width, m_Height); if (!src_pitch.IsValid()) { return 0; } @@ -383,9 +381,7 @@ int CPDF_DIBSource::ContinueLoadDIBSource(IFX_Pause* pPause) if (ret < 0) { delete m_pCachedBitmap; m_pCachedBitmap = NULL; - if (m_pGlobalStream) { - delete m_pGlobalStream; - } + delete m_pGlobalStream; m_pGlobalStream = NULL; pJbig2Moudle->DestroyJbig2Context(m_pJbig2Context); m_pJbig2Context = NULL; @@ -411,9 +407,7 @@ int CPDF_DIBSource::ContinueLoadDIBSource(IFX_Pause* pPause) if (ret < 0) { delete m_pCachedBitmap; m_pCachedBitmap = NULL; - if (m_pGlobalStream) { - delete m_pGlobalStream; - } + delete m_pGlobalStream; m_pGlobalStream = NULL; pJbig2Moudle->DestroyJbig2Context(m_pJbig2Context); m_pJbig2Context = NULL; @@ -578,7 +572,7 @@ int CPDF_DIBSource::CreateDecoder() } else if (decoder == FX_BSTRC("DCTDecode")) { m_pDecoder = CPDF_ModuleMgr::Get()->GetJpegModule()->CreateDecoder(src_data, src_size, m_Width, m_Height, m_nComponents, pParams ? pParams->GetInteger(FX_BSTR("ColorTransform"), 1) : 1); - if (NULL == m_pDecoder) { + if (!m_pDecoder) { FX_BOOL bTransform = FALSE; int comps, bpc; ICodec_JpegModule* pJpegModule = CPDF_ModuleMgr::Get()->GetJpegModule(); @@ -617,29 +611,25 @@ int CPDF_DIBSource::CreateDecoder() } else if (decoder == FX_BSTRC("RunLengthDecode")) { m_pDecoder = CPDF_ModuleMgr::Get()->GetCodecModule()->GetBasicModule()->CreateRunLengthDecoder(src_data, src_size, m_Width, m_Height, m_nComponents, m_bpc); } - if (m_pDecoder) { - FX_SAFE_DWORD requested_pitch = m_bpc; - requested_pitch *= m_nComponents; - requested_pitch *= m_Width; - requested_pitch += 7; - requested_pitch /= 8; - if (!requested_pitch.IsValid()) { - return 0; - } - FX_SAFE_DWORD provided_pitch = m_pDecoder->GetBPC(); - provided_pitch *= m_pDecoder->CountComps(); - provided_pitch *= m_pDecoder->GetWidth(); - provided_pitch += 7; - provided_pitch /= 8; - if (!provided_pitch.IsValid()) { - return 0; - } - if (provided_pitch.ValueOrDie() < requested_pitch.ValueOrDie()) { - return 0; - } - return 1; + if (!m_pDecoder) + return 0; + + FX_SAFE_DWORD requested_pitch = + CalculatePitch8(m_bpc, m_nComponents, m_Width, 1); + if (!requested_pitch.IsValid()) { + return 0; } - return 0; + FX_SAFE_DWORD provided_pitch = CalculatePitch8(m_pDecoder->GetBPC(), + m_pDecoder->CountComps(), + m_pDecoder->GetWidth(), + 1); + if (!provided_pitch.IsValid()) { + return 0; + } + if (provided_pitch.ValueOrDie() < requested_pitch.ValueOrDie()) { + return 0; + } + return 1; } void CPDF_DIBSource::LoadJpxBitmap() { @@ -729,37 +719,6 @@ void CPDF_DIBSource::LoadJpxBitmap() } m_bpc = 8; } -void CPDF_DIBSource::LoadJbig2Bitmap() -{ - ICodec_Jbig2Module* pJbig2Module = CPDF_ModuleMgr::Get()->GetJbig2Module(); - if (pJbig2Module == NULL) { - return; - } - CPDF_StreamAcc* pGlobalStream = NULL; - if (m_pStreamAcc->GetImageParam()) { - CPDF_Stream* pGlobals = m_pStreamAcc->GetImageParam()->GetStream(FX_BSTRC("JBIG2Globals")); - if (pGlobals) { - pGlobalStream = FX_NEW CPDF_StreamAcc; - pGlobalStream->LoadAllData(pGlobals, FALSE); - } - } - m_pCachedBitmap = FX_NEW CFX_DIBitmap; - if (!m_pCachedBitmap->Create(m_Width, m_Height, m_bImageMask ? FXDIB_1bppMask : FXDIB_1bppRgb)) { - return; - } - int ret = pJbig2Module->Decode(m_Width, m_Height, m_pStreamAcc->GetData(), m_pStreamAcc->GetSize(), - pGlobalStream ? pGlobalStream->GetData() : NULL, pGlobalStream ? pGlobalStream->GetSize() : 0, - m_pCachedBitmap->GetBuffer(), m_pCachedBitmap->GetPitch()); - if (ret < 0) { - delete m_pCachedBitmap; - m_pCachedBitmap = NULL; - } - if (pGlobalStream) { - delete pGlobalStream; - } - m_bpc = 1; - m_nComponents = 1; -} CPDF_DIBSource* CPDF_DIBSource::LoadMask(FX_DWORD& MatteColor) { MatteColor = 0xffffffff; @@ -1083,11 +1042,7 @@ FX_LPCBYTE CPDF_DIBSource::GetScanline(int line) const if (m_bpc == 0) { return NULL; } - FX_SAFE_DWORD src_pitch = m_Width; - src_pitch *= m_bpc; - src_pitch *= m_nComponents; - src_pitch += 7; - src_pitch /= 8; + FX_SAFE_DWORD src_pitch = CalculatePitch8(m_bpc, m_nComponents, m_Width, 1); if (!src_pitch.IsValid()) return NULL; FX_DWORD src_pitch_value = src_pitch.ValueOrDie(); @@ -1223,12 +1178,8 @@ void CPDF_DIBSource::DownSampleScanline(int line, FX_LPBYTE dest_scan, int dest_ } FX_DWORD src_width = m_Width; - FX_SAFE_DWORD pitch = src_width; - pitch *= m_bpc; - pitch *= m_nComponents; - pitch += 7; - pitch /= 8; - if (!pitch.IsValid()) { + FX_SAFE_DWORD pitch = CalculatePitch8(m_bpc, m_nComponents, m_Width, 1); + if (!pitch.IsValid()) { return; } @@ -1240,10 +1191,10 @@ void CPDF_DIBSource::DownSampleScanline(int line, FX_LPBYTE dest_scan, int dest_ } else { FX_DWORD src_pitch = pitch.ValueOrDie(); pitch *= (line+1); - if (!pitch.IsValid()) { + if (!pitch.IsValid()) { return; } - + if (m_pStreamAcc->GetSize() >= pitch.ValueOrDie()) { pSrcLine = m_pStreamAcc->GetData() + line * src_pitch; } @@ -1481,9 +1432,6 @@ CPDF_ProgressiveImageLoaderHandle::CPDF_ProgressiveImageLoaderHandle() } CPDF_ProgressiveImageLoaderHandle::~CPDF_ProgressiveImageLoaderHandle() { - m_pImageLoader = NULL; - m_pCache = NULL; - m_pImage = NULL; } FX_BOOL CPDF_ProgressiveImageLoaderHandle::Start(CPDF_ImageLoader* pImageLoader, const CPDF_ImageObject* pImage, CPDF_PageRenderCache* pCache, FX_BOOL bStdCS, FX_DWORD GroupFamily, FX_BOOL bLoadMask, CPDF_RenderStatus* pRenderStatus, FX_INT32 nDownsampleWidth, FX_INT32 nDownsampleHeight) { @@ -1565,12 +1513,7 @@ FX_BOOL CPDF_ImageLoader::Continue(FX_LPVOID LoadHandle, IFX_Pause* pPause) CPDF_ImageLoader::~CPDF_ImageLoader() { if (!m_bCached) { - if (m_pBitmap) { - delete m_pBitmap; - m_pBitmap = NULL; - } - if (m_pMask) { - delete m_pMask; - } + delete m_pBitmap; + delete m_pMask; } } diff --git a/core/src/fpdfapi/fpdf_render/render_int.h b/core/src/fpdfapi/fpdf_render/render_int.h index bd7d375837..70c41ca93c 100644 --- a/core/src/fpdfapi/fpdf_render/render_int.h +++ b/core/src/fpdfapi/fpdf_render/render_int.h @@ -419,9 +419,8 @@ protected: CPDF_DIBSource* LoadMask(FX_DWORD& MatteColor); CPDF_DIBSource* LoadMaskDIB(CPDF_Stream* pMask); void LoadJpxBitmap(); - void LoadJbig2Bitmap(); void LoadPalette(); - FX_BOOL CreateDecoder(); + int CreateDecoder(); void TranslateScanline24bpp(FX_LPBYTE dest_scan, FX_LPCBYTE src_scan) const; void ValidateDictParam(); CPDF_Document* m_pDocument; @@ -429,9 +428,15 @@ protected: CPDF_StreamAcc* m_pStreamAcc; const CPDF_Dictionary* m_pDict; CPDF_ColorSpace* m_pColorSpace; - FX_DWORD m_Family, m_bpc, m_bpc_orig, m_nComponents, m_GroupFamily; + FX_DWORD m_Family; + FX_DWORD m_bpc; + FX_DWORD m_bpc_orig; + FX_DWORD m_nComponents; + FX_DWORD m_GroupFamily; FX_BOOL m_bLoadMask; - FX_BOOL m_bDefaultDecode, m_bImageMask, m_bColorKey; + FX_BOOL m_bDefaultDecode; + FX_BOOL m_bImageMask; + FX_BOOL m_bColorKey; DIB_COMP_DATA* m_pCompData; FX_LPBYTE m_pLineBuf; FX_LPBYTE m_pMaskedLine; -- cgit v1.2.3