ImageDecoder.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2018-2020, 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 RefPtr<Gfx::Bitmap> bitmap() = 0;
  26. virtual void set_volatile() = 0;
  27. [[nodiscard]] virtual bool set_nonvolatile() = 0;
  28. virtual bool sniff() = 0;
  29. virtual bool is_animated() = 0;
  30. virtual size_t loop_count() = 0;
  31. virtual size_t frame_count() = 0;
  32. virtual ImageFrameDescriptor frame(size_t i) = 0;
  33. protected:
  34. ImageDecoderPlugin() { }
  35. };
  36. class ImageDecoder : public RefCounted<ImageDecoder> {
  37. public:
  38. static NonnullRefPtr<ImageDecoder> create(const u8* data, size_t size) { return adopt_ref(*new ImageDecoder(data, size)); }
  39. static NonnullRefPtr<ImageDecoder> create(const ByteBuffer& data) { return adopt_ref(*new ImageDecoder(data.data(), data.size())); }
  40. ~ImageDecoder();
  41. bool is_valid() const { return m_plugin; }
  42. IntSize size() const { return m_plugin ? m_plugin->size() : IntSize(); }
  43. int width() const { return size().width(); }
  44. int height() const { return size().height(); }
  45. RefPtr<Gfx::Bitmap> bitmap() const;
  46. void set_volatile()
  47. {
  48. if (m_plugin)
  49. m_plugin->set_volatile();
  50. }
  51. [[nodiscard]] bool set_nonvolatile() { return m_plugin ? m_plugin->set_nonvolatile() : false; }
  52. bool sniff() const { return m_plugin ? m_plugin->sniff() : false; }
  53. bool is_animated() const { return m_plugin ? m_plugin->is_animated() : false; }
  54. size_t loop_count() const { return m_plugin ? m_plugin->loop_count() : 0; }
  55. size_t frame_count() const { return m_plugin ? m_plugin->frame_count() : 0; }
  56. ImageFrameDescriptor frame(size_t i) const { return m_plugin ? m_plugin->frame(i) : ImageFrameDescriptor(); }
  57. private:
  58. ImageDecoder(const u8*, size_t);
  59. mutable OwnPtr<ImageDecoderPlugin> m_plugin;
  60. };
  61. }