LibGfx: Remove ImageDecoderPlugin::bitmap() in favor of frame(index)

To encourage proper support for multi-frame images throughout the
system, get rid of the single-frame convenience bitmap() API.
This commit is contained in:
Andreas Kling 2021-11-18 13:47:29 +01:00
parent 750f1d580a
commit 2b866e3c9b
Notes: sideshowbarker 2024-07-18 01:00:41 +09:00
22 changed files with 105 additions and 155 deletions

View file

@ -14,7 +14,7 @@
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
Gfx::GIFImageDecoderPlugin gif_decoder(data, size);
auto bitmap = gif_decoder.bitmap();
auto bitmap = gif_decoder.frame(0).image;
if (bitmap) {
// Looks like a valid GIF. Try to load the other frames:
dbgln_if(GIF_DEBUG, "bitmap size: {}", bitmap->size());

View file

@ -188,7 +188,7 @@ Icon FileIconProvider::icon_for_executable(const String& path)
if (!section.has_value()) {
bitmap = s_executable_icon.bitmap_for_size(icon_section.image_size);
} else {
bitmap = Gfx::PNGImageDecoderPlugin(reinterpret_cast<u8 const*>(section->raw_data()), section->size()).bitmap();
bitmap = Gfx::PNGImageDecoderPlugin(reinterpret_cast<u8 const*>(section->raw_data()), section->size()).frame(0).image;
}
if (!bitmap) {

View file

@ -1321,18 +1321,6 @@ IntSize BMPImageDecoderPlugin::size()
return { m_context->dib.core.width, abs(m_context->dib.core.height) };
}
RefPtr<Gfx::Bitmap> BMPImageDecoderPlugin::bitmap()
{
if (m_context->state == BMPLoadingContext::State::Error)
return nullptr;
if (m_context->state < BMPLoadingContext::State::PixelDataDecoded && !decode_bmp_pixel_data(*m_context))
return nullptr;
VERIFY(m_context->bitmap);
return m_context->bitmap;
}
void BMPImageDecoderPlugin::set_volatile()
{
if (m_context->bitmap)
@ -1370,7 +1358,15 @@ ImageFrameDescriptor BMPImageDecoderPlugin::frame(size_t i)
{
if (i > 0)
return {};
return { bitmap(), 0 };
if (m_context->state == BMPLoadingContext::State::Error)
return {};
if (m_context->state < BMPLoadingContext::State::PixelDataDecoded && !decode_bmp_pixel_data(*m_context))
return {};
VERIFY(m_context->bitmap);
return { m_context->bitmap, 0 };
}
}

View file

@ -18,7 +18,6 @@ public:
BMPImageDecoderPlugin(const u8*, size_t);
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) override;
virtual bool sniff() override;

View file

@ -959,21 +959,6 @@ IntSize DDSImageDecoderPlugin::size()
return {};
}
RefPtr<Gfx::Bitmap> DDSImageDecoderPlugin::bitmap()
{
if (m_context->state == DDSLoadingContext::State::Error)
return nullptr;
if (m_context->state < DDSLoadingContext::State::BitmapDecoded) {
bool success = decode_dds(*m_context);
if (!success)
return nullptr;
}
VERIFY(m_context->bitmap);
return m_context->bitmap;
}
void DDSImageDecoderPlugin::set_volatile()
{
if (m_context->bitmap)
@ -1016,7 +1001,18 @@ ImageFrameDescriptor DDSImageDecoderPlugin::frame(size_t i)
{
if (i > 0)
return {};
return { bitmap(), 0 };
if (m_context->state == DDSLoadingContext::State::Error)
return {};
if (m_context->state < DDSLoadingContext::State::BitmapDecoded) {
bool success = decode_dds(*m_context);
if (!success)
return {};
}
VERIFY(m_context->bitmap);
return { m_context->bitmap, 0 };
}
}

View file

@ -239,7 +239,6 @@ public:
DDSImageDecoderPlugin(const u8*, size_t);
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) override;
virtual bool sniff() override;

View file

