QOIWriter.cpp 6.4 KB

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