SDL_blit_0.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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_0
  20. #include "SDL_video.h"
  21. #include "SDL_blit.h"
  22. /* Functions to blit from bitmaps to other surfaces */
  23. static void
  24. BlitBto1(SDL_BlitInfo * info)
  25. {
  26. int c;
  27. int width, height;
  28. Uint8 *src, *map, *dst;
  29. int srcskip, dstskip;
  30. /* Set up some basic variables */
  31. width = info->dst_w;
  32. height = info->dst_h;
  33. src = info->src;
  34. srcskip = info->src_skip;
  35. dst = info->dst;
  36. dstskip = info->dst_skip;
  37. map = info->table;
  38. srcskip += width - (width + 7) / 8;
  39. if (map) {
  40. while (height--) {
  41. Uint8 byte = 0, bit;
  42. for (c = 0; c < width; ++c) {
  43. if ((c & 7) == 0) {
  44. byte = *src++;
  45. }
  46. bit = (byte & 0x80) >> 7;
  47. if (1) {
  48. *dst = map[bit];
  49. }
  50. dst++;
  51. byte <<= 1;
  52. }
  53. src += srcskip;
  54. dst += dstskip;
  55. }
  56. } else {
  57. while (height--) {
  58. Uint8 byte = 0, bit;
  59. for (c = 0; c < width; ++c) {
  60. if ((c & 7) == 0) {
  61. byte = *src++;
  62. }
  63. bit = (byte & 0x80) >> 7;
  64. if (1) {
  65. *dst = bit;
  66. }
  67. dst++;
  68. byte <<= 1;
  69. }
  70. src += srcskip;
  71. dst += dstskip;
  72. }
  73. }
  74. }
  75. static void
  76. BlitBto2(SDL_BlitInfo * info)
  77. {
  78. int c;
  79. int width, height;
  80. Uint8 *src;
  81. Uint16 *map, *dst;
  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 = (Uint16 *) info->dst;
  89. dstskip = info->dst_skip / 2;
  90. map = (Uint16 *) info->table;
  91. srcskip += width - (width + 7) / 8;
  92. while (height--) {
  93. Uint8 byte = 0, bit;
  94. for (c = 0; c < width; ++c) {
  95. if ((c & 7) == 0) {
  96. byte = *src++;
  97. }
  98. bit = (byte & 0x80) >> 7;
  99. if (1) {
  100. *dst = map[bit];
  101. }
  102. byte <<= 1;
  103. dst++;
  104. }
  105. src += srcskip;
  106. dst += dstskip;
  107. }
  108. }
  109. static void
  110. BlitBto3(SDL_BlitInfo * info)
  111. {
  112. int c, o;
  113. int width, height;
  114. Uint8 *src, *map, *dst;
  115. int srcskip, dstskip;
  116. /* Set up some basic variables */
  117. width = info->dst_w;
  118. height = info->dst_h;
  119. src = info->src;
  120. srcskip = info->src_skip;
  121. dst = info->dst;
  122. dstskip = info->dst_skip;
  123. map = info->table;
  124. srcskip += width - (width + 7) / 8;
  125. while (height--) {
  126. Uint8 byte = 0, bit;
  127. for (c = 0; c < width; ++c) {
  128. if ((c & 7) == 0) {
  129. byte = *src++;
  130. }
  131. bit = (byte & 0x80) >> 7;
  132. if (1) {
  133. o = bit * 4;
  134. dst[0] = map[o++];
  135. dst[1] = map[o++];
  136. dst[2] = map[o++];
  137. }
  138. byte <<= 1;
  139. dst += 3;
  140. }
  141. src += srcskip;
  142. dst += dstskip;
  143. }
  144. }
  145. static void
  146. BlitBto4(SDL_BlitInfo * info)
  147. {
  148. int width, height;
  149. Uint8 *src;
  150. Uint32 *map, *dst;
  151. int srcskip, dstskip;
  152. int c;
  153. /* Set up some basic variables */
  154. width = info->dst_w;
  155. height = info->dst_h;
  156. src = info->src;
  157. srcskip = info->src_skip;
  158. dst = (Uint32 *) info->dst;
  159. dstskip = info->dst_skip / 4;
  160. map = (Uint32 *) info->table;
  161. srcskip += width - (width + 7) / 8;
  162. while (height--) {
  163. Uint8 byte = 0, bit;
  164. for (c = 0; c < width; ++c) {
  165. if ((c & 7) == 0) {
  166. byte = *src++;
  167. }
  168. bit = (byte & 0x80) >> 7;
  169. if (1) {
  170. *dst = map[bit];
  171. }
  172. byte <<= 1;
  173. dst++;
  174. }
  175. src += srcskip;
  176. dst += dstskip;
  177. }
  178. }
  179. static void
  180. BlitBto1Key(SDL_BlitInfo * info)
  181. {
  182. int width = info->dst_w;
  183. int height = info->dst_h;
  184. Uint8 *src = info->src;
  185. Uint8 *dst = info->dst;
  186. int srcskip = info->src_skip;
  187. int dstskip = info->dst_skip;
  188. Uint32 ckey = info->colorkey;
  189. Uint8 *palmap = info->table;
  190. int c;
  191. /* Set up some basic variables */
  192. srcskip += width - (width + 7) / 8;
  193. if (palmap) {
  194. while (height--) {
  195. Uint8 byte = 0, bit;
  196. for (c = 0; c < width; ++c) {
  197. if ((c & 7) == 0) {
  198. byte = *src++;
  199. }
  200. bit = (byte & 0x80) >> 7;
  201. if (bit != ckey) {
  202. *dst = palmap[bit];
  203. }
  204. dst++;
  205. byte <<= 1;
  206. }
  207. src += srcskip;
  208. dst += dstskip;
  209. }
  210. } else {
  211. while (height--) {
  212. Uint8 byte = 0, bit;
  213. for (c = 0; c < width; ++c) {
  214. if ((c & 7) == 0) {
  215. byte = *src++;
  216. }
  217. bit = (byte & 0x80) >> 7;
  218. if (bit != ckey) {
  219. *dst = bit;
  220. }
  221. dst++;
  222. byte <<= 1;
  223. }
  224. src += srcskip;
  225. dst += dstskip;
  226. }
  227. }
  228. }
  229. static void
  230. BlitBto2Key(SDL_BlitInfo * info)
  231. {
  232. int width = info->dst_w;
  233. int height = info->dst_h;
  234. Uint8 *src = info->src;
  235. Uint16 *dstp = (Uint16 *) info->dst;
  236. int srcskip = info->src_skip;
  237. int dstskip = info->dst_skip;
  238. Uint32 ckey = info->colorkey;
  239. Uint8 *palmap = info->table;
  240. int c;
  241. /* Set up some basic variables */
  242. srcskip += width - (width + 7) / 8;
  243. dstskip /= 2;
  244. while (height--) {
  245. Uint8 byte = 0, bit;
  246. for (c = 0; c < width; ++c) {
  247. if ((c & 7) == 0) {
  248. byte = *src++;
  249. }
  250. bit = (byte & 0x80) >> 7;
  251. if (bit != ckey) {
  252. *dstp = ((Uint16 *) palmap)[bit];
  253. }
  254. byte <<= 1;
  255. dstp++;
  256. }
  257. src += srcskip;
  258. dstp += dstskip;
  259. }
  260. }
  261. static void
  262. BlitBto3Key(SDL_BlitInfo * info)
  263. {
  264. int width = info->dst_w;
  265. int height = info->dst_h;
  266. Uint8 *src = info->src;
  267. Uint8 *dst = info->dst;
  268. int srcskip = info->src_skip;
  269. int dstskip = info->dst_skip;
  270. Uint32 ckey = info->colorkey;
  271. Uint8 *palmap = info->table;
  272. int c;
  273. /* Set up some basic variables */
  274. srcskip += width - (width + 7) / 8;
  275. while (height--) {
  276. Uint8 byte = 0, bit;
  277. for (c = 0; c < width; ++c) {
  278. if ((c & 7) == 0) {
  279. byte = *src++;
  280. }
  281. bit = (byte & 0x80) >> 7;
  282. if (bit != ckey) {
  283. SDL_memcpy(dst, &palmap[bit * 4], 3);
  284. }
  285. byte <<= 1;
  286. dst += 3;
  287. }
  288. src += srcskip;
  289. dst += dstskip;
  290. }
  291. }
  292. static void
  293. BlitBto4Key(SDL_BlitInfo * info)
  294. {
  295. int width = info->dst_w;
  296. int height = info->dst_h;
  297. Uint8 *src = info->src;
  298. Uint32 *dstp = (Uint32 *) info->dst;
  299. int srcskip = info->src_skip;
  300. int dstskip = info->dst_skip;
  301. Uint32 ckey = info->colorkey;
  302. Uint8 *palmap = info->table;
  303. int c;
  304. /* Set up some basic variables */
  305. srcskip += width - (width + 7) / 8;
  306. dstskip /= 4;
  307. while (height--) {
  308. Uint8 byte = 0, bit;
  309. for (c = 0; c < width; ++c) {
  310. if ((c & 7) == 0) {
  311. byte = *src++;
  312. }
  313. bit = (byte & 0x80) >> 7;
  314. if (bit != ckey) {
  315. *dstp = ((Uint32 *) palmap)[bit];
  316. }
  317. byte <<= 1;
  318. dstp++;
  319. }
  320. src += srcskip;
  321. dstp += dstskip;
  322. }
  323. }
  324. static void
  325. BlitBtoNAlpha(SDL_BlitInfo * info)
  326. {
  327. int width = info->dst_w;
  328. int height = info->dst_h;
  329. Uint8 *src = info->src;
  330. Uint8 *dst = info->dst;
  331. int srcskip = info->src_skip;
  332. int dstskip = info->dst_skip;
  333. const SDL_Color *srcpal = info->src_fmt->palette->colors;
  334. SDL_PixelFormat *dstfmt = info->dst_fmt;
  335. int dstbpp;
  336. int c;
  337. Uint32 pixel;
  338. unsigned sR, sG, sB;
  339. unsigned dR, dG, dB, dA;
  340. const unsigned A = info->a;
  341. /* Set up some basic variables */
  342. dstbpp = dstfmt->BytesPerPixel;
  343. srcskip += width - (width + 7) / 8;
  344. while (height--) {
  345. Uint8 byte = 0, bit;
  346. for (c = 0; c < width; ++c) {
  347. if ((c & 7) == 0) {
  348. byte = *src++;
  349. }
  350. bit = (byte & 0x80) >> 7;
  351. if (1) {
  352. sR = srcpal[bit].r;
  353. sG = srcpal[bit].g;
  354. sB = srcpal[bit].b;
  355. DISEMBLE_RGBA(dst, dstbpp, dstfmt, pixel, dR, dG, dB, dA);
  356. ALPHA_BLEND_RGBA(sR, sG, sB, A, dR, dG, dB, dA);
  357. ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
  358. }
  359. byte <<= 1;
  360. dst += dstbpp;
  361. }
  362. src += srcskip;
  363. dst += dstskip;
  364. }
  365. }
  366. static void
  367. BlitBtoNAlphaKey(SDL_BlitInfo * info)
  368. {
  369. int width = info->dst_w;
  370. int height = info->dst_h;
  371. Uint8 *src = info->src;
  372. Uint8 *dst = info->dst;
  373. int srcskip = info->src_skip;
  374. int dstskip = info->dst_skip;
  375. SDL_PixelFormat *srcfmt = info->src_fmt;
  376. SDL_PixelFormat *dstfmt = info->dst_fmt;
  377. const SDL_Color *srcpal = srcfmt->palette->colors;
  378. int dstbpp;
  379. int c;
  380. Uint32 pixel;
  381. unsigned sR, sG, sB;
  382. unsigned dR, dG, dB, dA;
  383. const unsigned A = info->a;
  384. Uint32 ckey = info->colorkey;
  385. /* Set up some basic variables */
  386. dstbpp = dstfmt->BytesPerPixel;
  387. srcskip += width - (width + 7) / 8;
  388. while (height--) {
  389. Uint8 byte = 0, bit;
  390. for (c = 0; c < width; ++c) {
  391. if ((c & 7) == 0) {
  392. byte = *src++;
  393. }
  394. bit = (byte & 0x80) >> 7;
  395. if (bit != ckey) {
  396. sR = srcpal[bit].r;
  397. sG = srcpal[bit].g;
  398. sB = srcpal[bit].b;
  399. DISEMBLE_RGBA(dst, dstbpp, dstfmt, pixel, dR, dG, dB, dA);
  400. ALPHA_BLEND_RGBA(sR, sG, sB, A, dR, dG, dB, dA);
  401. ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
  402. }
  403. byte <<= 1;
  404. dst += dstbpp;
  405. }
  406. src += srcskip;
  407. dst += dstskip;
  408. }
  409. }
  410. static const SDL_BlitFunc bitmap_blit[] = {
  411. (SDL_BlitFunc) NULL, BlitBto1, BlitBto2, BlitBto3, BlitBto4
  412. };
  413. static const SDL_BlitFunc colorkey_blit[] = {
  414. (SDL_BlitFunc) NULL, BlitBto1Key, BlitBto2Key, BlitBto3Key, BlitBto4Key
  415. };
  416. SDL_BlitFunc
  417. SDL_CalculateBlit0(SDL_Surface * surface)
  418. {
  419. int which;
  420. if (surface->format->BitsPerPixel != 1) {
  421. /* We don't support sub 8-bit packed pixel modes */
  422. return (SDL_BlitFunc) NULL;
  423. }
  424. if (surface->map->dst->format->BitsPerPixel < 8) {
  425. which = 0;
  426. } else {
  427. which = surface->map->dst->format->BytesPerPixel;
  428. }
  429. switch (surface->map->info.flags & ~SDL_COPY_RLE_MASK) {
  430. case 0:
  431. return bitmap_blit[which];
  432. case SDL_COPY_COLORKEY:
  433. return colorkey_blit[which];
  434. case SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
  435. return which >= 2 ? BlitBtoNAlpha : (SDL_BlitFunc) NULL;
  436. case SDL_COPY_COLORKEY | SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
  437. return which >= 2 ? BlitBtoNAlphaKey : (SDL_BlitFunc) NULL;
  438. }
  439. return (SDL_BlitFunc) NULL;
  440. }
  441. #endif /* SDL_HAVE_BLIT_0 */
  442. /* vi: set ts=4 sw=4 expandtab: */