SDL_Pixels.h 7.4 KB

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