Skip to content

Commit d22bb92

Browse files
peterdnawesomekling
authored andcommitted
LibGfx: Add support for animated images to ImageDecoder{Plugin}
Adds methods to determine whether an image is animated, how many times the animation loops, the number of frames, and to get individual frames. Implements stubs of these methods for PNGImageDecoderPlugin and GIFImageDecoderPlugin.
1 parent eec99b2 commit d22bb92

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

Libraries/LibGfx/GIFLoader.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,4 +528,28 @@ bool GIFImageDecoderPlugin::sniff()
528528
BufferStream stream(buffer);
529529
return decode_gif_header(stream).has_value();
530530
}
531+
532+
bool GIFImageDecoderPlugin::is_animated()
533+
{
534+
return false;
535+
}
536+
537+
size_t GIFImageDecoderPlugin::loop_count()
538+
{
539+
return 0;
540+
}
541+
542+
size_t GIFImageDecoderPlugin::frame_count()
543+
{
544+
return 1;
545+
}
546+
547+
ImageFrameDescriptor GIFImageDecoderPlugin::frame(size_t i)
548+
{
549+
if (i > 0) {
550+
return { bitmap(), 0 };
551+
}
552+
return {};
553+
}
554+
531555
}

Libraries/LibGfx/GIFLoader.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class GIFImageDecoderPlugin final : public ImageDecoderPlugin {
4646
virtual void set_volatile() override;
4747
[[nodiscard]] virtual bool set_nonvolatile() override;
4848
virtual bool sniff() override;
49+
virtual bool is_animated() override;
50+
virtual size_t loop_count() override;
51+
virtual size_t frame_count() override;
52+
virtual ImageFrameDescriptor frame(size_t i) override;
4953

5054
private:
5155
OwnPtr<GIFLoadingContext> m_context;

Libraries/LibGfx/ImageDecoder.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,20 @@
2626

2727
#pragma once
2828

29-
#include <AK/NonnullRefPtr.h>
3029
#include <AK/OwnPtr.h>
3130
#include <AK/RefCounted.h>
31+
#include <AK/RefPtr.h>
3232
#include <LibGfx/Size.h>
3333

3434
namespace Gfx {
3535

3636
class Bitmap;
3737

38+
struct ImageFrameDescriptor {
39+
RefPtr<Bitmap> image;
40+
int duration { 0 };
41+
};
42+
3843
class ImageDecoderPlugin {
3944
public:
4045
virtual ~ImageDecoderPlugin() {}
@@ -47,6 +52,11 @@ class ImageDecoderPlugin {
4752

4853
virtual bool sniff() = 0;
4954

55+
virtual bool is_animated() = 0;
56+
virtual size_t loop_count() = 0;
57+
virtual size_t frame_count() = 0;
58+
virtual ImageFrameDescriptor frame(size_t i) = 0;
59+
5060
protected:
5161
ImageDecoderPlugin() {}
5262
};
@@ -62,7 +72,11 @@ class ImageDecoder : public RefCounted<ImageDecoder> {
6272
RefPtr<Gfx::Bitmap> bitmap() const;
6373
void set_volatile() { m_plugin->set_volatile(); }
6474
[[nodiscard]] bool set_nonvolatile() { return m_plugin->set_nonvolatile(); }
65-
bool sniff() { return m_plugin->sniff(); };
75+
bool sniff() const { return m_plugin->sniff(); }
76+
bool is_animated() const { return m_plugin->is_animated(); }
77+
size_t loop_count() const { return m_plugin->loop_count(); }
78+
size_t frame_count() const { return m_plugin->frame_count(); }
79+
ImageFrameDescriptor frame(size_t i) const { return m_plugin->frame(i); }
6680

6781
private:
6882
ImageDecoder(const u8*, size_t);

Libraries/LibGfx/PNGLoader.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,4 +834,27 @@ bool PNGImageDecoderPlugin::sniff()
834834
return decode_png_header(*m_context);
835835
}
836836

837+
bool PNGImageDecoderPlugin::is_animated()
838+
{
839+
return false;
840+
}
841+
842+
size_t PNGImageDecoderPlugin::loop_count()
843+
{
844+
return 0;
845+
}
846+
847+
size_t PNGImageDecoderPlugin::frame_count()
848+
{
849+
return 1;
850+
}
851+
852+
ImageFrameDescriptor PNGImageDecoderPlugin::frame(size_t i)
853+
{
854+
if (i > 0) {
855+
return { bitmap(), 0 };
856+
}
857+
return {};
858+
}
859+
837860
}

Libraries/LibGfx/PNGLoader.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class PNGImageDecoderPlugin final : public ImageDecoderPlugin {
4646
virtual void set_volatile() override;
4747
[[nodiscard]] virtual bool set_nonvolatile() override;
4848
virtual bool sniff() override;
49+
virtual bool is_animated() override;
50+
virtual size_t loop_count() override;
51+
virtual size_t frame_count() override;
52+
virtual ImageFrameDescriptor frame(size_t i) override;
4953

5054
private:
5155
OwnPtr<PNGLoadingContext> m_context;

0 commit comments

Comments
 (0)