Procházet zdrojové kódy

Decode images at display size and fetch chat media asynchronously

Remote <img> jobs now run the JPEG/scale work in the curl __serve helper:
the request carries the display bounds, the JPEG decoder can emit a 1/2,
1/4 or 1/8 IDCT-scaled bitmap, and the UI only copies the published
RGB565+alpha bytes.  The browser keeps a 6 M-pixel LRU of display-size
bitmaps so repaints are 1:1 blits, coalesces image-driven repaints, bounds
decode time per main-loop pass and records the net/io/dec/scale/paint
breakdown in /share/IMGSTAT.  img_blit adds the 1:1 opaque copy and
integer-alpha paths; the image tests cover the reduced IDCT output.

Chat message images and avatars use the same helper (480x320 and 32x32)
instead of blocking on chat_http(); uhttp_read consumes a buffered body
incrementally with a 16 KB reader, and ngui scroll panes can hide their
scrollbar for the control panel.
Johannes Zellner před 1 týdnem
rodič
revize
ee904a6a3c
14 změnil soubory, kde provedl 1726 přidání a 149 odebrání
  1. 671 48
      browser.c
  2. 5 2
      img.c
  3. 4 0
      img.h
  4. 67 2
      img_blit.c
  5. 261 19
      img_jpeg.c
  6. 116 4
      img_test.c
  7. 4 2
      ngui.c
  8. 1 0
      ngui.h
  9. 341 25
      ucmd_chat.c
  10. 182 8
      ucmd_curl.c
  11. 19 17
      ucmd_netmenu.c
  12. 1 1
      ucmd_view.c
  13. 53 20
      uhttp.c
  14. 1 1
      uhttp.h

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 671 - 48
browser.c


+ 5 - 2
img.c

@@ -151,8 +151,11 @@ int img_decode(const void *data, unsigned int len,
 		r = IMG_ERR_UNSUPPORTED;
 		break;
 	}
-	if (r == 0 && (!out->px || out->w != info.width ||
-		       out->h != info.height))
+	if (r == 0 && (!out->px || out->w == 0 || out->h == 0))
+		return IMG_ERR_CORRUPT;
+	/* A decoder may legitimately return a reduced bitmap (the JPEG IDCT
+	 * scale); it must never be larger than the intrinsic size. */
+	if (r == 0 && (out->w > info.width || out->h > info.height))
 		return IMG_ERR_CORRUPT;
 	return r;
 }

+ 4 - 0
img.h

