SDL_PixelAccess.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. uint16_t * 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. template<>
  97. struct px<2>
  98. {
  99. static channel_subpx<5, 0xF800, 11> r;
  100. static channel_subpx<6, 0x07E0, 5 > g;
  101. static channel_subpx<5, 0x001F, 0 > b;
  102. static channel_empty a;
  103. };
  104. }
  105. template<int bpp, int incrementPtr>
  106. struct ColorPutter
  107. {
  108. static STRONG_INLINE void PutColor(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B);
  109. static STRONG_INLINE void PutColor(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A);
  110. static STRONG_INLINE void PutColorAlphaSwitch(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A);
  111. static STRONG_INLINE void PutColor(uint8_t *&ptr, const SDL_Color & Color);
  112. static STRONG_INLINE void PutColorAlpha(uint8_t *&ptr, const SDL_Color & Color);
  113. static STRONG_INLINE void PutColorRow(uint8_t *&ptr, const SDL_Color & Color, size_t count);
  114. };
  115. template <int incrementPtr>
  116. struct ColorPutter<2, incrementPtr>
  117. {
  118. static STRONG_INLINE void PutColor(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B);
  119. static STRONG_INLINE void PutColor(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A);
  120. static STRONG_INLINE void PutColorAlphaSwitch(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A);
  121. static STRONG_INLINE void PutColor(uint8_t *&ptr, const SDL_Color & Color);
  122. static STRONG_INLINE void PutColorAlpha(uint8_t *&ptr, const SDL_Color & Color);
  123. static STRONG_INLINE void PutColorRow(uint8_t *&ptr, const SDL_Color & Color, size_t count);
  124. };
  125. template<int bpp, int incrementPtr>
  126. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlpha(uint8_t *&ptr, const SDL_Color & Color)
  127. {
  128. PutColor(ptr, Color.r, Color.g, Color.b, Color.a);
  129. }
  130. template<int bpp, int incrementPtr>
  131. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(uint8_t *&ptr, const SDL_Color & Color)
  132. {
  133. PutColor(ptr, Color.r, Color.g, Color.b);
  134. }
  135. template<int bpp, int incrementPtr>
  136. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlphaSwitch(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A)
  137. {
  138. switch (A)
  139. {
  140. case 0:
  141. ptr += bpp * incrementPtr;
  142. return;
  143. case 255:
  144. PutColor(ptr, R, G, B);
  145. return;
  146. case 128: // optimized
  147. PutColor(ptr,
  148. ((uint16_t)R + Channels::px<bpp>::r.get(ptr)) >> 1,
  149. ((uint16_t)G + Channels::px<bpp>::g.get(ptr)) >> 1,
  150. ((uint16_t)B + Channels::px<bpp>::b.get(ptr)) >> 1);
  151. return;
  152. default:
  153. PutColor(ptr, R, G, B, A);
  154. return;
  155. }
  156. }
  157. template<int bpp, int incrementPtr>
  158. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A)
  159. {
  160. 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),
  161. ((((uint32_t)G - (uint32_t)Channels::px<bpp>::g.get(ptr))*(uint32_t)A) >> 8 ) + (uint32_t)Channels::px<bpp>::g.get(ptr),
  162. ((((uint32_t)B - (uint32_t)Channels::px<bpp>::b.get(ptr))*(uint32_t)A) >> 8 ) + (uint32_t)Channels::px<bpp>::b.get(ptr));
  163. }
  164. template<int bpp, int incrementPtr>
  165. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B)
  166. {
  167. static_assert(incrementPtr >= -1 && incrementPtr <= +1, "Invalid incrementPtr value!");
  168. if (incrementPtr < 0)
  169. ptr -= bpp;
  170. Channels::px<bpp>::r.set(ptr, R);
  171. Channels::px<bpp>::g.set(ptr, G);
  172. Channels::px<bpp>::b.set(ptr, B);
  173. Channels::px<bpp>::a.set(ptr, 255);
  174. if (incrementPtr > 0)
  175. ptr += bpp;
  176. }
  177. template<int bpp, int incrementPtr>
  178. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorRow(uint8_t *&ptr, const SDL_Color & Color, size_t count)
  179. {
  180. if (count)
  181. {
  182. uint8_t *pixel = ptr;
  183. PutColor(ptr, Color.r, Color.g, Color.b);
  184. for (size_t i=0; i<count-1; i++)
  185. {
  186. memcpy(ptr, pixel, bpp);
  187. ptr += bpp * incrementPtr;
  188. }
  189. }
  190. }
  191. template <int incrementPtr>
  192. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B)
  193. {
  194. if(incrementPtr == -1)
  195. ptr -= 2;
  196. uint16_t * const px = (uint16_t*)ptr;
  197. *px = (B>>3) + ((G>>2) << 5) + ((R>>3) << 11); //drop least significant bits of 24 bpp encoded color
  198. if(incrementPtr == 1)
  199. ptr += 2; //bpp
  200. }
  201. template <int incrementPtr>
  202. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlphaSwitch(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A)
  203. {
  204. switch (A)
  205. {
  206. case 0:
  207. ptr += 2 * incrementPtr;
  208. return;
  209. case 255:
  210. PutColor(ptr, R, G, B);
  211. return;
  212. default:
  213. PutColor(ptr, R, G, B, A);
  214. return;
  215. }
  216. }
  217. template <int incrementPtr>
  218. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A)
  219. {
  220. const int rbit = 5, gbit = 6, bbit = 5; //bits per color
  221. const int rmask = 0xF800, gmask = 0x7E0, bmask = 0x1F;
  222. const int rshift = 11, gshift = 5, bshift = 0;
  223. const uint8_t r5 = (*((uint16_t *)ptr) & rmask) >> rshift,
  224. b5 = (*((uint16_t *)ptr) & bmask) >> bshift,
  225. g5 = (*((uint16_t *)ptr) & gmask) >> gshift;
  226. const uint32_t r8 = (r5 << (8 - rbit)) | (r5 >> (2*rbit - 8)),
  227. g8 = (g5 << (8 - gbit)) | (g5 >> (2*gbit - 8)),
  228. b8 = (b5 << (8 - bbit)) | (b5 >> (2*bbit - 8));
  229. PutColor(ptr,
  230. (((R-r8)*A) >> 8) + r8,
  231. (((G-g8)*A) >> 8) + g8,
  232. (((B-b8)*A) >> 8) + b8);
  233. }
  234. template <int incrementPtr>
  235. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlpha(uint8_t *&ptr, const SDL_Color & Color)
  236. {
  237. PutColor(ptr, Color.r, Color.g, Color.b, Color.a);
  238. }
  239. template <int incrementPtr>
  240. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(uint8_t *&ptr, const SDL_Color & Color)
  241. {
  242. PutColor(ptr, Color.r, Color.g, Color.b);
  243. }
  244. template <int incrementPtr>
  245. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorRow(uint8_t *&ptr, const SDL_Color & Color, size_t count)
  246. {
  247. //drop least significant bits of 24 bpp encoded color
  248. uint16_t pixel = (Color.b>>3) + ((Color.g>>2) << 5) + ((Color.r>>3) << 11);
  249. for (size_t i=0; i<count; i++)
  250. {
  251. memcpy(ptr, &pixel, 2);
  252. if(incrementPtr == -1)
  253. ptr -= 2;
  254. if(incrementPtr == 1)
  255. ptr += 2;
  256. }
  257. }