From 826f2223fb0414144d7a627d5d0264da1a5ce82e Mon Sep 17 00:00:00 2001 From: Nicolas Pena Date: Tue, 7 Nov 2017 19:16:35 +0000 Subject: Use MaybeOwned in CJBig2_Image MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Id53cbb8c95f6eb929f6e4eb57333daa24f1b776f Reviewed-on: https://pdfium-review.googlesource.com/18010 Reviewed-by: Tom Sepez Commit-Queue: Nicolás Peña Moreno --- core/fxcodec/jbig2/JBig2_Context.cpp | 2 +- core/fxcodec/jbig2/JBig2_GrdProc.cpp | 42 ++++++------- core/fxcodec/jbig2/JBig2_GrrdProc.cpp | 16 ++--- core/fxcodec/jbig2/JBig2_Image.cpp | 91 +++++++++++++---------------- core/fxcodec/jbig2/JBig2_Image.h | 5 +- core/fxcodec/jbig2/JBig2_Image_unittest.cpp | 4 +- core/fxcodec/jbig2/JBig2_SddProc.cpp | 4 +- 7 files changed, 76 insertions(+), 88 deletions(-) (limited to 'core') diff --git a/core/fxcodec/jbig2/JBig2_Context.cpp b/core/fxcodec/jbig2/JBig2_Context.cpp index 7c79ad8802..53e6b7a84a 100644 --- a/core/fxcodec/jbig2/JBig2_Context.cpp +++ b/core/fxcodec/jbig2/JBig2_Context.cpp @@ -388,7 +388,7 @@ int32_t CJBig2_Context::ProcessingParseSegmentData(CJBig2_Segment* pSegment, pdfium::MakeUnique(pPageInfo->m_dwWidth, height); } - if (!m_pPage->m_pData) { + if (!m_pPage->data()) { m_ProcessingStatus = FXCODEC_STATUS_ERROR; return JBIG2_ERROR_TOO_SHORT; } diff --git a/core/fxcodec/jbig2/JBig2_GrdProc.cpp b/core/fxcodec/jbig2/JBig2_GrdProc.cpp index 7612114117..e308ae35ee 100644 --- a/core/fxcodec/jbig2/JBig2_GrdProc.cpp +++ b/core/fxcodec/jbig2/JBig2_GrdProc.cpp @@ -74,11 +74,11 @@ std::unique_ptr CJBig2_GRDProc::decode_Arith_Template0_opt3( CJBig2_ArithDecoder* pArithDecoder, JBig2ArithCtx* gbContext) { auto GBREG = pdfium::MakeUnique(GBW, GBH); - if (!GBREG->m_pData) + if (!GBREG->data()) return nullptr; int LTP = 0; - uint8_t* pLine = GBREG->m_pData; + uint8_t* pLine = GBREG->data(); int32_t nStride = GBREG->stride(); int32_t nStride2 = nStride << 1; int32_t nLineBytes = ((GBW + 7) >> 3) - 1; @@ -223,11 +223,11 @@ std::unique_ptr CJBig2_GRDProc::decode_Arith_Template1_opt3( CJBig2_ArithDecoder* pArithDecoder, JBig2ArithCtx* gbContext) { auto GBREG = pdfium::MakeUnique(GBW, GBH); - if (!GBREG->m_pData) + if (!GBREG->data()) return nullptr; int LTP = 0; - uint8_t* pLine = GBREG->m_pData; + uint8_t* pLine = GBREG->data(); int32_t nStride = GBREG->stride(); int32_t nStride2 = nStride << 1; int32_t nLineBytes = ((GBW + 7) >> 3) - 1; @@ -369,11 +369,11 @@ std::unique_ptr CJBig2_GRDProc::decode_Arith_Template2_opt3( CJBig2_ArithDecoder* pArithDecoder, JBig2ArithCtx* gbContext) { auto GBREG = pdfium::MakeUnique(GBW, GBH); - if (!GBREG->m_pData) + if (!GBREG->data()) return nullptr; int LTP = 0; - uint8_t* pLine = GBREG->m_pData; + uint8_t* pLine = GBREG->data(); int32_t nStride = GBREG->stride(); int32_t nStride2 = nStride << 1; int32_t nLineBytes = ((GBW + 7) >> 3) - 1; @@ -513,11 +513,11 @@ std::unique_ptr CJBig2_GRDProc::decode_Arith_Template3_opt3( CJBig2_ArithDecoder* pArithDecoder, JBig2ArithCtx* gbContext) { auto GBREG = pdfium::MakeUnique(GBW, GBH); - if (!GBREG->m_pData) + if (!GBREG->data()) return nullptr; int LTP = 0; - uint8_t* pLine = GBREG->m_pData; + uint8_t* pLine = GBREG->data(); int32_t nStride = GBREG->stride(); int32_t nLineBytes = ((GBW + 7) >> 3) - 1; int32_t nBitsLeft = GBW - (nLineBytes << 3); @@ -650,7 +650,7 @@ FXCODEC_STATUS CJBig2_GRDProc::Start_decode_Arith( m_ProssiveStatus = FXCODEC_STATUS_DECODE_READY; if (!*pImage) *pImage = pdfium::MakeUnique(GBW, GBH); - if (!(*pImage)->m_pData) { + if (!(*pImage)->data()) { *pImage = nullptr; m_ProssiveStatus = FXCODEC_STATUS_ERROR; return FXCODEC_STATUS_ERROR; @@ -717,17 +717,17 @@ FXCODEC_STATUS CJBig2_GRDProc::Start_decode_MMR( CJBig2_BitStream* pStream) { int bitpos, i; auto image = pdfium::MakeUnique(GBW, GBH); - if (!image->m_pData) { + if (!image->data()) { *pImage = nullptr; m_ProssiveStatus = FXCODEC_STATUS_ERROR; return m_ProssiveStatus; } bitpos = static_cast(pStream->getBitPos()); - FaxG4Decode(pStream->getBuf(), pStream->getLength(), &bitpos, image->m_pData, + FaxG4Decode(pStream->getBuf(), pStream->getLength(), &bitpos, image->data(), GBW, GBH, image->stride()); pStream->setBitPos(bitpos); for (i = 0; (uint32_t)i < image->stride() * GBH; ++i) - image->m_pData[i] = ~image->m_pData[i]; + image->data()[i] = ~image->data()[i]; m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH; *pImage = std::move(image); return m_ProssiveStatus; @@ -751,9 +751,8 @@ FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template0_opt3( CJBig2_ArithDecoder* pArithDecoder, JBig2ArithCtx* gbContext, IFX_PauseIndicator* pPause) { - if (!m_pLine) { - m_pLine = pImage->m_pData; - } + if (!m_pLine) + m_pLine = pImage->data(); int32_t nStride = pImage->stride(); int32_t nStride2 = nStride << 1; int32_t nLineBytes = ((GBW + 7) >> 3) - 1; @@ -913,9 +912,8 @@ FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template1_opt3( CJBig2_ArithDecoder* pArithDecoder, JBig2ArithCtx* gbContext, IFX_PauseIndicator* pPause) { - if (!m_pLine) { - m_pLine = pImage->m_pData; - } + if (!m_pLine) + m_pLine = pImage->data(); int32_t nStride = pImage->stride(); int32_t nStride2 = nStride << 1; int32_t nLineBytes = ((GBW + 7) >> 3) - 1; @@ -1069,9 +1067,8 @@ FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template2_opt3( CJBig2_ArithDecoder* pArithDecoder, JBig2ArithCtx* gbContext, IFX_PauseIndicator* pPause) { - if (!m_pLine) { - m_pLine = pImage->m_pData; - } + if (!m_pLine) + m_pLine = pImage->data(); int32_t nStride = pImage->stride(); int32_t nStride2 = nStride << 1; int32_t nLineBytes = ((GBW + 7) >> 3) - 1; @@ -1226,8 +1223,7 @@ FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template3_opt3( JBig2ArithCtx* gbContext, IFX_PauseIndicator* pPause) { if (!m_pLine) - m_pLine = pImage->m_pData; - + m_pLine = pImage->data(); int32_t nStride = pImage->stride(); int32_t nLineBytes = ((GBW + 7) >> 3) - 1; int32_t nBitsLeft = GBW - (nLineBytes << 3); diff --git a/core/fxcodec/jbig2/JBig2_GrrdProc.cpp b/core/fxcodec/jbig2/JBig2_GrrdProc.cpp index 71bd8e53e4..df86c88e2c 100644 --- a/core/fxcodec/jbig2/JBig2_GrrdProc.cpp +++ b/core/fxcodec/jbig2/JBig2_GrrdProc.cpp @@ -130,18 +130,18 @@ void CJBig2_GRRDProc::decode_Template0_unopt_SetPixel(CJBig2_Image* GRREG, std::unique_ptr CJBig2_GRRDProc::decode_Template0_opt( CJBig2_ArithDecoder* pArithDecoder, JBig2ArithCtx* grContext) { - if (!GRREFERENCE->m_pData) + if (!GRREFERENCE->data()) return nullptr; int32_t iGRW = static_cast(GRW); int32_t iGRH = static_cast(GRH); auto GRREG = pdfium::MakeUnique(iGRW, iGRH); - if (!GRREG->m_pData) + if (!GRREG->data()) return nullptr; int LTP = 0; - uint8_t* pLine = GRREG->m_pData; - uint8_t* pLineR = GRREFERENCE->m_pData; + uint8_t* pLine = GRREG->data(); + uint8_t* pLineR = GRREFERENCE->data(); intptr_t nStride = GRREG->stride(); intptr_t nStrideR = GRREFERENCE->stride(); int32_t GRWR = GRREFERENCE->width(); @@ -363,18 +363,18 @@ std::unique_ptr CJBig2_GRRDProc::decode_Template1_unopt( std::unique_ptr CJBig2_GRRDProc::decode_Template1_opt( CJBig2_ArithDecoder* pArithDecoder, JBig2ArithCtx* grContext) { - if (!GRREFERENCE->m_pData) + if (!GRREFERENCE->data()) return nullptr; int32_t iGRW = static_cast(GRW); int32_t iGRH = static_cast(GRH); auto GRREG = pdfium::MakeUnique(iGRW, iGRH); - if (!GRREG->m_pData) + if (!GRREG->data()) return nullptr; int LTP = 0; - uint8_t* pLine = GRREG->m_pData; - uint8_t* pLineR = GRREFERENCE->m_pData; + uint8_t* pLine = GRREG->data(); + uint8_t* pLineR = GRREFERENCE->data(); intptr_t nStride = GRREG->stride(); intptr_t nStrideR = GRREFERENCE->stride(); int32_t GRWR = GRREFERENCE->width(); diff --git a/core/fxcodec/jbig2/JBig2_Image.cpp b/core/fxcodec/jbig2/JBig2_Image.cpp index f9868797da..a8d28ace77 100644 --- a/core/fxcodec/jbig2/JBig2_Image.cpp +++ b/core/fxcodec/jbig2/JBig2_Image.cpp @@ -5,6 +5,7 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com #include +#include #include "core/fxcodec/jbig2/JBig2_Image.h" #include "core/fxcrt/fx_coordinates.h" @@ -20,11 +21,7 @@ const int kMaxImageBytes = kMaxImagePixels / 8; } // namespace CJBig2_Image::CJBig2_Image(int32_t w, int32_t h) - : m_pData(nullptr), - m_nWidth(0), - m_nHeight(0), - m_nStride(0), - m_bOwnsBuffer(true) { + : m_pData(nullptr), m_nWidth(0), m_nHeight(0), m_nStride(0) { if (w <= 0 || h <= 0 || w > kMaxImagePixels) return; @@ -35,15 +32,12 @@ CJBig2_Image::CJBig2_Image(int32_t w, int32_t h) m_nWidth = w; m_nHeight = h; m_nStride = stride_pixels / 8; - m_pData = FX_Alloc2D(uint8_t, m_nStride, m_nHeight); + m_pData.Reset(std::unique_ptr( + FX_Alloc2D(uint8_t, m_nStride, m_nHeight))); } CJBig2_Image::CJBig2_Image(int32_t w, int32_t h, int32_t stride, uint8_t* pBuf) - : m_pData(nullptr), - m_nWidth(0), - m_nHeight(0), - m_nStride(0), - m_bOwnsBuffer(false) { + : m_pData(nullptr), m_nWidth(0), m_nHeight(0), m_nStride(0) { if (w < 0 || h < 0 || stride < 0 || stride > kMaxImageBytes) return; @@ -54,26 +48,22 @@ CJBig2_Image::CJBig2_Image(int32_t w, int32_t h, int32_t stride, uint8_t* pBuf) m_nWidth = w; m_nHeight = h; m_nStride = stride; - m_pData = pBuf; + m_pData.Reset(pBuf); } CJBig2_Image::CJBig2_Image(const CJBig2_Image& other) : m_pData(nullptr), m_nWidth(other.m_nWidth), m_nHeight(other.m_nHeight), - m_nStride(other.m_nStride), - m_bOwnsBuffer(true) { + m_nStride(other.m_nStride) { if (other.m_pData) { - m_pData = FX_Alloc2D(uint8_t, m_nStride, m_nHeight); - JBIG2_memcpy(m_pData, other.m_pData, m_nStride * m_nHeight); + m_pData.Reset(std::unique_ptr( + FX_Alloc2D(uint8_t, m_nStride, m_nHeight))); + JBIG2_memcpy(data(), other.data(), m_nStride * m_nHeight); } } -CJBig2_Image::~CJBig2_Image() { - if (m_bOwnsBuffer) { - FX_Free(m_pData); - } -} +CJBig2_Image::~CJBig2_Image() {} int CJBig2_Image::getPixel(int32_t x, int32_t y) const { if (!m_pData) @@ -87,7 +77,7 @@ int CJBig2_Image::getPixel(int32_t x, int32_t y) const { int32_t m = y * m_nStride + (x >> 3); int32_t n = x & 7; - return ((m_pData[m] >> (7 - n)) & 1); + return ((data()[m] >> (7 - n)) & 1); } int32_t CJBig2_Image::setPixel(int32_t x, int32_t y, int v) { @@ -103,37 +93,37 @@ int32_t CJBig2_Image::setPixel(int32_t x, int32_t y, int v) { int32_t m = y * m_nStride + (x >> 3); int32_t n = x & 7; if (v) - m_pData[m] |= 1 << (7 - n); + data()[m] |= 1 << (7 - n); else - m_pData[m] &= ~(1 << (7 - n)); + data()[m] &= ~(1 << (7 - n)); return 1; } void CJBig2_Image::copyLine(int32_t hTo, int32_t hFrom) { - if (!m_pData) { + if (!m_pData) return; - } + if (hFrom < 0 || hFrom >= m_nHeight) { - JBIG2_memset(m_pData + hTo * m_nStride, 0, m_nStride); + JBIG2_memset(data() + hTo * m_nStride, 0, m_nStride); } else { - JBIG2_memcpy(m_pData + hTo * m_nStride, m_pData + hFrom * m_nStride, + JBIG2_memcpy(data() + hTo * m_nStride, data() + hFrom * m_nStride, m_nStride); } } void CJBig2_Image::fill(bool v) { - if (!m_pData) { + if (!m_pData) return; - } - JBIG2_memset(m_pData, v ? 0xff : 0, m_nStride * m_nHeight); + + JBIG2_memset(data(), v ? 0xff : 0, m_nStride * m_nHeight); } bool CJBig2_Image::composeTo(CJBig2_Image* pDst, int32_t x, int32_t y, JBig2ComposeOp op) { - if (!m_pData) { + if (!m_pData) return false; - } + return composeTo_opt2(pDst, x, y, op); } bool CJBig2_Image::composeTo(CJBig2_Image* pDst, @@ -154,9 +144,9 @@ bool CJBig2_Image::composeFrom(int32_t x, int32_t y, CJBig2_Image* pSrc, JBig2ComposeOp op) { - if (!m_pData) { + if (!m_pData) return false; - } + return pSrc->composeTo(this, x, y, op); } bool CJBig2_Image::composeFrom(int32_t x, @@ -195,8 +185,8 @@ std::unique_ptr CJBig2_Image::subImage(int32_t x, if (!pImage->m_pData) return pImage; - pLineSrc = m_pData + m_nStride * y; - pLineDst = pImage->m_pData; + pLineSrc = data() + m_nStride * y; + pLineDst = pImage->data(); m = (x >> 5) << 2; n = x & 31; if (n == 0) { @@ -240,15 +230,16 @@ void CJBig2_Image::expand(int32_t h, bool v) { if (!m_pData || h <= m_nHeight || h > kMaxImageBytes / m_nStride) return; - if (m_bOwnsBuffer) { - m_pData = FX_Realloc(uint8_t, m_pData, h * m_nStride); + if (m_pData.IsOwned()) { + m_pData.Reset(std::unique_ptr( + FX_Realloc(uint8_t, m_pData.Release().release(), h * m_nStride))); } else { - uint8_t* pExternalBuffer = m_pData; - m_pData = FX_Alloc(uint8_t, h * m_nStride); - JBIG2_memcpy(m_pData, pExternalBuffer, m_nHeight * m_nStride); - m_bOwnsBuffer = true; + uint8_t* pExternalBuffer = data(); + m_pData.Reset(std::unique_ptr( + FX_Alloc(uint8_t, h * m_nStride))); + JBIG2_memcpy(data(), pExternalBuffer, m_nHeight * m_nStride); } - JBIG2_memset(m_pData + m_nHeight * m_nStride, v ? 0xff : 0, + JBIG2_memset(data() + m_nHeight * m_nStride, v ? 0xff : 0, (h - m_nHeight) * m_nStride); m_nHeight = h; } @@ -304,9 +295,9 @@ bool CJBig2_Image::composeTo_opt2(CJBig2_Image* pDst, maskL = 0xffffffff >> d1; maskR = 0xffffffff << ((32 - (xd1 & 31)) % 32); maskM = maskL & maskR; - uint8_t* lineSrc = m_pData + ys0 * m_nStride + ((xs0 >> 5) << 2); + uint8_t* lineSrc = data() + ys0 * m_nStride + ((xs0 >> 5) << 2); lineLeft = m_nStride - ((xs0 >> 5) << 2); - uint8_t* lineDst = pDst->m_pData + yd0 * pDst->m_nStride + ((xd0 >> 5) << 2); + uint8_t* lineDst = pDst->data() + yd0 * pDst->m_nStride + ((xd0 >> 5) << 2); if ((xd0 & ~31) == ((xd1 - 1) & ~31)) { if ((xs0 & ~31) == ((xs1 - 1) & ~31)) { if (s1 > d1) { @@ -686,9 +677,9 @@ bool CJBig2_Image::composeTo_opt2(CJBig2_Image* pDst, int32_t y, JBig2ComposeOp op, const FX_RECT* pSrcRect) { - if (!m_pData) { + if (!m_pData) return false; - } + // TODO(weili): Check whether the range check is correct. Should x>=1048576? if (x < -1048576 || x > 1048576 || y < -1048576 || y > 1048576) { return false; @@ -714,10 +705,10 @@ bool CJBig2_Image::composeTo_opt2(CJBig2_Image* pDst, int32_t maskL = 0xffffffff >> d1; int32_t maskR = 0xffffffff << ((32 - (xd1 & 31)) % 32); int32_t maskM = maskL & maskR; - uint8_t* lineSrc = m_pData + (pSrcRect->top + ys0) * m_nStride + + uint8_t* lineSrc = data() + (pSrcRect->top + ys0) * m_nStride + (((xs0 + pSrcRect->left) >> 5) << 2); int32_t lineLeft = m_nStride - ((xs0 >> 5) << 2); - uint8_t* lineDst = pDst->m_pData + yd0 * pDst->m_nStride + ((xd0 >> 5) << 2); + uint8_t* lineDst = pDst->data() + yd0 * pDst->m_nStride + ((xd0 >> 5) << 2); if ((xd0 & ~31) == ((xd1 - 1) & ~31)) { if ((xs0 & ~31) == ((xs1 - 1) & ~31)) { if (s1 > d1) { diff --git a/core/fxcodec/jbig2/JBig2_Image.h b/core/fxcodec/jbig2/JBig2_Image.h index 1f418fb7e2..c0980407db 100644 --- a/core/fxcodec/jbig2/JBig2_Image.h +++ b/core/fxcodec/jbig2/JBig2_Image.h @@ -10,6 +10,7 @@ #include #include "core/fxcodec/jbig2/JBig2_Define.h" +#include "core/fxcrt/maybe_owned.h" struct FX_RECT; @@ -31,6 +32,7 @@ class CJBig2_Image { int32_t width() const { return m_nWidth; } int32_t height() const { return m_nHeight; } int32_t stride() const { return m_nStride; } + uint8_t* data() const { return m_pData.Get(); } int getPixel(int32_t x, int32_t y) const; int32_t setPixel(int32_t x, int32_t y, int bVal); @@ -68,13 +70,12 @@ class CJBig2_Image { int32_t h); void expand(int32_t h, bool v); - uint8_t* m_pData; private: + MaybeOwned m_pData; int32_t m_nWidth; // 1-bit pixels int32_t m_nHeight; // lines int32_t m_nStride; // bytes - bool m_bOwnsBuffer; }; #endif // CORE_FXCODEC_JBIG2_JBIG2_IMAGE_H_ diff --git a/core/fxcodec/jbig2/JBig2_Image_unittest.cpp b/core/fxcodec/jbig2/JBig2_Image_unittest.cpp index 788f922a02..2e53e7b337 100644 --- a/core/fxcodec/jbig2/JBig2_Image_unittest.cpp +++ b/core/fxcodec/jbig2/JBig2_Image_unittest.cpp @@ -34,7 +34,7 @@ TEST(fxcodec, JBig2ImageCreateTooBig) { CJBig2_Image img(kWidthPixels, kTooLargeHeightLines); EXPECT_EQ(0, img.width()); EXPECT_EQ(0, img.height()); - EXPECT_EQ(nullptr, img.m_pData); + EXPECT_EQ(nullptr, img.data()); } TEST(fxcodec, JBig2ImageCreateExternal) { @@ -53,7 +53,7 @@ TEST(fxcodec, JBig2ImageCreateExternalTooBig) { CJBig2_Image img(kWidthPixels, kTooLargeHeightLines, kStrideBytes, buf); EXPECT_EQ(0, img.width()); EXPECT_EQ(0, img.height()); - EXPECT_EQ(nullptr, img.m_pData); + EXPECT_EQ(nullptr, img.data()); } TEST(fxcodec, JBig2ImageExpand) { diff --git a/core/fxcodec/jbig2/JBig2_SddProc.cpp b/core/fxcodec/jbig2/JBig2_SddProc.cpp index b587ac01dd..45aa22e655 100644 --- a/core/fxcodec/jbig2/JBig2_SddProc.cpp +++ b/core/fxcodec/jbig2/JBig2_SddProc.cpp @@ -469,8 +469,8 @@ std::unique_ptr CJBig2_SDDProc::decode_Huffman( if (pStream->getByteLeft() >= stride * HCHEIGHT) { BHC = pdfium::MakeUnique(TOTWIDTH, HCHEIGHT); for (I = 0; I < HCHEIGHT; I++) { - JBIG2_memcpy(BHC->m_pData + I * BHC->stride(), - pStream->getPointer(), stride); + JBIG2_memcpy(BHC->data() + I * BHC->stride(), pStream->getPointer(), + stride); pStream->offset(stride); } } else { -- cgit v1.2.3