SDL_blendpoint.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #if !SDL_RENDER_DISABLED
  20. #include "SDL_draw.h"
  21. #include "SDL_blendpoint.h"
  22. static int
  23. SDL_BlendPoint_RGB555(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode, Uint8 r,
  24. Uint8 g, Uint8 b, Uint8 a)
  25. {
  26. unsigned inva = 0xff - a;
  27. switch (blendMode) {
  28. case SDL_BLENDMODE_BLEND:
  29. DRAW_SETPIXELXY_BLEND_RGB555(x, y);
  30. break;
  31. case SDL_BLENDMODE_ADD:
  32. DRAW_SETPIXELXY_ADD_RGB555(x, y);
  33. break;
  34. case SDL_BLENDMODE_MOD:
  35. DRAW_SETPIXELXY_MOD_RGB555(x, y);
  36. break;
  37. default:
  38. DRAW_SETPIXELXY_RGB555(x, y);
  39. break;
  40. }
  41. return 0;
  42. }
  43. static int
  44. SDL_BlendPoint_RGB565(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode, Uint8 r,
  45. Uint8 g, Uint8 b, Uint8 a)
  46. {
  47. unsigned inva = 0xff - a;
  48. switch (blendMode) {
  49. case SDL_BLENDMODE_BLEND:
  50. DRAW_SETPIXELXY_BLEND_RGB565(x, y);
  51. break;
  52. case SDL_BLENDMODE_ADD:
  53. DRAW_SETPIXELXY_ADD_RGB565(x, y);
  54. break;
  55. case SDL_BLENDMODE_MOD:
  56. DRAW_SETPIXELXY_MOD_RGB565(x, y);
  57. break;
  58. default:
  59. DRAW_SETPIXELXY_RGB565(x, y);
  60. break;
  61. }
  62. return 0;
  63. }
  64. static int
  65. SDL_BlendPoint_RGB888(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode, Uint8 r,
  66. Uint8 g, Uint8 b, Uint8 a)
  67. {
  68. unsigned inva = 0xff - a;
  69. switch (blendMode) {
  70. case SDL_BLENDMODE_BLEND:
  71. DRAW_SETPIXELXY_BLEND_RGB888(x, y);
  72. break;
  73. case SDL_BLENDMODE_ADD:
  74. DRAW_SETPIXELXY_ADD_RGB888(x, y);
  75. break;
  76. case SDL_BLENDMODE_MOD:
  77. DRAW_SETPIXELXY_MOD_RGB888(x, y);
  78. break;
  79. default:
  80. DRAW_SETPIXELXY_RGB888(x, y);
  81. break;
  82. }
  83. return 0;
  84. }
  85. static int
  86. SDL_BlendPoint_ARGB8888(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode,
  87. Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  88. {
  89. unsigned inva = 0xff - a;
  90. switch (blendMode) {
  91. case SDL_BLENDMODE_BLEND:
  92. DRAW_SETPIXELXY_BLEND_ARGB8888(x, y);
  93. break;
  94. case SDL_BLENDMODE_ADD:
  95. DRAW_SETPIXELXY_ADD_ARGB8888(x, y);
  96. break;
  97. case SDL_BLENDMODE_MOD:
  98. DRAW_SETPIXELXY_MOD_ARGB8888(x, y);
  99. break;
  100. default:
  101. DRAW_SETPIXELXY_ARGB8888(x, y);
  102. break;
  103. }
  104. return 0;
  105. }
  106. static int
  107. SDL_BlendPoint_RGB(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode, Uint8 r,
  108. Uint8 g, Uint8 b, Uint8 a)
  109. {
  110. SDL_PixelFormat *fmt = dst->format;
  111. unsigned inva = 0xff - a;
  112. switch (fmt->BytesPerPixel) {
  113. case 2:
  114. switch (blendMode) {
  115. case SDL_BLENDMODE_BLEND:
  116. DRAW_SETPIXELXY2_BLEND_RGB(x, y);
  117. break;
  118. case SDL_BLENDMODE_ADD:
  119. DRAW_SETPIXELXY2_ADD_RGB(x, y);
  120. break;
  121. case SDL_BLENDMODE_MOD:
  122. DRAW_SETPIXELXY2_MOD_RGB(x, y);
  123. break;
  124. default:
  125. DRAW_SETPIXELXY2_RGB(x, y);
  126. break;
  127. }
  128. return 0;
  129. case 4:
  130. switch (blendMode) {
  131. case SDL_BLENDMODE_BLEND:
  132. DRAW_SETPIXELXY4_BLEND_RGB(x, y);
  133. break;
  134. case SDL_BLENDMODE_ADD:
  135. DRAW_SETPIXELXY4_ADD_RGB(x, y);
  136. break;
  137. case SDL_BLENDMODE_MOD:
  138. DRAW_SETPIXELXY4_MOD_RGB(x, y);
  139. break;
  140. default:
  141. DRAW_SETPIXELXY4_RGB(x, y);
  142. break;
  143. }
  144. return 0;
  145. default:
  146. return SDL_Unsupported();
  147. }
  148. }
  149. static int
  150. SDL_BlendPoint_RGBA(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode, Uint8 r,
  151. Uint8 g, Uint8 b, Uint8 a)
  152. {
  153. SDL_PixelFormat *fmt = dst->format;
  154. unsigned inva = 0xff - a;
  155. switch (fmt->BytesPerPixel) {
  156. case 4:
  157. switch (blendMode) {
  158. case SDL_BLENDMODE_BLEND:
  159. DRAW_SETPIXELXY4_BLEND_RGBA(x, y);
  160. break;
  161. case SDL_BLENDMODE_ADD:
  162. DRAW_SETPIXELXY4_ADD_RGBA(x, y);
  163. break;
  164. case SDL_BLENDMODE_MOD:
  165. DRAW_SETPIXELXY4_MOD_RGBA(x, y);
  166. break;
  167. default:
  168. DRAW_SETPIXELXY4_RGBA(x, y);
  169. break;
  170. }
  171. return 0;
  172. default:
  173. return SDL_Unsupported();
  174. }
  175. }
  176. int
  177. SDL_BlendPoint(SDL_Surface * dst, int x, int y, SDL_BlendMode blendMode, Uint8 r,
  178. Uint8 g, Uint8 b, Uint8 a)
  179. {
  180. if (!dst) {
  181. return SDL_SetError("Passed NULL destination surface");
  182. }
  183. /* This function doesn't work on surfaces < 8 bpp */
  184. if (dst->format->BitsPerPixel < 8) {
  185. return SDL_SetError("SDL_BlendPoint(): Unsupported surface format");
  186. }
  187. /* Perform clipping */
  188. if (x < dst->clip_rect.x || y < dst->clip_rect.y ||
  189. x >= (dst->clip_rect.x + dst->clip_rect.w) ||
  190. y >= (dst->clip_rect.y + dst->clip_rect.h)) {
  191. return 0;
  192. }
  193. if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
  194. r = DRAW_MUL(r, a);
  195. g = DRAW_MUL(g, a);
  196. b = DRAW_MUL(b, a);
  197. }
  198. switch (dst->format->BitsPerPixel) {
  199. case 15:
  200. switch (dst->format->Rmask) {
  201. case 0x7C00:
  202. return SDL_BlendPoint_RGB555(dst, x, y, blendMode, r, g, b, a);
  203. }
  204. break;
  205. case 16:
  206. switch (dst->format->Rmask) {
  207. case 0xF800:
  208. return SDL_BlendPoint_RGB565(dst, x, y, blendMode, r, g, b, a);
  209. }
  210. break;
  211. case 32:
  212. switch (dst->format->Rmask) {
  213. case 0x00FF0000:
  214. if (!dst->format->Amask) {
  215. return SDL_BlendPoint_RGB888(dst, x, y, blendMode, r, g, b, a);
  216. } else {
  217. return SDL_BlendPoint_ARGB8888(dst, x, y, blendMode, r, g, b, a);
  218. }
  219. /* break; -Wunreachable-code-break */
  220. }
  221. break;
  222. default:
  223. break;
  224. }
  225. if (!dst->format->Amask) {
  226. return SDL_BlendPoint_RGB(dst, x, y, blendMode, r, g, b, a);
  227. } else {
  228. return SDL_BlendPoint_RGBA(dst, x, y, blendMode, r, g, b, a);
  229. }
  230. }
  231. int
  232. SDL_BlendPoints(SDL_Surface * dst, const SDL_Point * points, int count,
  233. SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  234. {
  235. int minx, miny;
  236. int maxx, maxy;
  237. int i;
  238. int x, y;
  239. int (*func)(SDL_Surface * dst, int x, int y,
  240. SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a) = NULL;
  241. int status = 0;
  242. if (!dst) {
  243. return SDL_SetError("Passed NULL destination surface");
  244. }
  245. /* This function doesn't work on surfaces < 8 bpp */
  246. if (dst->format->BitsPerPixel < 8) {
  247. return SDL_SetError("SDL_BlendPoints(): Unsupported surface format");
  248. }
  249. if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
  250. r = DRAW_MUL(r, a);
  251. g = DRAW_MUL(g, a);
  252. b = DRAW_MUL(b, a);
  253. }
  254. /* FIXME: Does this function pointer slow things down significantly? */
  255. switch (dst->format->BitsPerPixel) {
  256. case 15:
  257. switch (dst->format->Rmask) {
  258. case 0x7C00:
  259. func = SDL_BlendPoint_RGB555;
  260. break;
  261. }
  262. break;
  263. case 16:
  264. switch (dst->format->Rmask) {
  265. case 0xF800:
  266. func = SDL_BlendPoint_RGB565;
  267. break;
  268. }
  269. break;
  270. case 32:
  271. switch (dst->format->Rmask) {
  272. case 0x00FF0000:
  273. if (!dst->format->Amask) {
  274. func = SDL_BlendPoint_RGB888;
  275. } else {
  276. func = SDL_BlendPoint_ARGB8888;
  277. }
  278. break;
  279. }
  280. break;
  281. default:
  282. break;
  283. }
  284. if (!func) {
  285. if (!dst->format->Amask) {
  286. func = SDL_BlendPoint_RGB;
  287. } else {
  288. func = SDL_BlendPoint_RGBA;
  289. }
  290. }
  291. minx = dst->clip_rect.x;
  292. maxx = dst->clip_rect.x + dst->clip_rect.w - 1;
  293. miny = dst->clip_rect.y;
  294. maxy = dst->clip_rect.y + dst->clip_rect.h - 1;
  295. for (i = 0; i < count; ++i) {
  296. x = points[i].x;
  297. y = points[i].y;
  298. if (x < minx || x > maxx || y < miny || y > maxy) {
  299. continue;
  300. }
  301. status = func(dst, x, y, blendMode, r, g, b, a);
  302. }
  303. return status;
  304. }
  305. #endif /* !SDL_RENDER_DISABLED */
  306. /* vi: set ts=4 sw=4 expandtab: */