PortableImageLoaderCommon.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/StringBuilder.h>
  13. #include <AK/Types.h>
  14. #include <AK/Vector.h>
  15. #include <LibGfx/Bitmap.h>
  16. #include <LibGfx/Color.h>
  17. #include <LibGfx/ImageFormats/ImageDecoder.h>
  18. #include <LibGfx/Streamer.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. template<typename TValue>
  28. static bool read_number(Streamer& streamer, TValue* value)
  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 opt_value = sb.to_deprecated_string().to_uint();
  40. if (!opt_value.has_value()) {
  41. *value = 0;
  42. return false;
  43. }
  44. *value = static_cast<u16>(opt_value.value());
  45. return true;
  46. }
  47. template<typename TContext>
  48. static bool read_comment([[maybe_unused]] TContext& context, Streamer& streamer)
  49. {
  50. bool exist = false;
  51. u8 byte {};
  52. while (streamer.read(byte)) {
  53. if (byte == '#') {
  54. exist = true;
  55. } else if (byte == '\t' || byte == '\n') {
  56. return exist;
  57. }
  58. }
  59. return exist;
  60. }
  61. template<typename TContext>
  62. static bool read_magic_number(TContext& context, Streamer& streamer)
  63. {
  64. if (context.state >= TContext::State::MagicNumber) {
  65. return true;
  66. }
  67. if (!context.data || context.data_size < 2) {
  68. context.state = TContext::State::Error;
  69. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "There is no enough data for {}", TContext::FormatDetails::image_type);
  70. return false;
  71. }
  72. u8 magic_number[2] {};
  73. if (!streamer.read_bytes(magic_number, 2)) {
  74. context.state = TContext::State::Error;
  75. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "We can't read magic number for {}", TContext::FormatDetails::image_type);
  76. return false;
  77. }
  78. if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::ascii_magic_number) {
  79. context.type = TContext::Type::ASCII;
  80. context.state = TContext::State::MagicNumber;
  81. return true;
  82. }
  83. if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::binary_magic_number) {
  84. context.type = TContext::Type::RAWBITS;
  85. context.state = TContext::State::MagicNumber;
  86. return true;
  87. }
  88. context.state = TContext::State::Error;
  89. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "Magic number is not valid for {}{}{}", magic_number[0], magic_number[1], TContext::FormatDetails::image_type);
  90. return false;
  91. }
  92. template<typename TContext>
  93. static bool read_whitespace(TContext& context, Streamer& streamer)
  94. {
  95. bool exist = false;
  96. u8 byte {};
  97. while (streamer.read(byte)) {
  98. if (byte == ' ' || byte == '\t' || byte == '\n' || byte == '\r') {
  99. exist = true;
  100. } else if (byte == '#') {
  101. streamer.step_back();
  102. read_comment(context, streamer);
  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. if (bool const result = read_number(streamer, &context.width);
  114. !result || context.width == 0) {
  115. return false;
  116. }
  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. if (bool const result = read_number(streamer, &context.height);
  124. !result || context.height == 0) {
  125. return false;
  126. }
  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. if (bool const result = read_number(streamer, &context.format_details.max_val);
  134. !result || context.format_details.max_val == 0) {
  135. return false;
  136. }
  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. }