ImageFormat.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibGfx/Vector4.h>
  9. namespace SoftGPU {
  10. enum class ImageFormat {
  11. RGB565,
  12. RGB888,
  13. BGR888,
  14. RGBA8888,
  15. BGRA8888,
  16. L8,
  17. L8A8,
  18. };
  19. static constexpr size_t element_size(ImageFormat format)
  20. {
  21. switch (format) {
  22. case ImageFormat::L8:
  23. return 1;
  24. case ImageFormat::RGB565:
  25. case ImageFormat::L8A8:
  26. return 2;
  27. case ImageFormat::RGB888:
  28. case ImageFormat::BGR888:
  29. return 3;
  30. case ImageFormat::RGBA8888:
  31. case ImageFormat::BGRA8888:
  32. return 4;
  33. default:
  34. VERIFY_NOT_REACHED();
  35. }
  36. }
  37. inline static FloatVector4 unpack_color(void const* ptr, ImageFormat format)
  38. {
  39. constexpr auto one_over_255 = 1.0f / 255;
  40. switch (format) {
  41. case ImageFormat::RGB888: {
  42. auto rgb = reinterpret_cast<u8 const*>(ptr);
  43. return {
  44. rgb[0] * one_over_255,
  45. rgb[1] * one_over_255,
  46. rgb[2] * one_over_255,
  47. 1.0f,
  48. };
  49. }
  50. case ImageFormat::BGR888: {
  51. auto bgr = reinterpret_cast<u8 const*>(ptr);
  52. return {
  53. bgr[2] * one_over_255,
  54. bgr[1] * one_over_255,
  55. bgr[0] * one_over_255,
  56. 1.0f,
  57. };
  58. }
  59. case ImageFormat::RGBA8888: {
  60. auto rgba = *reinterpret_cast<u32 const*>(ptr);
  61. return {
  62. (rgba & 0xff) * one_over_255,
  63. ((rgba >> 8) & 0xff) * one_over_255,
  64. ((rgba >> 16) & 0xff) * one_over_255,
  65. ((rgba >> 24) & 0xff) * one_over_255,
  66. };
  67. }
  68. case ImageFormat::BGRA8888: {
  69. auto bgra = *reinterpret_cast<u32 const*>(ptr);
  70. return {
  71. ((bgra >> 16) & 0xff) * one_over_255,
  72. ((bgra >> 8) & 0xff) * one_over_255,
  73. (bgra & 0xff) * one_over_255,
  74. ((bgra >> 24) & 0xff) * one_over_255,
  75. };
  76. }
  77. case ImageFormat::RGB565: {
  78. auto rgb = *reinterpret_cast<u16 const*>(ptr);
  79. return {
  80. ((rgb >> 11) & 0x1f) / 31.f,
  81. ((rgb >> 5) & 0x3f) / 63.f,
  82. (rgb & 0x1f) / 31.f,
  83. 1.0f
  84. };
  85. }
  86. case ImageFormat::L8: {
  87. auto luminance = *reinterpret_cast<u8 const*>(ptr);
  88. auto clamped_luminance = luminance * one_over_255;
  89. return {
  90. clamped_luminance,
  91. clamped_luminance,
  92. clamped_luminance,
  93. 1.0f,
  94. };
  95. }
  96. case ImageFormat::L8A8: {
  97. auto luminance_and_alpha = reinterpret_cast<u8 const*>(ptr);
  98. auto clamped_luminance = luminance_and_alpha[0] * one_over_255;
  99. return {
  100. clamped_luminance,
  101. clamped_luminance,
  102. clamped_luminance,
  103. luminance_and_alpha[1] * one_over_255,
  104. };
  105. }
  106. default:
  107. VERIFY_NOT_REACHED();
  108. }
  109. }
  110. inline static void pack_color(FloatVector4 const& color, void* ptr, ImageFormat format)
  111. {
  112. auto r = static_cast<u8>(clamp(color.x(), 0.0f, 1.0f) * 255);
  113. auto g = static_cast<u8>(clamp(color.y(), 0.0f, 1.0f) * 255);
  114. auto b = static_cast<u8>(clamp(color.z(), 0.0f, 1.0f) * 255);
  115. auto a = static_cast<u8>(clamp(color.w(), 0.0f, 1.0f) * 255);
  116. switch (format) {
  117. case ImageFormat::RGB888:
  118. reinterpret_cast<u8*>(ptr)[0] = r;
  119. reinterpret_cast<u8*>(ptr)[1] = g;
  120. reinterpret_cast<u8*>(ptr)[2] = b;
  121. return;
  122. case ImageFormat::BGR888:
  123. reinterpret_cast<u8*>(ptr)[2] = b;
  124. reinterpret_cast<u8*>(ptr)[1] = g;
  125. reinterpret_cast<u8*>(ptr)[0] = r;
  126. return;
  127. case ImageFormat::RGBA8888:
  128. *reinterpret_cast<u32*>(ptr) = r | (g << 8) | (b << 16) | (a << 24);
  129. return;
  130. case ImageFormat::BGRA8888:
  131. *reinterpret_cast<u32*>(ptr) = b | (g << 8) | (r << 16) | (a << 24);
  132. return;
  133. case ImageFormat::RGB565:
  134. *reinterpret_cast<u16*>(ptr) = (r & 0x1f) | ((g & 0x3f) << 5) | ((b & 0x1f) << 11);
  135. return;
  136. case ImageFormat::L8:
  137. *reinterpret_cast<u8*>(ptr) = r;
  138. return;
  139. case ImageFormat::L8A8:
  140. reinterpret_cast<u8*>(ptr)[0] = r;
  141. reinterpret_cast<u8*>(ptr)[1] = a;
  142. return;
  143. default:
  144. VERIFY_NOT_REACHED();
  145. }
  146. }
  147. }