QOIWriter.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2022, Olivier De Cannière <olivier.decanniere96@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "QOIWriter.h"
  7. #include <AK/Endian.h>
  8. namespace Gfx {
  9. static constexpr Array<u8, 4> qoi_magic_bytes = { 'q', 'o', 'i', 'f' };
  10. static constexpr Array<u8, 8> qoi_end_marker = { 0, 0, 0, 0, 0, 0, 0, 1 };
  11. enum class Colorspace {
  12. sRGB,
  13. Linear,
  14. };
  15. enum class Channels {
  16. RGB,
  17. RGBA,
  18. };
  19. ErrorOr<ByteBuffer> QOIWriter::encode(Bitmap const& bitmap)
  20. {
  21. QOIWriter writer;
  22. TRY(writer.add_header(bitmap.width(), bitmap.height(), Channels::RGBA, Colorspace::sRGB));
  23. Color previous_pixel = { 0, 0, 0, 255 };
  24. bool creating_run = false;
  25. int run_length = 0;
  26. for (auto y = 0; y < bitmap.height(); y++) {
  27. for (auto x = 0; x < bitmap.width(); x++) {
  28. auto pixel = bitmap.get_pixel(x, y);
  29. // Check for at most 62 consecutive identical pixels.
  30. if (pixel == previous_pixel) {
  31. if (!creating_run) {
  32. creating_run = true;
  33. run_length = 0;
  34. writer.insert_into_running_array(pixel);
  35. }
  36. run_length++;
  37. // If the run reaches a maximum length of 62 or if this is the last pixel then create the chunk.
  38. if (run_length == 62 || (y == bitmap.height() - 1 && x == bitmap.width() - 1)) {
  39. TRY(writer.add_run_chunk(run_length));
  40. creating_run = false;
  41. }
  42. continue;
  43. }
  44. // Run ended with the previous pixel. Create a chunk for it and continue processing this pixel.
  45. if (creating_run) {
  46. TRY(writer.add_run_chunk(run_length));
  47. creating_run = false;
  48. }
  49. // Check if the pixel matches a pixel in the running array.
  50. auto index = pixel_hash_function(pixel);
  51. auto& array_pixel = writer.running_array[index];
  52. if (array_pixel == pixel) {
  53. TRY(writer.add_index_chunk(index));
  54. previous_pixel = pixel;
  55. continue;
  56. }
  57. writer.running_array[index] = pixel;
  58. // Check if pixel can be expressed as a difference of the previous pixel.
  59. if (pixel.alpha() == previous_pixel.alpha()) {
  60. int red_difference = pixel.red() - previous_pixel.red();
  61. int green_difference = pixel.green() - previous_pixel.green();
  62. int blue_difference = pixel.blue() - previous_pixel.blue();
  63. int relative_red_difference = red_difference - green_difference;
  64. int relative_blue_difference = blue_difference - green_difference;
  65. if (red_difference > -3 && red_difference < 2
  66. && green_difference > -3 && green_difference < 2
  67. && blue_difference > -3 && blue_difference < 2) {
  68. TRY(writer.add_diff_chunk(red_difference, green_difference, blue_difference));
  69. previous_pixel = pixel;
  70. continue;
  71. }
  72. if (relative_red_difference > -9 && relative_red_difference < 8
  73. && green_difference > -33 && green_difference < 32
  74. && relative_blue_difference > -9 && relative_blue_difference < 8) {
  75. TRY(writer.add_luma_chunk(relative_red_difference, green_difference, relative_blue_difference));
  76. previous_pixel = pixel;
  77. continue;
  78. }
  79. TRY(writer.add_rgb_chunk(pixel.red(), pixel.green(), pixel.blue()));
  80. previous_pixel = pixel;
  81. continue;
  82. }
  83. previous_pixel = pixel;
  84. // Write full color values.
  85. TRY(writer.add_rgba_chunk(pixel.red(), pixel.green(), pixel.blue(), pixel.alpha()));
  86. }
  87. }
  88. TRY(writer.add_end_marker());
  89. return ByteBuffer::copy(writer.m_data);
  90. }
  91. ErrorOr<void> QOIWriter::add_header(u32 width, u32 height, Channels channels = Channels::RGBA, Colorspace color_space = Colorspace::sRGB)
  92. {
  93. // FIXME: Handle RGB and all linear channels.
  94. if (channels == Channels::RGB || color_space == Colorspace::Linear)
  95. TODO();
  96. TRY(m_data.try_append(qoi_magic_bytes.data(), sizeof(qoi_magic_bytes)));
  97. auto big_endian_width = AK::convert_between_host_and_big_endian(width);
  98. TRY(m_data.try_append(bit_cast<u8*>(&big_endian_width), sizeof(width)));
  99. auto big_endian_height = AK::convert_between_host_and_big_endian(height);
  100. TRY(m_data.try_append(bit_cast<u8*>(&big_endian_height), sizeof(height)));
  101. // Number of channels: 3 = RGB, 4 = RGBA.
  102. TRY(m_data.try_append(4));
  103. // Colorspace: 0 = sRGB, 1 = all linear channels.
  104. TRY(m_data.try_append(color_space == Colorspace::sRGB ? 0 : 1));
  105. return {};
  106. }
  107. ErrorOr<void> QOIWriter::add_rgb_chunk(u8 r, u8 g, u8 b)
  108. {
  109. constexpr static u8 rgb_tag = 0b1111'1110;
  110. TRY(m_data.try_append(rgb_tag));
  111. TRY(m_data.try_append(r));
  112. TRY(m_data.try_append(g));
  113. TRY(m_data.try_append(b));
  114. return {};
  115. }
  116. ErrorOr<void> QOIWriter::add_rgba_chunk(u8 r, u8 g, u8 b, u8 a)
  117. {
  118. constexpr static u8 rgba_tag = 0b1111'1111;
  119. TRY(m_data.try_append(rgba_tag));
  120. TRY(m_data.try_append(r));
  121. TRY(m_data.try_append(g));
  122. TRY(m_data.try_append(b));
  123. TRY(m_data.try_append(a));
  124. return {};
  125. }
  126. ErrorOr<void> QOIWriter::add_index_chunk(unsigned int index)
  127. {
  128. constexpr static u8 index_tag = 0b0000'0000;
  129. u8 chunk = index_tag | index;
  130. TRY(m_data.try_append(chunk));
  131. return {};
  132. }
  133. ErrorOr<void> QOIWriter::add_diff_chunk(i8 red_difference, i8 green_difference, i8 blue_difference)
  134. {
  135. constexpr static u8 diff_tag = 0b0100'0000;
  136. u8 bias = 2;
  137. u8 red = red_difference + bias;
  138. u8 green = green_difference + bias;
  139. u8 blue = blue_difference + bias;
  140. u8 chunk = diff_tag | (red << 4) | (green << 2) | blue;
  141. TRY(m_data.try_append(chunk));
  142. return {};
  143. }
  144. ErrorOr<void> QOIWriter::add_luma_chunk(i8 relative_red_difference, i8 green_difference, i8 relative_blue_difference)
  145. {
  146. constexpr static u8 luma_tag = 0b1000'0000;
  147. u8 green_bias = 32;
  148. u8 red_blue_bias = 8;
  149. u8 chunk1 = luma_tag | (green_difference + green_bias);
  150. u8 chunk2 = ((relative_red_difference + red_blue_bias) << 4) | (relative_blue_difference + red_blue_bias);
  151. TRY(m_data.try_append(chunk1));
  152. TRY(m_data.try_append(chunk2));
  153. return {};
  154. }
  155. ErrorOr<void> QOIWriter::add_run_chunk(unsigned run_length)
  156. {
  157. constexpr static u8 run_tag = 0b1100'0000;
  158. int bias = -1;
  159. u8 chunk = run_tag | (run_length + bias);
  160. TRY(m_data.try_append(chunk));
  161. return {};
  162. }
  163. ErrorOr<void> QOIWriter::add_end_marker()
  164. {
  165. TRY(m_data.try_append(qoi_end_marker.data(), sizeof(qoi_end_marker)));
  166. return {};
  167. }
  168. u32 QOIWriter::pixel_hash_function(Color pixel)
  169. {
  170. return (pixel.red() * 3 + pixel.green() * 5 + pixel.blue() * 7 + pixel.alpha() * 11) % 64;
  171. }
  172. void QOIWriter::insert_into_running_array(Color pixel)
  173. {
  174. auto index = pixel_hash_function(pixel);
  175. running_array[index] = pixel;
  176. }
  177. }