From e6ff2ebfaa19421c8d932f4d275548156ae2e13c Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Fri, 24 Aug 2018 21:55:46 +0000 Subject: Rename CFX_DIBSource to CFX_DIBBase. It is not a source from which you can get CFX_DIBs, but rather a base class from which all DIBs inherit. Do the same thing for the CPDF_DIBSource wrapper class. Mechanical change apart from adding a one-line comment in cfx_dibbase.h Change-Id: Id2bde87813ca301d9fafc55ce08d703dfc6a7184 Reviewed-on: https://pdfium-review.googlesource.com/41352 Reviewed-by: Lei Zhang Commit-Queue: Tom Sepez --- core/fpdfapi/font/cpdf_type3char.cpp | 10 +- core/fpdfapi/page/cpdf_generalstate.cpp | 2 +- core/fpdfapi/page/cpdf_image.cpp | 52 +- core/fpdfapi/page/cpdf_image.h | 22 +- core/fpdfapi/parser/cpdf_document.cpp | 2 +- core/fpdfapi/render/cpdf_dibbase.cpp | 1349 +++++++++++++++++++++++++ core/fpdfapi/render/cpdf_dibbase.h | 163 ++++ core/fpdfapi/render/cpdf_dibsource.cpp | 1350 -------------------------- core/fpdfapi/render/cpdf_dibsource.h | 163 ---- core/fpdfapi/render/cpdf_docrenderdata.cpp | 2 +- core/fpdfapi/render/cpdf_imagecacheentry.cpp | 38 +- core/fpdfapi/render/cpdf_imagecacheentry.h | 16 +- core/fpdfapi/render/cpdf_imageloader.cpp | 4 +- core/fpdfapi/render/cpdf_imageloader.h | 4 +- core/fpdfapi/render/cpdf_imagerenderer.cpp | 86 +- core/fpdfapi/render/cpdf_imagerenderer.h | 14 +- core/fpdfapi/render/cpdf_pagerendercache.cpp | 6 +- core/fpdfapi/render/cpdf_renderstatus.cpp | 4 +- core/fpdfapi/render/cpdf_transferfunc.cpp | 6 +- core/fpdfapi/render/cpdf_transferfunc.h | 4 +- core/fxcodec/codec/ccodec_jpegmodule.h | 4 +- core/fxcodec/codec/fx_codec_jpeg.cpp | 4 +- core/fxcodec/fx_codec.h | 2 +- core/fxge/agg/fx_agg_driver.cpp | 8 +- core/fxge/agg/fx_agg_driver.h | 6 +- core/fxge/cfx_defaultrenderdevice.h | 4 +- core/fxge/cfx_renderdevice.cpp | 23 +- core/fxge/cfx_renderdevice.h | 22 +- core/fxge/dib/cfx_dibbase.cpp | 1259 ++++++++++++++++++++++++ core/fxge/dib/cfx_dibbase.h | 140 +++ core/fxge/dib/cfx_dibextractor.cpp | 6 +- core/fxge/dib/cfx_dibextractor.h | 4 +- core/fxge/dib/cfx_dibitmap.cpp | 24 +- core/fxge/dib/cfx_dibitmap.h | 24 +- core/fxge/dib/cfx_dibsource.cpp | 1262 ------------------------ core/fxge/dib/cfx_dibsource.h | 139 --- core/fxge/dib/cfx_filtereddib.cpp | 2 +- core/fxge/dib/cfx_filtereddib.h | 10 +- core/fxge/dib/cfx_imagerenderer.cpp | 2 +- core/fxge/dib/cfx_imagerenderer.h | 4 +- core/fxge/dib/cfx_imagestretcher.cpp | 6 +- core/fxge/dib/cfx_imagestretcher.h | 8 +- core/fxge/dib/cfx_imagetransformer.cpp | 4 +- core/fxge/dib/cfx_imagetransformer.h | 6 +- core/fxge/dib/cfx_scanlinecompositor.h | 2 +- core/fxge/dib/cstretchengine.cpp | 4 +- core/fxge/dib/cstretchengine.h | 4 +- core/fxge/dib/cstretchengine_unittest.cpp | 4 +- core/fxge/fx_dib.h | 2 +- core/fxge/renderdevicedriver_iface.cpp | 4 +- core/fxge/renderdevicedriver_iface.h | 12 +- core/fxge/skia/fx_skia_device.cpp | 41 +- core/fxge/skia/fx_skia_device.h | 14 +- core/fxge/win32/cfx_psrenderer.cpp | 8 +- core/fxge/win32/cfx_psrenderer.h | 8 +- core/fxge/win32/fx_win32_device.cpp | 8 +- core/fxge/win32/fx_win32_print.cpp | 18 +- core/fxge/win32/win32_int.h | 26 +- 58 files changed, 3210 insertions(+), 3215 deletions(-) create mode 100644 core/fpdfapi/render/cpdf_dibbase.cpp create mode 100644 core/fpdfapi/render/cpdf_dibbase.h delete mode 100644 core/fpdfapi/render/cpdf_dibsource.cpp delete mode 100644 core/fpdfapi/render/cpdf_dibsource.h create mode 100644 core/fxge/dib/cfx_dibbase.cpp create mode 100644 core/fxge/dib/cfx_dibbase.h delete mode 100644 core/fxge/dib/cfx_dibsource.cpp delete mode 100644 core/fxge/dib/cfx_dibsource.h (limited to 'core') diff --git a/core/fpdfapi/font/cpdf_type3char.cpp b/core/fpdfapi/font/cpdf_type3char.cpp index 8a89582b00..a04e83c468 100644 --- a/core/fpdfapi/font/cpdf_type3char.cpp +++ b/core/fpdfapi/font/cpdf_type3char.cpp @@ -49,15 +49,15 @@ bool CPDF_Type3Char::LoadBitmap(CPDF_RenderContext* pContext) { m_ImageMatrix = pPageObj->AsImage()->matrix(); { - // |pSource| actually gets assigned a CPDF_DIBSource, which has pointers + // |pSource| actually gets assigned a CPDF_DIBBase, which has pointers // into objects owned by |m_pForm|. Make sure it is out of scope before // clearing the form. - RetainPtr pSource = - pPageObj->AsImage()->GetImage()->LoadDIBSource(); + RetainPtr pSource = + pPageObj->AsImage()->GetImage()->LoadDIBBase(); - // Clone() is non-virtual, and can't be overloaded by CPDF_DIBSource to + // Clone() is non-virtual, and can't be overloaded by CPDF_DIBBase to // return a clone of the subclass as one would typically expect from a - // such a method. Instead, it only clones the CFX_DIBSource, none of whose + // such a method. Instead, it only clones the CFX_DIBBase, none of whose // members point to objects owned by the form. As a result, |m_pBitmap| // may outlive |m_pForm|. if (pSource) diff --git a/core/fpdfapi/page/cpdf_generalstate.cpp b/core/fpdfapi/page/cpdf_generalstate.cpp index 2657578ab7..ea912736fe 100644 --- a/core/fpdfapi/page/cpdf_generalstate.cpp +++ b/core/fpdfapi/page/cpdf_generalstate.cpp @@ -8,7 +8,7 @@ #include "constants/transparency.h" #include "core/fpdfapi/parser/cpdf_document.h" -#include "core/fpdfapi/render/cpdf_dibsource.h" +#include "core/fpdfapi/render/cpdf_dibbase.h" #include "core/fpdfapi/render/cpdf_docrenderdata.h" #include "core/fpdfapi/render/cpdf_transferfunc.h" diff --git a/core/fpdfapi/page/cpdf_image.cpp b/core/fpdfapi/page/cpdf_image.cpp index a1be407dfe..b1c2fc9e82 100644 --- a/core/fpdfapi/page/cpdf_image.cpp +++ b/core/fpdfapi/page/cpdf_image.cpp @@ -23,7 +23,7 @@ #include "core/fpdfapi/parser/cpdf_reference.h" #include "core/fpdfapi/parser/cpdf_stream.h" #include "core/fpdfapi/parser/cpdf_string.h" -#include "core/fpdfapi/render/cpdf_dibsource.h" +#include "core/fpdfapi/render/cpdf_dibbase.h" #include "core/fpdfapi/render/cpdf_pagerendercache.h" #include "core/fxcodec/codec/ccodec_jpegmodule.h" #include "core/fxcrt/fx_stream.h" @@ -329,43 +329,43 @@ void CPDF_Image::ResetCache(CPDF_Page* pPage, pPage->GetRenderCache()->ResetBitmap(pHolder, pBitmap); } -RetainPtr CPDF_Image::LoadDIBSource() const { - auto source = pdfium::MakeRetain(); +RetainPtr CPDF_Image::LoadDIBBase() const { + auto source = pdfium::MakeRetain(); if (!source->Load(m_pDocument.Get(), m_pStream.Get())) return nullptr; if (!source->IsJBigImage()) return source; - CPDF_DIBSource::LoadState ret = CPDF_DIBSource::LoadState::kContinue; - while (ret == CPDF_DIBSource::LoadState::kContinue) - ret = source->ContinueLoadDIBSource(nullptr); - return ret == CPDF_DIBSource::LoadState::kSuccess ? source : nullptr; + CPDF_DIBBase::LoadState ret = CPDF_DIBBase::LoadState::kContinue; + while (ret == CPDF_DIBBase::LoadState::kContinue) + ret = source->ContinueLoadDIBBase(nullptr); + return ret == CPDF_DIBBase::LoadState::kSuccess ? source : nullptr; } -RetainPtr CPDF_Image::DetachBitmap() { - return std::move(m_pDIBSource); +RetainPtr CPDF_Image::DetachBitmap() { + return std::move(m_pDIBBase); } -RetainPtr CPDF_Image::DetachMask() { +RetainPtr CPDF_Image::DetachMask() { return std::move(m_pMask); } -bool CPDF_Image::StartLoadDIBSource(const CPDF_Dictionary* pFormResource, - CPDF_Dictionary* pPageResource, - bool bStdCS, - uint32_t GroupFamily, - bool bLoadMask) { - auto source = pdfium::MakeRetain(); - CPDF_DIBSource::LoadState ret = source->StartLoadDIBSource( +bool CPDF_Image::StartLoadDIBBase(const CPDF_Dictionary* pFormResource, + CPDF_Dictionary* pPageResource, + bool bStdCS, + uint32_t GroupFamily, + bool bLoadMask) { + auto source = pdfium::MakeRetain(); + CPDF_DIBBase::LoadState ret = source->StartLoadDIBBase( m_pDocument.Get(), m_pStream.Get(), true, pFormResource, pPageResource, bStdCS, GroupFamily, bLoadMask); - if (ret == CPDF_DIBSource::LoadState::kFail) { - m_pDIBSource.Reset(); + if (ret == CPDF_DIBBase::LoadState::kFail) { + m_pDIBBase.Reset(); return false; } - m_pDIBSource = source; - if (ret == CPDF_DIBSource::LoadState::kContinue) + m_pDIBBase = source; + if (ret == CPDF_DIBBase::LoadState::kContinue) return true; m_pMask = source->DetachMask(); @@ -374,16 +374,16 @@ bool CPDF_Image::StartLoadDIBSource(const CPDF_Dictionary* pFormResource, } bool CPDF_Image::Continue(PauseIndicatorIface* pPause) { - RetainPtr pSource = m_pDIBSource.As(); - CPDF_DIBSource::LoadState ret = pSource->ContinueLoadDIBSource(pPause); - if (ret == CPDF_DIBSource::LoadState::kContinue) + RetainPtr pSource = m_pDIBBase.As(); + CPDF_DIBBase::LoadState ret = pSource->ContinueLoadDIBBase(pPause); + if (ret == CPDF_DIBBase::LoadState::kContinue) return true; - if (ret == CPDF_DIBSource::LoadState::kSuccess) { + if (ret == CPDF_DIBBase::LoadState::kSuccess) { m_pMask = pSource->DetachMask(); m_MatteColor = pSource->GetMatteColor(); } else { - m_pDIBSource.Reset(); + m_pDIBBase.Reset(); } return false; } diff --git a/core/fpdfapi/page/cpdf_image.h b/core/fpdfapi/page/cpdf_image.h index e6c6d18986..4306109199 100644 --- a/core/fpdfapi/page/cpdf_image.h +++ b/core/fpdfapi/page/cpdf_image.h @@ -15,7 +15,7 @@ #include "core/fxcrt/retain_ptr.h" #include "core/fxcrt/unowned_ptr.h" -class CFX_DIBSource; +class CFX_DIBBase; class CFX_DIBitmap; class CPDF_Document; class CPDF_Page; @@ -41,7 +41,7 @@ class CPDF_Image : public Retainable { bool IsMask() const { return m_bIsMask; } bool IsInterpol() const { return m_bInterpolate; } - RetainPtr LoadDIBSource() const; + RetainPtr LoadDIBBase() const; void SetImage(const RetainPtr& pDIBitmap); void SetJpegImage(const RetainPtr& pFile); @@ -50,20 +50,20 @@ class CPDF_Image : public Retainable { void ResetCache(CPDF_Page* pPage, const RetainPtr& pDIBitmap); // Returns whether to Continue() or not. - bool StartLoadDIBSource(const CPDF_Dictionary* pFormResource, - CPDF_Dictionary* pPageResource, - bool bStdCS, - uint32_t GroupFamily, - bool bLoadMask); + bool StartLoadDIBBase(const CPDF_Dictionary* pFormResource, + CPDF_Dictionary* pPageResource, + bool bStdCS, + uint32_t GroupFamily, + bool bLoadMask); // Returns whether to Continue() or not. bool Continue(PauseIndicatorIface* pPause); - RetainPtr DetachBitmap(); - RetainPtr DetachMask(); + RetainPtr DetachBitmap(); + RetainPtr DetachMask(); - RetainPtr m_pDIBSource; - RetainPtr m_pMask; + RetainPtr m_pDIBBase; + RetainPtr m_pMask; uint32_t m_MatteColor = 0; private: diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp index 63556de250..7119e7649a 100644 --- a/core/fpdfapi/parser/cpdf_document.cpp +++ b/core/fpdfapi/parser/cpdf_document.cpp @@ -26,7 +26,7 @@ #include "core/fpdfapi/parser/cpdf_reference.h" #include "core/fpdfapi/parser/cpdf_stream.h" #include "core/fpdfapi/parser/cpdf_string.h" -#include "core/fpdfapi/render/cpdf_dibsource.h" +#include "core/fpdfapi/render/cpdf_dibbase.h" #include "core/fpdfapi/render/cpdf_docrenderdata.h" #include "core/fxcodec/JBig2_DocumentContext.h" #include "core/fxcrt/fx_codepage.h" diff --git a/core/fpdfapi/render/cpdf_dibbase.cpp b/core/fpdfapi/render/cpdf_dibbase.cpp new file mode 100644 index 0000000000..9aa1ab40a6 --- /dev/null +++ b/core/fpdfapi/render/cpdf_dibbase.cpp @@ -0,0 +1,1349 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fpdfapi/render/cpdf_dibbase.h" + +#include +#include +#include +#include + +#include "core/fpdfapi/cpdf_modulemgr.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" +#include "core/fpdfapi/page/cpdf_image.h" +#include "core/fpdfapi/page/cpdf_imageobject.h" +#include "core/fpdfapi/parser/cpdf_array.h" +#include "core/fpdfapi/parser/cpdf_dictionary.h" +#include "core/fpdfapi/parser/cpdf_document.h" +#include "core/fpdfapi/parser/fpdf_parser_decode.h" +#include "core/fpdfapi/render/cpdf_pagerendercache.h" +#include "core/fpdfapi/render/cpdf_renderstatus.h" +#include "core/fxcodec/codec/ccodec_basicmodule.h" +#include "core/fxcodec/codec/ccodec_jbig2module.h" +#include "core/fxcodec/codec/ccodec_jpegmodule.h" +#include "core/fxcodec/codec/ccodec_jpxmodule.h" +#include "core/fxcodec/codec/ccodec_scanlinedecoder.h" +#include "core/fxcodec/codec/cjpx_decoder.h" +#include "core/fxcodec/fx_codec.h" +#include "core/fxcrt/cfx_fixedbufgrow.h" +#include "core/fxcrt/fx_safe_types.h" +#include "core/fxge/dib/cfx_dibitmap.h" +#include "third_party/base/ptr_util.h" + +namespace { + +constexpr int kMaxImageDimension = 0x01FFFF; + +unsigned int GetBits8(const uint8_t* pData, uint64_t bitpos, size_t nbits) { + ASSERT(nbits == 1 || nbits == 2 || nbits == 4 || nbits == 8 || nbits == 16); + ASSERT((bitpos & (nbits - 1)) == 0); + unsigned int byte = pData[bitpos / 8]; + if (nbits == 8) + return byte; + + if (nbits == 16) + return byte * 256 + pData[bitpos / 8 + 1]; + + return (byte >> (8 - nbits - (bitpos % 8))) & ((1 << nbits) - 1); +} + +bool GetBitValue(const uint8_t* pSrc, uint32_t pos) { + return pSrc[pos / 8] & (1 << (7 - pos % 8)); +} + +bool IsAllowedBPCValue(int bpc) { + return bpc == 1 || bpc == 2 || bpc == 4 || bpc == 8 || bpc == 16; +} + +bool IsAllowedICCComponents(int nComp) { + return nComp == 1 || nComp == 3 || nComp == 4; +} + +bool IsColorIndexOutOfBounds(uint8_t index, const DIB_COMP_DATA& comp_datum) { + return index < comp_datum.m_ColorKeyMin || index > comp_datum.m_ColorKeyMax; +} + +bool AreColorIndicesOutOfBounds(const uint8_t* indices, + const DIB_COMP_DATA* comp_data, + size_t count) { + for (size_t i = 0; i < count; ++i) { + if (IsColorIndexOutOfBounds(indices[i], comp_data[i])) + return true; + } + return false; +} + +// Wrapper class to use with std::unique_ptr for CJPX_Decoder. +class JpxBitMapContext { + public: + explicit JpxBitMapContext(CCodec_JpxModule* jpx_module) + : jpx_module_(jpx_module), decoder_(nullptr) {} + + ~JpxBitMapContext() {} + + void set_decoder(std::unique_ptr decoder) { + decoder_ = std::move(decoder); + } + + CJPX_Decoder* decoder() { return decoder_.get(); } + + private: + CCodec_JpxModule* const jpx_module_; // Weak pointer. + std::unique_ptr decoder_; + + // Disallow evil constructors + JpxBitMapContext(const JpxBitMapContext&); + void operator=(const JpxBitMapContext&); +}; + +} // namespace + +CPDF_DIBBase::CPDF_DIBBase() {} + +CPDF_DIBBase::~CPDF_DIBBase() { + if (m_pColorSpace && m_pDocument) { + auto* pPageData = m_pDocument->GetPageData(); + if (pPageData) { + auto* pSpace = m_pColorSpace.Release(); + pPageData->ReleaseColorSpace(pSpace->GetArray()); + } + } +} + +bool CPDF_DIBBase::Load(CPDF_Document* pDoc, const CPDF_Stream* pStream) { + if (!pStream) + return false; + + m_pDocument = pDoc; + m_pDict = pStream->GetDict(); + if (!m_pDict) + return false; + + m_pStream = pStream; + m_Width = m_pDict->GetIntegerFor("Width"); + m_Height = m_pDict->GetIntegerFor("Height"); + if (m_Width <= 0 || m_Height <= 0 || m_Width > kMaxImageDimension || + m_Height > kMaxImageDimension) { + return false; + } + m_GroupFamily = 0; + m_bLoadMask = false; + if (!LoadColorInfo(nullptr, nullptr)) + return false; + + if (m_bDoBpcCheck && (m_bpc == 0 || m_nComponents == 0)) + return false; + + FX_SAFE_UINT32 src_size = + CalculatePitch8(m_bpc, m_nComponents, m_Width) * m_Height; + if (!src_size.IsValid()) + return false; + + m_pStreamAcc = pdfium::MakeRetain(pStream); + m_pStreamAcc->LoadAllData(false, src_size.ValueOrDie(), true); + if (m_pStreamAcc->GetSize() == 0 || !m_pStreamAcc->GetData()) + return false; + + if (CreateDecoder() == LoadState::kFail) + return false; + + if (m_bImageMask) { + m_bpp = 1; + m_bpc = 1; + m_nComponents = 1; + m_AlphaFlag = 1; + } else if (m_bpc * m_nComponents == 1) { + m_bpp = 1; + } else if (m_bpc * m_nComponents <= 8) { + m_bpp = 8; + } else { + m_bpp = 24; + } + FX_SAFE_UINT32 pitch = CalculatePitch32(m_bpp, m_Width); + if (!pitch.IsValid()) + return false; + + m_pLineBuf.reset(FX_Alloc(uint8_t, pitch.ValueOrDie())); + LoadPalette(); + if (m_bColorKey) { + m_bpp = 32; + m_AlphaFlag = 2; + pitch = CalculatePitch32(m_bpp, m_Width); + if (!pitch.IsValid()) + return false; + + m_pMaskedLine.reset(FX_Alloc(uint8_t, pitch.ValueOrDie())); + } + m_Pitch = pitch.ValueOrDie(); + return true; +} + +bool CPDF_DIBBase::ContinueToLoadMask() { + if (m_bImageMask) { + m_bpp = 1; + m_bpc = 1; + m_nComponents = 1; + m_AlphaFlag = 1; + } else if (m_bpc * m_nComponents == 1) { + m_bpp = 1; + } else if (m_bpc * m_nComponents <= 8) { + m_bpp = 8; + } else { + m_bpp = 24; + } + if (!m_bpc || !m_nComponents) { + return false; + } + FX_SAFE_UINT32 pitch = CalculatePitch32(m_bpp, m_Width); + if (!pitch.IsValid()) + return false; + + m_pLineBuf.reset(FX_Alloc(uint8_t, pitch.ValueOrDie())); + if (m_pColorSpace && m_bStdCS) { + m_pColorSpace->EnableStdConversion(true); + } + LoadPalette(); + if (m_bColorKey) { + m_bpp = 32; + m_AlphaFlag = 2; + pitch = CalculatePitch32(m_bpp, m_Width); + if (!pitch.IsValid()) + return false; + m_pMaskedLine.reset(FX_Alloc(uint8_t, pitch.ValueOrDie())); + } + m_Pitch = pitch.ValueOrDie(); + return true; +} + +CPDF_DIBBase::LoadState CPDF_DIBBase::StartLoadDIBBase( + CPDF_Document* pDoc, + const CPDF_Stream* pStream, + bool bHasMask, + const CPDF_Dictionary* pFormResources, + CPDF_Dictionary* pPageResources, + bool bStdCS, + uint32_t GroupFamily, + bool bLoadMask) { + if (!pStream) + return LoadState::kFail; + + m_pDocument = pDoc; + m_pDict = pStream->GetDict(); + m_pStream = pStream; + m_bStdCS = bStdCS; + m_bHasMask = bHasMask; + m_Width = m_pDict->GetIntegerFor("Width"); + m_Height = m_pDict->GetIntegerFor("Height"); + if (m_Width <= 0 || m_Height <= 0 || m_Width > kMaxImageDimension || + m_Height > kMaxImageDimension) { + return LoadState::kFail; + } + m_GroupFamily = GroupFamily; + m_bLoadMask = bLoadMask; + if (!LoadColorInfo(m_pStream->IsInline() ? pFormResources : nullptr, + pPageResources)) { + return LoadState::kFail; + } + if (m_bDoBpcCheck && (m_bpc == 0 || m_nComponents == 0)) + return LoadState::kFail; + + FX_SAFE_UINT32 src_size = + CalculatePitch8(m_bpc, m_nComponents, m_Width) * m_Height; + if (!src_size.IsValid()) + return LoadState::kFail; + + m_pStreamAcc = pdfium::MakeRetain(pStream); + m_pStreamAcc->LoadAllData(false, src_size.ValueOrDie(), true); + if (m_pStreamAcc->GetSize() == 0 || !m_pStreamAcc->GetData()) + return LoadState::kFail; + + LoadState iCreatedDecoder = CreateDecoder(); + if (iCreatedDecoder == LoadState::kFail) + return LoadState::kFail; + + if (!ContinueToLoadMask()) + return LoadState::kFail; + + LoadState iLoadedMask = m_bHasMask ? StartLoadMask() : LoadState::kSuccess; + if (iCreatedDecoder == LoadState::kContinue || + iLoadedMask == LoadState::kContinue) { + return LoadState::kContinue; + } + + ASSERT(iCreatedDecoder == LoadState::kSuccess); + ASSERT(iLoadedMask == LoadState::kSuccess); + if (m_pColorSpace && m_bStdCS) + m_pColorSpace->EnableStdConversion(false); + return LoadState::kSuccess; +} + +CPDF_DIBBase::LoadState CPDF_DIBBase::ContinueLoadDIBBase( + PauseIndicatorIface* pPause) { + if (m_Status == LoadState::kContinue) + return ContinueLoadMaskDIB(pPause); + + if (m_Status == LoadState::kFail) + return LoadState::kFail; + + if (m_pStreamAcc->GetImageDecoder() == "JPXDecode") + return LoadState::kFail; + + FXCODEC_STATUS iDecodeStatus; + CCodec_Jbig2Module* pJbig2Module = CPDF_ModuleMgr::Get()->GetJbig2Module(); + if (!m_pJbig2Context) { + m_pJbig2Context = pdfium::MakeUnique(); + if (m_pStreamAcc->GetImageParam()) { + const CPDF_Stream* pGlobals = + m_pStreamAcc->GetImageParam()->GetStreamFor("JBIG2Globals"); + if (pGlobals) { + m_pGlobalStream = pdfium::MakeRetain(pGlobals); + m_pGlobalStream->LoadAllDataFiltered(); + } + } + iDecodeStatus = pJbig2Module->StartDecode( + m_pJbig2Context.get(), m_pDocument->CodecContext(), m_Width, m_Height, + m_pStreamAcc, m_pGlobalStream, m_pCachedBitmap->GetBuffer(), + m_pCachedBitmap->GetPitch(), pPause); + } else { + iDecodeStatus = pJbig2Module->ContinueDecode(m_pJbig2Context.get(), pPause); + } + + if (iDecodeStatus < 0) { + m_pJbig2Context.reset(); + m_pCachedBitmap.Reset(); + m_pGlobalStream.Reset(); + return LoadState::kFail; + } + if (iDecodeStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE) + return LoadState::kContinue; + + LoadState iContinueStatus = LoadState::kSuccess; + if (m_bHasMask) { + if (ContinueLoadMaskDIB(pPause) == LoadState::kContinue) { + iContinueStatus = LoadState::kContinue; + m_Status = LoadState::kContinue; + } + } + if (iContinueStatus == LoadState::kContinue) + return LoadState::kContinue; + + if (m_pColorSpace && m_bStdCS) + m_pColorSpace->EnableStdConversion(false); + return iContinueStatus; +} + +bool CPDF_DIBBase::LoadColorInfo(const CPDF_Dictionary* pFormResources, + const CPDF_Dictionary* pPageResources) { + m_bpc_orig = m_pDict->GetIntegerFor("BitsPerComponent"); + if (m_pDict->GetIntegerFor("ImageMask")) + m_bImageMask = true; + + if (m_bImageMask || !m_pDict->KeyExist("ColorSpace")) { + if (!m_bImageMask) { + const CPDF_Object* pFilter = m_pDict->GetDirectObjectFor("Filter"); + if (pFilter) { + ByteString filter; + if (pFilter->IsName()) { + filter = pFilter->GetString(); + } else if (const CPDF_Array* pArray = pFilter->AsArray()) { + filter = pArray->GetStringAt(pArray->GetCount() - 1); + } + + if (filter == "JPXDecode") { + m_bDoBpcCheck = false; + return true; + } + } + } + m_bImageMask = true; + m_bpc = m_nComponents = 1; + const CPDF_Array* pDecode = m_pDict->GetArrayFor("Decode"); + m_bDefaultDecode = !pDecode || !pDecode->GetIntegerAt(0); + return true; + } + + const CPDF_Object* pCSObj = m_pDict->GetDirectObjectFor("ColorSpace"); + if (!pCSObj) + return false; + + CPDF_DocPageData* pDocPageData = m_pDocument->GetPageData(); + if (pFormResources) + m_pColorSpace = pDocPageData->GetColorSpace(pCSObj, pFormResources); + if (!m_pColorSpace) + m_pColorSpace = pDocPageData->GetColorSpace(pCSObj, pPageResources); + if (!m_pColorSpace) + return false; + + m_Family = m_pColorSpace->GetFamily(); + m_nComponents = m_pColorSpace->CountComponents(); + if (m_Family == PDFCS_ICCBASED && pCSObj->IsName()) { + ByteString cs = pCSObj->GetString(); + if (cs == "DeviceGray") + m_nComponents = 1; + else if (cs == "DeviceRGB") + m_nComponents = 3; + else if (cs == "DeviceCMYK") + m_nComponents = 4; + } + ValidateDictParam(); + return GetDecodeAndMaskArray(&m_bDefaultDecode, &m_bColorKey); +} + +bool CPDF_DIBBase::GetDecodeAndMaskArray(bool* bDefaultDecode, + bool* bColorKey) { + if (!m_pColorSpace) + return false; + + m_CompData.resize(m_nComponents); + int max_data = (1 << m_bpc) - 1; + const CPDF_Array* pDecode = m_pDict->GetArrayFor("Decode"); + if (pDecode) { + for (uint32_t i = 0; i < m_nComponents; i++) { + m_CompData[i].m_DecodeMin = pDecode->GetNumberAt(i * 2); + float max = pDecode->GetNumberAt(i * 2 + 1); + m_CompData[i].m_DecodeStep = (max - m_CompData[i].m_DecodeMin) / max_data; + float def_value; + float def_min; + float def_max; + m_pColorSpace->GetDefaultValue(i, &def_value, &def_min, &def_max); + if (m_Family == PDFCS_INDEXED) + def_max = max_data; + if (def_min != m_CompData[i].m_DecodeMin || def_max != max) + *bDefaultDecode = false; + } + } else { + for (uint32_t i = 0; i < m_nComponents; i++) { + float def_value; + m_pColorSpace->GetDefaultValue(i, &def_value, &m_CompData[i].m_DecodeMin, + &m_CompData[i].m_DecodeStep); + if (m_Family == PDFCS_INDEXED) + m_CompData[i].m_DecodeStep = max_data; + m_CompData[i].m_DecodeStep = + (m_CompData[i].m_DecodeStep - m_CompData[i].m_DecodeMin) / max_data; + } + } + if (m_pDict->KeyExist("SMask")) + return true; + + const CPDF_Object* pMask = m_pDict->GetDirectObjectFor("Mask"); + if (!pMask) + return true; + + if (const CPDF_Array* pArray = pMask->AsArray()) { + if (pArray->GetCount() >= m_nComponents * 2) { + for (uint32_t i = 0; i < m_nComponents; i++) { + int min_num = pArray->GetIntegerAt(i * 2); + int max_num = pArray->GetIntegerAt(i * 2 + 1); + m_CompData[i].m_ColorKeyMin = std::max(min_num, 0); + m_CompData[i].m_ColorKeyMax = std::min(max_num, max_data); + } + } + *bColorKey = true; + } + return true; +} + +CPDF_DIBBase::LoadState CPDF_DIBBase::CreateDecoder() { + ByteString decoder = m_pStreamAcc->GetImageDecoder(); + if (decoder.IsEmpty()) + return LoadState::kSuccess; + + if (m_bDoBpcCheck && m_bpc == 0) + return LoadState::kFail; + + if (decoder == "JPXDecode") { + m_pCachedBitmap = LoadJpxBitmap(); + return m_pCachedBitmap ? LoadState::kSuccess : LoadState::kFail; + } + + if (decoder == "JBIG2Decode") { + m_pCachedBitmap = pdfium::MakeRetain(); + if (!m_pCachedBitmap->Create( + m_Width, m_Height, m_bImageMask ? FXDIB_1bppMask : FXDIB_1bppRgb)) { + m_pCachedBitmap.Reset(); + return LoadState::kFail; + } + m_Status = LoadState::kSuccess; + return LoadState::kContinue; + } + + const uint8_t* src_data = m_pStreamAcc->GetData(); + uint32_t src_size = m_pStreamAcc->GetSize(); + const CPDF_Dictionary* pParams = m_pStreamAcc->GetImageParam(); + if (decoder == "CCITTFaxDecode") { + m_pDecoder = FPDFAPI_CreateFaxDecoder(src_data, src_size, m_Width, m_Height, + pParams); + } else if (decoder == "FlateDecode") { + m_pDecoder = FPDFAPI_CreateFlateDecoder( + src_data, src_size, m_Width, m_Height, m_nComponents, m_bpc, pParams); + } else if (decoder == "RunLengthDecode") { + CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); + m_pDecoder = pEncoders->GetBasicModule()->CreateRunLengthDecoder( + src_data, src_size, m_Width, m_Height, m_nComponents, m_bpc); + } else if (decoder == "DCTDecode") { + if (!CreateDCTDecoder(src_data, src_size, pParams)) + return LoadState::kFail; + } + if (!m_pDecoder) + return LoadState::kFail; + + FX_SAFE_UINT32 requested_pitch = + CalculatePitch8(m_bpc, m_nComponents, m_Width); + if (!requested_pitch.IsValid()) + return LoadState::kFail; + FX_SAFE_UINT32 provided_pitch = CalculatePitch8( + m_pDecoder->GetBPC(), m_pDecoder->CountComps(), m_pDecoder->GetWidth()); + if (!provided_pitch.IsValid()) + return LoadState::kFail; + if (provided_pitch.ValueOrDie() < requested_pitch.ValueOrDie()) + return LoadState::kFail; + return LoadState::kSuccess; +} + +bool CPDF_DIBBase::CreateDCTDecoder(const uint8_t* src_data, + uint32_t src_size, + const CPDF_Dictionary* pParams) { + CCodec_JpegModule* pJpegModule = CPDF_ModuleMgr::Get()->GetJpegModule(); + m_pDecoder = pJpegModule->CreateDecoder( + src_data, src_size, m_Width, m_Height, m_nComponents, + !pParams || pParams->GetIntegerFor("ColorTransform", 1)); + if (m_pDecoder) + return true; + + bool bTransform = false; + int comps; + int bpc; + if (!pJpegModule->LoadInfo(src_data, src_size, &m_Width, &m_Height, &comps, + &bpc, &bTransform)) { + return false; + } + + if (m_nComponents == static_cast(comps)) { + m_bpc = bpc; + m_pDecoder = pJpegModule->CreateDecoder( + src_data, src_size, m_Width, m_Height, m_nComponents, bTransform); + return true; + } + + m_nComponents = static_cast(comps); + m_CompData.clear(); + if (m_pColorSpace) { + switch (m_Family) { + case PDFCS_DEVICEGRAY: + case PDFCS_DEVICERGB: + case PDFCS_DEVICECMYK: { + uint32_t dwMinComps = ComponentsForFamily(m_Family); + if (m_pColorSpace->CountComponents() < dwMinComps || + m_nComponents < dwMinComps) { + return false; + } + break; + } + case PDFCS_LAB: { + if (m_nComponents != 3 || m_pColorSpace->CountComponents() < 3) + return false; + break; + } + case PDFCS_ICCBASED: { + if (!IsAllowedICCComponents(m_nComponents) || + !IsAllowedICCComponents(m_pColorSpace->CountComponents()) || + m_pColorSpace->CountComponents() < m_nComponents) { + return false; + } + break; + } + default: { + if (m_pColorSpace->CountComponents() != m_nComponents) + return false; + break; + } + } + } else { + if (m_Family == PDFCS_LAB && m_nComponents != 3) + return false; + } + if (!GetDecodeAndMaskArray(&m_bDefaultDecode, &m_bColorKey)) + return false; + + m_bpc = bpc; + m_pDecoder = pJpegModule->CreateDecoder(src_data, src_size, m_Width, m_Height, + m_nComponents, bTransform); + return true; +} + +RetainPtr CPDF_DIBBase::LoadJpxBitmap() { + CCodec_JpxModule* pJpxModule = CPDF_ModuleMgr::Get()->GetJpxModule(); + auto context = pdfium::MakeUnique(pJpxModule); + context->set_decoder(pJpxModule->CreateDecoder( + m_pStreamAcc->GetData(), m_pStreamAcc->GetSize(), m_pColorSpace.Get())); + if (!context->decoder()) + return nullptr; + + uint32_t width = 0; + uint32_t height = 0; + uint32_t components = 0; + pJpxModule->GetImageInfo(context->decoder(), &width, &height, &components); + if (static_cast(width) < m_Width || static_cast(height) < m_Height) + return nullptr; + + bool bSwapRGB = false; + if (m_pColorSpace) { + if (components != m_pColorSpace->CountComponents()) + return nullptr; + + if (m_pColorSpace == CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB)) { + bSwapRGB = true; + m_pColorSpace = nullptr; + } + } else { + if (components == 3) { + bSwapRGB = true; + } else if (components == 4) { + m_pColorSpace = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICECMYK); + } + m_nComponents = components; + } + + FXDIB_Format format; + if (components == 1) { + format = FXDIB_8bppRgb; + } else if (components <= 3) { + format = FXDIB_Rgb; + } else if (components == 4) { + format = FXDIB_Rgb32; + } else { + width = (width * components + 2) / 3; + format = FXDIB_Rgb; + } + + auto pCachedBitmap = pdfium::MakeRetain(); + if (!pCachedBitmap->Create(width, height, format)) + return nullptr; + + pCachedBitmap->Clear(0xFFFFFFFF); + std::vector output_offsets(components); + for (uint32_t i = 0; i < components; ++i) + output_offsets[i] = i; + if (bSwapRGB) { + output_offsets[0] = 2; + output_offsets[2] = 0; + } + if (!pJpxModule->Decode(context->decoder(), pCachedBitmap->GetBuffer(), + pCachedBitmap->GetPitch(), output_offsets)) { + return nullptr; + } + + if (m_pColorSpace && m_pColorSpace->GetFamily() == PDFCS_INDEXED && + m_bpc < 8) { + int scale = 8 - m_bpc; + for (uint32_t row = 0; row < height; ++row) { + uint8_t* scanline = pCachedBitmap->GetWritableScanline(row); + for (uint32_t col = 0; col < width; ++col) { + *scanline = (*scanline) >> scale; + ++scanline; + } + } + } + m_bpc = 8; + return pCachedBitmap; +} + +CPDF_DIBBase::LoadState CPDF_DIBBase::StartLoadMask() { + m_MatteColor = 0XFFFFFFFF; + m_pMaskStream = m_pDict->GetStreamFor("SMask"); + if (!m_pMaskStream) { + m_pMaskStream = ToStream(m_pDict->GetDirectObjectFor("Mask")); + return m_pMaskStream ? StartLoadMaskDIB() : LoadState::kSuccess; + } + + const CPDF_Array* pMatte = m_pMaskStream->GetDict()->GetArrayFor("Matte"); + if (pMatte && m_pColorSpace && m_Family != PDFCS_PATTERN && + m_pColorSpace->CountComponents() <= m_nComponents) { + std::vector colors(m_nComponents); + for (uint32_t i = 0; i < m_nComponents; i++) + colors[i] = pMatte->GetFloatAt(i); + + float R; + float G; + float B; + m_pColorSpace->GetRGB(colors.data(), &R, &G, &B); + m_MatteColor = ArgbEncode(0, FXSYS_round(R * 255), FXSYS_round(G * 255), + FXSYS_round(B * 255)); + } + return StartLoadMaskDIB(); +} + +CPDF_DIBBase::LoadState CPDF_DIBBase::ContinueLoadMaskDIB( + PauseIndicatorIface* pPause) { + if (!m_pMask) + return LoadState::kSuccess; + + LoadState ret = m_pMask->ContinueLoadDIBBase(pPause); + if (ret == LoadState::kContinue) + return LoadState::kContinue; + + if (m_pColorSpace && m_bStdCS) + m_pColorSpace->EnableStdConversion(false); + + if (ret == LoadState::kFail) { + m_pMask.Reset(); + return LoadState::kFail; + } + return LoadState::kSuccess; +} + +RetainPtr CPDF_DIBBase::DetachMask() { + return std::move(m_pMask); +} + +bool CPDF_DIBBase::IsJBigImage() const { + return m_pStreamAcc->GetImageDecoder() == "JBIG2Decode"; +} + +CPDF_DIBBase::LoadState CPDF_DIBBase::StartLoadMaskDIB() { + m_pMask = pdfium::MakeRetain(); + LoadState ret = + m_pMask->StartLoadDIBBase(m_pDocument.Get(), m_pMaskStream.Get(), false, + nullptr, nullptr, true, 0, false); + if (ret == LoadState::kContinue) { + if (m_Status == LoadState::kFail) + m_Status = LoadState::kContinue; + return LoadState::kContinue; + } + if (ret == LoadState::kFail) + m_pMask.Reset(); + return LoadState::kSuccess; +} + +void CPDF_DIBBase::LoadPalette() { + if (!m_pColorSpace || m_Family == PDFCS_PATTERN) + return; + + if (m_bpc == 0 || m_bpc * m_nComponents > 8) + return; + + if (m_bpc * m_nComponents == 1) { + if (m_bDefaultDecode && + (m_Family == PDFCS_DEVICEGRAY || m_Family == PDFCS_DEVICERGB)) { + return; + } + if (m_pColorSpace->CountComponents() > 3) { + return; + } + float color_values[3]; + color_values[0] = m_CompData[0].m_DecodeMin; + color_values[1] = color_values[0]; + color_values[2] = color_values[0]; + + float R = 0.0f; + float G = 0.0f; + float B = 0.0f; + m_pColorSpace->GetRGB(color_values, &R, &G, &B); + + FX_ARGB argb0 = ArgbEncode(255, FXSYS_round(R * 255), FXSYS_round(G * 255), + FXSYS_round(B * 255)); + color_values[0] += m_CompData[0].m_DecodeStep; + color_values[1] += m_CompData[0].m_DecodeStep; + color_values[2] += m_CompData[0].m_DecodeStep; + m_pColorSpace->GetRGB(color_values, &R, &G, &B); + FX_ARGB argb1 = ArgbEncode(255, FXSYS_round(R * 255), FXSYS_round(G * 255), + FXSYS_round(B * 255)); + if (argb0 != 0xFF000000 || argb1 != 0xFFFFFFFF) { + SetPaletteArgb(0, argb0); + SetPaletteArgb(1, argb1); + } + return; + } + if (m_bpc == 8 && m_bDefaultDecode && + m_pColorSpace == CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY)) { + return; + } + + int palette_count = 1 << (m_bpc * m_nComponents); + // Using at least 16 elements due to the call m_pColorSpace->GetRGB(). + std::vector color_values(std::max(m_nComponents, 16u)); + for (int i = 0; i < palette_count; i++) { + int color_data = i; + for (uint32_t j = 0; j < m_nComponents; j++) { + int encoded_component = color_data % (1 << m_bpc); + color_data /= 1 << m_bpc; + color_values[j] = m_CompData[j].m_DecodeMin + + m_CompData[j].m_DecodeStep * encoded_component; + } + float R = 0; + float G = 0; + float B = 0; + if (m_nComponents == 1 && m_Family == PDFCS_ICCBASED && + m_pColorSpace->CountComponents() > 1) { + int nComponents = m_pColorSpace->CountComponents(); + std::vector temp_buf(nComponents); + for (int k = 0; k < nComponents; ++k) + temp_buf[k] = color_values[0]; + m_pColorSpace->GetRGB(temp_buf.data(), &R, &G, &B); + } else { + m_pColorSpace->GetRGB(color_values.data(), &R, &G, &B); + } + SetPaletteArgb(i, ArgbEncode(255, FXSYS_round(R * 255), + FXSYS_round(G * 255), FXSYS_round(B * 255))); + } +} + +void CPDF_DIBBase::ValidateDictParam() { + m_bpc = m_bpc_orig; + const CPDF_Object* pFilter = m_pDict->GetDirectObjectFor("Filter"); + if (pFilter) { + if (pFilter->IsName()) { + ByteString filter = pFilter->GetString(); + if (filter == "CCITTFaxDecode" || filter == "JBIG2Decode") { + m_bpc = 1; + m_nComponents = 1; + } else if (filter == "RunLengthDecode") { + if (m_bpc != 1) { + m_bpc = 8; + } + } else if (filter == "DCTDecode") { + m_bpc = 8; + } + } else if (const CPDF_Array* pArray = pFilter->AsArray()) { + ByteString filter = pArray->GetStringAt(pArray->GetCount() - 1); + if (filter == "CCITTFaxDecode" || filter == "JBIG2Decode") { + m_bpc = 1; + m_nComponents = 1; + } else if (filter == "DCTDecode") { + // Previously, filter == "RunLengthDecode" was checked in the "if" + // statement as well, but too many documents don't conform to it. + m_bpc = 8; + } + } + } + + if (!IsAllowedBPCValue(m_bpc)) + m_bpc = 0; +} + +void CPDF_DIBBase::TranslateScanline24bpp(uint8_t* dest_scan, + const uint8_t* src_scan) const { + if (m_bpc == 0) + return; + + if (TranslateScanline24bppDefaultDecode(dest_scan, src_scan)) + return; + + // Using at least 16 elements due to the call m_pColorSpace->GetRGB(). + std::vector color_values(std::max(m_nComponents, 16u)); + float R = 0.0f; + float G = 0.0f; + float B = 0.0f; + uint64_t src_bit_pos = 0; + uint64_t src_byte_pos = 0; + size_t dest_byte_pos = 0; + const bool bpp8 = m_bpc == 8; + for (int column = 0; column < m_Width; column++) { + for (uint32_t color = 0; color < m_nComponents; color++) { + if (bpp8) { + uint8_t data = src_scan[src_byte_pos++]; + color_values[color] = m_CompData[color].m_DecodeMin + + m_CompData[color].m_DecodeStep * data; + } else { + unsigned int data = GetBits8(src_scan, src_bit_pos, m_bpc); + color_values[color] = m_CompData[color].m_DecodeMin + + m_CompData[color].m_DecodeStep * data; + src_bit_pos += m_bpc; + } + } + + if (TransMask()) { + float k = 1.0f - color_values[3]; + R = (1.0f - color_values[0]) * k; + G = (1.0f - color_values[1]) * k; + B = (1.0f - color_values[2]) * k; + } else if (m_Family != PDFCS_PATTERN) { + m_pColorSpace->GetRGB(color_values.data(), &R, &G, &B); + } + R = pdfium::clamp(R, 0.0f, 1.0f); + G = pdfium::clamp(G, 0.0f, 1.0f); + B = pdfium::clamp(B, 0.0f, 1.0f); + dest_scan[dest_byte_pos] = static_cast(B * 255); + dest_scan[dest_byte_pos + 1] = static_cast(G * 255); + dest_scan[dest_byte_pos + 2] = static_cast(R * 255); + dest_byte_pos += 3; + } +} + +bool CPDF_DIBBase::TranslateScanline24bppDefaultDecode( + uint8_t* dest_scan, + const uint8_t* src_scan) const { + if (!m_bDefaultDecode) + return false; + + if (m_Family != PDFCS_DEVICERGB && m_Family != PDFCS_CALRGB) { + if (m_bpc != 8) + return false; + + if (m_nComponents == m_pColorSpace->CountComponents()) { + m_pColorSpace->TranslateImageLine(dest_scan, src_scan, m_Width, m_Width, + m_Height, TransMask()); + } + return true; + } + + if (m_nComponents != 3) + return true; + + const uint8_t* src_pos = src_scan; + switch (m_bpc) { + case 8: + for (int column = 0; column < m_Width; column++) { + *dest_scan++ = src_pos[2]; + *dest_scan++ = src_pos[1]; + *dest_scan++ = *src_pos; + src_pos += 3; + } + break; + case 16: + for (int col = 0; col < m_Width; col++) { + *dest_scan++ = src_pos[4]; + *dest_scan++ = src_pos[2]; + *dest_scan++ = *src_pos; + src_pos += 6; + } + break; + default: + const unsigned int max_data = (1 << m_bpc) - 1; + uint64_t src_bit_pos = 0; + size_t dest_byte_pos = 0; + for (int column = 0; column < m_Width; column++) { + unsigned int R = GetBits8(src_scan, src_bit_pos, m_bpc); + src_bit_pos += m_bpc; + unsigned int G = GetBits8(src_scan, src_bit_pos, m_bpc); + src_bit_pos += m_bpc; + unsigned int B = GetBits8(src_scan, src_bit_pos, m_bpc); + src_bit_pos += m_bpc; + R = std::min(R, max_data); + G = std::min(G, max_data); + B = std::min(B, max_data); + dest_scan[dest_byte_pos] = B * 255 / max_data; + dest_scan[dest_byte_pos + 1] = G * 255 / max_data; + dest_scan[dest_byte_pos + 2] = R * 255 / max_data; + dest_byte_pos += 3; + } + break; + } + return true; +} + +uint8_t* CPDF_DIBBase::GetBuffer() const { + return m_pCachedBitmap ? m_pCachedBitmap->GetBuffer() : nullptr; +} + +const uint8_t* CPDF_DIBBase::GetScanline(int line) const { + if (m_bpc == 0) + return nullptr; + + FX_SAFE_UINT32 src_pitch = CalculatePitch8(m_bpc, m_nComponents, m_Width); + if (!src_pitch.IsValid()) + return nullptr; + uint32_t src_pitch_value = src_pitch.ValueOrDie(); + + const uint8_t* pSrcLine = nullptr; + if (m_pCachedBitmap && src_pitch_value <= m_pCachedBitmap->GetPitch()) { + if (line >= m_pCachedBitmap->GetHeight()) { + line = m_pCachedBitmap->GetHeight() - 1; + } + pSrcLine = m_pCachedBitmap->GetScanline(line); + } else if (m_pDecoder) { + pSrcLine = m_pDecoder->GetScanline(line); + } else if (m_pStreamAcc->GetSize() >= (line + 1) * src_pitch_value) { + pSrcLine = m_pStreamAcc->GetData() + line * src_pitch_value; + } + if (!pSrcLine) { + uint8_t* pLineBuf = m_pMaskedLine ? m_pMaskedLine.get() : m_pLineBuf.get(); + memset(pLineBuf, 0xFF, m_Pitch); + return pLineBuf; + } + + if (m_bpc * m_nComponents == 1) { + if (m_bImageMask && m_bDefaultDecode) { + for (uint32_t i = 0; i < src_pitch_value; i++) + m_pLineBuf.get()[i] = ~pSrcLine[i]; + return m_pLineBuf.get(); + } + + if (!m_bColorKey) { + memcpy(m_pLineBuf.get(), pSrcLine, src_pitch_value); + return m_pLineBuf.get(); + } + + uint32_t reset_argb = m_pPalette ? m_pPalette.get()[0] : 0xFF000000; + uint32_t set_argb = m_pPalette ? m_pPalette.get()[1] : 0xFFFFFFFF; + if (m_CompData[0].m_ColorKeyMin == 0) + reset_argb = 0; + if (m_CompData[0].m_ColorKeyMax == 1) + set_argb = 0; + set_argb = FXARGB_TODIB(set_argb); + reset_argb = FXARGB_TODIB(reset_argb); + uint32_t* dest_scan = reinterpret_cast(m_pMaskedLine.get()); + for (int col = 0; col < m_Width; col++) { + *dest_scan = GetBitValue(pSrcLine, col) ? set_argb : reset_argb; + dest_scan++; + } + return m_pMaskedLine.get(); + } + if (m_bpc * m_nComponents <= 8) { + if (m_bpc == 8) { + memcpy(m_pLineBuf.get(), pSrcLine, src_pitch_value); + } else { + uint64_t src_bit_pos = 0; + for (int col = 0; col < m_Width; col++) { + unsigned int color_index = 0; + for (uint32_t color = 0; color < m_nComponents; color++) { + unsigned int data = GetBits8(pSrcLine, src_bit_pos, m_bpc); + color_index |= data << (color * m_bpc); + src_bit_pos += m_bpc; + } + m_pLineBuf.get()[col] = color_index; + } + } + if (!m_bColorKey) + return m_pLineBuf.get(); + + uint8_t* pDestPixel = m_pMaskedLine.get(); + const uint8_t* pSrcPixel = m_pLineBuf.get(); + for (int col = 0; col < m_Width; col++) { + uint8_t index = *pSrcPixel++; + if (m_pPalette) { + *pDestPixel++ = FXARGB_B(m_pPalette.get()[index]); + *pDestPixel++ = FXARGB_G(m_pPalette.get()[index]); + *pDestPixel++ = FXARGB_R(m_pPalette.get()[index]); + } else { + *pDestPixel++ = index; + *pDestPixel++ = index; + *pDestPixel++ = index; + } + *pDestPixel = IsColorIndexOutOfBounds(index, m_CompData[0]) ? 0xFF : 0; + pDestPixel++; + } + return m_pMaskedLine.get(); + } + if (m_bColorKey) { + if (m_nComponents == 3 && m_bpc == 8) { + uint8_t* alpha_channel = m_pMaskedLine.get() + 3; + for (int col = 0; col < m_Width; col++) { + const uint8_t* pPixel = pSrcLine + col * 3; + alpha_channel[col * 4] = + AreColorIndicesOutOfBounds(pPixel, m_CompData.data(), 3) ? 0xFF : 0; + } + } else { + memset(m_pMaskedLine.get(), 0xFF, m_Pitch); + } + } + if (m_pColorSpace) { + TranslateScanline24bpp(m_pLineBuf.get(), pSrcLine); + pSrcLine = m_pLineBuf.get(); + } + if (!m_bColorKey) + return pSrcLine; + + const uint8_t* pSrcPixel = pSrcLine; + uint8_t* pDestPixel = m_pMaskedLine.get(); + for (int col = 0; col < m_Width; col++) { + *pDestPixel++ = *pSrcPixel++; + *pDestPixel++ = *pSrcPixel++; + *pDestPixel++ = *pSrcPixel++; + pDestPixel++; + } + return m_pMaskedLine.get(); +} + +bool CPDF_DIBBase::SkipToScanline(int line, PauseIndicatorIface* pPause) const { + return m_pDecoder && m_pDecoder->SkipToScanline(line, pPause); +} + +void CPDF_DIBBase::DownSampleScanline(int line, + uint8_t* dest_scan, + int dest_bpp, + int dest_width, + bool bFlipX, + int clip_left, + int clip_width) const { + if (line < 0 || !dest_scan || dest_bpp <= 0 || dest_width <= 0 || + clip_left < 0 || clip_width <= 0) { + return; + } + + uint32_t src_width = m_Width; + FX_SAFE_UINT32 pitch = CalculatePitch8(m_bpc, m_nComponents, m_Width); + if (!pitch.IsValid()) + return; + + const uint8_t* pSrcLine = nullptr; + if (m_pCachedBitmap) { + pSrcLine = m_pCachedBitmap->GetScanline(line); + } else if (m_pDecoder) { + pSrcLine = m_pDecoder->GetScanline(line); + } else { + uint32_t src_pitch = pitch.ValueOrDie(); + pitch *= (line + 1); + if (!pitch.IsValid()) { + return; + } + + if (m_pStreamAcc->GetSize() >= pitch.ValueOrDie()) { + pSrcLine = m_pStreamAcc->GetData() + line * src_pitch; + } + } + int orig_Bpp = m_bpc * m_nComponents / 8; + int dest_Bpp = dest_bpp / 8; + if (!pSrcLine) { + memset(dest_scan, 0xFF, dest_Bpp * clip_width); + return; + } + + FX_SAFE_INT32 max_src_x = clip_left; + max_src_x += clip_width - 1; + max_src_x *= src_width; + max_src_x /= dest_width; + if (!max_src_x.IsValid()) + return; + + if (m_bpc * m_nComponents == 1) { + DownSampleScanline1Bit(orig_Bpp, dest_Bpp, src_width, pSrcLine, dest_scan, + dest_width, bFlipX, clip_left, clip_width); + } else if (m_bpc * m_nComponents <= 8) { + DownSampleScanline8Bit(orig_Bpp, dest_Bpp, src_width, pSrcLine, dest_scan, + dest_width, bFlipX, clip_left, clip_width); + } else { + DownSampleScanline32Bit(orig_Bpp, dest_Bpp, src_width, pSrcLine, dest_scan, + dest_width, bFlipX, clip_left, clip_width); + } +} + +void CPDF_DIBBase::DownSampleScanline1Bit(int orig_Bpp, + int dest_Bpp, + uint32_t src_width, + const uint8_t* pSrcLine, + uint8_t* dest_scan, + int dest_width, + bool bFlipX, + int clip_left, + int clip_width) const { + if (m_bColorKey && !m_bImageMask) { + uint32_t reset_argb = m_pPalette ? m_pPalette.get()[0] : 0xFF000000; + uint32_t set_argb = m_pPalette ? m_pPalette.get()[1] : 0xFFFFFFFF; + if (m_CompData[0].m_ColorKeyMin == 0) + reset_argb = 0; + if (m_CompData[0].m_ColorKeyMax == 1) + set_argb = 0; + set_argb = FXARGB_TODIB(set_argb); + reset_argb = FXARGB_TODIB(reset_argb); + uint32_t* dest_scan_dword = reinterpret_cast(dest_scan); + for (int i = 0; i < clip_width; i++) { + uint32_t src_x = (clip_left + i) * src_width / dest_width; + if (bFlipX) + src_x = src_width - src_x - 1; + src_x %= src_width; + dest_scan_dword[i] = GetBitValue(pSrcLine, src_x) ? set_argb : reset_argb; + } + return; + } + + uint32_t set_argb = 0xFFFFFFFF; + uint32_t reset_argb = 0; + if (m_bImageMask) { + if (m_bDefaultDecode) { + set_argb = 0; + reset_argb = 0xFFFFFFFF; + } + } else if (m_pPalette && dest_Bpp != 1) { + reset_argb = m_pPalette.get()[0]; + set_argb = m_pPalette.get()[1]; + } + for (int i = 0; i < clip_width; i++) { + uint32_t src_x = (clip_left + i) * src_width / dest_width; + if (bFlipX) + src_x = src_width - src_x - 1; + src_x %= src_width; + int dest_pos = i * dest_Bpp; + uint32_t value_argb = GetBitValue(pSrcLine, src_x) ? set_argb : reset_argb; + if (dest_Bpp == 1) { + dest_scan[dest_pos] = static_cast(value_argb); + } else if (dest_Bpp == 3) { + dest_scan[dest_pos] = FXARGB_B(value_argb); + dest_scan[dest_pos + 1] = FXARGB_G(value_argb); + dest_scan[dest_pos + 2] = FXARGB_R(value_argb); + } else { + *reinterpret_cast(dest_scan + dest_pos) = value_argb; + } + } +} + +void CPDF_DIBBase::DownSampleScanline8Bit(int orig_Bpp, + int dest_Bpp, + uint32_t src_width, + const uint8_t* pSrcLine, + uint8_t* dest_scan, + int dest_width, + bool bFlipX, + int clip_left, + int clip_width) const { + if (m_bpc < 8) { + uint64_t src_bit_pos = 0; + for (uint32_t col = 0; col < src_width; col++) { + unsigned int color_index = 0; + for (uint32_t color = 0; color < m_nComponents; color++) { + unsigned int data = GetBits8(pSrcLine, src_bit_pos, m_bpc); + color_index |= data << (color * m_bpc); + src_bit_pos += m_bpc; + } + m_pLineBuf.get()[col] = color_index; + } + pSrcLine = m_pLineBuf.get(); + } + if (m_bColorKey) { + for (int i = 0; i < clip_width; i++) { + uint32_t src_x = (clip_left + i) * src_width / dest_width; + if (bFlipX) { + src_x = src_width - src_x - 1; + } + src_x %= src_width; + uint8_t* pDestPixel = dest_scan + i * 4; + uint8_t index = pSrcLine[src_x]; + if (m_pPalette) { + *pDestPixel++ = FXARGB_B(m_pPalette.get()[index]); + *pDestPixel++ = FXARGB_G(m_pPalette.get()[index]); + *pDestPixel++ = FXARGB_R(m_pPalette.get()[index]); + } else { + *pDestPixel++ = index; + *pDestPixel++ = index; + *pDestPixel++ = index; + } + *pDestPixel = (index < m_CompData[0].m_ColorKeyMin || + index > m_CompData[0].m_ColorKeyMax) + ? 0xFF + : 0; + } + return; + } + for (int i = 0; i < clip_width; i++) { + uint32_t src_x = (clip_left + i) * src_width / dest_width; + if (bFlipX) + src_x = src_width - src_x - 1; + src_x %= src_width; + uint8_t index = pSrcLine[src_x]; + if (dest_Bpp == 1) { + dest_scan[i] = index; + } else { + int dest_pos = i * dest_Bpp; + FX_ARGB argb = m_pPalette.get()[index]; + dest_scan[dest_pos] = FXARGB_B(argb); + dest_scan[dest_pos + 1] = FXARGB_G(argb); + dest_scan[dest_pos + 2] = FXARGB_R(argb); + } + } +} + +void CPDF_DIBBase::DownSampleScanline32Bit(int orig_Bpp, + int dest_Bpp, + uint32_t src_width, + const uint8_t* pSrcLine, + uint8_t* dest_scan, + int dest_width, + bool bFlipX, + int clip_left, + int clip_width) const { + // last_src_x used to store the last seen src_x position which should be + // in [0, src_width). Set the initial value to be an invalid src_x value. + uint32_t last_src_x = src_width; + FX_ARGB last_argb = ArgbEncode(0xFF, 0xFF, 0xFF, 0xFF); + float unit_To8Bpc = 255.0f / ((1 << m_bpc) - 1); + for (int i = 0; i < clip_width; i++) { + int dest_x = clip_left + i; + uint32_t src_x = (bFlipX ? (dest_width - dest_x - 1) : dest_x) * + (int64_t)src_width / dest_width; + src_x %= src_width; + + uint8_t* pDestPixel = dest_scan + i * dest_Bpp; + FX_ARGB argb; + if (src_x == last_src_x) { + argb = last_argb; + } else { + CFX_FixedBufGrow extracted_components(m_nComponents); + const uint8_t* pSrcPixel = nullptr; + if (m_bpc % 8 != 0) { + // No need to check for 32-bit overflow, as |src_x| is bounded by + // |src_width| and DownSampleScanline() already checked for overflow + // with the pitch calculation. + size_t num_bits = src_x * m_bpc * m_nComponents; + uint64_t src_bit_pos = num_bits % 8; + pSrcPixel = pSrcLine + num_bits / 8; + for (uint32_t j = 0; j < m_nComponents; ++j) { + extracted_components[j] = static_cast( + GetBits8(pSrcPixel, src_bit_pos, m_bpc) * unit_To8Bpc); + src_bit_pos += m_bpc; + } + pSrcPixel = extracted_components; + } else { + pSrcPixel = pSrcLine + src_x * orig_Bpp; + if (m_bpc == 16) { + for (uint32_t j = 0; j < m_nComponents; ++j) + extracted_components[j] = pSrcPixel[j * 2]; + pSrcPixel = extracted_components; + } + } + + if (m_pColorSpace) { + uint8_t color[4]; + const bool bTransMask = TransMask(); + if (!m_bDefaultDecode) { + for (uint32_t j = 0; j < m_nComponents; ++j) { + float component_value = static_cast(pSrcPixel[j]); + int color_value = static_cast( + (m_CompData[j].m_DecodeMin + + m_CompData[j].m_DecodeStep * component_value) * + 255.0f + + 0.5f); + extracted_components[j] = pdfium::clamp(color_value, 0, 255); + } + } + const uint8_t* pSrc = + m_bDefaultDecode ? pSrcPixel : extracted_components; + m_pColorSpace->TranslateImageLine(color, pSrc, 1, 0, 0, bTransMask); + argb = ArgbEncode(0xFF, color[2], color[1], color[0]); + } else { + argb = ArgbEncode(0xFF, pSrcPixel[2], pSrcPixel[1], pSrcPixel[0]); + } + if (m_bColorKey) { + int alpha = 0xFF; + if (m_nComponents == 3 && m_bpc == 8) { + alpha = (pSrcPixel[0] < m_CompData[0].m_ColorKeyMin || + pSrcPixel[0] > m_CompData[0].m_ColorKeyMax || + pSrcPixel[1] < m_CompData[1].m_ColorKeyMin || + pSrcPixel[1] > m_CompData[1].m_ColorKeyMax || + pSrcPixel[2] < m_CompData[2].m_ColorKeyMin || + pSrcPixel[2] > m_CompData[2].m_ColorKeyMax) + ? 0xFF + : 0; + } + argb &= 0xFFFFFF; + argb |= alpha << 24; + } + last_src_x = src_x; + last_argb = argb; + } + if (dest_Bpp == 4) { + *reinterpret_cast(pDestPixel) = FXARGB_TODIB(argb); + } else { + *pDestPixel++ = FXARGB_B(argb); + *pDestPixel++ = FXARGB_G(argb); + *pDestPixel = FXARGB_R(argb); + } + } +} + +bool CPDF_DIBBase::TransMask() const { + return m_bLoadMask && m_GroupFamily == PDFCS_DEVICECMYK && + m_Family == PDFCS_DEVICECMYK; +} diff --git a/core/fpdfapi/render/cpdf_dibbase.h b/core/fpdfapi/render/cpdf_dibbase.h new file mode 100644 index 0000000000..a1008470b6 --- /dev/null +++ b/core/fpdfapi/render/cpdf_dibbase.h @@ -0,0 +1,163 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FPDFAPI_RENDER_CPDF_DIBBASE_H_ +#define CORE_FPDFAPI_RENDER_CPDF_DIBBASE_H_ + +#include +#include +#include + +#include "core/fpdfapi/page/cpdf_clippath.h" +#include "core/fpdfapi/page/cpdf_countedobject.h" +#include "core/fpdfapi/page/cpdf_graphicstates.h" +#include "core/fpdfapi/parser/cpdf_stream_acc.h" +#include "core/fpdfapi/render/cpdf_imageloader.h" +#include "core/fpdfapi/render/cpdf_rendercontext.h" +#include "core/fpdfapi/render/cpdf_renderoptions.h" +#include "core/fxcrt/retain_ptr.h" +#include "core/fxcrt/unowned_ptr.h" +#include "core/fxge/cfx_defaultrenderdevice.h" +#include "core/fxge/dib/cfx_dibbase.h" + +class CCodec_Jbig2Context; +class CCodec_ScanlineDecoder; +class CPDF_Color; +class CPDF_Dictionary; +class CPDF_Document; +class CPDF_Stream; + +struct DIB_COMP_DATA { + float m_DecodeMin; + float m_DecodeStep; + int m_ColorKeyMin; + int m_ColorKeyMax; +}; + +#define FPDF_HUGE_IMAGE_SIZE 60000000 + +class CPDF_DIBBase : public CFX_DIBBase { + public: + enum class LoadState : uint8_t { kFail, kSuccess, kContinue }; + + template + friend RetainPtr pdfium::MakeRetain(Args&&... args); + + bool Load(CPDF_Document* pDoc, const CPDF_Stream* pStream); + + // CFX_DIBBase + bool SkipToScanline(int line, PauseIndicatorIface* pPause) const override; + uint8_t* GetBuffer() const override; + const uint8_t* GetScanline(int line) const override; + void DownSampleScanline(int line, + uint8_t* dest_scan, + int dest_bpp, + int dest_width, + bool bFlipX, + int clip_left, + int clip_width) const override; + + const CPDF_ColorSpace* GetColorSpace() const { return m_pColorSpace.Get(); } + uint32_t GetMatteColor() const { return m_MatteColor; } + + LoadState StartLoadDIBBase(CPDF_Document* pDoc, + const CPDF_Stream* pStream, + bool bHasMask, + const CPDF_Dictionary* pFormResources, + CPDF_Dictionary* pPageResources, + bool bStdCS, + uint32_t GroupFamily, + bool bLoadMask); + LoadState ContinueLoadDIBBase(PauseIndicatorIface* pPause); + RetainPtr DetachMask(); + + bool IsJBigImage() const; + + private: + CPDF_DIBBase(); + ~CPDF_DIBBase() override; + + LoadState StartLoadMask(); + LoadState StartLoadMaskDIB(); + bool ContinueToLoadMask(); + LoadState ContinueLoadMaskDIB(PauseIndicatorIface* pPause); + bool LoadColorInfo(const CPDF_Dictionary* pFormResources, + const CPDF_Dictionary* pPageResources); + bool GetDecodeAndMaskArray(bool* bDefaultDecode, bool* bColorKey); + RetainPtr LoadJpxBitmap(); + void LoadPalette(); + LoadState CreateDecoder(); + bool CreateDCTDecoder(const uint8_t* src_data, + uint32_t src_size, + const CPDF_Dictionary* pParams); + void TranslateScanline24bpp(uint8_t* dest_scan, + const uint8_t* src_scan) const; + bool TranslateScanline24bppDefaultDecode(uint8_t* dest_scan, + const uint8_t* src_scan) const; + void ValidateDictParam(); + void DownSampleScanline1Bit(int orig_Bpp, + int dest_Bpp, + uint32_t src_width, + const uint8_t* pSrcLine, + uint8_t* dest_scan, + int dest_width, + bool bFlipX, + int clip_left, + int clip_width) const; + void DownSampleScanline8Bit(int orig_Bpp, + int dest_Bpp, + uint32_t src_width, + const uint8_t* pSrcLine, + uint8_t* dest_scan, + int dest_width, + bool bFlipX, + int clip_left, + int clip_width) const; + void DownSampleScanline32Bit(int orig_Bpp, + int dest_Bpp, + uint32_t src_width, + const uint8_t* pSrcLine, + uint8_t* dest_scan, + int dest_width, + bool bFlipX, + int clip_left, + int clip_width) const; + bool TransMask() const; + + UnownedPtr m_pDocument; + UnownedPtr m_pStream; + UnownedPtr m_pDict; + RetainPtr m_pStreamAcc; + UnownedPtr m_pColorSpace; + uint32_t m_Family = 0; + uint32_t m_bpc = 0; + uint32_t m_bpc_orig = 0; + uint32_t m_nComponents = 0; + uint32_t m_GroupFamily = 0; + uint32_t m_MatteColor = 0; + bool m_bLoadMask = false; + bool m_bDefaultDecode = true; + bool m_bImageMask = false; + bool m_bDoBpcCheck = true; + bool m_bColorKey = false; + bool m_bHasMask = false; + bool m_bStdCS = false; + std::vector m_CompData; + std::unique_ptr m_pLineBuf; + std::unique_ptr m_pMaskedLine; + RetainPtr m_pCachedBitmap; + RetainPtr m_pMask; + RetainPtr m_pGlobalStream; + std::unique_ptr m_pDecoder; + + // Must come after |m_pCachedBitmap|. + std::unique_ptr m_pJbig2Context; + + UnownedPtr m_pMaskStream; + LoadState m_Status = LoadState::kFail; +}; + +#endif // CORE_FPDFAPI_RENDER_CPDF_DIBBASE_H_ diff --git a/core/fpdfapi/render/cpdf_dibsource.cpp b/core/fpdfapi/render/cpdf_dibsource.cpp deleted file mode 100644 index 0c69852354..0000000000 --- a/core/fpdfapi/render/cpdf_dibsource.cpp +++ /dev/null @@ -1,1350 +0,0 @@ -// Copyright 2017 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "core/fpdfapi/render/cpdf_dibsource.h" - -#include -#include -#include -#include - -#include "core/fpdfapi/cpdf_modulemgr.h" -#include "core/fpdfapi/page/cpdf_docpagedata.h" -#include "core/fpdfapi/page/cpdf_image.h" -#include "core/fpdfapi/page/cpdf_imageobject.h" -#include "core/fpdfapi/parser/cpdf_array.h" -#include "core/fpdfapi/parser/cpdf_dictionary.h" -#include "core/fpdfapi/parser/cpdf_document.h" -#include "core/fpdfapi/parser/fpdf_parser_decode.h" -#include "core/fpdfapi/render/cpdf_pagerendercache.h" -#include "core/fpdfapi/render/cpdf_renderstatus.h" -#include "core/fxcodec/codec/ccodec_basicmodule.h" -#include "core/fxcodec/codec/ccodec_jbig2module.h" -#include "core/fxcodec/codec/ccodec_jpegmodule.h" -#include "core/fxcodec/codec/ccodec_jpxmodule.h" -#include "core/fxcodec/codec/ccodec_scanlinedecoder.h" -#include "core/fxcodec/codec/cjpx_decoder.h" -#include "core/fxcodec/fx_codec.h" -#include "core/fxcrt/cfx_fixedbufgrow.h" -#include "core/fxcrt/fx_safe_types.h" -#include "core/fxge/dib/cfx_dibitmap.h" -#include "third_party/base/ptr_util.h" - -namespace { - -constexpr int kMaxImageDimension = 0x01FFFF; - -unsigned int GetBits8(const uint8_t* pData, uint64_t bitpos, size_t nbits) { - ASSERT(nbits == 1 || nbits == 2 || nbits == 4 || nbits == 8 || nbits == 16); - ASSERT((bitpos & (nbits - 1)) == 0); - unsigned int byte = pData[bitpos / 8]; - if (nbits == 8) - return byte; - - if (nbits == 16) - return byte * 256 + pData[bitpos / 8 + 1]; - - return (byte >> (8 - nbits - (bitpos % 8))) & ((1 << nbits) - 1); -} - -bool GetBitValue(const uint8_t* pSrc, uint32_t pos) { - return pSrc[pos / 8] & (1 << (7 - pos % 8)); -} - -bool IsAllowedBPCValue(int bpc) { - return bpc == 1 || bpc == 2 || bpc == 4 || bpc == 8 || bpc == 16; -} - -bool IsAllowedICCComponents(int nComp) { - return nComp == 1 || nComp == 3 || nComp == 4; -} - -bool IsColorIndexOutOfBounds(uint8_t index, const DIB_COMP_DATA& comp_datum) { - return index < comp_datum.m_ColorKeyMin || index > comp_datum.m_ColorKeyMax; -} - -bool AreColorIndicesOutOfBounds(const uint8_t* indices, - const DIB_COMP_DATA* comp_data, - size_t count) { - for (size_t i = 0; i < count; ++i) { - if (IsColorIndexOutOfBounds(indices[i], comp_data[i])) - return true; - } - return false; -} - -// Wrapper class to use with std::unique_ptr for CJPX_Decoder. -class JpxBitMapContext { - public: - explicit JpxBitMapContext(CCodec_JpxModule* jpx_module) - : jpx_module_(jpx_module), decoder_(nullptr) {} - - ~JpxBitMapContext() {} - - void set_decoder(std::unique_ptr decoder) { - decoder_ = std::move(decoder); - } - - CJPX_Decoder* decoder() { return decoder_.get(); } - - private: - CCodec_JpxModule* const jpx_module_; // Weak pointer. - std::unique_ptr decoder_; - - // Disallow evil constructors - JpxBitMapContext(const JpxBitMapContext&); - void operator=(const JpxBitMapContext&); -}; - -} // namespace - -CPDF_DIBSource::CPDF_DIBSource() {} - -CPDF_DIBSource::~CPDF_DIBSource() { - if (m_pColorSpace && m_pDocument) { - auto* pPageData = m_pDocument->GetPageData(); - if (pPageData) { - auto* pSpace = m_pColorSpace.Release(); - pPageData->ReleaseColorSpace(pSpace->GetArray()); - } - } -} - -bool CPDF_DIBSource::Load(CPDF_Document* pDoc, const CPDF_Stream* pStream) { - if (!pStream) - return false; - - m_pDocument = pDoc; - m_pDict = pStream->GetDict(); - if (!m_pDict) - return false; - - m_pStream = pStream; - m_Width = m_pDict->GetIntegerFor("Width"); - m_Height = m_pDict->GetIntegerFor("Height"); - if (m_Width <= 0 || m_Height <= 0 || m_Width > kMaxImageDimension || - m_Height > kMaxImageDimension) { - return false; - } - m_GroupFamily = 0; - m_bLoadMask = false; - if (!LoadColorInfo(nullptr, nullptr)) - return false; - - if (m_bDoBpcCheck && (m_bpc == 0 || m_nComponents == 0)) - return false; - - FX_SAFE_UINT32 src_size = - CalculatePitch8(m_bpc, m_nComponents, m_Width) * m_Height; - if (!src_size.IsValid()) - return false; - - m_pStreamAcc = pdfium::MakeRetain(pStream); - m_pStreamAcc->LoadAllData(false, src_size.ValueOrDie(), true); - if (m_pStreamAcc->GetSize() == 0 || !m_pStreamAcc->GetData()) - return false; - - if (CreateDecoder() == LoadState::kFail) - return false; - - if (m_bImageMask) { - m_bpp = 1; - m_bpc = 1; - m_nComponents = 1; - m_AlphaFlag = 1; - } else if (m_bpc * m_nComponents == 1) { - m_bpp = 1; - } else if (m_bpc * m_nComponents <= 8) { - m_bpp = 8; - } else { - m_bpp = 24; - } - FX_SAFE_UINT32 pitch = CalculatePitch32(m_bpp, m_Width); - if (!pitch.IsValid()) - return false; - - m_pLineBuf.reset(FX_Alloc(uint8_t, pitch.ValueOrDie())); - LoadPalette(); - if (m_bColorKey) { - m_bpp = 32; - m_AlphaFlag = 2; - pitch = CalculatePitch32(m_bpp, m_Width); - if (!pitch.IsValid()) - return false; - - m_pMaskedLine.reset(FX_Alloc(uint8_t, pitch.ValueOrDie())); - } - m_Pitch = pitch.ValueOrDie(); - return true; -} - -bool CPDF_DIBSource::ContinueToLoadMask() { - if (m_bImageMask) { - m_bpp = 1; - m_bpc = 1; - m_nComponents = 1; - m_AlphaFlag = 1; - } else if (m_bpc * m_nComponents == 1) { - m_bpp = 1; - } else if (m_bpc * m_nComponents <= 8) { - m_bpp = 8; - } else { - m_bpp = 24; - } - if (!m_bpc || !m_nComponents) { - return false; - } - FX_SAFE_UINT32 pitch = CalculatePitch32(m_bpp, m_Width); - if (!pitch.IsValid()) - return false; - - m_pLineBuf.reset(FX_Alloc(uint8_t, pitch.ValueOrDie())); - if (m_pColorSpace && m_bStdCS) { - m_pColorSpace->EnableStdConversion(true); - } - LoadPalette(); - if (m_bColorKey) { - m_bpp = 32; - m_AlphaFlag = 2; - pitch = CalculatePitch32(m_bpp, m_Width); - if (!pitch.IsValid()) - return false; - m_pMaskedLine.reset(FX_Alloc(uint8_t, pitch.ValueOrDie())); - } - m_Pitch = pitch.ValueOrDie(); - return true; -} - -CPDF_DIBSource::LoadState CPDF_DIBSource::StartLoadDIBSource( - CPDF_Document* pDoc, - const CPDF_Stream* pStream, - bool bHasMask, - const CPDF_Dictionary* pFormResources, - CPDF_Dictionary* pPageResources, - bool bStdCS, - uint32_t GroupFamily, - bool bLoadMask) { - if (!pStream) - return LoadState::kFail; - - m_pDocument = pDoc; - m_pDict = pStream->GetDict(); - m_pStream = pStream; - m_bStdCS = bStdCS; - m_bHasMask = bHasMask; - m_Width = m_pDict->GetIntegerFor("Width"); - m_Height = m_pDict->GetIntegerFor("Height"); - if (m_Width <= 0 || m_Height <= 0 || m_Width > kMaxImageDimension || - m_Height > kMaxImageDimension) { - return LoadState::kFail; - } - m_GroupFamily = GroupFamily; - m_bLoadMask = bLoadMask; - if (!LoadColorInfo(m_pStream->IsInline() ? pFormResources : nullptr, - pPageResources)) { - return LoadState::kFail; - } - if (m_bDoBpcCheck && (m_bpc == 0 || m_nComponents == 0)) - return LoadState::kFail; - - FX_SAFE_UINT32 src_size = - CalculatePitch8(m_bpc, m_nComponents, m_Width) * m_Height; - if (!src_size.IsValid()) - return LoadState::kFail; - - m_pStreamAcc = pdfium::MakeRetain(pStream); - m_pStreamAcc->LoadAllData(false, src_size.ValueOrDie(), true); - if (m_pStreamAcc->GetSize() == 0 || !m_pStreamAcc->GetData()) - return LoadState::kFail; - - LoadState iCreatedDecoder = CreateDecoder(); - if (iCreatedDecoder == LoadState::kFail) - return LoadState::kFail; - - if (!ContinueToLoadMask()) - return LoadState::kFail; - - LoadState iLoadedMask = m_bHasMask ? StartLoadMask() : LoadState::kSuccess; - if (iCreatedDecoder == LoadState::kContinue || - iLoadedMask == LoadState::kContinue) { - return LoadState::kContinue; - } - - ASSERT(iCreatedDecoder == LoadState::kSuccess); - ASSERT(iLoadedMask == LoadState::kSuccess); - if (m_pColorSpace && m_bStdCS) - m_pColorSpace->EnableStdConversion(false); - return LoadState::kSuccess; -} - -CPDF_DIBSource::LoadState CPDF_DIBSource::ContinueLoadDIBSource( - PauseIndicatorIface* pPause) { - if (m_Status == LoadState::kContinue) - return ContinueLoadMaskDIB(pPause); - - if (m_Status == LoadState::kFail) - return LoadState::kFail; - - if (m_pStreamAcc->GetImageDecoder() == "JPXDecode") - return LoadState::kFail; - - FXCODEC_STATUS iDecodeStatus; - CCodec_Jbig2Module* pJbig2Module = CPDF_ModuleMgr::Get()->GetJbig2Module(); - if (!m_pJbig2Context) { - m_pJbig2Context = pdfium::MakeUnique(); - if (m_pStreamAcc->GetImageParam()) { - const CPDF_Stream* pGlobals = - m_pStreamAcc->GetImageParam()->GetStreamFor("JBIG2Globals"); - if (pGlobals) { - m_pGlobalStream = pdfium::MakeRetain(pGlobals); - m_pGlobalStream->LoadAllDataFiltered(); - } - } - iDecodeStatus = pJbig2Module->StartDecode( - m_pJbig2Context.get(), m_pDocument->CodecContext(), m_Width, m_Height, - m_pStreamAcc, m_pGlobalStream, m_pCachedBitmap->GetBuffer(), - m_pCachedBitmap->GetPitch(), pPause); - } else { - iDecodeStatus = pJbig2Module->ContinueDecode(m_pJbig2Context.get(), pPause); - } - - if (iDecodeStatus < 0) { - m_pJbig2Context.reset(); - m_pCachedBitmap.Reset(); - m_pGlobalStream.Reset(); - return LoadState::kFail; - } - if (iDecodeStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE) - return LoadState::kContinue; - - LoadState iContinueStatus = LoadState::kSuccess; - if (m_bHasMask) { - if (ContinueLoadMaskDIB(pPause) == LoadState::kContinue) { - iContinueStatus = LoadState::kContinue; - m_Status = LoadState::kContinue; - } - } - if (iContinueStatus == LoadState::kContinue) - return LoadState::kContinue; - - if (m_pColorSpace && m_bStdCS) - m_pColorSpace->EnableStdConversion(false); - return iContinueStatus; -} - -bool CPDF_DIBSource::LoadColorInfo(const CPDF_Dictionary* pFormResources, - const CPDF_Dictionary* pPageResources) { - m_bpc_orig = m_pDict->GetIntegerFor("BitsPerComponent"); - if (m_pDict->GetIntegerFor("ImageMask")) - m_bImageMask = true; - - if (m_bImageMask || !m_pDict->KeyExist("ColorSpace")) { - if (!m_bImageMask) { - const CPDF_Object* pFilter = m_pDict->GetDirectObjectFor("Filter"); - if (pFilter) { - ByteString filter; - if (pFilter->IsName()) { - filter = pFilter->GetString(); - } else if (const CPDF_Array* pArray = pFilter->AsArray()) { - filter = pArray->GetStringAt(pArray->GetCount() - 1); - } - - if (filter == "JPXDecode") { - m_bDoBpcCheck = false; - return true; - } - } - } - m_bImageMask = true; - m_bpc = m_nComponents = 1; - const CPDF_Array* pDecode = m_pDict->GetArrayFor("Decode"); - m_bDefaultDecode = !pDecode || !pDecode->GetIntegerAt(0); - return true; - } - - const CPDF_Object* pCSObj = m_pDict->GetDirectObjectFor("ColorSpace"); - if (!pCSObj) - return false; - - CPDF_DocPageData* pDocPageData = m_pDocument->GetPageData(); - if (pFormResources) - m_pColorSpace = pDocPageData->GetColorSpace(pCSObj, pFormResources); - if (!m_pColorSpace) - m_pColorSpace = pDocPageData->GetColorSpace(pCSObj, pPageResources); - if (!m_pColorSpace) - return false; - - m_Family = m_pColorSpace->GetFamily(); - m_nComponents = m_pColorSpace->CountComponents(); - if (m_Family == PDFCS_ICCBASED && pCSObj->IsName()) { - ByteString cs = pCSObj->GetString(); - if (cs == "DeviceGray") - m_nComponents = 1; - else if (cs == "DeviceRGB") - m_nComponents = 3; - else if (cs == "DeviceCMYK") - m_nComponents = 4; - } - ValidateDictParam(); - return GetDecodeAndMaskArray(&m_bDefaultDecode, &m_bColorKey); -} - -bool CPDF_DIBSource::GetDecodeAndMaskArray(bool* bDefaultDecode, - bool* bColorKey) { - if (!m_pColorSpace) - return false; - - m_CompData.resize(m_nComponents); - int max_data = (1 << m_bpc) - 1; - const CPDF_Array* pDecode = m_pDict->GetArrayFor("Decode"); - if (pDecode) { - for (uint32_t i = 0; i < m_nComponents; i++) { - m_CompData[i].m_DecodeMin = pDecode->GetNumberAt(i * 2); - float max = pDecode->GetNumberAt(i * 2 + 1); - m_CompData[i].m_DecodeStep = (max - m_CompData[i].m_DecodeMin) / max_data; - float def_value; - float def_min; - float def_max; - m_pColorSpace->GetDefaultValue(i, &def_value, &def_min, &def_max); - if (m_Family == PDFCS_INDEXED) - def_max = max_data; - if (def_min != m_CompData[i].m_DecodeMin || def_max != max) - *bDefaultDecode = false; - } - } else { - for (uint32_t i = 0; i < m_nComponents; i++) { - float def_value; - m_pColorSpace->GetDefaultValue(i, &def_value, &m_CompData[i].m_DecodeMin, - &m_CompData[i].m_DecodeStep); - if (m_Family == PDFCS_INDEXED) - m_CompData[i].m_DecodeStep = max_data; - m_CompData[i].m_DecodeStep = - (m_CompData[i].m_DecodeStep - m_CompData[i].m_DecodeMin) / max_data; - } - } - if (m_pDict->KeyExist("SMask")) - return true; - - const CPDF_Object* pMask = m_pDict->GetDirectObjectFor("Mask"); - if (!pMask) - return true; - - if (const CPDF_Array* pArray = pMask->AsArray()) { - if (pArray->GetCount() >= m_nComponents * 2) { - for (uint32_t i = 0; i < m_nComponents; i++) { - int min_num = pArray->GetIntegerAt(i * 2); - int max_num = pArray->GetIntegerAt(i * 2 + 1); - m_CompData[i].m_ColorKeyMin = std::max(min_num, 0); - m_CompData[i].m_ColorKeyMax = std::min(max_num, max_data); - } - } - *bColorKey = true; - } - return true; -} - -CPDF_DIBSource::LoadState CPDF_DIBSource::CreateDecoder() { - ByteString decoder = m_pStreamAcc->GetImageDecoder(); - if (decoder.IsEmpty()) - return LoadState::kSuccess; - - if (m_bDoBpcCheck && m_bpc == 0) - return LoadState::kFail; - - if (decoder == "JPXDecode") { - m_pCachedBitmap = LoadJpxBitmap(); - return m_pCachedBitmap ? LoadState::kSuccess : LoadState::kFail; - } - - if (decoder == "JBIG2Decode") { - m_pCachedBitmap = pdfium::MakeRetain(); - if (!m_pCachedBitmap->Create( - m_Width, m_Height, m_bImageMask ? FXDIB_1bppMask : FXDIB_1bppRgb)) { - m_pCachedBitmap.Reset(); - return LoadState::kFail; - } - m_Status = LoadState::kSuccess; - return LoadState::kContinue; - } - - const uint8_t* src_data = m_pStreamAcc->GetData(); - uint32_t src_size = m_pStreamAcc->GetSize(); - const CPDF_Dictionary* pParams = m_pStreamAcc->GetImageParam(); - if (decoder == "CCITTFaxDecode") { - m_pDecoder = FPDFAPI_CreateFaxDecoder(src_data, src_size, m_Width, m_Height, - pParams); - } else if (decoder == "FlateDecode") { - m_pDecoder = FPDFAPI_CreateFlateDecoder( - src_data, src_size, m_Width, m_Height, m_nComponents, m_bpc, pParams); - } else if (decoder == "RunLengthDecode") { - CCodec_ModuleMgr* pEncoders = CPDF_ModuleMgr::Get()->GetCodecModule(); - m_pDecoder = pEncoders->GetBasicModule()->CreateRunLengthDecoder( - src_data, src_size, m_Width, m_Height, m_nComponents, m_bpc); - } else if (decoder == "DCTDecode") { - if (!CreateDCTDecoder(src_data, src_size, pParams)) - return LoadState::kFail; - } - if (!m_pDecoder) - return LoadState::kFail; - - FX_SAFE_UINT32 requested_pitch = - CalculatePitch8(m_bpc, m_nComponents, m_Width); - if (!requested_pitch.IsValid()) - return LoadState::kFail; - FX_SAFE_UINT32 provided_pitch = CalculatePitch8( - m_pDecoder->GetBPC(), m_pDecoder->CountComps(), m_pDecoder->GetWidth()); - if (!provided_pitch.IsValid()) - return LoadState::kFail; - if (provided_pitch.ValueOrDie() < requested_pitch.ValueOrDie()) - return LoadState::kFail; - return LoadState::kSuccess; -} - -bool CPDF_DIBSource::CreateDCTDecoder(const uint8_t* src_data, - uint32_t src_size, - const CPDF_Dictionary* pParams) { - CCodec_JpegModule* pJpegModule = CPDF_ModuleMgr::Get()->GetJpegModule(); - m_pDecoder = pJpegModule->CreateDecoder( - src_data, src_size, m_Width, m_Height, m_nComponents, - !pParams || pParams->GetIntegerFor("ColorTransform", 1)); - if (m_pDecoder) - return true; - - bool bTransform = false; - int comps; - int bpc; - if (!pJpegModule->LoadInfo(src_data, src_size, &m_Width, &m_Height, &comps, - &bpc, &bTransform)) { - return false; - } - - if (m_nComponents == static_cast(comps)) { - m_bpc = bpc; - m_pDecoder = pJpegModule->CreateDecoder( - src_data, src_size, m_Width, m_Height, m_nComponents, bTransform); - return true; - } - - m_nComponents = static_cast(comps); - m_CompData.clear(); - if (m_pColorSpace) { - switch (m_Family) { - case PDFCS_DEVICEGRAY: - case PDFCS_DEVICERGB: - case PDFCS_DEVICECMYK: { - uint32_t dwMinComps = ComponentsForFamily(m_Family); - if (m_pColorSpace->CountComponents() < dwMinComps || - m_nComponents < dwMinComps) { - return false; - } - break; - } - case PDFCS_LAB: { - if (m_nComponents != 3 || m_pColorSpace->CountComponents() < 3) - return false; - break; - } - case PDFCS_ICCBASED: { - if (!IsAllowedICCComponents(m_nComponents) || - !IsAllowedICCComponents(m_pColorSpace->CountComponents()) || - m_pColorSpace->CountComponents() < m_nComponents) { - return false; - } - break; - } - default: { - if (m_pColorSpace->CountComponents() != m_nComponents) - return false; - break; - } - } - } else { - if (m_Family == PDFCS_LAB && m_nComponents != 3) - return false; - } - if (!GetDecodeAndMaskArray(&m_bDefaultDecode, &m_bColorKey)) - return false; - - m_bpc = bpc; - m_pDecoder = pJpegModule->CreateDecoder(src_data, src_size, m_Width, m_Height, - m_nComponents, bTransform); - return true; -} - -RetainPtr CPDF_DIBSource::LoadJpxBitmap() { - CCodec_JpxModule* pJpxModule = CPDF_ModuleMgr::Get()->GetJpxModule(); - auto context = pdfium::MakeUnique(pJpxModule); - context->set_decoder(pJpxModule->CreateDecoder( - m_pStreamAcc->GetData(), m_pStreamAcc->GetSize(), m_pColorSpace.Get())); - if (!context->decoder()) - return nullptr; - - uint32_t width = 0; - uint32_t height = 0; - uint32_t components = 0; - pJpxModule->GetImageInfo(context->decoder(), &width, &height, &components); - if (static_cast(width) < m_Width || static_cast(height) < m_Height) - return nullptr; - - bool bSwapRGB = false; - if (m_pColorSpace) { - if (components != m_pColorSpace->CountComponents()) - return nullptr; - - if (m_pColorSpace == CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB)) { - bSwapRGB = true; - m_pColorSpace = nullptr; - } - } else { - if (components == 3) { - bSwapRGB = true; - } else if (components == 4) { - m_pColorSpace = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICECMYK); - } - m_nComponents = components; - } - - FXDIB_Format format; - if (components == 1) { - format = FXDIB_8bppRgb; - } else if (components <= 3) { - format = FXDIB_Rgb; - } else if (components == 4) { - format = FXDIB_Rgb32; - } else { - width = (width * components + 2) / 3; - format = FXDIB_Rgb; - } - - auto pCachedBitmap = pdfium::MakeRetain(); - if (!pCachedBitmap->Create(width, height, format)) - return nullptr; - - pCachedBitmap->Clear(0xFFFFFFFF); - std::vector output_offsets(components); - for (uint32_t i = 0; i < components; ++i) - output_offsets[i] = i; - if (bSwapRGB) { - output_offsets[0] = 2; - output_offsets[2] = 0; - } - if (!pJpxModule->Decode(context->decoder(), pCachedBitmap->GetBuffer(), - pCachedBitmap->GetPitch(), output_offsets)) { - return nullptr; - } - - if (m_pColorSpace && m_pColorSpace->GetFamily() == PDFCS_INDEXED && - m_bpc < 8) { - int scale = 8 - m_bpc; - for (uint32_t row = 0; row < height; ++row) { - uint8_t* scanline = pCachedBitmap->GetWritableScanline(row); - for (uint32_t col = 0; col < width; ++col) { - *scanline = (*scanline) >> scale; - ++scanline; - } - } - } - m_bpc = 8; - return pCachedBitmap; -} - -CPDF_DIBSource::LoadState CPDF_DIBSource::StartLoadMask() { - m_MatteColor = 0XFFFFFFFF; - m_pMaskStream = m_pDict->GetStreamFor("SMask"); - if (!m_pMaskStream) { - m_pMaskStream = ToStream(m_pDict->GetDirectObjectFor("Mask")); - return m_pMaskStream ? StartLoadMaskDIB() : LoadState::kSuccess; - } - - const CPDF_Array* pMatte = m_pMaskStream->GetDict()->GetArrayFor("Matte"); - if (pMatte && m_pColorSpace && m_Family != PDFCS_PATTERN && - m_pColorSpace->CountComponents() <= m_nComponents) { - std::vector colors(m_nComponents); - for (uint32_t i = 0; i < m_nComponents; i++) - colors[i] = pMatte->GetFloatAt(i); - - float R; - float G; - float B; - m_pColorSpace->GetRGB(colors.data(), &R, &G, &B); - m_MatteColor = ArgbEncode(0, FXSYS_round(R * 255), FXSYS_round(G * 255), - FXSYS_round(B * 255)); - } - return StartLoadMaskDIB(); -} - -CPDF_DIBSource::LoadState CPDF_DIBSource::ContinueLoadMaskDIB( - PauseIndicatorIface* pPause) { - if (!m_pMask) - return LoadState::kSuccess; - - LoadState ret = m_pMask->ContinueLoadDIBSource(pPause); - if (ret == LoadState::kContinue) - return LoadState::kContinue; - - if (m_pColorSpace && m_bStdCS) - m_pColorSpace->EnableStdConversion(false); - - if (ret == LoadState::kFail) { - m_pMask.Reset(); - return LoadState::kFail; - } - return LoadState::kSuccess; -} - -RetainPtr CPDF_DIBSource::DetachMask() { - return std::move(m_pMask); -} - -bool CPDF_DIBSource::IsJBigImage() const { - return m_pStreamAcc->GetImageDecoder() == "JBIG2Decode"; -} - -CPDF_DIBSource::LoadState CPDF_DIBSource::StartLoadMaskDIB() { - m_pMask = pdfium::MakeRetain(); - LoadState ret = - m_pMask->StartLoadDIBSource(m_pDocument.Get(), m_pMaskStream.Get(), false, - nullptr, nullptr, true, 0, false); - if (ret == LoadState::kContinue) { - if (m_Status == LoadState::kFail) - m_Status = LoadState::kContinue; - return LoadState::kContinue; - } - if (ret == LoadState::kFail) - m_pMask.Reset(); - return LoadState::kSuccess; -} - -void CPDF_DIBSource::LoadPalette() { - if (!m_pColorSpace || m_Family == PDFCS_PATTERN) - return; - - if (m_bpc == 0 || m_bpc * m_nComponents > 8) - return; - - if (m_bpc * m_nComponents == 1) { - if (m_bDefaultDecode && - (m_Family == PDFCS_DEVICEGRAY || m_Family == PDFCS_DEVICERGB)) { - return; - } - if (m_pColorSpace->CountComponents() > 3) { - return; - } - float color_values[3]; - color_values[0] = m_CompData[0].m_DecodeMin; - color_values[1] = color_values[0]; - color_values[2] = color_values[0]; - - float R = 0.0f; - float G = 0.0f; - float B = 0.0f; - m_pColorSpace->GetRGB(color_values, &R, &G, &B); - - FX_ARGB argb0 = ArgbEncode(255, FXSYS_round(R * 255), FXSYS_round(G * 255), - FXSYS_round(B * 255)); - color_values[0] += m_CompData[0].m_DecodeStep; - color_values[1] += m_CompData[0].m_DecodeStep; - color_values[2] += m_CompData[0].m_DecodeStep; - m_pColorSpace->GetRGB(color_values, &R, &G, &B); - FX_ARGB argb1 = ArgbEncode(255, FXSYS_round(R * 255), FXSYS_round(G * 255), - FXSYS_round(B * 255)); - if (argb0 != 0xFF000000 || argb1 != 0xFFFFFFFF) { - SetPaletteArgb(0, argb0); - SetPaletteArgb(1, argb1); - } - return; - } - if (m_bpc == 8 && m_bDefaultDecode && - m_pColorSpace == CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY)) { - return; - } - - int palette_count = 1 << (m_bpc * m_nComponents); - // Using at least 16 elements due to the call m_pColorSpace->GetRGB(). - std::vector color_values(std::max(m_nComponents, 16u)); - for (int i = 0; i < palette_count; i++) { - int color_data = i; - for (uint32_t j = 0; j < m_nComponents; j++) { - int encoded_component = color_data % (1 << m_bpc); - color_data /= 1 << m_bpc; - color_values[j] = m_CompData[j].m_DecodeMin + - m_CompData[j].m_DecodeStep * encoded_component; - } - float R = 0; - float G = 0; - float B = 0; - if (m_nComponents == 1 && m_Family == PDFCS_ICCBASED && - m_pColorSpace->CountComponents() > 1) { - int nComponents = m_pColorSpace->CountComponents(); - std::vector temp_buf(nComponents); - for (int k = 0; k < nComponents; ++k) - temp_buf[k] = color_values[0]; - m_pColorSpace->GetRGB(temp_buf.data(), &R, &G, &B); - } else { - m_pColorSpace->GetRGB(color_values.data(), &R, &G, &B); - } - SetPaletteArgb(i, ArgbEncode(255, FXSYS_round(R * 255), - FXSYS_round(G * 255), FXSYS_round(B * 255))); - } -} - -void CPDF_DIBSource::ValidateDictParam() { - m_bpc = m_bpc_orig; - const CPDF_Object* pFilter = m_pDict->GetDirectObjectFor("Filter"); - if (pFilter) { - if (pFilter->IsName()) { - ByteString filter = pFilter->GetString(); - if (filter == "CCITTFaxDecode" || filter == "JBIG2Decode") { - m_bpc = 1; - m_nComponents = 1; - } else if (filter == "RunLengthDecode") { - if (m_bpc != 1) { - m_bpc = 8; - } - } else if (filter == "DCTDecode") { - m_bpc = 8; - } - } else if (const CPDF_Array* pArray = pFilter->AsArray()) { - ByteString filter = pArray->GetStringAt(pArray->GetCount() - 1); - if (filter == "CCITTFaxDecode" || filter == "JBIG2Decode") { - m_bpc = 1; - m_nComponents = 1; - } else if (filter == "DCTDecode") { - // Previously, filter == "RunLengthDecode" was checked in the "if" - // statement as well, but too many documents don't conform to it. - m_bpc = 8; - } - } - } - - if (!IsAllowedBPCValue(m_bpc)) - m_bpc = 0; -} - -void CPDF_DIBSource::TranslateScanline24bpp(uint8_t* dest_scan, - const uint8_t* src_scan) const { - if (m_bpc == 0) - return; - - if (TranslateScanline24bppDefaultDecode(dest_scan, src_scan)) - return; - - // Using at least 16 elements due to the call m_pColorSpace->GetRGB(). - std::vector color_values(std::max(m_nComponents, 16u)); - float R = 0.0f; - float G = 0.0f; - float B = 0.0f; - uint64_t src_bit_pos = 0; - uint64_t src_byte_pos = 0; - size_t dest_byte_pos = 0; - const bool bpp8 = m_bpc == 8; - for (int column = 0; column < m_Width; column++) { - for (uint32_t color = 0; color < m_nComponents; color++) { - if (bpp8) { - uint8_t data = src_scan[src_byte_pos++]; - color_values[color] = m_CompData[color].m_DecodeMin + - m_CompData[color].m_DecodeStep * data; - } else { - unsigned int data = GetBits8(src_scan, src_bit_pos, m_bpc); - color_values[color] = m_CompData[color].m_DecodeMin + - m_CompData[color].m_DecodeStep * data; - src_bit_pos += m_bpc; - } - } - - if (TransMask()) { - float k = 1.0f - color_values[3]; - R = (1.0f - color_values[0]) * k; - G = (1.0f - color_values[1]) * k; - B = (1.0f - color_values[2]) * k; - } else if (m_Family != PDFCS_PATTERN) { - m_pColorSpace->GetRGB(color_values.data(), &R, &G, &B); - } - R = pdfium::clamp(R, 0.0f, 1.0f); - G = pdfium::clamp(G, 0.0f, 1.0f); - B = pdfium::clamp(B, 0.0f, 1.0f); - dest_scan[dest_byte_pos] = static_cast(B * 255); - dest_scan[dest_byte_pos + 1] = static_cast(G * 255); - dest_scan[dest_byte_pos + 2] = static_cast(R * 255); - dest_byte_pos += 3; - } -} - -bool CPDF_DIBSource::TranslateScanline24bppDefaultDecode( - uint8_t* dest_scan, - const uint8_t* src_scan) const { - if (!m_bDefaultDecode) - return false; - - if (m_Family != PDFCS_DEVICERGB && m_Family != PDFCS_CALRGB) { - if (m_bpc != 8) - return false; - - if (m_nComponents == m_pColorSpace->CountComponents()) { - m_pColorSpace->TranslateImageLine(dest_scan, src_scan, m_Width, m_Width, - m_Height, TransMask()); - } - return true; - } - - if (m_nComponents != 3) - return true; - - const uint8_t* src_pos = src_scan; - switch (m_bpc) { - case 8: - for (int column = 0; column < m_Width; column++) { - *dest_scan++ = src_pos[2]; - *dest_scan++ = src_pos[1]; - *dest_scan++ = *src_pos; - src_pos += 3; - } - break; - case 16: - for (int col = 0; col < m_Width; col++) { - *dest_scan++ = src_pos[4]; - *dest_scan++ = src_pos[2]; - *dest_scan++ = *src_pos; - src_pos += 6; - } - break; - default: - const unsigned int max_data = (1 << m_bpc) - 1; - uint64_t src_bit_pos = 0; - size_t dest_byte_pos = 0; - for (int column = 0; column < m_Width; column++) { - unsigned int R = GetBits8(src_scan, src_bit_pos, m_bpc); - src_bit_pos += m_bpc; - unsigned int G = GetBits8(src_scan, src_bit_pos, m_bpc); - src_bit_pos += m_bpc; - unsigned int B = GetBits8(src_scan, src_bit_pos, m_bpc); - src_bit_pos += m_bpc; - R = std::min(R, max_data); - G = std::min(G, max_data); - B = std::min(B, max_data); - dest_scan[dest_byte_pos] = B * 255 / max_data; - dest_scan[dest_byte_pos + 1] = G * 255 / max_data; - dest_scan[dest_byte_pos + 2] = R * 255 / max_data; - dest_byte_pos += 3; - } - break; - } - return true; -} - -uint8_t* CPDF_DIBSource::GetBuffer() const { - return m_pCachedBitmap ? m_pCachedBitmap->GetBuffer() : nullptr; -} - -const uint8_t* CPDF_DIBSource::GetScanline(int line) const { - if (m_bpc == 0) - return nullptr; - - FX_SAFE_UINT32 src_pitch = CalculatePitch8(m_bpc, m_nComponents, m_Width); - if (!src_pitch.IsValid()) - return nullptr; - uint32_t src_pitch_value = src_pitch.ValueOrDie(); - - const uint8_t* pSrcLine = nullptr; - if (m_pCachedBitmap && src_pitch_value <= m_pCachedBitmap->GetPitch()) { - if (line >= m_pCachedBitmap->GetHeight()) { - line = m_pCachedBitmap->GetHeight() - 1; - } - pSrcLine = m_pCachedBitmap->GetScanline(line); - } else if (m_pDecoder) { - pSrcLine = m_pDecoder->GetScanline(line); - } else if (m_pStreamAcc->GetSize() >= (line + 1) * src_pitch_value) { - pSrcLine = m_pStreamAcc->GetData() + line * src_pitch_value; - } - if (!pSrcLine) { - uint8_t* pLineBuf = m_pMaskedLine ? m_pMaskedLine.get() : m_pLineBuf.get(); - memset(pLineBuf, 0xFF, m_Pitch); - return pLineBuf; - } - - if (m_bpc * m_nComponents == 1) { - if (m_bImageMask && m_bDefaultDecode) { - for (uint32_t i = 0; i < src_pitch_value; i++) - m_pLineBuf.get()[i] = ~pSrcLine[i]; - return m_pLineBuf.get(); - } - - if (!m_bColorKey) { - memcpy(m_pLineBuf.get(), pSrcLine, src_pitch_value); - return m_pLineBuf.get(); - } - - uint32_t reset_argb = m_pPalette ? m_pPalette.get()[0] : 0xFF000000; - uint32_t set_argb = m_pPalette ? m_pPalette.get()[1] : 0xFFFFFFFF; - if (m_CompData[0].m_ColorKeyMin == 0) - reset_argb = 0; - if (m_CompData[0].m_ColorKeyMax == 1) - set_argb = 0; - set_argb = FXARGB_TODIB(set_argb); - reset_argb = FXARGB_TODIB(reset_argb); - uint32_t* dest_scan = reinterpret_cast(m_pMaskedLine.get()); - for (int col = 0; col < m_Width; col++) { - *dest_scan = GetBitValue(pSrcLine, col) ? set_argb : reset_argb; - dest_scan++; - } - return m_pMaskedLine.get(); - } - if (m_bpc * m_nComponents <= 8) { - if (m_bpc == 8) { - memcpy(m_pLineBuf.get(), pSrcLine, src_pitch_value); - } else { - uint64_t src_bit_pos = 0; - for (int col = 0; col < m_Width; col++) { - unsigned int color_index = 0; - for (uint32_t color = 0; color < m_nComponents; color++) { - unsigned int data = GetBits8(pSrcLine, src_bit_pos, m_bpc); - color_index |= data << (color * m_bpc); - src_bit_pos += m_bpc; - } - m_pLineBuf.get()[col] = color_index; - } - } - if (!m_bColorKey) - return m_pLineBuf.get(); - - uint8_t* pDestPixel = m_pMaskedLine.get(); - const uint8_t* pSrcPixel = m_pLineBuf.get(); - for (int col = 0; col < m_Width; col++) { - uint8_t index = *pSrcPixel++; - if (m_pPalette) { - *pDestPixel++ = FXARGB_B(m_pPalette.get()[index]); - *pDestPixel++ = FXARGB_G(m_pPalette.get()[index]); - *pDestPixel++ = FXARGB_R(m_pPalette.get()[index]); - } else { - *pDestPixel++ = index; - *pDestPixel++ = index; - *pDestPixel++ = index; - } - *pDestPixel = IsColorIndexOutOfBounds(index, m_CompData[0]) ? 0xFF : 0; - pDestPixel++; - } - return m_pMaskedLine.get(); - } - if (m_bColorKey) { - if (m_nComponents == 3 && m_bpc == 8) { - uint8_t* alpha_channel = m_pMaskedLine.get() + 3; - for (int col = 0; col < m_Width; col++) { - const uint8_t* pPixel = pSrcLine + col * 3; - alpha_channel[col * 4] = - AreColorIndicesOutOfBounds(pPixel, m_CompData.data(), 3) ? 0xFF : 0; - } - } else { - memset(m_pMaskedLine.get(), 0xFF, m_Pitch); - } - } - if (m_pColorSpace) { - TranslateScanline24bpp(m_pLineBuf.get(), pSrcLine); - pSrcLine = m_pLineBuf.get(); - } - if (!m_bColorKey) - return pSrcLine; - - const uint8_t* pSrcPixel = pSrcLine; - uint8_t* pDestPixel = m_pMaskedLine.get(); - for (int col = 0; col < m_Width; col++) { - *pDestPixel++ = *pSrcPixel++; - *pDestPixel++ = *pSrcPixel++; - *pDestPixel++ = *pSrcPixel++; - pDestPixel++; - } - return m_pMaskedLine.get(); -} - -bool CPDF_DIBSource::SkipToScanline(int line, - PauseIndicatorIface* pPause) const { - return m_pDecoder && m_pDecoder->SkipToScanline(line, pPause); -} - -void CPDF_DIBSource::DownSampleScanline(int line, - uint8_t* dest_scan, - int dest_bpp, - int dest_width, - bool bFlipX, - int clip_left, - int clip_width) const { - if (line < 0 || !dest_scan || dest_bpp <= 0 || dest_width <= 0 || - clip_left < 0 || clip_width <= 0) { - return; - } - - uint32_t src_width = m_Width; - FX_SAFE_UINT32 pitch = CalculatePitch8(m_bpc, m_nComponents, m_Width); - if (!pitch.IsValid()) - return; - - const uint8_t* pSrcLine = nullptr; - if (m_pCachedBitmap) { - pSrcLine = m_pCachedBitmap->GetScanline(line); - } else if (m_pDecoder) { - pSrcLine = m_pDecoder->GetScanline(line); - } else { - uint32_t src_pitch = pitch.ValueOrDie(); - pitch *= (line + 1); - if (!pitch.IsValid()) { - return; - } - - if (m_pStreamAcc->GetSize() >= pitch.ValueOrDie()) { - pSrcLine = m_pStreamAcc->GetData() + line * src_pitch; - } - } - int orig_Bpp = m_bpc * m_nComponents / 8; - int dest_Bpp = dest_bpp / 8; - if (!pSrcLine) { - memset(dest_scan, 0xFF, dest_Bpp * clip_width); - return; - } - - FX_SAFE_INT32 max_src_x = clip_left; - max_src_x += clip_width - 1; - max_src_x *= src_width; - max_src_x /= dest_width; - if (!max_src_x.IsValid()) - return; - - if (m_bpc * m_nComponents == 1) { - DownSampleScanline1Bit(orig_Bpp, dest_Bpp, src_width, pSrcLine, dest_scan, - dest_width, bFlipX, clip_left, clip_width); - } else if (m_bpc * m_nComponents <= 8) { - DownSampleScanline8Bit(orig_Bpp, dest_Bpp, src_width, pSrcLine, dest_scan, - dest_width, bFlipX, clip_left, clip_width); - } else { - DownSampleScanline32Bit(orig_Bpp, dest_Bpp, src_width, pSrcLine, dest_scan, - dest_width, bFlipX, clip_left, clip_width); - } -} - -void CPDF_DIBSource::DownSampleScanline1Bit(int orig_Bpp, - int dest_Bpp, - uint32_t src_width, - const uint8_t* pSrcLine, - uint8_t* dest_scan, - int dest_width, - bool bFlipX, - int clip_left, - int clip_width) const { - if (m_bColorKey && !m_bImageMask) { - uint32_t reset_argb = m_pPalette ? m_pPalette.get()[0] : 0xFF000000; - uint32_t set_argb = m_pPalette ? m_pPalette.get()[1] : 0xFFFFFFFF; - if (m_CompData[0].m_ColorKeyMin == 0) - reset_argb = 0; - if (m_CompData[0].m_ColorKeyMax == 1) - set_argb = 0; - set_argb = FXARGB_TODIB(set_argb); - reset_argb = FXARGB_TODIB(reset_argb); - uint32_t* dest_scan_dword = reinterpret_cast(dest_scan); - for (int i = 0; i < clip_width; i++) { - uint32_t src_x = (clip_left + i) * src_width / dest_width; - if (bFlipX) - src_x = src_width - src_x - 1; - src_x %= src_width; - dest_scan_dword[i] = GetBitValue(pSrcLine, src_x) ? set_argb : reset_argb; - } - return; - } - - uint32_t set_argb = 0xFFFFFFFF; - uint32_t reset_argb = 0; - if (m_bImageMask) { - if (m_bDefaultDecode) { - set_argb = 0; - reset_argb = 0xFFFFFFFF; - } - } else if (m_pPalette && dest_Bpp != 1) { - reset_argb = m_pPalette.get()[0]; - set_argb = m_pPalette.get()[1]; - } - for (int i = 0; i < clip_width; i++) { - uint32_t src_x = (clip_left + i) * src_width / dest_width; - if (bFlipX) - src_x = src_width - src_x - 1; - src_x %= src_width; - int dest_pos = i * dest_Bpp; - uint32_t value_argb = GetBitValue(pSrcLine, src_x) ? set_argb : reset_argb; - if (dest_Bpp == 1) { - dest_scan[dest_pos] = static_cast(value_argb); - } else if (dest_Bpp == 3) { - dest_scan[dest_pos] = FXARGB_B(value_argb); - dest_scan[dest_pos + 1] = FXARGB_G(value_argb); - dest_scan[dest_pos + 2] = FXARGB_R(value_argb); - } else { - *reinterpret_cast(dest_scan + dest_pos) = value_argb; - } - } -} - -void CPDF_DIBSource::DownSampleScanline8Bit(int orig_Bpp, - int dest_Bpp, - uint32_t src_width, - const uint8_t* pSrcLine, - uint8_t* dest_scan, - int dest_width, - bool bFlipX, - int clip_left, - int clip_width) const { - if (m_bpc < 8) { - uint64_t src_bit_pos = 0; - for (uint32_t col = 0; col < src_width; col++) { - unsigned int color_index = 0; - for (uint32_t color = 0; color < m_nComponents; color++) { - unsigned int data = GetBits8(pSrcLine, src_bit_pos, m_bpc); - color_index |= data << (color * m_bpc); - src_bit_pos += m_bpc; - } - m_pLineBuf.get()[col] = color_index; - } - pSrcLine = m_pLineBuf.get(); - } - if (m_bColorKey) { - for (int i = 0; i < clip_width; i++) { - uint32_t src_x = (clip_left + i) * src_width / dest_width; - if (bFlipX) { - src_x = src_width - src_x - 1; - } - src_x %= src_width; - uint8_t* pDestPixel = dest_scan + i * 4; - uint8_t index = pSrcLine[src_x]; - if (m_pPalette) { - *pDestPixel++ = FXARGB_B(m_pPalette.get()[index]); - *pDestPixel++ = FXARGB_G(m_pPalette.get()[index]); - *pDestPixel++ = FXARGB_R(m_pPalette.get()[index]); - } else { - *pDestPixel++ = index; - *pDestPixel++ = index; - *pDestPixel++ = index; - } - *pDestPixel = (index < m_CompData[0].m_ColorKeyMin || - index > m_CompData[0].m_ColorKeyMax) - ? 0xFF - : 0; - } - return; - } - for (int i = 0; i < clip_width; i++) { - uint32_t src_x = (clip_left + i) * src_width / dest_width; - if (bFlipX) - src_x = src_width - src_x - 1; - src_x %= src_width; - uint8_t index = pSrcLine[src_x]; - if (dest_Bpp == 1) { - dest_scan[i] = index; - } else { - int dest_pos = i * dest_Bpp; - FX_ARGB argb = m_pPalette.get()[index]; - dest_scan[dest_pos] = FXARGB_B(argb); - dest_scan[dest_pos + 1] = FXARGB_G(argb); - dest_scan[dest_pos + 2] = FXARGB_R(argb); - } - } -} - -void CPDF_DIBSource::DownSampleScanline32Bit(int orig_Bpp, - int dest_Bpp, - uint32_t src_width, - const uint8_t* pSrcLine, - uint8_t* dest_scan, - int dest_width, - bool bFlipX, - int clip_left, - int clip_width) const { - // last_src_x used to store the last seen src_x position which should be - // in [0, src_width). Set the initial value to be an invalid src_x value. - uint32_t last_src_x = src_width; - FX_ARGB last_argb = ArgbEncode(0xFF, 0xFF, 0xFF, 0xFF); - float unit_To8Bpc = 255.0f / ((1 << m_bpc) - 1); - for (int i = 0; i < clip_width; i++) { - int dest_x = clip_left + i; - uint32_t src_x = (bFlipX ? (dest_width - dest_x - 1) : dest_x) * - (int64_t)src_width / dest_width; - src_x %= src_width; - - uint8_t* pDestPixel = dest_scan + i * dest_Bpp; - FX_ARGB argb; - if (src_x == last_src_x) { - argb = last_argb; - } else { - CFX_FixedBufGrow extracted_components(m_nComponents); - const uint8_t* pSrcPixel = nullptr; - if (m_bpc % 8 != 0) { - // No need to check for 32-bit overflow, as |src_x| is bounded by - // |src_width| and DownSampleScanline() already checked for overflow - // with the pitch calculation. - size_t num_bits = src_x * m_bpc * m_nComponents; - uint64_t src_bit_pos = num_bits % 8; - pSrcPixel = pSrcLine + num_bits / 8; - for (uint32_t j = 0; j < m_nComponents; ++j) { - extracted_components[j] = static_cast( - GetBits8(pSrcPixel, src_bit_pos, m_bpc) * unit_To8Bpc); - src_bit_pos += m_bpc; - } - pSrcPixel = extracted_components; - } else { - pSrcPixel = pSrcLine + src_x * orig_Bpp; - if (m_bpc == 16) { - for (uint32_t j = 0; j < m_nComponents; ++j) - extracted_components[j] = pSrcPixel[j * 2]; - pSrcPixel = extracted_components; - } - } - - if (m_pColorSpace) { - uint8_t color[4]; - const bool bTransMask = TransMask(); - if (!m_bDefaultDecode) { - for (uint32_t j = 0; j < m_nComponents; ++j) { - float component_value = static_cast(pSrcPixel[j]); - int color_value = static_cast( - (m_CompData[j].m_DecodeMin + - m_CompData[j].m_DecodeStep * component_value) * - 255.0f + - 0.5f); - extracted_components[j] = pdfium::clamp(color_value, 0, 255); - } - } - const uint8_t* pSrc = - m_bDefaultDecode ? pSrcPixel : extracted_components; - m_pColorSpace->TranslateImageLine(color, pSrc, 1, 0, 0, bTransMask); - argb = ArgbEncode(0xFF, color[2], color[1], color[0]); - } else { - argb = ArgbEncode(0xFF, pSrcPixel[2], pSrcPixel[1], pSrcPixel[0]); - } - if (m_bColorKey) { - int alpha = 0xFF; - if (m_nComponents == 3 && m_bpc == 8) { - alpha = (pSrcPixel[0] < m_CompData[0].m_ColorKeyMin || - pSrcPixel[0] > m_CompData[0].m_ColorKeyMax || - pSrcPixel[1] < m_CompData[1].m_ColorKeyMin || - pSrcPixel[1] > m_CompData[1].m_ColorKeyMax || - pSrcPixel[2] < m_CompData[2].m_ColorKeyMin || - pSrcPixel[2] > m_CompData[2].m_ColorKeyMax) - ? 0xFF - : 0; - } - argb &= 0xFFFFFF; - argb |= alpha << 24; - } - last_src_x = src_x; - last_argb = argb; - } - if (dest_Bpp == 4) { - *reinterpret_cast(pDestPixel) = FXARGB_TODIB(argb); - } else { - *pDestPixel++ = FXARGB_B(argb); - *pDestPixel++ = FXARGB_G(argb); - *pDestPixel = FXARGB_R(argb); - } - } -} - -bool CPDF_DIBSource::TransMask() const { - return m_bLoadMask && m_GroupFamily == PDFCS_DEVICECMYK && - m_Family == PDFCS_DEVICECMYK; -} diff --git a/core/fpdfapi/render/cpdf_dibsource.h b/core/fpdfapi/render/cpdf_dibsource.h deleted file mode 100644 index 597390cd18..0000000000 --- a/core/fpdfapi/render/cpdf_dibsource.h +++ /dev/null @@ -1,163 +0,0 @@ -// Copyright 2017 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef CORE_FPDFAPI_RENDER_CPDF_DIBSOURCE_H_ -#define CORE_FPDFAPI_RENDER_CPDF_DIBSOURCE_H_ - -#include -#include -#include - -#include "core/fpdfapi/page/cpdf_clippath.h" -#include "core/fpdfapi/page/cpdf_countedobject.h" -#include "core/fpdfapi/page/cpdf_graphicstates.h" -#include "core/fpdfapi/parser/cpdf_stream_acc.h" -#include "core/fpdfapi/render/cpdf_imageloader.h" -#include "core/fpdfapi/render/cpdf_rendercontext.h" -#include "core/fpdfapi/render/cpdf_renderoptions.h" -#include "core/fxcrt/retain_ptr.h" -#include "core/fxcrt/unowned_ptr.h" -#include "core/fxge/cfx_defaultrenderdevice.h" -#include "core/fxge/dib/cfx_dibsource.h" - -class CCodec_Jbig2Context; -class CCodec_ScanlineDecoder; -class CPDF_Color; -class CPDF_Dictionary; -class CPDF_Document; -class CPDF_Stream; - -struct DIB_COMP_DATA { - float m_DecodeMin; - float m_DecodeStep; - int m_ColorKeyMin; - int m_ColorKeyMax; -}; - -#define FPDF_HUGE_IMAGE_SIZE 60000000 - -class CPDF_DIBSource : public CFX_DIBSource { - public: - enum class LoadState : uint8_t { kFail, kSuccess, kContinue }; - - template - friend RetainPtr pdfium::MakeRetain(Args&&... args); - - bool Load(CPDF_Document* pDoc, const CPDF_Stream* pStream); - - // CFX_DIBSource - bool SkipToScanline(int line, PauseIndicatorIface* pPause) const override; - uint8_t* GetBuffer() const override; - const uint8_t* GetScanline(int line) const override; - void DownSampleScanline(int line, - uint8_t* dest_scan, - int dest_bpp, - int dest_width, - bool bFlipX, - int clip_left, - int clip_width) const override; - - const CPDF_ColorSpace* GetColorSpace() const { return m_pColorSpace.Get(); } - uint32_t GetMatteColor() const { return m_MatteColor; } - - LoadState StartLoadDIBSource(CPDF_Document* pDoc, - const CPDF_Stream* pStream, - bool bHasMask, - const CPDF_Dictionary* pFormResources, - CPDF_Dictionary* pPageResources, - bool bStdCS, - uint32_t GroupFamily, - bool bLoadMask); - LoadState ContinueLoadDIBSource(PauseIndicatorIface* pPause); - RetainPtr DetachMask(); - - bool IsJBigImage() const; - - private: - CPDF_DIBSource(); - ~CPDF_DIBSource() override; - - LoadState StartLoadMask(); - LoadState StartLoadMaskDIB(); - bool ContinueToLoadMask(); - LoadState ContinueLoadMaskDIB(PauseIndicatorIface* pPause); - bool LoadColorInfo(const CPDF_Dictionary* pFormResources, - const CPDF_Dictionary* pPageResources); - bool GetDecodeAndMaskArray(bool* bDefaultDecode, bool* bColorKey); - RetainPtr LoadJpxBitmap(); - void LoadPalette(); - LoadState CreateDecoder(); - bool CreateDCTDecoder(const uint8_t* src_data, - uint32_t src_size, - const CPDF_Dictionary* pParams); - void TranslateScanline24bpp(uint8_t* dest_scan, - const uint8_t* src_scan) const; - bool TranslateScanline24bppDefaultDecode(uint8_t* dest_scan, - const uint8_t* src_scan) const; - void ValidateDictParam(); - void DownSampleScanline1Bit(int orig_Bpp, - int dest_Bpp, - uint32_t src_width, - const uint8_t* pSrcLine, - uint8_t* dest_scan, - int dest_width, - bool bFlipX, - int clip_left, - int clip_width) const; - void DownSampleScanline8Bit(int orig_Bpp, - int dest_Bpp, - uint32_t src_width, - const uint8_t* pSrcLine, - uint8_t* dest_scan, - int dest_width, - bool bFlipX, - int clip_left, - int clip_width) const; - void DownSampleScanline32Bit(int orig_Bpp, - int dest_Bpp, - uint32_t src_width, - const uint8_t* pSrcLine, - uint8_t* dest_scan, - int dest_width, - bool bFlipX, - int clip_left, - int clip_width) const; - bool TransMask() const; - - UnownedPtr m_pDocument; - UnownedPtr m_pStream; - UnownedPtr m_pDict; - RetainPtr m_pStreamAcc; - UnownedPtr m_pColorSpace; - uint32_t m_Family = 0; - uint32_t m_bpc = 0; - uint32_t m_bpc_orig = 0; - uint32_t m_nComponents = 0; - uint32_t m_GroupFamily = 0; - uint32_t m_MatteColor = 0; - bool m_bLoadMask = false; - bool m_bDefaultDecode = true; - bool m_bImageMask = false; - bool m_bDoBpcCheck = true; - bool m_bColorKey = false; - bool m_bHasMask = false; - bool m_bStdCS = false; - std::vector m_CompData; - std::unique_ptr m_pLineBuf; - std::unique_ptr m_pMaskedLine; - RetainPtr m_pCachedBitmap; - RetainPtr m_pMask; - RetainPtr m_pGlobalStream; - std::unique_ptr m_pDecoder; - - // Must come after |m_pCachedBitmap|. - std::unique_ptr m_pJbig2Context; - - UnownedPtr m_pMaskStream; - LoadState m_Status = LoadState::kFail; -}; - -#endif // CORE_FPDFAPI_RENDER_CPDF_DIBSOURCE_H_ diff --git a/core/fpdfapi/render/cpdf_docrenderdata.cpp b/core/fpdfapi/render/cpdf_docrenderdata.cpp index 568bf76e97..c86eb9b147 100644 --- a/core/fpdfapi/render/cpdf_docrenderdata.cpp +++ b/core/fpdfapi/render/cpdf_docrenderdata.cpp @@ -12,7 +12,7 @@ #include "core/fpdfapi/page/cpdf_function.h" #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_document.h" -#include "core/fpdfapi/render/cpdf_dibsource.h" +#include "core/fpdfapi/render/cpdf_dibbase.h" #include "core/fpdfapi/render/cpdf_transferfunc.h" #include "core/fpdfapi/render/cpdf_type3cache.h" diff --git a/core/fpdfapi/render/cpdf_imagecacheentry.cpp b/core/fpdfapi/render/cpdf_imagecacheentry.cpp index 615a44d869..681c39e624 100644 --- a/core/fpdfapi/render/cpdf_imagecacheentry.cpp +++ b/core/fpdfapi/render/cpdf_imagecacheentry.cpp @@ -13,7 +13,7 @@ #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_document.h" #include "core/fpdfapi/parser/cpdf_stream.h" -#include "core/fpdfapi/render/cpdf_dibsource.h" +#include "core/fpdfapi/render/cpdf_dibbase.h" #include "core/fpdfapi/render/cpdf_pagerendercache.h" #include "core/fpdfapi/render/cpdf_rendercontext.h" #include "core/fpdfapi/render/cpdf_renderstatus.h" @@ -37,22 +37,22 @@ void CPDF_ImageCacheEntry::Reset(const RetainPtr& pBitmap) { } static uint32_t FPDF_ImageCache_EstimateImageSize( - const RetainPtr& pDIB) { + const RetainPtr& pDIB) { return pDIB && pDIB->GetBuffer() ? (uint32_t)pDIB->GetHeight() * pDIB->GetPitch() + (uint32_t)pDIB->GetPaletteSize() * 4 : 0; } -RetainPtr CPDF_ImageCacheEntry::DetachBitmap() { +RetainPtr CPDF_ImageCacheEntry::DetachBitmap() { return std::move(m_pCurBitmap); } -RetainPtr CPDF_ImageCacheEntry::DetachMask() { +RetainPtr CPDF_ImageCacheEntry::DetachMask() { return std::move(m_pCurMask); } -CPDF_DIBSource::LoadState CPDF_ImageCacheEntry::StartGetCachedBitmap( +CPDF_DIBBase::LoadState CPDF_ImageCacheEntry::StartGetCachedBitmap( const CPDF_Dictionary* pFormResources, CPDF_Dictionary* pPageResources, bool bStdCS, @@ -64,32 +64,32 @@ CPDF_DIBSource::LoadState CPDF_ImageCacheEntry::StartGetCachedBitmap( if (m_pCachedBitmap) { m_pCurBitmap = m_pCachedBitmap; m_pCurMask = m_pCachedMask; - return CPDF_DIBSource::LoadState::kSuccess; + return CPDF_DIBBase::LoadState::kSuccess; } - m_pCurBitmap = pdfium::MakeRetain(); - CPDF_DIBSource::LoadState ret = - m_pCurBitmap.As()->StartLoadDIBSource( + m_pCurBitmap = pdfium::MakeRetain(); + CPDF_DIBBase::LoadState ret = + m_pCurBitmap.As()->StartLoadDIBBase( m_pDocument.Get(), m_pImage->GetStream(), true, pFormResources, pPageResources, bStdCS, GroupFamily, bLoadMask); - if (ret == CPDF_DIBSource::LoadState::kContinue) - return CPDF_DIBSource::LoadState::kContinue; + if (ret == CPDF_DIBBase::LoadState::kContinue) + return CPDF_DIBBase::LoadState::kContinue; - if (ret == CPDF_DIBSource::LoadState::kSuccess) + if (ret == CPDF_DIBBase::LoadState::kSuccess) ContinueGetCachedBitmap(pRenderStatus); else m_pCurBitmap.Reset(); - return CPDF_DIBSource::LoadState::kFail; + return CPDF_DIBBase::LoadState::kFail; } bool CPDF_ImageCacheEntry::Continue(PauseIndicatorIface* pPause, CPDF_RenderStatus* pRenderStatus) { - CPDF_DIBSource::LoadState ret = - m_pCurBitmap.As()->ContinueLoadDIBSource(pPause); - if (ret == CPDF_DIBSource::LoadState::kContinue) + CPDF_DIBBase::LoadState ret = + m_pCurBitmap.As()->ContinueLoadDIBBase(pPause); + if (ret == CPDF_DIBBase::LoadState::kContinue) return true; - if (ret == CPDF_DIBSource::LoadState::kSuccess) + if (ret == CPDF_DIBBase::LoadState::kSuccess) ContinueGetCachedBitmap(pRenderStatus); else m_pCurBitmap.Reset(); @@ -98,8 +98,8 @@ bool CPDF_ImageCacheEntry::Continue(PauseIndicatorIface* pPause, void CPDF_ImageCacheEntry::ContinueGetCachedBitmap( CPDF_RenderStatus* pRenderStatus) { - m_MatteColor = m_pCurBitmap.As()->GetMatteColor(); - m_pCurMask = m_pCurBitmap.As()->DetachMask(); + m_MatteColor = m_pCurBitmap.As()->GetMatteColor(); + m_pCurMask = m_pCurBitmap.As()->DetachMask(); CPDF_RenderContext* pContext = pRenderStatus->GetContext(); CPDF_PageRenderCache* pPageRenderCache = pContext->GetPageCache(); m_dwTimeCount = pPageRenderCache->GetTimeCount(); diff --git a/core/fpdfapi/render/cpdf_imagecacheentry.h b/core/fpdfapi/render/cpdf_imagecacheentry.h index df13c4b3f8..8a4f919e8d 100644 --- a/core/fpdfapi/render/cpdf_imagecacheentry.h +++ b/core/fpdfapi/render/cpdf_imagecacheentry.h @@ -9,7 +9,7 @@ #include -#include "core/fpdfapi/render/cpdf_dibsource.h" +#include "core/fpdfapi/render/cpdf_dibbase.h" #include "core/fxcrt/fx_system.h" #include "core/fxcrt/retain_ptr.h" #include "core/fxcrt/unowned_ptr.h" @@ -32,7 +32,7 @@ class CPDF_ImageCacheEntry { uint32_t GetTimeCount() const { return m_dwTimeCount; } CPDF_Image* GetImage() const { return m_pImage.Get(); } - CPDF_DIBSource::LoadState StartGetCachedBitmap( + CPDF_DIBBase::LoadState StartGetCachedBitmap( const CPDF_Dictionary* pFormResources, CPDF_Dictionary* pPageResources, bool bStdCS, @@ -43,8 +43,8 @@ class CPDF_ImageCacheEntry { // Returns whether to Continue() or not. bool Continue(PauseIndicatorIface* pPause, CPDF_RenderStatus* pRenderStatus); - RetainPtr DetachBitmap(); - RetainPtr DetachMask(); + RetainPtr DetachBitmap(); + RetainPtr DetachMask(); int m_dwTimeCount; uint32_t m_MatteColor; @@ -55,10 +55,10 @@ class CPDF_ImageCacheEntry { UnownedPtr const m_pDocument; RetainPtr const m_pImage; - RetainPtr m_pCurBitmap; - RetainPtr m_pCurMask; - RetainPtr m_pCachedBitmap; - RetainPtr m_pCachedMask; + RetainPtr m_pCurBitmap; + RetainPtr m_pCurMask; + RetainPtr m_pCachedBitmap; + RetainPtr m_pCachedMask; uint32_t m_dwCacheSize; }; diff --git a/core/fpdfapi/render/cpdf_imageloader.cpp b/core/fpdfapi/render/cpdf_imageloader.cpp index f3a626350b..157bc48d1b 100644 --- a/core/fpdfapi/render/cpdf_imageloader.cpp +++ b/core/fpdfapi/render/cpdf_imageloader.cpp @@ -8,7 +8,7 @@ #include "core/fpdfapi/page/cpdf_image.h" #include "core/fpdfapi/page/cpdf_imageobject.h" -#include "core/fpdfapi/render/cpdf_dibsource.h" +#include "core/fpdfapi/render/cpdf_dibbase.h" #include "core/fpdfapi/render/cpdf_imagecacheentry.h" #include "core/fpdfapi/render/cpdf_pagerendercache.h" #include "core/fpdfapi/render/cpdf_renderstatus.h" @@ -34,7 +34,7 @@ bool CPDF_ImageLoader::Start(CPDF_ImageObject* pImage, ret = pCache->StartGetCachedBitmap(m_pImageObject->GetImage(), bStdCS, GroupFamily, bLoadMask, pRenderStatus); } else { - ret = m_pImageObject->GetImage()->StartLoadDIBSource( + ret = m_pImageObject->GetImage()->StartLoadDIBBase( pRenderStatus->GetFormResource(), pRenderStatus->GetPageResource(), bStdCS, GroupFamily, bLoadMask); } diff --git a/core/fpdfapi/render/cpdf_imageloader.h b/core/fpdfapi/render/cpdf_imageloader.h index 629609df48..de29d0e2a9 100644 --- a/core/fpdfapi/render/cpdf_imageloader.h +++ b/core/fpdfapi/render/cpdf_imageloader.h @@ -31,8 +31,8 @@ class CPDF_ImageLoader { CPDF_RenderStatus* pRenderStatus); bool Continue(PauseIndicatorIface* pPause, CPDF_RenderStatus* pRenderStatus); - RetainPtr m_pBitmap; - RetainPtr m_pMask; + RetainPtr m_pBitmap; + RetainPtr m_pMask; uint32_t m_MatteColor; bool m_bCached; diff --git a/core/fpdfapi/render/cpdf_imagerenderer.cpp b/core/fpdfapi/render/cpdf_imagerenderer.cpp index 9f14e8470c..33aca42ee1 100644 --- a/core/fpdfapi/render/cpdf_imagerenderer.cpp +++ b/core/fpdfapi/render/cpdf_imagerenderer.cpp @@ -19,7 +19,7 @@ #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_document.h" -#include "core/fpdfapi/render/cpdf_dibsource.h" +#include "core/fpdfapi/render/cpdf_dibbase.h" #include "core/fpdfapi/render/cpdf_pagerendercache.h" #include "core/fpdfapi/render/cpdf_rendercontext.h" #include "core/fpdfapi/render/cpdf_renderstatus.h" @@ -29,8 +29,8 @@ #include "core/fxcrt/maybe_owned.h" #include "core/fxge/cfx_defaultrenderdevice.h" #include "core/fxge/cfx_pathdata.h" +#include "core/fxge/dib/cfx_dibbase.h" #include "core/fxge/dib/cfx_dibitmap.h" -#include "core/fxge/dib/cfx_dibsource.h" #include "core/fxge/dib/cfx_imagestretcher.h" #include "core/fxge/dib/cfx_imagetransformer.h" #include "third_party/base/ptr_util.h" @@ -53,7 +53,7 @@ CPDF_ImageRenderer::CPDF_ImageRenderer() CPDF_ImageRenderer::~CPDF_ImageRenderer() {} -bool CPDF_ImageRenderer::StartLoadDIBSource() { +bool CPDF_ImageRenderer::StartLoadDIBBase() { CFX_FloatRect image_rect_f = m_ImageMatrix.GetUnitRect(); FX_RECT image_rect = image_rect_f.GetOuterRect(); if (!image_rect.Valid()) @@ -69,13 +69,13 @@ bool CPDF_ImageRenderer::StartLoadDIBSource() { return false; } -bool CPDF_ImageRenderer::StartRenderDIBSource() { +bool CPDF_ImageRenderer::StartRenderDIBBase() { if (!m_Loader.m_pBitmap) return false; CPDF_GeneralState& state = m_pImageObject->m_GeneralState; m_BitmapAlpha = FXSYS_round(255 * state.GetFillAlpha()); - m_pDIBSource = m_Loader.m_pBitmap; + m_pDIBBase = m_Loader.m_pBitmap; if (m_pRenderStatus->GetRenderOptions().ColorModeIs( CPDF_RenderOptions::kAlpha) && !m_Loader.m_pMask) { @@ -86,7 +86,7 @@ bool CPDF_ImageRenderer::StartRenderDIBSource() { state.SetTransferFunc(m_pRenderStatus->GetTransferFunc(state.GetTR())); if (state.GetTransferFunc() && !state.GetTransferFunc()->GetIdentity()) { - m_pDIBSource = m_Loader.m_pBitmap = + m_pDIBBase = m_Loader.m_pBitmap = state.GetTransferFunc()->TranslateImage(m_Loader.m_pBitmap); if (m_Loader.m_bCached && m_Loader.m_pMask) m_Loader.m_pMask = m_Loader.m_pMask->Clone(nullptr); @@ -96,7 +96,7 @@ bool CPDF_ImageRenderer::StartRenderDIBSource() { m_FillArgb = 0; m_bPatternColor = false; m_pPattern = nullptr; - if (m_pDIBSource->IsAlphaMask()) { + if (m_pDIBBase->IsAlphaMask()) { const CPDF_Color* pColor = m_pImageObject->m_ColorState.GetFillColor(); if (pColor && pColor->IsPattern()) { m_pPattern = pColor->GetPattern(); @@ -106,12 +106,12 @@ bool CPDF_ImageRenderer::StartRenderDIBSource() { m_FillArgb = m_pRenderStatus->GetFillArgb(m_pImageObject.Get()); } else if (m_pRenderStatus->GetRenderOptions().ColorModeIs( CPDF_RenderOptions::kGray)) { - RetainPtr pClone = m_pDIBSource->Clone(nullptr); + RetainPtr pClone = m_pDIBBase->Clone(nullptr); if (!pClone) return false; pClone->ConvertColorScale(0xffffff, 0); - m_pDIBSource = pClone; + m_pDIBBase = pClone; } m_Flags = 0; if (m_pRenderStatus->GetRenderOptions().HasFlag(RENDER_FORCE_DOWNSAMPLE)) @@ -136,7 +136,7 @@ bool CPDF_ImageRenderer::StartRenderDIBSource() { if (m_BitmapAlpha != 255 || !state.HasRef() || !state.GetFillOP() || state.GetOPMode() != 0 || state.GetBlendType() != FXDIB_BLEND_NORMAL || state.GetStrokeAlpha() != 1.0f || state.GetFillAlpha() != 1.0f) { - return StartDIBSource(); + return StartDIBBase(); } CPDF_Document* pDocument = nullptr; CPDF_Page* pPage = nullptr; @@ -154,14 +154,14 @@ bool CPDF_ImageRenderer::StartRenderDIBSource() { CPDF_ColorSpace* pColorSpace = pDocument->LoadColorSpace(pCSObj, pPageResources); if (!pColorSpace) - return StartDIBSource(); + return StartDIBBase(); int format = pColorSpace->GetFamily(); if (format == PDFCS_DEVICECMYK || format == PDFCS_SEPARATION || format == PDFCS_DEVICEN) { m_BlendType = FXDIB_BLEND_DARKEN; } pDocument->GetPageData()->ReleaseColorSpace(pCSObj); - return StartDIBSource(); + return StartDIBBase(); } bool CPDF_ImageRenderer::Start(CPDF_RenderStatus* pStatus, @@ -183,13 +183,13 @@ bool CPDF_ImageRenderer::Start(CPDF_RenderStatus* pStatus, } m_ImageMatrix = m_pImageObject->matrix(); m_ImageMatrix.Concat(*pObj2Device); - if (StartLoadDIBSource()) + if (StartLoadDIBBase()) return true; - return StartRenderDIBSource(); + return StartRenderDIBBase(); } bool CPDF_ImageRenderer::Start(CPDF_RenderStatus* pStatus, - const RetainPtr& pDIBSource, + const RetainPtr& pDIBBase, FX_ARGB bitmap_argb, int bitmap_alpha, const CFX_Matrix* pImage2Device, @@ -197,14 +197,14 @@ bool CPDF_ImageRenderer::Start(CPDF_RenderStatus* pStatus, bool bStdCS, int blendType) { m_pRenderStatus = pStatus; - m_pDIBSource = pDIBSource; + m_pDIBBase = pDIBBase; m_FillArgb = bitmap_argb; m_BitmapAlpha = bitmap_alpha; m_ImageMatrix = *pImage2Device; m_Flags = flags; m_bStdCS = bStdCS; m_BlendType = blendType; - return StartDIBSource(); + return StartDIBBase(); } bool CPDF_ImageRenderer::NotDrawing() const { @@ -228,7 +228,7 @@ CFX_Matrix CPDF_ImageRenderer::GetDrawMatrix(const FX_RECT& rect) const { void CPDF_ImageRenderer::CalculateDrawImage( CFX_DefaultRenderDevice* pBitmapDevice1, CFX_DefaultRenderDevice* pBitmapDevice2, - const RetainPtr& pDIBSource, + const RetainPtr& pDIBBase, CFX_Matrix* pNewMatrix, const FX_RECT& rect) const { CPDF_RenderStatus bitmap_render(m_pRenderStatus->GetContext(), @@ -238,8 +238,8 @@ void CPDF_ImageRenderer::CalculateDrawImage( bitmap_render.Initialize(nullptr, nullptr); CPDF_ImageRenderer image_render; - if (image_render.Start(&bitmap_render, pDIBSource, 0xffffffff, 255, - pNewMatrix, m_Flags, true, FXDIB_BLEND_NORMAL)) { + if (image_render.Start(&bitmap_render, pDIBBase, 0xffffffff, 255, pNewMatrix, + m_Flags, true, FXDIB_BLEND_NORMAL)) { image_render.Continue(nullptr); } if (m_Loader.m_MatteColor == 0xffffffff) @@ -309,8 +309,8 @@ bool CPDF_ImageRenderer::DrawPatternImage(const CFX_Matrix* pObj2Device) { return true; } bitmap_device2.GetBitmap()->Clear(0); - CalculateDrawImage(&bitmap_device1, &bitmap_device2, m_pDIBSource, - &new_matrix, rect); + CalculateDrawImage(&bitmap_device1, &bitmap_device2, m_pDIBBase, &new_matrix, + rect); bitmap_device2.GetBitmap()->ConvertFormat(FXDIB_8bppMask); bitmap_device1.GetBitmap()->MultiplyAlpha(bitmap_device2.GetBitmap()); bitmap_device1.GetBitmap()->MultiplyAlpha(255); @@ -345,7 +345,7 @@ bool CPDF_ImageRenderer::DrawMaskedImage() { bitmap_render.SetStdCS(true); bitmap_render.Initialize(nullptr, nullptr); CPDF_ImageRenderer image_render; - if (image_render.Start(&bitmap_render, m_pDIBSource, 0, 255, &new_matrix, + if (image_render.Start(&bitmap_render, m_pDIBBase, 0, 255, &new_matrix, m_Flags, true, FXDIB_BLEND_NORMAL)) { image_render.Continue(nullptr); } @@ -376,12 +376,12 @@ bool CPDF_ImageRenderer::DrawMaskedImage() { return false; } -bool CPDF_ImageRenderer::StartDIBSource() { - if (!(m_Flags & RENDER_FORCE_DOWNSAMPLE) && m_pDIBSource->GetBPP() > 1) { - FX_SAFE_SIZE_T image_size = m_pDIBSource->GetBPP(); +bool CPDF_ImageRenderer::StartDIBBase() { + if (!(m_Flags & RENDER_FORCE_DOWNSAMPLE) && m_pDIBBase->GetBPP() > 1) { + FX_SAFE_SIZE_T image_size = m_pDIBBase->GetBPP(); image_size /= 8; - image_size *= m_pDIBSource->GetWidth(); - image_size *= m_pDIBSource->GetHeight(); + image_size *= m_pDIBBase->GetWidth(); + image_size *= m_pDIBBase->GetHeight(); if (!image_size.IsValid()) return false; @@ -391,8 +391,8 @@ bool CPDF_ImageRenderer::StartDIBSource() { } } #ifdef _SKIA_SUPPORT_ - RetainPtr premultiplied = m_pDIBSource->Clone(nullptr); - if (m_pDIBSource->HasAlpha()) + RetainPtr premultiplied = m_pDIBBase->Clone(nullptr); + if (m_pDIBBase->HasAlpha()) CFX_SkiaDeviceDriver::PreMultiply(premultiplied); if (m_pRenderStatus->GetRenderDevice()->StartDIBitsWithBlend( premultiplied, m_BitmapAlpha, m_FillArgb, &m_ImageMatrix, m_Flags, @@ -405,7 +405,7 @@ bool CPDF_ImageRenderer::StartDIBSource() { } #else if (m_pRenderStatus->GetRenderDevice()->StartDIBitsWithBlend( - m_pDIBSource, m_BitmapAlpha, m_FillArgb, &m_ImageMatrix, m_Flags, + m_pDIBBase, m_BitmapAlpha, m_FillArgb, &m_ImageMatrix, m_Flags, &m_DeviceHandle, m_BlendType)) { if (m_DeviceHandle) { m_Status = 3; @@ -429,7 +429,7 @@ bool CPDF_ImageRenderer::StartDIBSource() { clip_box.Intersect(image_rect); m_Status = 2; m_pTransformer = pdfium::MakeUnique( - m_pDIBSource, &m_ImageMatrix, m_Flags, &clip_box); + m_pDIBBase, &m_ImageMatrix, m_Flags, &clip_box); return true; } if (m_ImageMatrix.a < 0) @@ -440,18 +440,18 @@ bool CPDF_ImageRenderer::StartDIBSource() { int dest_left = dest_width > 0 ? image_rect.left : image_rect.right; int dest_top = dest_height > 0 ? image_rect.top : image_rect.bottom; - if (m_pDIBSource->IsOpaqueImage() && m_BitmapAlpha == 255) { + if (m_pDIBBase->IsOpaqueImage() && m_BitmapAlpha == 255) { if (m_pRenderStatus->GetRenderDevice()->StretchDIBitsWithFlagsAndBlend( - m_pDIBSource, dest_left, dest_top, dest_width, dest_height, m_Flags, + m_pDIBBase, dest_left, dest_top, dest_width, dest_height, m_Flags, m_BlendType)) { return false; } } - if (m_pDIBSource->IsAlphaMask()) { + if (m_pDIBBase->IsAlphaMask()) { if (m_BitmapAlpha != 255) m_FillArgb = FXARGB_MUL_ALPHA(m_FillArgb, m_BitmapAlpha); if (m_pRenderStatus->GetRenderDevice()->StretchBitMaskWithFlags( - m_pDIBSource, dest_left, dest_top, dest_width, dest_height, + m_pDIBBase, dest_left, dest_top, dest_width, dest_height, m_FillArgb, m_Flags)) { return false; } @@ -468,7 +468,7 @@ bool CPDF_ImageRenderer::StartDIBSource() { dest_rect.left - image_rect.left, dest_rect.top - image_rect.top, dest_rect.right - image_rect.left, dest_rect.bottom - image_rect.top); RetainPtr pStretched = - m_pDIBSource->StretchTo(dest_width, dest_height, m_Flags, &dest_clip); + m_pDIBBase->StretchTo(dest_width, dest_height, m_Flags, &dest_clip); if (pStretched) { m_pRenderStatus->CompositeDIBitmap(pStretched, dest_rect.left, dest_rect.top, m_FillArgb, m_BitmapAlpha, @@ -478,7 +478,7 @@ bool CPDF_ImageRenderer::StartDIBSource() { } bool CPDF_ImageRenderer::StartBitmapAlpha() { - if (m_pDIBSource->IsOpaqueImage()) { + if (m_pDIBBase->IsOpaqueImage()) { CFX_PathData path; path.AppendRect(0, 0, 1, 1); path.Transform(&m_ImageMatrix); @@ -488,11 +488,11 @@ bool CPDF_ImageRenderer::StartBitmapAlpha() { fill_color, 0, FXFILL_WINDING); return false; } - RetainPtr pAlphaMask; - if (m_pDIBSource->IsAlphaMask()) - pAlphaMask = m_pDIBSource; + RetainPtr pAlphaMask; + if (m_pDIBBase->IsAlphaMask()) + pAlphaMask = m_pDIBBase; else - pAlphaMask = m_pDIBSource->CloneAlphaMask(); + pAlphaMask = m_pDIBBase->CloneAlphaMask(); if (fabs(m_ImageMatrix.b) >= 0.5f || fabs(m_ImageMatrix.c) >= 0.5f) { int left; @@ -554,7 +554,7 @@ bool CPDF_ImageRenderer::Continue(PauseIndicatorIface* pPause) { if (m_Loader.Continue(pPause, m_pRenderStatus.Get())) return true; - if (StartRenderDIBSource()) + if (StartRenderDIBBase()) return Continue(pPause); } return false; diff --git a/core/fpdfapi/render/cpdf_imagerenderer.h b/core/fpdfapi/render/cpdf_imagerenderer.h index e6c6d0c1b8..c6ccb0c86d 100644 --- a/core/fpdfapi/render/cpdf_imagerenderer.h +++ b/core/fpdfapi/render/cpdf_imagerenderer.h @@ -15,7 +15,7 @@ #include "core/fxge/dib/cfx_imagerenderer.h" class CFX_DIBitmap; -class CFX_DIBSource; +class CFX_DIBBase; class CFX_DefaultRenderDevice; class CFX_ImageTransformer; class CPDF_ImageObject; @@ -35,7 +35,7 @@ class CPDF_ImageRenderer { int blendType); bool Start(CPDF_RenderStatus* pStatus, - const RetainPtr& pDIBSource, + const RetainPtr& pDIBBase, FX_ARGB bitmap_argb, int bitmap_alpha, const CFX_Matrix* pImage2Device, @@ -48,9 +48,9 @@ class CPDF_ImageRenderer { private: bool StartBitmapAlpha(); - bool StartDIBSource(); - bool StartRenderDIBSource(); - bool StartLoadDIBSource(); + bool StartDIBBase(); + bool StartRenderDIBBase(); + bool StartLoadDIBBase(); bool DrawMaskedImage(); bool DrawPatternImage(const CFX_Matrix* pObj2Device); bool NotDrawing() const; @@ -58,7 +58,7 @@ class CPDF_ImageRenderer { CFX_Matrix GetDrawMatrix(const FX_RECT& rect) const; void CalculateDrawImage(CFX_DefaultRenderDevice* bitmap_device1, CFX_DefaultRenderDevice* bitmap_device2, - const RetainPtr& pDIBSource, + const RetainPtr& pDIBBase, CFX_Matrix* pNewMatrix, const FX_RECT& rect) const; void HandleFilters(); @@ -69,7 +69,7 @@ class CPDF_ImageRenderer { UnownedPtr m_pObj2Device; CFX_Matrix m_ImageMatrix; CPDF_ImageLoader m_Loader; - RetainPtr m_pDIBSource; + RetainPtr m_pDIBBase; int m_BitmapAlpha; bool m_bPatternColor; UnownedPtr m_pPattern; diff --git a/core/fpdfapi/render/cpdf_pagerendercache.cpp b/core/fpdfapi/render/cpdf_pagerendercache.cpp index 1358943454..61602d36f0 100644 --- a/core/fpdfapi/render/cpdf_pagerendercache.cpp +++ b/core/fpdfapi/render/cpdf_pagerendercache.cpp @@ -94,17 +94,17 @@ bool CPDF_PageRenderCache::StartGetCachedBitmap( m_pCurImageCacheEntry = new CPDF_ImageCacheEntry(m_pPage->GetDocument(), pImage); } - CPDF_DIBSource::LoadState ret = m_pCurImageCacheEntry->StartGetCachedBitmap( + CPDF_DIBBase::LoadState ret = m_pCurImageCacheEntry->StartGetCachedBitmap( pRenderStatus->GetFormResource(), m_pPage->m_pPageResources.Get(), bStdCS, GroupFamily, bLoadMask, pRenderStatus); - if (ret == CPDF_DIBSource::LoadState::kContinue) + if (ret == CPDF_DIBBase::LoadState::kContinue) return true; m_nTimeCount++; if (!m_bCurFindCache) m_ImageCache[pStream] = m_pCurImageCacheEntry; - if (ret == CPDF_DIBSource::LoadState::kFail) + if (ret == CPDF_DIBBase::LoadState::kFail) m_nCacheSize += m_pCurImageCacheEntry->EstimateSize(); return false; diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp index 2cbe495ce4..9965cbb945 100644 --- a/core/fpdfapi/render/cpdf_renderstatus.cpp +++ b/core/fpdfapi/render/cpdf_renderstatus.cpp @@ -37,7 +37,7 @@ #include "core/fpdfapi/parser/cpdf_document.h" #include "core/fpdfapi/render/cpdf_charposlist.h" #include "core/fpdfapi/render/cpdf_devicebuffer.h" -#include "core/fpdfapi/render/cpdf_dibsource.h" +#include "core/fpdfapi/render/cpdf_dibbase.h" #include "core/fpdfapi/render/cpdf_docrenderdata.h" #include "core/fpdfapi/render/cpdf_imagerenderer.h" #include "core/fpdfapi/render/cpdf_pagerendercache.h" @@ -1582,7 +1582,7 @@ bool CPDF_RenderStatus::ProcessTransparency(CPDF_PageObject* pPageObj, if (pSMaskDict) { CFX_Matrix smask_matrix = *pPageObj->m_GeneralState.GetSMaskMatrix(); smask_matrix.Concat(*pObj2Device); - RetainPtr pSMaskSource = + RetainPtr pSMaskSource = LoadSMask(pSMaskDict, &rect, &smask_matrix); if (pSMaskSource) bitmap->MultiplyAlpha(pSMaskSource); diff --git a/core/fpdfapi/render/cpdf_transferfunc.cpp b/core/fpdfapi/render/cpdf_transferfunc.cpp index 25717437d3..f67f508e8e 100644 --- a/core/fpdfapi/render/cpdf_transferfunc.cpp +++ b/core/fpdfapi/render/cpdf_transferfunc.cpp @@ -8,7 +8,7 @@ #include "core/fpdfapi/parser/cpdf_document.h" #include "core/fpdfapi/render/cpdf_dibtransferfunc.h" -#include "core/fxge/dib/cfx_dibsource.h" +#include "core/fxge/dib/cfx_dibbase.h" CPDF_TransferFunc::CPDF_TransferFunc(CPDF_Document* pDoc) : m_pPDFDoc(pDoc) {} @@ -20,8 +20,8 @@ FX_COLORREF CPDF_TransferFunc::TranslateColor(FX_COLORREF colorref) const { m_Samples[FXSYS_GetRValue(colorref)]); } -RetainPtr CPDF_TransferFunc::TranslateImage( - const RetainPtr& pSrc) { +RetainPtr CPDF_TransferFunc::TranslateImage( + const RetainPtr& pSrc) { RetainPtr pHolder(this); auto pDest = pdfium::MakeRetain(pHolder); pDest->LoadSrc(pSrc); diff --git a/core/fpdfapi/render/cpdf_transferfunc.h b/core/fpdfapi/render/cpdf_transferfunc.h index dfc186ec6c..e8964727c6 100644 --- a/core/fpdfapi/render/cpdf_transferfunc.h +++ b/core/fpdfapi/render/cpdf_transferfunc.h @@ -12,7 +12,7 @@ #include "core/fxge/fx_dib.h" class CPDF_Document; -class CFX_DIBSource; +class CFX_DIBBase; class CPDF_TransferFunc : public Retainable { public: @@ -20,7 +20,7 @@ class CPDF_TransferFunc : public Retainable { friend RetainPtr pdfium::MakeRetain(Args&&... args); FX_COLORREF TranslateColor(FX_COLORREF colorref) const; - RetainPtr TranslateImage(const RetainPtr& pSrc); + RetainPtr TranslateImage(const RetainPtr& pSrc); const CPDF_Document* GetDocument() const { return m_pPDFDoc.Get(); } diff --git a/core/fxcodec/codec/ccodec_jpegmodule.h b/core/fxcodec/codec/ccodec_jpegmodule.h index 06cd2242c2..d990ee938f 100644 --- a/core/fxcodec/codec/ccodec_jpegmodule.h +++ b/core/fxcodec/codec/ccodec_jpegmodule.h @@ -14,7 +14,7 @@ #include "core/fxcrt/retain_ptr.h" class CCodec_ScanlineDecoder; -class CFX_DIBSource; +class CFX_DIBBase; #ifdef PDF_ENABLE_XFA class CFX_DIBAttribute; @@ -58,7 +58,7 @@ class CCodec_JpegModule { uint32_t GetAvailInput(Context* pContext, uint8_t** avail_buf_ptr); #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ - static bool JpegEncode(const RetainPtr& pSource, + static bool JpegEncode(const RetainPtr& pSource, uint8_t** dest_buf, size_t* dest_size); #endif // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ diff --git a/core/fxcodec/codec/fx_codec_jpeg.cpp b/core/fxcodec/codec/fx_codec_jpeg.cpp index f0b8d66d3a..4c37465386 100644 --- a/core/fxcodec/codec/fx_codec_jpeg.cpp +++ b/core/fxcodec/codec/fx_codec_jpeg.cpp @@ -13,7 +13,7 @@ #include "core/fxcodec/codec/ccodec_scanlinedecoder.h" #include "core/fxcodec/fx_codec.h" #include "core/fxcrt/fx_safe_types.h" -#include "core/fxge/dib/cfx_dibsource.h" +#include "core/fxge/dib/cfx_dibbase.h" #include "core/fxge/fx_dib.h" #include "third_party/base/logging.h" #include "third_party/base/ptr_util.h" @@ -493,7 +493,7 @@ uint32_t CCodec_JpegModule::GetAvailInput(Context* pContext, #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ #define JPEG_BLOCK_SIZE 1048576 -bool CCodec_JpegModule::JpegEncode(const RetainPtr& pSource, +bool CCodec_JpegModule::JpegEncode(const RetainPtr& pSource, uint8_t** dest_buf, size_t* dest_size) { struct jpeg_error_mgr jerr; diff --git a/core/fxcodec/fx_codec.h b/core/fxcodec/fx_codec.h index 1eaf846fd7..0a0c08afc3 100644 --- a/core/fxcodec/fx_codec.h +++ b/core/fxcodec/fx_codec.h @@ -43,7 +43,7 @@ class CCodec_IccModule; class CCodec_Jbig2Module; class CCodec_JpegModule; class CCodec_JpxModule; -class CFX_DIBSource; +class CFX_DIBBase; class CJPX_Decoder; class CPDF_ColorSpace; class CPDF_StreamAcc; diff --git a/core/fxge/agg/fx_agg_driver.cpp b/core/fxge/agg/fx_agg_driver.cpp index 9166fbb0c8..063f36d600 100644 --- a/core/fxge/agg/fx_agg_driver.cpp +++ b/core/fxge/agg/fx_agg_driver.cpp @@ -144,7 +144,7 @@ void RgbByteOrderTransferBitmap(const RetainPtr& pBitmap, int dest_top, int width, int height, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, int src_left, int src_top) { if (!pBitmap) @@ -1491,7 +1491,7 @@ RetainPtr CFX_AggDeviceDriver::GetBackDrop() { return m_pBackdropBitmap; } -bool CFX_AggDeviceDriver::SetDIBits(const RetainPtr& pBitmap, +bool CFX_AggDeviceDriver::SetDIBits(const RetainPtr& pBitmap, uint32_t argb, const FX_RECT* pSrcRect, int left, @@ -1511,7 +1511,7 @@ bool CFX_AggDeviceDriver::SetDIBits(const RetainPtr& pBitmap, pSrcRect->top, blend_type, m_pClipRgn.get(), m_bRgbByteOrder); } -bool CFX_AggDeviceDriver::StretchDIBits(const RetainPtr& pSource, +bool CFX_AggDeviceDriver::StretchDIBits(const RetainPtr& pSource, uint32_t argb, int dest_left, int dest_top, @@ -1545,7 +1545,7 @@ bool CFX_AggDeviceDriver::StretchDIBits(const RetainPtr& pSource, } bool CFX_AggDeviceDriver::StartDIBits( - const RetainPtr& pSource, + const RetainPtr& pSource, int bitmap_alpha, uint32_t argb, const CFX_Matrix* pMatrix, diff --git a/core/fxge/agg/fx_agg_driver.h b/core/fxge/agg/fx_agg_driver.h index b59cec004c..167b211746 100644 --- a/core/fxge/agg/fx_agg_driver.h +++ b/core/fxge/agg/fx_agg_driver.h @@ -67,13 +67,13 @@ class CFX_AggDeviceDriver : public RenderDeviceDriverIface { int left, int top) override; RetainPtr GetBackDrop() override; - bool SetDIBits(const RetainPtr& pBitmap, + bool SetDIBits(const RetainPtr& pBitmap, uint32_t color, const FX_RECT* pSrcRect, int left, int top, int blend_type) override; - bool StretchDIBits(const RetainPtr& pBitmap, + bool StretchDIBits(const RetainPtr& pBitmap, uint32_t color, int dest_left, int dest_top, @@ -82,7 +82,7 @@ class CFX_AggDeviceDriver : public RenderDeviceDriverIface { const FX_RECT* pClipRect, uint32_t flags, int blend_type) override; - bool StartDIBits(const RetainPtr& pBitmap, + bool StartDIBits(const RetainPtr& pBitmap, int bitmap_alpha, uint32_t color, const CFX_Matrix* pMatrix, diff --git a/core/fxge/cfx_defaultrenderdevice.h b/core/fxge/cfx_defaultrenderdevice.h index 6510e41f6f..07e3cc57c9 100644 --- a/core/fxge/cfx_defaultrenderdevice.h +++ b/core/fxge/cfx_defaultrenderdevice.h @@ -31,8 +31,8 @@ class CFX_DefaultRenderDevice : public CFX_RenderDevice { void Clear(uint32_t color); SkPictureRecorder* CreateRecorder(int size_x, int size_y); void DebugVerifyBitmapIsPreMultiplied() const override; - bool SetBitsWithMask(const RetainPtr& pBitmap, - const RetainPtr& pMask, + bool SetBitsWithMask(const RetainPtr& pBitmap, + const RetainPtr& pMask, int left, int top, int bitmap_alpha, diff --git a/core/fxge/cfx_renderdevice.cpp b/core/fxge/cfx_renderdevice.cpp index 16d723721f..ab57391401 100644 --- a/core/fxge/cfx_renderdevice.cpp +++ b/core/fxge/cfx_renderdevice.cpp @@ -722,11 +722,10 @@ RetainPtr CFX_RenderDevice::GetBackDrop() { return m_pDeviceDriver->GetBackDrop(); } -bool CFX_RenderDevice::SetDIBitsWithBlend( - const RetainPtr& pBitmap, - int left, - int top, - int blend_mode) { +bool CFX_RenderDevice::SetDIBitsWithBlend(const RetainPtr& pBitmap, + int left, + int top, + int blend_mode) { ASSERT(!pBitmap->IsAlphaMask()); FX_RECT dest_rect(left, top, left + pBitmap->GetWidth(), top + pBitmap->GetHeight()); @@ -767,7 +766,7 @@ bool CFX_RenderDevice::SetDIBitsWithBlend( } bool CFX_RenderDevice::StretchDIBitsWithFlagsAndBlend( - const RetainPtr& pBitmap, + const RetainPtr& pBitmap, int left, int top, int dest_width, @@ -782,7 +781,7 @@ bool CFX_RenderDevice::StretchDIBitsWithFlagsAndBlend( dest_height, &clip_box, flags, blend_mode); } -bool CFX_RenderDevice::SetBitMask(const RetainPtr& pBitmap, +bool CFX_RenderDevice::SetBitMask(const RetainPtr& pBitmap, int left, int top, uint32_t argb) { @@ -791,7 +790,7 @@ bool CFX_RenderDevice::SetBitMask(const RetainPtr& pBitmap, FXDIB_BLEND_NORMAL); } -bool CFX_RenderDevice::StretchBitMask(const RetainPtr& pBitmap, +bool CFX_RenderDevice::StretchBitMask(const RetainPtr& pBitmap, int left, int top, int dest_width, @@ -802,7 +801,7 @@ bool CFX_RenderDevice::StretchBitMask(const RetainPtr& pBitmap, } bool CFX_RenderDevice::StretchBitMaskWithFlags( - const RetainPtr& pBitmap, + const RetainPtr& pBitmap, int left, int top, int dest_width, @@ -818,7 +817,7 @@ bool CFX_RenderDevice::StretchBitMaskWithFlags( } bool CFX_RenderDevice::StartDIBitsWithBlend( - const RetainPtr& pBitmap, + const RetainPtr& pBitmap, int bitmap_alpha, uint32_t argb, const CFX_Matrix* pMatrix, @@ -839,8 +838,8 @@ void CFX_RenderDevice::DebugVerifyBitmapIsPreMultiplied() const { SkASSERT(0); } -bool CFX_RenderDevice::SetBitsWithMask(const RetainPtr& pBitmap, - const RetainPtr& pMask, +bool CFX_RenderDevice::SetBitsWithMask(const RetainPtr& pBitmap, + const RetainPtr& pMask, int left, int top, int bitmap_alpha, diff --git a/core/fxge/cfx_renderdevice.h b/core/fxge/cfx_renderdevice.h index 1d6fe48170..f0d78989be 100644 --- a/core/fxge/cfx_renderdevice.h +++ b/core/fxge/cfx_renderdevice.h @@ -153,14 +153,14 @@ class CFX_RenderDevice { RetainPtr GetBackDrop(); bool GetDIBits(const RetainPtr& pBitmap, int left, int top); - bool SetDIBits(const RetainPtr& pBitmap, int left, int top) { + bool SetDIBits(const RetainPtr& pBitmap, int left, int top) { return SetDIBitsWithBlend(pBitmap, left, top, FXDIB_BLEND_NORMAL); } - bool SetDIBitsWithBlend(const RetainPtr& pBitmap, + bool SetDIBitsWithBlend(const RetainPtr& pBitmap, int left, int top, int blend_type); - bool StretchDIBits(const RetainPtr& pBitmap, + bool StretchDIBits(const RetainPtr& pBitmap, int left, int top, int dest_width, @@ -168,31 +168,31 @@ class CFX_RenderDevice { return StretchDIBitsWithFlagsAndBlend(pBitmap, left, top, dest_width, dest_height, 0, FXDIB_BLEND_NORMAL); } - bool StretchDIBitsWithFlagsAndBlend(const RetainPtr& pBitmap, + bool StretchDIBitsWithFlagsAndBlend(const RetainPtr& pBitmap, int left, int top, int dest_width, int dest_height, uint32_t flags, int blend_type); - bool SetBitMask(const RetainPtr& pBitmap, + bool SetBitMask(const RetainPtr& pBitmap, int left, int top, uint32_t color); - bool StretchBitMask(const RetainPtr& pBitmap, + bool StretchBitMask(const RetainPtr& pBitmap, int left, int top, int dest_width, int dest_height, uint32_t color); - bool StretchBitMaskWithFlags(const RetainPtr& pBitmap, + bool StretchBitMaskWithFlags(const RetainPtr& pBitmap, int left, int top, int dest_width, int dest_height, uint32_t color, uint32_t flags); - bool StartDIBits(const RetainPtr& pBitmap, + bool StartDIBits(const RetainPtr& pBitmap, int bitmap_alpha, uint32_t color, const CFX_Matrix* pMatrix, @@ -201,7 +201,7 @@ class CFX_RenderDevice { return StartDIBitsWithBlend(pBitmap, bitmap_alpha, color, pMatrix, flags, handle, FXDIB_BLEND_NORMAL); } - bool StartDIBitsWithBlend(const RetainPtr& pBitmap, + bool StartDIBitsWithBlend(const RetainPtr& pBitmap, int bitmap_alpha, uint32_t color, const CFX_Matrix* pMatrix, @@ -266,8 +266,8 @@ class CFX_RenderDevice { #ifdef _SKIA_SUPPORT_ virtual void DebugVerifyBitmapIsPreMultiplied() const; - virtual bool SetBitsWithMask(const RetainPtr& pBitmap, - const RetainPtr& pMask, + virtual bool SetBitsWithMask(const RetainPtr& pBitmap, + const RetainPtr& pMask, int left, int top, int bitmap_alpha, diff --git a/core/fxge/dib/cfx_dibbase.cpp b/core/fxge/dib/cfx_dibbase.cpp new file mode 100644 index 0000000000..79150ba616 --- /dev/null +++ b/core/fxge/dib/cfx_dibbase.cpp @@ -0,0 +1,1259 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fxge/dib/cfx_dibbase.h" + +#include +#include +#include +#include + +#include "core/fxcodec/fx_codec.h" +#include "core/fxge/cfx_cliprgn.h" +#include "core/fxge/dib/cfx_bitmapstorer.h" +#include "core/fxge/dib/cfx_dibitmap.h" +#include "core/fxge/dib/cfx_imagestretcher.h" +#include "core/fxge/dib/cfx_imagetransformer.h" +#include "third_party/base/logging.h" +#include "third_party/base/ptr_util.h" + +namespace { + +void ColorDecode(uint32_t pal_v, uint8_t* r, uint8_t* g, uint8_t* b) { + *r = static_cast((pal_v & 0xf00) >> 4); + *g = static_cast(pal_v & 0x0f0); + *b = static_cast((pal_v & 0x00f) << 4); +} + +void Obtain_Pal(std::pair* luts, + uint32_t* dest_pal, + uint32_t lut) { + uint32_t lut_1 = lut - 1; + for (int row = 0; row < 256; ++row) { + int lut_offset = lut_1 - row; + if (lut_offset < 0) + lut_offset += 256; + uint32_t color = luts[lut_offset].second; + uint8_t r; + uint8_t g; + uint8_t b; + ColorDecode(color, &r, &g, &b); + dest_pal[row] = (static_cast(r) << 16) | + (static_cast(g) << 8) | b | 0xff000000; + luts[lut_offset].first = row; + } +} + +class CFX_Palette { + public: + explicit CFX_Palette(const RetainPtr& pBitmap); + ~CFX_Palette(); + + const uint32_t* GetPalette() { return m_Palette.data(); } + const std::pair* GetLuts() const { return m_Luts.data(); } + int32_t GetLutCount() const { return m_lut; } + void SetAmountLut(int row, uint32_t value) { m_Luts[row].first = value; } + + private: + std::vector m_Palette; + // (Amount, Color) pairs + std::vector> m_Luts; + int m_lut; +}; + +CFX_Palette::CFX_Palette(const RetainPtr& pBitmap) + : m_Palette(256), m_Luts(4096), m_lut(0) { + int bpp = pBitmap->GetBPP() / 8; + int width = pBitmap->GetWidth(); + int height = pBitmap->GetHeight(); + for (int row = 0; row < height; ++row) { + const uint8_t* scan_line = pBitmap->GetScanline(row); + for (int col = 0; col < width; ++col) { + const uint8_t* src_port = scan_line + col * bpp; + uint32_t b = src_port[0] & 0xf0; + uint32_t g = src_port[1] & 0xf0; + uint32_t r = src_port[2] & 0xf0; + uint32_t index = (r << 4) + g + (b >> 4); + ++m_Luts[index].first; + } + } + // Move non-zeros to the front and count them + for (int row = 0; row < 4096; ++row) { + if (m_Luts[row].first != 0) { + m_Luts[m_lut].first = m_Luts[row].first; + m_Luts[m_lut].second = row; + ++m_lut; + } + } + std::sort(m_Luts.begin(), m_Luts.begin() + m_lut, + [](const std::pair& arg1, + const std::pair& arg2) { + return arg1.first < arg2.first; + }); + Obtain_Pal(m_Luts.data(), m_Palette.data(), m_lut); +} + +CFX_Palette::~CFX_Palette() {} + +void ConvertBuffer_1bppMask2Gray(uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + static constexpr uint8_t kSetGray = 0xff; + static constexpr uint8_t kResetGray = 0x00; + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + memset(dest_scan, kResetGray, width); + const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row); + for (int col = src_left; col < src_left + width; ++col) { + if (src_scan[col / 8] & (1 << (7 - col % 8))) + *dest_scan = kSetGray; + ++dest_scan; + } + } +} + +void ConvertBuffer_8bppMask2Gray(uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left; + memcpy(dest_scan, src_scan, width); + } +} + +void ConvertBuffer_1bppPlt2Gray(uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + uint32_t* src_plt = pSrcBitmap->GetPalette(); + uint8_t gray[2]; + uint8_t reset_r; + uint8_t reset_g; + uint8_t reset_b; + uint8_t set_r; + uint8_t set_g; + uint8_t set_b; + if (pSrcBitmap->IsCmykImage()) { + std::tie(reset_r, reset_g, reset_b) = AdobeCMYK_to_sRGB1( + FXSYS_GetCValue(src_plt[0]), FXSYS_GetMValue(src_plt[0]), + FXSYS_GetYValue(src_plt[0]), FXSYS_GetKValue(src_plt[0])); + std::tie(set_r, set_g, set_b) = AdobeCMYK_to_sRGB1( + FXSYS_GetCValue(src_plt[1]), FXSYS_GetMValue(src_plt[1]), + FXSYS_GetYValue(src_plt[1]), FXSYS_GetKValue(src_plt[1])); + } else { + reset_r = FXARGB_R(src_plt[0]); + reset_g = FXARGB_G(src_plt[0]); + reset_b = FXARGB_B(src_plt[0]); + set_r = FXARGB_R(src_plt[1]); + set_g = FXARGB_G(src_plt[1]); + set_b = FXARGB_B(src_plt[1]); + } + gray[0] = FXRGB2GRAY(reset_r, reset_g, reset_b); + gray[1] = FXRGB2GRAY(set_r, set_g, set_b); + + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + memset(dest_scan, gray[0], width); + const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row); + for (int col = src_left; col < src_left + width; ++col) { + if (src_scan[col / 8] & (1 << (7 - col % 8))) + *dest_scan = gray[1]; + ++dest_scan; + } + } +} + +void ConvertBuffer_8bppPlt2Gray(uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + uint32_t* src_plt = pSrcBitmap->GetPalette(); + uint8_t gray[256]; + if (pSrcBitmap->IsCmykImage()) { + uint8_t r; + uint8_t g; + uint8_t b; + for (size_t i = 0; i < FX_ArraySize(gray); ++i) { + std::tie(r, g, b) = AdobeCMYK_to_sRGB1( + FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]), + FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i])); + gray[i] = FXRGB2GRAY(r, g, b); + } + } else { + for (size_t i = 0; i < FX_ArraySize(gray); ++i) { + gray[i] = FXRGB2GRAY(FXARGB_R(src_plt[i]), FXARGB_G(src_plt[i]), + FXARGB_B(src_plt[i])); + } + } + + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left; + for (int col = 0; col < width; ++col) + *dest_scan++ = gray[*src_scan++]; + } +} + +void ConvertBuffer_RgbOrCmyk2Gray(uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + int Bpp = pSrcBitmap->GetBPP() / 8; + if (pSrcBitmap->IsCmykImage()) { + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + const uint8_t* src_scan = + pSrcBitmap->GetScanline(src_top + row) + src_left * 4; + for (int col = 0; col < width; ++col) { + uint8_t r; + uint8_t g; + uint8_t b; + std::tie(r, g, b) = AdobeCMYK_to_sRGB1( + FXSYS_GetCValue(static_cast(src_scan[0])), + FXSYS_GetMValue(static_cast(src_scan[1])), + FXSYS_GetYValue(static_cast(src_scan[2])), + FXSYS_GetKValue(static_cast(src_scan[3]))); + *dest_scan++ = FXRGB2GRAY(r, g, b); + src_scan += 4; + } + } + } else { + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + const uint8_t* src_scan = + pSrcBitmap->GetScanline(src_top + row) + src_left * Bpp; + for (int col = 0; col < width; ++col) { + *dest_scan++ = FXRGB2GRAY(src_scan[2], src_scan[1], src_scan[0]); + src_scan += Bpp; + } + } + } +} + +void ConvertBuffer_IndexCopy(uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + if (pSrcBitmap->GetBPP() == 1) { + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + // Set all destination pixels to be white initially. + memset(dest_scan, 255, width); + const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row); + for (int col = src_left; col < src_left + width; ++col) { + // If the source bit is set, then set the destination pixel to be black. + if (src_scan[col / 8] & (1 << (7 - col % 8))) + *dest_scan = 0; + + ++dest_scan; + } + } + } else { + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + const uint8_t* src_scan = + pSrcBitmap->GetScanline(src_top + row) + src_left; + memcpy(dest_scan, src_scan, width); + } + } +} + +void ConvertBuffer_Plt2PltRgb8(uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top, + uint32_t* dst_plt) { + ConvertBuffer_IndexCopy(dest_buf, dest_pitch, width, height, pSrcBitmap, + src_left, src_top); + uint32_t* src_plt = pSrcBitmap->GetPalette(); + int plt_size = pSrcBitmap->GetPaletteSize(); + if (pSrcBitmap->IsCmykImage()) { + for (int i = 0; i < plt_size; ++i) { + uint8_t r; + uint8_t g; + uint8_t b; + std::tie(r, g, b) = AdobeCMYK_to_sRGB1( + FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]), + FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i])); + dst_plt[i] = ArgbEncode(0xff, r, g, b); + } + } else { + memcpy(dst_plt, src_plt, plt_size * 4); + } +} + +void ConvertBuffer_Rgb2PltRgb8(uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top, + uint32_t* dst_plt) { + int bpp = pSrcBitmap->GetBPP() / 8; + CFX_Palette palette(pSrcBitmap); + const std::pair* Luts = palette.GetLuts(); + int lut = palette.GetLutCount(); + const uint32_t* pal = palette.GetPalette(); + if (lut > 256) { + int err; + int min_err; + int lut_256 = lut - 256; + for (int row = 0; row < lut_256; ++row) { + min_err = 1000000; + uint8_t r; + uint8_t g; + uint8_t b; + ColorDecode(Luts[row].second, &r, &g, &b); + uint32_t clrindex = 0; + for (int col = 0; col < 256; ++col) { + uint32_t p_color = pal[col]; + int d_r = r - static_cast(p_color >> 16); + int d_g = g - static_cast(p_color >> 8); + int d_b = b - static_cast(p_color); + err = d_r * d_r + d_g * d_g + d_b * d_b; + if (err < min_err) { + min_err = err; + clrindex = col; + } + } + palette.SetAmountLut(row, clrindex); + } + } + int32_t lut_1 = lut - 1; + for (int row = 0; row < height; ++row) { + const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left; + uint8_t* dest_scan = dest_buf + row * dest_pitch; + for (int col = 0; col < width; ++col) { + const uint8_t* src_port = src_scan + col * bpp; + int r = src_port[2] & 0xf0; + int g = src_port[1] & 0xf0; + int b = src_port[0] & 0xf0; + uint32_t clrindex = (r << 4) + g + (b >> 4); + for (int i = lut_1; i >= 0; --i) + if (clrindex == Luts[i].second) { + *(dest_scan + col) = static_cast(Luts[i].first); + break; + } + } + } + memcpy(dst_plt, pal, sizeof(uint32_t) * 256); +} + +void ConvertBuffer_1bppMask2Rgb(FXDIB_Format dest_format, + uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + int comps = GetCompsFromFormat(dest_format); + static constexpr uint8_t kSetGray = 0xff; + static constexpr uint8_t kResetGray = 0x00; + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row); + for (int col = src_left; col < src_left + width; ++col) { + if (src_scan[col / 8] & (1 << (7 - col % 8))) { + dest_scan[0] = kSetGray; + dest_scan[1] = kSetGray; + dest_scan[2] = kSetGray; + } else { + dest_scan[0] = kResetGray; + dest_scan[1] = kResetGray; + dest_scan[2] = kResetGray; + } + dest_scan += comps; + } + } +} + +void ConvertBuffer_8bppMask2Rgb(FXDIB_Format dest_format, + uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + int comps = GetCompsFromFormat(dest_format); + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left; + uint8_t src_pixel; + for (int col = 0; col < width; ++col) { + src_pixel = *src_scan++; + *dest_scan++ = src_pixel; + *dest_scan++ = src_pixel; + *dest_scan = src_pixel; + dest_scan += comps - 2; + } + } +} + +void ConvertBuffer_1bppPlt2Rgb(FXDIB_Format dest_format, + uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + int comps = GetCompsFromFormat(dest_format); + uint32_t* src_plt = pSrcBitmap->GetPalette(); + uint32_t plt[2]; + uint8_t* bgr_ptr = reinterpret_cast(plt); + if (pSrcBitmap->IsCmykImage()) { + plt[0] = FXCMYK_TODIB(src_plt[0]); + plt[1] = FXCMYK_TODIB(src_plt[1]); + } else { + bgr_ptr[0] = FXARGB_B(src_plt[0]); + bgr_ptr[1] = FXARGB_G(src_plt[0]); + bgr_ptr[2] = FXARGB_R(src_plt[0]); + bgr_ptr[3] = FXARGB_B(src_plt[1]); + bgr_ptr[4] = FXARGB_G(src_plt[1]); + bgr_ptr[5] = FXARGB_R(src_plt[1]); + } + + if (pSrcBitmap->IsCmykImage()) { + std::tie(bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]) = AdobeCMYK_to_sRGB1( + FXSYS_GetCValue(src_plt[0]), FXSYS_GetMValue(src_plt[0]), + FXSYS_GetYValue(src_plt[0]), FXSYS_GetKValue(src_plt[0])); + std::tie(bgr_ptr[5], bgr_ptr[4], bgr_ptr[3]) = AdobeCMYK_to_sRGB1( + FXSYS_GetCValue(src_plt[1]), FXSYS_GetMValue(src_plt[1]), + FXSYS_GetYValue(src_plt[1]), FXSYS_GetKValue(src_plt[1])); + } + + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row); + for (int col = src_left; col < src_left + width; ++col) { + if (src_scan[col / 8] & (1 << (7 - col % 8))) { + *dest_scan++ = bgr_ptr[3]; + *dest_scan++ = bgr_ptr[4]; + *dest_scan = bgr_ptr[5]; + } else { + *dest_scan++ = bgr_ptr[0]; + *dest_scan++ = bgr_ptr[1]; + *dest_scan = bgr_ptr[2]; + } + dest_scan += comps - 2; + } + } +} + +void ConvertBuffer_8bppPlt2Rgb(FXDIB_Format dest_format, + uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + int comps = GetCompsFromFormat(dest_format); + uint32_t* src_plt = pSrcBitmap->GetPalette(); + uint32_t plt[256]; + uint8_t* bgr_ptr = reinterpret_cast(plt); + if (!pSrcBitmap->IsCmykImage()) { + for (int i = 0; i < 256; ++i) { + *bgr_ptr++ = FXARGB_B(src_plt[i]); + *bgr_ptr++ = FXARGB_G(src_plt[i]); + *bgr_ptr++ = FXARGB_R(src_plt[i]); + } + bgr_ptr = reinterpret_cast(plt); + } + + if (pSrcBitmap->IsCmykImage()) { + for (int i = 0; i < 256; ++i) { + std::tie(bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]) = AdobeCMYK_to_sRGB1( + FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]), + FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i])); + bgr_ptr += 3; + } + bgr_ptr = reinterpret_cast(plt); + } + + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left; + for (int col = 0; col < width; ++col) { + uint8_t* src_pixel = bgr_ptr + 3 * (*src_scan++); + *dest_scan++ = *src_pixel++; + *dest_scan++ = *src_pixel++; + *dest_scan = *src_pixel++; + dest_scan += comps - 2; + } + } +} + +void ConvertBuffer_24bppRgb2Rgb24(uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + const uint8_t* src_scan = + pSrcBitmap->GetScanline(src_top + row) + src_left * 3; + memcpy(dest_scan, src_scan, width * 3); + } +} + +void ConvertBuffer_32bppRgb2Rgb24(uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + const uint8_t* src_scan = + pSrcBitmap->GetScanline(src_top + row) + src_left * 4; + for (int col = 0; col < width; ++col) { + *dest_scan++ = *src_scan++; + *dest_scan++ = *src_scan++; + *dest_scan++ = *src_scan++; + ++src_scan; + } + } +} + +void ConvertBuffer_Rgb2Rgb32(uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + int comps = pSrcBitmap->GetBPP() / 8; + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + const uint8_t* src_scan = + pSrcBitmap->GetScanline(src_top + row) + src_left * comps; + for (int col = 0; col < width; ++col) { + *dest_scan++ = *src_scan++; + *dest_scan++ = *src_scan++; + *dest_scan++ = *src_scan++; + ++dest_scan; + src_scan += comps - 3; + } + } +} + +void ConvertBuffer_32bppCmyk2Rgb32(uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + for (int row = 0; row < height; ++row) { + uint8_t* dest_scan = dest_buf + row * dest_pitch; + const uint8_t* src_scan = + pSrcBitmap->GetScanline(src_top + row) + src_left * 4; + for (int col = 0; col < width; ++col) { + std::tie(dest_scan[2], dest_scan[1], dest_scan[0]) = AdobeCMYK_to_sRGB1( + src_scan[0], src_scan[1], src_scan[2], src_scan[3]); + dest_scan += 4; + src_scan += 4; + } + } +} + +bool ConvertBuffer_8bppMask(int bpp, + uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + switch (bpp) { + case 1: + if (pSrcBitmap->GetPalette()) { + ConvertBuffer_1bppPlt2Gray(dest_buf, dest_pitch, width, height, + pSrcBitmap, src_left, src_top); + } else { + ConvertBuffer_1bppMask2Gray(dest_buf, dest_pitch, width, height, + pSrcBitmap, src_left, src_top); + } + return true; + case 8: + if (pSrcBitmap->GetPalette()) { + ConvertBuffer_8bppPlt2Gray(dest_buf, dest_pitch, width, height, + pSrcBitmap, src_left, src_top); + } else { + ConvertBuffer_8bppMask2Gray(dest_buf, dest_pitch, width, height, + pSrcBitmap, src_left, src_top); + } + return true; + case 24: + case 32: + ConvertBuffer_RgbOrCmyk2Gray(dest_buf, dest_pitch, width, height, + pSrcBitmap, src_left, src_top); + return true; + default: + return false; + } +} + +bool ConvertBuffer_Rgb(int bpp, + FXDIB_Format dest_format, + uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + switch (bpp) { + case 1: + if (pSrcBitmap->GetPalette()) { + ConvertBuffer_1bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, + height, pSrcBitmap, src_left, src_top); + } else { + ConvertBuffer_1bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, + height, pSrcBitmap, src_left, src_top); + } + return true; + case 8: + if (pSrcBitmap->GetPalette()) { + ConvertBuffer_8bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, + height, pSrcBitmap, src_left, src_top); + } else { + ConvertBuffer_8bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, + height, pSrcBitmap, src_left, src_top); + } + return true; + case 24: + ConvertBuffer_24bppRgb2Rgb24(dest_buf, dest_pitch, width, height, + pSrcBitmap, src_left, src_top); + return true; + case 32: + ConvertBuffer_32bppRgb2Rgb24(dest_buf, dest_pitch, width, height, + pSrcBitmap, src_left, src_top); + return true; + default: + return false; + } +} + +bool ConvertBuffer_Argb(int bpp, + bool cmyk, + FXDIB_Format dest_format, + uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top) { + switch (bpp) { + case 1: + if (pSrcBitmap->GetPalette()) { + ConvertBuffer_1bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, + height, pSrcBitmap, src_left, src_top); + } else { + ConvertBuffer_1bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, + height, pSrcBitmap, src_left, src_top); + } + return true; + case 8: + if (pSrcBitmap->GetPalette()) { + ConvertBuffer_8bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, + height, pSrcBitmap, src_left, src_top); + } else { + ConvertBuffer_8bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, + height, pSrcBitmap, src_left, src_top); + } + return true; + case 24: + case 32: + if (cmyk) { + ConvertBuffer_32bppCmyk2Rgb32(dest_buf, dest_pitch, width, height, + pSrcBitmap, src_left, src_top); + } else { + ConvertBuffer_Rgb2Rgb32(dest_buf, dest_pitch, width, height, pSrcBitmap, + src_left, src_top); + } + return true; + default: + return false; + } +} + +} // namespace + +CFX_DIBBase::CFX_DIBBase() + : m_Width(0), m_Height(0), m_bpp(0), m_AlphaFlag(0), m_Pitch(0) {} + +CFX_DIBBase::~CFX_DIBBase() {} + +uint8_t* CFX_DIBBase::GetBuffer() const { + return nullptr; +} + +bool CFX_DIBBase::SkipToScanline(int line, PauseIndicatorIface* pPause) const { + return false; +} + +RetainPtr CFX_DIBBase::Clone(const FX_RECT* pClip) const { + FX_RECT rect(0, 0, m_Width, m_Height); + if (pClip) { + rect.Intersect(*pClip); + if (rect.IsEmpty()) + return nullptr; + } + auto pNewBitmap = pdfium::MakeRetain(); + if (!pNewBitmap->Create(rect.Width(), rect.Height(), GetFormat())) + return nullptr; + + 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; + int dword_count = pNewBitmap->m_Pitch / 4; + for (int row = rect.top; row < rect.bottom; ++row) { + const uint32_t* src_scan = + reinterpret_cast(GetScanline(row)) + rect.left / 32; + uint32_t* dest_scan = reinterpret_cast( + pNewBitmap->GetWritableScanline(row - rect.top)); + for (int i = 0; i < dword_count; ++i) { + dest_scan[i] = + (src_scan[i] << left_shift) | (src_scan[i + 1] >> right_shift); + } + } + } else { + int copy_len = (pNewBitmap->GetWidth() * pNewBitmap->GetBPP() + 7) / 8; + if (m_Pitch < static_cast(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 = pNewBitmap->GetWritableScanline(row - rect.top); + memcpy(dest_scan, src_scan, copy_len); + } + } + return pNewBitmap; +} + +void CFX_DIBBase::BuildPalette() { + if (m_pPalette) + return; + + if (GetBPP() == 1) { + m_pPalette.reset(FX_Alloc(uint32_t, 2)); + if (IsCmykImage()) { + m_pPalette.get()[0] = 0xff; + m_pPalette.get()[1] = 0; + } else { + m_pPalette.get()[0] = 0xff000000; + m_pPalette.get()[1] = 0xffffffff; + } + } else if (GetBPP() == 8) { + m_pPalette.reset(FX_Alloc(uint32_t, 256)); + if (IsCmykImage()) { + for (int i = 0; i < 256; ++i) + m_pPalette.get()[i] = 0xff - i; + } else { + for (int i = 0; i < 256; ++i) + m_pPalette.get()[i] = 0xff000000 | (i * 0x10101); + } + } +} + +bool CFX_DIBBase::BuildAlphaMask() { + if (m_pAlphaMask) + return true; + + m_pAlphaMask = pdfium::MakeRetain(); + if (!m_pAlphaMask->Create(m_Width, m_Height, FXDIB_8bppMask)) { + m_pAlphaMask = nullptr; + return false; + } + memset(m_pAlphaMask->GetBuffer(), 0xff, + m_pAlphaMask->GetHeight() * m_pAlphaMask->GetPitch()); + return true; +} + +uint32_t CFX_DIBBase::GetPaletteArgb(int index) const { + ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask()); + if (m_pPalette) + return m_pPalette.get()[index]; + + if (IsCmykImage()) { + if (GetBPP() == 1) + return index ? 0 : 0xff; + + return 0xff - index; + } + if (GetBPP() == 1) + return index ? 0xffffffff : 0xff000000; + + return index * 0x10101 | 0xff000000; +} + +void CFX_DIBBase::SetPaletteArgb(int index, uint32_t color) { + ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask()); + if (!m_pPalette) { + BuildPalette(); + } + m_pPalette.get()[index] = color; +} + +int CFX_DIBBase::FindPalette(uint32_t color) const { + ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask()); + if (!m_pPalette) { + if (IsCmykImage()) { + if (GetBPP() == 1) + return (static_cast(color) == 0xff) ? 0 : 1; + + return 0xff - static_cast(color); + } + if (GetBPP() == 1) + return (static_cast(color) == 0xff) ? 1 : 0; + + return static_cast(color); + } + int palsize = (1 << GetBPP()); + for (int i = 0; i < palsize; ++i) { + if (m_pPalette.get()[i] == color) + return i; + } + return -1; +} + +void CFX_DIBBase::GetOverlapRect(int& dest_left, + int& dest_top, + int& width, + int& height, + int src_width, + int src_height, + int& src_left, + int& src_top, + const CFX_ClipRgn* pClipRgn) { + if (width == 0 || height == 0) + return; + + ASSERT(width > 0 && height > 0); + if (dest_left > m_Width || dest_top > m_Height) { + width = 0; + height = 0; + return; + } + int x_offset = dest_left - src_left; + int y_offset = dest_top - src_top; + FX_RECT src_rect(src_left, src_top, src_left + width, src_top + height); + FX_RECT src_bound(0, 0, src_width, src_height); + src_rect.Intersect(src_bound); + FX_RECT dest_rect(src_rect.left + x_offset, src_rect.top + y_offset, + src_rect.right + x_offset, src_rect.bottom + y_offset); + FX_RECT dest_bound(0, 0, m_Width, m_Height); + dest_rect.Intersect(dest_bound); + if (pClipRgn) + dest_rect.Intersect(pClipRgn->GetBox()); + dest_left = dest_rect.left; + dest_top = dest_rect.top; + src_left = dest_left - x_offset; + src_top = dest_top - y_offset; + width = dest_rect.right - dest_rect.left; + height = dest_rect.bottom - dest_rect.top; +} + +void CFX_DIBBase::SetPalette(const uint32_t* pSrc) { + static const uint32_t kPaletteSize = 256; + if (!pSrc || GetBPP() > 8) { + m_pPalette.reset(); + 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); + memcpy(m_pPalette.get(), pSrc, pal_size * sizeof(uint32_t)); +} + +void CFX_DIBBase::GetPalette(uint32_t* pal, int alpha) const { + ASSERT(GetBPP() <= 8 && !IsCmykImage()); + if (GetBPP() == 1) { + pal[0] = ((m_pPalette ? m_pPalette.get()[0] : 0xff000000) & 0xffffff) | + (alpha << 24); + pal[1] = ((m_pPalette ? m_pPalette.get()[1] : 0xffffffff) & 0xffffff) | + (alpha << 24); + return; + } + if (m_pPalette) { + for (int i = 0; i < 256; ++i) + pal[i] = (m_pPalette.get()[i] & 0x00ffffff) | (alpha << 24); + } else { + for (int i = 0; i < 256; ++i) + pal[i] = (i * 0x10101) | (alpha << 24); + } +} + +RetainPtr CFX_DIBBase::CloneAlphaMask() const { + ASSERT(GetFormat() == FXDIB_Argb); + FX_RECT rect(0, 0, m_Width, m_Height); + auto pMask = pdfium::MakeRetain(); + 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 = pMask->GetWritableScanline(row - rect.top); + for (int col = rect.left; col < rect.right; ++col) { + *dest_scan++ = *src_scan; + src_scan += 4; + } + } + return pMask; +} + +bool CFX_DIBBase::SetAlphaMask(const RetainPtr& pAlphaMask, + const FX_RECT* pClip) { + if (!HasAlpha() || GetFormat() == FXDIB_Argb) + return false; + + if (!pAlphaMask) { + m_pAlphaMask->Clear(0xff000000); + return true; + } + 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; + } + for (int row = 0; row < m_Height; ++row) { + memcpy(m_pAlphaMask->GetWritableScanline(row), + pAlphaMask->GetScanline(row + rect.top) + rect.left, + m_pAlphaMask->m_Pitch); + } + return true; +} + +RetainPtr CFX_DIBBase::FlipImage(bool bXFlip, bool bYFlip) const { + auto pFlipped = pdfium::MakeRetain(); + if (!pFlipped->Create(m_Width, m_Height, GetFormat())) + return nullptr; + + pFlipped->SetPalette(m_pPalette.get()); + uint8_t* pDestBuffer = pFlipped->GetBuffer(); + int Bpp = m_bpp / 8; + for (int row = 0; row < m_Height; ++row) { + const uint8_t* src_scan = GetScanline(row); + uint8_t* dest_scan = + pDestBuffer + m_Pitch * (bYFlip ? (m_Height - row - 1) : row); + if (!bXFlip) { + memcpy(dest_scan, src_scan, m_Pitch); + continue; + } + if (m_bpp == 1) { + memset(dest_scan, 0, m_Pitch); + for (int col = 0; col < m_Width; ++col) + if (src_scan[col / 8] & (1 << (7 - col % 8))) { + int dest_col = m_Width - col - 1; + dest_scan[dest_col / 8] |= (1 << (7 - dest_col % 8)); + } + } else { + dest_scan += (m_Width - 1) * Bpp; + if (Bpp == 1) { + for (int col = 0; col < m_Width; ++col) { + *dest_scan = *src_scan; + --dest_scan; + ++src_scan; + } + } else if (Bpp == 3) { + for (int col = 0; col < m_Width; ++col) { + dest_scan[0] = src_scan[0]; + dest_scan[1] = src_scan[1]; + dest_scan[2] = src_scan[2]; + dest_scan -= 3; + src_scan += 3; + } + } else { + ASSERT(Bpp == 4); + for (int col = 0; col < m_Width; ++col) { + *(uint32_t*)dest_scan = *(uint32_t*)src_scan; + dest_scan -= 4; + src_scan += 4; + } + } + } + } + if (m_pAlphaMask) { + pDestBuffer = pFlipped->m_pAlphaMask->GetBuffer(); + uint32_t dest_pitch = pFlipped->m_pAlphaMask->GetPitch(); + for (int row = 0; row < m_Height; ++row) { + const uint8_t* src_scan = m_pAlphaMask->GetScanline(row); + uint8_t* dest_scan = + pDestBuffer + dest_pitch * (bYFlip ? (m_Height - row - 1) : row); + if (!bXFlip) { + memcpy(dest_scan, src_scan, dest_pitch); + continue; + } + dest_scan += (m_Width - 1); + for (int col = 0; col < m_Width; ++col) { + *dest_scan = *src_scan; + --dest_scan; + ++src_scan; + } + } + } + return pFlipped; +} + +RetainPtr CFX_DIBBase::CloneConvert(FXDIB_Format dest_format) { + if (dest_format == GetFormat()) + return Clone(nullptr); + + auto pClone = pdfium::MakeRetain(); + if (!pClone->Create(m_Width, m_Height, dest_format)) + return nullptr; + + RetainPtr pSrcAlpha; + if (HasAlpha()) { + pSrcAlpha = (GetFormat() == FXDIB_Argb) ? CloneAlphaMask() : m_pAlphaMask; + if (!pSrcAlpha) + return nullptr; + } + if (dest_format & 0x0200) { + bool ret; + if (dest_format == FXDIB_Argb) { + ret = pSrcAlpha ? pClone->LoadChannel(FXDIB_Alpha, pSrcAlpha, FXDIB_Alpha) + : pClone->LoadChannel(FXDIB_Alpha, 0xff); + } else { + ret = pClone->SetAlphaMask(pSrcAlpha, nullptr); + } + if (!ret) + return nullptr; + } + + RetainPtr holder(this); + std::unique_ptr pal_8bpp; + if (!ConvertBuffer(dest_format, pClone->GetBuffer(), pClone->GetPitch(), + m_Width, m_Height, holder, 0, 0, &pal_8bpp)) { + return nullptr; + } + if (pal_8bpp) + pClone->SetPalette(pal_8bpp.get()); + + return pClone; +} + +RetainPtr CFX_DIBBase::SwapXY(bool bXFlip, bool bYFlip) const { + FX_RECT dest_clip(0, 0, m_Height, m_Width); + if (dest_clip.IsEmpty()) + return nullptr; + + auto pTransBitmap = pdfium::MakeRetain(); + int result_height = dest_clip.Height(); + int result_width = dest_clip.Width(); + if (!pTransBitmap->Create(result_width, result_height, GetFormat())) + return nullptr; + + 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; + int row_end = bXFlip ? m_Height - dest_clip.left : dest_clip.right; + int col_start = bYFlip ? m_Width - dest_clip.bottom : dest_clip.top; + int col_end = bYFlip ? m_Width - dest_clip.top : dest_clip.bottom; + if (GetBPP() == 1) { + memset(dest_buf, 0xff, dest_pitch * result_height); + for (int row = row_start; row < row_end; ++row) { + const uint8_t* src_scan = GetScanline(row); + int dest_col = (bXFlip ? dest_clip.right - (row - row_start) - 1 : row) - + dest_clip.left; + uint8_t* dest_scan = dest_buf; + if (bYFlip) + dest_scan += (result_height - 1) * dest_pitch; + int dest_step = bYFlip ? -dest_pitch : dest_pitch; + for (int col = col_start; col < col_end; ++col) { + if (!(src_scan[col / 8] & (1 << (7 - col % 8)))) + dest_scan[dest_col / 8] &= ~(1 << (7 - dest_col % 8)); + dest_scan += dest_step; + } + } + } else { + int nBytes = GetBPP() / 8; + int dest_step = bYFlip ? -dest_pitch : dest_pitch; + if (nBytes == 3) + dest_step -= 2; + for (int row = row_start; row < row_end; ++row) { + int dest_col = (bXFlip ? dest_clip.right - (row - row_start) - 1 : row) - + dest_clip.left; + uint8_t* dest_scan = dest_buf + dest_col * nBytes; + if (bYFlip) + dest_scan += (result_height - 1) * dest_pitch; + if (nBytes == 4) { + const uint32_t* src_scan = + reinterpret_cast(GetScanline(row)) + col_start; + for (int col = col_start; col < col_end; ++col) { + *(uint32_t*)dest_scan = *src_scan++; + dest_scan += dest_step; + } + } else { + const uint8_t* src_scan = GetScanline(row) + col_start * nBytes; + if (nBytes == 1) { + for (int col = col_start; col < col_end; ++col) { + *dest_scan = *src_scan++; + dest_scan += dest_step; + } + } else { + for (int col = col_start; col < col_end; ++col) { + *dest_scan++ = *src_scan++; + *dest_scan++ = *src_scan++; + *dest_scan = *src_scan++; + dest_scan += dest_step; + } + } + } + } + } + if (m_pAlphaMask) { + dest_pitch = pTransBitmap->m_pAlphaMask->GetPitch(); + dest_buf = pTransBitmap->m_pAlphaMask->GetBuffer(); + int dest_step = bYFlip ? -dest_pitch : dest_pitch; + for (int row = row_start; row < row_end; ++row) { + int dest_col = (bXFlip ? dest_clip.right - (row - row_start) - 1 : row) - + dest_clip.left; + uint8_t* dest_scan = dest_buf + dest_col; + if (bYFlip) + dest_scan += (result_height - 1) * dest_pitch; + const uint8_t* src_scan = m_pAlphaMask->GetScanline(row) + col_start; + for (int col = col_start; col < col_end; ++col) { + *dest_scan = *src_scan++; + dest_scan += dest_step; + } + } + } + return pTransBitmap; +} + +RetainPtr CFX_DIBBase::TransformTo(const CFX_Matrix* pDestMatrix, + int* result_left, + int* result_top) { + RetainPtr holder(this); + CFX_ImageTransformer transformer(holder, pDestMatrix, 0, nullptr); + transformer.Continue(nullptr); + *result_left = transformer.result().left; + *result_top = transformer.result().top; + return transformer.DetachBitmap(); +} + +RetainPtr CFX_DIBBase::StretchTo(int dest_width, + int dest_height, + uint32_t flags, + const FX_RECT* pClip) { + RetainPtr holder(this); + FX_RECT clip_rect(0, 0, abs(dest_width), abs(dest_height)); + if (pClip) + clip_rect.Intersect(*pClip); + + if (clip_rect.IsEmpty()) + return nullptr; + + if (dest_width == m_Width && dest_height == m_Height) + return Clone(&clip_rect); + + CFX_BitmapStorer storer; + CFX_ImageStretcher stretcher(&storer, holder, dest_width, dest_height, + clip_rect, flags); + if (stretcher.Start()) + stretcher.Continue(nullptr); + + return storer.Detach(); +} + +// static +bool CFX_DIBBase::ConvertBuffer( + FXDIB_Format dest_format, + uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top, + std::unique_ptr* p_pal) { + FXDIB_Format src_format = pSrcBitmap->GetFormat(); + const int bpp = GetBppFromFormat(src_format); + switch (dest_format) { + case FXDIB_8bppMask: { + return ConvertBuffer_8bppMask(bpp, dest_buf, dest_pitch, width, height, + pSrcBitmap, src_left, src_top); + } + case FXDIB_8bppRgb: + case FXDIB_8bppRgba: { + const bool bpp_1_or_8 = (bpp == 1 || bpp == 8); + if (bpp_1_or_8 && !pSrcBitmap->GetPalette()) { + return ConvertBuffer(FXDIB_8bppMask, dest_buf, dest_pitch, width, + height, pSrcBitmap, src_left, src_top, p_pal); + } + p_pal->reset(FX_Alloc(uint32_t, 256)); + if (bpp_1_or_8 && pSrcBitmap->GetPalette()) { + ConvertBuffer_Plt2PltRgb8(dest_buf, dest_pitch, width, height, + pSrcBitmap, src_left, src_top, p_pal->get()); + return true; + } + if (bpp >= 24) { + ConvertBuffer_Rgb2PltRgb8(dest_buf, dest_pitch, width, height, + pSrcBitmap, src_left, src_top, p_pal->get()); + return true; + } + return false; + } + case FXDIB_Rgb: + case FXDIB_Rgba: { + return ConvertBuffer_Rgb(bpp, dest_format, dest_buf, dest_pitch, width, + height, pSrcBitmap, src_left, src_top); + } + case FXDIB_Argb: + case FXDIB_Rgb32: { + const bool cmyk = src_format & 0x0400; + return ConvertBuffer_Argb(bpp, cmyk, dest_format, dest_buf, dest_pitch, + width, height, pSrcBitmap, src_left, src_top); + } + default: + NOTREACHED(); + return false; + } +} diff --git a/core/fxge/dib/cfx_dibbase.h b/core/fxge/dib/cfx_dibbase.h new file mode 100644 index 0000000000..ef35fc86e7 --- /dev/null +++ b/core/fxge/dib/cfx_dibbase.h @@ -0,0 +1,140 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXGE_DIB_CFX_DIBBASE_H_ +#define CORE_FXGE_DIB_CFX_DIBBASE_H_ + +#include + +#include "core/fxcrt/fx_coordinates.h" +#include "core/fxcrt/fx_memory.h" +#include "core/fxcrt/retain_ptr.h" +#include "core/fxge/fx_dib.h" + +enum FXDIB_Channel { + FXDIB_Red = 1, + FXDIB_Green, + FXDIB_Blue, + FXDIB_Cyan, + FXDIB_Magenta, + FXDIB_Yellow, + FXDIB_Black, + FXDIB_Alpha +}; + +class CFX_ClipRgn; +class CFX_DIBitmap; +class PauseIndicatorIface; + +// Base class for all Device-Indepenent Bitmaps. +class CFX_DIBBase : public Retainable { + public: + ~CFX_DIBBase() override; + + virtual uint8_t* GetBuffer() const; + virtual const uint8_t* GetScanline(int line) const = 0; + virtual bool SkipToScanline(int line, PauseIndicatorIface* pPause) const; + virtual void DownSampleScanline(int line, + uint8_t* dest_scan, + int dest_bpp, + int dest_width, + bool bFlipX, + int clip_left, + int clip_width) const = 0; + + uint8_t* GetWritableScanline(int line) { + return const_cast(GetScanline(line)); + } + int GetWidth() const { return m_Width; } + int GetHeight() const { return m_Height; } + + FXDIB_Format GetFormat() const { + return static_cast(m_AlphaFlag * 0x100 + m_bpp); + } + uint32_t GetPitch() const { return m_Pitch; } + uint32_t* GetPalette() const { return m_pPalette.get(); } + int GetBPP() const { return m_bpp; } + + // TODO(thestig): Investigate this. Given the possible values of FXDIB_Format, + // it feels as though this should be implemented as !!(m_AlphaFlag & 1) and + // IsOpaqueImage() below should never be able to return true. + bool IsAlphaMask() const { return m_AlphaFlag == 1; } + bool HasAlpha() const { return !!(m_AlphaFlag & 2); } + bool IsOpaqueImage() const { return !(m_AlphaFlag & 3); } + bool IsCmykImage() const { return !!(m_AlphaFlag & 4); } + + int GetPaletteSize() const { + return IsAlphaMask() ? 0 : (m_bpp == 1 ? 2 : (m_bpp == 8 ? 256 : 0)); + } + + uint32_t GetPaletteArgb(int index) const; + void SetPaletteArgb(int index, uint32_t color); + + // Copies into internally-owned palette. + void SetPalette(const uint32_t* pSrcPal); + + RetainPtr Clone(const FX_RECT* pClip) const; + RetainPtr CloneConvert(FXDIB_Format format); + RetainPtr StretchTo(int dest_width, + int dest_height, + uint32_t flags, + const FX_RECT* pClip); + RetainPtr TransformTo(const CFX_Matrix* pMatrix, + int* left, + int* top); + RetainPtr SwapXY(bool bXFlip, bool bYFlip) const; + RetainPtr FlipImage(bool bXFlip, bool bYFlip) const; + + RetainPtr CloneAlphaMask() const; + + // Copies into internally-owned mask. + bool SetAlphaMask(const RetainPtr& pAlphaMask, + const FX_RECT* pClip); + + void GetOverlapRect(int& dest_left, + int& dest_top, + int& width, + int& height, + int src_width, + int src_height, + int& src_left, + int& src_top, + const CFX_ClipRgn* pClipRgn); + +#if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_ + void DebugVerifyBitmapIsPreMultiplied(void* buffer) const; +#endif + + RetainPtr m_pAlphaMask; + + protected: + CFX_DIBBase(); + + static bool ConvertBuffer(FXDIB_Format dest_format, + uint8_t* dest_buf, + int dest_pitch, + int width, + int height, + const RetainPtr& pSrcBitmap, + int src_left, + int src_top, + std::unique_ptr* pal); + + void BuildPalette(); + bool BuildAlphaMask(); + int FindPalette(uint32_t color) const; + void GetPalette(uint32_t* pal, int alpha) const; + + int m_Width; + int m_Height; + int m_bpp; + uint32_t m_AlphaFlag; + uint32_t m_Pitch; + // TODO(weili): Use std::vector for this. + std::unique_ptr m_pPalette; +}; + +#endif // CORE_FXGE_DIB_CFX_DIBBASE_H_ diff --git a/core/fxge/dib/cfx_dibextractor.cpp b/core/fxge/dib/cfx_dibextractor.cpp index 9fe3114b1f..8211dc725a 100644 --- a/core/fxge/dib/cfx_dibextractor.cpp +++ b/core/fxge/dib/cfx_dibextractor.cpp @@ -6,14 +6,14 @@ #include "core/fxge/dib/cfx_dibextractor.h" -#include "core/fxge/dib/cfx_dibsource.h" +#include "core/fxge/dib/cfx_dibbase.h" -CFX_DIBExtractor::CFX_DIBExtractor(const RetainPtr& pSrc) { +CFX_DIBExtractor::CFX_DIBExtractor(const RetainPtr& pSrc) { if (!pSrc->GetBuffer()) { m_pBitmap = pSrc->Clone(nullptr); return; } - RetainPtr pOldSrc(pSrc); + RetainPtr pOldSrc(pSrc); m_pBitmap = pdfium::MakeRetain(); if (!m_pBitmap->Create(pOldSrc->GetWidth(), pOldSrc->GetHeight(), pOldSrc->GetFormat(), pOldSrc->GetBuffer(), 0)) { diff --git a/core/fxge/dib/cfx_dibextractor.h b/core/fxge/dib/cfx_dibextractor.h index b6c27a7bd7..cd97f20aac 100644 --- a/core/fxge/dib/cfx_dibextractor.h +++ b/core/fxge/dib/cfx_dibextractor.h @@ -10,11 +10,11 @@ #include "core/fxcrt/retain_ptr.h" #include "core/fxge/dib/cfx_dibitmap.h" -class CFX_DIBSource; +class CFX_DIBBase; class CFX_DIBExtractor { public: - explicit CFX_DIBExtractor(const RetainPtr& pSrc); + explicit CFX_DIBExtractor(const RetainPtr& pSrc); ~CFX_DIBExtractor(); RetainPtr GetBitmap() { return m_pBitmap; } diff --git a/core/fxge/dib/cfx_dibitmap.cpp b/core/fxge/dib/cfx_dibitmap.cpp index 2c99bd16e0..89cfbb68d3 100644 --- a/core/fxge/dib/cfx_dibitmap.cpp +++ b/core/fxge/dib/cfx_dibitmap.cpp @@ -85,7 +85,7 @@ bool CFX_DIBitmap::Create(int width, return false; } -bool CFX_DIBitmap::Copy(const RetainPtr& pSrc) { +bool CFX_DIBitmap::Copy(const RetainPtr& pSrc) { if (m_pBuffer) return false; @@ -188,7 +188,7 @@ bool CFX_DIBitmap::TransferBitmap(int dest_left, int dest_top, int width, int height, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, int src_left, int src_top) { if (!m_pBuffer) @@ -223,7 +223,7 @@ bool CFX_DIBitmap::TransferWithUnequalFormats( int dest_top, int width, int height, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, int src_left, int src_top) { if (m_pPalette) @@ -247,7 +247,7 @@ void CFX_DIBitmap::TransferWithMultipleBPP( int dest_top, int width, int height, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, int src_left, int src_top) { int Bpp = GetBPP() / 8; @@ -265,7 +265,7 @@ void CFX_DIBitmap::TransferEqualFormatsOneBPP( int dest_top, int width, int height, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, int src_left, int src_top) { for (int row = 0; row < height; ++row) { @@ -283,12 +283,12 @@ void CFX_DIBitmap::TransferEqualFormatsOneBPP( } bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, FXDIB_Channel srcChannel) { if (!m_pBuffer) return false; - RetainPtr pSrcClone = pSrcBitmap; + RetainPtr pSrcClone = pSrcBitmap; int srcOffset; if (srcChannel == FXDIB_Alpha) { if (!pSrcBitmap->HasAlpha() && !pSrcBitmap->IsAlphaMask()) @@ -348,7 +348,7 @@ bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, destOffset = kChannelOffset[destChannel]; } if (srcChannel == FXDIB_Alpha && pSrcClone->m_pAlphaMask) { - RetainPtr pAlphaMask = pSrcClone->m_pAlphaMask; + RetainPtr pAlphaMask = pSrcClone->m_pAlphaMask; if (pSrcClone->GetWidth() != m_Width || pSrcClone->GetHeight() != m_Height) { if (pAlphaMask) { @@ -446,7 +446,7 @@ bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, int value) { return true; } -bool CFX_DIBitmap::MultiplyAlpha(const RetainPtr& pSrcBitmap) { +bool CFX_DIBitmap::MultiplyAlpha(const RetainPtr& pSrcBitmap) { if (!m_pBuffer) return false; @@ -881,7 +881,7 @@ bool CFX_DIBitmap::CompositeBitmap(int dest_left, int dest_top, int width, int height, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, int src_left, int src_top, int blend_type, @@ -952,7 +952,7 @@ bool CFX_DIBitmap::CompositeMask(int dest_left, int dest_top, int width, int height, - const RetainPtr& pMask, + const RetainPtr& pMask, uint32_t color, int src_left, int src_top, @@ -1266,7 +1266,7 @@ bool CFX_DIBitmap::ConvertFormat(FXDIB_Format dest_format) { } } bool ret = false; - RetainPtr holder(this); + RetainPtr holder(this); std::unique_ptr pal_8bpp; ret = ConvertBuffer(dest_format, dest_buf.get(), dest_pitch, m_Width, m_Height, holder, 0, 0, &pal_8bpp); diff --git a/core/fxge/dib/cfx_dibitmap.h b/core/fxge/dib/cfx_dibitmap.h index d9afee6800..dc6720ed96 100644 --- a/core/fxge/dib/cfx_dibitmap.h +++ b/core/fxge/dib/cfx_dibitmap.h @@ -10,10 +10,10 @@ #include "core/fxcrt/fx_coordinates.h" #include "core/fxcrt/maybe_owned.h" #include "core/fxcrt/retain_ptr.h" -#include "core/fxge/dib/cfx_dibsource.h" +#include "core/fxge/dib/cfx_dibbase.h" #include "third_party/base/stl_util.h" -class CFX_DIBitmap : public CFX_DIBSource { +class CFX_DIBitmap : public CFX_DIBBase { public: template friend RetainPtr pdfium::MakeRetain(Args&&... args); @@ -26,9 +26,9 @@ class CFX_DIBitmap : public CFX_DIBSource { uint8_t* pBuffer, uint32_t pitch); - bool Copy(const RetainPtr& pSrc); + bool Copy(const RetainPtr& pSrc); - // CFX_DIBSource + // CFX_DIBBase uint8_t* GetBuffer() const override; const uint8_t* GetScanline(int line) const override; void DownSampleScanline(int line, @@ -47,18 +47,18 @@ class CFX_DIBitmap : public CFX_DIBSource { void SetPixel(int x, int y, uint32_t color); bool LoadChannel(FXDIB_Channel destChannel, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, FXDIB_Channel srcChannel); bool LoadChannel(FXDIB_Channel destChannel, int value); bool MultiplyAlpha(int alpha); - bool MultiplyAlpha(const RetainPtr& pAlphaMask); + bool MultiplyAlpha(const RetainPtr& pAlphaMask); bool TransferBitmap(int dest_left, int dest_top, int width, int height, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, int src_left, int src_top); @@ -66,7 +66,7 @@ class CFX_DIBitmap : public CFX_DIBSource { int dest_top, int width, int height, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, int src_left, int src_top, int blend_type, @@ -77,7 +77,7 @@ class CFX_DIBitmap : public CFX_DIBSource { int dest_top, int width, int height, - const RetainPtr& pMask, + const RetainPtr& pMask, uint32_t color, int src_left, int src_top, @@ -130,21 +130,21 @@ class CFX_DIBitmap : public CFX_DIBSource { int dest_top, int width, int height, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, int src_left, int src_top); void TransferWithMultipleBPP(int dest_left, int dest_top, int width, int height, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, int src_left, int src_top); void TransferEqualFormatsOneBPP(int dest_left, int dest_top, int width, int height, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, int src_left, int src_top); }; diff --git a/core/fxge/dib/cfx_dibsource.cpp b/core/fxge/dib/cfx_dibsource.cpp deleted file mode 100644 index 4608b51802..0000000000 --- a/core/fxge/dib/cfx_dibsource.cpp +++ /dev/null @@ -1,1262 +0,0 @@ -// Copyright 2017 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "core/fxge/dib/cfx_dibsource.h" - -#include -#include -#include -#include - -#include "core/fxcodec/fx_codec.h" -#include "core/fxge/cfx_cliprgn.h" -#include "core/fxge/dib/cfx_bitmapstorer.h" -#include "core/fxge/dib/cfx_dibitmap.h" -#include "core/fxge/dib/cfx_imagestretcher.h" -#include "core/fxge/dib/cfx_imagetransformer.h" -#include "third_party/base/logging.h" -#include "third_party/base/ptr_util.h" - -namespace { - -void ColorDecode(uint32_t pal_v, uint8_t* r, uint8_t* g, uint8_t* b) { - *r = static_cast((pal_v & 0xf00) >> 4); - *g = static_cast(pal_v & 0x0f0); - *b = static_cast((pal_v & 0x00f) << 4); -} - -void Obtain_Pal(std::pair* luts, - uint32_t* dest_pal, - uint32_t lut) { - uint32_t lut_1 = lut - 1; - for (int row = 0; row < 256; ++row) { - int lut_offset = lut_1 - row; - if (lut_offset < 0) - lut_offset += 256; - uint32_t color = luts[lut_offset].second; - uint8_t r; - uint8_t g; - uint8_t b; - ColorDecode(color, &r, &g, &b); - dest_pal[row] = (static_cast(r) << 16) | - (static_cast(g) << 8) | b | 0xff000000; - luts[lut_offset].first = row; - } -} - -class CFX_Palette { - public: - explicit CFX_Palette(const RetainPtr& pBitmap); - ~CFX_Palette(); - - const uint32_t* GetPalette() { return m_Palette.data(); } - const std::pair* GetLuts() const { return m_Luts.data(); } - int32_t GetLutCount() const { return m_lut; } - void SetAmountLut(int row, uint32_t value) { m_Luts[row].first = value; } - - private: - std::vector m_Palette; - // (Amount, Color) pairs - std::vector> m_Luts; - int m_lut; -}; - -CFX_Palette::CFX_Palette(const RetainPtr& pBitmap) - : m_Palette(256), m_Luts(4096), m_lut(0) { - int bpp = pBitmap->GetBPP() / 8; - int width = pBitmap->GetWidth(); - int height = pBitmap->GetHeight(); - for (int row = 0; row < height; ++row) { - const uint8_t* scan_line = pBitmap->GetScanline(row); - for (int col = 0; col < width; ++col) { - const uint8_t* src_port = scan_line + col * bpp; - uint32_t b = src_port[0] & 0xf0; - uint32_t g = src_port[1] & 0xf0; - uint32_t r = src_port[2] & 0xf0; - uint32_t index = (r << 4) + g + (b >> 4); - ++m_Luts[index].first; - } - } - // Move non-zeros to the front and count them - for (int row = 0; row < 4096; ++row) { - if (m_Luts[row].first != 0) { - m_Luts[m_lut].first = m_Luts[row].first; - m_Luts[m_lut].second = row; - ++m_lut; - } - } - std::sort(m_Luts.begin(), m_Luts.begin() + m_lut, - [](const std::pair& arg1, - const std::pair& arg2) { - return arg1.first < arg2.first; - }); - Obtain_Pal(m_Luts.data(), m_Palette.data(), m_lut); -} - -CFX_Palette::~CFX_Palette() {} - -void ConvertBuffer_1bppMask2Gray(uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - static constexpr uint8_t kSetGray = 0xff; - static constexpr uint8_t kResetGray = 0x00; - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - memset(dest_scan, kResetGray, width); - const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row); - for (int col = src_left; col < src_left + width; ++col) { - if (src_scan[col / 8] & (1 << (7 - col % 8))) - *dest_scan = kSetGray; - ++dest_scan; - } - } -} - -void ConvertBuffer_8bppMask2Gray(uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left; - memcpy(dest_scan, src_scan, width); - } -} - -void ConvertBuffer_1bppPlt2Gray(uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - uint32_t* src_plt = pSrcBitmap->GetPalette(); - uint8_t gray[2]; - uint8_t reset_r; - uint8_t reset_g; - uint8_t reset_b; - uint8_t set_r; - uint8_t set_g; - uint8_t set_b; - if (pSrcBitmap->IsCmykImage()) { - std::tie(reset_r, reset_g, reset_b) = AdobeCMYK_to_sRGB1( - FXSYS_GetCValue(src_plt[0]), FXSYS_GetMValue(src_plt[0]), - FXSYS_GetYValue(src_plt[0]), FXSYS_GetKValue(src_plt[0])); - std::tie(set_r, set_g, set_b) = AdobeCMYK_to_sRGB1( - FXSYS_GetCValue(src_plt[1]), FXSYS_GetMValue(src_plt[1]), - FXSYS_GetYValue(src_plt[1]), FXSYS_GetKValue(src_plt[1])); - } else { - reset_r = FXARGB_R(src_plt[0]); - reset_g = FXARGB_G(src_plt[0]); - reset_b = FXARGB_B(src_plt[0]); - set_r = FXARGB_R(src_plt[1]); - set_g = FXARGB_G(src_plt[1]); - set_b = FXARGB_B(src_plt[1]); - } - gray[0] = FXRGB2GRAY(reset_r, reset_g, reset_b); - gray[1] = FXRGB2GRAY(set_r, set_g, set_b); - - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - memset(dest_scan, gray[0], width); - const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row); - for (int col = src_left; col < src_left + width; ++col) { - if (src_scan[col / 8] & (1 << (7 - col % 8))) - *dest_scan = gray[1]; - ++dest_scan; - } - } -} - -void ConvertBuffer_8bppPlt2Gray(uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - uint32_t* src_plt = pSrcBitmap->GetPalette(); - uint8_t gray[256]; - if (pSrcBitmap->IsCmykImage()) { - uint8_t r; - uint8_t g; - uint8_t b; - for (size_t i = 0; i < FX_ArraySize(gray); ++i) { - std::tie(r, g, b) = AdobeCMYK_to_sRGB1( - FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]), - FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i])); - gray[i] = FXRGB2GRAY(r, g, b); - } - } else { - for (size_t i = 0; i < FX_ArraySize(gray); ++i) { - gray[i] = FXRGB2GRAY(FXARGB_R(src_plt[i]), FXARGB_G(src_plt[i]), - FXARGB_B(src_plt[i])); - } - } - - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left; - for (int col = 0; col < width; ++col) - *dest_scan++ = gray[*src_scan++]; - } -} - -void ConvertBuffer_RgbOrCmyk2Gray(uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - int Bpp = pSrcBitmap->GetBPP() / 8; - if (pSrcBitmap->IsCmykImage()) { - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - const uint8_t* src_scan = - pSrcBitmap->GetScanline(src_top + row) + src_left * 4; - for (int col = 0; col < width; ++col) { - uint8_t r; - uint8_t g; - uint8_t b; - std::tie(r, g, b) = AdobeCMYK_to_sRGB1( - FXSYS_GetCValue(static_cast(src_scan[0])), - FXSYS_GetMValue(static_cast(src_scan[1])), - FXSYS_GetYValue(static_cast(src_scan[2])), - FXSYS_GetKValue(static_cast(src_scan[3]))); - *dest_scan++ = FXRGB2GRAY(r, g, b); - src_scan += 4; - } - } - } else { - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - const uint8_t* src_scan = - pSrcBitmap->GetScanline(src_top + row) + src_left * Bpp; - for (int col = 0; col < width; ++col) { - *dest_scan++ = FXRGB2GRAY(src_scan[2], src_scan[1], src_scan[0]); - src_scan += Bpp; - } - } - } -} - -void ConvertBuffer_IndexCopy(uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - if (pSrcBitmap->GetBPP() == 1) { - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - // Set all destination pixels to be white initially. - memset(dest_scan, 255, width); - const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row); - for (int col = src_left; col < src_left + width; ++col) { - // If the source bit is set, then set the destination pixel to be black. - if (src_scan[col / 8] & (1 << (7 - col % 8))) - *dest_scan = 0; - - ++dest_scan; - } - } - } else { - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - const uint8_t* src_scan = - pSrcBitmap->GetScanline(src_top + row) + src_left; - memcpy(dest_scan, src_scan, width); - } - } -} - -void ConvertBuffer_Plt2PltRgb8(uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top, - uint32_t* dst_plt) { - ConvertBuffer_IndexCopy(dest_buf, dest_pitch, width, height, pSrcBitmap, - src_left, src_top); - uint32_t* src_plt = pSrcBitmap->GetPalette(); - int plt_size = pSrcBitmap->GetPaletteSize(); - if (pSrcBitmap->IsCmykImage()) { - for (int i = 0; i < plt_size; ++i) { - uint8_t r; - uint8_t g; - uint8_t b; - std::tie(r, g, b) = AdobeCMYK_to_sRGB1( - FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]), - FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i])); - dst_plt[i] = ArgbEncode(0xff, r, g, b); - } - } else { - memcpy(dst_plt, src_plt, plt_size * 4); - } -} - -void ConvertBuffer_Rgb2PltRgb8(uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top, - uint32_t* dst_plt) { - int bpp = pSrcBitmap->GetBPP() / 8; - CFX_Palette palette(pSrcBitmap); - const std::pair* Luts = palette.GetLuts(); - int lut = palette.GetLutCount(); - const uint32_t* pal = palette.GetPalette(); - if (lut > 256) { - int err; - int min_err; - int lut_256 = lut - 256; - for (int row = 0; row < lut_256; ++row) { - min_err = 1000000; - uint8_t r; - uint8_t g; - uint8_t b; - ColorDecode(Luts[row].second, &r, &g, &b); - uint32_t clrindex = 0; - for (int col = 0; col < 256; ++col) { - uint32_t p_color = pal[col]; - int d_r = r - static_cast(p_color >> 16); - int d_g = g - static_cast(p_color >> 8); - int d_b = b - static_cast(p_color); - err = d_r * d_r + d_g * d_g + d_b * d_b; - if (err < min_err) { - min_err = err; - clrindex = col; - } - } - palette.SetAmountLut(row, clrindex); - } - } - int32_t lut_1 = lut - 1; - for (int row = 0; row < height; ++row) { - const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left; - uint8_t* dest_scan = dest_buf + row * dest_pitch; - for (int col = 0; col < width; ++col) { - const uint8_t* src_port = src_scan + col * bpp; - int r = src_port[2] & 0xf0; - int g = src_port[1] & 0xf0; - int b = src_port[0] & 0xf0; - uint32_t clrindex = (r << 4) + g + (b >> 4); - for (int i = lut_1; i >= 0; --i) - if (clrindex == Luts[i].second) { - *(dest_scan + col) = static_cast(Luts[i].first); - break; - } - } - } - memcpy(dst_plt, pal, sizeof(uint32_t) * 256); -} - -void ConvertBuffer_1bppMask2Rgb(FXDIB_Format dest_format, - uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - int comps = GetCompsFromFormat(dest_format); - static constexpr uint8_t kSetGray = 0xff; - static constexpr uint8_t kResetGray = 0x00; - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row); - for (int col = src_left; col < src_left + width; ++col) { - if (src_scan[col / 8] & (1 << (7 - col % 8))) { - dest_scan[0] = kSetGray; - dest_scan[1] = kSetGray; - dest_scan[2] = kSetGray; - } else { - dest_scan[0] = kResetGray; - dest_scan[1] = kResetGray; - dest_scan[2] = kResetGray; - } - dest_scan += comps; - } - } -} - -void ConvertBuffer_8bppMask2Rgb(FXDIB_Format dest_format, - uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - int comps = GetCompsFromFormat(dest_format); - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left; - uint8_t src_pixel; - for (int col = 0; col < width; ++col) { - src_pixel = *src_scan++; - *dest_scan++ = src_pixel; - *dest_scan++ = src_pixel; - *dest_scan = src_pixel; - dest_scan += comps - 2; - } - } -} - -void ConvertBuffer_1bppPlt2Rgb(FXDIB_Format dest_format, - uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - int comps = GetCompsFromFormat(dest_format); - uint32_t* src_plt = pSrcBitmap->GetPalette(); - uint32_t plt[2]; - uint8_t* bgr_ptr = reinterpret_cast(plt); - if (pSrcBitmap->IsCmykImage()) { - plt[0] = FXCMYK_TODIB(src_plt[0]); - plt[1] = FXCMYK_TODIB(src_plt[1]); - } else { - bgr_ptr[0] = FXARGB_B(src_plt[0]); - bgr_ptr[1] = FXARGB_G(src_plt[0]); - bgr_ptr[2] = FXARGB_R(src_plt[0]); - bgr_ptr[3] = FXARGB_B(src_plt[1]); - bgr_ptr[4] = FXARGB_G(src_plt[1]); - bgr_ptr[5] = FXARGB_R(src_plt[1]); - } - - if (pSrcBitmap->IsCmykImage()) { - std::tie(bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]) = AdobeCMYK_to_sRGB1( - FXSYS_GetCValue(src_plt[0]), FXSYS_GetMValue(src_plt[0]), - FXSYS_GetYValue(src_plt[0]), FXSYS_GetKValue(src_plt[0])); - std::tie(bgr_ptr[5], bgr_ptr[4], bgr_ptr[3]) = AdobeCMYK_to_sRGB1( - FXSYS_GetCValue(src_plt[1]), FXSYS_GetMValue(src_plt[1]), - FXSYS_GetYValue(src_plt[1]), FXSYS_GetKValue(src_plt[1])); - } - - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row); - for (int col = src_left; col < src_left + width; ++col) { - if (src_scan[col / 8] & (1 << (7 - col % 8))) { - *dest_scan++ = bgr_ptr[3]; - *dest_scan++ = bgr_ptr[4]; - *dest_scan = bgr_ptr[5]; - } else { - *dest_scan++ = bgr_ptr[0]; - *dest_scan++ = bgr_ptr[1]; - *dest_scan = bgr_ptr[2]; - } - dest_scan += comps - 2; - } - } -} - -void ConvertBuffer_8bppPlt2Rgb(FXDIB_Format dest_format, - uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - int comps = GetCompsFromFormat(dest_format); - uint32_t* src_plt = pSrcBitmap->GetPalette(); - uint32_t plt[256]; - uint8_t* bgr_ptr = reinterpret_cast(plt); - if (!pSrcBitmap->IsCmykImage()) { - for (int i = 0; i < 256; ++i) { - *bgr_ptr++ = FXARGB_B(src_plt[i]); - *bgr_ptr++ = FXARGB_G(src_plt[i]); - *bgr_ptr++ = FXARGB_R(src_plt[i]); - } - bgr_ptr = reinterpret_cast(plt); - } - - if (pSrcBitmap->IsCmykImage()) { - for (int i = 0; i < 256; ++i) { - std::tie(bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]) = AdobeCMYK_to_sRGB1( - FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]), - FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i])); - bgr_ptr += 3; - } - bgr_ptr = reinterpret_cast(plt); - } - - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left; - for (int col = 0; col < width; ++col) { - uint8_t* src_pixel = bgr_ptr + 3 * (*src_scan++); - *dest_scan++ = *src_pixel++; - *dest_scan++ = *src_pixel++; - *dest_scan = *src_pixel++; - dest_scan += comps - 2; - } - } -} - -void ConvertBuffer_24bppRgb2Rgb24(uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - const uint8_t* src_scan = - pSrcBitmap->GetScanline(src_top + row) + src_left * 3; - memcpy(dest_scan, src_scan, width * 3); - } -} - -void ConvertBuffer_32bppRgb2Rgb24(uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - const uint8_t* src_scan = - pSrcBitmap->GetScanline(src_top + row) + src_left * 4; - for (int col = 0; col < width; ++col) { - *dest_scan++ = *src_scan++; - *dest_scan++ = *src_scan++; - *dest_scan++ = *src_scan++; - ++src_scan; - } - } -} - -void ConvertBuffer_Rgb2Rgb32(uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - int comps = pSrcBitmap->GetBPP() / 8; - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - const uint8_t* src_scan = - pSrcBitmap->GetScanline(src_top + row) + src_left * comps; - for (int col = 0; col < width; ++col) { - *dest_scan++ = *src_scan++; - *dest_scan++ = *src_scan++; - *dest_scan++ = *src_scan++; - ++dest_scan; - src_scan += comps - 3; - } - } -} - -void ConvertBuffer_32bppCmyk2Rgb32(uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - for (int row = 0; row < height; ++row) { - uint8_t* dest_scan = dest_buf + row * dest_pitch; - const uint8_t* src_scan = - pSrcBitmap->GetScanline(src_top + row) + src_left * 4; - for (int col = 0; col < width; ++col) { - std::tie(dest_scan[2], dest_scan[1], dest_scan[0]) = AdobeCMYK_to_sRGB1( - src_scan[0], src_scan[1], src_scan[2], src_scan[3]); - dest_scan += 4; - src_scan += 4; - } - } -} - -bool ConvertBuffer_8bppMask(int bpp, - uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - switch (bpp) { - case 1: - if (pSrcBitmap->GetPalette()) { - ConvertBuffer_1bppPlt2Gray(dest_buf, dest_pitch, width, height, - pSrcBitmap, src_left, src_top); - } else { - ConvertBuffer_1bppMask2Gray(dest_buf, dest_pitch, width, height, - pSrcBitmap, src_left, src_top); - } - return true; - case 8: - if (pSrcBitmap->GetPalette()) { - ConvertBuffer_8bppPlt2Gray(dest_buf, dest_pitch, width, height, - pSrcBitmap, src_left, src_top); - } else { - ConvertBuffer_8bppMask2Gray(dest_buf, dest_pitch, width, height, - pSrcBitmap, src_left, src_top); - } - return true; - case 24: - case 32: - ConvertBuffer_RgbOrCmyk2Gray(dest_buf, dest_pitch, width, height, - pSrcBitmap, src_left, src_top); - return true; - default: - return false; - } -} - -bool ConvertBuffer_Rgb(int bpp, - FXDIB_Format dest_format, - uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - switch (bpp) { - case 1: - if (pSrcBitmap->GetPalette()) { - ConvertBuffer_1bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, - height, pSrcBitmap, src_left, src_top); - } else { - ConvertBuffer_1bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, - height, pSrcBitmap, src_left, src_top); - } - return true; - case 8: - if (pSrcBitmap->GetPalette()) { - ConvertBuffer_8bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, - height, pSrcBitmap, src_left, src_top); - } else { - ConvertBuffer_8bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, - height, pSrcBitmap, src_left, src_top); - } - return true; - case 24: - ConvertBuffer_24bppRgb2Rgb24(dest_buf, dest_pitch, width, height, - pSrcBitmap, src_left, src_top); - return true; - case 32: - ConvertBuffer_32bppRgb2Rgb24(dest_buf, dest_pitch, width, height, - pSrcBitmap, src_left, src_top); - return true; - default: - return false; - } -} - -bool ConvertBuffer_Argb(int bpp, - bool cmyk, - FXDIB_Format dest_format, - uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top) { - switch (bpp) { - case 1: - if (pSrcBitmap->GetPalette()) { - ConvertBuffer_1bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, - height, pSrcBitmap, src_left, src_top); - } else { - ConvertBuffer_1bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, - height, pSrcBitmap, src_left, src_top); - } - return true; - case 8: - if (pSrcBitmap->GetPalette()) { - ConvertBuffer_8bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, - height, pSrcBitmap, src_left, src_top); - } else { - ConvertBuffer_8bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, - height, pSrcBitmap, src_left, src_top); - } - return true; - case 24: - case 32: - if (cmyk) { - ConvertBuffer_32bppCmyk2Rgb32(dest_buf, dest_pitch, width, height, - pSrcBitmap, src_left, src_top); - } else { - ConvertBuffer_Rgb2Rgb32(dest_buf, dest_pitch, width, height, pSrcBitmap, - src_left, src_top); - } - return true; - default: - return false; - } -} - -} // namespace - -CFX_DIBSource::CFX_DIBSource() - : m_Width(0), m_Height(0), m_bpp(0), m_AlphaFlag(0), m_Pitch(0) {} - -CFX_DIBSource::~CFX_DIBSource() {} - -uint8_t* CFX_DIBSource::GetBuffer() const { - return nullptr; -} - -bool CFX_DIBSource::SkipToScanline(int line, - PauseIndicatorIface* pPause) const { - return false; -} - -RetainPtr 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()) - return nullptr; - } - auto pNewBitmap = pdfium::MakeRetain(); - if (!pNewBitmap->Create(rect.Width(), rect.Height(), GetFormat())) - return nullptr; - - 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; - int dword_count = pNewBitmap->m_Pitch / 4; - for (int row = rect.top; row < rect.bottom; ++row) { - const uint32_t* src_scan = - reinterpret_cast(GetScanline(row)) + rect.left / 32; - uint32_t* dest_scan = reinterpret_cast( - pNewBitmap->GetWritableScanline(row - rect.top)); - for (int i = 0; i < dword_count; ++i) { - dest_scan[i] = - (src_scan[i] << left_shift) | (src_scan[i + 1] >> right_shift); - } - } - } else { - int copy_len = (pNewBitmap->GetWidth() * pNewBitmap->GetBPP() + 7) / 8; - if (m_Pitch < static_cast(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 = pNewBitmap->GetWritableScanline(row - rect.top); - memcpy(dest_scan, src_scan, copy_len); - } - } - return pNewBitmap; -} - -void CFX_DIBSource::BuildPalette() { - if (m_pPalette) - return; - - if (GetBPP() == 1) { - m_pPalette.reset(FX_Alloc(uint32_t, 2)); - if (IsCmykImage()) { - m_pPalette.get()[0] = 0xff; - m_pPalette.get()[1] = 0; - } else { - m_pPalette.get()[0] = 0xff000000; - m_pPalette.get()[1] = 0xffffffff; - } - } else if (GetBPP() == 8) { - m_pPalette.reset(FX_Alloc(uint32_t, 256)); - if (IsCmykImage()) { - for (int i = 0; i < 256; ++i) - m_pPalette.get()[i] = 0xff - i; - } else { - for (int i = 0; i < 256; ++i) - m_pPalette.get()[i] = 0xff000000 | (i * 0x10101); - } - } -} - -bool CFX_DIBSource::BuildAlphaMask() { - if (m_pAlphaMask) - return true; - - m_pAlphaMask = pdfium::MakeRetain(); - if (!m_pAlphaMask->Create(m_Width, m_Height, FXDIB_8bppMask)) { - m_pAlphaMask = nullptr; - return false; - } - memset(m_pAlphaMask->GetBuffer(), 0xff, - m_pAlphaMask->GetHeight() * m_pAlphaMask->GetPitch()); - return true; -} - -uint32_t CFX_DIBSource::GetPaletteArgb(int index) const { - ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask()); - if (m_pPalette) - return m_pPalette.get()[index]; - - if (IsCmykImage()) { - if (GetBPP() == 1) - return index ? 0 : 0xff; - - return 0xff - index; - } - if (GetBPP() == 1) - return index ? 0xffffffff : 0xff000000; - - return index * 0x10101 | 0xff000000; -} - -void CFX_DIBSource::SetPaletteArgb(int index, uint32_t color) { - ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask()); - if (!m_pPalette) { - BuildPalette(); - } - m_pPalette.get()[index] = color; -} - -int CFX_DIBSource::FindPalette(uint32_t color) const { - ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask()); - if (!m_pPalette) { - if (IsCmykImage()) { - if (GetBPP() == 1) - return (static_cast(color) == 0xff) ? 0 : 1; - - return 0xff - static_cast(color); - } - if (GetBPP() == 1) - return (static_cast(color) == 0xff) ? 1 : 0; - - return static_cast(color); - } - int palsize = (1 << GetBPP()); - for (int i = 0; i < palsize; ++i) { - if (m_pPalette.get()[i] == color) - return i; - } - return -1; -} - -void CFX_DIBSource::GetOverlapRect(int& dest_left, - int& dest_top, - int& width, - int& height, - int src_width, - int src_height, - int& src_left, - int& src_top, - const CFX_ClipRgn* pClipRgn) { - if (width == 0 || height == 0) - return; - - ASSERT(width > 0 && height > 0); - if (dest_left > m_Width || dest_top > m_Height) { - width = 0; - height = 0; - return; - } - int x_offset = dest_left - src_left; - int y_offset = dest_top - src_top; - FX_RECT src_rect(src_left, src_top, src_left + width, src_top + height); - FX_RECT src_bound(0, 0, src_width, src_height); - src_rect.Intersect(src_bound); - FX_RECT dest_rect(src_rect.left + x_offset, src_rect.top + y_offset, - src_rect.right + x_offset, src_rect.bottom + y_offset); - FX_RECT dest_bound(0, 0, m_Width, m_Height); - dest_rect.Intersect(dest_bound); - if (pClipRgn) - dest_rect.Intersect(pClipRgn->GetBox()); - dest_left = dest_rect.left; - dest_top = dest_rect.top; - src_left = dest_left - x_offset; - src_top = dest_top - y_offset; - width = dest_rect.right - dest_rect.left; - height = dest_rect.bottom - dest_rect.top; -} - -void CFX_DIBSource::SetPalette(const uint32_t* pSrc) { - static const uint32_t kPaletteSize = 256; - if (!pSrc || GetBPP() > 8) { - m_pPalette.reset(); - 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); - memcpy(m_pPalette.get(), pSrc, pal_size * sizeof(uint32_t)); -} - -void CFX_DIBSource::GetPalette(uint32_t* pal, int alpha) const { - ASSERT(GetBPP() <= 8 && !IsCmykImage()); - if (GetBPP() == 1) { - pal[0] = ((m_pPalette ? m_pPalette.get()[0] : 0xff000000) & 0xffffff) | - (alpha << 24); - pal[1] = ((m_pPalette ? m_pPalette.get()[1] : 0xffffffff) & 0xffffff) | - (alpha << 24); - return; - } - if (m_pPalette) { - for (int i = 0; i < 256; ++i) - pal[i] = (m_pPalette.get()[i] & 0x00ffffff) | (alpha << 24); - } else { - for (int i = 0; i < 256; ++i) - pal[i] = (i * 0x10101) | (alpha << 24); - } -} - -RetainPtr CFX_DIBSource::CloneAlphaMask() const { - ASSERT(GetFormat() == FXDIB_Argb); - FX_RECT rect(0, 0, m_Width, m_Height); - auto pMask = pdfium::MakeRetain(); - 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 = pMask->GetWritableScanline(row - rect.top); - for (int col = rect.left; col < rect.right; ++col) { - *dest_scan++ = *src_scan; - src_scan += 4; - } - } - return pMask; -} - -bool CFX_DIBSource::SetAlphaMask(const RetainPtr& pAlphaMask, - const FX_RECT* pClip) { - if (!HasAlpha() || GetFormat() == FXDIB_Argb) - return false; - - if (!pAlphaMask) { - m_pAlphaMask->Clear(0xff000000); - return true; - } - 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; - } - for (int row = 0; row < m_Height; ++row) { - memcpy(m_pAlphaMask->GetWritableScanline(row), - pAlphaMask->GetScanline(row + rect.top) + rect.left, - m_pAlphaMask->m_Pitch); - } - return true; -} - -RetainPtr CFX_DIBSource::FlipImage(bool bXFlip, - bool bYFlip) const { - auto pFlipped = pdfium::MakeRetain(); - if (!pFlipped->Create(m_Width, m_Height, GetFormat())) - return nullptr; - - pFlipped->SetPalette(m_pPalette.get()); - uint8_t* pDestBuffer = pFlipped->GetBuffer(); - int Bpp = m_bpp / 8; - for (int row = 0; row < m_Height; ++row) { - const uint8_t* src_scan = GetScanline(row); - uint8_t* dest_scan = - pDestBuffer + m_Pitch * (bYFlip ? (m_Height - row - 1) : row); - if (!bXFlip) { - memcpy(dest_scan, src_scan, m_Pitch); - continue; - } - if (m_bpp == 1) { - memset(dest_scan, 0, m_Pitch); - for (int col = 0; col < m_Width; ++col) - if (src_scan[col / 8] & (1 << (7 - col % 8))) { - int dest_col = m_Width - col - 1; - dest_scan[dest_col / 8] |= (1 << (7 - dest_col % 8)); - } - } else { - dest_scan += (m_Width - 1) * Bpp; - if (Bpp == 1) { - for (int col = 0; col < m_Width; ++col) { - *dest_scan = *src_scan; - --dest_scan; - ++src_scan; - } - } else if (Bpp == 3) { - for (int col = 0; col < m_Width; ++col) { - dest_scan[0] = src_scan[0]; - dest_scan[1] = src_scan[1]; - dest_scan[2] = src_scan[2]; - dest_scan -= 3; - src_scan += 3; - } - } else { - ASSERT(Bpp == 4); - for (int col = 0; col < m_Width; ++col) { - *(uint32_t*)dest_scan = *(uint32_t*)src_scan; - dest_scan -= 4; - src_scan += 4; - } - } - } - } - if (m_pAlphaMask) { - pDestBuffer = pFlipped->m_pAlphaMask->GetBuffer(); - uint32_t dest_pitch = pFlipped->m_pAlphaMask->GetPitch(); - for (int row = 0; row < m_Height; ++row) { - const uint8_t* src_scan = m_pAlphaMask->GetScanline(row); - uint8_t* dest_scan = - pDestBuffer + dest_pitch * (bYFlip ? (m_Height - row - 1) : row); - if (!bXFlip) { - memcpy(dest_scan, src_scan, dest_pitch); - continue; - } - dest_scan += (m_Width - 1); - for (int col = 0; col < m_Width; ++col) { - *dest_scan = *src_scan; - --dest_scan; - ++src_scan; - } - } - } - return pFlipped; -} - -RetainPtr CFX_DIBSource::CloneConvert(FXDIB_Format dest_format) { - if (dest_format == GetFormat()) - return Clone(nullptr); - - auto pClone = pdfium::MakeRetain(); - if (!pClone->Create(m_Width, m_Height, dest_format)) - return nullptr; - - RetainPtr pSrcAlpha; - if (HasAlpha()) { - pSrcAlpha = (GetFormat() == FXDIB_Argb) ? CloneAlphaMask() : m_pAlphaMask; - if (!pSrcAlpha) - return nullptr; - } - if (dest_format & 0x0200) { - bool ret; - if (dest_format == FXDIB_Argb) { - ret = pSrcAlpha ? pClone->LoadChannel(FXDIB_Alpha, pSrcAlpha, FXDIB_Alpha) - : pClone->LoadChannel(FXDIB_Alpha, 0xff); - } else { - ret = pClone->SetAlphaMask(pSrcAlpha, nullptr); - } - if (!ret) - return nullptr; - } - - RetainPtr holder(this); - std::unique_ptr pal_8bpp; - if (!ConvertBuffer(dest_format, pClone->GetBuffer(), pClone->GetPitch(), - m_Width, m_Height, holder, 0, 0, &pal_8bpp)) { - return nullptr; - } - if (pal_8bpp) - pClone->SetPalette(pal_8bpp.get()); - - return pClone; -} - -RetainPtr CFX_DIBSource::SwapXY(bool bXFlip, bool bYFlip) const { - FX_RECT dest_clip(0, 0, m_Height, m_Width); - if (dest_clip.IsEmpty()) - return nullptr; - - auto pTransBitmap = pdfium::MakeRetain(); - int result_height = dest_clip.Height(); - int result_width = dest_clip.Width(); - if (!pTransBitmap->Create(result_width, result_height, GetFormat())) - return nullptr; - - 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; - int row_end = bXFlip ? m_Height - dest_clip.left : dest_clip.right; - int col_start = bYFlip ? m_Width - dest_clip.bottom : dest_clip.top; - int col_end = bYFlip ? m_Width - dest_clip.top : dest_clip.bottom; - if (GetBPP() == 1) { - memset(dest_buf, 0xff, dest_pitch * result_height); - for (int row = row_start; row < row_end; ++row) { - const uint8_t* src_scan = GetScanline(row); - int dest_col = (bXFlip ? dest_clip.right - (row - row_start) - 1 : row) - - dest_clip.left; - uint8_t* dest_scan = dest_buf; - if (bYFlip) - dest_scan += (result_height - 1) * dest_pitch; - int dest_step = bYFlip ? -dest_pitch : dest_pitch; - for (int col = col_start; col < col_end; ++col) { - if (!(src_scan[col / 8] & (1 << (7 - col % 8)))) - dest_scan[dest_col / 8] &= ~(1 << (7 - dest_col % 8)); - dest_scan += dest_step; - } - } - } else { - int nBytes = GetBPP() / 8; - int dest_step = bYFlip ? -dest_pitch : dest_pitch; - if (nBytes == 3) - dest_step -= 2; - for (int row = row_start; row < row_end; ++row) { - int dest_col = (bXFlip ? dest_clip.right - (row - row_start) - 1 : row) - - dest_clip.left; - uint8_t* dest_scan = dest_buf + dest_col * nBytes; - if (bYFlip) - dest_scan += (result_height - 1) * dest_pitch; - if (nBytes == 4) { - const uint32_t* src_scan = - reinterpret_cast(GetScanline(row)) + col_start; - for (int col = col_start; col < col_end; ++col) { - *(uint32_t*)dest_scan = *src_scan++; - dest_scan += dest_step; - } - } else { - const uint8_t* src_scan = GetScanline(row) + col_start * nBytes; - if (nBytes == 1) { - for (int col = col_start; col < col_end; ++col) { - *dest_scan = *src_scan++; - dest_scan += dest_step; - } - } else { - for (int col = col_start; col < col_end; ++col) { - *dest_scan++ = *src_scan++; - *dest_scan++ = *src_scan++; - *dest_scan = *src_scan++; - dest_scan += dest_step; - } - } - } - } - } - if (m_pAlphaMask) { - dest_pitch = pTransBitmap->m_pAlphaMask->GetPitch(); - dest_buf = pTransBitmap->m_pAlphaMask->GetBuffer(); - int dest_step = bYFlip ? -dest_pitch : dest_pitch; - for (int row = row_start; row < row_end; ++row) { - int dest_col = (bXFlip ? dest_clip.right - (row - row_start) - 1 : row) - - dest_clip.left; - uint8_t* dest_scan = dest_buf + dest_col; - if (bYFlip) - dest_scan += (result_height - 1) * dest_pitch; - const uint8_t* src_scan = m_pAlphaMask->GetScanline(row) + col_start; - for (int col = col_start; col < col_end; ++col) { - *dest_scan = *src_scan++; - dest_scan += dest_step; - } - } - } - return pTransBitmap; -} - -RetainPtr CFX_DIBSource::TransformTo( - const CFX_Matrix* pDestMatrix, - int* result_left, - int* result_top) { - RetainPtr holder(this); - CFX_ImageTransformer transformer(holder, pDestMatrix, 0, nullptr); - transformer.Continue(nullptr); - *result_left = transformer.result().left; - *result_top = transformer.result().top; - return transformer.DetachBitmap(); -} - -RetainPtr CFX_DIBSource::StretchTo(int dest_width, - int dest_height, - uint32_t flags, - const FX_RECT* pClip) { - RetainPtr holder(this); - FX_RECT clip_rect(0, 0, abs(dest_width), abs(dest_height)); - if (pClip) - clip_rect.Intersect(*pClip); - - if (clip_rect.IsEmpty()) - return nullptr; - - if (dest_width == m_Width && dest_height == m_Height) - return Clone(&clip_rect); - - CFX_BitmapStorer storer; - CFX_ImageStretcher stretcher(&storer, holder, dest_width, dest_height, - clip_rect, flags); - if (stretcher.Start()) - stretcher.Continue(nullptr); - - return storer.Detach(); -} - -// static -bool CFX_DIBSource::ConvertBuffer( - FXDIB_Format dest_format, - uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top, - std::unique_ptr* p_pal) { - FXDIB_Format src_format = pSrcBitmap->GetFormat(); - const int bpp = GetBppFromFormat(src_format); - switch (dest_format) { - case FXDIB_8bppMask: { - return ConvertBuffer_8bppMask(bpp, dest_buf, dest_pitch, width, height, - pSrcBitmap, src_left, src_top); - } - case FXDIB_8bppRgb: - case FXDIB_8bppRgba: { - const bool bpp_1_or_8 = (bpp == 1 || bpp == 8); - if (bpp_1_or_8 && !pSrcBitmap->GetPalette()) { - return ConvertBuffer(FXDIB_8bppMask, dest_buf, dest_pitch, width, - height, pSrcBitmap, src_left, src_top, p_pal); - } - p_pal->reset(FX_Alloc(uint32_t, 256)); - if (bpp_1_or_8 && pSrcBitmap->GetPalette()) { - ConvertBuffer_Plt2PltRgb8(dest_buf, dest_pitch, width, height, - pSrcBitmap, src_left, src_top, p_pal->get()); - return true; - } - if (bpp >= 24) { - ConvertBuffer_Rgb2PltRgb8(dest_buf, dest_pitch, width, height, - pSrcBitmap, src_left, src_top, p_pal->get()); - return true; - } - return false; - } - case FXDIB_Rgb: - case FXDIB_Rgba: { - return ConvertBuffer_Rgb(bpp, dest_format, dest_buf, dest_pitch, width, - height, pSrcBitmap, src_left, src_top); - } - case FXDIB_Argb: - case FXDIB_Rgb32: { - const bool cmyk = src_format & 0x0400; - return ConvertBuffer_Argb(bpp, cmyk, dest_format, dest_buf, dest_pitch, - width, height, pSrcBitmap, src_left, src_top); - } - default: - NOTREACHED(); - return false; - } -} diff --git a/core/fxge/dib/cfx_dibsource.h b/core/fxge/dib/cfx_dibsource.h deleted file mode 100644 index 064dbe2055..0000000000 --- a/core/fxge/dib/cfx_dibsource.h +++ /dev/null @@ -1,139 +0,0 @@ -// Copyright 2017 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef CORE_FXGE_DIB_CFX_DIBSOURCE_H_ -#define CORE_FXGE_DIB_CFX_DIBSOURCE_H_ - -#include - -#include "core/fxcrt/fx_coordinates.h" -#include "core/fxcrt/fx_memory.h" -#include "core/fxcrt/retain_ptr.h" -#include "core/fxge/fx_dib.h" - -enum FXDIB_Channel { - FXDIB_Red = 1, - FXDIB_Green, - FXDIB_Blue, - FXDIB_Cyan, - FXDIB_Magenta, - FXDIB_Yellow, - FXDIB_Black, - FXDIB_Alpha -}; - -class CFX_ClipRgn; -class CFX_DIBitmap; -class PauseIndicatorIface; - -class CFX_DIBSource : public Retainable { - public: - ~CFX_DIBSource() override; - - virtual uint8_t* GetBuffer() const; - virtual const uint8_t* GetScanline(int line) const = 0; - virtual bool SkipToScanline(int line, PauseIndicatorIface* pPause) const; - virtual void DownSampleScanline(int line, - uint8_t* dest_scan, - int dest_bpp, - int dest_width, - bool bFlipX, - int clip_left, - int clip_width) const = 0; - - uint8_t* GetWritableScanline(int line) { - return const_cast(GetScanline(line)); - } - int GetWidth() const { return m_Width; } - int GetHeight() const { return m_Height; } - - FXDIB_Format GetFormat() const { - return static_cast(m_AlphaFlag * 0x100 + m_bpp); - } - uint32_t GetPitch() const { return m_Pitch; } - uint32_t* GetPalette() const { return m_pPalette.get(); } - int GetBPP() const { return m_bpp; } - - // TODO(thestig): Investigate this. Given the possible values of FXDIB_Format, - // it feels as though this should be implemented as !!(m_AlphaFlag & 1) and - // IsOpaqueImage() below should never be able to return true. - bool IsAlphaMask() const { return m_AlphaFlag == 1; } - bool HasAlpha() const { return !!(m_AlphaFlag & 2); } - bool IsOpaqueImage() const { return !(m_AlphaFlag & 3); } - bool IsCmykImage() const { return !!(m_AlphaFlag & 4); } - - int GetPaletteSize() const { - return IsAlphaMask() ? 0 : (m_bpp == 1 ? 2 : (m_bpp == 8 ? 256 : 0)); - } - - uint32_t GetPaletteArgb(int index) const; - void SetPaletteArgb(int index, uint32_t color); - - // Copies into internally-owned palette. - void SetPalette(const uint32_t* pSrcPal); - - RetainPtr Clone(const FX_RECT* pClip) const; - RetainPtr CloneConvert(FXDIB_Format format); - RetainPtr StretchTo(int dest_width, - int dest_height, - uint32_t flags, - const FX_RECT* pClip); - RetainPtr TransformTo(const CFX_Matrix* pMatrix, - int* left, - int* top); - RetainPtr SwapXY(bool bXFlip, bool bYFlip) const; - RetainPtr FlipImage(bool bXFlip, bool bYFlip) const; - - RetainPtr CloneAlphaMask() const; - - // Copies into internally-owned mask. - bool SetAlphaMask(const RetainPtr& pAlphaMask, - const FX_RECT* pClip); - - void GetOverlapRect(int& dest_left, - int& dest_top, - int& width, - int& height, - int src_width, - int src_height, - int& src_left, - int& src_top, - const CFX_ClipRgn* pClipRgn); - -#if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_ - void DebugVerifyBitmapIsPreMultiplied(void* buffer) const; -#endif - - RetainPtr m_pAlphaMask; - - protected: - CFX_DIBSource(); - - static bool ConvertBuffer(FXDIB_Format dest_format, - uint8_t* dest_buf, - int dest_pitch, - int width, - int height, - const RetainPtr& pSrcBitmap, - int src_left, - int src_top, - std::unique_ptr* pal); - - void BuildPalette(); - bool BuildAlphaMask(); - int FindPalette(uint32_t color) const; - void GetPalette(uint32_t* pal, int alpha) const; - - int m_Width; - int m_Height; - int m_bpp; - uint32_t m_AlphaFlag; - uint32_t m_Pitch; - // TODO(weili): Use std::vector for this. - std::unique_ptr m_pPalette; -}; - -#endif // CORE_FXGE_DIB_CFX_DIBSOURCE_H_ diff --git a/core/fxge/dib/cfx_filtereddib.cpp b/core/fxge/dib/cfx_filtereddib.cpp index 1d5ed6200c..297a099a4c 100644 --- a/core/fxge/dib/cfx_filtereddib.cpp +++ b/core/fxge/dib/cfx_filtereddib.cpp @@ -12,7 +12,7 @@ CFX_FilteredDIB::CFX_FilteredDIB() {} CFX_FilteredDIB::~CFX_FilteredDIB() {} -void CFX_FilteredDIB::LoadSrc(const RetainPtr& pSrc) { +void CFX_FilteredDIB::LoadSrc(const RetainPtr& pSrc) { m_pSrc = pSrc; m_Width = pSrc->GetWidth(); m_Height = pSrc->GetHeight(); diff --git a/core/fxge/dib/cfx_filtereddib.h b/core/fxge/dib/cfx_filtereddib.h index 9c1efa544d..ec32a787c4 100644 --- a/core/fxge/dib/cfx_filtereddib.h +++ b/core/fxge/dib/cfx_filtereddib.h @@ -10,9 +10,9 @@ #include #include "core/fxcrt/retain_ptr.h" -#include "core/fxge/dib/cfx_dibsource.h" +#include "core/fxge/dib/cfx_dibbase.h" -class CFX_FilteredDIB : public CFX_DIBSource { +class CFX_FilteredDIB : public CFX_DIBBase { public: template friend RetainPtr pdfium::MakeRetain(Args&&... args); @@ -26,13 +26,13 @@ class CFX_FilteredDIB : public CFX_DIBSource { int pixels, int Bpp) const = 0; - void LoadSrc(const RetainPtr& pSrc); + void LoadSrc(const RetainPtr& pSrc); protected: CFX_FilteredDIB(); ~CFX_FilteredDIB() override; - // CFX_DIBSource + // CFX_DIBBase const uint8_t* GetScanline(int line) const override; void DownSampleScanline(int line, uint8_t* dest_scan, @@ -42,7 +42,7 @@ class CFX_FilteredDIB : public CFX_DIBSource { int clip_left, int clip_width) const override; - RetainPtr m_pSrc; + RetainPtr m_pSrc; mutable std::vector m_Scanline; }; diff --git a/core/fxge/dib/cfx_imagerenderer.cpp b/core/fxge/dib/cfx_imagerenderer.cpp index a196068a2e..64c484f24d 100644 --- a/core/fxge/dib/cfx_imagerenderer.cpp +++ b/core/fxge/dib/cfx_imagerenderer.cpp @@ -15,7 +15,7 @@ CFX_ImageRenderer::CFX_ImageRenderer(const RetainPtr& pDevice, const CFX_ClipRgn* pClipRgn, - const RetainPtr& pSource, + const RetainPtr& pSource, int bitmap_alpha, uint32_t mask_color, const CFX_Matrix* pMatrix, diff --git a/core/fxge/dib/cfx_imagerenderer.h b/core/fxge/dib/cfx_imagerenderer.h index 14eaf56ff8..905ff8c25d 100644 --- a/core/fxge/dib/cfx_imagerenderer.h +++ b/core/fxge/dib/cfx_imagerenderer.h @@ -13,8 +13,8 @@ #include "core/fxcrt/retain_ptr.h" #include "core/fxcrt/unowned_ptr.h" #include "core/fxge/dib/cfx_bitmapcomposer.h" +#include "core/fxge/dib/cfx_dibbase.h" #include "core/fxge/dib/cfx_dibitmap.h" -#include "core/fxge/dib/cfx_dibsource.h" #include "core/fxge/fx_dib.h" #include "third_party/base/stl_util.h" @@ -25,7 +25,7 @@ class CFX_ImageRenderer { public: CFX_ImageRenderer(const RetainPtr& pDevice, const CFX_ClipRgn* pClipRgn, - const RetainPtr& pSource, + const RetainPtr& pSource, int bitmap_alpha, uint32_t mask_color, const CFX_Matrix* pMatrix, diff --git a/core/fxge/dib/cfx_imagestretcher.cpp b/core/fxge/dib/cfx_imagestretcher.cpp index 1c54e3fdd0..809d3b12dd 100644 --- a/core/fxge/dib/cfx_imagestretcher.cpp +++ b/core/fxge/dib/cfx_imagestretcher.cpp @@ -9,8 +9,8 @@ #include #include +#include "core/fxge/dib/cfx_dibbase.h" #include "core/fxge/dib/cfx_dibitmap.h" -#include "core/fxge/dib/cfx_dibsource.h" #include "core/fxge/dib/cstretchengine.h" #include "core/fxge/fx_dib.h" #include "third_party/base/ptr_util.h" @@ -23,7 +23,7 @@ bool SourceSizeWithinLimit(int width, int height) { return !height || width < kMaxProgressiveStretchPixels / height; } -FXDIB_Format GetStretchedFormat(const CFX_DIBSource& src) { +FXDIB_Format GetStretchedFormat(const CFX_DIBBase& src) { FXDIB_Format format = src.GetFormat(); if (format == FXDIB_1bppMask) return FXDIB_8bppMask; @@ -43,7 +43,7 @@ std::tuple CmykDecode(const uint32_t cmyk) { } // namespace CFX_ImageStretcher::CFX_ImageStretcher(ScanlineComposerIface* pDest, - const RetainPtr& pSource, + const RetainPtr& pSource, int dest_width, int dest_height, const FX_RECT& bitmap_rect, diff --git a/core/fxge/dib/cfx_imagestretcher.h b/core/fxge/dib/cfx_imagestretcher.h index 113372571b..8e866620fd 100644 --- a/core/fxge/dib/cfx_imagestretcher.h +++ b/core/fxge/dib/cfx_imagestretcher.h @@ -16,13 +16,13 @@ #include "core/fxge/dib/scanlinecomposer_iface.h" #include "core/fxge/fx_dib.h" -class CFX_DIBSource; +class CFX_DIBBase; class PauseIndicatorIface; class CFX_ImageStretcher { public: CFX_ImageStretcher(ScanlineComposerIface* pDest, - const RetainPtr& pSource, + const RetainPtr& pSource, int dest_width, int dest_height, const FX_RECT& bitmap_rect, @@ -32,7 +32,7 @@ class CFX_ImageStretcher { bool Start(); bool Continue(PauseIndicatorIface* pPause); - RetainPtr source() { return m_pSource; } + RetainPtr source() { return m_pSource; } private: bool StartQuickStretch(); @@ -41,7 +41,7 @@ class CFX_ImageStretcher { bool ContinueStretch(PauseIndicatorIface* pPause); UnownedPtr const m_pDest; - RetainPtr m_pSource; + RetainPtr m_pSource; std::unique_ptr m_pStretchEngine; std::unique_ptr m_pScanline; std::unique_ptr m_pMaskScanline; diff --git a/core/fxge/dib/cfx_imagetransformer.cpp b/core/fxge/dib/cfx_imagetransformer.cpp index c41ac2bdf2..0042462c89 100644 --- a/core/fxge/dib/cfx_imagetransformer.cpp +++ b/core/fxge/dib/cfx_imagetransformer.cpp @@ -99,7 +99,7 @@ void bicubic_get_pos_weight(int pos_pixel[], v_w[3] = SDP_Table[512 - res_y]; } -FXDIB_Format GetTransformedFormat(const RetainPtr& pDrc) { +FXDIB_Format GetTransformedFormat(const RetainPtr& pDrc) { if (pDrc->IsAlphaMask()) return FXDIB_8bppMask; @@ -200,7 +200,7 @@ class CFX_BilinearMatrix : public CPDF_FixedMatrix { } // namespace -CFX_ImageTransformer::CFX_ImageTransformer(const RetainPtr& pSrc, +CFX_ImageTransformer::CFX_ImageTransformer(const RetainPtr& pSrc, const CFX_Matrix* pMatrix, int flags, const FX_RECT* pClip) diff --git a/core/fxge/dib/cfx_imagetransformer.h b/core/fxge/dib/cfx_imagetransformer.h index a5b8841611..61fe1e1ba1 100644 --- a/core/fxge/dib/cfx_imagetransformer.h +++ b/core/fxge/dib/cfx_imagetransformer.h @@ -13,14 +13,14 @@ #include "core/fxcrt/retain_ptr.h" #include "core/fxcrt/unowned_ptr.h" #include "core/fxge/dib/cfx_bitmapstorer.h" +#include "core/fxge/dib/cfx_dibbase.h" #include "core/fxge/dib/cfx_dibitmap.h" -#include "core/fxge/dib/cfx_dibsource.h" class CFX_ImageStretcher; class CFX_ImageTransformer { public: - CFX_ImageTransformer(const RetainPtr& pSrc, + CFX_ImageTransformer(const RetainPtr& pSrc, const CFX_Matrix* pMatrix, int flags, const FX_RECT* pClip); @@ -98,7 +98,7 @@ class CFX_ImageTransformer { int increment, std::function func); - const RetainPtr m_pSrc; + const RetainPtr m_pSrc; UnownedPtr const m_pMatrix; const FX_RECT* const m_pClip; FX_RECT m_StretchClip; diff --git a/core/fxge/dib/cfx_scanlinecompositor.h b/core/fxge/dib/cfx_scanlinecompositor.h index 75ab578128..1c373f036b 100644 --- a/core/fxge/dib/cfx_scanlinecompositor.h +++ b/core/fxge/dib/cfx_scanlinecompositor.h @@ -9,7 +9,7 @@ #include -#include "core/fxge/dib/cfx_dibsource.h" +#include "core/fxge/dib/cfx_dibbase.h" class CFX_ScanlineCompositor { public: diff --git a/core/fxge/dib/cstretchengine.cpp b/core/fxge/dib/cstretchengine.cpp index ad9a356f34..41a843de66 100644 --- a/core/fxge/dib/cstretchengine.cpp +++ b/core/fxge/dib/cstretchengine.cpp @@ -10,8 +10,8 @@ #include #include "core/fxcrt/pauseindicator_iface.h" +#include "core/fxge/dib/cfx_dibbase.h" #include "core/fxge/dib/cfx_dibitmap.h" -#include "core/fxge/dib/cfx_dibsource.h" #include "core/fxge/dib/scanlinecomposer_iface.h" #include "core/fxge/fx_dib.h" @@ -227,7 +227,7 @@ CStretchEngine::CStretchEngine(ScanlineComposerIface* pDestBitmap, int dest_width, int dest_height, const FX_RECT& clip_rect, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, int flags) : m_DestFormat(dest_format), m_DestBpp(GetBppFromFormat(dest_format)), diff --git a/core/fxge/dib/cstretchengine.h b/core/fxge/dib/cstretchengine.h index c9f11d6006..db7a2be56d 100644 --- a/core/fxge/dib/cstretchengine.h +++ b/core/fxge/dib/cstretchengine.h @@ -24,7 +24,7 @@ class CStretchEngine { int dest_width, int dest_height, const FX_RECT& clip_rect, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, int flags); ~CStretchEngine(); @@ -80,7 +80,7 @@ class CStretchEngine { const int m_DestBpp; const int m_SrcBpp; const int m_bHasAlpha; - RetainPtr const m_pSource; + RetainPtr const m_pSource; const uint32_t* m_pSrcPalette; const int m_SrcWidth; const int m_SrcHeight; diff --git a/core/fxge/dib/cstretchengine_unittest.cpp b/core/fxge/dib/cstretchengine_unittest.cpp index 2c7e0342de..8169cae328 100644 --- a/core/fxge/dib/cstretchengine_unittest.cpp +++ b/core/fxge/dib/cstretchengine_unittest.cpp @@ -10,7 +10,7 @@ #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_number.h" #include "core/fpdfapi/parser/cpdf_stream.h" -#include "core/fpdfapi/render/cpdf_dibsource.h" +#include "core/fpdfapi/render/cpdf_dibbase.h" #include "core/fxcrt/fx_memory.h" #include "core/fxge/fx_dib.h" #include "testing/gtest/include/gtest/gtest.h" @@ -24,7 +24,7 @@ TEST(CStretchEngine, OverflowInCtor) { dict_obj->SetNewFor("Height", 12500); std::unique_ptr stream = pdfium::MakeUnique(nullptr, 0, std::move(dict_obj)); - auto dib_source = pdfium::MakeRetain(); + auto dib_source = pdfium::MakeRetain(); dib_source->Load(nullptr, stream.get()); CStretchEngine engine(nullptr, FXDIB_8bppRgb, 500, 500, clip_rect, dib_source, 0); diff --git a/core/fxge/fx_dib.h b/core/fxge/fx_dib.h index 3475e5c4bd..7d8ca5fd72 100644 --- a/core/fxge/fx_dib.h +++ b/core/fxge/fx_dib.h @@ -45,7 +45,7 @@ using FX_COLORREF = uint32_t; using FX_CMYK = uint32_t; class CFX_ClipRgn; -class CFX_DIBSource; +class CFX_DIBBase; class CStretchEngine; extern const int16_t SDP_Table[513]; diff --git a/core/fxge/renderdevicedriver_iface.cpp b/core/fxge/renderdevicedriver_iface.cpp index 7023e021bd..646851ff45 100644 --- a/core/fxge/renderdevicedriver_iface.cpp +++ b/core/fxge/renderdevicedriver_iface.cpp @@ -81,8 +81,8 @@ bool RenderDeviceDriverIface::DrawShading(const CPDF_ShadingPattern* pPattern, } bool RenderDeviceDriverIface::SetBitsWithMask( - const RetainPtr& pBitmap, - const RetainPtr& pMask, + const RetainPtr& pBitmap, + const RetainPtr& pMask, int left, int top, int bitmap_alpha, diff --git a/core/fxge/renderdevicedriver_iface.h b/core/fxge/renderdevicedriver_iface.h index a0c148d117..62287abe62 100644 --- a/core/fxge/renderdevicedriver_iface.h +++ b/core/fxge/renderdevicedriver_iface.h @@ -14,7 +14,7 @@ #include "core/fxcrt/retain_ptr.h" class CFX_DIBitmap; -class CFX_DIBSource; +class CFX_DIBBase; class CFX_Font; class CFX_GraphStateData; class CFX_ImageRenderer; @@ -63,13 +63,13 @@ class RenderDeviceDriverIface { int left, int top); virtual RetainPtr GetBackDrop(); - virtual bool SetDIBits(const RetainPtr& pBitmap, + virtual bool SetDIBits(const RetainPtr& pBitmap, uint32_t color, const FX_RECT* pSrcRect, int dest_left, int dest_top, int blend_type) = 0; - virtual bool StretchDIBits(const RetainPtr& pBitmap, + virtual bool StretchDIBits(const RetainPtr& pBitmap, uint32_t color, int dest_left, int dest_top, @@ -78,7 +78,7 @@ class RenderDeviceDriverIface { const FX_RECT* pClipRect, uint32_t flags, int blend_type) = 0; - virtual bool StartDIBits(const RetainPtr& pBitmap, + virtual bool StartDIBits(const RetainPtr& pBitmap, int bitmap_alpha, uint32_t color, const CFX_Matrix* pMatrix, @@ -100,8 +100,8 @@ class RenderDeviceDriverIface { const FX_RECT& clip_rect, int alpha, bool bAlphaMode); - virtual bool SetBitsWithMask(const RetainPtr& pBitmap, - const RetainPtr& pMask, + virtual bool SetBitsWithMask(const RetainPtr& pBitmap, + const RetainPtr& pMask, int left, int top, int bitmap_alpha, diff --git a/core/fxge/skia/fx_skia_device.cpp b/core/fxge/skia/fx_skia_device.cpp index 0b96cdca5f..db181003ed 100644 --- a/core/fxge/skia/fx_skia_device.cpp +++ b/core/fxge/skia/fx_skia_device.cpp @@ -60,7 +60,7 @@ void RgbByteOrderTransferBitmap(const RetainPtr& pBitmap, int dest_top, int width, int height, - const RetainPtr& pSrcBitmap, + const RetainPtr& pSrcBitmap, int src_left, int src_top) { if (!pBitmap) @@ -566,7 +566,7 @@ void SetBitmapPaint(bool isAlphaMask, paint->setAlpha(bitmap_alpha); } -bool Upsample(const RetainPtr& pSource, +bool Upsample(const RetainPtr& pSource, std::unique_ptr& dst8Storage, std::unique_ptr& dst32Storage, SkBitmap* skBitmap, @@ -2256,7 +2256,7 @@ RetainPtr CFX_SkiaDeviceDriver::GetBackDrop() { return m_pBackdropBitmap; } -bool CFX_SkiaDeviceDriver::SetDIBits(const RetainPtr& pBitmap, +bool CFX_SkiaDeviceDriver::SetDIBits(const RetainPtr& pBitmap, uint32_t argb, const FX_RECT* pSrcRect, int left, @@ -2286,16 +2286,15 @@ bool CFX_SkiaDeviceDriver::SetDIBits(const RetainPtr& pBitmap, #endif // _SKIA_SUPPORT_PATHS_ } -bool CFX_SkiaDeviceDriver::StretchDIBits( - const RetainPtr& pSource, - uint32_t argb, - int dest_left, - int dest_top, - int dest_width, - int dest_height, - const FX_RECT* pClipRect, - uint32_t flags, - int blend_type) { +bool CFX_SkiaDeviceDriver::StretchDIBits(const RetainPtr& pSource, + uint32_t argb, + int dest_left, + int dest_top, + int dest_width, + int dest_height, + const FX_RECT* pClipRect, + uint32_t flags, + int blend_type) { #ifdef _SKIA_SUPPORT_ m_pCache->FlushForDraw(); if (!m_pBitmap->GetBuffer()) @@ -2339,7 +2338,7 @@ bool CFX_SkiaDeviceDriver::StretchDIBits( } bool CFX_SkiaDeviceDriver::StartDIBits( - const RetainPtr& pSource, + const RetainPtr& pSource, int bitmap_alpha, uint32_t argb, const CFX_Matrix* pMatrix, @@ -2472,8 +2471,8 @@ void CFX_DIBitmap::UnPreMultiply() { #ifdef _SKIA_SUPPORT_ bool CFX_SkiaDeviceDriver::DrawBitsWithMask( - const RetainPtr& pSource, - const RetainPtr& pMask, + const RetainPtr& pSource, + const RetainPtr& pMask, int bitmap_alpha, const CFX_Matrix* pMatrix, int blend_type) { @@ -2513,8 +2512,8 @@ bool CFX_SkiaDeviceDriver::DrawBitsWithMask( } bool CFX_SkiaDeviceDriver::SetBitsWithMask( - const RetainPtr& pBitmap, - const RetainPtr& pMask, + const RetainPtr& pBitmap, + const RetainPtr& pMask, int dest_left, int dest_top, int bitmap_alpha, @@ -2614,8 +2613,8 @@ void CFX_DefaultRenderDevice::DebugVerifyBitmapIsPreMultiplied() const { } bool CFX_DefaultRenderDevice::SetBitsWithMask( - const RetainPtr& pBitmap, - const RetainPtr& pMask, + const RetainPtr& pBitmap, + const RetainPtr& pMask, int left, int top, int bitmap_alpha, @@ -2629,7 +2628,7 @@ bool CFX_DefaultRenderDevice::SetBitsWithMask( } #endif // _SKIA_SUPPORT_ -void CFX_DIBSource::DebugVerifyBitmapIsPreMultiplied(void* opt) const { +void CFX_DIBBase::DebugVerifyBitmapIsPreMultiplied(void* opt) const { #ifdef SK_DEBUG SkASSERT(32 == GetBPP()); const uint32_t* buffer = (const uint32_t*)(opt ? opt : GetBuffer()); diff --git a/core/fxge/skia/fx_skia_device.h b/core/fxge/skia/fx_skia_device.h index 692199f6bb..ce6ca6b000 100644 --- a/core/fxge/skia/fx_skia_device.h +++ b/core/fxge/skia/fx_skia_device.h @@ -83,15 +83,15 @@ class CFX_SkiaDeviceDriver : public RenderDeviceDriverIface { RetainPtr GetBackDrop() override; - bool SetDIBits(const RetainPtr& pBitmap, + bool SetDIBits(const RetainPtr& pBitmap, uint32_t color, const FX_RECT* pSrcRect, int dest_left, int dest_top, int blend_type) override; #ifdef _SKIA_SUPPORT_ - bool SetBitsWithMask(const RetainPtr& pBitmap, - const RetainPtr& pMask, + bool SetBitsWithMask(const RetainPtr& pBitmap, + const RetainPtr& pMask, int dest_left, int dest_top, int bitmap_alpha, @@ -102,7 +102,7 @@ class CFX_SkiaDeviceDriver : public RenderDeviceDriverIface { void SetClipMask(const FX_RECT& clipBox, const SkPath& skClipPath); #endif - bool StretchDIBits(const RetainPtr& pBitmap, + bool StretchDIBits(const RetainPtr& pBitmap, uint32_t color, int dest_left, int dest_top, @@ -112,7 +112,7 @@ class CFX_SkiaDeviceDriver : public RenderDeviceDriverIface { uint32_t flags, int blend_type) override; - bool StartDIBits(const RetainPtr& pBitmap, + bool StartDIBits(const RetainPtr& pBitmap, int bitmap_alpha, uint32_t color, const CFX_Matrix* pMatrix, @@ -123,8 +123,8 @@ class CFX_SkiaDeviceDriver : public RenderDeviceDriverIface { bool ContinueDIBits(CFX_ImageRenderer* handle, PauseIndicatorIface* pPause) override; - bool DrawBitsWithMask(const RetainPtr& pBitmap, - const RetainPtr& pMask, + bool DrawBitsWithMask(const RetainPtr& pBitmap, + const RetainPtr& pMask, int bitmap_alpha, const CFX_Matrix* pMatrix, int blend_type); diff --git a/core/fxge/win32/cfx_psrenderer.cpp b/core/fxge/win32/cfx_psrenderer.cpp index 036cb43577..f5970a3ad1 100644 --- a/core/fxge/win32/cfx_psrenderer.cpp +++ b/core/fxge/win32/cfx_psrenderer.cpp @@ -337,7 +337,7 @@ void CFX_PSRenderer::SetGraphState(const CFX_GraphStateData* pGraphState) { WriteToStream(&buf); } -bool CFX_PSRenderer::SetDIBits(const RetainPtr& pSource, +bool CFX_PSRenderer::SetDIBits(const RetainPtr& pSource, uint32_t color, int left, int top) { @@ -348,7 +348,7 @@ bool CFX_PSRenderer::SetDIBits(const RetainPtr& pSource, return DrawDIBits(pSource, color, &matrix, 0); } -bool CFX_PSRenderer::StretchDIBits(const RetainPtr& pSource, +bool CFX_PSRenderer::StretchDIBits(const RetainPtr& pSource, uint32_t color, int dest_left, int dest_top, @@ -361,7 +361,7 @@ bool CFX_PSRenderer::StretchDIBits(const RetainPtr& pSource, return DrawDIBits(pSource, color, &matrix, flags); } -bool CFX_PSRenderer::DrawDIBits(const RetainPtr& pSource, +bool CFX_PSRenderer::DrawDIBits(const RetainPtr& pSource, uint32_t color, const CFX_Matrix* pMatrix, uint32_t flags) { @@ -423,7 +423,7 @@ bool CFX_PSRenderer::DrawDIBits(const RetainPtr& pSource, output_buf.release(); } else { CFX_DIBExtractor source_extractor(pSource); - RetainPtr pConverted = source_extractor.GetBitmap(); + RetainPtr pConverted = source_extractor.GetBitmap(); if (!pConverted) return false; switch (pSource->GetFormat()) { diff --git a/core/fxge/win32/cfx_psrenderer.h b/core/fxge/win32/cfx_psrenderer.h index 4116dd4a51..e54c2f8744 100644 --- a/core/fxge/win32/cfx_psrenderer.h +++ b/core/fxge/win32/cfx_psrenderer.h @@ -16,7 +16,7 @@ #include "core/fxcrt/retain_ptr.h" #include "core/fxge/cfx_graphstatedata.h" -class CFX_DIBSource; +class CFX_DIBBase; class CFX_FaceCache; class CFX_Font; class CFX_FontCache; @@ -52,18 +52,18 @@ class CFX_PSRenderer { uint32_t fill_color, uint32_t stroke_color, int fill_mode); - bool SetDIBits(const RetainPtr& pBitmap, + bool SetDIBits(const RetainPtr& pBitmap, uint32_t color, int dest_left, int dest_top); - bool StretchDIBits(const RetainPtr& pBitmap, + bool StretchDIBits(const RetainPtr& pBitmap, uint32_t color, int dest_left, int dest_top, int dest_width, int dest_height, uint32_t flags); - bool DrawDIBits(const RetainPtr& pBitmap, + bool DrawDIBits(const RetainPtr& pBitmap, uint32_t color, const CFX_Matrix* pMatrix, uint32_t flags); diff --git a/core/fxge/win32/fx_win32_device.cpp b/core/fxge/win32/fx_win32_device.cpp index ed03101ecd..c8f049704f 100644 --- a/core/fxge/win32/fx_win32_device.cpp +++ b/core/fxge/win32/fx_win32_device.cpp @@ -1187,7 +1187,7 @@ bool CGdiDisplayDriver::GetDIBits(const RetainPtr& pBitmap, return ret; } -bool CGdiDisplayDriver::SetDIBits(const RetainPtr& pSource, +bool CGdiDisplayDriver::SetDIBits(const RetainPtr& pSource, uint32_t color, const FX_RECT* pSrcRect, int left, @@ -1235,7 +1235,7 @@ bool CGdiDisplayDriver::SetDIBits(const RetainPtr& pSource, } bool CGdiDisplayDriver::UseFoxitStretchEngine( - const RetainPtr& pSource, + const RetainPtr& pSource, uint32_t color, int dest_left, int dest_top, @@ -1261,7 +1261,7 @@ bool CGdiDisplayDriver::UseFoxitStretchEngine( pClipRect->top, FXDIB_BLEND_NORMAL); } -bool CGdiDisplayDriver::StretchDIBits(const RetainPtr& pSource, +bool CGdiDisplayDriver::StretchDIBits(const RetainPtr& pSource, uint32_t color, int dest_left, int dest_top, @@ -1328,7 +1328,7 @@ bool CGdiDisplayDriver::StretchDIBits(const RetainPtr& pSource, dest_height, flags); } -bool CGdiDisplayDriver::StartDIBits(const RetainPtr& pBitmap, +bool CGdiDisplayDriver::StartDIBits(const RetainPtr& pBitmap, int bitmap_alpha, uint32_t color, const CFX_Matrix* pMatrix, diff --git a/core/fxge/win32/fx_win32_print.cpp b/core/fxge/win32/fx_win32_print.cpp index 1e2b2d4e9a..83a1627087 100644 --- a/core/fxge/win32/fx_win32_print.cpp +++ b/core/fxge/win32/fx_win32_print.cpp @@ -69,7 +69,7 @@ int CGdiPrinterDriver::GetDeviceCaps(int caps_id) const { return CGdiDeviceDriver::GetDeviceCaps(caps_id); } -bool CGdiPrinterDriver::SetDIBits(const RetainPtr& pSource, +bool CGdiPrinterDriver::SetDIBits(const RetainPtr& pSource, uint32_t color, const FX_RECT* pSrcRect, int left, @@ -96,7 +96,7 @@ bool CGdiPrinterDriver::SetDIBits(const RetainPtr& pSource, return GDI_SetDIBits(pBitmap, pSrcRect, left, top); } -bool CGdiPrinterDriver::StretchDIBits(const RetainPtr& pSource, +bool CGdiPrinterDriver::StretchDIBits(const RetainPtr& pSource, uint32_t color, int dest_left, int dest_top, @@ -159,7 +159,7 @@ bool CGdiPrinterDriver::StretchDIBits(const RetainPtr& pSource, dest_height, flags); } -bool CGdiPrinterDriver::StartDIBits(const RetainPtr& pSource, +bool CGdiPrinterDriver::StartDIBits(const RetainPtr& pSource, int bitmap_alpha, uint32_t color, const CFX_Matrix* pMatrix, @@ -454,7 +454,7 @@ bool CPSPrinterDriver::GetClipBox(FX_RECT* pRect) { return true; } -bool CPSPrinterDriver::SetDIBits(const RetainPtr& pBitmap, +bool CPSPrinterDriver::SetDIBits(const RetainPtr& pBitmap, uint32_t color, const FX_RECT* pSrcRect, int left, @@ -465,7 +465,7 @@ bool CPSPrinterDriver::SetDIBits(const RetainPtr& pBitmap, return m_PSRenderer.SetDIBits(pBitmap, color, left, top); } -bool CPSPrinterDriver::StretchDIBits(const RetainPtr& pBitmap, +bool CPSPrinterDriver::StretchDIBits(const RetainPtr& pBitmap, uint32_t color, int dest_left, int dest_top, @@ -480,7 +480,7 @@ bool CPSPrinterDriver::StretchDIBits(const RetainPtr& pBitmap, dest_width, dest_height, flags); } -bool CPSPrinterDriver::StartDIBits(const RetainPtr& pBitmap, +bool CPSPrinterDriver::StartDIBits(const RetainPtr& pBitmap, int bitmap_alpha, uint32_t color, const CFX_Matrix* pMatrix, @@ -565,7 +565,7 @@ bool CTextOnlyPrinterDriver::DrawPath(const CFX_PathData* pPathData, return false; } -bool CTextOnlyPrinterDriver::SetDIBits(const RetainPtr& pBitmap, +bool CTextOnlyPrinterDriver::SetDIBits(const RetainPtr& pBitmap, uint32_t color, const FX_RECT* pSrcRect, int left, @@ -583,7 +583,7 @@ bool CTextOnlyPrinterDriver::GetClipBox(FX_RECT* pRect) { } bool CTextOnlyPrinterDriver::StretchDIBits( - const RetainPtr& pBitmap, + const RetainPtr& pBitmap, uint32_t color, int dest_left, int dest_top, @@ -596,7 +596,7 @@ bool CTextOnlyPrinterDriver::StretchDIBits( } bool CTextOnlyPrinterDriver::StartDIBits( - const RetainPtr& pBitmap, + const RetainPtr& pBitmap, int bitmap_alpha, uint32_t color, const CFX_Matrix* pMatrix, diff --git a/core/fxge/win32/win32_int.h b/core/fxge/win32/win32_int.h index fe0e7f3ba9..76a1d8800e 100644 --- a/core/fxge/win32/win32_int.h +++ b/core/fxge/win32/win32_int.h @@ -197,13 +197,13 @@ class CGdiDisplayDriver : public CGdiDeviceDriver { bool GetDIBits(const RetainPtr& pBitmap, int left, int top) override; - bool SetDIBits(const RetainPtr& pBitmap, + bool SetDIBits(const RetainPtr& pBitmap, uint32_t color, const FX_RECT* pSrcRect, int left, int top, int blend_type) override; - bool StretchDIBits(const RetainPtr& pBitmap, + bool StretchDIBits(const RetainPtr& pBitmap, uint32_t color, int dest_left, int dest_top, @@ -212,14 +212,14 @@ class CGdiDisplayDriver : public CGdiDeviceDriver { const FX_RECT* pClipRect, uint32_t flags, int blend_type) override; - bool StartDIBits(const RetainPtr& pBitmap, + bool StartDIBits(const RetainPtr& pBitmap, int bitmap_alpha, uint32_t color, const CFX_Matrix* pMatrix, uint32_t render_flags, std::unique_ptr* handle, int blend_type) override; - bool UseFoxitStretchEngine(const RetainPtr& pSource, + bool UseFoxitStretchEngine(const RetainPtr& pSource, uint32_t color, int dest_left, int dest_top, @@ -236,13 +236,13 @@ class CGdiPrinterDriver : public CGdiDeviceDriver { protected: int GetDeviceCaps(int caps_id) const override; - bool SetDIBits(const RetainPtr& pBitmap, + bool SetDIBits(const RetainPtr& pBitmap, uint32_t color, const FX_RECT* pSrcRect, int left, int top, int blend_type) override; - bool StretchDIBits(const RetainPtr& pBitmap, + bool StretchDIBits(const RetainPtr& pBitmap, uint32_t color, int dest_left, int dest_top, @@ -251,7 +251,7 @@ class CGdiPrinterDriver : public CGdiDeviceDriver { const FX_RECT* pClipRect, uint32_t flags, int blend_type) override; - bool StartDIBits(const RetainPtr& pBitmap, + bool StartDIBits(const RetainPtr& pBitmap, int bitmap_alpha, uint32_t color, const CFX_Matrix* pMatrix, @@ -295,13 +295,13 @@ class CPSPrinterDriver : public RenderDeviceDriverIface { int fill_mode, int blend_type) override; bool GetClipBox(FX_RECT* pRect) override; - bool SetDIBits(const RetainPtr& pBitmap, + bool SetDIBits(const RetainPtr& pBitmap, uint32_t color, const FX_RECT* pSrcRect, int left, int top, int blend_type) override; - bool StretchDIBits(const RetainPtr& pBitmap, + bool StretchDIBits(const RetainPtr& pBitmap, uint32_t color, int dest_left, int dest_top, @@ -310,7 +310,7 @@ class CPSPrinterDriver : public RenderDeviceDriverIface { const FX_RECT* pClipRect, uint32_t flags, int blend_type) override; - bool StartDIBits(const RetainPtr& pBitmap, + bool StartDIBits(const RetainPtr& pBitmap, int bitmap_alpha, uint32_t color, const CFX_Matrix* pMatrix, @@ -358,13 +358,13 @@ class CTextOnlyPrinterDriver : public RenderDeviceDriverIface { int fill_mode, int blend_type) override; bool GetClipBox(FX_RECT* pRect) override; - bool SetDIBits(const RetainPtr& pBitmap, + bool SetDIBits(const RetainPtr& pBitmap, uint32_t color, const FX_RECT* pSrcRect, int left, int top, int blend_type) override; - bool StretchDIBits(const RetainPtr& pBitmap, + bool StretchDIBits(const RetainPtr& pBitmap, uint32_t color, int dest_left, int dest_top, @@ -373,7 +373,7 @@ class CTextOnlyPrinterDriver : public RenderDeviceDriverIface { const FX_RECT* pClipRect, uint32_t flags, int blend_type) override; - bool StartDIBits(const RetainPtr& pBitmap, + bool StartDIBits(const RetainPtr& pBitmap, int bitmap_alpha, uint32_t color, const CFX_Matrix* pMatrix, -- cgit v1.2.3