| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- /* Scaled, clipped RGB565 drawing with optional alpha compositing. */
- #include "img.h"
- unsigned int img_fit_scale(unsigned int iw, unsigned int ih,
- unsigned int bw, unsigned int bh)
- {
- unsigned int sx, sy;
- if (!iw || !ih || !bw || !bh)
- return 65536u;
- if (bw > 65535u) bw = 65535u;
- if (bh > 65535u) bh = 65535u;
- sx = (bw << 16) / iw;
- sy = (bh << 16) / ih;
- if (sx > sy)
- sx = sy;
- if (sx < 256)
- sx = 256; /* 1/256 minimum */
- if (sx > (8u << 16))
- sx = 8u << 16;
- return sx;
- }
- static void get_px(const struct img_image *im, int x, int y,
- unsigned int *r, unsigned int *g, unsigned int *b,
- unsigned int *a)
- {
- unsigned int v, i;
- if (x < 0) x = 0;
- if (y < 0) y = 0;
- if (x >= (int)im->w) x = (int)im->w - 1;
- if (y >= (int)im->h) y = (int)im->h - 1;
- i = (unsigned int)y * im->w + (unsigned int)x;
- v = im->px[i];
- *r = ((v >> 11) & 31) * 255 / 31;
- *g = ((v >> 5) & 63) * 255 / 63;
- *b = (v & 31) * 255 / 31;
- *a = im->alpha ? im->alpha[i] : 255;
- }
- static unsigned short blend(unsigned int sr, unsigned int sg, unsigned int sb,
- unsigned int sa, unsigned short dst)
- {
- unsigned int dr, dg, db;
- if (sa == 255)
- return (unsigned short)(((sr >> 3) << 11) | ((sg >> 2) << 5) |
- (sb >> 3));
- if (sa == 0)
- return dst;
- dr = ((dst >> 11) & 31) * 255 / 31;
- dg = ((dst >> 5) & 63) * 255 / 63;
- db = (dst & 31) * 255 / 31;
- sr = (sr * sa + dr * (255 - sa) + 127) / 255;
- sg = (sg * sa + dg * (255 - sa) + 127) / 255;
- sb = (sb * sa + db * (255 - sa) + 127) / 255;
- return (unsigned short)(((sr >> 3) << 11) | ((sg >> 2) << 5) |
- (sb >> 3));
- }
- void img_draw(const struct img_image *im, const struct img_blit *b)
- {
- unsigned int step, sw, sh;
- int lx, rx, ty, by;
- int x, y;
- if (!im || !b || !b->dst || !im->px || !im->w || !im->h ||
- !b->dst_w || !b->dst_h || !b->scale || b->clip_w <= 0 ||
- b->clip_h <= 0)
- return;
- sw = (unsigned int)(((unsigned long long)im->w * b->scale) >> 16);
- sh = (unsigned int)(((unsigned long long)im->h * b->scale) >> 16);
- if (!sw) sw = 1;
- if (!sh) sh = 1;
- /* ceil(2^32 / scale), without a 64-bit division (no libgcc in ring 3
- * on the target). The +1 only rounds a source coordinate up by less
- * than one pixel. */
- step = 0xFFFFFFFFu / b->scale + 1u;
- lx = b->x;
- if (lx < b->clip_x) lx = b->clip_x;
- rx = b->x + (int)sw;
- if (rx > b->clip_x + b->clip_w) rx = b->clip_x + b->clip_w;
- if (rx > b->dst_w) rx = b->dst_w;
- ty = b->y;
- if (ty < b->clip_y) ty = b->clip_y;
- by = b->y + (int)sh;
- if (by > b->clip_y + b->clip_h) by = b->clip_y + b->clip_h;
- if (by > b->dst_h) by = b->dst_h;
- if (lx >= rx || ty >= by)
- return;
- /* 1:1, opaque: a straight copy. OVER_DST and CHECKER only describe
- * the colour behind transparent pixels, and an opaque source never
- * blends, so an image drawn over the page can still take this path
- * (the browser sets OVER_DST on every image). */
- if (b->scale == 65536u && !im->alpha &&
- !(b->flags & IMG_BLIT_SAVE_ALPHA)) {
- for (y = ty; y < by; y++) {
- int sy = y - b->y;
- const unsigned short *src = im->px + (unsigned int)sy * im->w;
- unsigned short *dst = b->dst + (unsigned int)y * b->dst_w;
- for (x = lx; x < rx; x++)
- dst[x] = src[x - b->x];
- }
- return;
- }
- /* 1:1 with alpha: blend in RGB565 directly. get_px() and blend()
- * expand every channel to 8 bits and divide by 255; this path only
- * needs one multiply-add per channel and is what transparent logos
- * and icons hit on every scroll/paint once the bitmap is cached at
- * the display size. */
- if (b->scale == 65536u && im->alpha &&
- !(b->flags & IMG_BLIT_SAVE_ALPHA)) {
- for (y = ty; y < by; y++) {
- int sy = y - b->y;
- const unsigned short *src = im->px +
- (unsigned int)sy * im->w;
- const unsigned char *sa = im->alpha +
- (unsigned int)sy * im->w;
- unsigned short *dst = b->dst +
- (unsigned int)y * b->dst_w;
- for (x = lx; x < rx; x++) {
- unsigned int a = sa[x - b->x];
- unsigned short s = src[x - b->x];
- unsigned short d;
- if (b->flags & IMG_BLIT_OVER_DST)
- d = dst[x];
- else if (b->flags & IMG_BLIT_CHECKER)
- d = ((x >> 3) ^ (y >> 3)) & 1 ?
- (unsigned short)b->bg2 :
- (unsigned short)b->bg;
- else
- d = (unsigned short)b->bg;
- if (a == 0) {
- dst[x] = d;
- continue;
- }
- if (a == 255) {
- dst[x] = s;
- continue;
- }
- {
- unsigned int sr = (s >> 11) & 31;
- unsigned int sg = (s >> 5) & 63;
- unsigned int sb = s & 31;
- unsigned int dr = (d >> 11) & 31;
- unsigned int dg = (d >> 5) & 63;
- unsigned int db = d & 31;
- unsigned int r = (sr * a +
- dr * (255 - a) +
- 127) / 255;
- unsigned int g = (sg * a +
- dg * (255 - a) +
- 127) / 255;
- unsigned int bl = (sb * a +
- db * (255 - a) +
- 127) / 255;
- dst[x] = (unsigned short)((r << 11) |
- (g << 5) | bl);
- }
- }
- }
- return;
- }
- for (y = ty; y < by; y++) {
- unsigned long long sy_fp =
- (unsigned long long)(y - b->y) * step;
- int sy0 = (int)(sy_fp >> 16);
- int sy1 = (int)((sy_fp + step) >> 16);
- if (sy1 <= sy0)
- sy1 = sy0 + 1;
- for (x = lx; x < rx; x++) {
- unsigned long long sx_fp =
- (unsigned long long)(x - b->x) * step;
- int sx0 = (int)(sx_fp >> 16);
- int sx1 = (int)((sx_fp + step) >> 16);
- unsigned int sr, sg, sb, sa;
- unsigned short d;
- if (sx1 <= sx0)
- sx1 = sx0 + 1;
- if (!(b->flags & IMG_BLIT_FAST) && b->scale < 65536u) {
- unsigned int rsum = 0, gsum = 0, bsum = 0;
- unsigned int asum = 0, count = 0;
- int xx, yy;
- if (sx0 < 0) sx0 = 0;
- if (sy0 < 0) sy0 = 0;
- if (sx1 > (int)im->w) sx1 = (int)im->w;
- if (sy1 > (int)im->h) sy1 = (int)im->h;
- for (yy = sy0; yy < sy1; yy++) {
- for (xx = sx0; xx < sx1; xx++) {
- unsigned int r, g, bb, a;
- get_px(im, xx, yy, &r, &g, &bb, &a);
- rsum += r;
- gsum += g;
- bsum += bb;
- asum += a;
- count++;
- }
- }
- if (!count) {
- get_px(im, sx0, sy0, &sr, &sg, &sb, &sa);
- } else {
- sr = (rsum + count / 2) / count;
- sg = (gsum + count / 2) / count;
- sb = (bsum + count / 2) / count;
- sa = (asum + count / 2) / count;
- }
- } else if (!(b->flags & IMG_BLIT_FAST) &&
- b->scale > 65536u) {
- int fx = (int)(sx_fp & 65535);
- int fy = (int)(sy_fp & 65535);
- unsigned int r0, g0, b0, a0, r1, g1, b1, a1;
- get_px(im, sx0, sy0, &r0, &g0, &b0, &a0);
- get_px(im, sx0 + 1, sy0, &r1, &g1, &b1, &a1);
- {
- unsigned int rr = (r0 * (65536 - fx) +
- r1 * fx) >> 16;
- unsigned int gg = (g0 * (65536 - fx) +
- g1 * fx) >> 16;
- unsigned int bb = (b0 * (65536 - fx) +
- b1 * fx) >> 16;
- unsigned int aa = (a0 * (65536 - fx) +
- a1 * fx) >> 16;
- get_px(im, sx0, sy0 + 1, &r0, &g0, &b0, &a0);
- get_px(im, sx0 + 1, sy0 + 1, &r1, &g1, &b1,
- &a1);
- sr = (rr * (65536 - fy) +
- ((r0 * (65536 - fx) + r1 * fx) >> 16) * fy) >> 16;
- sg = (gg * (65536 - fy) +
- ((g0 * (65536 - fx) + g1 * fx) >> 16) * fy) >> 16;
- sb = (bb * (65536 - fy) +
- ((b0 * (65536 - fx) + b1 * fx) >> 16) * fy) >> 16;
- sa = (aa * (65536 - fy) +
- ((a0 * (65536 - fx) + a1 * fx) >> 16) * fy) >> 16;
- }
- } else {
- get_px(im, sx0, sy0, &sr, &sg, &sb, &sa);
- }
- {
- unsigned int di = (unsigned int)y * b->dst_w + x;
- unsigned short bg;
- if (b->flags & IMG_BLIT_OVER_DST)
- bg = b->dst[di];
- else if (b->flags & IMG_BLIT_CHECKER)
- bg = ((x >> 3) ^ (y >> 3)) & 1 ?
- (unsigned short)b->bg2 :
- (unsigned short)b->bg;
- else
- bg = (unsigned short)b->bg;
- if ((b->flags & IMG_BLIT_SAVE_ALPHA) && b->dst_a) {
- b->dst_a[di] = (unsigned char)(sa > 255 ? 255 : sa);
- d = (unsigned short)(((sr >> 3) << 11) |
- ((sg >> 2) << 5) |
- (sb >> 3));
- } else {
- d = (im->alpha && sa < 255) ?
- blend(sr, sg, sb, sa, bg) :
- (unsigned short)(((sr >> 3) << 11) |
- ((sg >> 2) << 5) |
- (sb >> 3));
- }
- b->dst[di] = d;
- }
- }
- }
- }
|