[rust png] Replace `src_color_type` with `components_count`.
Before this CL, `ProgressiveDecoder::PngReadHeader` would only use
`src_color_type` to calculate `src_components_count_`:
https://source.chromium.org/chromium/chromium/src/+/main:third_party/pdfium/core/fxcodec/progressive_decoder.cpp;l=99;drc=1a1c55244b379198ca0eaa9498c1fd527850abb2
After this CL, the signature of `PngReadHeader` is simplified, so that
the caller can directly pass the number of components, without the
indirection of having to convert to/from `EncodedColorType` enum.
Bug: 444045690
Change-Id: Ida372cb2448cbf57b3f59a0dc6cc76fed54e2103
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/136890
Auto-Submit: Ćukasz Anforowicz <lukasza@google.com>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcodec/BUILD.gn b/core/fxcodec/BUILD.gn
index c7ef717..a484ce6 100644
--- a/core/fxcodec/BUILD.gn
+++ b/core/fxcodec/BUILD.gn
@@ -122,7 +122,6 @@
sources += [
"png/libpng_png_decoder.cpp",
"png/libpng_png_decoder.h",
- "png/png_decoder_delegate.cpp",
"png/png_decoder_delegate.h",
]
deps += [ "../../third_party:png" ]
diff --git a/core/fxcodec/png/libpng_png_decoder.cpp b/core/fxcodec/png/libpng_png_decoder.cpp
index 999f7f0..4f05abc 100644
--- a/core/fxcodec/png/libpng_png_decoder.cpp
+++ b/core/fxcodec/png/libpng_png_decoder.cpp
@@ -27,7 +27,6 @@
using PngDecoderDelegate = fxcodec::PngDecoderDelegate;
using DecodedColorType = PngDecoderDelegate::DecodedColorType;
-using EncodedColorType = PngDecoderDelegate::EncodedColorType;
class CPngContext final : public ProgressiveDecoderIface::Context {
public:
@@ -79,24 +78,30 @@
int pass = png_set_interlace_handling(png_ptr);
- static_assert(static_cast<int>(EncodedColorType::kGrayscale) ==
- PNG_COLOR_TYPE_GRAY);
- static_assert(static_cast<int>(EncodedColorType::kGrayscaleWithAlpha) ==
- PNG_COLOR_TYPE_GRAY_ALPHA);
- static_assert(static_cast<int>(EncodedColorType::kIndexedColor) ==
- PNG_COLOR_TYPE_PALETTE);
- static_assert(static_cast<int>(EncodedColorType::kTruecolor) ==
- PNG_COLOR_TYPE_RGB);
- static_assert(static_cast<int>(EncodedColorType::kTruecolorWithAlpha) ==
- PNG_COLOR_TYPE_RGB_ALPHA);
- static_assert(sizeof(EncodedColorType) == sizeof(int));
- auto src_color_type = static_cast<EncodedColorType>(libpng_color_type);
+ int components_count;
+ switch (libpng_color_type) {
+ case PNG_COLOR_TYPE_GRAY:
+ components_count = 1;
+ break;
+ case PNG_COLOR_TYPE_GRAY_ALPHA:
+ components_count = 2;
+ break;
+ case PNG_COLOR_TYPE_RGB:
+ components_count = 3;
+ break;
+ case PNG_COLOR_TYPE_RGB_ALPHA:
+ case PNG_COLOR_TYPE_PALETTE:
+ components_count = 4;
+ break;
+ default:
+ NOTREACHED();
+ }
DecodedColorType dst_color_type;
double gamma = 1.0;
if (!pContext->delegate_->PngReadHeader(width, height, bits_per_component,
- pass, src_color_type, &dst_color_type,
- &gamma)) {
+ components_count, pass,
+ &dst_color_type, &gamma)) {
// Note that `png_error` function is marked as `PNG_NORETURN`.
png_error(pContext->png_, "Read Header Callback Error");
}
diff --git a/core/fxcodec/png/png_decoder_delegate.cpp b/core/fxcodec/png/png_decoder_delegate.cpp
deleted file mode 100644
index f6a210a..0000000
--- a/core/fxcodec/png/png_decoder_delegate.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-// Copyright 2025 The PDFium Authors
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "core/fxcodec/png/png_decoder_delegate.h"
-
-#include "core/fxcrt/notreached.h"
-
-namespace fxcodec {
-
-// static
-int PngDecoderDelegate::GetNumberOfComponents(EncodedColorType color_type) {
- switch (color_type) {
- case EncodedColorType::kGrayscale:
- return 1;
- case EncodedColorType::kGrayscaleWithAlpha:
- return 2;
- case EncodedColorType::kTruecolor:
- return 3;
- case EncodedColorType::kIndexedColor:
- case EncodedColorType::kTruecolorWithAlpha:
- return 4;
- }
- NOTREACHED();
-}
-
-} // namespace fxcodec
diff --git a/core/fxcodec/png/png_decoder_delegate.h b/core/fxcodec/png/png_decoder_delegate.h
index 528fed2..43529a1 100644
--- a/core/fxcodec/png/png_decoder_delegate.h
+++ b/core/fxcodec/png/png_decoder_delegate.h
@@ -17,20 +17,6 @@
// to renamed .h/.cc files.
class PngDecoderDelegate {
public:
- // Color type specified in the PNG IHDR chunk. See also
- // https://www.w3.org/TR/png-3/#6Colour-values and
- // https://www.w3.org/TR/png-3/#11IHDR
- //
- // The enumerator values below have been picked to match 1) the PNG spec and
- // 2) `PNG_COLOR_TYPE_...` constants from `libpng/png.h`.
- enum class EncodedColorType : int {
- kGrayscale = 0,
- kTruecolor = 2,
- kIndexedColor = 3,
- kGrayscaleWithAlpha = 4,
- kTruecolorWithAlpha = 6,
- };
-
// Color format to decode into.
enum class DecodedColorType {
kBgr,
@@ -42,7 +28,9 @@
// * `width` and `height` specify image dimensions in pixels
// * `bits_per_component` is number of bits per component (e.g. per red,
// or per alpha)
- // * `src_color_type` is the color type the image has been encoded with
+ // * `components_count` describes the number of channels that
+ // the encoded pixels are composed of. For example it would be 3
+ // for a PNG containing RGB pixels.
//
// Implementation should:
//
@@ -53,8 +41,8 @@
virtual bool PngReadHeader(int width,
int height,
int bits_per_component,
+ int components_count,
int pass,
- EncodedColorType src_color_type,
DecodedColorType* dst_color_type,
double* gamma) = 0;
@@ -69,11 +57,6 @@
// Called by `PngDecoder` Communicates that `line`th row has been decoded
// enough to be displayed.
virtual void PngFillScanlineBufCompleted(int line) = 0;
-
- // Helper to get the number of components in the given `color_type`. For
- // example, when called with `EncodedColorType::kTruecolor` (RGB) the helper
- // will return `3`.
- static int GetNumberOfComponents(EncodedColorType color_type);
};
} // namespace fxcodec
diff --git a/core/fxcodec/progressive_decoder.cpp b/core/fxcodec/progressive_decoder.cpp
index 2617d5d..7448d67 100644
--- a/core/fxcodec/progressive_decoder.cpp
+++ b/core/fxcodec/progressive_decoder.cpp
@@ -54,7 +54,6 @@
#ifdef PDF_ENABLE_XFA_PNG
using PngDecodedColorType = fxcodec::PngDecoderDelegate::DecodedColorType;
-using PngEncodedColorType = fxcodec::PngDecoderDelegate::EncodedColorType;
#if BUILDFLAG(IS_APPLE)
const double kPngGamma = 1.7;
#else
@@ -87,8 +86,8 @@
bool ProgressiveDecoder::PngReadHeader(int width,
int height,
int bits_per_component,
+ int components_count,
int pass,
- PngEncodedColorType src_color_type,
PngDecodedColorType* dst_color_type,
double* gamma) {
if (!device_bitmap_) {
@@ -96,8 +95,7 @@
src_height_ = height;
src_bits_per_component_ = bits_per_component;
src_pass_number_ = pass;
- src_components_count_ =
- PngDecoderDelegate::GetNumberOfComponents(src_color_type);
+ src_components_count_ = components_count;
return false;
}
switch (device_bitmap_->GetFormat()) {
diff --git a/core/fxcodec/progressive_decoder.h b/core/fxcodec/progressive_decoder.h
index a2aca33..f1776c3 100644
--- a/core/fxcodec/progressive_decoder.h
+++ b/core/fxcodec/progressive_decoder.h
@@ -89,8 +89,8 @@
bool PngReadHeader(int width,
int height,
int bits_per_component,
+ int components_count,
int pass,
- PngDecoderDelegate::EncodedColorType src_color_type,
PngDecoderDelegate::DecodedColorType* dst_color_type,
double* gamma) override;
uint8_t* PngAskScanlineBuf(int line) override;