SDL_PixelAccess.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * SDL_PixelAccess.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "SDL_Extensions.h"
  12. #include <SDL_endian.h>
  13. #include <SDL_pixels.h>
  14. // for accessing channels from pixel in format-independent way
  15. //should be as fast as accessing via pointer at least for 3-4 bytes per pixel
  16. namespace Channels
  17. {
  18. // channel not present in this format
  19. struct channel_empty
  20. {
  21. static STRONG_INLINE void set(uint8_t*, uint8_t) {}
  22. static STRONG_INLINE uint8_t get(const uint8_t *) { return 255;}
  23. };
  24. // channel which uses whole pixel
  25. template<int offset>
  26. struct channel_pixel
  27. {
  28. static STRONG_INLINE void set(uint8_t *ptr, uint8_t value)
  29. {
  30. ptr[offset] = value;
  31. }
  32. static STRONG_INLINE uint8_t get(const uint8_t *ptr)
  33. {
  34. return ptr[offset];
  35. }
  36. };
  37. // channel which uses part of pixel
  38. template <int bits, int mask, int shift>
  39. struct channel_subpx
  40. {
  41. static void STRONG_INLINE set(uint8_t *ptr, uint8_t value)
  42. {
  43. auto * const pixel = (uint16_t *)ptr;
  44. uint8_t subpx = value >> (8 - bits);
  45. *pixel = (*pixel & ~mask) | ((subpx << shift) & mask );
  46. }
  47. static uint8_t STRONG_INLINE get(const uint8_t *ptr)
  48. {
  49. uint8_t subpx = (*((uint16_t *)ptr) & mask) >> shift;
  50. return (subpx << (8 - bits)) | (subpx >> (2*bits - 8));
  51. }
  52. };
  53. template<int bpp>
  54. struct px
  55. {
  56. static channel_empty r;
  57. static channel_empty g;
  58. static channel_empty b;
  59. static channel_empty a;
  60. };
  61. #ifdef VCMI_ENDIAN_BIG
  62. template<>
  63. struct px<4>
  64. {
  65. static channel_pixel<1> r;
  66. static channel_pixel<2> g;
  67. static channel_pixel<3> b;
  68. static channel_pixel<0> a;
  69. };
  70. template<>
  71. struct px<3>
  72. {
  73. static channel_pixel<0> r;
  74. static channel_pixel<1> g;
  75. static channel_pixel<2> b;
  76. static channel_empty a;
  77. };
  78. #else
  79. template<>
  80. struct px<4>
  81. {
  82. static channel_pixel<2> r;
  83. static channel_pixel<1> g;
  84. static channel_pixel<0> b;
  85. static channel_pixel<3> a;
  86. };
  87. template<>
  88. struct px<3>
  89. {
  90. static channel_pixel<2> r;
  91. static channel_pixel<1> g;
  92. static channel_pixel<0> b;
  93. static channel_empty a;
  94. };
  95. #endif
  96. }
  97. template<int bpp>
  98. struct ColorPutter
  99. {
  100. static STRONG_INLINE void PutColor(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B);
  101. static STRONG_INLINE void PutColor(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A);
  102. static STRONG_INLINE void PutColorAlphaSwitch(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A);
  103. static STRONG_INLINE void PutColorAlpha(uint8_t *&ptr, const SDL_Color & Color);
  104. };
  105. template<int bpp>
  106. STRONG_INLINE void ColorPutter<bpp>::PutColorAlpha(uint8_t *&ptr, const SDL_Color & Color)
  107. {
  108. PutColor(ptr, Color.r, Color.g, Color.b, Color.a);
  109. }
  110. template<int bpp>
  111. STRONG_INLINE void ColorPutter<bpp>::PutColorAlphaSwitch(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A)
  112. {
  113. switch (A)
  114. {
  115. case 0:
  116. return;
  117. case 255:
  118. PutColor(ptr, R, G, B);
  119. return;
  120. case 128: // optimized
  121. PutColor(ptr,
  122. ((uint16_t)R + Channels::px<bpp>::r.get(ptr)) >> 1,
  123. ((uint16_t)G + Channels::px<bpp>::g.get(ptr)) >> 1,
  124. ((uint16_t)B + Channels::px<bpp>::b.get(ptr)) >> 1);
  125. return;
  126. default:
  127. PutColor(ptr, R, G, B, A);
  128. return;
  129. }
  130. }
  131. template<int bpp>
  132. STRONG_INLINE void ColorPutter<bpp>::PutColor(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A)
  133. {
  134. PutColor(ptr, ((((uint32_t)R - (uint32_t)Channels::px<bpp>::r.get(ptr))*(uint32_t)A) >> 8 ) + (uint32_t)Channels::px<bpp>::r.get(ptr),
  135. ((((uint32_t)G - (uint32_t)Channels::px<bpp>::g.get(ptr))*(uint32_t)A) >> 8 ) + (uint32_t)Channels::px<bpp>::g.get(ptr),
  136. ((((uint32_t)B - (uint32_t)Channels::px<bpp>::b.get(ptr))*(uint32_t)A) >> 8 ) + (uint32_t)Channels::px<bpp>::b.get(ptr));
  137. }
  138. template<int bpp>
  139. STRONG_INLINE void ColorPutter<bpp>::PutColor(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B)
  140. {
  141. Channels::px<bpp>::r.set(ptr, R);
  142. Channels::px<bpp>::g.set(ptr, G);
  143. Channels::px<bpp>::b.set(ptr, B);
  144. Channels::px<bpp>::a.set(ptr, 255);
  145. }