// Copyright 2016 The PDFium Authors // 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/page/cpdf_image.h" #include #include #include #include #include "constants/stream_dict_common.h" #include "core/fpdfapi/page/cpdf_dib.h" #include "core/fpdfapi/page/cpdf_page.h" #include "core/fpdfapi/page/cpdf_pageimagecache.h" #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_boolean.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_document.h" #include "core/fpdfapi/parser/cpdf_name.h" #include "core/fpdfapi/parser/cpdf_number.h" #include "core/fpdfapi/parser/cpdf_reference.h" #include "core/fpdfapi/parser/cpdf_stream.h" #include "core/fpdfapi/parser/cpdf_string.h" #include "core/fxcodec/jpeg/jpegmodule.h" #include "core/fxcrt/data_vector.h" #include "core/fxcrt/fx_2d_size.h" #include "core/fxcrt/fx_memory_wrappers.h" #include "core/fxcrt/fx_stream.h" #include "core/fxcrt/span_util.h" #include "core/fxge/dib/cfx_dibitmap.h" #include "core/fxge/dib/fx_dib.h" #include "third_party/base/check.h" #include "third_party/base/numerics/safe_conversions.h" // static bool CPDF_Image::IsValidJpegComponent(int32_t comps) { return comps == 1 || comps == 3 || comps == 4; } // static bool CPDF_Image::IsValidJpegBitsPerComponent(int32_t bpc) { return bpc == 1 || bpc == 2 || bpc == 4 || bpc == 8 || bpc == 16; } CPDF_Image::CPDF_Image(CPDF_Document* pDoc) : m_pDocument(pDoc) { DCHECK(m_pDocument); } CPDF_Image::CPDF_Image(CPDF_Document* pDoc, RetainPtr pStream) : m_bIsInline(true), m_pDocument(pDoc), m_pStream(std::move(pStream)) { DCHECK(m_pDocument); FinishInitialization(); } CPDF_Image::CPDF_Image(CPDF_Document* pDoc, uint32_t dwStreamObjNum) : m_pDocument(pDoc), m_pStream(ToStream(pDoc->GetMutableIndirectObject(dwStreamObjNum))) { DCHECK(m_pDocument); FinishInitialization(); } CPDF_Image::~CPDF_Image() = default; void CPDF_Image::FinishInitialization() { RetainPtr pStreamDict = m_pStream->GetMutableDict(); m_pOC = pStreamDict->GetMutableDictFor("OC"); m_bIsMask = !pStreamDict->KeyExist("ColorSpace") || pStreamDict->GetBooleanFor("ImageMask", /*bDefault=*/false); m_bInterpolate = !!pStreamDict->GetIntegerFor("Interpolate"); m_Height = pStreamDict->GetIntegerFor("Height"); m_Width = pStreamDict->GetIntegerFor("Width"); } void CPDF_Image::ConvertStreamToIndirectObject() { if (!m_pStream->IsInline()) return; m_pDocument->AddIndirectObject(m_pStream); } RetainPtr CPDF_Image::GetDict() const { return m_pStream ? m_pStream->GetDict() : nullptr; } RetainPtr CPDF_Image::GetStream() const { return m_pStream; } RetainPtr CPDF_Image::GetOC() const { return m_pOC; } RetainPtr CPDF_Image::InitJPEG( pdfium::span src_span) { absl::optional info_opt = JpegModule::LoadInfo(src_span); if (!info_opt.has_value()) return nullptr; const JpegModule::ImageInfo& info = info_opt.value(); if (!IsValidJpegComponent(info.num_components) || !IsValidJpegBitsPerComponent(info.bits_per_components)) { return nullptr; } RetainPtr pDict = CreateXObjectImageDict(info.width, info.height); const char* csname = nullptr; if (info.num_components == 1) { csname = "DeviceGray"; } else if (info.num_components == 3) { csname = "DeviceRGB"; } else if (info.num_components == 4) { csname = "DeviceCMYK"; auto pDecode = pDict->SetNewFor("Decode"); for (int n = 0; n < 4; n++) { pDecode->AppendNew(1); pDecode->AppendNew(0); } } pDict->SetNewFor("ColorSpace", csname); pDict->SetNewFor("BitsPerComponent", info.bits_per_components); pDict->SetNewFor("Filter", "DCTDecode"); if (!info.color_transform) { auto pParms = pDict->SetNewFor(pdfium::stream::kDecodeParms); pParms->SetNewFor("ColorTransform", 0); } m_bIsMask = false; m_Width = info.width; m_Height = info.height; if (!m_pStream) m_pStream = pdfium::MakeRetain(); return pDict; } void CPDF_Image::SetJpegImage(RetainPtr pFile) { uint32_t size = pdfium::base::checked_cast(pFile->GetSize()); if (!size) return; uint32_t dwEstimateSize = std::min(size, 8192U); DataVector data(dwEstimateSize); if (!pFile->ReadBlockAtOffset(data, 0)) return; RetainPtr pDict = InitJPEG(data); if (!pDict && size > dwEstimateSize) { data.resize(size); if (pFile->ReadBlockAtOffset(data, 0)) pDict = InitJPEG(data); } if (!pDict) return; m_pStream->InitStreamFromFile(std::move(pFile), std::move(pDict)); } void CPDF_Image::SetJpegImageInline(RetainPtr pFile) { uint32_t size = pdfium::base::checked_cast(pFile->GetSize()); if (!size) return; DataVector data(size); if (!pFile->ReadBlockAtOffset(data, 0)) return; RetainPtr pDict = InitJPEG(data); if (!pDict) return; m_pStream = pdfium::MakeRetain(std::move(data), std::move(pDict)); } void CPDF_Image::SetImage(const RetainPtr& pBitmap) { int32_t BitmapWidth = pBitmap->GetWidth(); int32_t BitmapHeight = pBitmap->GetHeight(); if (BitmapWidth < 1 || BitmapHeight < 1) return; RetainPtr pDict = CreateXObjectImageDict(BitmapWidth, BitmapHeight); const int32_t bpp = pBitmap->GetBPP(); size_t dest_pitch = 0; bool bCopyWithoutAlpha = true; if (bpp == 1) { int32_t reset_a = 0; int32_t reset_r = 0; int32_t reset_g = 0; int32_t reset_b = 0; int32_t set_a = 0; int32_t set_r = 0; int32_t set_g = 0; int32_t set_b = 0; if (!pBitmap->IsMaskFormat()) { std::tie(reset_a, reset_r, reset_g, reset_b) = ArgbDecode(pBitmap->GetPaletteArgb(0)); std::tie(set_a, set_r, set_g, set_b) = ArgbDecode(pBitmap->GetPaletteArgb(1)); } if (set_a == 0 || reset_a == 0) { pDict->SetNewFor("ImageMask", true); if (reset_a == 0) { auto pArray = pDict->SetNewFor("Decode"); pArray->AppendNew(1); pArray->AppendNew(0); } } else { auto pCS = pDict->SetNewFor("ColorSpace"); pCS->AppendNew("Indexed"); pCS->AppendNew("DeviceRGB"); pCS->AppendNew(1); ByteString ct; { // Span's lifetime must end before ReleaseBuffer() below. pdfium::span pBuf = ct.GetBuffer(6); pBuf[0] = static_cast(reset_r); pBuf[1] = static_cast(reset_g); pBuf[2] = static_cast(reset_b); pBuf[3] = static_cast(set_r); pBuf[4] = static_cast(set_g); pBuf[5] = static_cast(set_b); } ct.ReleaseBuffer(6); pCS->AppendNew(ct, true); } pDict->SetNewFor("BitsPerComponent", 1); dest_pitch = (BitmapWidth + 7) / 8; } else if (bpp == 8) { size_t palette_size = pBitmap->GetRequiredPaletteSize(); if (palette_size > 0) { DCHECK(palette_size <= 256); auto pCS = m_pDocument->NewIndirect(); pCS->AppendNew("Indexed"); pCS->AppendNew("DeviceRGB"); pCS->AppendNew(static_cast(palette_size - 1)); DataVector color_table(Fx2DSizeOrDie(palette_size, 3)); auto color_table_span = pdfium::make_span(color_table); for (size_t i = 0; i < palette_size; i++) { uint32_t argb = pBitmap->GetPaletteArgb(i); color_table_span[0] = FXARGB_R(argb); color_table_span[1] = FXARGB_G(argb); color_table_span[2] = FXARGB_B(argb); color_table_span = color_table_span.subspan(3); } auto pNewDict = m_pDocument->New(); auto pCTS = m_pDocument->NewIndirect(std::move(color_table), std::move(pNewDict)); pCS->AppendNew(m_pDocument, pCTS->GetObjNum()); pDict->SetNewFor("ColorSpace", m_pDocument, pCS->GetObjNum()); } else { pDict->SetNewFor("ColorSpace", "DeviceGray"); } pDict->SetNewFor("BitsPerComponent", 8); dest_pitch = BitmapWidth; } else { pDict->SetNewFor("ColorSpace", "DeviceRGB"); pDict->SetNewFor("BitsPerComponent", 8); dest_pitch = BitmapWidth * 3; bCopyWithoutAlpha = false; } RetainPtr pMaskBitmap; if (pBitmap->IsAlphaFormat()) pMaskBitmap = pBitmap->CloneAlphaMask(); if (pMaskBitmap) { const int32_t mask_width = pMaskBitmap->GetWidth(); const int32_t mask_height = pMaskBitmap->GetHeight(); DataVector mask_buf; RetainPtr pMaskDict = CreateXObjectImageDict(mask_width, mask_height); pMaskDict->SetNewFor("ColorSpace", "DeviceGray"); pMaskDict->SetNewFor("BitsPerComponent", 8); if (pMaskBitmap->GetFormat() != FXDIB_Format::k1bppMask) { mask_buf.resize(Fx2DSizeOrDie(mask_width, mask_height)); for (int32_t a = 0; a < mask_height; a++) { fxcrt::spancpy(pdfium::make_span(mask_buf).subspan(a * mask_width), pMaskBitmap->GetScanline(a).first(mask_width)); } } pMaskDict->SetNewFor( "Length", pdfium::base::checked_cast(mask_buf.size())); auto pNewStream = m_pDocument->NewIndirect( std::move(mask_buf), std::move(pMaskDict)); pDict->SetNewFor("SMask", m_pDocument, pNewStream->GetObjNum()); } DataVector dest_buf(Fx2DSizeOrDie(dest_pitch, BitmapHeight)); pdfium::span dest_span = pdfium::make_span(dest_buf); pdfium::span src_span = pBitmap->GetBuffer(); const int32_t src_pitch = pBitmap->GetPitch(); if (bCopyWithoutAlpha) { for (int32_t i = 0; i < BitmapHeight; i++) { fxcrt::spancpy(dest_span, src_span.first(dest_pitch)); dest_span = dest_span.subspan(dest_pitch); src_span = src_span.subspan(src_pitch); } } else { const size_t src_step = bpp == 24 ? 3 : 4; for (int32_t row = 0; row < BitmapHeight; row++) { uint8_t* dest_ptr = dest_span.data(); const uint8_t* src_ptr = src_span.data(); for (int32_t column = 0; column < BitmapWidth; column++) { dest_ptr[0] = src_ptr[2]; dest_ptr[1] = src_ptr[1]; dest_ptr[2] = src_ptr[0]; dest_ptr += 3; src_ptr += src_step; } dest_span = dest_span.subspan(dest_pitch); src_span = src_span.subspan(src_pitch); } } m_pStream = pdfium::MakeRetain(std::move(dest_buf), std::move(pDict)); m_bIsMask = pBitmap->IsMaskFormat(); m_Width = BitmapWidth; m_Height = BitmapHeight; } void CPDF_Image::ResetCache(CPDF_Page* pPage) { RetainPtr pHolder(this); pPage->GetPageImageCache()->ResetBitmapForImage(std::move(pHolder)); } RetainPtr CPDF_Image::CreateNewDIB() const { return pdfium::MakeRetain(GetDocument(), GetStream()); } RetainPtr CPDF_Image::LoadDIBBase() const { RetainPtr source = CreateNewDIB(); if (!source->Load()) return nullptr; if (!source->IsJBigImage()) return source; CPDF_DIB::LoadState ret = CPDF_DIB::LoadState::kContinue; while (ret == CPDF_DIB::LoadState::kContinue) ret = source->ContinueLoadDIBBase(nullptr); return ret == CPDF_DIB::LoadState::kSuccess ? source : nullptr; } RetainPtr CPDF_Image::DetachBitmap() { return std::move(m_pDIBBase); } RetainPtr CPDF_Image::DetachMask() { return std::move(m_pMask); } bool CPDF_Image::StartLoadDIBBase(const CPDF_Dictionary* pFormResource, const CPDF_Dictionary* pPageResource, bool bStdCS, CPDF_ColorSpace::Family GroupFamily, bool bLoadMask, const CFX_Size& max_size_required) { RetainPtr source = CreateNewDIB(); CPDF_DIB::LoadState ret = source->StartLoadDIBBase(true, pFormResource, pPageResource, bStdCS, GroupFamily, bLoadMask, max_size_required); if (ret == CPDF_DIB::LoadState::kFail) { m_pDIBBase.Reset(); return false; } m_pDIBBase = source; if (ret == CPDF_DIB::LoadState::kContinue) return true; m_pMask = source->DetachMask(); m_MatteColor = source->GetMatteColor(); return false; } bool CPDF_Image::Continue(PauseIndicatorIface* pPause) { RetainPtr pSource = m_pDIBBase.As(); CPDF_DIB::LoadState ret = pSource->ContinueLoadDIBBase(pPause); if (ret == CPDF_DIB::LoadState::kContinue) return true; if (ret == CPDF_DIB::LoadState::kSuccess) { m_pMask = pSource->DetachMask(); m_MatteColor = pSource->GetMatteColor(); } else { m_pDIBBase.Reset(); } return false; } RetainPtr CPDF_Image::CreateXObjectImageDict(int width, int height) { auto dict = m_pDocument->New(); dict->SetNewFor("Type", "XObject"); dict->SetNewFor("Subtype", "Image"); dict->SetNewFor("Width", width); dict->SetNewFor("Height", height); return dict; }