SDL_blit_1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. #ifdef SDL_HAVE_BLIT_1
  20. #include "SDL_surface_c.h"
  21. #include "SDL_sysvideo.h"
  22. // Functions to blit from 8-bit surfaces to other surfaces
  23. static void Blit1to1(SDL_BlitInfo *info)
  24. {
  25. #ifndef USE_DUFFS_LOOP
  26. int c;
  27. #endif
  28. int width, height;
  29. Uint8 *src, *map, *dst;
  30. int srcskip, dstskip;
  31. // Set up some basic variables
  32. width = info->dst_w;
  33. height = info->dst_h;
  34. src = info->src;
  35. srcskip = info->src_skip;
  36. dst = info->dst;
  37. dstskip = info->dst_skip;
  38. map = info->table;
  39. while (height--) {
  40. #ifdef USE_DUFFS_LOOP
  41. /* *INDENT-OFF* */ // clang-format off
  42. DUFFS_LOOP_TRIVIAL(
  43. {
  44. *dst = map[*src];
  45. }
  46. dst++;
  47. src++;
  48. , width);
  49. /* *INDENT-ON* */ // clang-format on
  50. #else
  51. for (c = width; c; --c) {
  52. *dst = map[*src];
  53. dst++;
  54. src++;
  55. }
  56. #endif
  57. src += srcskip;
  58. dst += dstskip;
  59. }
  60. }
  61. // This is now endian dependent
  62. #ifndef USE_DUFFS_LOOP
  63. #if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
  64. #define HI 1
  65. #define LO 0
  66. #else // ( SDL_BYTEORDER == SDL_BIG_ENDIAN )
  67. #define HI 0
  68. #define LO 1
  69. #endif
  70. #endif
  71. static void Blit1to2(SDL_BlitInfo *info)
  72. {
  73. #ifndef USE_DUFFS_LOOP
  74. int c;
  75. #endif
  76. int width, height;
  77. Uint8 *src, *dst;
  78. Uint16 *map;
  79. int srcskip, dstskip;
  80. // Set up some basic variables
  81. width = info->dst_w;
  82. height = info->dst_h;
  83. src = info->src;
  84. srcskip = info->src_skip;
  85. dst = info->dst;
  86. dstskip = info->dst_skip;
  87. map = (Uint16 *)info->table;
  88. #ifdef USE_DUFFS_LOOP
  89. while (height--) {
  90. /* *INDENT-OFF* */ // clang-format off
  91. DUFFS_LOOP_TRIVIAL(
  92. {
  93. *(Uint16 *)dst = map[*src++];
  94. dst += 2;
  95. },
  96. width);
  97. /* *INDENT-ON* */ // clang-format on
  98. src += srcskip;
  99. dst += dstskip;
  100. }
  101. #else
  102. // Memory align at 4-byte boundary, if necessary
  103. if ((long)dst & 0x03) {
  104. // Don't do anything if width is 0
  105. if (width == 0) {
  106. return;
  107. }
  108. --width;
  109. while (height--) {
  110. // Perform copy alignment
  111. *(Uint16 *)dst = map[*src++];
  112. dst += 2;
  113. // Copy in 4 pixel chunks
  114. for (c = width / 4; c; --c) {
  115. *(Uint32 *)dst = (map[src[HI]] << 16) | (map[src[LO]]);
  116. src += 2;
  117. dst += 4;
  118. *(Uint32 *)dst = (map[src[HI]] << 16) | (map[src[LO]]);
  119. src += 2;
  120. dst += 4;
  121. }
  122. // Get any leftovers
  123. switch (width & 3) {
  124. case 3:
  125. *(Uint16 *)dst = map[*src++];
  126. dst += 2;
  127. SDL_FALLTHROUGH;
  128. case 2:
  129. *(Uint32 *)dst = (map[src[HI]] << 16) | (map[src[LO]]);
  130. src += 2;
  131. dst += 4;
  132. break;
  133. case 1:
  134. *(Uint16 *)dst = map[*src++];
  135. dst += 2;
  136. break;
  137. }
  138. src += srcskip;
  139. dst += dstskip;
  140. }
  141. } else {
  142. while (height--) {
  143. // Copy in 4 pixel chunks
  144. for (c = width / 4; c; --c) {
  145. *(Uint32 *)dst = (map[src[HI]] << 16) | (map[src[LO]]);
  146. src += 2;
  147. dst += 4;
  148. *(Uint32 *)dst = (map[src[HI]] << 16) | (map[src[LO]]);
  149. src += 2;
  150. dst += 4;
  151. }
  152. // Get any leftovers
  153. switch (width & 3) {
  154. case 3:
  155. *(Uint16 *)dst = map[*src++];
  156. dst += 2;
  157. SDL_FALLTHROUGH;
  158. case 2:
  159. *(Uint32 *)dst = (map[src[HI]] << 16) | (map[src[LO]]);
  160. src += 2;
  161. dst += 4;
  162. break;
  163. case 1:
  164. *(Uint16 *)dst = map[*src++];
  165. dst += 2;
  166. break;
  167. }
  168. src += srcskip;
  169. dst += dstskip;
  170. }
  171. }
  172. #endif // USE_DUFFS_LOOP
  173. }
  174. static void Blit1to3(SDL_BlitInfo *info)
  175. {
  176. #ifndef USE_DUFFS_LOOP
  177. int c;
  178. #endif
  179. int o;
  180. int width, height;
  181. Uint8 *src, *map, *dst;
  182. int srcskip, dstskip;
  183. // Set up some basic variables
  184. width = info->dst_w;
  185. height = info->dst_h;
  186. src = info->src;
  187. srcskip = info->src_skip;
  188. dst = info->dst;
  189. dstskip = info->dst_skip;
  190. map = info->table;
  191. while (height--) {
  192. #ifdef USE_DUFFS_LOOP
  193. /* *INDENT-OFF* */ // clang-format off
  194. DUFFS_LOOP(
  195. {
  196. o = *src * 4;
  197. dst[0] = map[o++];
  198. dst[1] = map[o++];
  199. dst[2] = map[o++];
  200. }
  201. src++;
  202. dst += 3;
  203. , width);
  204. /* *INDENT-ON* */ // clang-format on
  205. #else
  206. for (c = width; c; --c) {
  207. o = *src * 4;
  208. dst[0] = map[o++];
  209. dst[1] = map[o++];
  210. dst[2] = map[o++];
  211. src++;
  212. dst += 3;
  213. }
  214. #endif // USE_DUFFS_LOOP
  215. src += srcskip;
  216. dst += dstskip;
  217. }
  218. }
  219. static void Blit1to4(SDL_BlitInfo *info)
  220. {
  221. #ifndef USE_DUFFS_LOOP
  222. int c;
  223. #endif
  224. int width, height;
  225. Uint8 *src;
  226. Uint32 *map, *dst;
  227. int srcskip, dstskip;
  228. // Set up some basic variables
  229. width = info->dst_w;
  230. height = info->dst_h;
  231. src = info->src;
  232. srcskip = info->src_skip;
  233. dst = (Uint32 *)info->dst;
  234. dstskip = info->dst_skip / 4;
  235. map = (Uint32 *)info->table;
  236. while (height--) {
  237. #ifdef USE_DUFFS_LOOP
  238. /* *INDENT-OFF* */ // clang-format off
  239. DUFFS_LOOP_TRIVIAL(
  240. *dst++ = map[*src++];
  241. , width);
  242. /* *INDENT-ON* */ // clang-format on
  243. #else
  244. for (c = width / 4; c; --c) {
  245. *dst++ = map[*src++];
  246. *dst++ = map[*src++];
  247. *dst++ = map[*src++];
  248. *dst++ = map[*src++];
  249. }
  250. switch (width & 3) {
  251. case 3:
  252. *dst++ = map[*src++];
  253. SDL_FALLTHROUGH;
  254. case 2:
  255. *dst++ = map[*src++];
  256. SDL_FALLTHROUGH;
  257. case 1:
  258. *dst++ = map[*src++];
  259. }
  260. #endif // USE_DUFFS_LOOP
  261. src += srcskip;
  262. dst += dstskip;
  263. }
  264. }
  265. static void Blit1to1Key(SDL_BlitInfo *info)
  266. {
  267. int width = info->dst_w;
  268. int height = info->dst_h;
  269. Uint8 *src = info->src;
  270. int srcskip = info->src_skip;
  271. Uint8 *dst = info->dst;
  272. int dstskip = info->dst_skip;
  273. Uint8 *palmap = info->table;
  274. Uint32 ckey = info->colorkey;
  275. if (palmap) {
  276. while (height--) {
  277. /* *INDENT-OFF* */ // clang-format off
  278. DUFFS_LOOP_TRIVIAL(
  279. {
  280. if ( *src != ckey ) {
  281. *dst = palmap[*src];
  282. }
  283. dst++;
  284. src++;
  285. },
  286. width);
  287. /* *INDENT-ON* */ // clang-format on
  288. src += srcskip;
  289. dst += dstskip;
  290. }
  291. } else {
  292. while (height--) {
  293. /* *INDENT-OFF* */ // clang-format off
  294. DUFFS_LOOP_TRIVIAL(
  295. {
  296. if ( *src != ckey ) {
  297. *dst = *src;
  298. }
  299. dst++;
  300. src++;
  301. },
  302. width);
  303. /* *INDENT-ON* */ // clang-format on
  304. src += srcskip;
  305. dst += dstskip;
  306. }
  307. }
  308. }
  309. static void Blit1to2Key(SDL_BlitInfo *info)
  310. {
  311. int width = info->dst_w;
  312. int height = info->dst_h;
  313. Uint8 *src = info->src;
  314. int srcskip = info->src_skip;
  315. Uint16 *dstp = (Uint16 *)info->dst;
  316. int dstskip = info->dst_skip;
  317. Uint16 *palmap = (Uint16 *)info->table;
  318. Uint32 ckey = info->colorkey;
  319. // Set up some basic variables
  320. dstskip /= 2;
  321. while (height--) {
  322. /* *INDENT-OFF* */ // clang-format off
  323. DUFFS_LOOP_TRIVIAL(
  324. {
  325. if ( *src != ckey ) {
  326. *dstp=palmap[*src];
  327. }
  328. src++;
  329. dstp++;
  330. },
  331. width);
  332. /* *INDENT-ON* */ // clang-format on
  333. src += srcskip;
  334. dstp += dstskip;
  335. }
  336. }
  337. static void Blit1to3Key(SDL_BlitInfo *info)
  338. {
  339. int width = info->dst_w;
  340. int height = info->dst_h;
  341. Uint8 *src = info->src;
  342. int srcskip = info->src_skip;
  343. Uint8 *dst = info->dst;
  344. int dstskip = info->dst_skip;
  345. Uint8 *palmap = info->table;
  346. Uint32 ckey = info->colorkey;
  347. int o;
  348. while (height--) {
  349. /* *INDENT-OFF* */ // clang-format off
  350. DUFFS_LOOP(
  351. {
  352. if ( *src != ckey ) {
  353. o = *src * 4;
  354. dst[0] = palmap[o++];
  355. dst[1] = palmap[o++];
  356. dst[2] = palmap[o++];
  357. }
  358. src++;
  359. dst += 3;
  360. },
  361. width);
  362. /* *INDENT-ON* */ // clang-format on
  363. src += srcskip;
  364. dst += dstskip;
  365. }
  366. }
  367. static void Blit1to4Key(SDL_BlitInfo *info)
  368. {
  369. int width = info->dst_w;
  370. int height = info->dst_h;
  371. Uint8 *src = info->src;
  372. int srcskip = info->src_skip;
  373. Uint32 *dstp = (Uint32 *)info->dst;
  374. int dstskip = info->dst_skip;
  375. Uint32 *palmap = (Uint32 *)info->table;
  376. Uint32 ckey = info->colorkey;
  377. // Set up some basic variables
  378. dstskip /= 4;
  379. while (height--) {
  380. /* *INDENT-OFF* */ // clang-format off
  381. DUFFS_LOOP_TRIVIAL(
  382. {
  383. if ( *src != ckey ) {
  384. *dstp = palmap[*src];
  385. }
  386. src++;
  387. dstp++;
  388. },
  389. width);
  390. /* *INDENT-ON* */ // clang-format on
  391. src += srcskip;
  392. dstp += dstskip;
  393. }
  394. }
  395. static void Blit1toNAlpha(SDL_BlitInfo *info)
  396. {
  397. int width = info->dst_w;
  398. int height = info->dst_h;
  399. Uint8 *src = info->src;
  400. int srcskip = info->src_skip;
  401. Uint8 *dst = info->dst;
  402. int dstskip = info->dst_skip;
  403. const SDL_PixelFormatDetails *dstfmt = info->dst_fmt;
  404. const SDL_Color *srcpal = info->src_pal->colors;
  405. int dstbpp;
  406. Uint32 pixel;
  407. unsigned sR, sG, sB, sA;
  408. unsigned dR, dG, dB, dA;
  409. const unsigned A = info->a;
  410. // Set up some basic variables
  411. dstbpp = dstfmt->bytes_per_pixel;
  412. while (height--) {
  413. /* *INDENT-OFF* */ // clang-format off
  414. DUFFS_LOOP(
  415. {
  416. sR = srcpal[*src].r;
  417. sG = srcpal[*src].g;
  418. sB = srcpal[*src].b;
  419. sA = (srcpal[*src].a * A) / 255;
  420. DISEMBLE_RGBA(dst, dstbpp, dstfmt, pixel, dR, dG, dB, dA);
  421. ALPHA_BLEND_RGBA(sR, sG, sB, sA, dR, dG, dB, dA);
  422. ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
  423. src++;
  424. dst += dstbpp;
  425. },
  426. width);
  427. /* *INDENT-ON* */ // clang-format on
  428. src += srcskip;
  429. dst += dstskip;
  430. }
  431. }
  432. static void Blit1toNAlphaKey(SDL_BlitInfo *info)
  433. {
  434. int width = info->dst_w;
  435. int height = info->dst_h;
  436. Uint8 *src = info->src;
  437. int srcskip = info->src_skip;
  438. Uint8 *dst = info->dst;
  439. int dstskip = info->dst_skip;
  440. const SDL_PixelFormatDetails *dstfmt = info->dst_fmt;
  441. const SDL_Color *srcpal = info->src_pal->colors;
  442. Uint32 ckey = info->colorkey;
  443. int dstbpp;
  444. Uint32 pixel;
  445. unsigned sR, sG, sB, sA;
  446. unsigned dR, dG, dB, dA;
  447. const unsigned A = info->a;
  448. // Set up some basic variables
  449. dstbpp = dstfmt->bytes_per_pixel;
  450. while (height--) {
  451. /* *INDENT-OFF* */ // clang-format off
  452. DUFFS_LOOP(
  453. {
  454. if ( *src != ckey ) {
  455. sR = srcpal[*src].r;
  456. sG = srcpal[*src].g;
  457. sB = srcpal[*src].b;
  458. sA = (srcpal[*src].a * A) / 255;
  459. DISEMBLE_RGBA(dst, dstbpp, dstfmt, pixel, dR, dG, dB, dA);
  460. ALPHA_BLEND_RGBA(sR, sG, sB, sA, dR, dG, dB, dA);
  461. ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
  462. }
  463. src++;
  464. dst += dstbpp;
  465. },
  466. width);
  467. /* *INDENT-ON* */ // clang-format on
  468. src += srcskip;
  469. dst += dstskip;
  470. }
  471. }
  472. static const SDL_BlitFunc one_blit[] = {
  473. (SDL_BlitFunc)NULL, Blit1to1, Blit1to2, Blit1to3, Blit1to4
  474. };
  475. static const SDL_BlitFunc one_blitkey[] = {
  476. (SDL_BlitFunc)NULL, Blit1to1Key, Blit1to2Key, Blit1to3Key, Blit1to4Key
  477. };
  478. SDL_BlitFunc SDL_CalculateBlit1(SDL_Surface *surface)
  479. {
  480. int which;
  481. if (SDL_BITSPERPIXEL(surface->map.info.dst_fmt->format) < 8) {
  482. which = 0;
  483. } else {
  484. which = SDL_BYTESPERPIXEL(surface->map.info.dst_fmt->format);
  485. }
  486. switch (surface->map.info.flags & ~SDL_COPY_RLE_MASK) {
  487. case 0:
  488. if (which < SDL_arraysize(one_blit)) {
  489. return one_blit[which];
  490. }
  491. break;
  492. case SDL_COPY_COLORKEY:
  493. if (which < SDL_arraysize(one_blitkey)) {
  494. return one_blitkey[which];
  495. }
  496. break;
  497. case SDL_COPY_COLORKEY | SDL_COPY_BLEND: // this is not super-robust but handles a specific case we found sdl12-compat.
  498. if (surface->map.info.a == 255) {
  499. if (which < SDL_arraysize(one_blitkey)) {
  500. return one_blitkey[which];
  501. }
  502. } else {
  503. return which >= 2 ? Blit1toNAlphaKey : (SDL_BlitFunc)NULL;
  504. }
  505. break;
  506. case SDL_COPY_BLEND:
  507. case SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
  508. /* Supporting 8bpp->8bpp alpha is doable but requires lots of
  509. tables which consume space and takes time to precompute,
  510. so is better left to the user */
  511. return which >= 2 ? Blit1toNAlpha : (SDL_BlitFunc)NULL;
  512. case SDL_COPY_COLORKEY | SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
  513. return which >= 2 ? Blit1toNAlphaKey : (SDL_BlitFunc)NULL;
  514. }
  515. return (SDL_BlitFunc)NULL;
  516. }
  517. #endif // SDL_HAVE_BLIT_1