From b0847596907e20a78819de88fefb010d98989f5c Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sat, 18 Mar 2023 16:56:36 -0400 Subject: [PATCH] LibGfx/JPEG: Make JPEGImageDecoderPlugin's constructor take a Stream This allows us to get rid of the raw pointer and size in the JPEG context struct. --- Userland/Libraries/LibGfx/JPEGLoader.cpp | 23 ++++++++++------------- Userland/Libraries/LibGfx/JPEGLoader.h | 3 ++- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/Userland/Libraries/LibGfx/JPEGLoader.cpp b/Userland/Libraries/LibGfx/JPEGLoader.cpp index 5c93041a36..e76dc760fa 100644 --- a/Userland/Libraries/LibGfx/JPEGLoader.cpp +++ b/Userland/Libraries/LibGfx/JPEGLoader.cpp @@ -232,8 +232,7 @@ struct JPEGLoadingContext { }; State state { State::NotDecoded }; - u8 const* data { nullptr }; - size_t data_size { 0 }; + u32 luma_table[64] = { 0 }; u32 chroma_table[64] = { 0 }; StartOfFrame frame; @@ -623,7 +622,7 @@ static ErrorOr read_start_of_scan(AK::SeekableStream& stream, JPEGLoadingC } u16 bytes_to_read = TRY(stream.read_value>()) - 2; - TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size)); + TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, TRY(stream.size()))); u8 const component_count = TRY(stream.read_value()); Scan current_scan; @@ -690,7 +689,7 @@ static ErrorOr read_restart_interval(AK::SeekableStream& stream, JPEGLoadi static ErrorOr read_huffman_table(AK::SeekableStream& stream, JPEGLoadingContext& context) { i32 bytes_to_read = TRY(stream.read_value>()); - TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size)); + TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, TRY(stream.size()))); bytes_to_read -= 2; while (bytes_to_read > 0) { HuffmanTableSpec table; @@ -844,7 +843,7 @@ static ErrorOr read_colour_encoding(SeekableStream& stream, [[maybe_unused static ErrorOr read_app_marker(SeekableStream& stream, JPEGLoadingContext& context, int app_marker_number) { i32 bytes_to_read = TRY(stream.read_value>()); - TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size)); + TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, TRY(stream.size()))); if (bytes_to_read <= 2) return Error::from_string_literal("app marker size too small"); @@ -913,7 +912,7 @@ static ErrorOr read_start_of_frame(AK::SeekableStream& stream, JPEGLoading i32 bytes_to_read = TRY(stream.read_value>()); bytes_to_read -= 2; - TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size)); + TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, TRY(stream.size()))); context.frame.precision = TRY(stream.read_value()); if (context.frame.precision != 8) { @@ -985,7 +984,7 @@ static ErrorOr read_start_of_frame(AK::SeekableStream& stream, JPEGLoading static ErrorOr read_quantization_table(AK::SeekableStream& stream, JPEGLoadingContext& context) { i32 bytes_to_read = TRY(stream.read_value>()) - 2; - TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size)); + TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, TRY(stream.size()))); while (bytes_to_read > 0) { u8 info_byte = TRY(stream.read_value()); u8 element_unit_hint = info_byte >> 4; @@ -1472,8 +1471,6 @@ static ErrorOr scan_huffman_stream(AK::SeekableStream& stream, HuffmanStre static ErrorOr decode_header(JPEGLoadingContext& context) { if (context.state < JPEGLoadingContext::State::HeaderDecoded) { - context.stream = TRY(try_make(ReadonlyBytes { context.data, context.data_size })); - if (auto result = parse_header(*context.stream, context); result.is_error()) { context.state = JPEGLoadingContext::State::Error; return result.release_error(); @@ -1532,11 +1529,10 @@ static ErrorOr decode_jpeg(JPEGLoadingContext& context) return {}; } -JPEGImageDecoderPlugin::JPEGImageDecoderPlugin(u8 const* data, size_t size) +JPEGImageDecoderPlugin::JPEGImageDecoderPlugin(NonnullOwnPtr stream) { m_context = make(); - m_context->data = data; - m_context->data_size = size; + m_context->stream = move(stream); } JPEGImageDecoderPlugin::~JPEGImageDecoderPlugin() = default; @@ -1579,7 +1575,8 @@ bool JPEGImageDecoderPlugin::sniff(ReadonlyBytes data) ErrorOr> JPEGImageDecoderPlugin::create(ReadonlyBytes data) { - return adopt_nonnull_own_or_enomem(new (nothrow) JPEGImageDecoderPlugin(data.data(), data.size())); + auto stream = TRY(try_make(data)); + return adopt_nonnull_own_or_enomem(new (nothrow) JPEGImageDecoderPlugin(move(stream))); } bool JPEGImageDecoderPlugin::is_animated() diff --git a/Userland/Libraries/LibGfx/JPEGLoader.h b/Userland/Libraries/LibGfx/JPEGLoader.h index fd2c30ce5d..2890d803c3 100644 --- a/Userland/Libraries/LibGfx/JPEGLoader.h +++ b/Userland/Libraries/LibGfx/JPEGLoader.h @@ -6,6 +6,7 @@ #pragma once +#include #include namespace Gfx { @@ -31,7 +32,7 @@ public: virtual ErrorOr> icc_data() override; private: - JPEGImageDecoderPlugin(u8 const*, size_t); + JPEGImageDecoderPlugin(NonnullOwnPtr); OwnPtr m_context; };