PackOrder.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2025 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_IMAGE_PACK_ORDER
  24. #define DFPSR_IMAGE_PACK_ORDER
  25. #include <cstdint>
  26. #include "Color.h"
  27. #include "../../base/endian.h"
  28. #include "../../base/noSimd.h"
  29. #include "../../api/stringAPI.h"
  30. namespace dsr {
  31. // The pack order defines where each color channel should be when uint32_t is interpreted as an array of four bytes using local endianness.
  32. // Packed into 2 bits in ImageDimensions, because one can assume that future pack orders at least have visible colors sorted by wavelength.
  33. enum class PackOrderIndex : uint32_t {
  34. RGBA, // Red Green Blue Alpha
  35. BGRA, // Blue Green Red Alpha
  36. ARGB, // Alpha Red Green Blue
  37. ABGR // Alpha Blue Green Red
  38. };
  39. inline String& string_toStreamIndented(String& target, const PackOrderIndex& index, const ReadableString& indentation) {
  40. ReadableString name;
  41. if (index == PackOrderIndex::RGBA) {
  42. name = U"RGBA";
  43. } else if (index == PackOrderIndex::BGRA) {
  44. name = U"BGRA";
  45. } else if (index == PackOrderIndex::ARGB) {
  46. name = U"ARGB";
  47. } else if (index == PackOrderIndex::ABGR) {
  48. name = U"ABGR";
  49. } else {
  50. name = U"?";
  51. }
  52. string_append(target, indentation, name);
  53. return target;
  54. }
  55. struct PackOrder {
  56. public:
  57. // Byte array indices for each channel
  58. // Indices are the locations of each color, not which color that holds each location
  59. // Example:
  60. // The indices for ARGB are (1, 2, 3, 0), because RGB are placed at byte indices 1..3 and A is placed first at byte index 0.
  61. int32_t redIndex, greenIndex, blueIndex, alphaIndex;
  62. // Pre-multipled bit offsets
  63. int32_t redOffset, greenOffset, blueOffset, alphaOffset;
  64. uint32_t redMask, greenMask, blueMask, alphaMask;
  65. private:
  66. constexpr PackOrder(int32_t redIndex, int32_t greenIndex, int32_t blueIndex, int32_t alphaIndex) :
  67. redIndex(redIndex), greenIndex(greenIndex), blueIndex(blueIndex), alphaIndex(alphaIndex),
  68. redOffset(redIndex << 3), greenOffset(greenIndex << 3), blueOffset(blueIndex << 3), alphaOffset(alphaIndex << 3),
  69. redMask(ENDIAN_POS_ADDR(ENDIAN32_BYTE_0, this->redOffset)),
  70. greenMask(ENDIAN_POS_ADDR(ENDIAN32_BYTE_0, this->greenOffset)),
  71. blueMask(ENDIAN_POS_ADDR(ENDIAN32_BYTE_0, this->blueOffset)),
  72. alphaMask(ENDIAN_POS_ADDR(ENDIAN32_BYTE_0, this->alphaOffset)) {}
  73. public:
  74. // Constructors
  75. PackOrder() :
  76. redIndex(0), greenIndex(1), blueIndex(2), alphaIndex(3),
  77. redOffset(0), greenOffset(8), blueOffset(16), alphaOffset(24),
  78. redMask(ENDIAN32_BYTE_0), greenMask(ENDIAN32_BYTE_1), blueMask(ENDIAN32_BYTE_2), alphaMask(ENDIAN32_BYTE_3) {}
  79. static PackOrder getPackOrder(PackOrderIndex index) {
  80. // Because the PackOrder constuctor is constexpr and all arguments are constant, these pack orders should be generated in compile time.
  81. if (index == PackOrderIndex::BGRA) {
  82. return PackOrder(2, 1, 0, 3); // PackOrderIndex::BGRA
  83. } else if (index == PackOrderIndex::ARGB) {
  84. return PackOrder(1, 2, 3, 0); // PackOrderIndex::ARGB
  85. } else if (index == PackOrderIndex::ABGR) {
  86. return PackOrder(3, 2, 1, 0); // PackOrderIndex::ABGR
  87. } else {
  88. return PackOrder(0, 1, 2, 3); // PackOrderIndex::RGBA
  89. }
  90. }
  91. // Pack the channels into a pixel color.
  92. uint32_t packRgba(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) const {
  93. uint32_t result;
  94. uint8_t *channels = (uint8_t*)(&result);
  95. channels[this->redIndex] = red;
  96. channels[this->greenIndex] = green;
  97. channels[this->blueIndex] = blue;
  98. channels[this->alphaIndex] = alpha;
  99. return result;
  100. }
  101. // Limit color to a 0..255 range and pack the channels into a pixel color.
  102. uint32_t saturateAndPackRgba(const ColorRgbaI32& color) {
  103. return this->packRgba(clamp(0, color.red, 255), clamp(0, color.green, 255), clamp(0, color.blue, 255), clamp(0, color.alpha, 255));
  104. }
  105. // A faster way of limiting input when you are sure that it won't overflow.
  106. uint32_t truncateAndPackRgba(const ColorRgbaI32& color) {
  107. return this->packRgba((uint8_t)color.red, (uint8_t)color.green, (uint8_t)color.blue, (uint8_t)color.alpha);
  108. }
  109. // The inverse of packRgba putting the channels back in order.
  110. ColorRgbaI32 unpackRgba(uint32_t packedColor) {
  111. uint8_t *channels = (uint8_t*)(&packedColor);
  112. return ColorRgbaI32(channels[this->redIndex], channels[this->greenIndex], channels[this->blueIndex], channels[this->alphaIndex]);
  113. }
  114. };
  115. template<typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U))> // Accepting uint32_t, U32x4, U32x8 ... U32xX
  116. inline U packOrder_getRed(U color) {
  117. return color & ENDIAN32_BYTE_0;
  118. }
  119. template<typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U))> // Accepting uint32_t, U32x4, U32x8 ... U32xX
  120. inline U packOrder_getRed(U color, const PackOrder &order) {
  121. return ENDIAN_NEG_ADDR(color & order.redMask, U(order.redOffset));
  122. }
  123. template<typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U))> // Accepting uint32_t, U32x4, U32x8 ... U32xX
  124. inline U packOrder_getGreen(U color) {
  125. return ENDIAN_NEG_ADDR_IMM(color & ENDIAN32_BYTE_1, 8);
  126. }
  127. template<typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U))> // Accepting uint32_t, U32x4, U32x8 ... U32xX
  128. inline U packOrder_getGreen(U color, const PackOrder &order) {
  129. return ENDIAN_NEG_ADDR(color & order.greenMask, U(order.greenOffset));
  130. }
  131. template<typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U))> // Accepting uint32_t, U32x4, U32x8 ... U32xX
  132. inline U packOrder_getBlue(U color) {
  133. return ENDIAN_NEG_ADDR_IMM(color & ENDIAN32_BYTE_2, 16);
  134. }
  135. template<typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U))> // Accepting uint32_t, U32x4, U32x8 ... U32xX
  136. inline U packOrder_getBlue(U color, const PackOrder &order) {
  137. return ENDIAN_NEG_ADDR(color & order.blueMask, U(order.blueOffset));
  138. }
  139. template<typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U))> // Accepting uint32_t, U32x4, U32x8 ... U32xX
  140. inline U packOrder_getAlpha(U color) {
  141. return ENDIAN_NEG_ADDR_IMM(color & ENDIAN32_BYTE_3, 24);
  142. }
  143. template<typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U))> // Accepting uint32_t, U32x4, U32x8 ... U32xX
  144. inline U packOrder_getAlpha(U color, const PackOrder &order) {
  145. return ENDIAN_NEG_ADDR(color & order.alphaMask, U(order.alphaOffset));
  146. }
  147. // Each input 32-bit element is from 0 to 255. Otherwise, the remainder will leak to other elements.
  148. template<typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U))> // Accepting uint32_t, U32x4, U32x8 ... U32xX
  149. U packOrder_packBytes(const U &s0, const U &s1, const U &s2) {
  150. return s0 | ENDIAN_POS_ADDR_IMM(s1, 8) | ENDIAN_POS_ADDR_IMM(s2, 16);
  151. }
  152. // Using a specified packing order
  153. template<typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U))> // Accepting uint32_t, U32x4, U32x8 ... U32xX
  154. U packOrder_packBytes(const U &s0, const U &s1, const U &s2, const PackOrder &order) {
  155. return ENDIAN_POS_ADDR(s0, U(order.redOffset))
  156. | ENDIAN_POS_ADDR(s1, U(order.greenOffset))
  157. | ENDIAN_POS_ADDR(s2, U(order.blueOffset));
  158. }
  159. // Each input 32-bit element is from 0 to 255. Otherwise, the remainder will leak to other elements.
  160. template<typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U))> // Accepting uint32_t, U32x4, U32x8 ... U32xX
  161. U packOrder_packBytes(const U &s0, const U &s1, const U &s2, const U &s3) {
  162. return s0 | ENDIAN_POS_ADDR_IMM(s1, 8) | ENDIAN_POS_ADDR_IMM(s2, 16) | ENDIAN_POS_ADDR_IMM(s3, 24);
  163. }
  164. // Using a specified packing order
  165. template<typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U))> // Accepting uint32_t, U32x4, U32x8 ... U32xX
  166. U packOrder_packBytes(const U &s0, const U &s1, const U &s2, const U &s3, const PackOrder &order) {
  167. return ENDIAN_POS_ADDR(s0, U(order.redOffset))
  168. | ENDIAN_POS_ADDR(s1, U(order.greenOffset))
  169. | ENDIAN_POS_ADDR(s2, U(order.blueOffset))
  170. | ENDIAN_POS_ADDR(s3, U(order.alphaOffset));
  171. }
  172. // Pack separate floats into saturated bytes
  173. // From float to uint32_t
  174. // From F32x4 to U32x4
  175. // From F32x8 to U32x8
  176. // From F32xX to U32xX
  177. template<typename U, typename F, DSR_ENABLE_IF(
  178. DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U)
  179. && DSR_CHECK_PROPERTY(DsrTrait_Any_F32, F)
  180. )>
  181. inline U packOrder_floatToSaturatedByte(const F &s0, const F &s1, const F &s2, const F &s3) {
  182. return packOrder_packBytes(
  183. truncateToU32(clampUpper(s0, F(255.1f))),
  184. truncateToU32(clampUpper(s1, F(255.1f))),
  185. truncateToU32(clampUpper(s2, F(255.1f))),
  186. truncateToU32(clampUpper(s3, F(255.1f)))
  187. );
  188. }
  189. // Using a specified pack order
  190. template<typename U, typename F, DSR_ENABLE_IF(
  191. DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U)
  192. && DSR_CHECK_PROPERTY(DsrTrait_Any_F32, F)
  193. )>
  194. inline U packOrder_floatToSaturatedByte(const F &s0, const F &s1, const F &s2, const F &s3, const PackOrder &order) {
  195. return packOrder_packBytes(
  196. truncateToU32(clampUpper(s0, F(255.1f))),
  197. truncateToU32(clampUpper(s1, F(255.1f))),
  198. truncateToU32(clampUpper(s2, F(255.1f))),
  199. truncateToU32(clampUpper(s3, F(255.1f))),
  200. order
  201. );
  202. }
  203. }
  204. #endif