ImageFormat.h 3.3 KB

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