ImageDecoder.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/CMYKBitmap.h>
  13. #include <LibGfx/Size.h>
  14. #include <LibGfx/VectorGraphic.h>
  15. namespace Gfx {
  16. class Bitmap;
  17. struct ImageFrameDescriptor {
  18. RefPtr<Bitmap> image;
  19. int duration { 0 };
  20. };
  21. struct VectorImageFrameDescriptor {
  22. RefPtr<VectorGraphic> image;
  23. int duration { 0 };
  24. };
  25. enum class NaturalFrameFormat {
  26. RGB,
  27. Grayscale,
  28. CMYK,
  29. Vector,
  30. };
  31. class ImageDecoderPlugin {
  32. public:
  33. virtual ~ImageDecoderPlugin() = default;
  34. // Each plugin should implement these static functions and register them in ImageDecoder.cpp
  35. // Implement sniff() if the file includes a magic number
  36. // static bool sniff(ReadonlyBytes);
  37. // Implement validate_before_create() otherwise
  38. // static ErrorOr<bool> validate_before_create(ReadonlyBytes);
  39. // This function should be used to both create the context and parse the image header.
  40. // static ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> create(ReadonlyBytes);
  41. // This should always be available as gathered in create()
  42. virtual IntSize size() = 0;
  43. // Override this if the format supports animated images
  44. virtual bool is_animated() { return false; }
  45. virtual size_t loop_count() { return 0; }
  46. virtual size_t frame_count() { return 1; }
  47. virtual size_t first_animated_frame_index() { return 0; }
  48. virtual ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) = 0;
  49. virtual ErrorOr<Optional<ReadonlyBytes>> icc_data() { return OptionalNone {}; }
  50. virtual NaturalFrameFormat natural_frame_format() const { return NaturalFrameFormat::RGB; }
  51. virtual ErrorOr<NonnullRefPtr<CMYKBitmap>> cmyk_frame() { VERIFY_NOT_REACHED(); }
  52. virtual ErrorOr<VectorImageFrameDescriptor> vector_frame(size_t) { VERIFY_NOT_REACHED(); }
  53. protected:
  54. ImageDecoderPlugin() = default;
  55. };
  56. class ImageDecoder : public RefCounted<ImageDecoder> {
  57. public:
  58. static RefPtr<ImageDecoder> try_create_for_raw_bytes(ReadonlyBytes, Optional<ByteString> mime_type = {});
  59. ~ImageDecoder() = default;
  60. IntSize size() const { return m_plugin->size(); }
  61. int width() const { return size().width(); }
  62. int height() const { return size().height(); }
  63. bool is_animated() const { return m_plugin->is_animated(); }
  64. size_t loop_count() const { return m_plugin->loop_count(); }
  65. size_t frame_count() const { return m_plugin->frame_count(); }
  66. size_t first_animated_frame_index() const { return m_plugin->first_animated_frame_index(); }
  67. ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) const { return m_plugin->frame(index, ideal_size); }
  68. ErrorOr<Optional<ReadonlyBytes>> icc_data() const { return m_plugin->icc_data(); }
  69. NaturalFrameFormat natural_frame_format() { return m_plugin->natural_frame_format(); }
  70. // Call only if natural_frame_format() == NaturalFrameFormat::CMYK.
  71. ErrorOr<NonnullRefPtr<CMYKBitmap>> cmyk_frame(size_t) { return m_plugin->cmyk_frame(); }
  72. // Call only if natural_frame_format() == NaturalFrameFormat::Vector.
  73. ErrorOr<VectorImageFrameDescriptor> vector_frame(size_t index) { return m_plugin->vector_frame(index); }
  74. private:
  75. explicit ImageDecoder(NonnullOwnPtr<ImageDecoderPlugin>);
  76. NonnullOwnPtr<ImageDecoderPlugin> mutable m_plugin;
  77. };
  78. }