@@ -50,6 +50,10 @@ struct img_info {
 struct img_options {
 	/* 0 = no additional limit beyond the arena / sanity checks. */
 	unsigned int max_pixels;
+	/* Optional output bounds.  The JPEG decoder picks a reduced IDCT
+	 * scale when it can (1/2, 1/4, 1/8); other decoders ignore these and
+	 * are scaled by the caller.  0 = unbounded. */
+	unsigned int max_w, max_h;
 };
 
 struct img_mem {

+ 67 - 2
img_blit.c

@@ -94,9 +94,12 @@ void img_draw(const struct img_image *im, const struct img_blit *b)
 	if (lx >= rx || ty >= by)
 		return;
 
-	/* 1:1, opaque, neat copy. */
+	/* 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_CHECKER | IMG_BLIT_OVER_DST))) {
+	    !(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;
@@ -108,6 +111,68 @@ void img_draw(const struct img_image *im, const struct img_blit *b)
 		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;

+ 261 - 19
img_jpeg.c

@@ -29,6 +29,17 @@ static const unsigned char zigzag[64] = {
 #define FIX_2_053119869  16819
 #define FIX_2_562915447  20995
 #define FIX_3_072711026  25172
+/* Scaled (4x4/2x2) IDCT constants, IJG/Loeffler. */
+#define FIX_0_211164243  1730
+#define FIX_0_509795579  4176
+#define FIX_0_601344887  4926
+#define FIX_0_720959822  5906
+#define FIX_0_850430095  6967
+#define FIX_1_061594337  8697
+#define FIX_1_272758580  10426
+#define FIX_1_451774981  11893
+#define FIX_2_172734803  17799
+#define FIX_3_624509785  29692
 
 static int descale(int x, int n)
 {
@@ -187,6 +198,199 @@ static void idct_block(const int *in, unsigned char *out)
 	}
 }
 
+#define SCALED_OUT(v, n) do { \
+	int _v = descale((v), (n)) + 128; \
+	if (_v < 0) _v = 0; \
+	if (_v > 255) _v = 255; \
+	*out++ = (unsigned char)_v; \
+} while (0)
+
+/* Reduced 4x4 output from the low-frequency coefficients.  This is the
+ * Loeffler/IGJ scaled IDCT: an 8-point column pass and a 4-point row pass,
+ * ignoring the coefficients the 4x4 output cannot represent.  It is the
+ * same result as decoding to 8x8 and box-filtering, at roughly a quarter
+ * of the work. */
+static void idct_block_4x4(const int *in, unsigned char *out)
+{
+	int ws[8 * 4];
+	int ctr;
+
+	for (ctr = 0; ctr < 8; ctr++) {
+		int *w = ws + ctr;
+
+		if (ctr == 4)
+			continue;
+		if (in[1 * 8 + ctr] == 0 && in[2 * 8 + ctr] == 0 &&
+		    in[3 * 8 + ctr] == 0 && in[5 * 8 + ctr] == 0 &&
+		    in[6 * 8 + ctr] == 0 && in[7 * 8 + ctr] == 0) {
+			int dc = in[ctr] << PASS1_BITS;
+
+			w[0 * 8] = w[1 * 8] = w[2 * 8] = w[3 * 8] = dc;
+			continue;
+		}
+		{
+			int tmp0, tmp2, tmp10, tmp12;
+			int z1, z2, z3, z4;
+
+			tmp0 = in[0 * 8 + ctr] << (CONST_BITS + 1);
+			z2 = in[2 * 8 + ctr];
+			z3 = in[6 * 8 + ctr];
+			tmp2 = z2 * FIX_1_847759065 +
+			       z3 * -FIX_0_765366865;
+			tmp10 = tmp0 + tmp2;
+			tmp12 = tmp0 - tmp2;
+
+			z1 = in[7 * 8 + ctr];
+			z2 = in[5 * 8 + ctr];
+			z3 = in[3 * 8 + ctr];
+			z4 = in[1 * 8 + ctr];
+			tmp0 = z1 * -FIX_0_211164243 +
+			       z2 * FIX_1_451774981 +
+			       z3 * -FIX_2_172734803 +
+			       z4 * FIX_1_061594337;
+			tmp2 = z1 * -FIX_0_509795579 +
+			       z2 * -FIX_0_601344887 +
+			       z3 * FIX_0_899976223 +
+			       z4 * FIX_2_562915447;
+
+			w[0 * 8] = descale(tmp10 + tmp2,
+					   CONST_BITS - PASS1_BITS + 1);
+			w[3 * 8] = descale(tmp10 - tmp2,
+					   CONST_BITS - PASS1_BITS + 1);
+			w[1 * 8] = descale(tmp12 + tmp0,
+					   CONST_BITS - PASS1_BITS + 1);
+			w[2 * 8] = descale(tmp12 - tmp0,
+					   CONST_BITS - PASS1_BITS + 1);
+		}
+	}
+
+	for (ctr = 0; ctr < 4; ctr++) {
+		int *w = ws + ctr * 8;
+
+		if (w[1] == 0 && w[2] == 0 && w[3] == 0 &&
+		    w[5] == 0 && w[6] == 0 && w[7] == 0) {
+			int dc = descale(w[0], PASS1_BITS + 3) + 128;
+
+			if (dc < 0) dc = 0;
+			if (dc > 255) dc = 255;
+			out[0] = out[1] = out[2] = out[3] = (unsigned char)dc;
+			out += 4;
+			continue;
+		}
+		{
+			int tmp0, tmp2, tmp10, tmp12;
+			int z1, z2, z3, z4;
+
+			tmp0 = w[0] << (CONST_BITS + 1);
+			tmp2 = w[2] * FIX_1_847759065 +
+			       w[6] * -FIX_0_765366865;
+			tmp10 = tmp0 + tmp2;
+			tmp12 = tmp0 - tmp2;
+
+			z1 = w[7];
+			z2 = w[5];
+			z3 = w[3];
+			z4 = w[1];
+			tmp0 = z1 * -FIX_0_211164243 +
+			       z2 * FIX_1_451774981 +
+			       z3 * -FIX_2_172734803 +
+			       z4 * FIX_1_061594337;
+			tmp2 = z1 * -FIX_0_509795579 +
+			       z2 * -FIX_0_601344887 +
+			       z3 * FIX_0_899976223 +
+			       z4 * FIX_2_562915447;
+
+			SCALED_OUT(tmp10 + tmp2,
+				   CONST_BITS + PASS1_BITS + 3 + 1);
+			SCALED_OUT(tmp12 + tmp0,
+				   CONST_BITS + PASS1_BITS + 3 + 1);
+			SCALED_OUT(tmp12 - tmp0,
+				   CONST_BITS + PASS1_BITS + 3 + 1);
+			SCALED_OUT(tmp10 - tmp2,
+				   CONST_BITS + PASS1_BITS + 3 + 1);
+		}
+	}
+}
+
+/* 2x2 output (1/4 scale). */
+static void idct_block_2x2(const int *in, unsigned char *out)
+{
+	int ws[8 * 2];
+	int ctr;
+
+	for (ctr = 0; ctr < 8; ctr++) {
+		int *w = ws + ctr;
+		int tmp0, tmp10;
+
+		if (ctr == 2 || ctr == 4 || ctr == 6)
+			continue;
+		if (in[1 * 8 + ctr] == 0 && in[3 * 8 + ctr] == 0 &&
+		    in[5 * 8 + ctr] == 0 && in[7 * 8 + ctr] == 0) {
+			int dc = in[ctr] << PASS1_BITS;
+
+			w[0] = w[8] = dc;
+			continue;
+		}
+		tmp10 = in[0 * 8 + ctr] << (CONST_BITS + 2);
+		tmp0 = in[7 * 8 + ctr] * -FIX_0_720959822 +
+		       in[5 * 8 + ctr] * FIX_0_850430095 +
+		       in[3 * 8 + ctr] * -FIX_1_272758580 +
+		       in[1 * 8 + ctr] * FIX_3_624509785;
+		w[0] = descale(tmp10 + tmp0, CONST_BITS - PASS1_BITS + 2);
+		w[8] = descale(tmp10 - tmp0, CONST_BITS - PASS1_BITS + 2);
+	}
+
+	for (ctr = 0; ctr < 2; ctr++) {
+		int *w = ws + ctr * 8;
+
+		if (w[1] == 0 && w[3] == 0 && w[5] == 0 && w[7] == 0) {
+			int dc = descale(w[0], PASS1_BITS + 3) + 128;
+
+			if (dc < 0) dc = 0;
+			if (dc > 255) dc = 255;
+			out[0] = out[1] = (unsigned char)dc;
+			out += 2;
+			continue;
+		}
+		{
+			int tmp0, tmp10;
+
+			tmp10 = w[0] << (CONST_BITS + 2);
+			tmp0 = w[7] * -FIX_0_720959822 +
+			       w[5] * FIX_0_850430095 +
+			       w[3] * -FIX_1_272758580 +
+			       w[1] * FIX_3_624509785;
+			SCALED_OUT(tmp10 + tmp0,
+				   CONST_BITS + PASS1_BITS + 3 + 2);
+			SCALED_OUT(tmp10 - tmp0,
+				   CONST_BITS + PASS1_BITS + 3 + 2);
+		}
+	}
+}
+
+/* 1x1 output (1/8 scale): just the DC average. */
+static void idct_block_1x1(const int *in, unsigned char *out)
+{
+	int dc = descale(in[0], 3) + 128;
+
+	if (dc < 0) dc = 0;
+	if (dc > 255) dc = 255;
+	out[0] = (unsigned char)dc;
+}
+
+static void idct_block_out(unsigned int scale, const int *blk,
+			   unsigned char *p)
+{
+	if (scale == 1)
+		idct_block(blk, p);
+	else if (scale == 2)
+		idct_block_4x4(blk, p);
+	else if (scale == 4)
+		idct_block_2x2(blk, p);
+	else
+		idct_block_1x1(blk, p);
+}
+
 struct jpeg_huff {
 	unsigned char bits[17];
 	unsigned char vals[256];
@@ -211,7 +415,10 @@ struct jpeg {
 	const unsigned char *d;
 	unsigned int n, pos;
 	unsigned int w, h;        /* raw dimensions */
+	unsigned int sw, sh;      /* raw dimensions after the IDCT scale */
 	unsigned int out_w, out_h;/* after EXIF orientation */
+	unsigned int scale;       /* 1, 2, 4 or 8: output is w/scale */
+	unsigned int want_w, want_h; /* caller's fit bounds, 0 = none */
 	unsigned int ncomp;
 	struct jpeg_comp comp[3];
 	unsigned int max_h, max_v;
@@ -719,17 +926,20 @@ static int jpeg_scan(struct jpeg *j, unsigned int ns, const unsigned int *sc,
 								int blk[64];
 								unsigned char p[64];
 								unsigned int xx, yy;
+								unsigned int s =
+									8 / j->scale;
 
 								if (jpeg_block(j, c, blk))
 									return -1;
-								idct_block(blk, p);
-								for (yy = 0; yy < 8; yy++)
-									for (xx = 0; xx < 8; xx++) {
-										unsigned int px = mx * c->h * 8 + bx * 8 + xx;
-										unsigned int py = my * c->v * 8 + by * 8 + yy;
+								idct_block_out(j->scale,
+									       blk, p);
+								for (yy = 0; yy < s; yy++)
+									for (xx = 0; xx < s; xx++) {
+										unsigned int px = mx * c->h * s + bx * s + xx;
+										unsigned int py = my * c->v * s + by * s + yy;
 
 										c->plane[py * c->pw + px] =
-											p[yy * 8 + xx];
+											p[yy * s + xx];
 									}
 							}
 						}
@@ -823,6 +1033,33 @@ static int jpeg_sof(struct jpeg *j, unsigned int marker, int *error)
 	}
 	j->mcus_x = (j->w + 8 * j->max_h - 1) / (8 * j->max_h);
 	j->mcus_y = (j->h + 8 * j->max_v - 1) / (8 * j->max_v);
+	{
+		unsigned int d = 1;
+
+		if (j->want_w || j->want_h) {
+			unsigned int dmax = 8;
+
+			if (j->want_w) {
+				unsigned int q = j->w / j->want_w;
+
+				if (q < dmax)
+					dmax = q;
+			}
+			if (j->want_h) {
+				unsigned int q = j->h / j->want_h;
+
+				if (q < dmax)
+					dmax = q;
+			}
+			if (dmax >= 8)
+				d = 8;
+			else if (dmax >= 4)
+				d = 4;
+			else if (dmax >= 2)
+				d = 2;
+		}
+		j->scale = d;
+	}
 	for (i = 0; i < j->ncomp; i++) {
 		j->comp[i].bw = j->mcus_x * j->comp[i].h;
 		j->comp[i].bh = j->mcus_y * j->comp[i].v;
@@ -830,14 +1067,16 @@ static int jpeg_sof(struct jpeg *j, unsigned int marker, int *error)
 				(8 * j->max_h);
 		j->comp[i].sh = (j->h * j->comp[i].v + 8 * j->max_v - 1) /
 				(8 * j->max_v);
-		j->comp[i].pw = j->comp[i].bw * 8;
-		j->comp[i].ph = j->comp[i].bh * 8;
+		j->comp[i].pw = j->comp[i].bw * (8 / j->scale);
+		j->comp[i].ph = j->comp[i].bh * (8 / j->scale);
 	}
-	j->out_w = j->w;
-	j->out_h = j->h;
+	j->sw = (j->w + j->scale - 1) / j->scale;
+	j->sh = (j->h + j->scale - 1) / j->scale;
+	j->out_w = j->sw;
+	j->out_h = j->sh;
 	if (j->orientation >= 5) {
-		j->out_w = j->h;
-		j->out_h = j->w;
+		j->out_w = j->sh;
+		j->out_h = j->sw;
 	}
 	return 0;
 }
