SDL_blit_1.c 12 KB

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