ImageDecoder.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include <LibGfx/VectorGraphic.h>
  14. namespace Gfx {
  15. class Bitmap;
  16. static constexpr size_t maximum_width_for_decoded_images = 16384;
  17. static constexpr size_t maximum_height_for_decoded_images = 16384;
  18. struct ImageFrameDescriptor {
  19. RefPtr<Bitmap> image;
  20. int duration { 0 };
  21. };
  22. struct VectorImageFrameDescriptor {
  23. RefPtr<VectorGraphic> image;
  24. int duration { 0 };
  25. };
  26. class ImageDecoderPlugin {
  27. public:
  28. virtual ~ImageDecoderPlugin() = default;
  29. // Each plugin should implement these static functions and register them in ImageDecoder.cpp
  30. // Implement sniff() if the file includes a magic number
  31. // static bool sniff(ReadonlyBytes);
  32. // Implement validate_before_create() otherwise
  33. // static ErrorOr<bool> validate_before_create(ReadonlyBytes);
  34. // This function should be used to both create the context and parse the image header.
  35. // static ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> create(ReadonlyBytes);
  36. // This should always be available as gathered in create()
  37. virtual IntSize size() = 0;
  38. virtual bool is_animated() = 0;
  39. virtual size_t loop_count() = 0;
  40. virtual size_t frame_count() = 0;
  41. virtual size_t first_animated_frame_index() = 0;
  42. virtual ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) = 0;
  43. virtual ErrorOr<Optional<ReadonlyBytes>> icc_data() = 0;
  44. virtual bool is_vector() { return false; }
  45. virtual ErrorOr<VectorImageFrameDescriptor> vector_frame(size_t) { VERIFY_NOT_REACHED(); }
  46. protected:
  47. ImageDecoderPlugin() = default;
  48. };
  49. class ImageDecoder : public RefCounted<ImageDecoder> {
  50. public:
  51. static RefPtr<ImageDecoder> try_create_for_raw_bytes(ReadonlyBytes, Optional<DeprecatedString> mime_type = {});
  52. ~ImageDecoder() = default;
  53. IntSize size() const { return m_plugin->size(); }
  54. int width() const { return size().width(); }
  55. int height() const { return size().height(); }
  56. bool is_animated() const { return m_plugin->is_animated(); }
  57. size_t loop_count() const { return m_plugin->loop_count(); }
  58. size_t frame_count() const { return m_plugin->frame_count(); }
  59. size_t first_animated_frame_index() const { return m_plugin->first_animated_frame_index(); }
  60. ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) const { return m_plugin->frame(index, ideal_size); }
  61. ErrorOr<Optional<ReadonlyBytes>> icc_data() const { return m_plugin->icc_data(); }
  62. bool is_vector() { return m_plugin->is_vector(); }
  63. ErrorOr<VectorImageFrameDescriptor> vector_frame(size_t index) { return m_plugin->vector_frame(index); }
  64. private:
  65. explicit ImageDecoder(NonnullOwnPtr<ImageDecoderPlugin>);
  66. NonnullOwnPtr<ImageDecoderPlugin> mutable m_plugin;
  67. };
  68. }