1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:47:34 +00:00

LibGfx: Improve ImageDecoder construction

Previously, ImageDecoder::create() would return a NonnullRefPtr and
could not "fail", although the returned decoder may be "invalid" which
you then had to check anyway.

The new interface looks like this:

    static RefPtr<Gfx::ImageDecoder> try_create(ReadonlyBytes);

This simplifies ImageDecoder since it no longer has to worry about its
validity. Client code gets slightly clearer as well.
This commit is contained in:
Andreas Kling 2021-07-27 01:12:53 +02:00
parent c6f4ecced9
commit d01b4327fa
6 changed files with 77 additions and 58 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -48,32 +48,25 @@ protected:
class ImageDecoder : public RefCounted<ImageDecoder> {
public:
static NonnullRefPtr<ImageDecoder> create(const u8* data, size_t size) { return adopt_ref(*new ImageDecoder(data, size)); }
static NonnullRefPtr<ImageDecoder> create(const ByteBuffer& data) { return adopt_ref(*new ImageDecoder(data.data(), data.size())); }
static RefPtr<ImageDecoder> try_create(ReadonlyBytes);
~ImageDecoder();
bool is_valid() const { return m_plugin; }
IntSize size() const { return m_plugin ? m_plugin->size() : IntSize(); }
IntSize size() const { return m_plugin->size(); }
int width() const { return size().width(); }
int height() const { return size().height(); }
RefPtr<Gfx::Bitmap> bitmap() const;
void set_volatile()
{
if (m_plugin)
m_plugin->set_volatile();
}
[[nodiscard]] bool set_nonvolatile(bool& was_purged) { return m_plugin ? m_plugin->set_nonvolatile(was_purged) : false; }
bool sniff() const { return m_plugin ? m_plugin->sniff() : false; }
bool is_animated() const { return m_plugin ? m_plugin->is_animated() : false; }
size_t loop_count() const { return m_plugin ? m_plugin->loop_count() : 0; }
size_t frame_count() const { return m_plugin ? m_plugin->frame_count() : 0; }
ImageFrameDescriptor frame(size_t i) const { return m_plugin ? m_plugin->frame(i) : ImageFrameDescriptor(); }
void set_volatile() { m_plugin->set_volatile(); }
[[nodiscard]] bool set_nonvolatile(bool& was_purged) { return m_plugin->set_nonvolatile(was_purged); }
bool sniff() const { return m_plugin->sniff(); }
bool is_animated() const { return m_plugin->is_animated(); }
size_t loop_count() const { return m_plugin->loop_count(); }
size_t frame_count() const { return m_plugin->frame_count(); }
ImageFrameDescriptor frame(size_t i) const { return m_plugin->frame(i); }
private:
ImageDecoder(const u8*, size_t);
explicit ImageDecoder(NonnullOwnPtr<ImageDecoderPlugin>);
mutable OwnPtr<ImageDecoderPlugin> m_plugin;
NonnullOwnPtr<ImageDecoderPlugin> mutable m_plugin;
};
}