PortableImageMapLoader.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/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. PortableImageDecoderPlugin(u8 const*, size_t);
  45. virtual ~PortableImageDecoderPlugin() override = default;
  46. virtual IntSize size() override;
  47. virtual void set_volatile() override;
  48. [[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) override;
  49. virtual bool sniff() override;
  50. virtual bool is_animated() override;
  51. virtual size_t loop_count() override;
  52. virtual size_t frame_count() override;
  53. virtual ErrorOr<ImageFrameDescriptor> frame(size_t index) override;
  54. private:
  55. OwnPtr<TContext> m_context;
  56. };
  57. template<typename TContext>
  58. PortableImageDecoderPlugin<TContext>::PortableImageDecoderPlugin(u8 const* data, size_t size)
  59. {
  60. m_context = make<TContext>();
  61. m_context->data = data;
  62. m_context->data_size = size;
  63. }
  64. template<typename TContext>
  65. IntSize PortableImageDecoderPlugin<TContext>::size()
  66. {
  67. if (m_context->state == TContext::State::Error)
  68. return {};
  69. if (m_context->state < TContext::State::Decoded) {
  70. bool success = decode(*m_context);
  71. if (!success)
  72. return {};
  73. }
  74. return { m_context->width, m_context->height };
  75. }
  76. template<typename TContext>
  77. void PortableImageDecoderPlugin<TContext>::set_volatile()
  78. {
  79. if (m_context->bitmap)
  80. m_context->bitmap->set_volatile();
  81. }
  82. template<typename TContext>
  83. bool PortableImageDecoderPlugin<TContext>::set_nonvolatile(bool& was_purged)
  84. {
  85. if (!m_context->bitmap)
  86. return false;
  87. return m_context->bitmap->set_nonvolatile(was_purged);
  88. }
  89. template<typename TContext>
  90. bool PortableImageDecoderPlugin<TContext>::sniff()
  91. {
  92. using Context = TContext;
  93. if (m_context->data_size < 2)
  94. return false;
  95. if (m_context->data[0] == 'P' && m_context->data[1] == Context::FormatDetails::ascii_magic_number)
  96. return true;
  97. if (m_context->data[0] == 'P' && m_context->data[1] == Context::FormatDetails::binary_magic_number)
  98. return true;
  99. return false;
  100. }
  101. template<typename TContext>
  102. bool PortableImageDecoderPlugin<TContext>::is_animated()
  103. {
  104. return false;
  105. }
  106. template<typename TContext>
  107. size_t PortableImageDecoderPlugin<TContext>::loop_count()
  108. {
  109. return 0;
  110. }
  111. template<typename TContext>
  112. size_t PortableImageDecoderPlugin<TContext>::frame_count()
  113. {
  114. return 1;
  115. }
  116. template<typename TContext>
  117. ErrorOr<ImageFrameDescriptor> PortableImageDecoderPlugin<TContext>::frame(size_t index)
  118. {
  119. if (index > 0)
  120. return Error::from_string_literal("PortableImageDecoderPlugin: Invalid frame index"sv);
  121. if (m_context->state == TContext::State::Error)
  122. return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed"sv);
  123. if (m_context->state < TContext::State::Decoded) {
  124. bool success = decode(*m_context);
  125. if (!success)
  126. return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed"sv);
  127. }
  128. VERIFY(m_context->bitmap);
  129. return ImageFrameDescriptor { m_context->bitmap, 0 };
  130. }
  131. }