From 05204905772ca4bdf08d9144695a23d23b2c6601 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Tue, 11 Jul 2023 11:53:02 -0400 Subject: [PATCH] LibGfx/DDS: Read the header in `create()` and remove `initialize()` This is done as a part of #19893. --- .../LibGfx/ImageFormats/DDSLoader.cpp | 22 +++++++------------ .../Libraries/LibGfx/ImageFormats/DDSLoader.h | 1 - 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/DDSLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/DDSLoader.cpp index d6714661d6..60fb9def2f 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/DDSLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/DDSLoader.cpp @@ -31,6 +31,7 @@ struct DDSLoadingContext { enum State { NotDecoded = 0, Error, + HeaderDecoded, BitmapDecoded, }; @@ -465,12 +466,14 @@ static ErrorOr decode_header(DDSLoadingContext& context) return Error::from_string_literal("Format type is not supported at the moment"); } + context.state = DDSLoadingContext::HeaderDecoded; + return {}; } static ErrorOr decode_dds(DDSLoadingContext& context) { - TRY(decode_header(context)); + VERIFY(context.state == DDSLoadingContext::HeaderDecoded); // We support parsing mipmaps, but we only care about the largest one :^) (At least for now) if (size_t mipmap_level = 0; mipmap_level < max(context.header.mip_map_count, 1u)) { @@ -631,18 +634,7 @@ DDSImageDecoderPlugin::~DDSImageDecoderPlugin() = default; IntSize DDSImageDecoderPlugin::size() { - if (m_context->state == DDSLoadingContext::State::Error) - return {}; - - if (m_context->state == DDSLoadingContext::State::BitmapDecoded) - return { m_context->header.width, m_context->header.height }; - - return {}; -} - -ErrorOr DDSImageDecoderPlugin::initialize() -{ - return {}; + return { m_context->header.width, m_context->header.height }; } bool DDSImageDecoderPlugin::sniff(ReadonlyBytes data) @@ -658,7 +650,9 @@ bool DDSImageDecoderPlugin::sniff(ReadonlyBytes data) ErrorOr> DDSImageDecoderPlugin::create(ReadonlyBytes data) { FixedMemoryStream stream { data }; - return adopt_nonnull_own_or_enomem(new (nothrow) DDSImageDecoderPlugin(move(stream))); + auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) DDSImageDecoderPlugin(move(stream)))); + TRY(decode_header(*plugin->m_context)); + return plugin; } bool DDSImageDecoderPlugin::is_animated() diff --git a/Userland/Libraries/LibGfx/ImageFormats/DDSLoader.h b/Userland/Libraries/LibGfx/ImageFormats/DDSLoader.h index b4e26fb8de..04f6d7578d 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/DDSLoader.h +++ b/Userland/Libraries/LibGfx/ImageFormats/DDSLoader.h @@ -243,7 +243,6 @@ public: virtual IntSize size() override; - virtual ErrorOr initialize() override; virtual bool is_animated() override; virtual size_t loop_count() override; virtual size_t frame_count() override;