Просмотр исходного кода

Clip chat avatars to circles

Avatar bitmaps now get a 4x4 supersampled circular alpha mask when they are cached, so message avatars and the New DM picker show round pictures instead of square images over the round fallback.  The initials fallback is unchanged, and the cache keeps the alpha plane for opaque pictures as well.

AGENTS.md and docs (network, graphics) updated.

The chat e2e run passes; its screenshots show the circular avatars.
Johannes Zellner 1 неделя назад
Родитель
Сommit
3527e786f9
4 измененных файлов с 48 добавлено и 11 удалено
  1. 6 6
      AGENTS.md
  2. 1 1
      docs/graphics.html
  3. 4 3
      docs/network.html
  4. 37 1
      ucmd_chat.c

+ 6 - 6
AGENTS.md

@@ -1331,12 +1331,12 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   since the floppy needs IRQ 6.
 - **Chat client**: `ucmd_chat.c` (`chat [config-path]`, top-bar `Chat`
   button) is a native Cloudron Chat client on ngui: channel/DM sidebar with
-  unread badges, presence dots and previews; a timeline with profile-picture
-  avatars (initials until the picture loads, or when there is none), author
-  grouping, UTC times, day headers, reply snippets, inline image
-  attachments/link-preview images and an attachments/thread/reaction line;
-  a composer; browse/join and new-DM pickers.  It talks to the Chat REST API
-  (`/api/me`, `/api/channels`, `/api/dms`, `/api/channels/public`,
+  unread badges, presence dots and previews; a timeline with circular
+  profile-picture avatars (initials until the picture loads, or when there is
+  none), author grouping, UTC times, day headers, reply snippets, inline
+  image attachments/link-preview images and an attachments/thread/reaction
+  line; a composer; browse/join and new-DM pickers.  It talks to the Chat REST
+  API (`/api/me`, `/api/channels`, `/api/dms`, `/api/channels/public`,
   `/api/users`, messages with `?limit=50`, `&before=` and `&after=`, POST
   messages with a `clientId`, `/read`, `/join`, `/api/dms`) through one
   `struct uhttp` with a cookie jar.  Message images and avatars use the same

+ 1 - 1
docs/graphics.html

@@ -377,7 +377,7 @@
 			<li><strong>Formats.</strong> PNG, baseline and progressive JPEG, animated GIF, BMP, TGA, PNM, ICO/CUR and PAM.</li>
 			<li><strong>Drawing.</strong> <code>img_draw()</code> is a clipped, scaled RGB565 blit: box-filter downscale, bilinear upscale, alpha over a checkerboard or over the destination.</li>
 			<li><strong>Orientation.</strong> EXIF orientation is applied; arithmetic JPEG and CMYK are rejected.</li>
-			<li><strong>Cost.</strong> Downscaling runs on the CPU. The viewer caches one image; the browser keeps a display bitmap but still rescales on repaint; the chat client caches up to 480x320 message images and 32x32 avatars.</li>
+			<li><strong>Cost.</strong> Downscaling runs on the CPU. The viewer caches one image; the browser keeps a display bitmap but still rescales on repaint; the chat client caches up to 480x320 message images and 32x32 circular avatars.</li>
 		</ul>
 	</section>
 

+ 4 - 3
docs/network.html

@@ -456,9 +456,10 @@
 			image library (PNG, JPEG, GIF first frame, BMP, TGA, PNM, ICO
 			and PAM).  Message images are scaled to at most 480x320 and
 			cached in a 16-entry, 6 MB LRU; avatars are 32x32 with a
-			64-entry cache and fall back to initials until they load or
-			when the account has no picture.  The fetch is capped at 4 MB
-			and the decoder at 4 MP / 12 MB of scratch.
+			64-entry cache and a circular alpha mask, and fall back to
+			initials until they load or when the account has no picture.
+			The fetch is capped at 4 MB and the decoder at 4 MP / 12 MB
+			of scratch.
 		</p>
 		<p>
 			There is no WebSocket: the open conversation is polled with

+ 37 - 1
ucmd_chat.c

@@ -583,6 +583,38 @@ static unsigned char *chat_pic_arena_get(unsigned int need)
 	return chat_arena;
 }
 
+/* Clip the scaled avatar to a circle with a 4x4 supersampled edge.  The RGB
+ * pixels stay untouched; only the alpha plane changes. */
+static void chat_avatar_mask(unsigned char *alpha, unsigned int w,
+			     unsigned int h)
+{
+	unsigned int x, y;
+
+	for (y = 0; y < h; y++) {
+		for (x = 0; x < w; x++) {
+			unsigned int hits = 0;
+			int sx, sy;
+
+			for (sy = 0; sy < 4; sy++) {
+				int py = (int)y * 8 + sy * 2 + 1;
+				int dy = py - (int)h * 4;
+
+				for (sx = 0; sx < 4; sx++) {
+					int px = (int)x * 8 + sx * 2 + 1;
+					int dx = px - (int)w * 4;
+
+					if (dx * dx + dy * dy <=
+					    (int)w * 4 * (int)w * 4)
+						hits++;
+				}
+			}
+			alpha[y * w + x] =
+				(unsigned char)((unsigned int)alpha[y * w + x] *
+						hits / 16u);
+		}
+	}
+}
+
 static int chat_pic_store(struct chat_pic *p, unsigned int len, int avatar)
 {
 	struct img_info info;
@@ -635,7 +667,9 @@ static int chat_pic_store(struct chat_pic *p, unsigned int len, int avatar)
 		return -1;
 	for (i = 0; i < dw * dh; i++)
 		px[i] = avatar ? 0xFFFF : ui.theme.bg;
-	if (avatar && decoded.alpha) {
+	if (avatar) {
+		/* Keep an alpha plane even for an opaque picture: the circular
+		 * mask below clips it to the avatar circle. */
 		alpha = (unsigned char *)malloc(dw * dh);
 		if (alpha)
 			memset(alpha, 255, dw * dh);
@@ -657,6 +691,8 @@ static int chat_pic_store(struct chat_pic *p, unsigned int len, int avatar)
 	blit.flags = alpha ? IMG_BLIT_SAVE_ALPHA :
 		     (decoded.alpha ? IMG_BLIT_OVER_DST : 0);
 	img_draw(&decoded, &blit);
+	if (avatar && alpha)
+		chat_avatar_mask(alpha, dw, dh);
 
 	p->img.format = decoded.format;
 	p->img.w = dw;