Browse Source

LibGfx/ICO: Decode the header in `create()` and remove `initialize()`

This is done as a part of #19893.
Lucas CHOLLET 2 năm trước cách đây
mục cha
commit
38dd4168be

+ 1 - 8
Tests/LibGfx/TestImageDecoder.cpp

@@ -77,14 +77,7 @@ TEST_CASE(test_not_ico)
 {
     auto file = MUST(Core::MappedFile::map(TEST_INPUT("buggie.png"sv)));
     EXPECT(!Gfx::ICOImageDecoderPlugin::sniff(file->bytes()));
-    auto plugin_decoder = MUST(Gfx::ICOImageDecoderPlugin::create(file->bytes()));
-    EXPECT(plugin_decoder->initialize().is_error());
-
-    EXPECT(plugin_decoder->frame_count());
-    EXPECT(!plugin_decoder->is_animated());
-    EXPECT(!plugin_decoder->loop_count());
-
-    EXPECT(plugin_decoder->frame(0).is_error());
+    EXPECT(Gfx::ICOImageDecoderPlugin::create(file->bytes()).is_error());
 }
 
 TEST_CASE(test_bmp_embedded_in_ico)

+ 4 - 22
Userland/Libraries/LibGfx/ImageFormats/ICOLoader.cpp

@@ -138,8 +138,7 @@ static ErrorOr<void> load_ico_directory(ICOLoadingContext& context)
 
 ErrorOr<void> ICOImageDecoderPlugin::load_ico_bitmap(ICOLoadingContext& context, Optional<size_t> index)
 {
-    if (context.state < ICOLoadingContext::State::DirectoryDecoded)
-        TRY(load_ico_directory(context));
+    VERIFY(context.state >= ICOLoadingContext::State::DirectoryDecoded);
 
     size_t real_index = context.largest_index;
     if (index.has_value())
@@ -186,7 +185,9 @@ bool ICOImageDecoderPlugin::sniff(ReadonlyBytes data)
 
 ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> ICOImageDecoderPlugin::create(ReadonlyBytes data)
 {
-    return adopt_nonnull_own_or_enomem(new (nothrow) ICOImageDecoderPlugin(data.data(), data.size()));
+    auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) ICOImageDecoderPlugin(data.data(), data.size())));
+    TRY(load_ico_directory(*plugin->m_context));
+    return plugin;
 }
 
 ICOImageDecoderPlugin::ICOImageDecoderPlugin(u8 const* data, size_t size)
@@ -200,28 +201,9 @@ ICOImageDecoderPlugin::~ICOImageDecoderPlugin() = default;
 
 IntSize ICOImageDecoderPlugin::size()
 {
-    if (m_context->state == ICOLoadingContext::State::Error) {
-        return {};
-    }
-
-    if (m_context->state < ICOLoadingContext::State::DirectoryDecoded) {
-        if (load_ico_directory(*m_context).is_error()) {
-            m_context->state = ICOLoadingContext::State::Error;
-            return {};
-        }
-        m_context->state = ICOLoadingContext::State::DirectoryDecoded;
-    }
-
     return { m_context->images[m_context->largest_index].width, m_context->images[m_context->largest_index].height };
 }
 
-ErrorOr<void> ICOImageDecoderPlugin::initialize()
-{
-    FixedMemoryStream stream { { m_context->data, m_context->data_size } };
-    TRY(decode_ico_header(stream));
-    return {};
-}
-
 bool ICOImageDecoderPlugin::is_animated()
 {
     return false;

+ 0 - 1
Userland/Libraries/LibGfx/ImageFormats/ICOLoader.h

@@ -21,7 +21,6 @@ public:
 
     virtual IntSize size() override;
 
-    virtual ErrorOr<void> initialize() override;
     virtual bool is_animated() override;
     virtual size_t loop_count() override;
     virtual size_t frame_count() override;