SDL_Pixels.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * SDL_Pixels.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. // for accessing channels from pixel in format-independent way
  14. //should be as fast as accessing via pointer at least for 3-4 bytes per pixel
  15. namespace Channels
  16. {
  17. // channel not present in this format
  18. struct channel_empty
  19. {
  20. static STRONG_INLINE void set(Uint8*, Uint8) {}
  21. static STRONG_INLINE Uint8 get(const Uint8 *) { return 255;}
  22. };
  23. // channel which uses whole pixel
  24. template<int offset>
  25. struct channel_pixel
  26. {
  27. static STRONG_INLINE void set(Uint8 *ptr, Uint8 value)
  28. {
  29. ptr[offset] = value;
  30. }
  31. static STRONG_INLINE Uint8 get(const Uint8 *ptr)
  32. {
  33. return ptr[offset];
  34. }
  35. };
  36. // channel which uses part of pixel
  37. template <int bits, int mask, int shift>
  38. struct channel_subpx
  39. {
  40. static void STRONG_INLINE set(Uint8 *ptr, Uint8 value)
  41. {
  42. Uint16 * const pixel = (Uint16*)ptr;
  43. Uint8 subpx = value >> (8 - bits);
  44. *pixel = (*pixel & ~mask) | ((subpx << shift) & mask );
  45. }
  46. static Uint8 STRONG_INLINE get(const Uint8 *ptr)
  47. {
  48. Uint8 subpx = (*((Uint16 *)ptr) & mask) >> shift;
  49. return (subpx << (8 - bits)) | (subpx >> (2*bits - 8));
  50. }
  51. };
  52. template<int bpp>
  53. struct px
  54. {
  55. static channel_empty r;
  56. static channel_empty g;
  57. static channel_empty b;
  58. static channel_empty a;
  59. };
  60. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  61. template<>
  62. struct px<4>
  63. {
  64. static channel_pixel<1> r;
  65. static channel_pixel<2> g;
  66. static channel_pixel<3> b;
  67. static channel_pixel<0> a;
  68. };
  69. template<>
  70. struct px<3>
  71. {
  72. static channel_pixel<0> r;
  73. static channel_pixel<1> g;
  74. static channel_pixel<2> b;
  75. static channel_empty a;
  76. };
  77. #else
  78. template<>
  79. struct px<4>
  80. {
  81. static channel_pixel<2> r;
  82. static channel_pixel<1> g;
  83. static channel_pixel<0> b;
  84. static channel_pixel<3> a;
  85. };
  86. template<>
  87. struct px<3>
  88. {
  89. static channel_pixel<2> r;
  90. static channel_pixel<1> g;
  91. static channel_pixel<0> b;
  92. static channel_empty a;
  93. };
  94. #endif
  95. template<>
  96. struct px<2>
  97. {
  98. static channel_subpx<5, 0xF800, 11> r;
  99. static channel_subpx<6, 0x07E0, 5 > g;
  100. static channel_subpx<5, 0x001F, 0 > b;
  101. static channel_empty a;
  102. };
  103. }
  104. template<int bpp, int incrementPtr>
  105. struct ColorPutter
  106. {
  107. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  108. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  109. static STRONG_INLINE void PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  110. static STRONG_INLINE void PutColor(Uint8 *&ptr, const SDL_Color & Color);
  111. static STRONG_INLINE void PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color);
  112. static STRONG_INLINE void PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count);
  113. };
  114. template <int incrementPtr>
  115. struct ColorPutter<2, incrementPtr>
  116. {
  117. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  118. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  119. static STRONG_INLINE void PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  120. static STRONG_INLINE void PutColor(Uint8 *&ptr, const SDL_Color & Color);
  121. static STRONG_INLINE void PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color);
  122. static STRONG_INLINE void PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count);
  123. };
  124. template<int bpp, int incrementPtr>
  125. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color)
  126. {
  127. PutColor(ptr, Color.r, Color.g, Color.b, Color.a);
  128. }
  129. template<int bpp, int incrementPtr>
  130. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const SDL_Color & Color)
  131. {
  132. PutColor(ptr, Color.r, Color.g, Color.b);
  133. }
  134. template<int bpp, int incrementPtr>
  135. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  136. {
  137. switch (A)
  138. {
  139. case 0:
  140. ptr += bpp * incrementPtr;
  141. return;
  142. case 255:
  143. PutColor(ptr, R, G, B);
  144. return;
  145. case 128: // optimized
  146. PutColor(ptr,
  147. ((Uint16)R + Channels::px<bpp>::r.get(ptr)) >> 1,
  148. ((Uint16)G + Channels::px<bpp>::g.get(ptr)) >> 1,
  149. ((Uint16)B + Channels::px<bpp>::b.get(ptr)) >> 1);
  150. return;
  151. default:
  152. PutColor(ptr, R, G, B, A);
  153. return;
  154. }
  155. }
  156. template<int bpp, int incrementPtr>
  157. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  158. {
  159. PutColor(ptr, ((((Uint32)R - (Uint32)Channels::px<bpp>::r.get(ptr))*(Uint32)A) >> 8 ) + (Uint32)Channels::px<bpp>::r.get(ptr),
  160. ((((Uint32)G - (Uint32)Channels::px<bpp>::g.get(ptr))*(Uint32)A) >> 8 ) + (Uint32)Channels::px<bpp>::g.get(ptr),
  161. ((((Uint32)B - (Uint32)Channels::px<bpp>::b.get(ptr))*(Uint32)A) >> 8 ) + (Uint32)Channels::px<bpp>::b.get(ptr));
  162. }
  163. template<int bpp, int incrementPtr>
  164. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B)
  165. {
  166. static_assert(incrementPtr >= -1 && incrementPtr <= +1, "Invalid incrementPtr value!");
  167. if (incrementPtr < 0)
  168. ptr -= bpp;
  169. Channels::px<bpp>::r.set(ptr, R);
  170. Channels::px<bpp>::g.set(ptr, G);
  171. Channels::px<bpp>::b.set(ptr, B);
  172. Channels::px<bpp>::a.set(ptr, 255);
  173. if (incrementPtr > 0)
  174. ptr += bpp;
  175. }
  176. template<int bpp, int incrementPtr>
  177. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count)
  178. {
  179. if (count)
  180. {
  181. Uint8 *pixel = ptr;
  182. PutColor(ptr, Color.r, Color.g, Color.b);
  183. for (size_t i=0; i<count-1; i++)
  184. {
  185. memcpy(ptr, pixel, bpp);
  186. ptr += bpp * incrementPtr;
  187. }
  188. }
  189. }
  190. template <int incrementPtr>
  191. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B)
  192. {
  193. if(incrementPtr == -1)
  194. ptr -= 2;
  195. Uint16 * const px = (Uint16*)ptr;
  196. *px = (B>>3) + ((G>>2) << 5) + ((R>>3) << 11); //drop least significant bits of 24 bpp encoded color
  197. if(incrementPtr == 1)
  198. ptr += 2; //bpp
  199. }
  200. template <int incrementPtr>
  201. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  202. {
  203. switch (A)
  204. {
  205. case 0:
  206. ptr += 2 * incrementPtr;
  207. return;
  208. case 255:
  209. PutColor(ptr, R, G, B);
  210. return;
  211. default:
  212. PutColor(ptr, R, G, B, A);
  213. return;
  214. }
  215. }
  216. template <int incrementPtr>
  217. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  218. {
  219. const int rbit = 5, gbit = 6, bbit = 5; //bits per color
  220. const int rmask = 0xF800, gmask = 0x7E0, bmask = 0x1F;
  221. const int rshift = 11, gshift = 5, bshift = 0;
  222. const Uint8 r5 = (*((Uint16 *)ptr) & rmask) >> rshift,
  223. b5 = (*((Uint16 *)ptr) & bmask) >> bshift,
  224. g5 = (*((Uint16 *)ptr) & gmask) >> gshift;
  225. const Uint32 r8 = (r5 << (8 - rbit)) | (r5 >> (2*rbit - 8)),
  226. g8 = (g5 << (8 - gbit)) | (g5 >> (2*gbit - 8)),
  227. b8 = (b5 << (8 - bbit)) | (b5 >> (2*bbit - 8));
  228. PutColor(ptr,
  229. (((R-r8)*A) >> 8) + r8,
  230. (((G-g8)*A) >> 8) + g8,
  231. (((B-b8)*A) >> 8) + b8);
  232. }
  233. template <int incrementPtr>
  234. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color)
  235. {
  236. PutColor(ptr, Color.r, Color.g, Color.b, Color.a);
  237. }
  238. template <int incrementPtr>
  239. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const SDL_Color & Color)
  240. {
  241. PutColor(ptr, Color.r, Color.g, Color.b);
  242. }
  243. template <int incrementPtr>
  244. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count)
  245. {
  246. //drop least significant bits of 24 bpp encoded color
  247. Uint16 pixel = (Color.b>>3) + ((Color.g>>2) << 5) + ((Color.r>>3) << 11);
  248. for (size_t i=0; i<count; i++)
  249. {
  250. memcpy(ptr, &pixel, 2);
  251. if(incrementPtr == -1)
  252. ptr -= 2;
  253. if(incrementPtr == 1)
  254. ptr += 2;
  255. }
  256. }