blob: 93c1cbeb328f09b4e5c52156aa06de0ee5229eda [file]
// Copyright 2026 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_JPEG_RUST_JPEG_SCANLINE_DECODER_H_
#define CORE_FXCODEC_JPEG_RUST_JPEG_SCANLINE_DECODER_H_
#include <stdint.h>
#include <memory>
#include <optional>
#include "core/fxcodec/jpeg/jpegmodule.h"
#include "core/fxcodec/scanlinedecoder.h"
#include "core/fxcrt/data_vector.h"
#include "core/fxcrt/raw_span.h"
#include "core/fxcrt/span.h"
namespace fxcodec {
class RustJpegScanlineDecoder final : public ScanlineDecoder {
public:
static std::unique_ptr<ScanlineDecoder> Create(
pdfium::span<const uint8_t> src_span,
uint32_t width,
uint32_t height,
int num_components,
bool color_transform,
uint32_t scale_denom);
static std::optional<JpegModule::ImageInfo> LoadInfo(
pdfium::span<const uint8_t> src_span);
RustJpegScanlineDecoder();
~RustJpegScanlineDecoder() override;
// ScanlineDecoder:
[[nodiscard]] bool Rewind() override;
pdfium::span<uint8_t> GetNextLine() override;
uint32_t GetSrcOffset() override;
private:
bool CreateImpl(pdfium::span<const uint8_t> src_span,
uint32_t width,
uint32_t height,
int num_components,
bool color_transform,
uint32_t scale_denom);
void CalcPitch();
bool HasKnownBadHeaderWithInvalidHeight(size_t dimension_offset,
uint32_t orig_width) const;
void PatchUpKnownBadHeaderWithInvalidHeight(size_t dimension_offset,
uint32_t orig_height);
bool IsSofSegment(size_t marker_offset) const;
// Patch up the JPEG trailer if missing.
void PatchUpTrailer();
pdfium::span<uint8_t> GetWritableSrcData();
DataVector<uint8_t> corrected_src_data_; // Must outlive `src_span_`.
pdfium::raw_span<const uint8_t> src_span_;
DataVector<uint8_t> decoded_image_;
uint32_t next_row_ = 0;
};
} // namespace fxcodec
#endif // CORE_FXCODEC_JPEG_RUST_JPEG_SCANLINE_DECODER_H_