From d7f04c9aa1eacd27d349adaf97ee7d7b05b45509 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Mon, 29 Jan 2024 09:52:53 -0500 Subject: [PATCH] LibGfx/JPEGLoader: Make byte_offset() return offset from start of stream JPEGStream::byte_offset() now returns an offset relative to the start of the stream, instead of relative to the buffered part. No behavior change except if JPEG_DEBUG is set. --- .../Libraries/LibGfx/ImageFormats/JPEGLoader.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp index c5c08dd465c..af7e3f6c9b2 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp @@ -182,6 +182,7 @@ public: JPEGStream jpeg_stream { move(stream), move(buffer) }; TRY(jpeg_stream.refill_buffer()); + jpeg_stream.m_offset_from_start = 0; return jpeg_stream; } @@ -205,8 +206,10 @@ public: auto const discarded_from_buffer = min(m_current_size - m_byte_offset, bytes); m_byte_offset += discarded_from_buffer; - if (discarded_from_buffer < bytes) + if (discarded_from_buffer < bytes) { + m_offset_from_start += bytes - discarded_from_buffer; TRY(m_stream->discard(bytes - discarded_from_buffer)); + } return {}; } @@ -216,8 +219,10 @@ public: auto const copied = m_buffer.span().slice(m_byte_offset).copy_trimmed_to(bytes); m_byte_offset += copied; - if (copied < bytes.size()) + if (copied < bytes.size()) { + m_offset_from_start += bytes.size() - copied; TRY(m_stream->read_until_filled(bytes.slice(copied))); + } return {}; } @@ -229,7 +234,7 @@ public: u64 byte_offset() const { - return m_byte_offset; + return m_offset_from_start + m_byte_offset; } private: @@ -243,6 +248,8 @@ private: { VERIFY(m_byte_offset == m_current_size); + m_offset_from_start += m_byte_offset; + m_current_size = TRY(m_stream->read_some(m_buffer.span())).size(); if (m_current_size == 0) return Error::from_string_literal("Unexpected end of file"); @@ -259,6 +266,7 @@ private: Optional m_saved_marker {}; Vector m_buffer {}; + u64 m_offset_from_start { 0 }; u64 m_byte_offset { buffer_size }; u64 m_current_size { buffer_size }; };