Change CJPX_Decoder::GetInfo() to return a struct.
Return the image info in a struct, instead of 3 separate out parameters.
Change-Id: I5e1d86820a5aa6a53e0b1c6eec0415e6bb472689
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/65750
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_dib.cpp b/core/fpdfapi/page/cpdf_dib.cpp
index 87845b7..cbad6b5 100644
--- a/core/fpdfapi/page/cpdf_dib.cpp
+++ b/core/fpdfapi/page/cpdf_dib.cpp
@@ -627,16 +627,15 @@
if (!decoder->StartDecode())
return nullptr;
- uint32_t width = 0;
- uint32_t height = 0;
- uint32_t components = 0;
- decoder->GetInfo(&width, &height, &components);
- if (static_cast<int>(width) < m_Width || static_cast<int>(height) < m_Height)
+ CJPX_Decoder::JpxImageInfo image_info = decoder->GetInfo();
+ if (static_cast<int>(image_info.width) < m_Width ||
+ static_cast<int>(image_info.height) < m_Height) {
return nullptr;
+ }
RetainPtr<CPDF_ColorSpace> original_colorspace = m_pColorSpace;
bool swap_rgb = false;
- switch (GetJpxDecodeAction(components, m_pColorSpace.Get())) {
+ switch (GetJpxDecodeAction(image_info.components, m_pColorSpace.Get())) {
case JpxDecodeAction::kFail:
return nullptr;
@@ -659,28 +658,28 @@
DCHECK_NE(0, m_nComponents);
} else {
DCHECK_EQ(0, m_nComponents);
- m_nComponents = components;
+ m_nComponents = image_info.components;
}
FXDIB_Format format;
- if (components == 1) {
+ if (image_info.components == 1) {
format = FXDIB_8bppRgb;
- } else if (components <= 3) {
+ } else if (image_info.components <= 3) {
format = FXDIB_Rgb;
- } else if (components == 4) {
+ } else if (image_info.components == 4) {
format = FXDIB_Rgb32;
} else {
- width = (width * components + 2) / 3;
+ image_info.width = (image_info.width * image_info.components + 2) / 3;
format = FXDIB_Rgb;
}
auto pCachedBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
- if (!pCachedBitmap->Create(width, height, format))
+ if (!pCachedBitmap->Create(image_info.width, image_info.height, format))
return nullptr;
pCachedBitmap->Clear(0xFFFFFFFF);
// Fill |output_offsets| with 0, 1, ... N.
- std::vector<uint8_t> output_offsets(components);
+ std::vector<uint8_t> output_offsets(image_info.components);
std::iota(output_offsets.begin(), output_offsets.end(), 0);
if (swap_rgb)
std::swap(output_offsets[0], output_offsets[2]);
@@ -692,9 +691,9 @@
if (m_pColorSpace && m_pColorSpace->GetFamily() == PDFCS_INDEXED &&
m_bpc < 8) {
int scale = 8 - m_bpc;
- for (uint32_t row = 0; row < height; ++row) {
+ for (uint32_t row = 0; row < image_info.height; ++row) {
uint8_t* scanline = pCachedBitmap->GetWritableScanline(row);
- for (uint32_t col = 0; col < width; ++col) {
+ for (uint32_t col = 0; col < image_info.width; ++col) {
*scanline = (*scanline) >> scale;
++scanline;
}
diff --git a/core/fxcodec/jpx/cjpx_decoder.cpp b/core/fxcodec/jpx/cjpx_decoder.cpp
index 4653b5e..986d2f1 100644
--- a/core/fxcodec/jpx/cjpx_decoder.cpp
+++ b/core/fxcodec/jpx/cjpx_decoder.cpp
@@ -489,12 +489,8 @@
return true;
}
-void CJPX_Decoder::GetInfo(uint32_t* width,
- uint32_t* height,
- uint32_t* components) {
- *width = m_Image->x1;
- *height = m_Image->y1;
- *components = m_Image->numcomps;
+CJPX_Decoder::JpxImageInfo CJPX_Decoder::GetInfo() const {
+ return {m_Image->x1, m_Image->y1, m_Image->numcomps};
}
bool CJPX_Decoder::Decode(uint8_t* dest_buf,
diff --git a/core/fxcodec/jpx/cjpx_decoder.h b/core/fxcodec/jpx/cjpx_decoder.h
index 1fe5043..0fdccb9 100644
--- a/core/fxcodec/jpx/cjpx_decoder.h
+++ b/core/fxcodec/jpx/cjpx_decoder.h
@@ -30,13 +30,19 @@
kIndexedColorSpace
};
+ struct JpxImageInfo {
+ uint32_t width;
+ uint32_t height;
+ uint32_t components;
+ };
+
static void Sycc420ToRgbForTesting(opj_image_t* img);
explicit CJPX_Decoder(ColorSpaceOption option);
~CJPX_Decoder();
bool Init(pdfium::span<const uint8_t> src_data);
- void GetInfo(uint32_t* width, uint32_t* height, uint32_t* components);
+ JpxImageInfo GetInfo() const;
bool StartDecode();
bool Decode(uint8_t* dest_buf,
uint32_t pitch,
diff --git a/testing/fuzzers/pdf_jpx_fuzzer.cc b/testing/fuzzers/pdf_jpx_fuzzer.cc
index 9c772b0..89705dd 100644
--- a/testing/fuzzers/pdf_jpx_fuzzer.cc
+++ b/testing/fuzzers/pdf_jpx_fuzzer.cc
@@ -17,11 +17,11 @@
const uint32_t kMaxJPXFuzzSize = 100 * 1024 * 1024; // 100 MB
-bool CheckImageSize(uint32_t width, uint32_t height, uint32_t components) {
+bool CheckImageSize(const CJPX_Decoder::JpxImageInfo& image_info) {
static constexpr uint32_t kMemLimitBytes = 1024 * 1024 * 1024; // 1 GB.
- FX_SAFE_UINT32 mem = width;
- mem *= height;
- mem *= components;
+ FX_SAFE_UINT32 mem = image_info.width;
+ mem *= image_info.height;
+ mem *= image_info.components;
return mem.IsValid() && mem.ValueOrDie() <= kMemLimitBytes;
}
@@ -39,34 +39,31 @@
// A call to StartDecode could be too expensive if image size is very big, so
// check size before calling StartDecode().
- uint32_t width;
- uint32_t height;
- uint32_t components;
- decoder->GetInfo(&width, &height, &components);
- if (!CheckImageSize(width, height, components))
+ CJPX_Decoder::JpxImageInfo image_info = decoder->GetInfo();
+ if (!CheckImageSize(image_info))
return 0;
if (!decoder->StartDecode())
return 0;
// StartDecode() could change image size, so check again.
- decoder->GetInfo(&width, &height, &components);
- if (!CheckImageSize(width, height, components))
+ image_info = decoder->GetInfo();
+ if (!CheckImageSize(image_info))
return 0;
FXDIB_Format format;
- if (components == 1) {
+ if (image_info.components == 1) {
format = FXDIB_8bppRgb;
- } else if (components <= 3) {
+ } else if (image_info.components <= 3) {
format = FXDIB_Rgb;
- } else if (components == 4) {
+ } else if (image_info.components == 4) {
format = FXDIB_Rgb32;
} else {
- width = (width * components + 2) / 3;
+ image_info.width = (image_info.width * image_info.components + 2) / 3;
format = FXDIB_Rgb;
}
auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
- if (!bitmap->Create(width, height, format))
+ if (!bitmap->Create(image_info.width, image_info.height, format))
return 0;
if (bitmap->GetHeight() <= 0 ||
@@ -74,8 +71,8 @@
static_cast<uint32_t>(bitmap->GetHeight()))
return 0;
- std::vector<uint8_t> output_offsets(components);
- for (uint32_t i = 0; i < components; ++i)
+ std::vector<uint8_t> output_offsets(image_info.components);
+ for (uint32_t i = 0; i < image_info.components; ++i)
output_offsets[i] = i;
decoder->Decode(bitmap->GetBuffer(), bitmap->GetPitch(), output_offsets);