@ -623,14 +623,6 @@ IntSize GIFImageDecoderPlugin::size()
return { m_context->logical_screen.width, m_context->logical_screen.height };
}
RefPtr<Gfx::Bitmap> GIFImageDecoderPlugin::bitmap()
{
if (m_context->state < GIFLoadingContext::State::FrameComplete) {
return frame(0).image;
}
return m_context->frame_buffer;
}
void GIFImageDecoderPlugin::set_volatile()
{
if (m_context->frame_buffer) {

View file

@ -19,7 +19,6 @@ public:
GIFImageDecoderPlugin(const u8*, size_t);
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) override;
virtual bool sniff() override;

View file

@ -263,7 +263,7 @@ static bool load_ico_bitmap(ICOLoadingContext& context, Optional<size_t> index)
PNGImageDecoderPlugin png_decoder(context.data + desc.offset, desc.size);
if (png_decoder.sniff()) {
desc.bitmap = png_decoder.bitmap();
desc.bitmap = png_decoder.frame(0).image;
if (!desc.bitmap) {
dbgln_if(ICO_DEBUG, "load_ico_bitmap: failed to load PNG encoded image index: {}", real_index);
return false;
@ -304,25 +304,6 @@ IntSize ICOImageDecoderPlugin::size()
return { m_context->images[m_context->largest_index].width, m_context->images[m_context->largest_index].height };
}
RefPtr<Gfx::Bitmap> ICOImageDecoderPlugin::bitmap()
{
if (m_context->state == ICOLoadingContext::State::Error)
return nullptr;
if (m_context->state < ICOLoadingContext::State::BitmapDecoded) {
// NOTE: This forces the chunk decoding to happen.
bool success = load_ico_bitmap(*m_context, {});
if (!success) {
m_context->state = ICOLoadingContext::State::Error;
return nullptr;
}
m_context->state = ICOLoadingContext::State::BitmapDecoded;
}
VERIFY(m_context->images[m_context->largest_index].bitmap);
return m_context->images[m_context->largest_index].bitmap;
}
void ICOImageDecoderPlugin::set_volatile()
{
if (m_context->images[0].bitmap)
@ -361,7 +342,22 @@ ImageFrameDescriptor ICOImageDecoderPlugin::frame(size_t i)
{
if (i > 0)
return {};
return { bitmap(), 0 };
if (m_context->state == ICOLoadingContext::State::Error)
return {};
if (m_context->state < ICOLoadingContext::State::BitmapDecoded) {
// NOTE: This forces the chunk decoding to happen.
bool success = load_ico_bitmap(*m_context, {});
if (!success) {
m_context->state = ICOLoadingContext::State::Error;
return {};
}
m_context->state = ICOLoadingContext::State::BitmapDecoded;
}
VERIFY(m_context->images[m_context->largest_index].bitmap);
return { m_context->images[m_context->largest_index].bitmap, 0 };
}
}

View file

@ -18,7 +18,6 @@ public:
ICOImageDecoderPlugin(const u8*, size_t);
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) override;
virtual bool sniff() override;

View file

@ -42,8 +42,6 @@ public:
virtual ImageFrameDescriptor frame(size_t i) = 0;
protected:
virtual RefPtr<Gfx::Bitmap> bitmap() = 0;
ImageDecoderPlugin() { }
};

View file

@ -1240,21 +1240,6 @@ IntSize JPGImageDecoderPlugin::size()
return {};
}
RefPtr<Gfx::Bitmap> JPGImageDecoderPlugin::bitmap()
{
if (m_context->state == JPGLoadingContext::State::Error)
return nullptr;
if (m_context->state < JPGLoadingContext::State::BitmapDecoded) {
if (!decode_jpg(*m_context)) {
m_context->state = JPGLoadingContext::State::Error;
return nullptr;
}
m_context->state = JPGLoadingContext::State::BitmapDecoded;
}
return m_context->bitmap;
}
void JPGImageDecoderPlugin::set_volatile()
{
if (m_context->bitmap)
@ -1295,7 +1280,19 @@ ImageFrameDescriptor JPGImageDecoderPlugin::frame(size_t i)
{
if (i > 0)
return {};
return { bitmap(), 0 };
if (m_context->state == JPGLoadingContext::State::Error)
return {};
if (m_context->state < JPGLoadingContext::State::BitmapDecoded) {
if (!decode_jpg(*m_context)) {
m_context->state = JPGLoadingContext::State::Error;
return {};
}
m_context->state = JPGLoadingContext::State::BitmapDecoded;
}
return { m_context->bitmap, 0 };
}
}

View file

@ -17,7 +17,6 @@ public:
virtual ~JPGImageDecoderPlugin() override;
JPGImageDecoderPlugin(const u8*, size_t);
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) override;
virtual bool sniff() override;

View file

@ -119,21 +119,6 @@ IntSize PBMImageDecoderPlugin::size()
return { m_context->width, m_context->height };
}
RefPtr<Gfx::Bitmap> PBMImageDecoderPlugin::bitmap()
{
if (m_context->state == PBMLoadingContext::State::Error)
return nullptr;
if (m_context->state < PBMLoadingContext::State::Decoded) {
bool success = decode(*m_context);
if (!success)
return nullptr;
}
VERIFY(m_context->bitmap);
return m_context->bitmap;
}
void PBMImageDecoderPlugin::set_volatile()
{
if (m_context->bitmap)
@ -181,7 +166,18 @@ ImageFrameDescriptor PBMImageDecoderPlugin::frame(size_t i)
{
if (i > 0)
return {};
return { bitmap(), 0 };
if (m_context->state == PBMLoadingContext::State::Error)
return {};
if (m_context->state < PBMLoadingContext::State::Decoded) {
bool success = decode(*m_context);
if (!success)
return {};
}
VERIFY(m_context->bitmap);
return { m_context->bitmap, 0 };
}
}

View file

@ -18,7 +18,6 @@ public:
virtual ~PBMImageDecoderPlugin() override;
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) override;