@@ -879,6 +1118,7 @@ static void jpeg_reconstruct(struct jpeg *j)
 	for (ci = 0; ci < j->ncomp; ci++) {
 		struct jpeg_comp *c = &j->comp[ci];
 		const unsigned char *q = j->qt[c->tq];
+		unsigned int s = 8 / j->scale;
 
 		for (by = 0; by < c->bh; by++) {
 			for (bx = 0; bx < c->bw; bx++) {
@@ -889,12 +1129,12 @@ static void jpeg_reconstruct(struct jpeg *j)
 
 				for (k = 0; k < 64; k++)
 					blk[zigzag[k]] = (int)coef[k] * q[k];
-				idct_block(blk, p);
-				for (yy = 0; yy < 8; yy++)
-					for (xx = 0; xx < 8; xx++)
-						c->plane[(by * 8 + yy) * c->pw +
-							 bx * 8 + xx] =
-							p[yy * 8 + xx];
+				idct_block_out(j->scale, blk, p);
+				for (yy = 0; yy < s; yy++)
+					for (xx = 0; xx < s; xx++)
+						c->plane[(by * s + yy) * c->pw +
+							 bx * s + xx] =
+							p[yy * s + xx];
 			}
 		}
 	}
@@ -924,7 +1164,7 @@ static void jpeg_output(struct jpeg *j, struct img_image *out)
 		for (ox = 0; ox < j->out_w; ox++) {
 			unsigned int x, y, yy, r, g, b;
 
-			jpeg_map_pixel(j->orientation, ox, oy, j->w, j->h,
+			jpeg_map_pixel(j->orientation, ox, oy, j->sw, j->sh,
 				       &x, &y);
 			yy = j->comp[0].plane[y * j->comp[0].pw + x];
 			if (j->ncomp == 1) {
@@ -1131,6 +1371,8 @@ int img_decode_jpeg(const unsigned char *d, unsigned int n,
 	j->pos = 2;
 	j->rst_next = 0;
 	j->orientation = jpeg_exif_orientation(d, n);
+	j->want_w = opt ? opt->max_w : 0;
+	j->want_h = opt ? opt->max_h : 0;
 
 	while (j->pos < n) {
 		int mk = jpeg_marker(j);

+ 116 - 4
img_test.c

@@ -64,7 +64,7 @@ static int test_lossless(const struct img_exp *e)
 	unsigned char *data = filebuf;
 	unsigned int n;
 	struct img_mem mem;
-	struct img_options opt;
+	struct img_options opt = {0};
 	struct img_info info;
 	struct img_image im;
 	int r;
@@ -161,7 +161,7 @@ static int test_jpeg(const char *name)
 	unsigned int n, rw, rh;
 	unsigned char *ref;
 	struct img_mem mem;
-	struct img_options opt;
+	struct img_options opt = {0};
 	struct img_info info;
 	struct img_image im;
 	int r, x, y;
@@ -224,12 +224,106 @@ static int test_jpeg(const char *name)
 	return 0;
 }
 
+static int test_jpeg_scaled(const char *name)
+{
+	char path[256];
+	unsigned int n, w, h;
+	unsigned short *full;
+	struct img_mem mem;
+	struct img_options opt = {0};
+	struct img_image im;
+	int r, div, x, y;
+
+	snprintf(path, sizeof(path), "testdata/images/%s", name);
+	if (read_file(path, filebuf, sizeof(filebuf), &n))
+		return 1;
+	img_mem_init(&mem, arena, sizeof(arena));
+	memset(&im, 0, sizeof(im));
+	if (img_decode(filebuf, n, &opt, &mem, &im) != 0)
+		return 1;
+	w = im.w;
+	h = im.h;
+	full = (unsigned short *)malloc((size_t)w * h * 2);
+	if (!full)
+		return 1;
+	memcpy(full, im.px, (size_t)w * h * 2);
+
+	for (div = 2; div <= 4; div *= 2) {
+		unsigned int ow = (w + div - 1) / div;
+		unsigned int oh = (h + div - 1) / div;
+		unsigned int mr = 0, mg = 0, mb = 0, mx = 0;
+		unsigned long long count = 0;
+
+		img_mem_init(&mem, arena, sizeof(arena));
+		opt.max_pixels = 0;
+		opt.max_w = w / div;
+		opt.max_h = h / div;
+		memset(&im, 0, sizeof(im));
+		r = img_decode(filebuf, n, &opt, &mem, &im);
+		if (r != 0 || im.w != ow || im.h != oh) {
+			printf("FAIL %s 1/%d: %s %ux%u (want %ux%u)\n",
+			       name, div, r ? img_error_string(r) : "size",
+			       im.w, im.h, ow, oh);
+			free(full);
+			return 1;
+		}
+		for (y = 0; y < (int)oh; y++) {
+			for (x = 0; x < (int)ow; x++) {
+				unsigned int sum[3] = {0, 0, 0};
+				unsigned int cnt = 0;
+				unsigned short p = im.px[y * im.w + x];
+				unsigned int sr = (p >> 11) & 31;
+				unsigned int sg = (p >> 5) & 63;
+				unsigned int sb = p & 31;
+				int dx, dy;
+
+				for (dy = 0; dy < div; dy++)
+					for (dx = 0; dx < div; dx++) {
+						unsigned int xx = x * div + dx;
+						unsigned int yy = y * div + dy;
+						unsigned short q;
+
+						if (xx >= w || yy >= h)
+							continue;
+						q = full[yy * w + xx];
+						sum[0] += (q >> 11) & 31;
+						sum[1] += (q >> 5) & 63;
+						sum[2] += q & 31;
+						cnt++;
+					}
+				{
+					unsigned int dr = (unsigned int)abs((int)sr - (int)(sum[0] / cnt));
+					unsigned int dg = (unsigned int)abs((int)sg - (int)(sum[1] / cnt));
+					unsigned int db = (unsigned int)abs((int)sb - (int)(sum[2] / cnt));
+
+					mr += dr; mg += dg; mb += db; count++;
+					if (dr > mx) mx = dr;
+					if (dg > mx) mx = dg;
+					if (db > mx) mx = db;
+				}
+			}
+		}
+		if (count) {
+			unsigned int mean = (mr + mg + mb) / (unsigned int)(count * 3);
+
+			if (mean > 3 || mx > 20) {
+				printf("FAIL %s 1/%d: mean %u max %u\n",
+				       name, div, mean, mx);
+				free(full);
+				return 1;
+			}
+		}
+	}
+	free(full);
+	return 0;
+}
+
 static void test_truncated(const char *name)
 {
 	char path[256];
 	unsigned int n, step, len;
 	struct img_mem mem;
-	struct img_options opt;
+	struct img_options opt = {0};
 	struct img_image im;
 	unsigned int i;
 
@@ -353,7 +447,7 @@ static int test_unsupported(void)
 {
 	unsigned int n;
 	struct img_mem mem;
-	struct img_options opt;
+	struct img_options opt = {0};
 	struct img_image im;
 
 	if (read_file("testdata/images/cmyk.jpg", filebuf, sizeof(filebuf), &n))
@@ -388,6 +482,24 @@ int main(void)
 			failures++;
 		test_truncated(img_jpeg[i]);
 	}
+	if (test_jpeg_scaled("j420.jpg"))
+		failures++;
+	if (test_jpeg_scaled("j422.jpg"))
+		failures++;
+	if (test_jpeg_scaled("j444.jpg"))
+		failures++;
+	if (test_jpeg_scaled("jgray.jpg"))
+		failures++;
+	if (test_jpeg_scaled("jrst.jpg"))
+		failures++;
+	if (test_jpeg_scaled("prog.jpg"))
+		failures++;
+	if (test_jpeg_scaled("progrst.jpg"))
+		failures++;
+	if (test_jpeg_scaled("prog444.jpg"))
+		failures++;
+	if (test_jpeg_scaled("prog422.jpg"))
+		failures++;
 	test_truncated("anim.gif");
 	if (failures) {
 		printf("%d image test(s) failed\n", failures);

+ 4 - 2
ngui.c

@@ -2222,7 +2222,9 @@ void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
 	scroll_clamp(s);
 	if (!ui->buttons)
 		s->dragging = 0;
-	if (scrolls) {
+	if (s->hide_bar)
+		s->dragging = 0;
+	if (!s->hide_bar && scrolls) {
 		int th = scroll_thumb_h(&r, s);
 		int ty = scroll_thumb_y(&r, s);
 
@@ -2295,7 +2297,7 @@ void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
 		     struct ngui_scroll *s)
 {
 	ngui_pop_clip(ui);
-	if (!scrollable(s))
+	if (s->hide_bar || !scrollable(s))
 		return;
 	{
 		struct ngui_rect bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);

+ 1 - 0
ngui.h

@@ -98,6 +98,7 @@ struct ngui_scroll {
 	int view_h;
 	int dragging;
 	int drag_dy;
+	int hide_bar;                 /* wheel/keys only; draw no scrollbar */
 };
 
 struct ngui_slider {

+ 341 - 25
ucmd_chat.c

@@ -25,6 +25,7 @@
 #include "img.h"
 
 #include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 
 #define DEFAULT_CFG_DATA  "/data/CHAT.CFG"
@@ -52,6 +53,7 @@
 #define CHAT_IMG_URL_MAX (UHTTP_HOST_MAX + UHTTP_PATH_MAX + 16)
 #define CHAT_PIC_QUEUED 0
 #define CHAT_PIC_READY  1
+#define CHAT_PIC_FETCHING 2
 #define CHAT_PIC_FAILED (-1)
 
 #define POLL_CONV_MS        2000
@@ -515,7 +517,8 @@ static struct chat_pic *chat_pic_slot(struct chat_pic *arr, int *count, int max,
 		(*count)++;
 	} else {
 		for (i = 0; i < *count; i++) {
-			if (arr[i].state == CHAT_PIC_QUEUED)
+			if (arr[i].state == CHAT_PIC_QUEUED ||
+			    arr[i].state == CHAT_PIC_FETCHING)
 				continue;
 			if (arr[i].last_used < best) {
 				best = arr[i].last_used;
@@ -618,7 +621,7 @@ static void chat_avatar_mask(unsigned char *alpha, unsigned int w,
 static int chat_pic_store(struct chat_pic *p, unsigned int len, int avatar)
 {
 	struct img_info info;
-	struct img_options opt;
+	struct img_options opt = {0};
 	struct img_mem mem;
 	struct img_image decoded;
 	struct img_blit blit;
@@ -637,6 +640,8 @@ static int chat_pic_store(struct chat_pic *p, unsigned int len, int avatar)
 		return -1;
 	img_mem_init(&mem, arena, est);
 	opt.max_pixels = 4u * 1024u * 1024u;
+	opt.max_w = avatar ? CHAT_AVATAR : CHAT_IMG_W;
+	opt.max_h = avatar ? CHAT_AVATAR : CHAT_IMG_H;
 	r = img_decode(chat_fetch_buf, len, &opt, &mem, &decoded);
 	if (r != 0 || !decoded.w || !decoded.h)
 		return -1;
@@ -706,33 +711,325 @@ static int chat_pic_store(struct chat_pic *p, unsigned int len, int avatar)
 	return 0;
 }
 
-static void chat_pic_load(struct chat_pic *p, int avatar)
+/* Install a message-image bitmap decoded by the fetch helper (the helper
+ * already applied the 480x320 bounds).  Avatars are still decoded here at
+ * 32x32 so the circular mask sees the same square source as before. */
+static int chat_pic_store_px(struct chat_pic *p, unsigned char *buf,
+			     unsigned int len, unsigned int dw,
+			     unsigned int dh, int has_alpha)
 {
-	unsigned int len = 0;
-	int st;
+	unsigned int npx = dw * dh, need;
+	unsigned short *px;
+	unsigned char *alpha = 0;
 
-	if (!chat_fetch_buf)
-		chat_fetch_buf = (unsigned char *)malloc(CHAT_IMG_FETCH_MAX);
-	if (!chat_fetch_buf) {
-		p->state = CHAT_PIC_FAILED;
+	if (!buf || !dw || !dh || len < npx * 2u)
+		return -1;
+	need = npx * 2u;
+	if (chat_image_room(need) != 0)
+		return -1;
+	px = (unsigned short *)malloc(need);
+	if (!px)
+		return -1;
+	memcpy(px, buf, need);
+	if (has_alpha && len >= need + npx) {
+		alpha = (unsigned char *)malloc(npx);
+		if (alpha)
+			memcpy(alpha, buf + need, npx);
+	}
+	p->img.format = 0;
+	p->img.w = dw;
+	p->img.h = dh;
+	p->img.px = px;
+	p->img.alpha = alpha;
+	p->img.flags = 0;
+	p->bytes = need;
+	chat_image_bytes += need;
+	return 0;
+}
+
+/* ---- asynchronous media fetch ----
+ *
+ * chat_http() blocks the UI for the whole body, so every image and avatar
+ * used to freeze the window for the download plus the decode.  Media now
+ * goes through the same curl __serve helper as the browser: the chat loop
+ * submits a request file and polls a small done marker, while a private tty
+ * absorbs the helper's DNS/TLS log lines.  Only the (short) decode runs on
+ * the chat thread. */
+static int chat_media_pid = -1;
+static int chat_media_tty = -1;
+static unsigned int chat_media_session;
+static unsigned int chat_media_seq;
+static unsigned int chat_media_deadline;
+static int chat_media_busy;
+static int chat_media_retried;
+static struct chat_pic *chat_media_pic;
+static int chat_media_avatar;
+static char chat_media_req[40], chat_media_body[40];
+static char chat_media_meta[40], chat_media_done[40];
+static char chat_media_px[40];
+
+static void chat_media_paths(void)
+{
+	if (chat_media_session)
+		return;
+	chat_media_session = ((unsigned int)u_getpid() << 16) ^
+			     (u_ticks_ms() & 0xFFFFu);
+	if (!chat_media_session)
+		chat_media_session = 1;
+	snprintf(chat_media_req, sizeof(chat_media_req), "/share/bf%u_0req",
+		 chat_media_session);
+	snprintf(chat_media_body, sizeof(chat_media_body), "/share/bf%u_0body",
+		 chat_media_session);
+	snprintf(chat_media_meta, sizeof(chat_media_meta), "/share/bf%u_0meta",
+		 chat_media_session);
+	snprintf(chat_media_done, sizeof(chat_media_done), "/share/bf%u_0done",
+		 chat_media_session);
+	snprintf(chat_media_px, sizeof(chat_media_px), "/share/bf%u_0px",
+		 chat_media_session);
+}
+
+static void chat_media_tty_drain(void)
+{
+	char sink[256];
+	int i;
+
+	if (chat_media_tty <= 0)
 		return;
+	for (i = 0; i < 8; i++)
+		if (u_tty_read(chat_media_tty, sink, sizeof(sink)) <= 0)
+			break;
+}
+
+static void chat_media_jar_sync(void)
+{
+	u_syscall3(SYS_WRITE_FILE, (int)"/share/bchat.jar", (int)&jar,
+		   (int)sizeof(jar));
+}
+
+static int chat_media_start(void)
+{
+	char arg[64];
+	int t;
+
+	chat_media_paths();
+	if (chat_media_pid > 0 && u_alive((unsigned int)chat_media_pid))
+		return 0;
+	chat_media_pid = -1;
+	if (chat_media_tty == -1) {
+		t = u_tty_create();
+		chat_media_tty = t > 0 ? t : 0;
+	}
+	chat_media_jar_sync();
+	snprintf(arg, sizeof(arg), "__serve %u 0 /share/bchat.jar",
+		 chat_media_session);
+	chat_media_pid = u_spawn("/ramfs/bin/curl", arg, chat_media_tty, "/");
+	if (chat_media_pid <= 0) {
+		chat_media_pid = -1;
+		return -1;
 	}
-	http.jar = &jar;
-	http.accept = "image/*,*/*";
-	st = chat_http(&http, 0, p->url, 0, (char *)chat_fetch_buf,
-		       CHAT_IMG_FETCH_MAX, &len);
-	if (st != 200 || len == 0 || chat_pic_store(p, len, avatar) != 0) {
-		p->state = CHAT_PIC_FAILED;
-	} else {
-		p->state = CHAT_PIC_READY;
-		p->last_used = ++chat_pic_clock;
-		if (!avatar) {
-			msgs_gen++;
-			if (at_bottom)
-				tl_scroll.offset = 1 << 28;
+	return 0;
+}
+
+static void chat_media_stop(void)
+{
+	chat_media_tty_drain();
+	if (chat_media_pid > 0)
+		u_kill((unsigned int)chat_media_pid);
+	chat_media_pid = -1;
+}
+
+static void chat_media_fail(void)
+{
+	if (chat_media_pic)
+		chat_media_pic->state = CHAT_PIC_FAILED;
+	chat_media_pic = 0;
+	chat_media_busy = 0;
+	chat_media_retried = 0;
+	u_syscall1(SYS_UNLINK, (int)chat_media_done);
+	u_syscall1(SYS_UNLINK, (int)chat_media_meta);
+	u_syscall1(SYS_UNLINK, (int)chat_media_body);
+	u_syscall1(SYS_UNLINK, (int)chat_media_px);
+	ngui_invalidate(&ui);
+}
+
+static int chat_media_submit(struct chat_pic *p, int avatar)
+{
+	char req[512];
+	int n;
+
+	if (!chat_fetch_buf) {
+		chat_fetch_buf = (unsigned char *)malloc(CHAT_IMG_FETCH_MAX);
+		if (!chat_fetch_buf)
+			return -1;
+	}
+	chat_media_paths();
+	if (chat_media_start() != 0)
+		return -1;
+	chat_media_jar_sync();
+	chat_media_seq++;
+	if (!chat_media_seq)
+		chat_media_seq = 1;
+	chat_media_retried = 0;
+	if (!avatar)
+		n = snprintf(req, sizeof(req), "%u %u D %u %u %s\n",
+			     chat_media_seq, CHAT_IMG_FETCH_MAX,
+			     CHAT_IMG_W, CHAT_IMG_H, p->url);
+	else
+		n = snprintf(req, sizeof(req), "%u %u %s\n",
+			     chat_media_seq, CHAT_IMG_FETCH_MAX, p->url);
+	if (n <= 0 || n >= (int)sizeof(req))
+		return -1;
+	u_syscall1(SYS_UNLINK, (int)chat_media_done);
+	u_syscall1(SYS_UNLINK, (int)chat_media_meta);
+	u_syscall1(SYS_UNLINK, (int)chat_media_body);
+	u_syscall1(SYS_UNLINK, (int)chat_media_px);
+	if (u_syscall3(SYS_WRITE_FILE, (int)chat_media_req, (int)req, n) < 0)
+		return -1;
+	p->state = CHAT_PIC_FETCHING;
+	chat_media_pic = p;
+	chat_media_avatar = avatar;
+	chat_media_busy = 1;
+	chat_media_deadline = u_ticks_ms() + 30000;
+	return 0;
+}
+
+/* 1 when the outstanding job finished (success or failure). */
+static unsigned int chat_meta_uint(char **pp)
+{
+	char *p = *pp;
+	unsigned int v = 0;
+
+	while (*p >= '0' && *p <= '9') {
+		v = v * 10 + (unsigned int)(*p - '0');
+		p++;
+	}
+	while (*p && *p != '\n')
+		p++;
+	if (*p)
+		p++;
+	*pp = p;
+	return v;
+}
+
+static int chat_media_poll(void)
+{
+	char done[24], meta[1200];
+	int rn, st = 0, err = 0;
+	unsigned int seq;
+	unsigned int dw = 0, dh = 0, nw = 0, nh = 0, alpha = 0;
+	unsigned int px_len = 0, dec_err = 0;
+
+	if (!chat_media_busy)
+		return 0;
+	chat_media_tty_drain();
+	rn = u_syscall3(SYS_READ_FILE, (int)chat_media_done, (int)done,
+			(int)(sizeof(done) - 1));
+	if (rn > 0) {
+		char *q;
+
+		done[rn] = '\0';
+		seq = (unsigned int)atoi(done);
+		if (seq != chat_media_seq)
+			return 0;
+		rn = u_syscall3(SYS_READ_FILE, (int)chat_media_meta, (int)meta,
+				(int)(sizeof(meta) - 1));
+		if (rn > 0) {
+			meta[rn] = '\0';
+			q = meta;
+			(void)chat_meta_uint(&q);       /* seq */
+			st = (int)chat_meta_uint(&q);
+			err = (int)chat_meta_uint(&q);
+			(void)chat_meta_uint(&q);       /* trunc */
+			while (*q && *q != '\n')         /* encoding */
+				q++;
+			if (*q)
+				q++;
+			while (*q && *q != '\n')         /* content type */
+				q++;
+			if (*q)
+				q++;
+			while (*q && *q != '\n')         /* final URL */
+				q++;
+			if (*q)
+				q++;
+			(void)chat_meta_uint(&q);       /* body bytes */
+			(void)chat_meta_uint(&q);       /* open */
+			(void)chat_meta_uint(&q);       /* first */
+			(void)chat_meta_uint(&q);       /* total */
+			dw = chat_meta_uint(&q);
+			dh = chat_meta_uint(&q);
+			nw = chat_meta_uint(&q);
+			nh = chat_meta_uint(&q);
+			alpha = chat_meta_uint(&q);
+			px_len = chat_meta_uint(&q);
+			dec_err = chat_meta_uint(&q);
+			(void)chat_meta_uint(&q);       /* dec ms */
 		}
+		if (st == 200 && !err && chat_media_pic) {
+			int stored = 0;
+
+			(void)nw;
+			(void)nh;
+			if (!chat_media_avatar && px_len && !dec_err &&
+			    dw && dh && px_len <= CHAT_IMG_FETCH_MAX) {
+				rn = u_syscall3(SYS_READ_FILE, (int)chat_media_px,
+						(int)chat_fetch_buf,
+						(int)CHAT_IMG_FETCH_MAX);
+				if (rn == (int)px_len &&
+				    chat_pic_store_px(chat_media_pic,
+						      chat_fetch_buf, px_len,
+						      dw, dh, (int)alpha) == 0)
+					stored = 1;
+			}
+			if (!stored) {
+				rn = u_syscall3(SYS_READ_FILE,
+						(int)chat_media_body,
+						(int)chat_fetch_buf,
+						(int)CHAT_IMG_FETCH_MAX);
+				if (rn > 0 &&
+				    chat_pic_store(chat_media_pic,
+						   (unsigned int)rn,
+						   chat_media_avatar) == 0)
+					stored = 1;
+			}
+			if (stored) {
+				chat_media_pic->state = CHAT_PIC_READY;
+				chat_media_pic->last_used = ++chat_pic_clock;
+				if (!chat_media_avatar) {
+					msgs_gen++;
+					if (at_bottom)
+						tl_scroll.offset = 1 << 28;
+				}
+			} else {
+				chat_media_pic->state = CHAT_PIC_FAILED;
+			}
+		} else if (chat_media_pic) {
+			chat_media_pic->state = CHAT_PIC_FAILED;
+		}
+		chat_media_pic = 0;
+		chat_media_busy = 0;
+		u_syscall1(SYS_UNLINK, (int)chat_media_done);
+		u_syscall1(SYS_UNLINK, (int)chat_media_meta);
+		u_syscall1(SYS_UNLINK, (int)chat_media_body);
+		u_syscall1(SYS_UNLINK, (int)chat_media_px);
+		ngui_invalidate(&ui);
+		return 1;
 	}
-	ngui_invalidate(&ui);
+	if (chat_media_pid > 0 && !u_alive((unsigned int)chat_media_pid)) {
+		chat_media_pid = -1;
+		if (!chat_media_retried && chat_media_start() == 0) {
+			chat_media_retried = 1;
+			return 0;
+		}
+		chat_media_fail();
+		return 1;
+	}
+	if ((int)(u_ticks_ms() - chat_media_deadline) >= 0) {
+		chat_media_stop();
+		chat_media_fail();
+		return 1;
+	}
+	return 0;
 }
 
 static void chat_media_tick(void)
@@ -741,15 +1038,30 @@ static void chat_media_tick(void)
 
 	if (screen != SCR_MAIN || offline)
 		return;
+	if (chat_media_busy) {
+		chat_media_poll();
+		return;
+	}
+	/* Holding a helper keeps a TCP socket open, which pauses the board's
+	 * net-dev watcher; release it when the window is not being used. */
+	if (!ui.focused) {
+		if (chat_media_pid > 0)
+			chat_media_stop();
+		return;
+	}
 	for (i = 0; i < nchat_images; i++) {
 		if (chat_images[i].state == CHAT_PIC_QUEUED) {
-			chat_pic_load(&chat_images[i], 0);
+			if (chat_media_submit(&chat_images[i], 0) != 0)
+				chat_images[i].state = CHAT_PIC_FAILED;
+			ngui_invalidate(&ui);
 			return;
 		}
 	}
 	for (i = 0; i < nchat_avatars; i++) {
 		if (chat_avatars[i].state == CHAT_PIC_QUEUED) {
-			chat_pic_load(&chat_avatars[i], 1);
+			if (chat_media_submit(&chat_avatars[i], 1) != 0)
+				chat_avatars[i].state = CHAT_PIC_FAILED;
+			ngui_invalidate(&ui);
 			return;
 		}
 	}
@@ -759,6 +1071,10 @@ static void chat_media_clear(void)
 {
 	int i;
 
+	chat_media_stop();
+	chat_media_pic = 0;
+	chat_media_busy = 0;
+	chat_media_retried = 0;
 	for (i = 0; i < nchat_images; i++)
 		chat_pic_free(&chat_images[i]);
 	for (i = 0; i < nchat_avatars; i++)

+ 182 - 8
ucmd_curl.c

@@ -14,6 +14,7 @@
 #include "ucmd_lib.h"
 #include "uhttp.h"
 #include "uhttp_cookie.h"
+#include "img.h"
 
 #define LINE_MAX_C 320
 
@@ -63,7 +64,7 @@ static const struct uhttp_hooks hooks = {
 
 #define FETCH_PATH_MAX 32
 #define FETCH_POST_MAX 16384u
-#define FETCH_MAX   (2u * 1024u * 1024u)
+#define FETCH_MAX   (4u * 1024u * 1024u)
 
 /* Each browser worker gets its own set of /share files; slot -1 keeps the
  * historical single-helper names for the one-shot __fetch mode. */
@@ -73,6 +74,7 @@ struct fetch_paths {
 	char meta[FETCH_PATH_MAX];
 	char done[FETCH_PATH_MAX];
 	char post[FETCH_PATH_MAX];
+	char px[FETCH_PATH_MAX];
 };
 static struct fetch_paths fp;
 
@@ -123,6 +125,117 @@ static void fetch_paths_init(unsigned int session, int slot)
 	fetch_path(fp.meta, sizeof(fp.meta), "meta", session, slot);
 	fetch_path(fp.done, sizeof(fp.done), "done", session, slot);
 	fetch_path(fp.post, sizeof(fp.post), "post", session, slot);
+	fetch_path(fp.px, sizeof(fp.px), "px", session, slot);
+}
+
+/* Optional decode in the fetch helper.  The browser's UI thread only has
+ * to copy the finished RGB565 bitmap; the JPEG IDCT/box filter runs in the
+ * helper process (and several helpers can decode in parallel). */
+#define DECODE_ARENA_MAX (24u * 1024u * 1024u)
+#define DECODE_PX_MAX    (8u * 1024u * 1024u)
+
+struct fetch_px {
+	unsigned char *data;
+	unsigned int len, dw, dh, nw, nh, alpha, format;
+	unsigned int dec_ms;
+	int err;
+};
+
+static void fetch_decode(const unsigned char *data, unsigned int len,
+			 unsigned int max_w, unsigned int max_h,
+			 struct fetch_px *out)
+{
+	struct img_info info;
+	struct img_options opt = {0};
+	struct img_mem mem;
+	struct img_image im;
+	unsigned char *arena;
+	unsigned int est, dw, dh, outlen, n;
+	int r;
+
+	memset(out, 0, sizeof(*out));
+	if (img_sniff(data, len, &info) != 0) {
+		out->err = 1;
+		return;
+	}
+	est = img_estimate(&info);
+	if (est == 0xFFFFFFFFu || est > DECODE_ARENA_MAX) {
+		out->err = 2;
+		return;
+	}
+	arena = (unsigned char *)malloc(est);
+	if (!arena) {
+		out->err = 3;
+		return;
+	}
+	img_mem_init(&mem, arena, est);
+	opt.max_pixels = 8u * 1024u * 1024u;
+	opt.max_w = max_w;
+	opt.max_h = max_h;
+	r = img_decode(data, len, &opt, &mem, &im);
+	if (r != 0 || !im.w || !im.h) {
+		out->err = 4;
+		free(arena);
+		return;
+	}
+	dw = im.w;
+	dh = im.h;
+	if (max_w && dw > max_w) {
+		dh = (unsigned int)((unsigned long long)dh * max_w / dw);
+		dw = max_w;
+	}
+	if (max_h && dh > max_h) {
+		dw = (unsigned int)((unsigned long long)dw * max_h / dh);
+		dh = max_h;
+	}
+	if (!dw)
+		dw = 1;
+	if (!dh)
+		dh = 1;
+	if (dw > 4096 || dh > 4096) {
+		out->err = 5;
+		free(arena);
+		return;
+	}
+	outlen = dw * dh * 2u + (im.alpha ? dw * dh : 0);
+	if (outlen > DECODE_PX_MAX) {
+		out->err = 5;
+		free(arena);
+		return;
+	}
+	out->data = (unsigned char *)malloc(outlen);
+	if (!out->data) {
+		out->err = 6;
+		free(arena);
+		return;
+	}
+	if (dw == im.w && dh == im.h) {
+		n = dw * dh;
+		memcpy(out->data, im.px, n * 2u);
+		if (im.alpha)
+			memcpy(out->data + n * 2u, im.alpha, n);
+	} else {
+		struct img_blit b;
+
+		memset(&b, 0, sizeof(b));
+		b.dst = (unsigned short *)out->data;
+		b.dst_w = (int)dw;
+		b.dst_h = (int)dh;
+		b.clip_w = (int)dw;
+		b.clip_h = (int)dh;
+		b.scale = ((unsigned int)dw << 16) / im.w;
+		b.dst_a = im.alpha ? out->data + dw * dh * 2u : 0;
+		b.flags = im.alpha ? IMG_BLIT_SAVE_ALPHA : 0;
+		img_draw(&im, &b);
+	}
+	out->len = outlen;
+	out->dw = dw;
+	out->dh = dh;
+	out->nw = info.width;
+	out->nh = info.height;
+	out->alpha = im.alpha ? 1 : 0;
+	out->format = info.format;
+	free(arena);
 }
 
 static void fetch_fail(unsigned int status, int err, const char *ctype,
@@ -146,11 +259,12 @@ static void fetch_fail(unsigned int status, int err, const char *ctype,
  * the parallel workers.  Re-read before each request and written back when
  * the response changed it; with several helpers the last writer wins. */
 #define FETCH_JAR_PATH "/share/bcookies"
+static char fetch_jar_path[64] = FETCH_JAR_PATH;
 static struct uhttp_jar fetch_jar;
 
 static void jar_load(void)
 {
-	int n = u_syscall3(SYS_READ_FILE, (int)FETCH_JAR_PATH,
+	int n = u_syscall3(SYS_READ_FILE, (int)fetch_jar_path,
 			   (int)&fetch_jar, (int)sizeof(fetch_jar));
 
 	if (n != (int)sizeof(fetch_jar))
@@ -203,7 +317,7 @@ static void jar_save(void)
 {
 	int i, n;
 
-	n = u_syscall3(SYS_READ_FILE, (int)FETCH_JAR_PATH, (int)&jar_file,
+	n = u_syscall3(SYS_READ_FILE, (int)fetch_jar_path, (int)&jar_file,
 		       (int)sizeof(jar_file));
 	if (n != (int)sizeof(jar_file))
 		uhttp_jar_init(&jar_file);
@@ -223,7 +337,7 @@ static void jar_save(void)
 		if (o->used && !jar_lookup(&fetch_jar, o))
 			jar_replay(o, 1);
 	}
-	u_syscall3(SYS_WRITE_FILE, (int)FETCH_JAR_PATH, (int)&jar_file,
+	u_syscall3(SYS_WRITE_FILE, (int)fetch_jar_path, (int)&jar_file,
 		   (int)sizeof(jar_file));
 }
 
@@ -235,7 +349,7 @@ struct fetch_times {
 
 static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
 		       int err, unsigned int body_len, unsigned int body_trunc,
-		       const struct fetch_times *t)
+		       const struct fetch_times *t, const struct fetch_px *px)
 {
 	char meta[1024];
 	unsigned int n = 0;
@@ -254,6 +368,16 @@ static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
 	app_dec(meta, &n, t ? t->open_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
 	app_dec(meta, &n, t ? t->first_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
 	app_dec(meta, &n, t ? t->total_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	/* Optional decoded bitmap: dw, dh, native w/h, alpha, px bytes, error,
+	 * format. */
+	app_dec(meta, &n, px ? px->dw : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, px ? px->dh : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, px ? px->nw : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, px ? px->nh : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, px ? px->alpha : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, px ? px->len : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, px ? (unsigned int)px->err : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, px ? px->dec_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
 	u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
 }
 
@@ -375,7 +499,7 @@ static void fetch_once(void)
 	if (body_len)
 		u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
 			   (int)body_len);
-	write_meta(0, 0, &h, err, body_len, body_trunc, &times);
+	write_meta(0, 0, &h, err, body_len, body_trunc, &times, 0);
 	uhttp_close(&h);
 	free(buf);
 	u_exit(err ? 1 : 0);
@@ -411,6 +535,15 @@ static void fetch_serve(const char *arg)
 				slot = slot * 10 + (*arg++ - '0');
 		}
 	}
+	while (*arg == ' ')
+		arg++;
+	if (*arg == '/') {
+		unsigned int n = 0;
+
+		while (*arg && *arg != ' ' && n + 1 < sizeof(fetch_jar_path))
+			fetch_jar_path[n++] = *arg++;
+		fetch_jar_path[n] = '\0';
+	}
 	fetch_paths_init(session, slot);
 
 	buf = (unsigned char *)malloc(FETCH_MAX);
@@ -426,7 +559,10 @@ static void fetch_serve(const char *arg)
 	for (;;) {
 		unsigned int seq = 0, cap = 0, body_len = 0, body_trunc = 0;
 		unsigned int post_len = 0, jar_changes;
+		unsigned int dec_w = 0, dec_h = 0;
+		int decode = 0;
 		int is_post = 0;
+		struct fetch_px px;
 		char method[12], ctype[96];
 
 		method[0] = '\0';
@@ -457,9 +593,31 @@ static void fetch_serve(const char *arg)
 				i++;
 			method[0] = '\0';
 			ctype[0] = '\0';
+			/* "<seq> <cap> D <max_w> <max_h> <url>": decode the
+			 * response at the display bounds before publishing it. */
+			if (!strncmp(req + i, "D ", 2)) {
+				i += 2;
+				while (req[i] == ' ')
+					i++;
+				while (req[i] >= '0' && req[i] <= '9') {
+					dec_w = dec_w * 10 +
+						(unsigned int)(req[i] - '0');
+					i++;
+				}
+				while (req[i] == ' ')
+					i++;
+				while (req[i] >= '0' && req[i] <= '9') {
+					dec_h = dec_h * 10 +
+						(unsigned int)(req[i] - '0');
+					i++;
+				}
+				while (req[i] == ' ')
+					i++;
+				decode = 1;
+			}
 			/* "X <method> <body length> <content type or -> <url>":
 			 * any method, for the browser's fetch()/XHR. */
-			if (!strncmp(req + i, "X ", 2)) {
+			if (!decode && !strncmp(req + i, "X ", 2)) {
 				unsigned int k = 0;
 
 				i += 2;
@@ -546,7 +704,23 @@ static void fetch_serve(const char *arg)
 		if (body_len)
 			u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
 				   (int)body_len);
-		write_meta(seq, 1, &h, err, body_len, body_trunc, &times);
+		memset(&px, 0, sizeof(px));
+		if (decode) {
+			u_syscall1(SYS_UNLINK, (int)fp.px);
+			if (body_len && !body_trunc && !err &&
+			    h.resp.status == 200 && !is_post) {
+				unsigned int td = u_ticks_ms();
+
+				fetch_decode(buf, body_len, dec_w, dec_h, &px);
+				px.dec_ms = u_ticks_ms() - td;
+				if (!px.err && px.data && px.len)
+					u_syscall3(SYS_WRITE_FILE, (int)fp.px,
+						   (int)px.data, (int)px.len);
+			}
+		}
+		write_meta(seq, 1, &h, err, body_len, body_trunc, &times, &px);
+		if (px.data)
+			free(px.data);
 		app_dec(done, &dn, seq, sizeof(done));
 		u_syscall3(SYS_WRITE_FILE, (int)fp.done, (int)done, (int)dn);
 

+ 19 - 17
ucmd_netmenu.c

@@ -1127,7 +1127,7 @@ static void draw_wifi_list(struct ngui_rect area, int *y)
 
 	for (i = 0; i < nbss; i++) {
 		struct sys_wifi_bss *b = &bss[i];
-		struct ngui_rect r = { area.x, *y, area.w - 18, WIFI_ROW_H };
+		struct ngui_rect r = { area.x, *y, area.w, WIFI_ROW_H };
 
 		if (r.y + r.h > area.y && r.y < area.y + area.h) {
 			ssid_name(b, ssid);
@@ -1344,10 +1344,10 @@ static void draw_tiles(struct ngui_rect area, int *yy)
 	n++;
 
 	nrows = (n + 1) / 2;
-	colw = (area.w - 18 - MENU_GAP) / 2;
+	colw = (area.w - MENU_GAP) / 2;
 	for (i = 0; i < n; i++) {
 		int col = i & 1;
-		int w = (i + 1 < n) ? colw : (col == 0 ? area.w - 18 :
+		int w = (i + 1 < n) ? colw : (col == 0 ? area.w :
 					      colw);
 		struct ngui_rect r = {
 			area.x + col * (colw + MENU_GAP),
@@ -1397,11 +1397,11 @@ static void draw_network_section(struct ngui_rect area, int *yy)
 	struct ngui_rect r;
 	int i, shown = 0, more, n, y = *yy;
 
-	r = (struct ngui_rect){ area.x, y, area.w - 18, SECTION_H };
+	r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
 	ngui_section(&ui, r, "Network", 0, 0);
 	y += SECTION_H;
 
-	r = (struct ngui_rect){ area.x, y, area.w - 18, 64 };
+	r = (struct ngui_rect){ area.x, y, area.w, 64 };
 	draw_status_card(r);
 	y += 64 + MENU_GAP;
 
@@ -1412,7 +1412,7 @@ static void draw_network_section(struct ngui_rect area, int *yy)
 	if (shown > 4)
 		shown = 4;
 
-	r = (struct ngui_rect){ area.x, y, area.w - 18, SECTION_H };
+	r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
 	if (ngui_section(&ui, r, "Devices", ID_REFRESH, "Refresh"))
 		actions |= ACT_REFRESH;
 	y += SECTION_H;
@@ -1421,7 +1421,7 @@ static void draw_network_section(struct ngui_rect area, int *yy)
 	for (i = 0; i < ndev && n < shown; i++) {
 		if (devs[i].kind == SYS_NETDEV_WIFI)
 			continue;
-		r = (struct ngui_rect){ area.x, y, area.w - 18, DEV_ROW_H };
+		r = (struct ngui_rect){ area.x, y, area.w, DEV_ROW_H };
 		if (r.y + r.h > area.y && r.y < area.y + area.h) {
 			row.title = devs[i].name;
 			row.subtitle = devs[i].chip;
@@ -1456,7 +1456,7 @@ static void draw_network_section(struct ngui_rect area, int *yy)
 	}
 
 	y += MENU_GAP;
-	r = (struct ngui_rect){ area.x, y, area.w - 18, SECTION_H };
+	r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
 	if (wifi_present && wst.state >= SYS_WIFI_ST_ASSOC) {
 		if (ngui_section(&ui, r, "Wi-Fi", ID_DISCONNECT,
 				 "Disconnect"))
@@ -1478,7 +1478,7 @@ static void draw_network_section(struct ngui_rect area, int *yy)
 		int di;
 
 		y += MENU_GAP;
-		r = (struct ngui_rect){ area.x, y, area.w - 18, SECTION_H };
+		r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
 		ngui_section(&ui, r, "Connection", 0, 0);
 		y += SECTION_H;
 		for (di = 0; di < 4; di++) {
@@ -1497,7 +1497,7 @@ static void draw_network_section(struct ngui_rect area, int *yy)
 				vp = put_mac(vp, net.mac);
 			*vp = '\0';
 
-			r = (struct ngui_rect){ area.x, y, area.w - 18, 30 };
+			r = (struct ngui_rect){ area.x, y, area.w, 30 };
 			if (r.y + r.h > area.y && r.y < area.y + area.h) {
 				row.title = labels[di];
 				row.subtitle = 0;
@@ -1514,7 +1514,7 @@ static void draw_network_section(struct ngui_rect area, int *yy)
 		char nbuf[12], *np = nbuf;
 
 		y += MENU_GAP;
-		r = (struct ngui_rect){ area.x, y, area.w - 18, SAVED_ROW_H };
+		r = (struct ngui_rect){ area.x, y, area.w, SAVED_ROW_H };
 		if (r.y + r.h > area.y && r.y < area.y + area.h) {
 			np = put_dec(np, (unsigned int)nsaved);
 			*np = '\0';
@@ -1555,11 +1555,11 @@ static void draw_power_section(struct ngui_rect area, int *yy)
 	struct ngui_rect r;
 	int i, y = *yy;
 
-	r = (struct ngui_rect){ area.x, y, area.w - 18, SECTION_H };
+	r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
 	ngui_section(&ui, r, "Power mode", 0, 0);
 	y += SECTION_H;
 	for (i = 0; i < 3; i++) {
-		r = (struct ngui_rect){ area.x, y, area.w - 18, 42 };
+		r = (struct ngui_rect){ area.x, y, area.w, 42 };
 		if (r.y + r.h > area.y && r.y < area.y + area.h) {
 			row.title = labels[i];
 			row.subtitle = subs[i];
@@ -1591,10 +1591,10 @@ static void date_field(struct ngui_rect area, int *yy, const char *label,
 	struct ngui_rect r;
 	int ev;
 
-	r = (struct ngui_rect){ area.x + 12, *yy, area.w - 42, 18 };
+	r = (struct ngui_rect){ area.x + 12, *yy, area.w - 24, 18 };
 	ngui_text(&ui, r.x, r.y, label, ui.theme.text_dim);
 	*yy += 18;
-	r = (struct ngui_rect){ area.x + 12, *yy, area.w - 42, 32 };
+	r = (struct ngui_rect){ area.x + 12, *yy, area.w - 24, 32 };
 	ev = ngui_text_input(&ui, id, r, in);
 	if (ev & NGUI_INPUT_SUBMIT)
 		actions |= id == ID_NTP_FIELD ? ACT_DATE_NTP : ACT_DATE_SET;
@@ -1611,7 +1611,7 @@ static void draw_date_section(struct ngui_rect area, int *yy)
 	char cur[64], *p;
 	int y = *yy;
 
-	r = (struct ngui_rect){ area.x, y, area.w - 18, SECTION_H };
+	r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
 	ngui_section(&ui, r, "Date & Time", 0, 0);
 	y += SECTION_H;
 
@@ -1924,7 +1924,7 @@ static void draw_saved(void)
 			row.x = list.x;
 			row.y = list.y - saved_scroll.offset +
 				i * SAVED_LIST_ROW_H;
-			row.w = list.w - 16;
+			row.w = list.w;
 			row.h = SAVED_LIST_ROW_H;
 			if (row.y + row.h <= list.y ||
 			    row.y >= list.y + list.h)
@@ -1999,6 +1999,8 @@ void _start(const char *arg)
 	view = VIEW_MAIN;
 	expanded = EXPAND_NONE;
 	main_scroll.offset = 0;
+	main_scroll.hide_bar = 1;
+	saved_scroll.hide_bar = 1;
 
 	load_devices();
 	load_status();

+ 1 - 1
ucmd_view.c

@@ -467,7 +467,7 @@ void _start(const char *arg)
 {
 	struct img_info info;
 	struct win_info wi;
-	struct img_options opt;
+	struct img_options opt = {0};
 	struct sys_stat st;
 	unsigned int n, fcap, est;
 	int r;

+ 53 - 20
uhttp.c

@@ -4,6 +4,8 @@
 #include "uhttp.h"
 #include "tls.h"
 
+#include <string.h>
+
 /* ---- tiny string helpers ---- */
 
 static unsigned int s_len(const char *s)
@@ -930,7 +932,6 @@ int uhttp_read(struct uhttp *h, void *vbuf, unsigned int max,
 {
 	unsigned char *out = (unsigned char *)vbuf;
 	unsigned int n = 0;
-	int c;
 
 	if (h->error)
 		return h->error;
@@ -987,17 +988,31 @@ int uhttp_read(struct uhttp *h, void *vbuf, unsigned int max,
 					h->chunk_state = 1;
 				}
 			} else if (h->chunk_state == 1) {
-				c = reader_getc(h, timeout_ms);
-				if (c < 0) {
-					if (h->eof)
-						h->peer_close = 1;
-					if (n)
-						break;
-					return h->error ? h->error : 0;
+				unsigned int avail, want;
+
+				if (h->pos >= h->len) {
+					if (!reader_fill(h, timeout_ms)) {
+						if (h->eof)
+							h->peer_close = 1;
+						if (n)
+							break;
+						return h->error ? h->error : 0;
+					}
 				}
-				out[n++] = (unsigned char)c;
-				h->body_read++;
-				if (--h->chunk_left == 0)
+				avail = h->len - h->pos;
+				want = max - n;
+				if (want > h->chunk_left)
+					want = h->chunk_left;
+				if (want > avail)
+					want = avail;
+				if (!want)
+					continue;
+				memcpy(out + n, h->buf + h->pos, want);
+				h->pos += want;
+				n += want;
+				h->body_read += want;
+				h->chunk_left -= want;
+				if (h->chunk_left == 0)
 					h->chunk_state = 2;
 			} else if (h->chunk_state == 2) {
 				(void)reader_getc(h, timeout_ms); /* CR */
@@ -1015,21 +1030,39 @@ int uhttp_read(struct uhttp *h, void *vbuf, unsigned int max,
 				}
 			}
 		} else {
+			unsigned int avail, want;
+
 			if (h->resp.have_length &&
 			    h->body_read >= h->resp.content_length) {
 				h->eof = 1;
 				break;
 			}
-			c = reader_getc(h, timeout_ms);
-			if (c < 0) {
-				if (h->eof)
-					h->peer_close = 1;
-				if (n)
-					break;
-				return h->error ? h->error : 0;
+			if (h->pos >= h->len) {
+				if (!reader_fill(h, timeout_ms)) {
+					if (h->eof)
+						h->peer_close = 1;
+					if (n)
+						break;
+					return h->error ? h->error : 0;
+				}
+			}
+			avail = h->len - h->pos;
+			want = max - n;
+			if (h->resp.have_length) {
+				unsigned int left = h->resp.content_length -
+						    h->body_read;
+
+				if (want > left)
+					want = left;
 			}
-			out[n++] = (unsigned char)c;
-			h->body_read++;
+			if (want > avail)
+				want = avail;
+			if (!want)
+				continue;
+			memcpy(out + n, h->buf + h->pos, want);
+			h->pos += want;
+			n += want;
+			h->body_read += want;
 		}
 	}
 	return (int)n;

+ 1 - 1
uhttp.h

@@ -18,7 +18,7 @@
 #define UHTTP_PATH_MAX       1024   /* path + query (OIDC URLs are long) */
 #define UHTTP_LOC_MAX        1280
 #define UHTTP_LINE_MAX       1536   /* Location / Set-Cookie header lines */
-#define UHTTP_READER_MAX     2048
+#define UHTTP_READER_MAX     16384
 #define UHTTP_REQ_MAX        4096   /* request line + headers */
 #define UHTTP_MAX_REDIRECTS  8
 #define UHTTP_IDLE_TIMEOUT_MS  10000

Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů