PortableImageMapLoader.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. HeaderDecoded,
  28. BitmapDecoded,
  29. };
  30. Type type { Type::Unknown };
  31. State state { State::NotDecoded };
  32. size_t width { 0 };
  33. size_t height { 0 };
  34. FormatDetails format_details {};
  35. RefPtr<Gfx::Bitmap> bitmap;
  36. NonnullOwnPtr<SeekableStream> stream;
  37. PortableImageMapLoadingContext(NonnullOwnPtr<SeekableStream> stream)
  38. : stream(move(stream))
  39. {
  40. }
  41. };
  42. template<typename TContext>
  43. class PortableImageDecoderPlugin final : public ImageDecoderPlugin {
  44. public:
  45. static bool sniff(ReadonlyBytes);
  46. static ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> create(ReadonlyBytes);
  47. virtual ~PortableImageDecoderPlugin() override = default;
  48. virtual IntSize size() override;
  49. virtual ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) override;
  50. virtual NaturalFrameFormat natural_frame_format() const override;
  51. virtual ErrorOr<NonnullRefPtr<CMYKBitmap>> cmyk_frame() override;
  52. private:
  53. PortableImageDecoderPlugin(NonnullOwnPtr<SeekableStream> stream);
  54. OwnPtr<TContext> m_context;
  55. };
  56. template<typename TContext>
  57. PortableImageDecoderPlugin<TContext>::PortableImageDecoderPlugin(NonnullOwnPtr<SeekableStream> stream)
  58. {
  59. m_context = make<TContext>(move(stream));
  60. }
  61. template<typename TContext>
  62. IntSize PortableImageDecoderPlugin<TContext>::size()
  63. {
  64. return { m_context->width, m_context->height };
  65. }
  66. template<typename TContext>
  67. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> PortableImageDecoderPlugin<TContext>::create(ReadonlyBytes data)
  68. {
  69. auto stream = TRY(try_make<FixedMemoryStream>(data));
  70. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) PortableImageDecoderPlugin<TContext>(move(stream))));
  71. if constexpr (TContext::FormatDetails::binary_magic_number == '7')
  72. TRY(read_pam_header(*plugin->m_context));
  73. else
  74. TRY(read_header(*plugin->m_context));
  75. return plugin;
  76. }
  77. template<typename TContext>
  78. bool PortableImageDecoderPlugin<TContext>::sniff(ReadonlyBytes data)
  79. {
  80. using Context = TContext;
  81. if (data.size() < 2)
  82. return false;
  83. if constexpr (requires { Context::FormatDetails::ascii_magic_number; }) {
  84. if (data.data()[0] == 'P' && data.data()[1] == Context::FormatDetails::ascii_magic_number)
  85. return true;
  86. }
  87. if (data.data()[0] == 'P' && data.data()[1] == Context::FormatDetails::binary_magic_number)
  88. return true;
  89. return false;
  90. }
  91. template<typename TContext>
  92. ErrorOr<ImageFrameDescriptor> PortableImageDecoderPlugin<TContext>::frame(size_t index, Optional<IntSize>)
  93. {
  94. if (index > 0)
  95. return Error::from_string_literal("PortableImageDecoderPlugin: Invalid frame index");
  96. if (m_context->state == TContext::State::Error)
  97. return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed");
  98. if (m_context->state < TContext::State::BitmapDecoded) {
  99. if (decode(*m_context).is_error()) {
  100. m_context->state = TContext::State::Error;
  101. return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed");
  102. }
  103. }
  104. if constexpr (requires { TContext::FormatDetails::cmyk_bitmap; }) {
  105. if (m_context->format_details.cmyk_bitmap.has_value())
  106. m_context->bitmap = TRY(m_context->format_details.cmyk_bitmap.value()->to_low_quality_rgb());
  107. }
  108. VERIFY(m_context->bitmap);
  109. return ImageFrameDescriptor { m_context->bitmap, 0 };
  110. }
  111. template<typename TContext>
  112. NaturalFrameFormat PortableImageDecoderPlugin<TContext>::natural_frame_format() const
  113. {
  114. if constexpr (requires { TContext::FormatDetails::cmyk_bitmap; }) {
  115. if (m_context->format_details.depth == 4 && m_context->format_details.tupl_type == "CMYK"sv)
  116. return NaturalFrameFormat::CMYK;
  117. }
  118. return NaturalFrameFormat::RGB;
  119. }
  120. template<typename TContext>
  121. ErrorOr<NonnullRefPtr<CMYKBitmap>> PortableImageDecoderPlugin<TContext>::cmyk_frame()
  122. {
  123. if constexpr (requires { TContext::FormatDetails::cmyk_bitmap; }) {
  124. VERIFY(natural_frame_format() == NaturalFrameFormat::CMYK);
  125. if (m_context->state == TContext::State::Error)
  126. return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed");
  127. if (m_context->state < TContext::State::BitmapDecoded) {
  128. if (decode(*m_context).is_error()) {
  129. m_context->state = TContext::State::Error;
  130. return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed");
  131. }
  132. }
  133. return *m_context->format_details.cmyk_bitmap.value();
  134. }
  135. VERIFY_NOT_REACHED();
  136. }
  137. }