ImageDecoder.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 ErrorOr<void> initialize() = 0;
  26. virtual bool is_animated() = 0;
  27. virtual size_t loop_count() = 0;
  28. virtual size_t frame_count() = 0;
  29. virtual size_t first_animated_frame_index() = 0;
  30. virtual ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) = 0;
  31. virtual ErrorOr<Optional<ReadonlyBytes>> icc_data() = 0;
  32. protected:
  33. ImageDecoderPlugin() = default;
  34. };
  35. class ImageDecoder : public RefCounted<ImageDecoder> {
  36. public:
  37. static RefPtr<ImageDecoder> try_create_for_raw_bytes(ReadonlyBytes, Optional<DeprecatedString> mime_type = {});
  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. bool is_animated() const { return m_plugin->is_animated(); }
  43. size_t loop_count() const { return m_plugin->loop_count(); }
  44. size_t frame_count() const { return m_plugin->frame_count(); }
  45. size_t first_animated_frame_index() const { return m_plugin->first_animated_frame_index(); }
  46. ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) const { return m_plugin->frame(index, ideal_size); }
  47. ErrorOr<Optional<ReadonlyBytes>> icc_data() const { return m_plugin->icc_data(); }
  48. private:
  49. explicit ImageDecoder(NonnullOwnPtr<ImageDecoderPlugin>);
  50. NonnullOwnPtr<ImageDecoderPlugin> mutable m_plugin;
  51. };
  52. }