SDL_blit_1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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. #if ( SDL_BYTEORDER == SDL_LIL_ENDIAN )
  65. #define HI 1
  66. #define LO 0
  67. #else /* ( SDL_BYTEORDER == SDL_BIG_ENDIAN ) */
  68. #define HI 0
  69. #define LO 1
  70. #endif
  71. static void
  72. Blit1to2(SDL_BlitInfo * info)
  73. {
  74. #ifndef USE_DUFFS_LOOP
  75. int c;
  76. #endif
  77. int width, height;
  78. Uint8 *src, *dst;
  79. Uint16 *map;
  80. int srcskip, dstskip;
  81. /* Set up some basic variables */
  82. width = info->dst_w;
  83. height = info->dst_h;
  84. src = info->src;
  85. srcskip = info->src_skip;
  86. dst = info->dst;
  87. dstskip = info->dst_skip;
  88. map = (Uint16 *) info->table;
  89. #ifdef USE_DUFFS_LOOP
  90. while (height--) {
  91. /* *INDENT-OFF* */
  92. DUFFS_LOOP(
  93. {
  94. *(Uint16 *)dst = map[*src++];
  95. dst += 2;
  96. },
  97. width);
  98. /* *INDENT-ON* */
  99. src += srcskip;
  100. dst += dstskip;
  101. }
  102. #else
  103. /* Memory align at 4-byte boundary, if necessary */
  104. if ((long) dst & 0x03) {
  105. /* Don't do anything if width is 0 */
  106. if (width == 0) {
  107. return;
  108. }
  109. --width;
  110. while (height--) {
  111. /* Perform copy alignment */
  112. *(Uint16 *) dst = map[*src++];
  113. dst += 2;
  114. /* Copy in 4 pixel chunks */
  115. for (c = width / 4; c; --c) {
  116. *(Uint32 *) dst = (map[src[HI]] << 16) | (map[src[LO]]);
  117. src += 2;
  118. dst += 4;
  119. *(Uint32 *) dst = (map[src[HI]] << 16) | (map[src[LO]]);
  120. src += 2;
  121. dst += 4;
  122. }
  123. /* Get any leftovers */
  124. switch (width & 3) {
  125. case 3:
  126. *(Uint16 *) dst = map[*src++];
  127. dst += 2;
  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. case 2:
  158. *(Uint32 *) dst = (map[src[HI]] << 16) | (map[src[LO]]);
  159. src += 2;
  160. dst += 4;
  161. break;
  162. case 1:
  163. *(Uint16 *) dst = map[*src++];
  164. dst += 2;
  165. break;
  166. }
  167. src += srcskip;
  168. dst += dstskip;
  169. }
  170. }
  171. #endif /* USE_DUFFS_LOOP */
  172. }
  173. static void
  174. 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* */
  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* */
  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
  220. Blit1to4(SDL_BlitInfo * info)
  221. {
  222. #ifndef USE_DUFFS_LOOP
  223. int c;
  224. #endif
  225. int width, height;
  226. Uint8 *src;
  227. Uint32 *map, *dst;
  228. int srcskip, dstskip;
  229. /* Set up some basic variables */
  230. width = info->dst_w;
  231. height = info->dst_h;
  232. src = info->src;
  233. srcskip = info->src_skip;
  234. dst = (Uint32 *) info->dst;
  235. dstskip = info->dst_skip / 4;
  236. map = (Uint32 *) info->table;
  237. while (height--) {
  238. #ifdef USE_DUFFS_LOOP
  239. /* *INDENT-OFF* */
  240. DUFFS_LOOP(
  241. *dst++ = map[*src++];
  242. , width);
  243. /* *INDENT-ON* */
  244. #else
  245. for (c = width / 4; c; --c) {
  246. *dst++ = map[*src++];
  247. *dst++ = map[*src++];
  248. *dst++ = map[*src++];
  249. *dst++ = map[*src++];
  250. }
  251. switch (width & 3) {
  252. case 3:
  253. *dst++ = map[*src++];
  254. case 2:
  255. *dst++ = map[*src++];
  256. case 1:
  257. *dst++ = map[*src++];
  258. }
  259. #endif /* USE_DUFFS_LOOP */
  260. src += srcskip;
  261. dst += dstskip;
  262. }
  263. }
  264. static void
  265. 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* */
  278. DUFFS_LOOP(
  279. {
  280. if ( *src != ckey ) {
  281. *dst = palmap[*src];
  282. }
  283. dst++;
  284. src++;
  285. },
  286. width);
  287. /* *INDENT-ON* */
  288. src += srcskip;
  289. dst += dstskip;
  290. }
  291. } else {
  292. while (height--) {
  293. /* *INDENT-OFF* */
  294. DUFFS_LOOP(
  295. {
  296. if ( *src != ckey ) {
  297. *dst = *src;
  298. }
  299. dst++;
  300. src++;
  301. },
  302. width);
  303. /* *INDENT-ON* */
  304. src += srcskip;
  305. dst += dstskip;
  306. }
  307. }
  308. }
  309. static void
  310. Blit1to2Key(SDL_BlitInfo * info)
  311. {
  312. int width = info->dst_w;
  313. int height = info->dst_h;
  314. Uint8 *src = info->src;
  315. int srcskip = info->src_skip;
  316. Uint16 *dstp = (Uint16 *) info->dst;
  317. int dstskip = info->dst_skip;
  318. Uint16 *palmap = (Uint16 *) info->table;
  319. Uint32 ckey = info->colorkey;
  320. /* Set up some basic variables */
  321. dstskip /= 2;
  322. while (height--) {
  323. /* *INDENT-OFF* */
  324. DUFFS_LOOP(
  325. {
  326. if ( *src != ckey ) {
  327. *dstp=palmap[*src];
  328. }
  329. src++;
  330. dstp++;
  331. },
  332. width);
  333. /* *INDENT-ON* */
  334. src += srcskip;
  335. dstp += dstskip;
  336. }
  337. }
  338. static void
  339. Blit1to3Key(SDL_BlitInfo * info)
  340. {
  341. int width = info->dst_w;
  342. int height = info->dst_h;
  343. Uint8 *src = info->src;
  344. int srcskip = info->src_skip;
  345. Uint8 *dst = info->dst;
  346. int dstskip = info->dst_skip;
  347. Uint8 *palmap = info->table;
  348. Uint32 ckey = info->colorkey;
  349. int o;
  350. while (height--) {
  351. /* *INDENT-OFF* */
  352. DUFFS_LOOP(
  353. {
  354. if ( *src != ckey ) {
  355. o = *src * 4;
  356. dst[0] = palmap[o++];
  357. dst[1] = palmap[o++];
  358. dst[2] = palmap[o++];
  359. }
  360. src++;
  361. dst += 3;
  362. },
  363. width);
  364. /* *INDENT-ON* */
  365. src += srcskip;
  366. dst += dstskip;
  367. }
  368. }
  369. static void
  370. Blit1to4Key(SDL_BlitInfo * info)
  371. {
  372. int width = info->dst_w;
  373. int height = info->dst_h;
  374. Uint8 *src = info->src;
  375. int srcskip = info->src_skip;
  376. Uint32 *dstp = (Uint32 *) info->dst;
  377. int dstskip = info->dst_skip;
  378. Uint32 *palmap = (Uint32 *) info->table;
  379. Uint32 ckey = info->colorkey;
  380. /* Set up some basic variables */
  381. dstskip /= 4;
  382. while (height--) {
  383. /* *INDENT-OFF* */
  384. DUFFS_LOOP(
  385. {
  386. if ( *src != ckey ) {
  387. *dstp = palmap[*src];
  388. }
  389. src++;
  390. dstp++;
  391. },
  392. width);
  393. /* *INDENT-ON* */
  394. src += srcskip;
  395. dstp += dstskip;
  396. }
  397. }
  398. static void
  399. Blit1toNAlpha(SDL_BlitInfo * info)
  400. {
  401. int width = info->dst_w;
  402. int height = info->dst_h;
  403. Uint8 *src = info->src;
  404. int srcskip = info->src_skip;
  405. Uint8 *dst = info->dst;
  406. int dstskip = info->dst_skip;
  407. SDL_PixelFormat *dstfmt = info->dst_fmt;
  408. const SDL_Color *srcpal = info->src_fmt->palette->colors;
  409. int dstbpp;
  410. Uint32 pixel;
  411. unsigned sR, sG, sB;
  412. unsigned dR, dG, dB, dA;
  413. const unsigned A = info->a;
  414. /* Set up some basic variables */
  415. dstbpp = dstfmt->BytesPerPixel;
  416. while (height--) {
  417. /* *INDENT-OFF* */
  418. DUFFS_LOOP4(
  419. {
  420. sR = srcpal[*src].r;
  421. sG = srcpal[*src].g;
  422. sB = srcpal[*src].b;
  423. DISEMBLE_RGBA(dst, dstbpp, dstfmt, pixel, dR, dG, dB, dA);
  424. ALPHA_BLEND_RGBA(sR, sG, sB, A, dR, dG, dB, dA);
  425. ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
  426. src++;
  427. dst += dstbpp;
  428. },
  429. width);
  430. /* *INDENT-ON* */
  431. src += srcskip;
  432. dst += dstskip;
  433. }
  434. }
  435. static void
  436. Blit1toNAlphaKey(SDL_BlitInfo * info)
  437. {
  438. int width = info->dst_w;
  439. int height = info->dst_h;
  440. Uint8 *src = info->src;
  441. int srcskip = info->src_skip;
  442. Uint8 *dst = info->dst;
  443. int dstskip = info->dst_skip;
  444. SDL_PixelFormat *dstfmt = info->dst_fmt;
  445. const SDL_Color *srcpal = info->src_fmt->palette->colors;
  446. Uint32 ckey = info->colorkey;
  447. int dstbpp;
  448. Uint32 pixel;
  449. unsigned sR, sG, sB;
  450. unsigned dR, dG, dB, dA;
  451. const unsigned A = info->a;
  452. /* Set up some basic variables */
  453. dstbpp = dstfmt->BytesPerPixel;
  454. while (height--) {
  455. /* *INDENT-OFF* */
  456. DUFFS_LOOP(
  457. {
  458. if ( *src != ckey ) {
  459. sR = srcpal[*src].r;
  460. sG = srcpal[*src].g;
  461. sB = srcpal[*src].b;
  462. DISEMBLE_RGBA(dst, dstbpp, dstfmt, pixel, dR, dG, dB, dA);
  463. ALPHA_BLEND_RGBA(sR, sG, sB, A, dR, dG, dB, dA);
  464. ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
  465. }
  466. src++;
  467. dst += dstbpp;
  468. },
  469. width);
  470. /* *INDENT-ON* */
  471. src += srcskip;
  472. dst += dstskip;
  473. }
  474. }
  475. static const SDL_BlitFunc one_blit[] = {
  476. (SDL_BlitFunc) NULL, Blit1to1, Blit1to2, Blit1to3, Blit1to4
  477. };
  478. static const SDL_BlitFunc one_blitkey[] = {
  479. (SDL_BlitFunc) NULL, Blit1to1Key, Blit1to2Key, Blit1to3Key, Blit1to4Key
  480. };
  481. SDL_BlitFunc
  482. SDL_CalculateBlit1(SDL_Surface * surface)
  483. {
  484. int which;
  485. SDL_PixelFormat *dstfmt;
  486. dstfmt = surface->map->dst->format;
  487. if (dstfmt->BitsPerPixel < 8) {
  488. which = 0;
  489. } else {
  490. which = dstfmt->BytesPerPixel;
  491. }
  492. switch (surface->map->info.flags & ~SDL_COPY_RLE_MASK) {
  493. case 0:
  494. return one_blit[which];
  495. case SDL_COPY_COLORKEY:
  496. return one_blitkey[which];
  497. case SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
  498. /* Supporting 8bpp->8bpp alpha is doable but requires lots of
  499. tables which consume space and takes time to precompute,
  500. so is better left to the user */
  501. return which >= 2 ? Blit1toNAlpha : (SDL_BlitFunc) NULL;
  502. case SDL_COPY_COLORKEY | SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
  503. return which >= 2 ? Blit1toNAlphaKey : (SDL_BlitFunc) NULL;
  504. }
  505. return (SDL_BlitFunc) NULL;
  506. }
  507. /* vi: set ts=4 sw=4 expandtab: */