SDL_Pixels.h 7.4 KB

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