ImageDecoder.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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() = default;
  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 ErrorOr<ImageFrameDescriptor> frame(size_t index) = 0;
  32. protected:
  33. ImageDecoderPlugin() = default;
  34. };
  35. class ImageDecoder : public RefCounted<ImageDecoder> {
  36. public:
  37. static RefPtr<ImageDecoder> try_create(ReadonlyBytes);
  38. ~ImageDecoder() = default;
  39. IntSize size() const { return m_plugin->size(); }
  40. int width() const { return size().width(); }
  41. int height() const { return size().height(); }
  42. void set_volatile() { m_plugin->set_volatile(); }
  43. [[nodiscard]] bool set_nonvolatile(bool& was_purged) { return m_plugin->set_nonvolatile(was_purged); }
  44. bool sniff() const { return m_plugin->sniff(); }
  45. bool is_animated() const { return m_plugin->is_animated(); }
  46. size_t loop_count() const { return m_plugin->loop_count(); }
  47. size_t frame_count() const { return m_plugin->frame_count(); }
  48. ErrorOr<ImageFrameDescriptor> frame(size_t index) const { return m_plugin->frame(index); }
  49. private:
  50. explicit ImageDecoder(NonnullOwnPtr<ImageDecoderPlugin>);
  51. NonnullOwnPtr<ImageDecoderPlugin> mutable m_plugin;
  52. };
  53. }