img_blit.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* Scaled, clipped RGB565 drawing with optional alpha compositing. */
  2. #include "img.h"
  3. unsigned int img_fit_scale(unsigned int iw, unsigned int ih,
  4. unsigned int bw, unsigned int bh)
  5. {
  6. unsigned int sx, sy;
  7. if (!iw || !ih || !bw || !bh)
  8. return 65536u;
  9. if (bw > 65535u) bw = 65535u;
  10. if (bh > 65535u) bh = 65535u;
  11. sx = (bw << 16) / iw;
  12. sy = (bh << 16) / ih;
  13. if (sx > sy)
  14. sx = sy;
  15. if (sx < 256)
  16. sx = 256; /* 1/256 minimum */
  17. if (sx > (8u << 16))
  18. sx = 8u << 16;
  19. return sx;
  20. }
  21. static void get_px(const struct img_image *im, int x, int y,
  22. unsigned int *r, unsigned int *g, unsigned int *b,
  23. unsigned int *a)
  24. {
  25. unsigned int v, i;
  26. if (x < 0) x = 0;
  27. if (y < 0) y = 0;
  28. if (x >= (int)im->w) x = (int)im->w - 1;
  29. if (y >= (int)im->h) y = (int)im->h - 1;
  30. i = (unsigned int)y * im->w + (unsigned int)x;
  31. v = im->px[i];
  32. *r = ((v >> 11) & 31) * 255 / 31;
  33. *g = ((v >> 5) & 63) * 255 / 63;
  34. *b = (v & 31) * 255 / 31;
  35. *a = im->alpha ? im->alpha[i] : 255;
  36. }
  37. static unsigned short blend(unsigned int sr, unsigned int sg, unsigned int sb,
  38. unsigned int sa, unsigned short dst)
  39. {
  40. unsigned int dr, dg, db;
  41. if (sa == 255)
  42. return (unsigned short)(((sr >> 3) << 11) | ((sg >> 2) << 5) |
  43. (sb >> 3));
  44. if (sa == 0)
  45. return dst;
  46. dr = ((dst >> 11) & 31) * 255 / 31;
  47. dg = ((dst >> 5) & 63) * 255 / 63;
  48. db = (dst & 31) * 255 / 31;
  49. sr = (sr * sa + dr * (255 - sa) + 127) / 255;
  50. sg = (sg * sa + dg * (255 - sa) + 127) / 255;
  51. sb = (sb * sa + db * (255 - sa) + 127) / 255;
  52. return (unsigned short)(((sr >> 3) << 11) | ((sg >> 2) << 5) |
  53. (sb >> 3));
  54. }
  55. void img_draw(const struct img_image *im, const struct img_blit *b)
  56. {
  57. unsigned int step, sw, sh;
  58. int lx, rx, ty, by;
  59. int x, y;
  60. if (!im || !b || !b->dst || !im->px || !im->w || !im->h ||
  61. !b->dst_w || !b->dst_h || !b->scale || b->clip_w <= 0 ||
  62. b->clip_h <= 0)
  63. return;
  64. sw = (unsigned int)(((unsigned long long)im->w * b->scale) >> 16);
  65. sh = (unsigned int)(((unsigned long long)im->h * b->scale) >> 16);
  66. if (!sw) sw = 1;
  67. if (!sh) sh = 1;
  68. /* ceil(2^32 / scale), without a 64-bit division (no libgcc in ring 3
  69. * on the target). The +1 only rounds a source coordinate up by less
  70. * than one pixel. */
  71. step = 0xFFFFFFFFu / b->scale + 1u;
  72. lx = b->x;
  73. if (lx < b->clip_x) lx = b->clip_x;
  74. rx = b->x + (int)sw;
  75. if (rx > b->clip_x + b->clip_w) rx = b->clip_x + b->clip_w;
  76. if (rx > b->dst_w) rx = b->dst_w;
  77. ty = b->y;
  78. if (ty < b->clip_y) ty = b->clip_y;
  79. by = b->y + (int)sh;
  80. if (by > b->clip_y + b->clip_h) by = b->clip_y + b->clip_h;
  81. if (by > b->dst_h) by = b->dst_h;
  82. if (lx >= rx || ty >= by)
  83. return;
  84. /* 1:1, opaque: a straight copy. OVER_DST and CHECKER only describe
  85. * the colour behind transparent pixels, and an opaque source never
  86. * blends, so an image drawn over the page can still take this path
  87. * (the browser sets OVER_DST on every image). */
  88. if (b->scale == 65536u && !im->alpha &&
  89. !(b->flags & IMG_BLIT_SAVE_ALPHA)) {
  90. for (y = ty; y < by; y++) {
  91. int sy = y - b->y;
  92. const unsigned short *src = im->px + (unsigned int)sy * im->w;
  93. unsigned short *dst = b->dst + (unsigned int)y * b->dst_w;
  94. for (x = lx; x < rx; x++)
  95. dst[x] = src[x - b->x];
  96. }
  97. return;
  98. }
  99. /* 1:1 with alpha: blend in RGB565 directly. get_px() and blend()
  100. * expand every channel to 8 bits and divide by 255; this path only
  101. * needs one multiply-add per channel and is what transparent logos
  102. * and icons hit on every scroll/paint once the bitmap is cached at
  103. * the display size. */
  104. if (b->scale == 65536u && im->alpha &&
  105. !(b->flags & IMG_BLIT_SAVE_ALPHA)) {
  106. for (y = ty; y < by; y++) {
  107. int sy = y - b->y;
  108. const unsigned short *src = im->px +
  109. (unsigned int)sy * im->w;
  110. const unsigned char *sa = im->alpha +
  111. (unsigned int)sy * im->w;
  112. unsigned short *dst = b->dst +
  113. (unsigned int)y * b->dst_w;
  114. for (x = lx; x < rx; x++) {
  115. unsigned int a = sa[x - b->x];
  116. unsigned short s = src[x - b->x];
  117. unsigned short d;
  118. if (b->flags & IMG_BLIT_OVER_DST)
  119. d = dst[x];
  120. else if (b->flags & IMG_BLIT_CHECKER)
  121. d = ((x >> 3) ^ (y >> 3)) & 1 ?
  122. (unsigned short)b->bg2 :
  123. (unsigned short)b->bg;
  124. else
  125. d = (unsigned short)b->bg;
  126. if (a == 0) {
  127. dst[x] = d;
  128. continue;
  129. }
  130. if (a == 255) {
  131. dst[x] = s;
  132. continue;
  133. }
  134. {
  135. unsigned int sr = (s >> 11) & 31;
  136. unsigned int sg = (s >> 5) & 63;
  137. unsigned int sb = s & 31;
  138. unsigned int dr = (d >> 11) & 31;
  139. unsigned int dg = (d >> 5) & 63;
  140. unsigned int db = d & 31;
  141. unsigned int r = (sr * a +
  142. dr * (255 - a) +
  143. 127) / 255;
  144. unsigned int g = (sg * a +
  145. dg * (255 - a) +
  146. 127) / 255;
  147. unsigned int bl = (sb * a +
  148. db * (255 - a) +
  149. 127) / 255;
  150. dst[x] = (unsigned short)((r << 11) |
  151. (g << 5) | bl);
  152. }
  153. }
  154. }
  155. return;
  156. }
  157. for (y = ty; y < by; y++) {
  158. unsigned long long sy_fp =
  159. (unsigned long long)(y - b->y) * step;
  160. int sy0 = (int)(sy_fp >> 16);
  161. int sy1 = (int)((sy_fp + step) >> 16);
  162. if (sy1 <= sy0)
  163. sy1 = sy0 + 1;
  164. for (x = lx; x < rx; x++) {
  165. unsigned long long sx_fp =
  166. (unsigned long long)(x - b->x) * step;
  167. int sx0 = (int)(sx_fp >> 16);
  168. int sx1 = (int)((sx_fp + step) >> 16);
  169. unsigned int sr, sg, sb, sa;
  170. unsigned short d;
  171. if (sx1 <= sx0)
  172. sx1 = sx0 + 1;
  173. if (!(b->flags & IMG_BLIT_FAST) && b->scale < 65536u) {
  174. unsigned int rsum = 0, gsum = 0, bsum = 0;
  175. unsigned int asum = 0, count = 0;
  176. int xx, yy;
  177. if (sx0 < 0) sx0 = 0;
  178. if (sy0 < 0) sy0 = 0;
  179. if (sx1 > (int)im->w) sx1 = (int)im->w;
  180. if (sy1 > (int)im->h) sy1 = (int)im->h;
  181. for (yy = sy0; yy < sy1; yy++) {
  182. for (xx = sx0; xx < sx1; xx++) {
  183. unsigned int r, g, bb, a;
  184. get_px(im, xx, yy, &r, &g, &bb, &a);
  185. rsum += r;
  186. gsum += g;
  187. bsum += bb;
  188. asum += a;
  189. count++;
  190. }
  191. }
  192. if (!count) {
  193. get_px(im, sx0, sy0, &sr, &sg, &sb, &sa);
  194. } else {
  195. sr = (rsum + count / 2) / count;
  196. sg = (gsum + count / 2) / count;
  197. sb = (bsum + count / 2) / count;
  198. sa = (asum + count / 2) / count;
  199. }
  200. } else if (!(b->flags & IMG_BLIT_FAST) &&
  201. b->scale > 65536u) {
  202. int fx = (int)(sx_fp & 65535);
  203. int fy = (int)(sy_fp & 65535);
  204. unsigned int r0, g0, b0, a0, r1, g1, b1, a1;
  205. get_px(im, sx0, sy0, &r0, &g0, &b0, &a0);
  206. get_px(im, sx0 + 1, sy0, &r1, &g1, &b1, &a1);
  207. {
  208. unsigned int rr = (r0 * (65536 - fx) +
  209. r1 * fx) >> 16;
  210. unsigned int gg = (g0 * (65536 - fx) +
  211. g1 * fx) >> 16;
  212. unsigned int bb = (b0 * (65536 - fx) +
  213. b1 * fx) >> 16;
  214. unsigned int aa = (a0 * (65536 - fx) +
  215. a1 * fx) >> 16;
  216. get_px(im, sx0, sy0 + 1, &r0, &g0, &b0, &a0);
  217. get_px(im, sx0 + 1, sy0 + 1, &r1, &g1, &b1,
  218. &a1);
  219. sr = (rr * (65536 - fy) +
  220. ((r0 * (65536 - fx) + r1 * fx) >> 16) * fy) >> 16;
  221. sg = (gg * (65536 - fy) +
  222. ((g0 * (65536 - fx) + g1 * fx) >> 16) * fy) >> 16;
  223. sb = (bb * (65536 - fy) +
  224. ((b0 * (65536 - fx) + b1 * fx) >> 16) * fy) >> 16;
  225. sa = (aa * (65536 - fy) +
  226. ((a0 * (65536 - fx) + a1 * fx) >> 16) * fy) >> 16;
  227. }
  228. } else {
  229. get_px(im, sx0, sy0, &sr, &sg, &sb, &sa);
  230. }
  231. {
  232. unsigned int di = (unsigned int)y * b->dst_w + x;
  233. unsigned short bg;
  234. if (b->flags & IMG_BLIT_OVER_DST)
  235. bg = b->dst[di];
  236. else if (b->flags & IMG_BLIT_CHECKER)
  237. bg = ((x >> 3) ^ (y >> 3)) & 1 ?
  238. (unsigned short)b->bg2 :
  239. (unsigned short)b->bg;
  240. else
  241. bg = (unsigned short)b->bg;
  242. if ((b->flags & IMG_BLIT_SAVE_ALPHA) && b->dst_a) {
  243. b->dst_a[di] = (unsigned char)(sa > 255 ? 255 : sa);
  244. d = (unsigned short)(((sr >> 3) << 11) |
  245. ((sg >> 2) << 5) |
  246. (sb >> 3));
  247. } else {
  248. d = (im->alpha && sa < 255) ?
  249. blend(sr, sg, sb, sa, bg) :
  250. (unsigned short)(((sr >> 3) << 11) |
  251. ((sg >> 2) << 5) |
  252. (sb >> 3));
  253. }
  254. b->dst[di] = d;
  255. }
  256. }
  257. }
  258. }