img_blit.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, neat copy. */
  85. if (b->scale == 65536u && !im->alpha &&
  86. !(b->flags & (IMG_BLIT_CHECKER | IMG_BLIT_OVER_DST))) {
  87. for (y = ty; y < by; y++) {
  88. int sy = y - b->y;
  89. const unsigned short *src = im->px + (unsigned int)sy * im->w;
  90. unsigned short *dst = b->dst + (unsigned int)y * b->dst_w;
  91. for (x = lx; x < rx; x++)
  92. dst[x] = src[x - b->x];
  93. }
  94. return;
  95. }
  96. for (y = ty; y < by; y++) {
  97. unsigned long long sy_fp =
  98. (unsigned long long)(y - b->y) * step;
  99. int sy0 = (int)(sy_fp >> 16);
  100. int sy1 = (int)((sy_fp + step) >> 16);
  101. if (sy1 <= sy0)
  102. sy1 = sy0 + 1;
  103. for (x = lx; x < rx; x++) {
  104. unsigned long long sx_fp =
  105. (unsigned long long)(x - b->x) * step;
  106. int sx0 = (int)(sx_fp >> 16);
  107. int sx1 = (int)((sx_fp + step) >> 16);
  108. unsigned int sr, sg, sb, sa;
  109. unsigned short d;
  110. if (sx1 <= sx0)
  111. sx1 = sx0 + 1;
  112. if (!(b->flags & IMG_BLIT_FAST) && b->scale < 65536u) {
  113. unsigned int rsum = 0, gsum = 0, bsum = 0;
  114. unsigned int asum = 0, count = 0;
  115. int xx, yy;
  116. if (sx0 < 0) sx0 = 0;
  117. if (sy0 < 0) sy0 = 0;
  118. if (sx1 > (int)im->w) sx1 = (int)im->w;
  119. if (sy1 > (int)im->h) sy1 = (int)im->h;
  120. for (yy = sy0; yy < sy1; yy++) {
  121. for (xx = sx0; xx < sx1; xx++) {
  122. unsigned int r, g, bb, a;
  123. get_px(im, xx, yy, &r, &g, &bb, &a);
  124. rsum += r;
  125. gsum += g;
  126. bsum += bb;
  127. asum += a;
  128. count++;
  129. }
  130. }
  131. if (!count) {
  132. get_px(im, sx0, sy0, &sr, &sg, &sb, &sa);
  133. } else {
  134. sr = (rsum + count / 2) / count;
  135. sg = (gsum + count / 2) / count;
  136. sb = (bsum + count / 2) / count;
  137. sa = (asum + count / 2) / count;
  138. }
  139. } else if (!(b->flags & IMG_BLIT_FAST) &&
  140. b->scale > 65536u) {
  141. int fx = (int)(sx_fp & 65535);
  142. int fy = (int)(sy_fp & 65535);
  143. unsigned int r0, g0, b0, a0, r1, g1, b1, a1;
  144. get_px(im, sx0, sy0, &r0, &g0, &b0, &a0);
  145. get_px(im, sx0 + 1, sy0, &r1, &g1, &b1, &a1);
  146. {
  147. unsigned int rr = (r0 * (65536 - fx) +
  148. r1 * fx) >> 16;
  149. unsigned int gg = (g0 * (65536 - fx) +
  150. g1 * fx) >> 16;
  151. unsigned int bb = (b0 * (65536 - fx) +
  152. b1 * fx) >> 16;
  153. unsigned int aa = (a0 * (65536 - fx) +
  154. a1 * fx) >> 16;
  155. get_px(im, sx0, sy0 + 1, &r0, &g0, &b0, &a0);
  156. get_px(im, sx0 + 1, sy0 + 1, &r1, &g1, &b1,
  157. &a1);
  158. sr = (rr * (65536 - fy) +
  159. ((r0 * (65536 - fx) + r1 * fx) >> 16) * fy) >> 16;
  160. sg = (gg * (65536 - fy) +
  161. ((g0 * (65536 - fx) + g1 * fx) >> 16) * fy) >> 16;
  162. sb = (bb * (65536 - fy) +
  163. ((b0 * (65536 - fx) + b1 * fx) >> 16) * fy) >> 16;
  164. sa = (aa * (65536 - fy) +
  165. ((a0 * (65536 - fx) + a1 * fx) >> 16) * fy) >> 16;
  166. }
  167. } else {
  168. get_px(im, sx0, sy0, &sr, &sg, &sb, &sa);
  169. }
  170. {
  171. unsigned int di = (unsigned int)y * b->dst_w + x;
  172. unsigned short bg;
  173. if (b->flags & IMG_BLIT_OVER_DST)
  174. bg = b->dst[di];
  175. else if (b->flags & IMG_BLIT_CHECKER)
  176. bg = ((x >> 3) ^ (y >> 3)) & 1 ?
  177. (unsigned short)b->bg2 :
  178. (unsigned short)b->bg;
  179. else
  180. bg = (unsigned short)b->bg;
  181. if ((b->flags & IMG_BLIT_SAVE_ALPHA) && b->dst_a) {
  182. b->dst_a[di] = (unsigned char)(sa > 255 ? 255 : sa);
  183. d = (unsigned short)(((sr >> 3) << 11) |
  184. ((sg >> 2) << 5) |
  185. (sb >> 3));
  186. } else {
  187. d = (im->alpha && sa < 255) ?
  188. blend(sr, sg, sb, sa, bg) :
  189. (unsigned short)(((sr >> 3) << 11) |
  190. ((sg >> 2) << 5) |
  191. (sb >> 3));
  192. }
  193. b->dst[di] = d;
  194. }
  195. }
  196. }
  197. }