graphics.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2012-2018 K. Lange
  5. *
  6. * Generic Graphics library for ToaruOS
  7. */
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include <fcntl.h>
  13. #include <sys/ioctl.h>
  14. #ifndef NO_SSE
  15. #include <xmmintrin.h>
  16. #include <emmintrin.h>
  17. #endif
  18. #include <kernel/video.h>
  19. #include <toaru/graphics.h>
  20. static inline int32_t min(int32_t a, int32_t b) {
  21. return (a < b) ? a : b;
  22. }
  23. static inline int32_t max(int32_t a, int32_t b) {
  24. return (a > b) ? a : b;
  25. }
  26. static inline uint16_t min16(uint16_t a, uint16_t b) {
  27. return (a < b) ? a : b;
  28. }
  29. static inline uint16_t max16(uint16_t a, uint16_t b) {
  30. return (a > b) ? a : b;
  31. }
  32. static int _is_in_clip(gfx_context_t * ctx, int32_t y) {
  33. if (!ctx->clips) return 1;
  34. if (y < 0 || y >= ctx->clips_size) return 1;
  35. return ctx->clips[y];
  36. }
  37. void gfx_add_clip(gfx_context_t * ctx, int32_t x, int32_t y, int32_t w, int32_t h) {
  38. (void)x;
  39. (void)w; // TODO Horizontal clipping
  40. if (!ctx->clips) {
  41. ctx->clips = malloc(ctx->height);
  42. memset(ctx->clips, 0, ctx->height);
  43. ctx->clips_size = ctx->height;
  44. }
  45. for (int i = max(y,0); i < min(y+h,ctx->clips_size); ++i) {
  46. ctx->clips[i] = 1;
  47. }
  48. }
  49. void gfx_clear_clip(gfx_context_t * ctx) {
  50. if (ctx->clips) {
  51. memset(ctx->clips, 0, ctx->clips_size);
  52. }
  53. }
  54. void gfx_no_clip(gfx_context_t * ctx) {
  55. void * tmp = ctx->clips;
  56. if (!tmp) return;
  57. ctx->clips = NULL;
  58. free(tmp);
  59. }
  60. /* Pointer to graphics memory */
  61. void flip(gfx_context_t * ctx) {
  62. if (ctx->clips) {
  63. for (size_t i = 0; i < ctx->height; ++i) {
  64. if (_is_in_clip(ctx,i)) {
  65. memcpy(&ctx->buffer[i*GFX_S(ctx)], &ctx->backbuffer[i*GFX_S(ctx)], 4 * ctx->width);
  66. }
  67. }
  68. } else {
  69. memcpy(ctx->buffer, ctx->backbuffer, ctx->size);
  70. }
  71. }
  72. void clearbuffer(gfx_context_t * ctx) {
  73. memset(ctx->backbuffer, 0, ctx->size);
  74. }
  75. /* Deprecated */
  76. static int framebuffer_fd = 0;
  77. gfx_context_t * init_graphics_fullscreen() {
  78. gfx_context_t * out = malloc(sizeof(gfx_context_t));
  79. out->clips = NULL;
  80. if (!framebuffer_fd) {
  81. framebuffer_fd = open("/dev/fb0", 0, 0);
  82. }
  83. if (framebuffer_fd < 0) {
  84. /* oh shit */
  85. free(out);
  86. return NULL;
  87. }
  88. ioctl(framebuffer_fd, IO_VID_WIDTH, &out->width);
  89. ioctl(framebuffer_fd, IO_VID_HEIGHT, &out->height);
  90. ioctl(framebuffer_fd, IO_VID_DEPTH, &out->depth);
  91. ioctl(framebuffer_fd, IO_VID_STRIDE, &out->stride);
  92. ioctl(framebuffer_fd, IO_VID_ADDR, &out->buffer);
  93. ioctl(framebuffer_fd, IO_VID_SIGNAL, NULL);
  94. out->size = GFX_H(out) * GFX_S(out);
  95. out->backbuffer = out->buffer;
  96. return out;
  97. }
  98. uint32_t framebuffer_stride(void) {
  99. uint32_t stride;
  100. ioctl(framebuffer_fd, IO_VID_STRIDE, &stride);
  101. return stride;
  102. }
  103. gfx_context_t * init_graphics_fullscreen_double_buffer() {
  104. gfx_context_t * out = init_graphics_fullscreen();
  105. if (!out) return NULL;
  106. out->backbuffer = malloc(GFX_S(out) * GFX_H(out));
  107. return out;
  108. }
  109. gfx_context_t * init_graphics_subregion(gfx_context_t * base, int x, int y, int width, int height) {
  110. gfx_context_t * out = malloc(sizeof(gfx_context_t));
  111. out->clips = NULL;
  112. out->depth = 32;
  113. out->width = width;
  114. out->height = height;
  115. out->stride = base->stride;
  116. out->backbuffer = base->buffer + (base->stride * y) + x * 4;
  117. out->buffer = base->buffer + (base->stride * y) + x * 4;
  118. if (base->clips) {
  119. for (int _y = 0; _y < height; ++_y) {
  120. if (_is_in_clip(base, y + _y)) {
  121. gfx_add_clip(out,0,_y,width,1);
  122. }
  123. }
  124. }
  125. out->size = 0; /* don't allow flip or clear operations */
  126. return out;
  127. }
  128. void reinit_graphics_fullscreen(gfx_context_t * out) {
  129. ioctl(framebuffer_fd, IO_VID_WIDTH, &out->width);
  130. ioctl(framebuffer_fd, IO_VID_HEIGHT, &out->height);
  131. ioctl(framebuffer_fd, IO_VID_DEPTH, &out->depth);
  132. ioctl(framebuffer_fd, IO_VID_STRIDE, &out->stride);
  133. out->size = GFX_H(out) * GFX_S(out);
  134. if (out->clips && out->clips_size != out->height) {
  135. free(out->clips);
  136. out->clips = NULL;
  137. out->clips_size = 0;
  138. }
  139. if (out->buffer != out->backbuffer) {
  140. ioctl(framebuffer_fd, IO_VID_ADDR, &out->buffer);
  141. out->backbuffer = realloc(out->backbuffer, GFX_S(out) * GFX_H(out));
  142. } else {
  143. ioctl(framebuffer_fd, IO_VID_ADDR, &out->buffer);
  144. out->backbuffer = out->buffer;
  145. }
  146. }
  147. gfx_context_t * init_graphics_sprite(sprite_t * sprite) {
  148. gfx_context_t * out = malloc(sizeof(gfx_context_t));
  149. out->clips = NULL;
  150. out->width = sprite->width;
  151. out->stride = sprite->width * sizeof(uint32_t);
  152. out->height = sprite->height;
  153. out->depth = 32;
  154. out->size = GFX_H(out) * GFX_W(out) * GFX_B(out);
  155. out->buffer = (char *)sprite->bitmap;
  156. out->backbuffer = out->buffer;
  157. return out;
  158. }
  159. sprite_t * create_sprite(size_t width, size_t height, int alpha) {
  160. sprite_t * out = malloc(sizeof(sprite_t));
  161. /*
  162. uint16_t width;
  163. uint16_t height;
  164. uint32_t * bitmap;
  165. uint32_t * masks;
  166. uint32_t blank;
  167. uint8_t alpha;
  168. */
  169. out->width = width;
  170. out->height = height;
  171. out->bitmap = malloc(sizeof(uint32_t) * out->width * out->height);
  172. out->masks = NULL;
  173. out->blank = 0x00000000;
  174. out->alpha = alpha;
  175. return out;
  176. }
  177. void sprite_free(sprite_t * sprite) {
  178. if (sprite->masks) {
  179. free(sprite->masks);
  180. }
  181. free(sprite->bitmap);
  182. free(sprite);
  183. }
  184. inline uint32_t rgb(uint8_t r, uint8_t g, uint8_t b) {
  185. return 0xFF000000 | (r << 16) | (g << 8) | (b);
  186. }
  187. inline uint32_t rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
  188. return (a << 24U) | (r << 16) | (g << 8) | (b);
  189. }
  190. uint32_t alpha_blend(uint32_t bottom, uint32_t top, uint32_t mask) {
  191. uint8_t a = _RED(mask);
  192. uint8_t red = (_RED(bottom) * (255 - a) + _RED(top) * a) / 255;
  193. uint8_t gre = (_GRE(bottom) * (255 - a) + _GRE(top) * a) / 255;
  194. uint8_t blu = (_BLU(bottom) * (255 - a) + _BLU(top) * a) / 255;
  195. uint8_t alp = (int)a + (int)_ALP(bottom) > 255 ? 255 : a + _ALP(bottom);
  196. return rgba(red,gre,blu, alp);
  197. }
  198. #define DONT_USE_FLOAT_FOR_ALPHA 1
  199. uint32_t alpha_blend_rgba(uint32_t bottom, uint32_t top) {
  200. if (_ALP(bottom) == 0) return top;
  201. if (_ALP(top) == 255) return top;
  202. if (_ALP(top) == 0) return bottom;
  203. #if DONT_USE_FLOAT_FOR_ALPHA
  204. uint16_t a = _ALP(top);
  205. uint16_t c = 255 - a;
  206. uint16_t b = ((int)_ALP(bottom) * c) / 255;
  207. uint16_t alp = min16(a + b, 255);
  208. uint16_t red = min16((uint32_t)(_RED(bottom) * c + _RED(top) * 255) / 255, 255);
  209. uint16_t gre = min16((uint32_t)(_GRE(bottom) * c + _GRE(top) * 255) / 255, 255);
  210. uint16_t blu = min16((uint32_t)(_BLU(bottom) * c + _BLU(top) * 255) / 255, 255);
  211. return rgba(red,gre,blu,alp);
  212. #else
  213. double a = _ALP(top) / 255.0;
  214. double c = 1.0 - a;
  215. double b = (_ALP(bottom) / 255.0) * c;
  216. double alp = a + b; if (alp > 1.0) alp = 1.0;
  217. double red = (_RED(bottom) / 255.0) * c + (_RED(top) / 255.0); if (red > 1.0) red = 1.0;
  218. double gre = (_GRE(bottom) / 255.0) * c + (_GRE(top) / 255.0); if (gre > 1.0) gre = 1.0;
  219. double blu = (_BLU(bottom) / 255.0) * c + (_BLU(top) / 255.0); if (blu > 1.0) blu = 1.0;
  220. return rgba(red * 255, gre * 255, blu * 255, alp * 255);
  221. #endif
  222. }
  223. uint32_t premultiply(uint32_t color) {
  224. uint16_t a = _ALP(color);
  225. uint16_t r = _RED(color);
  226. uint16_t g = _GRE(color);
  227. uint16_t b = _BLU(color);
  228. r = r * a / 255;
  229. g = g * a / 255;
  230. b = b * a / 255;
  231. return rgba(r,g,b,a);
  232. }
  233. static int clamp(int a, int l, int h) {
  234. return a < l ? l : (a > h ? h : a);
  235. }
  236. static void _box_blur_horizontal(gfx_context_t * _src, int radius) {
  237. int w = _src->width;
  238. int h = _src->height;
  239. int half_radius = radius / 2;
  240. uint32_t * out_color = calloc(sizeof(uint32_t), w);
  241. for (int y = 0; y < h; y++) {
  242. int hits = 0;
  243. int r = 0;
  244. int g = 0;
  245. int b = 0;
  246. int a = 0;
  247. for (int x = -half_radius; x < w; x++) {
  248. int old_p = x - half_radius - 1;
  249. if (old_p >= 0)
  250. {
  251. uint32_t col = GFX(_src, clamp(old_p,0,w-1), y);
  252. if (col) {
  253. r -= _RED(col);
  254. g -= _GRE(col);
  255. b -= _BLU(col);
  256. a -= _ALP(col);
  257. }
  258. hits--;
  259. }
  260. int newPixel = x + half_radius;
  261. if (newPixel < w) {
  262. uint32_t col = GFX(_src, clamp(newPixel,0,w-1), y);
  263. if (col != 0) {
  264. r += _RED(col);
  265. g += _GRE(col);
  266. b += _BLU(col);
  267. a += _ALP(col);
  268. }
  269. hits++;
  270. }
  271. if (x >= 0 && x < w) {
  272. out_color[x] = rgba(r / hits, g / hits, b / hits, a / hits);
  273. }
  274. }
  275. if (!_is_in_clip(_src, y)) continue;
  276. for (int x = 0; x < w; x++) {
  277. GFX(_src,x,y) = out_color[x];
  278. }
  279. }
  280. free(out_color);
  281. }
  282. static void _box_blur_vertical(gfx_context_t * _src, int radius) {
  283. int w = _src->width;
  284. int h = _src->height;
  285. int half_radius = radius / 2;
  286. uint32_t * out_color = calloc(sizeof(uint32_t), h);
  287. for (int x = 0; x < w; x++) {
  288. int hits = 0;
  289. int r = 0;
  290. int g = 0;
  291. int b = 0;
  292. int a = 0;
  293. for (int y = -half_radius; y < h; y++) {
  294. int old_p = y - half_radius - 1;
  295. if (old_p >= 0) {
  296. uint32_t col = GFX(_src,x,clamp(old_p,0,h-1));
  297. if (col != 0) {
  298. r -= _RED(col);
  299. g -= _GRE(col);
  300. b -= _BLU(col);
  301. a -= _ALP(col);
  302. }
  303. hits--;
  304. }
  305. int newPixel = y + half_radius;
  306. if (newPixel < h) {
  307. uint32_t col = GFX(_src,x,clamp(newPixel,0,h-1));
  308. if (col != 0)
  309. {
  310. r += _RED(col);
  311. g += _GRE(col);
  312. b += _BLU(col);
  313. a += _ALP(col);
  314. }
  315. hits++;
  316. }
  317. if (y >= 0 && y < h) {
  318. out_color[y] = rgba(r / hits, g / hits, b / hits, a / hits);
  319. }
  320. }
  321. for (int y = 0; y < h; y++) {
  322. if (!_is_in_clip(_src, y)) continue;
  323. GFX(_src,x,y) = out_color[y];
  324. }
  325. }
  326. free(out_color);
  327. }
  328. void blur_context_box(gfx_context_t * _src, int radius) {
  329. _box_blur_horizontal(_src,radius);
  330. _box_blur_vertical(_src,radius);
  331. }
  332. void load_sprite(sprite_t * sprite, char * filename) {
  333. /* Open the requested binary */
  334. FILE * image = fopen(filename, "r");
  335. size_t image_size= 0;
  336. fseek(image, 0, SEEK_END);
  337. image_size = ftell(image);
  338. fseek(image, 0, SEEK_SET);
  339. /* Alright, we have the length */
  340. char * bufferb = malloc(image_size);
  341. fread(bufferb, image_size, 1, image);
  342. if (bufferb[0] == 'B' && bufferb[1] == 'M') {
  343. /* Bitmaps */
  344. uint16_t x = 0; /* -> 212 */
  345. uint16_t y = 0; /* -> 68 */
  346. /* Get the width / height of the image */
  347. signed int *bufferi = (signed int *)((uintptr_t)bufferb + 2);
  348. uint32_t width = bufferi[4];
  349. uint32_t height = bufferi[5];
  350. uint16_t bpp = bufferi[6] / 0x10000;
  351. uint32_t row_width = (bpp * width + 31) / 32 * 4;
  352. /* Skip right to the important part */
  353. size_t i = bufferi[2];
  354. sprite->width = width;
  355. sprite->height = height;
  356. sprite->bitmap = malloc(sizeof(uint32_t) * width * height);
  357. sprite->masks = NULL;
  358. for (y = 0; y < height; ++y) {
  359. for (x = 0; x < width; ++x) {
  360. if (i > image_size) goto _cleanup_sprite;
  361. /* Extract the color */
  362. uint32_t color;
  363. if (bpp == 24) {
  364. color = (bufferb[i + 3 * x] & 0xFF) +
  365. (bufferb[i+1 + 3 * x] & 0xFF) * 0x100 +
  366. (bufferb[i+2 + 3 * x] & 0xFF) * 0x10000 + 0xFF000000;
  367. } else if (bpp == 32) {
  368. if (bufferb[i + 4 * x] == 0) {
  369. color = 0x000000;
  370. } else {
  371. color = (bufferb[i + 4 * x] & 0xFF) * 0x1000000 +
  372. (bufferb[i+1 + 4 * x] & 0xFF) * 0x1 +
  373. (bufferb[i+2 + 4 * x] & 0xFF) * 0x100 +
  374. (bufferb[i+3 + 4 * x] & 0xFF) * 0x10000;
  375. color = premultiply(color);
  376. }
  377. } else {
  378. color = rgb(bufferb[i + x],bufferb[i + x],bufferb[i + x]); /* Unsupported */
  379. }
  380. /* Set our point */
  381. sprite->bitmap[(height - y - 1) * width + x] = color;
  382. }
  383. i += row_width;
  384. }
  385. } else {
  386. /* Assume targa; limited support */
  387. struct Header {
  388. uint8_t id_length;
  389. uint8_t color_map_type;
  390. uint8_t image_type;
  391. uint16_t color_map_first_entry;
  392. uint16_t color_map_length;
  393. uint8_t color_map_entry_size;
  394. uint16_t x_origin;
  395. uint16_t y_origin;
  396. uint16_t width;
  397. uint16_t height;
  398. uint8_t depth;
  399. uint8_t descriptor;
  400. } __attribute__((packed));
  401. struct Header * header = (struct Header *)bufferb;
  402. if (header->id_length || header->color_map_type || (header->image_type != 2)) {
  403. /* Unable to parse */
  404. goto _cleanup_sprite;
  405. }
  406. sprite->width = header->width;
  407. sprite->height = header->height;
  408. sprite->bitmap = malloc(sizeof(uint32_t) * sprite->width * sprite->height);
  409. sprite->masks = NULL;
  410. uint16_t x = 0;
  411. uint16_t y = 0;
  412. int i = sizeof(struct Header);
  413. if (header->depth == 24) {
  414. for (y = 0; y < sprite->height; ++y) {
  415. for (x = 0; x < sprite->width; ++x) {
  416. uint32_t color = rgb(
  417. bufferb[i+2 + 3 * x],
  418. bufferb[i+1 + 3 * x],
  419. bufferb[i + 3 * x]);
  420. sprite->bitmap[(sprite->height - y - 1) * sprite->width + x] = color;
  421. }
  422. i += sprite->width * 3;
  423. }
  424. } else if (header->depth == 32) {
  425. for (y = 0; y < sprite->height; ++y) {
  426. for (x = 0; x < sprite->width; ++x) {
  427. uint32_t color = rgba(
  428. bufferb[i+2 + 4 * x],
  429. bufferb[i+1 + 4 * x],
  430. bufferb[i + 4 * x],
  431. bufferb[i+3 + 4 * x]);
  432. sprite->bitmap[(sprite->height - y - 1) * sprite->width + x] = color;
  433. }
  434. i += sprite->width * 4;
  435. }
  436. }
  437. }
  438. _cleanup_sprite:
  439. fclose(image);
  440. free(bufferb);
  441. }
  442. #ifndef NO_SSE
  443. static __m128i mask00ff;
  444. static __m128i mask0080;
  445. static __m128i mask0101;
  446. __attribute__((constructor)) static void _masks(void) {
  447. mask00ff = _mm_set1_epi16(0x00FF);
  448. mask0080 = _mm_set1_epi16(0x0080);
  449. mask0101 = _mm_set1_epi16(0x0101);
  450. }
  451. #endif
  452. __attribute__((__force_align_arg_pointer__))
  453. void draw_sprite(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y) {
  454. int32_t _left = max(x, 0);
  455. int32_t _top = max(y, 0);
  456. int32_t _right = min(x + sprite->width, ctx->width - 1);
  457. int32_t _bottom = min(y + sprite->height, ctx->height - 1);
  458. if (sprite->alpha == ALPHA_MASK) {
  459. for (uint16_t _y = 0; _y < sprite->height; ++_y) {
  460. if (!_is_in_clip(ctx, y + _y)) continue;
  461. for (uint16_t _x = 0; _x < sprite->width; ++_x) {
  462. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  463. continue;
  464. GFX(ctx, x + _x, y + _y) = alpha_blend(GFX(ctx, x + _x, y + _y), SPRITE(sprite, _x, _y), SMASKS(sprite, _x, _y));
  465. }
  466. }
  467. } else if (sprite->alpha == ALPHA_EMBEDDED) {
  468. /* Alpha embedded is the most important step. */
  469. for (uint16_t _y = 0; _y < sprite->height; ++_y) {
  470. if (!_is_in_clip(ctx, y + _y)) continue;
  471. #ifdef NO_SSE
  472. for (uint16_t _x = 0; _x < sprite->width; ++_x) {
  473. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  474. continue;
  475. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), SPRITE(sprite, _x, _y));
  476. }
  477. #else
  478. uint16_t _x = 0;
  479. /* Ensure alignment */
  480. for (; _x < sprite->width; ++_x) {
  481. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  482. continue;
  483. if (!((uintptr_t)&GFX(ctx, x + _x, y + _y) & 15))
  484. break;
  485. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), SPRITE(sprite, _x, _y));
  486. }
  487. for (; _x < sprite->width - 3; _x += 4) {
  488. if (x + _x < _left || y + _y < _top || y + _y > _bottom) {
  489. continue;
  490. }
  491. if (x + _x + 3 > _right)
  492. break;
  493. __m128i d = _mm_load_si128((void *)&GFX(ctx, x + _x, y + _y));
  494. __m128i s = _mm_loadu_si128((void *)&SPRITE(sprite, _x, _y));
  495. __m128i d_l, d_h;
  496. __m128i s_l, s_h;
  497. // unpack destination
  498. d_l = _mm_unpacklo_epi8(d, _mm_setzero_si128());
  499. d_h = _mm_unpackhi_epi8(d, _mm_setzero_si128());
  500. // unpack source
  501. s_l = _mm_unpacklo_epi8(s, _mm_setzero_si128());
  502. s_h = _mm_unpackhi_epi8(s, _mm_setzero_si128());
  503. __m128i a_l, a_h;
  504. __m128i t_l, t_h;
  505. // extract source alpha RGBA → AAAA
  506. a_l = _mm_shufflehi_epi16(_mm_shufflelo_epi16(s_l, _MM_SHUFFLE(3,3,3,3)), _MM_SHUFFLE(3,3,3,3));
  507. a_h = _mm_shufflehi_epi16(_mm_shufflelo_epi16(s_h, _MM_SHUFFLE(3,3,3,3)), _MM_SHUFFLE(3,3,3,3));
  508. // negate source alpha
  509. t_l = _mm_xor_si128(a_l, mask00ff);
  510. t_h = _mm_xor_si128(a_h, mask00ff);
  511. // apply source alpha to destination
  512. d_l = _mm_mulhi_epu16(_mm_adds_epu16(_mm_mullo_epi16(d_l,t_l),mask0080),mask0101);
  513. d_h = _mm_mulhi_epu16(_mm_adds_epu16(_mm_mullo_epi16(d_h,t_h),mask0080),mask0101);
  514. // combine source and destination
  515. d_l = _mm_adds_epu8(s_l,d_l);
  516. d_h = _mm_adds_epu8(s_h,d_h);
  517. // pack low + high and write back to memory
  518. _mm_storeu_si128((void*)&GFX(ctx, x + _x, y + _y), _mm_packus_epi16(d_l,d_h));
  519. }
  520. for (; _x < sprite->width; ++_x) {
  521. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  522. continue;
  523. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), SPRITE(sprite, _x, _y));
  524. }
  525. #endif
  526. }
  527. } else if (sprite->alpha == ALPHA_INDEXED) {
  528. for (uint16_t _y = 0; _y < sprite->height; ++_y) {
  529. if (!_is_in_clip(ctx, y + _y)) continue;
  530. for (uint16_t _x = 0; _x < sprite->width; ++_x) {
  531. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  532. continue;
  533. if (SPRITE(sprite, _x, _y) != sprite->blank) {
  534. GFX(ctx, x + _x, y + _y) = SPRITE(sprite, _x, _y) | 0xFF000000;
  535. }
  536. }
  537. }
  538. } else if (sprite->alpha == ALPHA_FORCE_SLOW_EMBEDDED) {
  539. for (uint16_t _y = 0; _y < sprite->height; ++_y) {
  540. if (!_is_in_clip(ctx, y + _y)) continue;
  541. for (uint16_t _x = 0; _x < sprite->width; ++_x) {
  542. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  543. continue;
  544. #if 1
  545. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), SPRITE(sprite, _x, _y));
  546. #else
  547. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(rgba(255,255,0,255), SPRITE(sprite, _x, _y));
  548. #endif
  549. }
  550. }
  551. } else {
  552. for (uint16_t _y = 0; _y < sprite->height; ++_y) {
  553. if (!_is_in_clip(ctx, y + _y)) continue;
  554. for (uint16_t _x = 0; _x < sprite->width; ++_x) {
  555. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  556. continue;
  557. GFX(ctx, x + _x, y + _y) = SPRITE(sprite, _x, _y) | 0xFF000000;
  558. }
  559. }
  560. }
  561. }
  562. void draw_line(gfx_context_t * ctx, int32_t x0, int32_t x1, int32_t y0, int32_t y1, uint32_t color) {
  563. int deltax = abs(x1 - x0);
  564. int deltay = abs(y1 - y0);
  565. int sx = (x0 < x1) ? 1 : -1;
  566. int sy = (y0 < y1) ? 1 : -1;
  567. int error = deltax - deltay;
  568. while (1) {
  569. if (x0 >= 0 && y0 >= 0 && x0 < ctx->width && y0 < ctx->height) {
  570. GFX(ctx, x0, y0) = color;
  571. }
  572. if (x0 == x1 && y0 == y1) break;
  573. int e2 = 2 * error;
  574. if (e2 > -deltay) {
  575. error -= deltay;
  576. x0 += sx;
  577. }
  578. if (e2 < deltax) {
  579. error += deltax;
  580. y0 += sy;
  581. }
  582. }
  583. }
  584. void draw_line_thick(gfx_context_t * ctx, int32_t x0, int32_t x1, int32_t y0, int32_t y1, uint32_t color, char thickness) {
  585. int deltax = abs(x1 - x0);
  586. int deltay = abs(y1 - y0);
  587. int sx = (x0 < x1) ? 1 : -1;
  588. int sy = (y0 < y1) ? 1 : -1;
  589. int error = deltax - deltay;
  590. while (1) {
  591. for (char j = -thickness; j <= thickness; ++j) {
  592. for (char i = -thickness; i <= thickness; ++i) {
  593. if (x0 + i >= 0 && x0 + i < ctx->width && y0 + j >= 0 && y0 + j < ctx->height) {
  594. GFX(ctx, x0 + i, y0 + j) = color;
  595. }
  596. }
  597. }
  598. if (x0 == x1 && y0 == y1) break;
  599. int e2 = 2 * error;
  600. if (e2 > -deltay) {
  601. error -= deltay;
  602. x0 += sx;
  603. }
  604. if (e2 < deltax) {
  605. error += deltax;
  606. y0 += sy;
  607. }
  608. }
  609. }
  610. void draw_fill(gfx_context_t * ctx, uint32_t color) {
  611. for (uint16_t y = 0; y < ctx->height; ++y) {
  612. for (uint16_t x = 0; x < ctx->width; ++x) {
  613. GFX(ctx, x, y) = color;
  614. }
  615. }
  616. }
  617. /* Bilinear filtering from Wikipedia */
  618. uint32_t getBilinearFilteredPixelColor(sprite_t * tex, double u, double v) {
  619. u *= tex->width;
  620. v *= tex->height;
  621. int x = floor(u);
  622. int y = floor(v);
  623. if (x >= tex->width) return 0;
  624. if (y >= tex->height) return 0;
  625. if (x <= 0) return 0;
  626. if (y <= 0) return 0;
  627. double u_ratio = u - x;
  628. double v_ratio = v - y;
  629. double u_o = 1 - u_ratio;
  630. double v_o = 1 - v_ratio;
  631. int r_ALP = 255;
  632. if (tex->alpha == ALPHA_MASK) {
  633. if (x == tex->width - 1 || y == tex->height - 1) return (SPRITE(tex,x,y) | 0xFF000000) & (0xFFFFFF + ((uint32_t)_RED(SMASKS(tex,x,y)) << 24));
  634. r_ALP = (_RED(SMASKS(tex,x,y)) * u_o + _RED(SMASKS(tex,x+1,y)) * u_ratio) * v_o + (_RED(SMASKS(tex,x,y+1)) * u_o + _RED(SMASKS(tex,x+1,y+1)) * u_ratio) * v_ratio;
  635. } else if (tex->alpha == ALPHA_EMBEDDED) {
  636. if (x == tex->width - 1 || y == tex->height - 1) return (SPRITE(tex,x,y) | 0xFF000000) & (0xFFFFFF + ((uint32_t)_ALP(SPRITE(tex,x,y)) << 24));
  637. r_ALP = (_ALP(SPRITE(tex,x,y)) * u_o + _ALP(SPRITE(tex,x+1,y)) * u_ratio) * v_o + (_ALP(SPRITE(tex,x,y+1)) * u_o + _ALP(SPRITE(tex,x+1,y+1)) * u_ratio) * v_ratio;
  638. }
  639. if (x == tex->width - 1 || y == tex->height - 1) return SPRITE(tex,x,y);
  640. int r_RED = (_RED(SPRITE(tex,x,y)) * u_o + _RED(SPRITE(tex,x+1,y)) * u_ratio) * v_o + (_RED(SPRITE(tex,x,y+1)) * u_o + _RED(SPRITE(tex,x+1,y+1)) * u_ratio) * v_ratio;
  641. int r_BLU = (_BLU(SPRITE(tex,x,y)) * u_o + _BLU(SPRITE(tex,x+1,y)) * u_ratio) * v_o + (_BLU(SPRITE(tex,x,y+1)) * u_o + _BLU(SPRITE(tex,x+1,y+1)) * u_ratio) * v_ratio;
  642. int r_GRE = (_GRE(SPRITE(tex,x,y)) * u_o + _GRE(SPRITE(tex,x+1,y)) * u_ratio) * v_o + (_GRE(SPRITE(tex,x,y+1)) * u_o + _GRE(SPRITE(tex,x+1,y+1)) * u_ratio) * v_ratio;
  643. return rgb(r_RED,r_GRE,r_BLU) & (0xFFFFFF + ((uint32_t)r_ALP << 24));
  644. }
  645. void draw_sprite_scaled(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, uint16_t width, uint16_t height) {
  646. int32_t _left = max(x, 0);
  647. int32_t _top = max(y, 0);
  648. int32_t _right = min(x + width, ctx->width - 1);
  649. int32_t _bottom = min(y + height, ctx->height - 1);
  650. for (uint16_t _y = 0; _y < height; ++_y) {
  651. if (!_is_in_clip(ctx, y + _y)) continue;
  652. for (uint16_t _x = 0; _x < width; ++_x) {
  653. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  654. continue;
  655. if (sprite->alpha > 0) {
  656. uint32_t n_color = getBilinearFilteredPixelColor(sprite, (double)_x / (double)width, (double)_y/(double)height);
  657. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), n_color);
  658. } else {
  659. GFX(ctx, x + _x, y + _y) = getBilinearFilteredPixelColor(sprite, (double)_x / (double)width, (double)_y/(double)height);
  660. }
  661. }
  662. }
  663. }
  664. void draw_sprite_alpha(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, float alpha) {
  665. int32_t _left = max(x, 0);
  666. int32_t _top = max(y, 0);
  667. int32_t _right = min(x + sprite->width, ctx->width - 1);
  668. int32_t _bottom = min(y + sprite->height, ctx->height - 1);
  669. for (uint16_t _y = 0; _y < sprite->height; ++_y) {
  670. if (!_is_in_clip(ctx, y + _y)) continue;
  671. for (uint16_t _x = 0; _x < sprite->width; ++_x) {
  672. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  673. continue;
  674. uint32_t n_color = SPRITE(sprite, _x, _y);
  675. uint32_t f_color = premultiply((n_color & 0xFFFFFF) | ((uint32_t)(255 * alpha) << 24));
  676. f_color = (f_color & 0xFFFFFF) | ((uint32_t)(alpha * _ALP(n_color)) << 24);
  677. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), f_color);
  678. }
  679. }
  680. }
  681. void draw_sprite_alpha_paint(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, float alpha, uint32_t c) {
  682. int32_t _left = max(x, 0);
  683. int32_t _top = max(y, 0);
  684. int32_t _right = min(x + sprite->width, ctx->width - 1);
  685. int32_t _bottom = min(y + sprite->height, ctx->height - 1);
  686. for (uint16_t _y = 0; _y < sprite->height; ++_y) {
  687. if (!_is_in_clip(ctx, y + _y)) continue;
  688. for (uint16_t _x = 0; _x < sprite->width; ++_x) {
  689. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  690. continue;
  691. uint32_t n_color = SPRITE(sprite, _x, _y);
  692. uint32_t f_color = rgb(_ALP(n_color) * alpha, 0, 0);
  693. GFX(ctx, x + _x, y + _y) = alpha_blend(GFX(ctx, x + _x, y + _y), c, f_color);
  694. }
  695. }
  696. }
  697. void draw_sprite_scaled_alpha(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, uint16_t width, uint16_t height, float alpha) {
  698. int32_t _left = max(x, 0);
  699. int32_t _top = max(y, 0);
  700. int32_t _right = min(x + width, ctx->width - 1);
  701. int32_t _bottom = min(y + height, ctx->height - 1);
  702. for (uint16_t _y = 0; _y < height; ++_y) {
  703. if (!_is_in_clip(ctx, y + _y)) continue;
  704. for (uint16_t _x = 0; _x < width; ++_x) {
  705. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  706. continue;
  707. uint32_t n_color = getBilinearFilteredPixelColor(sprite, (double)_x / (double)width, (double)_y/(double)height);
  708. uint32_t f_color = premultiply((n_color & 0xFFFFFF) | ((uint32_t)(255 * alpha) << 24));
  709. f_color = (f_color & 0xFFFFFF) | ((uint32_t)(alpha * _ALP(n_color)) << 24);
  710. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), f_color);
  711. }
  712. }
  713. }
  714. uint32_t interp_colors(uint32_t bottom, uint32_t top, uint8_t interp) {
  715. uint8_t red = (_RED(bottom) * (255 - interp) + _RED(top) * interp) / 255;
  716. uint8_t gre = (_GRE(bottom) * (255 - interp) + _GRE(top) * interp) / 255;
  717. uint8_t blu = (_BLU(bottom) * (255 - interp) + _BLU(top) * interp) / 255;
  718. uint8_t alp = (_ALP(bottom) * (255 - interp) + _ALP(top) * interp) / 255;
  719. return rgba(red,gre,blu, alp);
  720. }
  721. void draw_rectangle(gfx_context_t * ctx, int32_t x, int32_t y, uint16_t width, uint16_t height, uint32_t color) {
  722. int32_t _left = max(x, 0);
  723. int32_t _top = max(y, 0);
  724. int32_t _right = min(x + width, ctx->width - 1);
  725. int32_t _bottom = min(y + height, ctx->height - 1);
  726. for (uint16_t _y = 0; _y < height; ++_y) {
  727. if (!_is_in_clip(ctx, y + _y)) continue;
  728. for (uint16_t _x = 0; _x < width; ++_x) {
  729. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  730. continue;
  731. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), color);
  732. }
  733. }
  734. }
  735. void draw_rectangle_solid(gfx_context_t * ctx, int32_t x, int32_t y, uint16_t width, uint16_t height, uint32_t color) {
  736. int32_t _left = max(x, 0);
  737. int32_t _top = max(y, 0);
  738. int32_t _right = min(x + width, ctx->width - 1);
  739. int32_t _bottom = min(y + height, ctx->height - 1);
  740. for (uint16_t _y = 0; _y < height; ++_y) {
  741. if (!_is_in_clip(ctx, y + _y)) continue;
  742. for (uint16_t _x = 0; _x < width; ++_x) {
  743. if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom)
  744. continue;
  745. GFX(ctx, x + _x, y + _y) = color;
  746. }
  747. }
  748. }
  749. void draw_rounded_rectangle(gfx_context_t * ctx, int32_t x, int32_t y, uint16_t width, uint16_t height, int radius, uint32_t color) {
  750. /* Draw a rounded rectangle */
  751. if (radius > width / 2) {
  752. radius = width / 2;
  753. }
  754. if (radius > height / 2) {
  755. radius = height / 2;
  756. }
  757. uint32_t c = premultiply(color);
  758. for (int row = y; row < y + height; row++){
  759. for (int col = x; col < x + width; col++) {
  760. if ((col < x + radius || col > x + width - radius - 1) &&
  761. (row < y + radius || row > y + height - radius - 1)) {
  762. continue;
  763. }
  764. GFX(ctx, col, row) = alpha_blend_rgba(GFX(ctx, col, row), c);
  765. }
  766. }
  767. /* draw the actual rounding */
  768. for (int i = 0; i < radius; ++i) {
  769. long r2 = radius * radius;
  770. long i2 = i * i;
  771. long j2 = r2 - i2;
  772. double j_max = sqrt((double)j2);
  773. for (int j = 0; j <= (int)j_max; ++j) {
  774. int _x = x + width - radius + i;
  775. int _y = y + height - radius + j;
  776. int _z = y + radius - j - 1;
  777. uint32_t c = color;
  778. if (j == (int)j_max) {
  779. c = premultiply(rgba(_RED(c),_GRE(c),_BLU(c),(int)((double)_ALP(c) * (j_max - (double)j))));
  780. } else {
  781. c = premultiply(c);
  782. }
  783. GFX(ctx, _x, _y) = alpha_blend_rgba(GFX(ctx, _x, _y), c);
  784. GFX(ctx, _x, _z) = alpha_blend_rgba(GFX(ctx, _x, _z), c);
  785. _x = x + radius - i - 1;
  786. GFX(ctx, _x, _y) = alpha_blend_rgba(GFX(ctx, _x, _y), c);
  787. GFX(ctx, _x, _z) = alpha_blend_rgba(GFX(ctx, _x, _z), c);
  788. }
  789. }
  790. }
  791. void draw_rounded_rectangle_pattern(gfx_context_t * ctx, int32_t x, int32_t y, uint16_t width, uint16_t height, int radius, uint32_t (*pattern)(int32_t x, int32_t y, double alpha, void * extra), void * extra) {
  792. /* Draw a rounded rectangle */
  793. if (radius > width / 2) {
  794. radius = width / 2;
  795. }
  796. if (radius > height / 2) {
  797. radius = height / 2;
  798. }
  799. for (int row = y; row < y + height; row++){
  800. for (int col = x; col < x + width; col++) {
  801. if ((col < x + radius || col > x + width - radius - 1) &&
  802. (row < y + radius || row > y + height - radius - 1)) {
  803. continue;
  804. }
  805. GFX(ctx, col, row) = alpha_blend_rgba(GFX(ctx, col, row), pattern(col,row,1.0,extra));
  806. }
  807. }
  808. /* draw the actual rounding */
  809. for (int i = 0; i < radius; ++i) {
  810. long r2 = radius * radius;
  811. long i2 = i * i;
  812. long j2 = r2 - i2;
  813. double j_max = sqrt((double)j2);
  814. for (int j = 0; j <= (int)j_max; ++j) {
  815. int _x = x + width - radius + i;
  816. int _y = y + height - radius + j;
  817. int _z = y + radius - j - 1;
  818. double alpha = (j_max - (double)j);
  819. GFX(ctx, _x, _y) = alpha_blend_rgba(GFX(ctx, _x, _y), pattern(_x,_y,alpha,extra));
  820. GFX(ctx, _x, _z) = alpha_blend_rgba(GFX(ctx, _x, _z), pattern(_x,_z,alpha,extra));
  821. _x = x + radius - i - 1;
  822. GFX(ctx, _x, _y) = alpha_blend_rgba(GFX(ctx, _x, _y), pattern(_x,_y,alpha,extra));
  823. GFX(ctx, _x, _z) = alpha_blend_rgba(GFX(ctx, _x, _z), pattern(_x,_z,alpha,extra));
  824. }
  825. }
  826. }
  827. float gfx_point_distance(struct gfx_point * a, struct gfx_point * b) {
  828. return sqrt((a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y));
  829. }
  830. float gfx_point_distance_squared(struct gfx_point * a, struct gfx_point * b) {
  831. return (a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y);
  832. }
  833. float gfx_point_dot(struct gfx_point * a, struct gfx_point * b) {
  834. return (a->x * b->x) + (a->y * b->y);
  835. }
  836. struct gfx_point gfx_point_sub(struct gfx_point * a, struct gfx_point * b) {
  837. struct gfx_point p = {a->x - b->x, a->y - b->y};
  838. return p;
  839. }
  840. struct gfx_point gfx_point_add(struct gfx_point * a, struct gfx_point * b) {
  841. struct gfx_point p = {a->x + b->x, a->y + b->y};
  842. return p;
  843. }
  844. #define fmax(a,b) ((a) > (b) ? (a) : (b))
  845. #define fmin(a,b) ((a) < (b) ? (a) : (b))
  846. float gfx_line_distance(struct gfx_point * p, struct gfx_point * v, struct gfx_point * w) {
  847. float lengthlength = gfx_point_distance_squared(v,w);
  848. if (lengthlength == 0.0) return gfx_point_distance(p, v); /* point */
  849. struct gfx_point p_v = gfx_point_sub(p,v);
  850. struct gfx_point w_v = gfx_point_sub(w,v);
  851. float tmp = gfx_point_dot(&p_v,&w_v) / lengthlength;
  852. tmp = fmin(1.0,tmp);
  853. float t = fmax(0.0, tmp);
  854. w_v.x *= t;
  855. w_v.y *= t;
  856. struct gfx_point v_t = gfx_point_add(v, &w_v);
  857. return gfx_point_distance(p, &v_t);
  858. }
  859. /**
  860. * This is slow, but it works...
  861. *
  862. * Maybe acceptable for baked UI elements?
  863. */
  864. void draw_line_aa(gfx_context_t * ctx, int x_1, int x_2, int y_1, int y_2, uint32_t color, float thickness) {
  865. struct gfx_point v = {(float)x_1, (float)y_1};
  866. struct gfx_point w = {(float)x_2, (float)y_2};
  867. for (int y = 0; y < ctx->height; ++y) {
  868. for (int x = 0; x < ctx->width; ++x) {
  869. struct gfx_point p = {x,y};
  870. float d = gfx_line_distance(&p,&v,&w);
  871. if (d < thickness + 0.5) {
  872. if (d < thickness - 0.5) {
  873. GFX(ctx,x,y) = color;
  874. } else {
  875. uint32_t f_color = rgb(255 * (1.0 - (d - thickness + 0.5)), 0, 0);
  876. GFX(ctx,x,y) = alpha_blend(GFX(ctx,x,y), color, f_color);
  877. }
  878. }
  879. }
  880. }
  881. }
  882. static void calc_rotation(double x, double y, double px, double py, double s, double c, double * u, double * v) {
  883. /* Translate to pivot */
  884. x -= px;
  885. y -= py;
  886. *u = (x * c - y * s) + px;
  887. *v = (x * s + y * c) + py;
  888. }
  889. void draw_sprite_rotate(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, float rotation, float alpha) {
  890. double originx = (double)sprite->width / 2.0;
  891. double originy = (double)sprite->height / 2.0;
  892. /* Calculate corners */
  893. double ul_x, ul_y;
  894. double ll_x, ll_y;
  895. double ur_x, ur_y;
  896. double lr_x, lr_y;
  897. double _s = sin(rotation);
  898. double _c = cos(rotation);
  899. calc_rotation(-sprite->width/2, -sprite->height/2, 0, 0, _s, _c, &ul_x, &ul_y);
  900. calc_rotation(-sprite->width/2, sprite->height/2, 0, 0, _s, _c, &ll_x, &ll_y);
  901. calc_rotation(sprite->width/2, -sprite->height/2, 0, 0, _s, _c, &ur_x, &ur_y);
  902. calc_rotation(sprite->width/2, sprite->height/2, 0, 0, _s, _c, &lr_x, &lr_y);
  903. _s = sin(-rotation);
  904. _c = cos(-rotation);
  905. /* Calculate bounds */
  906. int32_t _left = min(min(ul_x, ll_x), min(ur_x, lr_x));
  907. int32_t _top = min(min(ul_y, ll_y), min(ur_y, lr_y));
  908. int32_t _right = max(max(ul_x, ll_x), max(ur_x, lr_x));
  909. int32_t _bottom = max(max(ul_y, ll_y), max(ur_y, lr_y));
  910. for (int32_t _y = _top; _y < _bottom; ++_y) {
  911. if (_y + y < 0) continue;
  912. if (_y + y >= ctx->height) break;
  913. if (!_is_in_clip(ctx, y + _y)) continue;
  914. for (int32_t _x = _left; _x < _right; ++_x) {
  915. if (_x + x < 0) continue;
  916. if (_x + x >= ctx->width) break;
  917. double u, v;
  918. calc_rotation(_x + originx, _y + originy, originx, originy, _s, _c, &u, &v);
  919. uint32_t n_color = getBilinearFilteredPixelColor(sprite, u / (double)sprite->width, v/(double)sprite->height);
  920. uint32_t f_color = premultiply((n_color & 0xFFFFFF) | ((uint32_t)(255 * alpha) << 24));
  921. f_color = (f_color & 0xFFFFFF) | ((uint32_t)(alpha * _ALP(n_color)) << 24);
  922. GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), f_color);
  923. }
  924. }
  925. }
  926. uint32_t gfx_vertical_gradient_pattern(int32_t x, int32_t y, double alpha, void * extra) {
  927. struct gradient_definition * gradient = extra;
  928. int base_r = _RED(gradient->top), base_g = _GRE(gradient->top), base_b = _BLU(gradient->top);
  929. int last_r = _RED(gradient->bottom), last_g = _GRE(gradient->bottom), last_b = _BLU(gradient->bottom);
  930. double gradpoint = (double)(y - (gradient->y)) / (double)gradient->height;
  931. if (alpha > 1.0) alpha = 1.0;
  932. if (alpha < 0.0) alpha = 0.0;
  933. return premultiply(rgba(
  934. base_r * (1.0 - gradpoint) + last_r * (gradpoint),
  935. base_g * (1.0 - gradpoint) + last_g * (gradpoint),
  936. base_b * (1.0 - gradpoint) + last_b * (gradpoint),
  937. alpha * 255));
  938. }