PortableImageLoaderCommon.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 bool read_whitespace(TContext& context, Streamer& streamer)
  93. {
  94. bool exist = false;
  95. u8 byte {};
  96. while (streamer.read(byte)) {
  97. if (byte == ' ' || byte == '\t' || byte == '\n' || byte == '\r') {
  98. exist = true;
  99. } else if (byte == '#') {
  100. streamer.step_back();
  101. if (read_comment(context, streamer).is_error())
  102. return false;
  103. } else {
  104. streamer.step_back();
  105. return exist;
  106. }
  107. }
  108. return exist;
  109. }
  110. template<typename TContext>
  111. static bool read_width(TContext& context, Streamer& streamer)
  112. {
  113. auto number_or_error = read_number(streamer);
  114. if (number_or_error.is_error())
  115. return false;
  116. context.width = number_or_error.value();
  117. context.state = TContext::State::Width;
  118. return true;
  119. }
  120. template<typename TContext>
  121. static bool read_height(TContext& context, Streamer& streamer)
  122. {
  123. auto number_or_error = read_number(streamer);
  124. if (number_or_error.is_error())
  125. return false;
  126. context.height = number_or_error.value();
  127. context.state = TContext::State::Height;
  128. return true;
  129. }
  130. template<typename TContext>
  131. static bool read_max_val(TContext& context, Streamer& streamer)
  132. {
  133. auto number_or_error = read_number(streamer);
  134. if (number_or_error.is_error())
  135. return false;
  136. context.format_details.max_val = number_or_error.value();
  137. if (context.format_details.max_val > 255) {
  138. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "We can't parse 2 byte color for {}", TContext::FormatDetails::image_type);
  139. context.state = TContext::State::Error;
  140. return false;
  141. }
  142. context.state = TContext::State::Maxval;
  143. return true;
  144. }
  145. template<typename TContext>
  146. static bool create_bitmap(TContext& context)
  147. {
  148. auto bitmap_or_error = Bitmap::create(BitmapFormat::BGRx8888, { context.width, context.height });
  149. if (bitmap_or_error.is_error()) {
  150. context.state = TContext::State::Error;
  151. return false;
  152. }
  153. context.bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  154. return true;
  155. }
  156. template<typename TContext>
  157. static void set_pixels(TContext& context, Vector<Gfx::Color> const& color_data)
  158. {
  159. size_t index = 0;
  160. for (size_t y = 0; y < context.height; ++y) {
  161. for (size_t x = 0; x < context.width; ++x) {
  162. context.bitmap->set_pixel(x, y, color_data.at(index));
  163. index++;
  164. }
  165. }
  166. }
  167. template<typename TContext>
  168. static bool decode(TContext& context)
  169. {
  170. if (context.state >= TContext::State::Decoded)
  171. return true;
  172. auto error_guard = ArmedScopeGuard([&] {
  173. context.state = TContext::State::Error;
  174. });
  175. Streamer streamer(context.data, context.data_size);
  176. if (!read_magic_number(context, streamer))
  177. return false;
  178. if (!read_whitespace(context, streamer))
  179. return false;
  180. if (!read_width(context, streamer))
  181. return false;
  182. if (!read_whitespace(context, streamer))
  183. return false;
  184. if (!read_height(context, streamer))
  185. return false;
  186. if (context.width > maximum_width_for_decoded_images || context.height > maximum_height_for_decoded_images) {
  187. dbgln("This portable network image is too large for comfort: {}x{}", context.width, context.height);
  188. return false;
  189. }
  190. if (!read_whitespace(context, streamer))
  191. return false;
  192. if constexpr (requires { context.format_details.max_val; }) {
  193. if (!read_max_val(context, streamer))
  194. return false;
  195. if (!read_whitespace(context, streamer))
  196. return false;
  197. }
  198. if (!read_image_data(context, streamer))
  199. return false;
  200. error_guard.disarm();
  201. context.state = TContext::State::Decoded;
  202. return true;
  203. }
  204. template<typename TContext>
  205. static RefPtr<Gfx::Bitmap> load_impl(u8 const* data, size_t data_size)
  206. {
  207. TContext context {};
  208. context.data = data;
  209. context.data_size = data_size;
  210. if (!decode(context)) {
  211. return nullptr;
  212. }
  213. return context.bitmap;
  214. }
  215. template<typename TContext>
  216. static RefPtr<Gfx::Bitmap> load_from_memory(u8 const* data, size_t length, DeprecatedString const& mmap_name)
  217. {
  218. auto bitmap = load_impl<TContext>(data, length);
  219. if (bitmap)
  220. bitmap->set_mmap_name(DeprecatedString::formatted("Gfx::Bitmap [{}] - Decoded {}: {}", bitmap->size(), TContext::FormatDetails::image_type, mmap_name));
  221. return bitmap;
  222. }
  223. }