PortableImageMapLoader.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/RefPtr.h>
  9. #include <AK/StringView.h>
  10. #include <AK/Types.h>
  11. #include <LibGfx/Bitmap.h>
  12. #include <LibGfx/ImageFormats/PortableImageLoaderCommon.h>
  13. namespace Gfx {
  14. template<class TFormatDetails>
  15. struct PortableImageMapLoadingContext {
  16. using FormatDetails = TFormatDetails;
  17. enum class Type {
  18. Unknown,
  19. ASCII,
  20. RAWBITS
  21. };
  22. enum class State {
  23. NotDecoded = 0,
  24. Error,
  25. MagicNumber,
  26. Width,
  27. Height,
  28. Maxval,
  29. Bitmap,
  30. Decoded
  31. };
  32. Type type { Type::Unknown };
  33. State state { State::NotDecoded };
  34. u8 const* data { nullptr };
  35. size_t data_size { 0 };
  36. size_t width { 0 };
  37. size_t height { 0 };
  38. FormatDetails format_details {};
  39. RefPtr<Gfx::Bitmap> bitmap;
  40. };
  41. template<typename TContext>
  42. class PortableImageDecoderPlugin final : public ImageDecoderPlugin {
  43. public:
  44. static bool sniff(ReadonlyBytes);
  45. static ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> create(ReadonlyBytes);
  46. virtual ~PortableImageDecoderPlugin() override = default;
  47. virtual IntSize size() override;
  48. virtual void set_volatile() override;
  49. [[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) override;
  50. virtual bool initialize() override { return true; }
  51. virtual bool is_animated() override;
  52. virtual size_t loop_count() override;
  53. virtual size_t frame_count() override;
  54. virtual ErrorOr<ImageFrameDescriptor> frame(size_t index) override;
  55. virtual ErrorOr<Optional<ReadonlyBytes>> icc_data() override;
  56. private:
  57. PortableImageDecoderPlugin(u8 const*, size_t);
  58. OwnPtr<TContext> m_context;
  59. };
  60. template<typename TContext>
  61. PortableImageDecoderPlugin<TContext>::PortableImageDecoderPlugin(u8 const* data, size_t size)
  62. {
  63. m_context = make<TContext>();
  64. m_context->data = data;
  65. m_context->data_size = size;
  66. }
  67. template<typename TContext>
  68. IntSize PortableImageDecoderPlugin<TContext>::size()
  69. {
  70. if (m_context->state == TContext::State::Error)
  71. return {};
  72. if (m_context->state < TContext::State::Decoded) {
  73. bool success = decode(*m_context);
  74. if (!success)
  75. return {};
  76. }
  77. return { m_context->width, m_context->height };
  78. }
  79. template<typename TContext>
  80. void PortableImageDecoderPlugin<TContext>::set_volatile()
  81. {
  82. if (m_context->bitmap)
  83. m_context->bitmap->set_volatile();
  84. }
  85. template<typename TContext>
  86. bool PortableImageDecoderPlugin<TContext>::set_nonvolatile(bool& was_purged)
  87. {
  88. if (!m_context->bitmap)
  89. return false;
  90. return m_context->bitmap->set_nonvolatile(was_purged);
  91. }
  92. template<typename TContext>
  93. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> PortableImageDecoderPlugin<TContext>::create(ReadonlyBytes data)
  94. {
  95. return adopt_nonnull_own_or_enomem(new (nothrow) PortableImageDecoderPlugin<TContext>(data.data(), data.size()));
  96. }
  97. template<typename TContext>
  98. bool PortableImageDecoderPlugin<TContext>::sniff(ReadonlyBytes data)
  99. {
  100. using Context = TContext;
  101. if (data.size() < 2)
  102. return false;
  103. if (data.data()[0] == 'P' && data.data()[1] == Context::FormatDetails::ascii_magic_number)
  104. return true;
  105. if (data.data()[0] == 'P' && data.data()[1] == Context::FormatDetails::binary_magic_number)
  106. return true;
  107. return false;
  108. }
  109. template<typename TContext>
  110. bool PortableImageDecoderPlugin<TContext>::is_animated()
  111. {
  112. return false;
  113. }
  114. template<typename TContext>
  115. size_t PortableImageDecoderPlugin<TContext>::loop_count()
  116. {
  117. return 0;
  118. }
  119. template<typename TContext>
  120. size_t PortableImageDecoderPlugin<TContext>::frame_count()
  121. {
  122. return 1;
  123. }
  124. template<typename TContext>
  125. ErrorOr<ImageFrameDescriptor> PortableImageDecoderPlugin<TContext>::frame(size_t index)
  126. {
  127. if (index > 0)
  128. return Error::from_string_literal("PortableImageDecoderPlugin: Invalid frame index");
  129. if (m_context->state == TContext::State::Error)
  130. return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed");
  131. if (m_context->state < TContext::State::Decoded) {
  132. bool success = decode(*m_context);
  133. if (!success)
  134. return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed");
  135. }
  136. VERIFY(m_context->bitmap);
  137. return ImageFrameDescriptor { m_context->bitmap, 0 };
  138. }
  139. template<typename TContext>
  140. ErrorOr<Optional<ReadonlyBytes>> PortableImageDecoderPlugin<TContext>::icc_data()
  141. {
  142. return OptionalNone {};
  143. }
  144. }