SDL_Pixels.h 7.5 KB

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