SDL_blit.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifndef SDL_blit_h_
  20. #define SDL_blit_h_
  21. // Table to do pixel byte expansion
  22. extern const Uint8 *SDL_expand_byte[9];
  23. extern const Uint16 SDL_expand_byte_10[];
  24. // SDL blit copy flags
  25. #define SDL_COPY_MODULATE_COLOR 0x00000001
  26. #define SDL_COPY_MODULATE_ALPHA 0x00000002
  27. #define SDL_COPY_MODULATE_MASK (SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA)
  28. #define SDL_COPY_BLEND 0x00000010
  29. #define SDL_COPY_BLEND_PREMULTIPLIED 0x00000020
  30. #define SDL_COPY_ADD 0x00000040
  31. #define SDL_COPY_ADD_PREMULTIPLIED 0x00000080
  32. #define SDL_COPY_MOD 0x00000100
  33. #define SDL_COPY_MUL 0x00000200
  34. #define SDL_COPY_BLEND_MASK (SDL_COPY_BLEND | SDL_COPY_BLEND_PREMULTIPLIED | SDL_COPY_ADD | SDL_COPY_ADD_PREMULTIPLIED | SDL_COPY_MOD | SDL_COPY_MUL)
  35. #define SDL_COPY_COLORKEY 0x00000400
  36. #define SDL_COPY_NEAREST 0x00000800
  37. #define SDL_COPY_RLE_DESIRED 0x00001000
  38. #define SDL_COPY_RLE_COLORKEY 0x00002000
  39. #define SDL_COPY_RLE_ALPHAKEY 0x00004000
  40. #define SDL_COPY_RLE_MASK (SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY | SDL_COPY_RLE_ALPHAKEY)
  41. // SDL blit CPU flags
  42. #define SDL_CPU_ANY 0x00000000
  43. #define SDL_CPU_MMX 0x00000001
  44. #define SDL_CPU_SSE 0x00000002
  45. #define SDL_CPU_SSE2 0x00000004
  46. #define SDL_CPU_ALTIVEC_PREFETCH 0x00000008
  47. #define SDL_CPU_ALTIVEC_NOPREFETCH 0x00000010
  48. typedef struct
  49. {
  50. SDL_Surface *src_surface;
  51. Uint8 *src;
  52. int src_w, src_h;
  53. int src_pitch;
  54. int src_skip;
  55. SDL_Surface *dst_surface;
  56. Uint8 *dst;
  57. int dst_w, dst_h;
  58. int dst_pitch;
  59. int dst_skip;
  60. const SDL_PixelFormatDetails *src_fmt;
  61. const SDL_Palette *src_pal;
  62. const SDL_PixelFormatDetails *dst_fmt;
  63. const SDL_Palette *dst_pal;
  64. Uint8 *table;
  65. SDL_HashTable *palette_map;
  66. int flags;
  67. Uint32 colorkey;
  68. Uint8 r, g, b, a;
  69. } SDL_BlitInfo;
  70. typedef void (*SDL_BlitFunc)(SDL_BlitInfo *info);
  71. typedef struct
  72. {
  73. SDL_PixelFormat src_format;
  74. SDL_PixelFormat dst_format;
  75. int flags;
  76. unsigned int cpu;
  77. SDL_BlitFunc func;
  78. } SDL_BlitFuncEntry;
  79. typedef bool (SDLCALL *SDL_Blit) (struct SDL_Surface *src, const SDL_Rect *srcrect, struct SDL_Surface *dst, const SDL_Rect *dstrect);
  80. // Blit mapping definition
  81. typedef struct SDL_BlitMap
  82. {
  83. int identity;
  84. SDL_Blit blit;
  85. void *data;
  86. SDL_BlitInfo info;
  87. /* the version count matches the destination; mismatch indicates
  88. an invalid mapping */
  89. Uint32 dst_palette_version;
  90. Uint32 src_palette_version;
  91. } SDL_BlitMap;
  92. // Functions found in SDL_blit.c
  93. extern bool SDL_CalculateBlit(SDL_Surface *surface, SDL_Surface *dst);
  94. /* Functions found in SDL_blit_*.c */
  95. extern SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface *surface);
  96. extern SDL_BlitFunc SDL_CalculateBlit1(SDL_Surface *surface);
  97. extern SDL_BlitFunc SDL_CalculateBlitN(SDL_Surface *surface);
  98. extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface *surface);
  99. /*
  100. * Useful macros for blitting routines
  101. */
  102. #ifdef __GNUC__
  103. #define DECLARE_ALIGNED(t, v, a) t __attribute__((aligned(a))) v
  104. #elif defined(_MSC_VER)
  105. #define DECLARE_ALIGNED(t, v, a) __declspec(align(a)) t v
  106. #else
  107. #define DECLARE_ALIGNED(t, v, a) t v
  108. #endif
  109. // Load pixel of the specified format from a buffer and get its R-G-B values
  110. #define RGB_FROM_PIXEL(Pixel, fmt, r, g, b) \
  111. { \
  112. r = SDL_expand_byte[fmt->Rbits][((Pixel & fmt->Rmask) >> fmt->Rshift)]; \
  113. g = SDL_expand_byte[fmt->Gbits][((Pixel & fmt->Gmask) >> fmt->Gshift)]; \
  114. b = SDL_expand_byte[fmt->Bbits][((Pixel & fmt->Bmask) >> fmt->Bshift)]; \
  115. }
  116. #define RGB_FROM_RGB565(Pixel, r, g, b) \
  117. { \
  118. r = SDL_expand_byte[5][((Pixel & 0xF800) >> 11)]; \
  119. g = SDL_expand_byte[6][((Pixel & 0x07E0) >> 5)]; \
  120. b = SDL_expand_byte[5][(Pixel & 0x001F)]; \
  121. }
  122. #define RGB_FROM_RGB555(Pixel, r, g, b) \
  123. { \
  124. r = SDL_expand_byte[5][((Pixel & 0x7C00) >> 10)]; \
  125. g = SDL_expand_byte[5][((Pixel & 0x03E0) >> 5)]; \
  126. b = SDL_expand_byte[5][(Pixel & 0x001F)]; \
  127. }
  128. #define RGB_FROM_XRGB8888(Pixel, r, g, b) \
  129. { \
  130. r = ((Pixel & 0xFF0000) >> 16); \
  131. g = ((Pixel & 0xFF00) >> 8); \
  132. b = (Pixel & 0xFF); \
  133. }
  134. #define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel) \
  135. do { \
  136. switch (bpp) { \
  137. case 1: \
  138. Pixel = *((Uint8 *)(buf)); \
  139. break; \
  140. \
  141. case 2: \
  142. Pixel = *((Uint16 *)(buf)); \
  143. break; \
  144. \
  145. case 3: \
  146. { \
  147. Uint8 *B = (Uint8 *)(buf); \
  148. if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
  149. Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
  150. } else { \
  151. Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
  152. } \
  153. } break; \
  154. \
  155. case 4: \
  156. Pixel = *((Uint32 *)(buf)); \
  157. break; \
  158. \
  159. default: \
  160. Pixel = 0; /* stop gcc complaints */ \
  161. break; \
  162. } \
  163. } while (0)
  164. #define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b) \
  165. do { \
  166. switch (bpp) { \
  167. case 1: \
  168. Pixel = *((Uint8 *)(buf)); \
  169. RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \
  170. break; \
  171. \
  172. case 2: \
  173. Pixel = *((Uint16 *)(buf)); \
  174. RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \
  175. break; \
  176. \
  177. case 3: \
  178. { \
  179. Pixel = 0; \
  180. if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
  181. r = *((buf) + fmt->Rshift / 8); \
  182. g = *((buf) + fmt->Gshift / 8); \
  183. b = *((buf) + fmt->Bshift / 8); \
  184. } else { \
  185. r = *((buf) + 2 - fmt->Rshift / 8); \
  186. g = *((buf) + 2 - fmt->Gshift / 8); \
  187. b = *((buf) + 2 - fmt->Bshift / 8); \
  188. } \
  189. } break; \
  190. \
  191. case 4: \
  192. Pixel = *((Uint32 *)(buf)); \
  193. RGB_FROM_PIXEL(Pixel, fmt, r, g, b); \
  194. break; \
  195. \
  196. default: \
  197. /* stop gcc complaints */ \
  198. Pixel = 0; \
  199. r = g = b = 0; \
  200. break; \
  201. } \
  202. } while (0)
  203. // Assemble R-G-B values into a specified pixel format and store them
  204. #define PIXEL_FROM_RGB(Pixel, fmt, r, g, b) \
  205. { \
  206. Pixel = ((r >> (8 - fmt->Rbits)) << fmt->Rshift) | \
  207. ((g >> (8 - fmt->Gbits)) << fmt->Gshift) | \
  208. ((b >> (8 - fmt->Bbits)) << fmt->Bshift) | \
  209. fmt->Amask; \
  210. }
  211. #define RGB332_FROM_RGB(Pixel, r, g, b) \
  212. { \
  213. Pixel = (Uint8)(((r >> 5) << 5) | ((g >> 5) << 2) | (b >> 6)); \
  214. }
  215. #define RGB565_FROM_RGB(Pixel, r, g, b) \
  216. { \
  217. Pixel = (Uint16)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3)); \
  218. }
  219. #define RGB555_FROM_RGB(Pixel, r, g, b) \
  220. { \
  221. Pixel = (Uint16)(((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3)); \
  222. }
  223. #define XRGB8888_FROM_RGB(Pixel, r, g, b) \
  224. { \
  225. Pixel = (r << 16) | (g << 8) | b; \
  226. }
  227. #define ARGB8888_FROM_RGBA(Pixel, r, g, b, a) \
  228. { \
  229. Pixel = (a << 24) | (r << 16) | (g << 8) | b; \
  230. }
  231. #define RGBA8888_FROM_RGBA(Pixel, r, g, b, a) \
  232. { \
  233. Pixel = (r << 24) | (g << 16) | (b << 8) | a; \
  234. }
  235. #define ABGR8888_FROM_RGBA(Pixel, r, g, b, a) \
  236. { \
  237. Pixel = (a << 24) | (b << 16) | (g << 8) | r; \
  238. }
  239. #define BGRA8888_FROM_RGBA(Pixel, r, g, b, a) \
  240. { \
  241. Pixel = (b << 24) | (g << 16) | (r << 8) | a; \
  242. }
  243. #define ARGB2101010_FROM_RGBA(Pixel, r, g, b, a) \
  244. { \
  245. r = r ? ((r << 2) | 0x3) : 0; \
  246. g = g ? ((g << 2) | 0x3) : 0; \
  247. b = b ? ((b << 2) | 0x3) : 0; \
  248. a = (a * 3) / 255; \
  249. Pixel = (a << 30) | (r << 20) | (g << 10) | b; \
  250. }
  251. #define ARGB2101010_FROM_RGBAFLOAT(Pixel, r, g, b, a) \
  252. { \
  253. r = SDL_clamp(r, 0.0f, 1.0f) * 1023.0f; \
  254. g = SDL_clamp(g, 0.0f, 1.0f) * 1023.0f; \
  255. b = SDL_clamp(b, 0.0f, 1.0f) * 1023.0f; \
  256. a = SDL_clamp(a, 0.0f, 1.0f) * 3.0f; \
  257. Pixel = (((Uint32)SDL_roundf(a)) << 30) | \
  258. (((Uint32)SDL_roundf(r)) << 20) | \
  259. (((Uint32)SDL_roundf(g)) << 10) | \
  260. (Uint32)SDL_roundf(b); \
  261. }
  262. #define ABGR2101010_FROM_RGBA(Pixel, r, g, b, a) \
  263. { \
  264. r = r ? ((r << 2) | 0x3) : 0; \
  265. g = g ? ((g << 2) | 0x3) : 0; \
  266. b = b ? ((b << 2) | 0x3) : 0; \
  267. a = (a * 3) / 255; \
  268. Pixel = (a << 30) | (b << 20) | (g << 10) | r; \
  269. }
  270. #define ABGR2101010_FROM_RGBAFLOAT(Pixel, r, g, b, a) \
  271. { \
  272. r = SDL_clamp(r, 0.0f, 1.0f) * 1023.0f; \
  273. g = SDL_clamp(g, 0.0f, 1.0f) * 1023.0f; \
  274. b = SDL_clamp(b, 0.0f, 1.0f) * 1023.0f; \
  275. a = SDL_clamp(a, 0.0f, 1.0f) * 3.0f; \
  276. Pixel = (((Uint32)SDL_roundf(a)) << 30) | \
  277. (((Uint32)SDL_roundf(b)) << 20) | \
  278. (((Uint32)SDL_roundf(g)) << 10) | \
  279. (Uint32)SDL_roundf(r); \
  280. }
  281. #define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) \
  282. { \
  283. switch (bpp) { \
  284. case 1: \
  285. { \
  286. Uint8 _pixel; \
  287. \
  288. PIXEL_FROM_RGB(_pixel, fmt, r, g, b); \
  289. *((Uint8 *)(buf)) = _pixel; \
  290. } break; \
  291. \
  292. case 2: \
  293. { \
  294. Uint16 _pixel; \
  295. \
  296. PIXEL_FROM_RGB(_pixel, fmt, r, g, b); \
  297. *((Uint16 *)(buf)) = _pixel; \
  298. } break; \
  299. \
  300. case 3: \
  301. { \
  302. if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
  303. *((buf) + fmt->Rshift / 8) = r; \
  304. *((buf) + fmt->Gshift / 8) = g; \
  305. *((buf) + fmt->Bshift / 8) = b; \
  306. } else { \
  307. *((buf) + 2 - fmt->Rshift / 8) = r; \
  308. *((buf) + 2 - fmt->Gshift / 8) = g; \
  309. *((buf) + 2 - fmt->Bshift / 8) = b; \
  310. } \
  311. } break; \
  312. \
  313. case 4: \
  314. { \
  315. Uint32 _pixel; \
  316. \
  317. PIXEL_FROM_RGB(_pixel, fmt, r, g, b); \
  318. *((Uint32 *)(buf)) = _pixel; \
  319. } break; \
  320. } \
  321. }
  322. // FIXME: Should we rescale alpha into 0..255 here?
  323. #define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a) \
  324. { \
  325. r = SDL_expand_byte[fmt->Rbits][((Pixel & fmt->Rmask) >> fmt->Rshift)]; \
  326. g = SDL_expand_byte[fmt->Gbits][((Pixel & fmt->Gmask) >> fmt->Gshift)]; \
  327. b = SDL_expand_byte[fmt->Bbits][((Pixel & fmt->Bmask) >> fmt->Bshift)]; \
  328. a = SDL_expand_byte[fmt->Abits][((Pixel & fmt->Amask) >> fmt->Ashift)]; \
  329. }
  330. #define RGBA_FROM_8888(Pixel, fmt, r, g, b, a) \
  331. { \
  332. r = (Pixel & fmt->Rmask) >> fmt->Rshift; \
  333. g = (Pixel & fmt->Gmask) >> fmt->Gshift; \
  334. b = (Pixel & fmt->Bmask) >> fmt->Bshift; \
  335. a = (Pixel & fmt->Amask) >> fmt->Ashift; \
  336. }
  337. #define RGBA_FROM_RGBA8888(Pixel, r, g, b, a) \
  338. { \
  339. r = (Pixel >> 24); \
  340. g = ((Pixel >> 16) & 0xFF); \
  341. b = ((Pixel >> 8) & 0xFF); \
  342. a = (Pixel & 0xFF); \
  343. }
  344. #define RGBA_FROM_ARGB8888(Pixel, r, g, b, a) \
  345. { \
  346. r = ((Pixel >> 16) & 0xFF); \
  347. g = ((Pixel >> 8) & 0xFF); \
  348. b = (Pixel & 0xFF); \
  349. a = (Pixel >> 24); \
  350. }
  351. #define RGBA_FROM_ABGR8888(Pixel, r, g, b, a) \
  352. { \
  353. r = (Pixel & 0xFF); \
  354. g = ((Pixel >> 8) & 0xFF); \
  355. b = ((Pixel >> 16) & 0xFF); \
  356. a = (Pixel >> 24); \
  357. }
  358. #define RGBA_FROM_BGRA8888(Pixel, r, g, b, a) \
  359. { \
  360. r = ((Pixel >> 8) & 0xFF); \
  361. g = ((Pixel >> 16) & 0xFF); \
  362. b = (Pixel >> 24); \
  363. a = (Pixel & 0xFF); \
  364. }
  365. #define RGBA_FROM_ARGB2101010(Pixel, r, g, b, a) \
  366. { \
  367. r = ((Pixel >> 22) & 0xFF); \
  368. g = ((Pixel >> 12) & 0xFF); \
  369. b = ((Pixel >> 2) & 0xFF); \
  370. a = SDL_expand_byte[2][(Pixel >> 30)]; \
  371. }
  372. #define RGBAFLOAT_FROM_ARGB2101010(Pixel, r, g, b, a) \
  373. { \
  374. r = (float)((Pixel >> 20) & 0x3FF) / 1023.0f; \
  375. g = (float)((Pixel >> 10) & 0x3FF) / 1023.0f; \
  376. b = (float)((Pixel >> 0) & 0x3FF) / 1023.0f; \
  377. a = (float)(Pixel >> 30) / 3.0f; \
  378. }
  379. #define RGBA_FROM_ABGR2101010(Pixel, r, g, b, a) \
  380. { \
  381. r = ((Pixel >> 2) & 0xFF); \
  382. g = ((Pixel >> 12) & 0xFF); \
  383. b = ((Pixel >> 22) & 0xFF); \
  384. a = SDL_expand_byte[2][(Pixel >> 30)]; \
  385. }
  386. #define RGBAFLOAT_FROM_ABGR2101010(Pixel, r, g, b, a) \
  387. { \
  388. r = (float)((Pixel >> 0) & 0x3FF) / 1023.0f; \
  389. g = (float)((Pixel >> 10) & 0x3FF) / 1023.0f; \
  390. b = (float)((Pixel >> 20) & 0x3FF) / 1023.0f; \
  391. a = (float)(Pixel >> 30) / 3.0f; \
  392. }
  393. #define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a) \
  394. do { \
  395. switch (bpp) { \
  396. case 1: \
  397. Pixel = *((Uint8 *)(buf)); \
  398. RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \
  399. break; \
  400. \
  401. case 2: \
  402. Pixel = *((Uint16 *)(buf)); \
  403. RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \
  404. break; \
  405. \
  406. case 3: \
  407. { \
  408. Pixel = 0; \
  409. if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
  410. r = *((buf) + fmt->Rshift / 8); \
  411. g = *((buf) + fmt->Gshift / 8); \
  412. b = *((buf) + fmt->Bshift / 8); \
  413. } else { \
  414. r = *((buf) + 2 - fmt->Rshift / 8); \
  415. g = *((buf) + 2 - fmt->Gshift / 8); \
  416. b = *((buf) + 2 - fmt->Bshift / 8); \
  417. } \
  418. a = 0xFF; \
  419. } break; \
  420. \
  421. case 4: \
  422. Pixel = *((Uint32 *)(buf)); \
  423. RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a); \
  424. break; \
  425. \
  426. default: \
  427. /* stop gcc complaints */ \
  428. Pixel = 0; \
  429. r = g = b = a = 0; \
  430. break; \
  431. } \
  432. } while (0)
  433. // FIXME: this isn't correct, especially for Alpha (maximum != 255)
  434. #define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a) \
  435. { \
  436. Pixel = ((r >> (8 - fmt->Rbits)) << fmt->Rshift) | \
  437. ((g >> (8 - fmt->Gbits)) << fmt->Gshift) | \
  438. ((b >> (8 - fmt->Bbits)) << fmt->Bshift) | \
  439. ((a >> (8 - fmt->Abits)) << fmt->Ashift); \
  440. }
  441. #define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a) \
  442. { \
  443. switch (bpp) { \
  444. case 1: \
  445. { \
  446. Uint8 _pixel; \
  447. \
  448. PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a); \
  449. *((Uint8 *)(buf)) = _pixel; \
  450. } break; \
  451. \
  452. case 2: \
  453. { \
  454. Uint16 _pixel; \
  455. \
  456. PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a); \
  457. *((Uint16 *)(buf)) = _pixel; \
  458. } break; \
  459. \
  460. case 3: \
  461. { \
  462. if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { \
  463. *((buf) + fmt->Rshift / 8) = r; \
  464. *((buf) + fmt->Gshift / 8) = g; \
  465. *((buf) + fmt->Bshift / 8) = b; \
  466. } else { \
  467. *((buf) + 2 - fmt->Rshift / 8) = r; \
  468. *((buf) + 2 - fmt->Gshift / 8) = g; \
  469. *((buf) + 2 - fmt->Bshift / 8) = b; \
  470. } \
  471. } break; \
  472. \
  473. case 4: \
  474. { \
  475. Uint32 _pixel; \
  476. \
  477. PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a); \
  478. *((Uint32 *)(buf)) = _pixel; \
  479. } break; \
  480. } \
  481. }
  482. // Convert any 32-bit 4-bpp pixel to ARGB format
  483. #define PIXEL_TO_ARGB_PIXEL(src, srcfmt, dst) \
  484. do { \
  485. Uint8 a, r, g, b; \
  486. RGBA_FROM_PIXEL(src, srcfmt, r, g, b, a); \
  487. dst = a << 24 | r << 16 | g << 8 | b; \
  488. } while (0)
  489. // Blend a single color channel or alpha value
  490. /* dC = ((sC * sA) + (dC * (255 - sA))) / 255 */
  491. #define ALPHA_BLEND_CHANNEL(sC, dC, sA) \
  492. do { \
  493. Uint16 x; \
  494. x = ((sC - dC) * sA) + ((dC << 8) - dC); \
  495. x += 0x1U; \
  496. x += x >> 8; \
  497. dC = x >> 8; \
  498. } while (0)
  499. // Perform a division by 255 after a multiplication of two 8-bit color channels
  500. /* out = (sC * dC) / 255 */
  501. #define MULT_DIV_255(sC, dC, out) \
  502. do { \
  503. Uint16 x = sC * dC; \
  504. x += 0x1U; \
  505. x += x >> 8; \
  506. out = x >> 8; \
  507. } while (0)
  508. // Blend the RGB values of two pixels with an alpha value
  509. #define ALPHA_BLEND_RGB(sR, sG, sB, A, dR, dG, dB) \
  510. do { \
  511. ALPHA_BLEND_CHANNEL(sR, dR, A); \
  512. ALPHA_BLEND_CHANNEL(sG, dG, A); \
  513. ALPHA_BLEND_CHANNEL(sB, dB, A); \
  514. } while (0)
  515. // Blend two 8888 pixels with the same format
  516. /* Calculates dst = ((src * factor) + (dst * (255 - factor))) / 255 */
  517. // FIXME: SDL_SIZE_MAX might not be an integer literal
  518. #if defined(SIZE_MAX) && (SIZE_MAX == 0xffffffffffffffff)
  519. #define FACTOR_BLEND_8888(src, dst, factor) \
  520. do { \
  521. Uint64 src64 = src; \
  522. src64 = (src64 | (src64 << 24)) & 0x00FF00FF00FF00FF; \
  523. \
  524. Uint64 dst64 = dst; \
  525. dst64 = (dst64 | (dst64 << 24)) & 0x00FF00FF00FF00FF; \
  526. \
  527. dst64 = ((src64 - dst64) * factor) + (dst64 << 8) - dst64; \
  528. dst64 += 0x0001000100010001; \
  529. dst64 += (dst64 >> 8) & 0x00FF00FF00FF00FF; \
  530. dst64 &= 0xFF00FF00FF00FF00; \
  531. \
  532. dst = (Uint32)((dst64 >> 8) | (dst64 >> 32)); \
  533. } while (0)
  534. #else
  535. #define FACTOR_BLEND_8888(src, dst, factor) \
  536. do { \
  537. Uint32 src02 = src & 0x00FF00FF; \
  538. Uint32 dst02 = dst & 0x00FF00FF; \
  539. \
  540. Uint32 src13 = (src >> 8) & 0x00FF00FF; \
  541. Uint32 dst13 = (dst >> 8) & 0x00FF00FF; \
  542. \
  543. Uint32 res02 = ((src02 - dst02) * factor) + (dst02 << 8) - dst02; \
  544. res02 += 0x00010001; \
  545. res02 += (res02 >> 8) & 0x00FF00FF; \
  546. res02 = (res02 >> 8) & 0x00FF00FF; \
  547. \
  548. Uint32 res13 = ((src13 - dst13) * factor) + (dst13 << 8) - dst13; \
  549. res13 += 0x00010001; \
  550. res13 += (res13 >> 8) & 0x00FF00FF; \
  551. res13 &= 0xFF00FF00; \
  552. dst = res02 | res13; \
  553. } while (0)
  554. #endif
  555. // Alpha blend two 8888 pixels with the same formats.
  556. #define ALPHA_BLEND_8888(src, dst, fmt) \
  557. do { \
  558. Uint32 srcA = (src >> fmt->Ashift) & 0xFF; \
  559. Uint32 tmp = src | fmt->Amask; \
  560. FACTOR_BLEND_8888(tmp, dst, srcA); \
  561. } while (0)
  562. // Alpha blend two 8888 pixels with differing formats.
  563. #define ALPHA_BLEND_SWIZZLE_8888(src, dst, srcfmt, dstfmt) \
  564. do { \
  565. Uint32 srcA = (src >> srcfmt->Ashift) & 0xFF; \
  566. Uint32 tmp = (((src >> srcfmt->Rshift) & 0xFF) << dstfmt->Rshift) | \
  567. (((src >> srcfmt->Gshift) & 0xFF) << dstfmt->Gshift) | \
  568. (((src >> srcfmt->Bshift) & 0xFF) << dstfmt->Bshift) | \
  569. dstfmt->Amask; \
  570. FACTOR_BLEND_8888(tmp, dst, srcA); \
  571. } while (0)
  572. // Blend the RGBA values of two pixels
  573. #define ALPHA_BLEND_RGBA(sR, sG, sB, sA, dR, dG, dB, dA) \
  574. do { \
  575. ALPHA_BLEND_CHANNEL(sR, dR, sA); \
  576. ALPHA_BLEND_CHANNEL(sG, dG, sA); \
  577. ALPHA_BLEND_CHANNEL(sB, dB, sA); \
  578. ALPHA_BLEND_CHANNEL(255, dA, sA); \
  579. } while (0)
  580. // This is a very useful loop for optimizing blitters
  581. #if defined(_MSC_VER) && (_MSC_VER == 1300)
  582. // There's a bug in the Visual C++ 7 optimizer when compiling this code
  583. #else
  584. #define USE_DUFFS_LOOP
  585. #endif
  586. #define DUFFS_LOOP1(pixel_copy_increment, width) \
  587. { \
  588. int n; \
  589. for (n = width; n > 0; --n) { \
  590. pixel_copy_increment; \
  591. } \
  592. }
  593. #ifdef USE_DUFFS_LOOP
  594. // 8-times unrolled loop
  595. #define DUFFS_LOOP8(pixel_copy_increment, width) \
  596. { \
  597. int n = (width + 7) / 8; \
  598. switch (width & 7) { \
  599. case 0: \
  600. do { \
  601. pixel_copy_increment; \
  602. SDL_FALLTHROUGH; \
  603. case 7: \
  604. pixel_copy_increment; \
  605. SDL_FALLTHROUGH; \
  606. case 6: \
  607. pixel_copy_increment; \
  608. SDL_FALLTHROUGH; \
  609. case 5: \
  610. pixel_copy_increment; \
  611. SDL_FALLTHROUGH; \
  612. case 4: \
  613. pixel_copy_increment; \
  614. SDL_FALLTHROUGH; \
  615. case 3: \
  616. pixel_copy_increment; \
  617. SDL_FALLTHROUGH; \
  618. case 2: \
  619. pixel_copy_increment; \
  620. SDL_FALLTHROUGH; \
  621. case 1: \
  622. pixel_copy_increment; \
  623. } while (--n > 0); \
  624. } \
  625. }
  626. // 4-times unrolled loop
  627. #define DUFFS_LOOP4(pixel_copy_increment, width) \
  628. { \
  629. int n = (width + 3) / 4; \
  630. switch (width & 3) { \
  631. case 0: \
  632. do { \
  633. pixel_copy_increment; \
  634. SDL_FALLTHROUGH; \
  635. case 3: \
  636. pixel_copy_increment; \
  637. SDL_FALLTHROUGH; \
  638. case 2: \
  639. pixel_copy_increment; \
  640. SDL_FALLTHROUGH; \
  641. case 1: \
  642. pixel_copy_increment; \
  643. } while (--n > 0); \
  644. } \
  645. }
  646. // 2-times unrolled loop
  647. #define DUFFS_LOOP2(pixel_copy_increment, width) \
  648. { \
  649. int n = (width + 1) / 2; \
  650. switch (width & 1) { \
  651. case 0: \
  652. do { \
  653. pixel_copy_increment; \
  654. SDL_FALLTHROUGH; \
  655. case 1: \
  656. pixel_copy_increment; \
  657. } while (--n > 0); \
  658. } \
  659. }
  660. // Use the 4-times version of the loop by default
  661. #define DUFFS_LOOP(pixel_copy_increment, width) \
  662. DUFFS_LOOP4(pixel_copy_increment, width)
  663. // Use the 8-times version of the loop for simple routines
  664. #define DUFFS_LOOP_TRIVIAL(pixel_copy_increment, width) \
  665. DUFFS_LOOP8(pixel_copy_increment, width)
  666. // Special version of Duff's device for even more optimization
  667. #define DUFFS_LOOP_124(pixel_copy_increment1, \
  668. pixel_copy_increment2, \
  669. pixel_copy_increment4, width) \
  670. { \
  671. int n = width; \
  672. if (n & 1) { \
  673. pixel_copy_increment1; \
  674. n -= 1; \
  675. } \
  676. if (n & 2) { \
  677. pixel_copy_increment2; \
  678. n -= 2; \
  679. } \
  680. if (n & 4) { \
  681. pixel_copy_increment4; \
  682. n -= 4; \
  683. } \
  684. if (n) { \
  685. n /= 8; \
  686. do { \
  687. pixel_copy_increment4; \
  688. pixel_copy_increment4; \
  689. } while (--n > 0); \
  690. } \
  691. }
  692. #else
  693. // Don't use Duff's device to unroll loops
  694. #define DUFFS_LOOP(pixel_copy_increment, width) \
  695. DUFFS_LOOP1(pixel_copy_increment, width)
  696. #define DUFFS_LOOP_TRIVIAL(pixel_copy_increment, width) \
  697. DUFFS_LOOP1(pixel_copy_increment, width)
  698. #define DUFFS_LOOP8(pixel_copy_increment, width) \
  699. DUFFS_LOOP1(pixel_copy_increment, width)
  700. #define DUFFS_LOOP4(pixel_copy_increment, width) \
  701. DUFFS_LOOP1(pixel_copy_increment, width)
  702. #define DUFFS_LOOP2(pixel_copy_increment, width) \
  703. DUFFS_LOOP1(pixel_copy_increment, width)
  704. #define DUFFS_LOOP_124(pixel_copy_increment1, \
  705. pixel_copy_increment2, \
  706. pixel_copy_increment4, width) \
  707. DUFFS_LOOP1(pixel_copy_increment1, width)
  708. #endif // USE_DUFFS_LOOP
  709. #if defined(_MSC_VER) && (_MSC_VER >= 600)
  710. #pragma warning(disable : 4244) // '=': conversion from 'X' to 'Y', possible loss of data
  711. #endif
  712. #endif // SDL_blit_h_