[rust png] Hoist `PngDecoder::Delegate` into a top-level class. This CL moves the declaration of the abstract `PngDecoder::Delegate` interface into a separate `png_decoder_delegate.h` header (renaming the interface into `PngDecoderDelegate`). This is desirable for follow-up CLs, where the interface will be used from a future `skia_png_decoder.cc` without having to reach for the current, `libpng`-related `png_decoder.cc`. Bug: 444045690 Change-Id: I546c8da2c8f6d90894cef9af8bd6df54c13d358f Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/135653 Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcodec/png/png_decoder.cpp b/core/fxcodec/png/png_decoder.cpp index 950e28a..60a70a2 100644 --- a/core/fxcodec/png/png_decoder.cpp +++ b/core/fxcodec/png/png_decoder.cpp
@@ -12,6 +12,7 @@ #include "core/fxcodec/cfx_codec_memory.h" #include "core/fxcodec/fx_codec.h" #include "core/fxcodec/fx_codec_def.h" +#include "core/fxcodec/png/png_decoder_delegate.h" #include "core/fxcrt/compiler_specific.h" #include "core/fxcrt/notreached.h" #include "core/fxcrt/unowned_ptr.h" @@ -24,17 +25,18 @@ #define PNG_ERROR_SIZE 256 -using DecodedColorType = fxcodec::PngDecoder::Delegate::DecodedColorType; -using EncodedColorType = fxcodec::PngDecoder::Delegate::EncodedColorType; +using PngDecoderDelegate = fxcodec::PngDecoderDelegate; +using DecodedColorType = PngDecoderDelegate::DecodedColorType; +using EncodedColorType = PngDecoderDelegate::EncodedColorType; class CPngContext final : public ProgressiveDecoderIface::Context { public: - explicit CPngContext(PngDecoder::Delegate* pDelegate); + explicit CPngContext(PngDecoderDelegate* pDelegate); ~CPngContext() override; png_structp png_ = nullptr; png_infop info_ = nullptr; - UnownedPtr<PngDecoder::Delegate> const delegate_; + UnownedPtr<PngDecoderDelegate> const delegate_; char last_error_[PNG_ERROR_SIZE] = {}; }; @@ -203,7 +205,7 @@ } // extern "C" -CPngContext::CPngContext(PngDecoder::Delegate* pDelegate) +CPngContext::CPngContext(PngDecoderDelegate* pDelegate) : delegate_(pDelegate) {} CPngContext::~CPngContext() { @@ -215,7 +217,7 @@ // static std::unique_ptr<ProgressiveDecoderIface::Context> PngDecoder::StartDecode( - Delegate* pDelegate) { + PngDecoderDelegate* pDelegate) { auto p = std::make_unique<CPngContext>(pDelegate); p->png_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
diff --git a/core/fxcodec/png/png_decoder.h b/core/fxcodec/png/png_decoder.h index d1f1cb6..635c05a 100644 --- a/core/fxcodec/png/png_decoder.h +++ b/core/fxcodec/png/png_decoder.h
@@ -19,66 +19,12 @@ namespace fxcodec { class CFX_DIBAttribute; +class PngDecoderDelegate; class PngDecoder { public: - class Delegate { - 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, - kBgra, - }; - - // Called by `PngDecoder` after decoding the image header: - // - // * `width` and `height` specify image dimensions in pixels - // * `bpc` 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 - // - // Implementation should: - // - // * Return `true` upon success (and `false` otherwise) - // * Set `*dst_color_type` to the color type to decode into - // * Set `*gamma` to the target gamma to decode with - // * TODO(crbug.com/355630556): Add out parameter for desired alpha-premul. - virtual bool PngReadHeader(int width, - int height, - int bpc, - int pass, - EncodedColorType src_color_type, - DecodedColorType* dst_color_type, - double* gamma) = 0; - - // Called by `PngDecoder` to ask where to write decoded pixels. - // Implementation should return a pointer to the buffer where the - // decoded pixels should be written to. - // - // `PngDecoder` guarantees that `0 <= line && line < height` - // (`height` that was earlier passed to `PngReadHeader`). - virtual uint8_t* PngAskScanlineBuf(int line) = 0; - - // Called by `PngDecoder` Communicates that `line`th row has been decoded - // enough to be displayed. - virtual void PngFillScanlineBufCompleted(int line) = 0; - }; - static std::unique_ptr<ProgressiveDecoderIface::Context> StartDecode( - Delegate* pDelegate); + PngDecoderDelegate* pDelegate); static bool ContinueDecode(ProgressiveDecoderIface::Context* pContext, RetainPtr<CFX_CodecMemory> codec_memory,
diff --git a/core/fxcodec/png/png_decoder_delegate.h b/core/fxcodec/png/png_decoder_delegate.h new file mode 100644 index 0000000..a28e18c --- /dev/null +++ b/core/fxcodec/png/png_decoder_delegate.h
@@ -0,0 +1,75 @@ +// 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. + +#ifndef CORE_FXCODEC_PNG_PNG_DECODER_DELEGATE_H_ +#define CORE_FXCODEC_PNG_PNG_DECODER_DELEGATE_H_ + +#include <stdint.h> + +namespace fxcodec { + +// Abstract interface used by the `libpng`-based decoder from `png_decoder.h` +// (in the future by Skia-based decoder from `skia_png_decoder.h`). +// +// TODO(https://crbug.com/444045690): Update the comment above once the +// Skia-based decoder is implemented and/or the `libpng`-based decoder moved +// 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, + kBgra, + }; + + // Called by `PngDecoder` after decoding the image header: + // + // * `width` and `height` specify image dimensions in pixels + // * `bpc` 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 + // + // Implementation should: + // + // * Return `true` upon success (and `false` otherwise) + // * Set `*dst_color_type` to the color type to decode into + // * Set `*gamma` to the target gamma to decode with + // * TODO(crbug.com/355630556): Add out parameter for desired alpha-premul. + virtual bool PngReadHeader(int width, + int height, + int bpc, + int pass, + EncodedColorType src_color_type, + DecodedColorType* dst_color_type, + double* gamma) = 0; + + // Called by `PngDecoder` to ask where to write decoded pixels. + // Implementation should return a pointer to the buffer where the + // decoded pixels should be written to. + // + // `PngDecoder` guarantees that `0 <= line && line < height` + // (`height` that was earlier passed to `PngReadHeader`). + virtual uint8_t* PngAskScanlineBuf(int line) = 0; + + // Called by `PngDecoder` Communicates that `line`th row has been decoded + // enough to be displayed. + virtual void PngFillScanlineBufCompleted(int line) = 0; +}; + +} // namespace fxcodec + +#endif // CORE_FXCODEC_PNG_PNG_DECODER_DELEGATE_H_
diff --git a/core/fxcodec/progressive_decoder.cpp b/core/fxcodec/progressive_decoder.cpp index 4fa0e8e..d38aa72 100644 --- a/core/fxcodec/progressive_decoder.cpp +++ b/core/fxcodec/progressive_decoder.cpp
@@ -48,8 +48,8 @@ constexpr size_t kBlockSize = 4096; #ifdef PDF_ENABLE_XFA_PNG -using PngDecodedColorType = fxcodec::PngDecoder::Delegate::DecodedColorType; -using PngEncodedColorType = fxcodec::PngDecoder::Delegate::EncodedColorType; +using PngDecodedColorType = fxcodec::PngDecoderDelegate::DecodedColorType; +using PngEncodedColorType = fxcodec::PngDecoderDelegate::EncodedColorType; #if BUILDFLAG(IS_APPLE) const double kPngGamma = 1.7; #else
diff --git a/core/fxcodec/progressive_decoder.h b/core/fxcodec/progressive_decoder.h index 3589dfb..c96138c 100644 --- a/core/fxcodec/progressive_decoder.h +++ b/core/fxcodec/progressive_decoder.h
@@ -33,6 +33,7 @@ #ifdef PDF_ENABLE_XFA_PNG #include "core/fxcodec/png/png_decoder.h" +#include "core/fxcodec/png/png_decoder_delegate.h" #endif // PDF_ENABLE_XFA_PNG class CFX_DIBitmap; @@ -52,7 +53,7 @@ public GifDecoder::Delegate, #endif // PDF_ENABLE_XFA_GIF #ifdef PDF_ENABLE_XFA_PNG - public PngDecoder::Delegate, + public PngDecoderDelegate, #endif // PDF_ENABLE_XFA_PNG public Dummy { public: @@ -85,13 +86,13 @@ FXCODEC_STATUS ContinueDecode(); #ifdef PDF_ENABLE_XFA_PNG - // PngDecoder::Delegate + // PngDecoderDelegate bool PngReadHeader(int width, int height, int bpc, int pass, - PngDecoder::Delegate::EncodedColorType src_color_type, - PngDecoder::Delegate::DecodedColorType* dst_color_type, + PngDecoderDelegate::EncodedColorType src_color_type, + PngDecoderDelegate::DecodedColorType* dst_color_type, double* gamma) override; uint8_t* PngAskScanlineBuf(int line) override; void PngFillScanlineBufCompleted(int line) override;