View file

@ -122,21 +122,6 @@ IntSize PGMImageDecoderPlugin::size()
return { m_context->width, m_context->height };
}
RefPtr<Gfx::Bitmap> PGMImageDecoderPlugin::bitmap()
{
if (m_context->state == PGMLoadingContext::State::Error)
return nullptr;
if (m_context->state < PGMLoadingContext::State::Decoded) {
bool success = decode(*m_context);
if (!success)
return nullptr;
}
VERIFY(m_context->bitmap);
return m_context->bitmap;
}
void PGMImageDecoderPlugin::set_volatile()
{
if (m_context->bitmap)
@ -184,7 +169,18 @@ ImageFrameDescriptor PGMImageDecoderPlugin::frame(size_t i)
{
if (i > 0)
return {};
return { bitmap(), 0 };
if (m_context->state == PGMLoadingContext::State::Error)
return {};
if (m_context->state < PGMLoadingContext::State::Decoded) {
bool success = decode(*m_context);
if (!success)
return {};
}
VERIFY(m_context->bitmap);
return { m_context->bitmap, 0 };
}
}

View file

@ -18,7 +18,6 @@ public:
virtual ~PGMImageDecoderPlugin() override;
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) override;

View file

@ -931,22 +931,6 @@ IntSize PNGImageDecoderPlugin::size()
return { m_context->width, m_context->height };
}
RefPtr<Gfx::Bitmap> PNGImageDecoderPlugin::bitmap()
{
if (m_context->state == PNGLoadingContext::State::Error)
return nullptr;
if (m_context->state < PNGLoadingContext::State::BitmapDecoded) {
// NOTE: This forces the chunk decoding to happen.
bool success = decode_png_bitmap(*m_context);
if (!success)
return nullptr;
}
VERIFY(m_context->bitmap);
return m_context->bitmap;
}
void PNGImageDecoderPlugin::set_volatile()
{
if (m_context->bitmap)
@ -984,7 +968,19 @@ ImageFrameDescriptor PNGImageDecoderPlugin::frame(size_t i)
{
if (i > 0)
return {};
return { bitmap(), 0 };
if (m_context->state == PNGLoadingContext::State::Error)
return {};
if (m_context->state < PNGLoadingContext::State::BitmapDecoded) {
// NOTE: This forces the chunk decoding to happen.
bool success = decode_png_bitmap(*m_context);
if (!success)
return {};
}
VERIFY(m_context->bitmap);
return { m_context->bitmap, 0 };
}
}

View file

@ -18,7 +18,6 @@ public:
PNGImageDecoderPlugin(const u8*, size_t);
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) override;
virtual bool sniff() override;

View file

@ -126,21 +126,6 @@ IntSize PPMImageDecoderPlugin::size()
return { m_context->width, m_context->height };
}
RefPtr<Gfx::Bitmap> PPMImageDecoderPlugin::bitmap()
{
if (m_context->state == PPMLoadingContext::State::Error)
return nullptr;
if (m_context->state < PPMLoadingContext::State::Decoded) {
bool success = decode(*m_context);
if (!success)
return nullptr;
}
VERIFY(m_context->bitmap);
return m_context->bitmap;
}
void PPMImageDecoderPlugin::set_volatile()
{
if (m_context->bitmap)
@ -188,7 +173,18 @@ ImageFrameDescriptor PPMImageDecoderPlugin::frame(size_t i)
{
if (i > 0)
return {};
return { bitmap(), 0 };
if (m_context->state == PPMLoadingContext::State::Error)
return {};
if (m_context->state < PPMLoadingContext::State::Decoded) {
bool success = decode(*m_context);
if (!success)
return {};
}
VERIFY(m_context->bitmap);
return { m_context->bitmap, 0 };
}
}

View file

@ -18,7 +18,6 @@ public:
virtual ~PPMImageDecoderPlugin() override;
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) override;

View file

@ -142,11 +142,11 @@ void SpiceAgent::on_message_received()
} else {
RefPtr<Gfx::Bitmap> bitmap;
if (type == ClipboardType::PNG) {
bitmap = Gfx::PNGImageDecoderPlugin(data_buffer.data(), data_buffer.size()).bitmap();
bitmap = Gfx::PNGImageDecoderPlugin(data_buffer.data(), data_buffer.size()).frame(0).image;
} else if (type == ClipboardType::BMP) {
bitmap = Gfx::BMPImageDecoderPlugin(data_buffer.data(), data_buffer.size()).bitmap();
bitmap = Gfx::BMPImageDecoderPlugin(data_buffer.data(), data_buffer.size()).frame(0).image;
} else if (type == ClipboardType::JPG) {
bitmap = Gfx::JPGImageDecoderPlugin(data_buffer.data(), data_buffer.size()).bitmap();
bitmap = Gfx::JPGImageDecoderPlugin(data_buffer.data(), data_buffer.size()).frame(0).image;
} else {
dbgln("Unknown clipboard type: {}", (u32)type);
return;