/project/jni/sdl-1.3/src/video/SDL_blit_0.c

https://github.com/aichunyu/FFPlayer · C · 483 lines · 410 code · 40 blank · 33 comment · 75 complexity · 24c463e4ef447ca65ea2dcd942176b37 MD5 · raw file

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