7#include "core/fpdfapi/page/cpdf_dib.h"
16#include "core/fpdfapi/page/cpdf_colorspace.h"
17#include "core/fpdfapi/page/cpdf_docpagedata.h"
18#include "core/fpdfapi/page/cpdf_image.h"
19#include "core/fpdfapi/page/cpdf_imageobject.h"
20#include "core/fpdfapi/page/cpdf_indexedcs.h"
21#include "core/fpdfapi/parser/cpdf_array.h"
22#include "core/fpdfapi/parser/cpdf_dictionary.h"
23#include "core/fpdfapi/parser/cpdf_document.h"
24#include "core/fpdfapi/parser/cpdf_name.h"
25#include "core/fpdfapi/parser/cpdf_number.h"
26#include "core/fpdfapi/parser/cpdf_stream.h"
27#include "core/fpdfapi/parser/cpdf_stream_acc.h"
28#include "core/fpdfapi/parser/fpdf_parser_decode.h"
29#include "core/fpdfapi/parser/fpdf_parser_utility.h"
30#include "core/fxcodec/basic/basicmodule.h"
31#include "core/fxcodec/icc/icc_transform.h"
32#include "core/fxcodec/jbig2/jbig2_decoder.h"
33#include "core/fxcodec/jpeg/jpegmodule.h"
34#include "core/fxcodec/jpx/cjpx_decoder.h"
35#include "core/fxcodec/scanlinedecoder.h"
36#include "core/fxcrt/check.h"
37#include "core/fxcrt/check_op.h"
38#include "core/fxcrt/compiler_specific.h"
39#include "core/fxcrt/data_vector.h"
40#include "core/fxcrt/fx_memcpy_wrappers.h"
41#include "core/fxcrt/fx_safe_types.h"
42#include "core/fxcrt/span_util.h"
43#include "core/fxcrt/stl_util.h"
44#include "core/fxcrt/zip.h"
45#include "core/fxge/calculate_pitch.h"
46#include "core/fxge/dib/cfx_dibitmap.h"
50bool IsValidDimension(
int value) {
51 constexpr int kMaxImageDimension = 0x01FFFF;
52 return value > 0 && value <= kMaxImageDimension;
55unsigned int GetBits8(pdfium::span<
const uint8_t> pData,
58 DCHECK(nbits == 1 || nbits == 2 || nbits == 4 || nbits == 8 || nbits == 16);
60 unsigned int byte = pData[bitpos / 8];
65 return byte * 256 + pData[bitpos / 8 + 1];
67 return (byte >> (8 - nbits - (bitpos % 8))) & ((1 << nbits) - 1);
70bool GetBitValue(pdfium::span<
const uint8_t> pSrc, uint32_t pos) {
71 return pSrc[pos / 8] & (1 << (7 - pos % 8));
75bool IsMaybeValidBitsPerComponent(
int bpc) {
76 return bpc >= 0 && bpc <= 16;
79bool IsAllowedBitsPerComponent(
int bpc) {
80 return bpc == 1 || bpc == 2 || bpc == 4 || bpc == 8 || bpc == 16;
83bool IsColorIndexOutOfBounds(uint8_t index,
const DIB_COMP_DATA& comp_datum) {
87bool AreColorIndicesOutOfBounds(
const uint8_t* indices,
91 for (size_t i = 0; i < count; ++i) {
92 if (IsColorIndexOutOfBounds(indices[i], comp_data[i])) {
111enum class JpxDecodeAction {
123bool IsJPXColorSpaceOrUnspecifiedOrUnknown(COLOR_SPACE actual,
124 COLOR_SPACE expected) {
125 return actual == expected || actual == OPJ_CLRSPC_UNSPECIFIED ||
126 actual == OPJ_CLRSPC_UNKNOWN;
132JpxDecodeAction GetJpxDecodeActionFromColorSpaces(
135 if (pdf_colorspace ==
137 if (!IsJPXColorSpaceOrUnspecifiedOrUnknown(jpx_info
.colorspace,
139 return JpxDecodeAction::kFail;
141 return JpxDecodeAction::kUseGray;
144 if (pdf_colorspace ==
146 if (!IsJPXColorSpaceOrUnspecifiedOrUnknown(jpx_info
.colorspace,
148 return JpxDecodeAction::kFail;
154 return JpxDecodeAction::kConvertArgbToRgb;
156 return JpxDecodeAction::kUseRgb;
159 if (pdf_colorspace ==
161 if (!IsJPXColorSpaceOrUnspecifiedOrUnknown(jpx_info
.colorspace,
163 return JpxDecodeAction::kFail;
165 return JpxDecodeAction::kUseCmyk;
172 return JpxDecodeAction::kConvertArgbToRgb;
175 return JpxDecodeAction::kDoNothing;
178JpxDecodeAction GetJpxDecodeActionFromImageColorSpace(
181 case OPJ_CLRSPC_SYCC:
182 case OPJ_CLRSPC_EYCC:
183 case OPJ_CLRSPC_UNKNOWN:
184 case OPJ_CLRSPC_UNSPECIFIED:
185 return JpxDecodeAction::kDoNothing;
187 case OPJ_CLRSPC_SRGB:
189 return JpxDecodeAction::kConvertArgbToRgb;
192 return JpxDecodeAction::kUseRgb;
194 case OPJ_CLRSPC_GRAY:
195 return JpxDecodeAction::kUseGray;
197 case OPJ_CLRSPC_CMYK:
198 return JpxDecodeAction::kUseCmyk;
204 if (pdf_colorspace) {
205 return GetJpxDecodeActionFromColorSpaces(jpx_info, pdf_colorspace);
209 return GetJpxDecodeActionFromImageColorSpace(jpx_info);
212int GetComponentCountFromOpjColorSpace(OPJ_COLOR_SPACE colorspace) {
213 switch (colorspace) {
214 case OPJ_CLRSPC_GRAY:
217 case OPJ_CLRSPC_SRGB:
218 case OPJ_CLRSPC_SYCC:
219 case OPJ_CLRSPC_EYCC:
222 case OPJ_CLRSPC_CMYK:
233 : m_pDocument(pDoc), m_pStream(std::move(pStream)) {}
237CPDF_DIB::JpxSMaskInlineData::JpxSMaskInlineData() =
default;
239CPDF_DIB::JpxSMaskInlineData::~JpxSMaskInlineData() =
default;
242 if (!LoadInternal(
nullptr,
nullptr))
248 return ContinueInternal();
251bool CPDF_DIB::ContinueToLoadMask() {
252 if (m_pColorSpace && m_bStdCS)
253 m_pColorSpace->EnableStdConversion(
true);
255 return ContinueInternal();
258bool CPDF_DIB::ContinueInternal() {
262 const uint32_t bpp = m_bpc * m_nComponents;
269 }
else if (bpp <= 8) {
276 std::optional<uint32_t> pitch = fxge::CalculatePitch32(GetBPP(), GetWidth());
277 if (!pitch.has_value())
280 m_LineBuf = DataVector<uint8_t>(pitch.value());
287 if (!pitch.has_value())
289 m_MaskBuf = DataVector<uint8_t>(pitch.value());
302 const CFX_Size& max_size_required) {
304 m_bHasMask = bHasMask;
305 m_GroupFamily = GroupFamily;
306 m_bLoadMask = bLoadMask;
308 if (!m_pStream->IsInline())
309 pFormResources =
nullptr;
311 if (!LoadInternal(pFormResources, pPageResources))
314 uint8_t resolution_levels_to_skip = 0;
315 if (max_size_required.width != 0 && max_size_required.height != 0) {
316 resolution_levels_to_skip =
static_cast<uint8_t>(
std::log2(
318 GetHeight() / max_size_required.height))));
321 LoadState iCreatedDecoder = CreateDecoder(resolution_levels_to_skip);
325 if (!ContinueToLoadMask())
336 if (m_pColorSpace && m_bStdCS)
337 m_pColorSpace->EnableStdConversion(
false);
343 return ContinueLoadMaskDIB(pPause);
345 ByteString decoder = m_pStreamAcc->GetImageDecoder();
346 if (decoder
== "JPXDecode")
349 if (decoder
!= "JBIG2Decode")
356 if (!m_pJbig2Context) {
357 m_pJbig2Context = std::make_unique<Jbig2Context>();
358 if (m_pStreamAcc->GetImageParam()) {
360 m_pStreamAcc->GetImageParam()->GetStreamFor(
"JBIG2Globals");
362 m_pGlobalAcc = pdfium::MakeRetain<CPDF_StreamAcc>(std::move(pGlobals));
363 m_pGlobalAcc->LoadAllDataFiltered();
366 uint64_t nSrcKey = 0;
367 pdfium::span<
const uint8_t> pSrcSpan;
369 pSrcSpan = m_pStreamAcc->GetSpan();
370 nSrcKey = m_pStreamAcc->KeyForCache();
372 uint64_t nGlobalKey = 0;
373 pdfium::span<
const uint8_t> pGlobalSpan;
375 pGlobalSpan = m_pGlobalAcc->GetSpan();
376 nGlobalKey = m_pGlobalAcc->KeyForCache();
378 iDecodeStatus = Jbig2Decoder::StartDecode(
379 m_pJbig2Context.get(), m_pDocument->GetOrCreateCodecContext(),
380 GetWidth(), GetHeight(), pSrcSpan, nSrcKey, pGlobalSpan, nGlobalKey,
381 m_pCachedBitmap->GetWritableBuffer(), m_pCachedBitmap->GetPitch(),
384 iDecodeStatus = Jbig2Decoder::ContinueDecode(m_pJbig2Context.get(), pPause);
388 m_pJbig2Context.reset();
389 m_pCachedBitmap.Reset();
390 m_pGlobalAcc.Reset();
406 if (m_pColorSpace && m_bStdCS)
407 m_pColorSpace->EnableStdConversion(
false);
408 return iContinueStatus;
413 std::optional<DecoderArray> decoder_array = GetDecoderArray(m_pDict);
414 if (!decoder_array.has_value())
417 m_bpc_orig = m_pDict->GetIntegerFor(
"BitsPerComponent");
418 if (!IsMaybeValidBitsPerComponent(m_bpc_orig))
421 m_bImageMask = m_pDict->GetBooleanFor(
"ImageMask",
false);
423 if (m_bImageMask || !m_pDict->KeyExist(
"ColorSpace")) {
424 if (!m_bImageMask && !decoder_array.value().empty()) {
425 const ByteString& filter = decoder_array.value().back().first;
426 if (filter
== "JPXDecode") {
427 m_bDoBpcCheck =
false;
432 m_bpc = m_nComponents = 1;
434 m_bDefaultDecode = !pDecode || !pDecode->GetIntegerAt(0);
439 m_pDict->GetDirectObjectFor(
"ColorSpace");
443 auto* pDocPageData = CPDF_DocPageData::FromDocument(m_pDocument);
445 m_pColorSpace = pDocPageData->GetColorSpace(pCSObj.Get(), pFormResources);
447 m_pColorSpace = pDocPageData->GetColorSpace(pCSObj.Get(), pPageResources);
455 m_nComponents = m_pColorSpace->ComponentCount();
456 m_Family = m_pColorSpace->GetFamily();
459 if (cs
== "DeviceGray")
461 else if (cs
== "DeviceRGB")
463 else if (cs
== "DeviceCMYK")
468 if (!decoder_array.value().empty())
469 filter = decoder_array.value().back().first;
471 if (!ValidateDictParam(filter))
474 return GetDecodeAndMaskArray();
477bool CPDF_DIB::GetDecodeAndMaskArray() {
481 m_CompData.resize(m_nComponents);
482 int max_data = (1 << m_bpc) - 1;
485 for (uint32_t i = 0; i < m_nComponents; i++) {
486 m_CompData[i].m_DecodeMin = pDecode->GetFloatAt(i * 2);
487 float max = pDecode->GetFloatAt(i * 2 + 1);
488 m_CompData[i].m_DecodeStep = (max - m_CompData[i].m_DecodeMin) / max_data;
492 m_pColorSpace->GetDefaultValue(i, &def_value, &def_min, &def_max);
495 if (def_min != m_CompData[i].m_DecodeMin || def_max != max)
496 m_bDefaultDecode =
false;
499 for (uint32_t i = 0; i < m_nComponents; i++) {
501 m_pColorSpace->GetDefaultValue(i, &def_value, &m_CompData[i].m_DecodeMin,
502 &m_CompData[i].m_DecodeStep);
503 if (m_Family == CPDF_ColorSpace::Family::kIndexed)
504 m_CompData[i].m_DecodeStep = max_data;
505 m_CompData[i].m_DecodeStep =
506 (m_CompData[i].m_DecodeStep - m_CompData[i].m_DecodeMin) / max_data;
509 if (m_pDict->KeyExist(
"SMask"))
516 if (
const CPDF_Array* pArray = pMask->AsArray()) {
517 if (pArray->size() >= m_nComponents * 2) {
518 for (uint32_t i = 0; i < m_nComponents; i++) {
519 int min_num = pArray->GetIntegerAt(i * 2);
520 int max_num = pArray->GetIntegerAt(i * 2 + 1);
521 m_CompData[i].m_ColorKeyMin = std::max(min_num, 0);
522 m_CompData[i].m_ColorKeyMax = std::min(max_num, max_data);
530CPDF_DIB::
LoadState CPDF_DIB::CreateDecoder(uint8_t resolution_levels_to_skip) {
531 ByteString decoder = m_pStreamAcc->GetImageDecoder();
532 if (decoder.IsEmpty())
535 if (m_bDoBpcCheck && m_bpc == 0)
538 if (decoder
== "JPXDecode") {
539 m_pCachedBitmap = LoadJpxBitmap(resolution_levels_to_skip);
540 return m_pCachedBitmap ? LoadState::kSuccess : LoadState::kFail;
543 if (decoder
== "JBIG2Decode") {
544 m_pCachedBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
545 if (!m_pCachedBitmap->Create(
546 GetWidth(), GetHeight(),
547 m_bImageMask ? FXDIB_Format::k1bppMask : FXDIB_Format::k1bppRgb)) {
548 m_pCachedBitmap.Reset();
555 pdfium::span<
const uint8_t> src_span = m_pStreamAcc->GetSpan();
557 if (decoder
== "CCITTFaxDecode") {
558 m_pDecoder = CreateFaxDecoder(src_span, GetWidth(), GetHeight(), pParams);
559 }
else if (decoder
== "FlateDecode") {
560 m_pDecoder = CreateFlateDecoder(src_span, GetWidth(), GetHeight(),
561 m_nComponents, m_bpc, pParams);
562 }
else if (decoder
== "RunLengthDecode") {
563 m_pDecoder = BasicModule::CreateRunLengthDecoder(
564 src_span, GetWidth(), GetHeight(), m_nComponents, m_bpc);
565 }
else if (decoder
== "DCTDecode") {
566 if (!CreateDCTDecoder(src_span, pParams))
572 const std::optional<uint32_t> requested_pitch =
573 fxge::CalculatePitch8(m_bpc, m_nComponents, GetWidth());
574 if (!requested_pitch.has_value())
576 const std::optional<uint32_t> provided_pitch = fxge::CalculatePitch8(
577 m_pDecoder->GetBPC(), m_pDecoder->CountComps(), m_pDecoder->GetWidth());
578 if (!provided_pitch.has_value())
580 if (provided_pitch.value() < requested_pitch.value())
585bool CPDF_DIB::CreateDCTDecoder(pdfium::span<
const uint8_t> src_span,
587 m_pDecoder = JpegModule::CreateDecoder(
588 src_span, GetWidth(), GetHeight(), m_nComponents,
589 !pParams || pParams->GetIntegerFor(
"ColorTransform", 1));
594 JpegModule::LoadInfo(src_span);
595 if (!info_opt.has_value())
609 m_pDecoder = JpegModule::CreateDecoder(src_span, GetWidth(), GetHeight(),
610 m_nComponents, info.color_transform);
617 uint32_t colorspace_comps = m_pColorSpace->ComponentCount();
623 if (colorspace_comps < dwMinComps || m_nComponents < dwMinComps)
628 if (m_nComponents != 3 || colorspace_comps < 3)
635 colorspace_comps < m_nComponents) {
641 if (colorspace_comps != m_nComponents)
650 if (!GetDecodeAndMaskArray())
654 m_pDecoder = JpegModule::CreateDecoder(src_span, GetWidth(), GetHeight(),
655 m_nComponents, info.color_transform);
659RetainPtr<CFX_DIBitmap> CPDF_DIB::LoadJpxBitmap(
660 uint8_t resolution_levels_to_skip) {
661 std::unique_ptr<CJPX_Decoder> decoder =
662 CJPX_Decoder::Create(m_pStreamAcc->GetSpan(),
663 ColorSpaceOptionFromColorSpace(m_pColorSpace.Get()),
664 resolution_levels_to_skip,
true);
671 if (!decoder->StartDecode())
681 bool swap_rgb =
false;
682 bool convert_argb_to_rgb =
false;
683 auto action = GetJpxDecodeAction(image_info, m_pColorSpace.Get());
685 case JpxDecodeAction::kFail:
688 case JpxDecodeAction::kDoNothing:
691 case JpxDecodeAction::kUseGray:
693 CPDF_ColorSpace::GetStockCS(CPDF_ColorSpace::Family::kDeviceGray);
696 case JpxDecodeAction::kUseRgb:
697 DCHECK(image_info.channels >= 3);
699 m_pColorSpace =
nullptr;
702 case JpxDecodeAction::kUseCmyk:
704 CPDF_ColorSpace::GetStockCS(CPDF_ColorSpace::Family::kDeviceCMYK);
707 case JpxDecodeAction::kConvertArgbToRgb:
709 convert_argb_to_rgb =
true;
710 m_pColorSpace.Reset();
715 if (original_colorspace) {
719 m_nComponents = GetComponentCountFromOpjColorSpace(image_info
.colorspace);
720 if (m_nComponents == 0) {
726 if (action == JpxDecodeAction::kUseGray) {
728 }
else if (action == JpxDecodeAction::kUseRgb && image_info
.channels == 3) {
730 }
else if (action == JpxDecodeAction::kUseRgb && image_info
.channels == 4) {
732 }
else if (action == JpxDecodeAction::kConvertArgbToRgb) {
740 auto result_bitmap =
pdfium::MakeRetain<CFX_DIBitmap>();
741 if (!result_bitmap->Create(image_info
.width, image_info
.height, format))
744 result_bitmap->Clear(0xFFFFFFFF);
745 if (!decoder->Decode(result_bitmap->GetWritableBuffer(),
746 result_bitmap->GetPitch(), swap_rgb, m_nComponents)) {
750 if (convert_argb_to_rgb) {
752 auto rgb_bitmap =
pdfium::MakeRetain<CFX_DIBitmap>();
757 if (m_pDict->GetIntegerFor(
"SMaskInData") == 1) {
760 DCHECK(m_JpxInlineData.data.empty());
761 m_JpxInlineData.width = image_info.width;
762 m_JpxInlineData.height = image_info.height;
763 m_JpxInlineData.data.reserve(image_info.width * image_info.height);
764 for (uint32_t row = 0; row < image_info
.height; ++row) {
769 rgb_bitmap->GetWritableScanlineAs<
FX_BGR_STRUCT<uint8_t>>(row);
770 for (
auto [input, output] : fxcrt::Zip(src, dest)) {
771 m_JpxInlineData.data.push_back(input.alpha);
772 const uint8_t na = 255 - input.alpha;
773 output.blue = (input.blue * input.alpha + 255 * na) / 255;
774 output.green = (input.green * input.alpha + 255 * na) / 255;
775 output.red = (input.red * input.alpha + 255 * na) / 255;
780 for (uint32_t row = 0; row < image_info
.height; ++row) {
785 rgb_bitmap->GetWritableScanlineAs<
FX_BGR_STRUCT<uint8_t>>(row);
786 for (
auto [input, output] : fxcrt::Zip(src, dest)) {
787 output.green = input.green;
788 output.red = input.red;
789 output.blue = input.blue;
793 result_bitmap =
std::move(rgb_bitmap);
794 }
else if (m_pColorSpace &&
795 m_pColorSpace->GetFamily() == CPDF_ColorSpace::Family::kIndexed &&
797 int scale = 8 - m_bpc;
798 for (uint32_t row = 0; row < image_info
.height; ++row) {
799 pdfium::span<uint8_t> scanline =
800 result_bitmap->GetWritableScanline(row).first(image_info
.width);
801 for (
auto& pixel : scanline) {
811 return result_bitmap;
819 m_pDict = m_pStream->GetDict();
820 SetWidth(m_pDict->GetIntegerFor(
"Width"));
821 SetHeight(m_pDict->GetIntegerFor(
"Height"));
826 if (!LoadColorInfo(pFormResources, pPageResources))
829 if (m_bDoBpcCheck && (m_bpc == 0 || m_nComponents == 0))
832 const std::optional<uint32_t> maybe_size =
833 fxge::CalculatePitch8(m_bpc, m_nComponents, GetWidth());
834 if (!maybe_size.has_value())
839 if (!src_size.IsValid())
842 m_pStreamAcc = pdfium::MakeRetain<CPDF_StreamAcc>(m_pStream);
843 m_pStreamAcc->LoadAllDataImageAcc(src_size.ValueOrDie());
844 return !m_pStreamAcc->GetSpan().empty();
847CPDF_DIB::
LoadState CPDF_DIB::StartLoadMask() {
848 m_MatteColor = 0XFFFFFFFF;
850 if (!m_JpxInlineData.data.empty()) {
852 dict->SetNewFor<CPDF_Name>(
"Type",
"XObject");
853 dict->SetNewFor<CPDF_Name>(
"Subtype",
"Image");
854 dict->SetNewFor<CPDF_Name>(
"ColorSpace",
"DeviceGray");
855 dict->SetNewFor<CPDF_Number>(
"Width", m_JpxInlineData.width);
856 dict->SetNewFor<CPDF_Number>(
"Height", m_JpxInlineData.height);
857 dict->SetNewFor<CPDF_Number>(
"BitsPerComponent", 8);
859 return StartLoadMaskDIB(
860 pdfium::MakeRetain<CPDF_Stream>(m_JpxInlineData.data, std::move(dict)));
863 RetainPtr<
const CPDF_Stream> mask(m_pDict->GetStreamFor(
"SMask"));
865 mask = ToStream(m_pDict->GetDirectObjectFor(
"Mask"));
870 if (pMatte && m_pColorSpace &&
871 m_Family != CPDF_ColorSpace::Family::kPattern &&
872 pMatte->size() == m_nComponents &&
873 m_pColorSpace->ComponentCount() <= m_nComponents) {
874 std::vector<
float> colors =
875 ReadArrayElementsToVector(pMatte.Get(), m_nComponents);
877 auto rgb = m_pColorSpace->GetRGBOrZerosOnError(colors);
879 ArgbEncode(0, FXSYS_roundf(rgb.red * 255),
880 FXSYS_roundf(rgb.green * 255), FXSYS_roundf(rgb.blue * 255));
882 return StartLoadMaskDIB(
std::move(mask));
889 LoadState ret = m_pMask->ContinueLoadDIBBase(pPause);
893 if (m_pColorSpace && m_bStdCS)
894 m_pColorSpace->EnableStdConversion(
false);
904 return std::move(m_pMask);
908 return m_pStreamAcc->GetImageDecoder() ==
"JBIG2Decode";
911CPDF_DIB::
LoadState CPDF_DIB::StartLoadMaskDIB(
912 RetainPtr<
const CPDF_Stream> mask_stream) {
913 m_pMask = pdfium::MakeRetain<CPDF_DIB>(m_pDocument, std::move(mask_stream));
914 LoadState ret = m_pMask->StartLoadDIBBase(
false,
nullptr,
nullptr,
true,
915 CPDF_ColorSpace::Family::kUnknown,
922 if (ret == LoadState::kFail)
927void CPDF_DIB::LoadPalette() {
928 if (!m_pColorSpace || m_Family == CPDF_ColorSpace::Family::kPattern)
937 safe_bits *= m_nComponents;
938 uint32_t bits = safe_bits.ValueOrDefault(255);
947 if (m_pColorSpace->ComponentCount() > 3) {
950 float color_values[3];
951 std::fill(std::begin(color_values), std::end(color_values),
952 m_CompData[0].m_DecodeMin);
954 auto rgb = m_pColorSpace->GetRGBOrZerosOnError(color_values);
956 ArgbEncode(255, FXSYS_roundf(rgb.red * 255),
957 FXSYS_roundf(rgb.green * 255), FXSYS_roundf(rgb.blue * 255));
959 const CPDF_IndexedCS* indexed_cs = m_pColorSpace->AsIndexedCS();
966 color_values[0] += m_CompData[0].m_DecodeStep;
967 color_values[1] += m_CompData[0].m_DecodeStep;
968 color_values[2] += m_CompData[0].m_DecodeStep;
969 auto result = m_pColorSpace->GetRGBOrZerosOnError(color_values);
970 argb1 = ArgbEncode(255, FXSYS_roundf(result.red * 255),
971 FXSYS_roundf(result.green * 255),
972 FXSYS_roundf(result.blue * 255));
975 if (argb0 != 0xFF000000 || argb1 != 0xFFFFFFFF) {
981 if (m_bpc == 8 && m_bDefaultDecode &&
983 CPDF_ColorSpace::GetStockCS(CPDF_ColorSpace::Family::kDeviceGray)) {
987 int palette_count = 1 << bits;
989 std::vector<
float> color_values(
std::max(m_nComponents, 16u));
990 for (
int i = 0; i < palette_count; i++) {
992 for (uint32_t j = 0; j < m_nComponents; j++) {
993 int encoded_component = color_data % (1 << m_bpc);
994 color_data /= 1 << m_bpc;
995 color_values[j] = m_CompData[j].m_DecodeMin +
996 m_CompData[j].m_DecodeStep * encoded_component;
999 if (m_nComponents == 1 && m_Family == CPDF_ColorSpace::Family::kICCBased &&
1000 m_pColorSpace->ComponentCount() > 1) {
1001 const size_t nComponents = m_pColorSpace->ComponentCount();
1002 std::vector<
float> temp_buf(nComponents, color_values[0]);
1003 rgb = m_pColorSpace->GetRGBOrZerosOnError(temp_buf);
1005 rgb = m_pColorSpace->GetRGBOrZerosOnError(color_values);
1008 FXSYS_roundf(rgb.green * 255),
1009 FXSYS_roundf(rgb.blue * 255))
);
1013bool CPDF_DIB::ValidateDictParam(
const ByteString& filter) {
1019 if (filter
== "JPXDecode") {
1020 m_bDoBpcCheck =
false;
1024 if (filter
== "CCITTFaxDecode" || filter
== "JBIG2Decode") {
1027 }
else if (filter
== "DCTDecode") {
1031 if (!IsAllowedBitsPerComponent(m_bpc)) {
1038void CPDF_DIB::TranslateScanline24bpp(
1039 pdfium::span<uint8_t> dest_scan,
1040 pdfium::span<
const uint8_t> src_scan)
const {
1044 if (TranslateScanline24bppDefaultDecode(dest_scan, src_scan))
1048 std::vector<
float> color_values(
std::max(m_nComponents, 16u));
1050 uint64_t src_bit_pos = 0;
1051 uint64_t src_byte_pos = 0;
1052 size_t dest_byte_pos = 0;
1053 const bool bpp8 = m_bpc == 8;
1054 for (
int column = 0; column <
GetWidth(); column++) {
1055 for (uint32_t color = 0; color < m_nComponents; color++) {
1057 uint8_t data = src_scan[src_byte_pos++];
1058 color_values[color] = m_CompData[color].m_DecodeMin +
1059 m_CompData[color].m_DecodeStep * data;
1061 unsigned int data = GetBits8(src_scan, src_bit_pos, m_bpc);
1062 color_values[color] = m_CompData[color].m_DecodeMin +
1063 m_CompData[color].m_DecodeStep * data;
1064 src_bit_pos += m_bpc;
1068 float k = 1.0f - color_values[3];
1069 rgb.red = (1.0f - color_values[0]) * k;
1070 rgb.green = (1.0f - color_values[1]) * k;
1071 rgb.blue = (1.0f - color_values[2]) * k;
1073 rgb = m_pColorSpace->GetRGBOrZerosOnError(color_values);
1075 const float R =
std::clamp(rgb.red, 0.0f, 1.0f);
1076 const float G =
std::clamp(rgb.green, 0.0f, 1.0f);
1077 const float B =
std::clamp(rgb.blue, 0.0f, 1.0f);
1078 dest_scan[dest_byte_pos] =
static_cast<uint8_t>(B * 255);
1079 dest_scan[dest_byte_pos + 1] =
static_cast<uint8_t>(G * 255);
1080 dest_scan[dest_byte_pos + 2] =
static_cast<uint8_t>(R * 255);
1085bool CPDF_DIB::TranslateScanline24bppDefaultDecode(
1086 pdfium::span<uint8_t> dest_scan,
1087 pdfium::span<
const uint8_t> src_scan)
const {
1088 if (!m_bDefaultDecode)
1096 if (m_nComponents == m_pColorSpace->ComponentCount()) {
1097 m_pColorSpace->TranslateImageLine(dest_scan, src_scan, GetWidth(),
1098 GetWidth(), GetHeight(), TransMask());
1103 if (m_nComponents != 3)
1106 uint8_t* dest_pos = dest_scan.data();
1107 const uint8_t* src_pos = src_scan.data();
1111 for (
int column = 0; column <
GetWidth(); column++) {
1112 *dest_pos++ = src_pos[2];
1113 *dest_pos++ = src_pos[1];
1114 *dest_pos++ = *src_pos;
1121 for (
int col = 0; col <
GetWidth(); col++) {
1122 *dest_pos++ = src_pos[4];
1123 *dest_pos++ = src_pos[2];
1124 *dest_pos++ = *src_pos;
1130 const unsigned int max_data = (1 << m_bpc) - 1;
1131 uint64_t src_bit_pos = 0;
1132 size_t dest_byte_pos = 0;
1133 for (
int column = 0; column <
GetWidth(); column++) {
1134 unsigned int R = GetBits8(src_scan, src_bit_pos, m_bpc);
1135 src_bit_pos += m_bpc;
1136 unsigned int G = GetBits8(src_scan, src_bit_pos, m_bpc);
1137 src_bit_pos += m_bpc;
1138 unsigned int B = GetBits8(src_scan, src_bit_pos, m_bpc);
1139 src_bit_pos += m_bpc;
1140 R =
std::min(R, max_data);
1141 G =
std::min(G, max_data);
1142 B =
std::min(B, max_data);
1144 dest_pos[dest_byte_pos] = B * 255 / max_data;
1145 dest_pos[dest_byte_pos + 1] = G * 255 / max_data;
1146 dest_pos[dest_byte_pos + 2] = R * 255 / max_data;
1157 return pdfium::span<
const uint8_t>();
1159 const std::optional<uint32_t> src_pitch =
1160 fxge::CalculatePitch8(m_bpc, m_nComponents, GetWidth());
1161 if (!src_pitch.has_value())
1162 return pdfium::span<
const uint8_t>();
1164 uint32_t src_pitch_value = src_pitch.value();
1167 DataVector<uint8_t> temp_buffer;
1168 pdfium::span<
const uint8_t> pSrcLine;
1170 if (m_pCachedBitmap && src_pitch_value <= m_pCachedBitmap->GetPitch()) {
1171 if (line >= m_pCachedBitmap->GetHeight())
1172 line = m_pCachedBitmap->GetHeight() - 1;
1173 pSrcLine = m_pCachedBitmap->GetScanline(line);
1174 }
else if (m_pDecoder) {
1175 pSrcLine = m_pDecoder->GetScanline(line);
1176 }
else if (m_pStreamAcc->GetSize() > line * src_pitch_value) {
1177 pdfium::span<
const uint8_t> remaining_bytes =
1178 m_pStreamAcc->GetSpan().subspan(line * src_pitch_value);
1179 if (remaining_bytes.size() >= src_pitch_value) {
1180 pSrcLine = remaining_bytes.first(src_pitch_value);
1182 temp_buffer = DataVector<uint8_t>(src_pitch_value);
1183 fxcrt::Copy(remaining_bytes, temp_buffer);
1184 pSrcLine = temp_buffer;
1188 if (pSrcLine.empty()) {
1189 pdfium::span<uint8_t> result = !m_MaskBuf.empty() ? m_MaskBuf : m_LineBuf;
1190 fxcrt::Fill(result, 0);
1193 if (m_bpc * m_nComponents == 1) {
1194 if (m_bImageMask && m_bDefaultDecode) {
1195 for (uint32_t i = 0; i < src_pitch_value; i++) {
1199 return pdfium::make_span(m_LineBuf).first(src_pitch_value);
1202 fxcrt::Copy(pSrcLine.first(src_pitch_value), m_LineBuf);
1203 return pdfium::make_span(m_LineBuf).first(src_pitch_value);
1205 uint32_t reset_argb = Get1BitResetValue();
1206 uint32_t set_argb = Get1BitSetValue();
1208 fxcrt::reinterpret_span<uint32_t>(pdfium::make_span(m_MaskBuf));
1209 for (
int col = 0; col <
GetWidth(); col++) {
1210 mask32_span[col] = GetBitValue(pSrcLine, col) ? set_argb : reset_argb;
1212 return fxcrt::reinterpret_span<uint8_t>(mask32_span.first(
GetWidth()));
1214 if (m_bpc * m_nComponents <= 8) {
1215 pdfium::span<uint8_t> result = m_LineBuf;
1217 fxcrt::Copy(pSrcLine.first(src_pitch_value), result);
1218 result = result.first(src_pitch_value);
1220 uint64_t src_bit_pos = 0;
1221 for (
int col = 0; col <
GetWidth(); col++) {
1222 unsigned int color_index = 0;
1223 for (uint32_t color = 0; color < m_nComponents; color++) {
1224 unsigned int data = GetBits8(pSrcLine, src_bit_pos, m_bpc);
1225 color_index |= data << (color * m_bpc);
1226 src_bit_pos += m_bpc;
1228 m_LineBuf[col] = color_index;
1235 uint8_t* pDestPixel = m_MaskBuf.data();
1236 const uint8_t* pSrcPixel = m_LineBuf.data();
1237 pdfium::span<
const uint32_t> palette = GetPaletteSpan();
1240 for (
int col = 0; col <
GetWidth(); col++) {
1241 uint8_t index = *pSrcPixel++;
1242 *pDestPixel++ =
FXARGB_B(palette[index]);
1243 *pDestPixel++ =
FXARGB_G(palette[index]);
1244 *pDestPixel++ =
FXARGB_R(palette[index]);
1246 IsColorIndexOutOfBounds(index, m_CompData[0]) ? 0xFF : 0;
1249 for (
int col = 0; col <
GetWidth(); col++) {
1250 uint8_t index = *pSrcPixel++;
1251 *pDestPixel++ = index;
1252 *pDestPixel++ = index;
1253 *pDestPixel++ = index;
1255 IsColorIndexOutOfBounds(index, m_CompData[0]) ? 0xFF : 0;
1259 return pdfium::make_span(m_MaskBuf).first(4 * GetWidth());
1262 if (m_nComponents == 3 && m_bpc == 8) {
1264 uint8_t* alpha_channel = m_MaskBuf.data() + 3;
1265 for (
int col = 0; col <
GetWidth(); col++) {
1266 const uint8_t* pPixel = pSrcLine.data() + col * 3;
1267 alpha_channel[col * 4] =
1268 AreColorIndicesOutOfBounds(pPixel, m_CompData.data(), 3) ? 0xFF
1273 fxcrt::Fill(m_MaskBuf, 0xFF);
1276 if (m_pColorSpace) {
1277 TranslateScanline24bpp(m_LineBuf, pSrcLine);
1279 pSrcLine = pdfium::make_span(m_LineBuf).first(src_pitch_value);
1285 const uint8_t* pSrcPixel = pSrcLine.data();
1286 uint8_t* pDestPixel = m_MaskBuf.data();
1288 for (
int col = 0; col <
GetWidth(); col++) {
1289 *pDestPixel++ = *pSrcPixel++;
1290 *pDestPixel++ = *pSrcPixel++;
1291 *pDestPixel++ = *pSrcPixel++;
1295 return pdfium::make_span(m_MaskBuf).first(4 * GetWidth());
1299 return m_pDecoder && m_pDecoder->SkipToScanline(line, pPause);
1303 return m_pCachedBitmap ? m_pCachedBitmap->GetEstimatedImageMemoryBurden() : 0;
1306bool CPDF_DIB::TransMask()
const {
1311void CPDF_DIB::SetMaskProperties() {
1317uint32_t CPDF_DIB::Get1BitSetValue()
const {
1318 if (m_CompData[0].m_ColorKeyMax == 1)
1320 return HasPalette() ? GetPaletteSpan()[1] : 0xFFFFFFFF;
1323uint32_t CPDF_DIB::Get1BitResetValue()
const {
1324 if (m_CompData[0].m_ColorKeyMin == 0)
1326 return HasPalette() ? GetPaletteSpan()[0] : 0xFF000000;
fxcrt::ByteString ByteString
void SetHeight(int height)
void SetFormat(FXDIB_Format format)
void SetPaletteArgb(int index, uint32_t color)
void SetPitch(uint32_t pitch)
std::vector< RetainPtr< CPDF_Object > >::const_iterator const_iterator
static uint32_t ComponentsForFamily(Family family)
uint32_t ComponentCount() const
static RetainPtr< CPDF_ColorSpace > GetStockCS(Family family)
RetainPtr< CPDF_DIB > DetachMask()
size_t GetEstimatedImageMemoryBurden() const override
bool SkipToScanline(int line, PauseIndicatorIface *pPause) const override
LoadState ContinueLoadDIBBase(PauseIndicatorIface *pPause)
pdfium::span< const uint8_t > GetScanline(int line) const override
LoadState StartLoadDIBBase(bool bHasMask, const CPDF_Dictionary *pFormResources, const CPDF_Dictionary *pPageResources, bool bStdCS, CPDF_ColorSpace::Family GroupFamily, bool bLoadMask, const CFX_Size &max_size_required)
std::map< ByteString, RetainPtr< CPDF_Object >, std::less<> > DictMap
static bool IsValidJpegBitsPerComponent(int32_t bpc)
static bool IsValidJpegComponent(int32_t comps)
bool operator==(const char *ptr) const
bool operator!=(const char *ptr) const
CFX_STemplate< int32_t > CFX_Size
pdfium::CheckedNumeric< uint32_t > FX_SAFE_UINT32
fxcodec::JpegModule JpegModule
std::optional< uint32_t > CalculatePitch32(int bits_per_pixel, int width_in_pixels)