SDL_blendpoint.c 11 KB

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