SDL_blit_1.c 13 KB

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