PortableImageLoaderCommon.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #include <LibGfx/Streamer.h>
  20. namespace Gfx {
  21. static constexpr Color adjust_color(u16 max_val, Color color)
  22. {
  23. color.set_red((color.red() * 255) / max_val);
  24. color.set_green((color.green() * 255) / max_val);
  25. color.set_blue((color.blue() * 255) / max_val);
  26. return color;
  27. }
  28. static inline ErrorOr<u16> read_number(Streamer& streamer)
  29. {
  30. u8 byte {};
  31. StringBuilder sb {};
  32. while (streamer.read(byte)) {
  33. if (byte == ' ' || byte == '\t' || byte == '\n' || byte == '\r') {
  34. streamer.step_back();
  35. break;
  36. }
  37. sb.append(byte);
  38. }
  39. auto const maybe_value = TRY(sb.to_string()).to_number<u16>();
  40. if (!maybe_value.has_value())
  41. return Error::from_string_literal("Can't convert bytes to a number");
  42. return *maybe_value;
  43. }
  44. template<typename TContext>
  45. static ErrorOr<void> read_comment([[maybe_unused]] TContext& context, Streamer& streamer)
  46. {
  47. bool is_first_char = true;
  48. u8 byte {};
  49. while (streamer.read(byte)) {
  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 bool read_magic_number(TContext& context, Streamer& streamer)
  62. {
  63. if (context.state >= TContext::State::MagicNumber) {
  64. return true;
  65. }
  66. if (!context.data || context.data_size < 2) {
  67. context.state = TContext::State::Error;
  68. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "There is no enough data for {}", TContext::FormatDetails::image_type);
  69. return false;
  70. }
  71. u8 magic_number[2] {};
  72. if (!streamer.read_bytes(magic_number, 2)) {
  73. context.state = TContext::State::Error;
  74. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "We can't read magic number for {}", TContext::FormatDetails::image_type);
  75. return false;
  76. }
  77. if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::ascii_magic_number) {
  78. context.type = TContext::Type::ASCII;
  79. context.state = TContext::State::MagicNumber;
  80. return true;
  81. }
  82. if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::binary_magic_number) {
  83. context.type = TContext::Type::RAWBITS;
  84. context.state = TContext::State::MagicNumber;
  85. return true;
  86. }
  87. context.state = TContext::State::Error;
  88. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "Magic number is not valid for {}{}{}", magic_number[0], magic_number[1], TContext::FormatDetails::image_type);
  89. return false;
  90. }
  91. template<typename TContext>
  92. static ErrorOr<void> read_whitespace(TContext& context, Streamer& streamer)
  93. {
  94. bool is_first_char = true;
  95. u8 byte {};
  96. while (streamer.read(byte)) {
  97. if (byte == '#') {
  98. streamer.step_back();
  99. TRY(read_comment(context, streamer));
  100. continue;
  101. }
  102. if (byte != ' ' && byte != '\t' && byte != '\n' && byte != '\r') {
  103. streamer.step_back();
  104. if (is_first_char)
  105. return Error::from_string_literal("Can't read whitespace from stream");
  106. break;
  107. }
  108. if (is_first_char)
  109. is_first_char = false;
  110. }
  111. return {};
  112. }
  113. template<typename TContext>
  114. static bool read_width(TContext& context, Streamer& streamer)
  115. {
  116. auto number_or_error = read_number(streamer);
  117. if (number_or_error.is_error())
  118. return false;
  119. context.width = number_or_error.value();
  120. context.state = TContext::State::Width;
  121. return true;
  122. }
  123. template<typename TContext>
  124. static bool read_height(TContext& context, Streamer& streamer)
  125. {
  126. auto number_or_error = read_number(streamer);
  127. if (number_or_error.is_error())
  128. return false;
  129. context.height = number_or_error.value();
  130. context.state = TContext::State::Height;
  131. return true;
  132. }
  133. template<typename TContext>
  134. static bool read_max_val(TContext& context, Streamer& streamer)
  135. {
  136. auto number_or_error = read_number(streamer);
  137. if (number_or_error.is_error())
  138. return false;
  139. context.format_details.max_val = number_or_error.value();
  140. if (context.format_details.max_val > 255) {
  141. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "We can't parse 2 byte color for {}", TContext::FormatDetails::image_type);
  142. context.state = TContext::State::Error;
  143. return false;
  144. }
  145. context.state = TContext::State::Maxval;
  146. return true;
  147. }
  148. template<typename TContext>
  149. static bool create_bitmap(TContext& context)
  150. {
  151. auto bitmap_or_error = Bitmap::create(BitmapFormat::BGRx8888, { context.width, context.height });
  152. if (bitmap_or_error.is_error()) {
  153. context.state = TContext::State::Error;
  154. return false;
  155. }
  156. context.bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  157. return true;
  158. }
  159. template<typename TContext>
  160. static void set_pixels(TContext& context, Vector<Gfx::Color> const& color_data)
  161. {
  162. size_t index = 0;
  163. for (size_t y = 0; y < context.height; ++y) {
  164. for (size_t x = 0; x < context.width; ++x) {
  165. context.bitmap->set_pixel(x, y, color_data.at(index));
  166. index++;
  167. }
  168. }
  169. }
  170. template<typename TContext>
  171. static bool decode(TContext& context)
  172. {
  173. if (context.state >= TContext::State::Decoded)
  174. return true;
  175. auto error_guard = ArmedScopeGuard([&] {
  176. context.state = TContext::State::Error;
  177. });
  178. Streamer streamer(context.data, context.data_size);
  179. if (!read_magic_number(context, streamer))
  180. return false;
  181. if (read_whitespace(context, streamer).is_error())
  182. return false;
  183. if (!read_width(context, streamer))
  184. return false;
  185. if (read_whitespace(context, streamer).is_error())
  186. return false;
  187. if (!read_height(context, streamer))
  188. return false;
  189. if (context.width > maximum_width_for_decoded_images || context.height > maximum_height_for_decoded_images) {
  190. dbgln("This portable network image is too large for comfort: {}x{}", context.width, context.height);
  191. return false;
  192. }
  193. if (read_whitespace(context, streamer).is_error())
  194. return false;
  195. if constexpr (requires { context.format_details.max_val; }) {
  196. if (!read_max_val(context, streamer))
  197. return false;
  198. if (read_whitespace(context, streamer).is_error())
  199. return false;
  200. }
  201. if (!read_image_data(context, streamer))
  202. return false;
  203. error_guard.disarm();
  204. context.state = TContext::State::Decoded;
  205. return true;
  206. }
  207. template<typename TContext>
  208. static RefPtr<Gfx::Bitmap> load_impl(u8 const* data, size_t data_size)
  209. {
  210. TContext context {};
  211. context.data = data;
  212. context.data_size = data_size;
  213. if (!decode(context)) {
  214. return nullptr;
  215. }
  216. return context.bitmap;
  217. }
  218. template<typename TContext>
  219. static RefPtr<Gfx::Bitmap> load_from_memory(u8 const* data, size_t length, DeprecatedString const& mmap_name)
  220. {
  221. auto bitmap = load_impl<TContext>(data, length);
  222. if (bitmap)
  223. bitmap->set_mmap_name(DeprecatedString::formatted("Gfx::Bitmap [{}] - Decoded {}: {}", bitmap->size(), TContext::FormatDetails::image_type, mmap_name));
  224. return bitmap;
  225. }
  226. }