PortableImageLoaderCommon.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * Copyright (c) 2020, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Array.h>
  9. #include <AK/Debug.h>
  10. #include <AK/Endian.h>
  11. #include <AK/LexicalPath.h>
  12. #include <AK/MappedFile.h>
  13. #include <AK/ScopeGuard.h>
  14. #include <AK/String.h>
  15. #include <AK/StringBuilder.h>
  16. #include <AK/Types.h>
  17. #include <AK/Vector.h>
  18. #include <LibGfx/Bitmap.h>
  19. #include <LibGfx/Color.h>
  20. #include <LibGfx/ImageDecoder.h>
  21. #include <LibGfx/Streamer.h>
  22. namespace Gfx {
  23. static constexpr Color adjust_color(u16 max_val, Color color)
  24. {
  25. color.set_red((color.red() * 255) / max_val);
  26. color.set_green((color.green() * 255) / max_val);
  27. color.set_blue((color.blue() * 255) / max_val);
  28. return color;
  29. }
  30. template<typename TValue>
  31. static bool read_number(Streamer& streamer, TValue* value)
  32. {
  33. u8 byte {};
  34. StringBuilder sb {};
  35. while (streamer.read(byte)) {
  36. if (byte == ' ' || byte == '\t' || byte == '\n' || byte == '\r') {
  37. streamer.step_back();
  38. break;
  39. }
  40. sb.append(byte);
  41. }
  42. const auto opt_value = sb.to_string().to_uint();
  43. if (!opt_value.has_value()) {
  44. *value = 0;
  45. return false;
  46. }
  47. *value = static_cast<u16>(opt_value.value());
  48. return true;
  49. }
  50. template<typename TContext>
  51. static bool read_comment([[maybe_unused]] TContext& context, Streamer& streamer)
  52. {
  53. bool exist = false;
  54. u8 byte {};
  55. while (streamer.read(byte)) {
  56. if (byte == '#') {
  57. exist = true;
  58. } else if (byte == '\t' || byte == '\n') {
  59. return exist;
  60. }
  61. }
  62. return exist;
  63. }
  64. template<typename TContext>
  65. static bool read_magic_number(TContext& context, Streamer& streamer)
  66. {
  67. if (context.state >= TContext::State::MagicNumber) {
  68. return true;
  69. }
  70. if (!context.data || context.data_size < 2) {
  71. context.state = TContext::State::Error;
  72. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "There is no enough data for {}", TContext::image_type);
  73. return false;
  74. }
  75. u8 magic_number[2] {};
  76. if (!streamer.read_bytes(magic_number, 2)) {
  77. context.state = TContext::State::Error;
  78. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "We can't read magic number for {}", TContext::image_type);
  79. return false;
  80. }
  81. if (magic_number[0] == 'P' && magic_number[1] == TContext::ascii_magic_number) {
  82. context.type = TContext::Type::ASCII;
  83. context.state = TContext::State::MagicNumber;
  84. return true;
  85. }
  86. if (magic_number[0] == 'P' && magic_number[1] == TContext::binary_magic_number) {
  87. context.type = TContext::Type::RAWBITS;
  88. context.state = TContext::State::MagicNumber;
  89. return true;
  90. }
  91. context.state = TContext::State::Error;
  92. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "Magic number is not valid for {}{}{}", magic_number[0], magic_number[1], TContext::image_type);
  93. return false;
  94. }
  95. template<typename TContext>
  96. static bool read_whitespace(TContext& context, Streamer& streamer)
  97. {
  98. bool exist = false;
  99. u8 byte {};
  100. while (streamer.read(byte)) {
  101. if (byte == ' ' || byte == '\t' || byte == '\n' || byte == '\r') {
  102. exist = true;
  103. } else if (byte == '#') {
  104. streamer.step_back();
  105. read_comment(context, streamer);
  106. } else {
  107. streamer.step_back();
  108. return exist;
  109. }
  110. }
  111. return exist;
  112. }
  113. template<typename TContext>
  114. static bool read_width(TContext& context, Streamer& streamer)
  115. {
  116. if (const bool result = read_number(streamer, &context.width);
  117. !result || context.width == 0) {
  118. return false;
  119. }
  120. context.state = TContext::Width;
  121. return true;
  122. }
  123. template<typename TContext>
  124. static bool read_height(TContext& context, Streamer& streamer)
  125. {
  126. if (const bool result = read_number(streamer, &context.height);
  127. !result || context.height == 0) {
  128. return false;
  129. }
  130. context.state = TContext::Height;
  131. return true;
  132. }
  133. template<typename TContext>
  134. static bool read_max_val(TContext& context, Streamer& streamer)
  135. {
  136. if (const bool result = read_number(streamer, &context.max_val);
  137. !result || context.max_val == 0) {
  138. return false;
  139. }
  140. if (context.max_val > 255) {
  141. dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "We can't parse 2 byte color for {}", TContext::image_type);
  142. context.state = TContext::Error;
  143. return false;
  144. }
  145. context.state = TContext::Maxval;
  146. return true;
  147. }
  148. template<typename TContext>
  149. static bool create_bitmap(TContext& context)
  150. {
  151. context.bitmap = Bitmap::create_purgeable(BitmapFormat::BGRx8888, { context.width, context.height });
  152. if (!context.bitmap) {
  153. context.state = TContext::State::Error;
  154. return false;
  155. }
  156. return true;
  157. }
  158. template<typename TContext>
  159. static void set_pixels(TContext& context, const Vector<Gfx::Color>& color_data)
  160. {
  161. size_t index = 0;
  162. for (size_t y = 0; y < context.height; ++y) {
  163. for (size_t x = 0; x < context.width; ++x) {
  164. context.bitmap->set_pixel(x, y, color_data.at(index));
  165. index++;
  166. }
  167. }
  168. }
  169. template<typename TContext>
  170. static bool decode(TContext& context)
  171. {
  172. if (context.state >= TContext::State::Decoded)
  173. return true;
  174. auto error_guard = ArmedScopeGuard([&] {
  175. context.state = TContext::State::Error;
  176. });
  177. Streamer streamer(context.data, context.data_size);
  178. if (!read_magic_number(context, streamer))
  179. return false;
  180. if (!read_whitespace(context, streamer))
  181. return false;
  182. if (!read_width(context, streamer))
  183. return false;
  184. if (!read_whitespace(context, streamer))
  185. return false;
  186. if (!read_height(context, streamer))
  187. return false;
  188. if (context.width > maximum_width_for_decoded_images || context.height > maximum_height_for_decoded_images) {
  189. dbgln("This portable network image is too large for comfort: {}x{}", context.width, context.height);
  190. return false;
  191. }
  192. if (!read_whitespace(context, streamer))
  193. return false;
  194. if constexpr (requires { context.max_val; }) {
  195. if (!read_max_val(context, streamer))
  196. return false;
  197. if (!read_whitespace(context, streamer))
  198. return false;
  199. }
  200. if (!read_image_data(context, streamer))
  201. return false;
  202. error_guard.disarm();
  203. context.state = TContext::State::Decoded;
  204. return true;
  205. }
  206. template<typename TContext>
  207. static RefPtr<Gfx::Bitmap> load_impl(const u8* data, size_t data_size)
  208. {
  209. TContext context {};
  210. context.data = data;
  211. context.data_size = data_size;
  212. if (!decode(context)) {
  213. return nullptr;
  214. }
  215. return context.bitmap;
  216. }
  217. template<typename TContext>
  218. static RefPtr<Gfx::Bitmap> load(const StringView& path)
  219. {
  220. auto file_or_error = MappedFile::map(path);
  221. if (file_or_error.is_error())
  222. return nullptr;
  223. auto bitmap = load_impl<TContext>((const u8*)file_or_error.value()->data(), file_or_error.value()->size());
  224. if (bitmap)
  225. bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded {}: {}",
  226. bitmap->size(),
  227. TContext::image_type,
  228. LexicalPath::canonicalized_path(path)));
  229. return bitmap;
  230. }
  231. }