PortableImageLoaderCommon.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * Copyright (c) 2020-2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Debug.h>
  9. #include <AK/DeprecatedString.h>
  10. #include <AK/Endian.h>
  11. #include <AK/ScopeGuard.h>
  12. #include <AK/String.h>
  13. #include <AK/StringBuilder.h>
  14. #include <AK/Types.h>
  15. #include <AK/Vector.h>
  16. #include <LibGfx/Bitmap.h>
  17. #include <LibGfx/Color.h>
  18. #include <LibGfx/ImageFormats/ImageDecoder.h>
  19. namespace Gfx {
  20. static constexpr Color adjust_color(u16 max_val, Color color)
  21. {
  22. color.set_red((color.red() * 255) / max_val);
  23. color.set_green((color.green() * 255) / max_val);
  24. color.set_blue((color.blue() * 255) / max_val);
  25. return color;
  26. }
  27. static inline ErrorOr<u16> read_number(SeekableStream& stream)
  28. {
  29. StringBuilder sb {};
  30. u8 byte {};
  31. for (auto buffer = TRY(stream.read_some({ &byte, 1 })); !buffer.is_empty(); buffer = TRY(stream.read_some({ &byte, 1 }))) {
  32. if (byte == ' ' || byte == '\t' || byte == '\n' || byte == '\r') {
  33. TRY(stream.seek(-1, SeekMode::FromCurrentPosition));
  34. break;
  35. }
  36. sb.append(byte);
  37. }
  38. auto const maybe_value = TRY(sb.to_string()).to_number<u16>();
  39. if (!maybe_value.has_value())
  40. return Error::from_string_literal("Can't convert bytes to a number");
  41. return *maybe_value;
  42. }
  43. template<typename TContext>
  44. static ErrorOr<void> read_comment(TContext& context)
  45. {
  46. auto& stream = *context.stream;
  47. bool is_first_char = true;
  48. u8 byte {};
  49. while ((byte = TRY(stream.template read_value<u8>()))) {
  50. if (is_first_char) {
  51. if (byte != '#')
  52. return Error::from_string_literal("Can't read comment from stream");
  53. is_first_char = false;
  54. } else if (byte == '\t' || byte == '\n') {
  55. break;
  56. }
  57. }
  58. return {};
  59. }
  60. template<typename TContext>
  61. static ErrorOr<void> read_magic_number(TContext& context)
  62. {
  63. if (context.state >= TContext::State::MagicNumber)
  64. return {};
  65. if (TRY(context.stream->size()) < 2) {
  66. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "There is no enough data for {}", TContext::FormatDetails::image_type);
  67. return Error::from_string_literal("There is no enough data to read magic number.");
  68. }
  69. Array<u8, 2> magic_number {};
  70. TRY(context.stream->read_until_filled(Bytes { magic_number }));
  71. if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::ascii_magic_number) {
  72. context.type = TContext::Type::ASCII;
  73. context.state = TContext::State::MagicNumber;
  74. return {};
  75. }
  76. if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::binary_magic_number) {
  77. context.type = TContext::Type::RAWBITS;
  78. context.state = TContext::State::MagicNumber;
  79. return {};
  80. }
  81. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "Magic number is not valid for {}{}{}", magic_number[0], magic_number[1], TContext::FormatDetails::image_type);
  82. return Error::from_string_literal("Unable to recognize magic bytes");
  83. }
  84. template<typename TContext>
  85. static ErrorOr<void> read_whitespace(TContext& context)
  86. {
  87. auto& stream = *context.stream;
  88. bool is_first_char = true;
  89. while (true) {
  90. auto byte_or_error = stream.template read_value<u8>();
  91. // Nothing went wrong if we reached eof while reading a comment.
  92. if (byte_or_error.is_error())
  93. return {};
  94. auto const byte = byte_or_error.value();
  95. if (byte == '#') {
  96. stream.seek(-1, SeekMode::FromCurrentPosition).release_value_but_fixme_should_propagate_errors();
  97. TRY(read_comment(context));
  98. continue;
  99. }
  100. if (byte != ' ' && byte != '\t' && byte != '\n' && byte != '\r') {
  101. stream.seek(-1, SeekMode::FromCurrentPosition).release_value_but_fixme_should_propagate_errors();
  102. if (is_first_char)
  103. return Error::from_string_literal("Can't read whitespace from stream");
  104. break;
  105. }
  106. if (is_first_char)
  107. is_first_char = false;
  108. }
  109. return {};
  110. }
  111. template<typename TContext>
  112. static ErrorOr<void> read_width(TContext& context)
  113. {
  114. context.width = TRY(read_number(*context.stream));
  115. context.state = TContext::State::Width;
  116. return {};
  117. }
  118. template<typename TContext>
  119. static ErrorOr<void> read_height(TContext& context)
  120. {
  121. context.height = TRY(read_number(*context.stream));
  122. context.state = TContext::State::Height;
  123. return {};
  124. }
  125. template<typename TContext>
  126. static ErrorOr<void> read_max_val(TContext& context)
  127. {
  128. context.format_details.max_val = TRY(read_number(*context.stream));
  129. if (context.format_details.max_val > 255) {
  130. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "We can't parse 2 byte color for {}", TContext::FormatDetails::image_type);
  131. context.state = TContext::State::Error;
  132. return Error::from_string_literal("Can't parse 2 byte color");
  133. }
  134. context.state = TContext::State::Maxval;
  135. return {};
  136. }
  137. template<typename TContext>
  138. static ErrorOr<void> create_bitmap(TContext& context)
  139. {
  140. context.bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { context.width, context.height }));
  141. return {};
  142. }
  143. template<typename TContext>
  144. static void set_pixels(TContext& context, Vector<Gfx::Color> const& color_data)
  145. {
  146. size_t index = 0;
  147. for (size_t y = 0; y < context.height; ++y) {
  148. for (size_t x = 0; x < context.width; ++x) {
  149. context.bitmap->set_pixel(x, y, color_data.at(index));
  150. index++;
  151. }
  152. }
  153. }
  154. template<typename TContext>
  155. static ErrorOr<void> decode(TContext& context)
  156. {
  157. if (context.state >= TContext::State::Decoded)
  158. return {};
  159. TRY(read_magic_number(context));
  160. TRY(read_whitespace(context));
  161. TRY(read_width(context));
  162. TRY(read_whitespace(context));
  163. TRY(read_height(context));
  164. if (context.width > maximum_width_for_decoded_images || context.height > maximum_height_for_decoded_images) {
  165. dbgln("This portable network image is too large for comfort: {}x{}", context.width, context.height);
  166. return Error::from_string_literal("This portable network image is too large.");
  167. }
  168. TRY(read_whitespace(context));
  169. if constexpr (requires { context.format_details.max_val; }) {
  170. TRY(read_max_val(context));
  171. TRY(read_whitespace(context));
  172. }
  173. TRY(read_image_data(context));
  174. context.state = TContext::State::Decoded;
  175. return {};
  176. }
  177. }