ImageDecoder.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/OwnPtr.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/RefPtr.h>
  11. #include <LibGfx/Bitmap.h>
  12. #include <LibGfx/Size.h>
  13. namespace Gfx {
  14. class Bitmap;
  15. static constexpr size_t maximum_width_for_decoded_images = 16384;
  16. static constexpr size_t maximum_height_for_decoded_images = 16384;
  17. struct ImageFrameDescriptor {
  18. RefPtr<Bitmap> image;
  19. int duration { 0 };
  20. };
  21. class ImageDecoderPlugin {
  22. public:
  23. virtual ~ImageDecoderPlugin() { }
  24. virtual IntSize size() = 0;
  25. virtual void set_volatile() = 0;
  26. [[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) = 0;
  27. virtual bool sniff() = 0;
  28. virtual bool is_animated() = 0;
  29. virtual size_t loop_count() = 0;
  30. virtual size_t frame_count() = 0;
  31. virtual ImageFrameDescriptor frame(size_t i) = 0;
  32. protected:
  33. virtual RefPtr<Gfx::Bitmap> bitmap() = 0;
  34. ImageDecoderPlugin() { }
  35. };
  36. class ImageDecoder : public RefCounted<ImageDecoder> {
  37. public:
  38. static RefPtr<ImageDecoder> try_create(ReadonlyBytes);
  39. ~ImageDecoder();
  40. IntSize size() const { return m_plugin->size(); }
  41. int width() const { return size().width(); }
  42. int height() const { return size().height(); }
  43. void set_volatile() { m_plugin->set_volatile(); }
  44. [[nodiscard]] bool set_nonvolatile(bool& was_purged) { return m_plugin->set_nonvolatile(was_purged); }
  45. bool sniff() const { return m_plugin->sniff(); }
  46. bool is_animated() const { return m_plugin->is_animated(); }
  47. size_t loop_count() const { return m_plugin->loop_count(); }
  48. size_t frame_count() const { return m_plugin->frame_count(); }
  49. ImageFrameDescriptor frame(size_t i) const { return m_plugin->frame(i); }
  50. private:
  51. explicit ImageDecoder(NonnullOwnPtr<ImageDecoderPlugin>);
  52. NonnullOwnPtr<ImageDecoderPlugin> mutable m_plugin;
  53. };
  54. }