QOILoader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Endian.h>
  7. #include <AK/MemoryStream.h>
  8. #include <LibGfx/Bitmap.h>
  9. #include <LibGfx/QOILoader.h>
  10. namespace Gfx {
  11. static constexpr auto QOI_MAGIC = "qoif"sv;
  12. static constexpr u8 QOI_OP_RGB = 0b11111110;
  13. static constexpr u8 QOI_OP_RGBA = 0b11111111;
  14. static constexpr u8 QOI_OP_INDEX = 0b00000000;
  15. static constexpr u8 QOI_OP_DIFF = 0b01000000;
  16. static constexpr u8 QOI_OP_LUMA = 0b10000000;
  17. static constexpr u8 QOI_OP_RUN = 0b11000000;
  18. static constexpr u8 QOI_MASK_2 = 0b11000000;
  19. static constexpr u8 END_MARKER[] = { 0, 0, 0, 0, 0, 0, 0, 1 };
  20. static ErrorOr<QOIHeader> decode_qoi_header(InputMemoryStream& stream)
  21. {
  22. QOIHeader header;
  23. stream >> Bytes { &header, sizeof(header) };
  24. if (stream.handle_any_error())
  25. return Error::from_string_literal("QOIImageDecoderPlugin: Invalid QOI image: end of stream while reading header"sv);
  26. if (StringView { header.magic, array_size(header.magic) } != QOI_MAGIC)
  27. return Error::from_string_literal("QOIImageDecoderPlugin: Invalid QOI image: incorrect header magic"sv);
  28. header.width = AK::convert_between_host_and_big_endian(header.width);
  29. header.height = AK::convert_between_host_and_big_endian(header.height);
  30. return header;
  31. }
  32. static ErrorOr<Color> decode_qoi_op_rgb(InputMemoryStream& stream)
  33. {
  34. u8 bytes[4];
  35. stream >> Bytes { &bytes, array_size(bytes) };
  36. if (stream.handle_any_error())
  37. return Error::from_string_literal("Invalid QOI image: end of stream while reading QOI_OP_RGB chunk"sv);
  38. VERIFY(bytes[0] == QOI_OP_RGB);
  39. return Color { bytes[1], bytes[2], bytes[3] };
  40. }
  41. static ErrorOr<Color> decode_qoi_op_rgba(InputMemoryStream& stream)
  42. {
  43. u8 bytes[5];
  44. stream >> Bytes { &bytes, array_size(bytes) };
  45. if (stream.handle_any_error())
  46. return Error::from_string_literal("Invalid QOI image: end of stream while reading QOI_OP_RGBA chunk"sv);
  47. VERIFY(bytes[0] == QOI_OP_RGBA);
  48. return Color { bytes[1], bytes[2], bytes[3], bytes[4] };
  49. }
  50. static ErrorOr<u8> decode_qoi_op_index(InputMemoryStream& stream)
  51. {
  52. u8 byte;
  53. stream >> byte;
  54. if (stream.handle_any_error())
  55. return Error::from_string_literal("Invalid QOI image: end of stream while reading QOI_OP_INDEX chunk"sv);
  56. VERIFY((byte & QOI_MASK_2) == QOI_OP_INDEX);
  57. u8 index = byte & ~QOI_MASK_2;
  58. VERIFY(index <= 63);
  59. return index;
  60. }
  61. static ErrorOr<Color> decode_qoi_op_diff(InputMemoryStream& stream, Color pixel)
  62. {
  63. u8 byte;
  64. stream >> byte;
  65. if (stream.handle_any_error())
  66. return Error::from_string_literal("Invalid QOI image: end of stream while reading QOI_OP_DIFF chunk"sv);
  67. VERIFY((byte & QOI_MASK_2) == QOI_OP_DIFF);
  68. u8 dr = (byte & 0b00110000) >> 4;
  69. u8 dg = (byte & 0b00001100) >> 2;
  70. u8 db = (byte & 0b00000011);
  71. VERIFY(dr <= 3 && dg <= 3 && db <= 3);
  72. // Values are stored as unsigned integers with a bias of 2.
  73. return Color {
  74. static_cast<u8>(pixel.red() + static_cast<i8>(dr - 2)),
  75. static_cast<u8>(pixel.green() + static_cast<i8>(dg - 2)),
  76. static_cast<u8>(pixel.blue() + static_cast<i8>(db - 2)),
  77. pixel.alpha(),
  78. };
  79. }
  80. static ErrorOr<Color> decode_qoi_op_luma(InputMemoryStream& stream, Color pixel)
  81. {
  82. u8 bytes[2];
  83. stream >> Bytes { &bytes, array_size(bytes) };
  84. if (stream.handle_any_error())
  85. return Error::from_string_literal("Invalid QOI image: end of stream while reading QOI_OP_LUMA chunk"sv);
  86. VERIFY((bytes[0] & QOI_MASK_2) == QOI_OP_LUMA);
  87. u8 diff_green = (bytes[0] & ~QOI_MASK_2);
  88. u8 dr_dg = (bytes[1] & 0b11110000) >> 4;
  89. u8 db_dg = (bytes[1] & 0b00001111);
  90. // Values are stored as unsigned integers with a bias of 32 for the green channel and a bias of 8 for the red and blue channel.
  91. return Color {
  92. static_cast<u8>(pixel.red() + static_cast<i8>((diff_green - 32) + (dr_dg - 8))),
  93. static_cast<u8>(pixel.green() + static_cast<i8>(diff_green - 32)),
  94. static_cast<u8>(pixel.blue() + static_cast<i8>((diff_green - 32) + (db_dg - 8))),
  95. pixel.alpha(),
  96. };
  97. }
  98. static ErrorOr<u8> decode_qoi_op_run(InputMemoryStream& stream)
  99. {
  100. u8 byte;
  101. stream >> byte;
  102. if (stream.handle_any_error())
  103. return Error::from_string_literal("QOIImageDecoderPlugin: Invalid QOI image: end of stream while reading QOI_OP_RUN chunk"sv);
  104. VERIFY((byte & QOI_MASK_2) == QOI_OP_RUN);
  105. u8 run = byte & ~QOI_MASK_2;
  106. // The run-length is stored with a bias of -1.
  107. run += 1;
  108. // Note that the run-lengths 63 and 64 (b111110 and b111111) are illegal as they are occupied by the QOI_OP_RGB and QOI_OP_RGBA tags.
  109. if (run == QOI_OP_RGB || run == QOI_OP_RGBA)
  110. return Error::from_string_literal("QOIImageDecoderPlugin: Invalid QOI image: illegal run length"sv);
  111. VERIFY(run >= 1 && run <= 62);
  112. return run;
  113. }
  114. static ErrorOr<void> decode_qoi_end_marker(InputMemoryStream& stream)
  115. {
  116. u8 bytes[array_size(END_MARKER)];
  117. stream >> Bytes { &bytes, array_size(bytes) };
  118. if (stream.handle_any_error())
  119. return Error::from_string_literal("QOIImageDecoderPlugin: Invalid QOI image: end of stream while reading end marker"sv);
  120. if (!stream.eof())
  121. return Error::from_string_literal("QOIImageDecoderPlugin: Invalid QOI image: expected end of stream but more bytes are available"sv);
  122. if (memcmp(&END_MARKER, &bytes, array_size(bytes)) != 0)
  123. return Error::from_string_literal("QOIImageDecoderPlugin: Invalid QOI image: incorrect end marker"sv);
  124. return {};
  125. }
  126. static ErrorOr<NonnullRefPtr<Bitmap>> decode_qoi_image(InputMemoryStream& stream, u32 width, u32 height)
  127. {
  128. // FIXME: Why is Gfx::Bitmap's size signed? Makes no sense whatsoever.
  129. if (width > NumericLimits<int>::max())
  130. return Error::from_string_literal("QOIImageDecoderPlugin: Cannot create bitmap for QOI image of valid size, width exceeds maximum Gfx::Bitmap width"sv);
  131. if (height > NumericLimits<int>::max())
  132. return Error::from_string_literal("QOIImageDecoderPlugin: Cannot create bitmap for QOI image of valid size, height exceeds maximum Gfx::Bitmap height"sv);
  133. auto bitmap = TRY(Bitmap::try_create(BitmapFormat::BGRA8888, { width, height }));
  134. u8 run = 0;
  135. Color pixel = { 0, 0, 0, 255 };
  136. Color previous_pixels[64] {};
  137. for (u32 y = 0; y < height; ++y) {
  138. for (u32 x = 0; x < width; ++x) {
  139. if (run > 0)
  140. --run;
  141. if (run == 0) {
  142. u8 tag = stream.peek_or_error();
  143. if (stream.handle_any_error())
  144. return Error::from_string_literal("QOIImageDecoderPlugin: Invalid QOI image: end of stream while reading chunk tag"sv);
  145. if (tag == QOI_OP_RGB)
  146. pixel = TRY(decode_qoi_op_rgb(stream));
  147. else if (tag == QOI_OP_RGBA)
  148. pixel = TRY(decode_qoi_op_rgba(stream));
  149. else if ((tag & QOI_MASK_2) == QOI_OP_INDEX)
  150. pixel = previous_pixels[TRY(decode_qoi_op_index(stream))];
  151. else if ((tag & QOI_MASK_2) == QOI_OP_DIFF)
  152. pixel = TRY(decode_qoi_op_diff(stream, pixel));
  153. else if ((tag & QOI_MASK_2) == QOI_OP_LUMA)
  154. pixel = TRY(decode_qoi_op_luma(stream, pixel));
  155. else if ((tag & QOI_MASK_2) == QOI_OP_RUN)
  156. run = TRY(decode_qoi_op_run(stream));
  157. else
  158. return Error::from_string_literal("QOIImageDecoderPlugin: Invalid QOI image: unknown chunk tag"sv);
  159. }
  160. auto index_position = (pixel.red() * 3 + pixel.green() * 5 + pixel.blue() * 7 + pixel.alpha() * 11) % 64;
  161. previous_pixels[index_position] = pixel;
  162. bitmap->set_pixel(x, y, pixel);
  163. }
  164. }
  165. TRY(decode_qoi_end_marker(stream));
  166. return { move(bitmap) };
  167. }
  168. QOIImageDecoderPlugin::QOIImageDecoderPlugin(u8 const* data, size_t size)
  169. {
  170. m_context = make<QOILoadingContext>();
  171. m_context->data = data;
  172. m_context->data_size = size;
  173. }
  174. IntSize QOIImageDecoderPlugin::size()
  175. {
  176. if (m_context->state < QOILoadingContext::State::HeaderDecoded) {
  177. InputMemoryStream stream { { m_context->data, m_context->data_size } };
  178. // FIXME: This is a weird API (inherited from ImageDecoderPlugin), should probably propagate errors by returning ErrorOr<IntSize>.
  179. // For the time being, ignore the result and rely on the context's state.
  180. (void)decode_header_and_update_context(stream);
  181. }
  182. if (m_context->state == QOILoadingContext::State::Error)
  183. return {};
  184. return { m_context->header.width, m_context->header.height };
  185. }
  186. void QOIImageDecoderPlugin::set_volatile()
  187. {
  188. if (m_context->bitmap)
  189. m_context->bitmap->set_volatile();
  190. }
  191. bool QOIImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  192. {
  193. if (!m_context->bitmap)
  194. return false;
  195. return m_context->bitmap->set_nonvolatile(was_purged);
  196. }
  197. bool QOIImageDecoderPlugin::sniff()
  198. {
  199. InputMemoryStream stream { { m_context->data, m_context->data_size } };
  200. return !decode_qoi_header(stream).is_error();
  201. }
  202. ErrorOr<ImageFrameDescriptor> QOIImageDecoderPlugin::frame(size_t index)
  203. {
  204. if (index > 0)
  205. return Error::from_string_literal("QOIImageDecoderPlugin: Invalid frame index"sv);
  206. if (m_context->state == QOILoadingContext::State::NotDecoded) {
  207. InputMemoryStream stream { { m_context->data, m_context->data_size } };
  208. TRY(decode_header_and_update_context(stream));
  209. TRY(decode_image_and_update_context(stream));
  210. } else if (m_context->state == QOILoadingContext::State::HeaderDecoded) {
  211. InputMemoryStream stream { { m_context->data, m_context->data_size } };
  212. VERIFY(stream.discard_or_error(sizeof(m_context->header)));
  213. TRY(decode_image_and_update_context(stream));
  214. }
  215. if (m_context->state == QOILoadingContext::State::ImageDecoded) {
  216. VERIFY(m_context->bitmap);
  217. return ImageFrameDescriptor { m_context->bitmap, 0 };
  218. }
  219. VERIFY(m_context->state == QOILoadingContext::State::Error);
  220. VERIFY(m_context->error.has_value());
  221. return *m_context->error;
  222. }
  223. ErrorOr<void> QOIImageDecoderPlugin::decode_header_and_update_context(InputMemoryStream& stream)
  224. {
  225. VERIFY(m_context->state < QOILoadingContext::State::HeaderDecoded);
  226. auto error_or_header = decode_qoi_header(stream);
  227. if (error_or_header.is_error()) {
  228. m_context->state = QOILoadingContext::State::Error;
  229. m_context->error = error_or_header.release_error();
  230. return *m_context->error;
  231. }
  232. m_context->state = QOILoadingContext::State::HeaderDecoded;
  233. m_context->header = error_or_header.release_value();
  234. return {};
  235. }
  236. ErrorOr<void> QOIImageDecoderPlugin::decode_image_and_update_context(InputMemoryStream& stream)
  237. {
  238. VERIFY(m_context->state < QOILoadingContext::State::ImageDecoded);
  239. auto error_or_bitmap = decode_qoi_image(stream, m_context->header.width, m_context->header.height);
  240. if (error_or_bitmap.is_error()) {
  241. m_context->state = QOILoadingContext::State::Error;
  242. m_context->error = error_or_bitmap.release_error();
  243. return *m_context->error;
  244. }
  245. m_context->state = QOILoadingContext::State::ImageDecoded;
  246. m_context->bitmap = error_or_bitmap.release_value();
  247. return {};
  248. }
  249. }