PortableImageMapLoader.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/BufferedStream.h>
  9. #include <AK/MemoryStream.h>
  10. #include <AK/RefPtr.h>
  11. #include <AK/StringView.h>
  12. #include <AK/Types.h>
  13. #include <LibGfx/Bitmap.h>
  14. #include <LibGfx/ImageFormats/PortableImageLoaderCommon.h>
  15. namespace Gfx {
  16. template<class TFormatDetails>
  17. struct PortableImageMapLoadingContext {
  18. using FormatDetails = TFormatDetails;
  19. enum class Type {
  20. Unknown,
  21. ASCII,
  22. RAWBITS
  23. };
  24. enum class State {
  25. NotDecoded = 0,
  26. Error,
  27. MagicNumber,
  28. Width,
  29. Height,
  30. Maxval,
  31. Bitmap,
  32. Decoded
  33. };
  34. Type type { Type::Unknown };
  35. State state { State::NotDecoded };
  36. size_t width { 0 };
  37. size_t height { 0 };
  38. FormatDetails format_details {};
  39. RefPtr<Gfx::Bitmap> bitmap;
  40. NonnullOwnPtr<SeekableStream> stream;
  41. PortableImageMapLoadingContext(NonnullOwnPtr<SeekableStream> stream)
  42. : stream(move(stream))
  43. {
  44. }
  45. };
  46. template<typename TContext>
  47. class PortableImageDecoderPlugin final : public ImageDecoderPlugin {
  48. public:
  49. static bool sniff(ReadonlyBytes);
  50. static ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> create(ReadonlyBytes);
  51. virtual ~PortableImageDecoderPlugin() override = default;
  52. virtual IntSize size() override;
  53. virtual void set_volatile() override;
  54. [[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) override;
  55. virtual bool initialize() override { return true; }
  56. virtual bool is_animated() override;
  57. virtual size_t loop_count() override;
  58. virtual size_t frame_count() override;
  59. virtual size_t first_animated_frame_index() override;
  60. virtual ErrorOr<ImageFrameDescriptor> frame(size_t index) override;
  61. virtual ErrorOr<Optional<ReadonlyBytes>> icc_data() override;
  62. private:
  63. PortableImageDecoderPlugin(NonnullOwnPtr<SeekableStream> stream);
  64. OwnPtr<TContext> m_context;
  65. };
  66. template<typename TContext>
  67. PortableImageDecoderPlugin<TContext>::PortableImageDecoderPlugin(NonnullOwnPtr<SeekableStream> stream)
  68. {
  69. m_context = make<TContext>(move(stream));
  70. }
  71. template<typename TContext>
  72. IntSize PortableImageDecoderPlugin<TContext>::size()
  73. {
  74. if (m_context->state == TContext::State::Error)
  75. return {};
  76. if (m_context->state < TContext::State::Decoded) {
  77. if (decode(*m_context).is_error()) {
  78. m_context->state = TContext::State::Error;
  79. // FIXME: We should propagate errors
  80. return {};
  81. }
  82. }
  83. return { m_context->width, m_context->height };
  84. }
  85. template<typename TContext>
  86. void PortableImageDecoderPlugin<TContext>::set_volatile()
  87. {
  88. if (m_context->bitmap)
  89. m_context->bitmap->set_volatile();
  90. }
  91. template<typename TContext>
  92. bool PortableImageDecoderPlugin<TContext>::set_nonvolatile(bool& was_purged)
  93. {
  94. if (!m_context->bitmap)
  95. return false;
  96. return m_context->bitmap->set_nonvolatile(was_purged);
  97. }
  98. template<typename TContext>
  99. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> PortableImageDecoderPlugin<TContext>::create(ReadonlyBytes data)
  100. {
  101. auto stream = TRY(try_make<FixedMemoryStream>(data));
  102. return adopt_nonnull_own_or_enomem(new (nothrow) PortableImageDecoderPlugin<TContext>(move(stream)));
  103. }
  104. template<typename TContext>
  105. bool PortableImageDecoderPlugin<TContext>::sniff(ReadonlyBytes data)
  106. {
  107. using Context = TContext;
  108. if (data.size() < 2)
  109. return false;
  110. if (data.data()[0] == 'P' && data.data()[1] == Context::FormatDetails::ascii_magic_number)
  111. return true;
  112. if (data.data()[0] == 'P' && data.data()[1] == Context::FormatDetails::binary_magic_number)
  113. return true;
  114. return false;
  115. }
  116. template<typename TContext>
  117. bool PortableImageDecoderPlugin<TContext>::is_animated()
  118. {
  119. return false;
  120. }
  121. template<typename TContext>
  122. size_t PortableImageDecoderPlugin<TContext>::loop_count()
  123. {
  124. return 0;
  125. }
  126. template<typename TContext>
  127. size_t PortableImageDecoderPlugin<TContext>::frame_count()
  128. {
  129. return 1;
  130. }
  131. template<typename TContext>
  132. size_t PortableImageDecoderPlugin<TContext>::first_animated_frame_index()
  133. {
  134. return 0;
  135. }
  136. template<typename TContext>
  137. ErrorOr<ImageFrameDescriptor> PortableImageDecoderPlugin<TContext>::frame(size_t index)
  138. {
  139. if (index > 0)
  140. return Error::from_string_literal("PortableImageDecoderPlugin: Invalid frame index");
  141. if (m_context->state == TContext::State::Error)
  142. return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed");
  143. if (m_context->state < TContext::State::Decoded) {
  144. if (decode(*m_context).is_error()) {
  145. m_context->state = TContext::State::Error;
  146. return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed");
  147. }
  148. }
  149. VERIFY(m_context->bitmap);
  150. return ImageFrameDescriptor { m_context->bitmap, 0 };
  151. }
  152. template<typename TContext>
  153. ErrorOr<Optional<ReadonlyBytes>> PortableImageDecoderPlugin<TContext>::icc_data()
  154. {
  155. return OptionalNone {};
  156. }
  157. }