|
|
@@ -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;
|