SDL_Pixels.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #pragma once
  2. #include <SDL_endian.h>
  3. #include "SDL_Extensions.h"
  4. /*
  5. * SDL_Pixels.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  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 incrementPtr>
  105. struct ColorPutter<2, incrementPtr>
  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 bpp, int incrementPtr>
  115. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color)
  116. {
  117. #ifdef VCMI_SDL1
  118. PutColor(ptr, Color.r, Color.g, Color.b, Color.unused);
  119. #else
  120. PutColor(ptr, Color.r, Color.g, Color.b, Color.a);
  121. #endif
  122. }
  123. template<int bpp, int incrementPtr>
  124. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const SDL_Color & Color)
  125. {
  126. PutColor(ptr, Color.r, Color.g, Color.b);
  127. }
  128. template<int bpp, int incrementPtr>
  129. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  130. {
  131. switch (A)
  132. {
  133. case 0:
  134. ptr += bpp * incrementPtr;
  135. return;
  136. case 255:
  137. PutColor(ptr, R, G, B);
  138. return;
  139. case 128: // optimized
  140. PutColor(ptr,
  141. ((Uint16)R + Channels::px<bpp>::r.get(ptr)) >> 1,
  142. ((Uint16)G + Channels::px<bpp>::g.get(ptr)) >> 1,
  143. ((Uint16)B + Channels::px<bpp>::b.get(ptr)) >> 1);
  144. return;
  145. default:
  146. PutColor(ptr, R, G, B, A);
  147. return;
  148. }
  149. }
  150. template<int bpp, int incrementPtr>
  151. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  152. {
  153. PutColor(ptr, ((((Uint32)R - (Uint32)Channels::px<bpp>::r.get(ptr))*(Uint32)A) >> 8 ) + (Uint32)Channels::px<bpp>::r.get(ptr),
  154. ((((Uint32)G - (Uint32)Channels::px<bpp>::g.get(ptr))*(Uint32)A) >> 8 ) + (Uint32)Channels::px<bpp>::g.get(ptr),
  155. ((((Uint32)B - (Uint32)Channels::px<bpp>::b.get(ptr))*(Uint32)A) >> 8 ) + (Uint32)Channels::px<bpp>::b.get(ptr));
  156. }
  157. template<int bpp, int incrementPtr>
  158. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B)
  159. {
  160. static_assert(incrementPtr >= -1 && incrementPtr <= +1, "Invalid incrementPtr value!");
  161. if (incrementPtr < 0)
  162. ptr -= bpp;
  163. Channels::px<bpp>::r.set(ptr, R);
  164. Channels::px<bpp>::g.set(ptr, G);
  165. Channels::px<bpp>::b.set(ptr, B);
  166. Channels::px<bpp>::a.set(ptr, 255);
  167. if (incrementPtr > 0)
  168. ptr += bpp;
  169. }
  170. template<int bpp, int incrementPtr>
  171. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count)
  172. {
  173. if (count)
  174. {
  175. Uint8 *pixel = ptr;
  176. PutColor(ptr, Color.r, Color.g, Color.b);
  177. for (size_t i=0; i<count-1; i++)
  178. {
  179. memcpy(ptr, pixel, bpp);
  180. ptr += bpp * incrementPtr;
  181. }
  182. }
  183. }
  184. template <int incrementPtr>
  185. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B)
  186. {
  187. if(incrementPtr == -1)
  188. ptr -= 2;
  189. Uint16 * const px = (Uint16*)ptr;
  190. *px = (B>>3) + ((G>>2) << 5) + ((R>>3) << 11); //drop least significant bits of 24 bpp encoded color
  191. if(incrementPtr == 1)
  192. ptr += 2; //bpp
  193. }
  194. template <int incrementPtr>
  195. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  196. {
  197. switch (A)
  198. {
  199. case 0:
  200. ptr += 2 * incrementPtr;
  201. return;
  202. case 255:
  203. PutColor(ptr, R, G, B);
  204. return;
  205. default:
  206. PutColor(ptr, R, G, B, A);
  207. return;
  208. }
  209. }
  210. template <int incrementPtr>
  211. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  212. {
  213. const int rbit = 5, gbit = 6, bbit = 5; //bits per color
  214. const int rmask = 0xF800, gmask = 0x7E0, bmask = 0x1F;
  215. const int rshift = 11, gshift = 5, bshift = 0;
  216. const Uint8 r5 = (*((Uint16 *)ptr) & rmask) >> rshift,
  217. b5 = (*((Uint16 *)ptr) & bmask) >> bshift,
  218. g5 = (*((Uint16 *)ptr) & gmask) >> gshift;
  219. const Uint32 r8 = (r5 << (8 - rbit)) | (r5 >> (2*rbit - 8)),
  220. g8 = (g5 << (8 - gbit)) | (g5 >> (2*gbit - 8)),
  221. b8 = (b5 << (8 - bbit)) | (b5 >> (2*bbit - 8));
  222. PutColor(ptr,
  223. (((R-r8)*A) >> 8) + r8,
  224. (((G-g8)*A) >> 8) + g8,
  225. (((B-b8)*A) >> 8) + b8);
  226. }
  227. template <int incrementPtr>
  228. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color)
  229. {
  230. #ifdef VCMI_SDL1
  231. PutColor(ptr, Color.r, Color.g, Color.b, Color.unused);
  232. #else
  233. PutColor(ptr, Color.r, Color.g, Color.b, Color.a);
  234. #endif
  235. }
  236. template <int incrementPtr>
  237. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const SDL_Color & Color)
  238. {
  239. PutColor(ptr, Color.r, Color.g, Color.b);
  240. }
  241. template <int incrementPtr>
  242. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count)
  243. {
  244. //drop least significant bits of 24 bpp encoded color
  245. Uint16 pixel = (Color.b>>3) + ((Color.g>>2) << 5) + ((Color.r>>3) << 11);
  246. for (size_t i=0; i<count; i++)
  247. {
  248. memcpy(ptr, &pixel, 2);
  249. if(incrementPtr == -1)
  250. ptr -= 2;
  251. if(incrementPtr == 1)
  252. ptr += 2;
  253. }
  254. }