surface.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. pygame - Python Game Library
  3. Copyright (C) 2000-2001 Pete Shinners
  4. Copyright (C) 2007 Marcus von Appen
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. Pete Shinners
  17. pete@shinners.org
  18. */
  19. #ifndef SURFACE_H
  20. #define SURFACE_H
  21. /* This is defined in SDL.h */
  22. #if defined(_POSIX_C_SOURCE)
  23. #undef _POSIX_C_SOURCE
  24. #endif
  25. #include <SDL.h>
  26. #include "pygame.h"
  27. /* Blend modes */
  28. #define PYGAME_BLEND_ADD 0x1
  29. #define PYGAME_BLEND_SUB 0x2
  30. #define PYGAME_BLEND_MULT 0x3
  31. #define PYGAME_BLEND_MIN 0x4
  32. #define PYGAME_BLEND_MAX 0x5
  33. #define PYGAME_BLEND_RGB_ADD 0x1
  34. #define PYGAME_BLEND_RGB_SUB 0x2
  35. #define PYGAME_BLEND_RGB_MULT 0x3
  36. #define PYGAME_BLEND_RGB_MIN 0x4
  37. #define PYGAME_BLEND_RGB_MAX 0x5
  38. #define PYGAME_BLEND_RGBA_ADD 0x6
  39. #define PYGAME_BLEND_RGBA_SUB 0x7
  40. #define PYGAME_BLEND_RGBA_MULT 0x8
  41. #define PYGAME_BLEND_RGBA_MIN 0x9
  42. #define PYGAME_BLEND_RGBA_MAX 0x10
  43. #define PYGAME_BLEND_PREMULTIPLIED 0x11
  44. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  45. #define GET_PIXEL_24(b) (b[0] + (b[1] << 8) + (b[2] << 16))
  46. #else
  47. #define GET_PIXEL_24(b) (b[2] + (b[1] << 8) + (b[0] << 16))
  48. #endif
  49. #define GET_PIXEL(pxl, bpp, source) \
  50. switch (bpp) \
  51. { \
  52. case 2: \
  53. pxl = *((Uint16 *) (source)); \
  54. break; \
  55. case 4: \
  56. pxl = *((Uint32 *) (source)); \
  57. break; \
  58. default: \
  59. { \
  60. Uint8 *b = (Uint8 *) source; \
  61. pxl = GET_PIXEL_24(b); \
  62. } \
  63. break; \
  64. }
  65. #if IS_SDLv1
  66. #define GET_PIXELVALS(_sR, _sG, _sB, _sA, px, fmt, ppa) \
  67. _sR = ((px & fmt->Rmask) >> fmt->Rshift); \
  68. _sR = (_sR << fmt->Rloss) + (_sR >> (8 - (fmt->Rloss << 1))); \
  69. _sG = ((px & fmt->Gmask) >> fmt->Gshift); \
  70. _sG = (_sG << fmt->Gloss) + (_sG >> (8 - (fmt->Gloss << 1))); \
  71. _sB = ((px & fmt->Bmask) >> fmt->Bshift); \
  72. _sB = (_sB << fmt->Bloss) + (_sB >> (8 - (fmt->Bloss << 1))); \
  73. if (ppa) \
  74. { \
  75. _sA = ((px & fmt->Amask) >> fmt->Ashift); \
  76. _sA = (_sA << fmt->Aloss) + (_sA >> (8 - (fmt->Aloss << 1))); \
  77. } \
  78. else \
  79. { \
  80. _sA = 255; \
  81. }
  82. #define GET_PIXELVALS_1(sr, sg, sb, sa, _src, _fmt) \
  83. sr = _fmt->palette->colors[*((Uint8 *) (_src))].r; \
  84. sg = _fmt->palette->colors[*((Uint8 *) (_src))].g; \
  85. sb = _fmt->palette->colors[*((Uint8 *) (_src))].b; \
  86. sa = 255;
  87. /* For 1 byte palette pixels */
  88. #define SET_PIXELVAL(px, fmt, _dR, _dG, _dB, _dA) \
  89. *(px) = (Uint8) SDL_MapRGB(fmt, _dR, _dG, _dB)
  90. #else /* IS_SDLv2 */
  91. #define GET_PIXELVALS(_sR, _sG, _sB, _sA, px, fmt, ppa) \
  92. SDL_GetRGBA(px, fmt, &(_sR), &(_sG), &(_sB), &(_sA)); \
  93. if (!ppa) { \
  94. _sA = 255; \
  95. }
  96. #define GET_PIXELVALS_1(sr, sg, sb, sa, _src, _fmt) \
  97. sr = _fmt->palette->colors[*((Uint8 *) (_src))].r; \
  98. sg = _fmt->palette->colors[*((Uint8 *) (_src))].g; \
  99. sb = _fmt->palette->colors[*((Uint8 *) (_src))].b; \
  100. sa = 255;
  101. /* For 1 byte palette pixels */
  102. #define SET_PIXELVAL(px, fmt, _dR, _dG, _dB, _dA) \
  103. *(px) = (Uint8) SDL_MapRGBA(fmt, _dR, _dG, _dB, _dA)
  104. #endif /* IS_SDLv2 */
  105. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  106. #define SET_OFFSETS_24(or, og, ob, fmt) \
  107. { \
  108. or = (fmt->Rshift == 0 ? 0 : \
  109. fmt->Rshift == 8 ? 1 : \
  110. 2 ); \
  111. og = (fmt->Gshift == 0 ? 0 : \
  112. fmt->Gshift == 8 ? 1 : \
  113. 2 ); \
  114. ob = (fmt->Bshift == 0 ? 0 : \
  115. fmt->Bshift == 8 ? 1 : \
  116. 2 ); \
  117. }
  118. #define SET_OFFSETS_32(or, og, ob, fmt) \
  119. { \
  120. or = (fmt->Rshift == 0 ? 0 : \
  121. fmt->Rshift == 8 ? 1 : \
  122. fmt->Rshift == 16 ? 2 : \
  123. 3 ); \
  124. og = (fmt->Gshift == 0 ? 0 : \
  125. fmt->Gshift == 8 ? 1 : \
  126. fmt->Gshift == 16 ? 2 : \
  127. 3 ); \
  128. ob = (fmt->Bshift == 0 ? 0 : \
  129. fmt->Bshift == 8 ? 1 : \
  130. fmt->Bshift == 16 ? 2 : \
  131. 3 ); \
  132. }
  133. #else
  134. #define SET_OFFSETS_24(or, og, ob, fmt) \
  135. { \
  136. or = (fmt->Rshift == 0 ? 2 : \
  137. fmt->Rshift == 8 ? 1 : \
  138. 0 ); \
  139. og = (fmt->Gshift == 0 ? 2 : \
  140. fmt->Gshift == 8 ? 1 : \
  141. 0 ); \
  142. ob = (fmt->Bshift == 0 ? 2 : \
  143. fmt->Bshift == 8 ? 1 : \
  144. 0 ); \
  145. }
  146. #define SET_OFFSETS_32(or, og, ob, fmt) \
  147. { \
  148. or = (fmt->Rshift == 0 ? 3 : \
  149. fmt->Rshift == 8 ? 2 : \
  150. fmt->Rshift == 16 ? 1 : \
  151. 0 ); \
  152. og = (fmt->Gshift == 0 ? 3 : \
  153. fmt->Gshift == 8 ? 2 : \
  154. fmt->Gshift == 16 ? 1 : \
  155. 0 ); \
  156. ob = (fmt->Bshift == 0 ? 3 : \
  157. fmt->Bshift == 8 ? 2 : \
  158. fmt->Bshift == 16 ? 1 : \
  159. 0 ); \
  160. }
  161. #endif
  162. #define CREATE_PIXEL(buf, r, g, b, a, bp, ft) \
  163. switch (bp) \
  164. { \
  165. case 2: \
  166. *((Uint16 *) (buf)) = \
  167. ((r >> ft->Rloss) << ft->Rshift) | \
  168. ((g >> ft->Gloss) << ft->Gshift) | \
  169. ((b >> ft->Bloss) << ft->Bshift) | \
  170. ((a >> ft->Aloss) << ft->Ashift); \
  171. break; \
  172. case 4: \
  173. *((Uint32 *) (buf)) = \
  174. ((r >> ft->Rloss) << ft->Rshift) | \
  175. ((g >> ft->Gloss) << ft->Gshift) | \
  176. ((b >> ft->Bloss) << ft->Bshift) | \
  177. ((a >> ft->Aloss) << ft->Ashift); \
  178. break; \
  179. }
  180. /* Pretty good idea from Tom Duff :-). */
  181. #define LOOP_UNROLLED4(code, n, width) \
  182. n = (width + 3) / 4; \
  183. switch (width & 3) \
  184. { \
  185. case 0: do { code; \
  186. case 3: code; \
  187. case 2: code; \
  188. case 1: code; \
  189. } while (--n > 0); \
  190. }
  191. /* Used in the srcbpp == dstbpp == 1 blend functions */
  192. #define REPEAT_3(code) \
  193. code; \
  194. code; \
  195. code;
  196. #define REPEAT_4(code) \
  197. code; \
  198. code; \
  199. code; \
  200. code;
  201. #define BLEND_ADD(tmp, sR, sG, sB, sA, dR, dG, dB, dA) \
  202. tmp = dR + sR; dR = (tmp <= 255 ? tmp : 255); \
  203. tmp = dG + sG; dG = (tmp <= 255 ? tmp : 255); \
  204. tmp = dB + sB; dB = (tmp <= 255 ? tmp : 255);
  205. #define BLEND_SUB(tmp, sR, sG, sB, sA, dR, dG, dB, dA) \
  206. tmp = dR - sR; dR = (tmp >= 0 ? tmp : 0); \
  207. tmp = dG - sG; dG = (tmp >= 0 ? tmp : 0); \
  208. tmp = dB - sB; dB = (tmp >= 0 ? tmp : 0);
  209. #define BLEND_MULT(sR, sG, sB, sA, dR, dG, dB, dA) \
  210. dR = (dR && sR) ? (dR * sR) >> 8 : 0; \
  211. dG = (dG && sG) ? (dG * sG) >> 8 : 0; \
  212. dB = (dB && sB) ? (dB * sB) >> 8 : 0;
  213. #define BLEND_MIN(sR, sG, sB, sA, dR, dG, dB, dA) \
  214. if(sR < dR) { dR = sR; } \
  215. if(sG < dG) { dG = sG; } \
  216. if(sB < dB) { dB = sB; }
  217. #define BLEND_MAX(sR, sG, sB, sA, dR, dG, dB, dA) \
  218. if(sR > dR) { dR = sR; } \
  219. if(sG > dG) { dG = sG; } \
  220. if(sB > dB) { dB = sB; }
  221. #define BLEND_RGBA_ADD(tmp, sR, sG, sB, sA, dR, dG, dB, dA) \
  222. tmp = dR + sR; dR = (tmp <= 255 ? tmp : 255); \
  223. tmp = dG + sG; dG = (tmp <= 255 ? tmp : 255); \
  224. tmp = dB + sB; dB = (tmp <= 255 ? tmp : 255); \
  225. tmp = dA + sA; dA = (tmp <= 255 ? tmp : 255);
  226. #define BLEND_RGBA_SUB(tmp, sR, sG, sB, sA, dR, dG, dB, dA) \
  227. tmp = dR - sR; dR = (tmp >= 0 ? tmp : 0); \
  228. tmp = dG - sG; dG = (tmp >= 0 ? tmp : 0); \
  229. tmp = dB - sB; dB = (tmp >= 0 ? tmp : 0); \
  230. tmp = dA - sA; dA = (tmp >= 0 ? tmp : 0);
  231. #define BLEND_RGBA_MULT(sR, sG, sB, sA, dR, dG, dB, dA) \
  232. dR = (dR && sR) ? (dR * sR) >> 8 : 0; \
  233. dG = (dG && sG) ? (dG * sG) >> 8 : 0; \
  234. dB = (dB && sB) ? (dB * sB) >> 8 : 0; \
  235. dA = (dA && sA) ? (dA * sA) >> 8 : 0;
  236. #define BLEND_RGBA_MIN(sR, sG, sB, sA, dR, dG, dB, dA) \
  237. if(sR < dR) { dR = sR; } \
  238. if(sG < dG) { dG = sG; } \
  239. if(sB < dB) { dB = sB; } \
  240. if(sA < dA) { dA = sA; }
  241. #define BLEND_RGBA_MAX(sR, sG, sB, sA, dR, dG, dB, dA) \
  242. if(sR > dR) { dR = sR; } \
  243. if(sG > dG) { dG = sG; } \
  244. if(sB > dB) { dB = sB; } \
  245. if(sA > dA) { dA = sA; }
  246. #if 1
  247. /* Choose an alpha blend equation. If the sign is preserved on a right shift
  248. * then use a specialized, faster, equation. Otherwise a more general form,
  249. * where all additions are done before the shift, is needed.
  250. */
  251. #if (-1 >> 1) < 0
  252. #define ALPHA_BLEND_COMP(sC, dC, sA) ((((sC - dC) * sA + sC) >> 8) + dC)
  253. #else
  254. #define ALPHA_BLEND_COMP(sC, dC, sA) (((dC << 8) + (sC - dC) * sA + sC) >> 8)
  255. #endif
  256. #define ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB, dA) \
  257. do { \
  258. if (dA) \
  259. { \
  260. dR = ALPHA_BLEND_COMP(sR, dR, sA); \
  261. dG = ALPHA_BLEND_COMP(sG, dG, sA); \
  262. dB = ALPHA_BLEND_COMP(sB, dB, sA); \
  263. dA = sA + dA - ((sA * dA) / 255); \
  264. } \
  265. else \
  266. { \
  267. dR = sR; \
  268. dG = sG; \
  269. dB = sB; \
  270. dA = sA; \
  271. } \
  272. } while(0)
  273. #define ALPHA_BLEND_PREMULTIPLIED_COMP(sC, dC, sA) (sC + dC - ((dC * sA) >> 8))
  274. #define ALPHA_BLEND_PREMULTIPLIED(tmp, sR, sG, sB, sA, dR, dG, dB, dA) \
  275. do { \
  276. tmp = ALPHA_BLEND_PREMULTIPLIED_COMP(sR, dR, sA); dR = (tmp > 255 ? 255 : tmp); \
  277. tmp = ALPHA_BLEND_PREMULTIPLIED_COMP(sG, dG, sA); dG = (tmp > 255 ? 255 : tmp); \
  278. tmp = ALPHA_BLEND_PREMULTIPLIED_COMP(sB, dB, sA); dB = (tmp > 255 ? 255 : tmp); \
  279. dA = sA + dA - ((sA * dA) / 255); \
  280. } while(0)
  281. #elif 0
  282. #define ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB, dA) \
  283. do { \
  284. if(sA){ \
  285. if(dA && sA < 255){ \
  286. int dContrib = dA*(255 - sA)/255; \
  287. dA = sA+dA - ((sA*dA)/255); \
  288. dR = (dR*dContrib + sR*sA)/dA; \
  289. dG = (dG*dContrib + sG*sA)/dA; \
  290. dB = (dB*dContrib + sB*sA)/dA; \
  291. }else{ \
  292. dR = sR; \
  293. dG = sG; \
  294. dB = sB; \
  295. dA = sA; \
  296. } \
  297. } \
  298. } while(0)
  299. #endif
  300. int
  301. surface_fill_blend (SDL_Surface *surface, SDL_Rect *rect, Uint32 color,
  302. int blendargs);
  303. void
  304. surface_respect_clip_rect (SDL_Surface *surface, SDL_Rect *rect);
  305. int
  306. pygame_AlphaBlit (SDL_Surface * src, SDL_Rect * srcrect,
  307. SDL_Surface * dst, SDL_Rect * dstrect, int the_args);
  308. int
  309. pygame_Blit (SDL_Surface * src, SDL_Rect * srcrect,
  310. SDL_Surface * dst, SDL_Rect * dstrect, int the_args);
  311. #endif /* SURFACE_H */