ImageDecoder.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 initialize() = 0;
  28. virtual bool is_animated() = 0;
  29. virtual size_t loop_count() = 0;
  30. virtual size_t frame_count() = 0;
  31. virtual size_t first_animated_frame_index() = 0;
  32. virtual ErrorOr<ImageFrameDescriptor> frame(size_t index) = 0;
  33. virtual ErrorOr<Optional<ReadonlyBytes>> icc_data() = 0;
  34. protected:
  35. ImageDecoderPlugin() = default;
  36. };
  37. class ImageDecoder : public RefCounted<ImageDecoder> {
  38. public:
  39. static RefPtr<ImageDecoder> try_create_for_raw_bytes(ReadonlyBytes, Optional<DeprecatedString> mime_type = {});
  40. ~ImageDecoder() = default;
  41. IntSize size() const { return m_plugin->size(); }
  42. int width() const { return size().width(); }
  43. int height() const { return size().height(); }
  44. void set_volatile() { m_plugin->set_volatile(); }
  45. [[nodiscard]] bool set_nonvolatile(bool& was_purged) { return m_plugin->set_nonvolatile(was_purged); }
  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. size_t first_animated_frame_index() const { return m_plugin->first_animated_frame_index(); }
  50. ErrorOr<ImageFrameDescriptor> frame(size_t index) const { return m_plugin->frame(index); }
  51. ErrorOr<Optional<ReadonlyBytes>> icc_data() const { return m_plugin->icc_data(); }
  52. private:
  53. explicit ImageDecoder(NonnullOwnPtr<ImageDecoderPlugin>);
  54. NonnullOwnPtr<ImageDecoderPlugin> mutable m_plugin;
  55. };
  56. }