ImageDecoder.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 ErrorOr<ImageFrameDescriptor> frame(size_t index) = 0;
  32. virtual ErrorOr<Optional<ReadonlyBytes>> icc_data() = 0;
  33. protected:
  34. ImageDecoderPlugin() = default;
  35. };
  36. class ImageDecoder : public RefCounted<ImageDecoder> {
  37. public:
  38. static RefPtr<ImageDecoder> try_create_for_raw_bytes(ReadonlyBytes, Optional<DeprecatedString> mime_type = {});
  39. ~ImageDecoder() = default;
  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 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. ErrorOr<Optional<ReadonlyBytes>> icc_data() const { return m_plugin->icc_data(); }
  50. private:
  51. explicit ImageDecoder(NonnullOwnPtr<ImageDecoderPlugin>);
  52. NonnullOwnPtr<ImageDecoderPlugin> mutable m_plugin;
  53. };
  54. }