BlueGit

ict rusty-teams

fix(avatar): keep atlas neighbours out of photo edges

- Avatar textures get a 2 px margin of repeated edge texels
- Painted with image bounds widened by the margin, so linear sampling never reaches the next atlas tile

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-11-08 · 3 weeks ago

Hash
08da47bf524a4077c9b7c28aecfaacc6bd3a50cb
Parent
d69b1473
2 files changed +31 −4
crates/app-base/src/avatar_image.rs +28 −2
@@ -8,6 +8,7 @@ use image::{Frame, ImageFormat, ImageReader, RgbaImage};
8 8 const MAX_TEXTURES: usize = 8;
9 9 const SOURCE_SIDE: u32 = 240;
10 10 const SOURCE_LIMIT: u32 = 2 * SOURCE_SIDE;
11 pub const TEXTURE_MARGIN: u32 = 2;
11 12
12 13 pub struct AvatarPhoto {
13 14 bytes: Vec<u8>,
@@ -51,7 +52,7 @@ impl AvatarPhoto {
51 52 evicted: None,
52 53 });
53 54 }
54 let pixels = square_texture(&self.bytes, side)?;
55 let pixels = with_edge_margin(&square_texture(&self.bytes, side)?, TEXTURE_MARGIN);
55 56 let image = Arc::new(RenderImage::new(vec![Frame::new(pixels)]));
56 57 let evicted = (textures.len() >= MAX_TEXTURES).then(|| textures.remove(0).1);
57 58 textures.push((side, image.clone()));
@@ -81,6 +82,17 @@ fn round_half_toward_zero(value: f32) -> f32 {
81 82 value.signum() * (value.abs() - 0.5).ceil()
82 83 }
83 84
85 /// The sprite atlas packs images without gaps and samples linearly, so the outer texels
86 /// of a photo blend with its neighbour. Repeated edge texels keep that blend inside the photo.
87 pub fn with_edge_margin(texture: &RgbaImage, margin: u32) -> RgbaImage {
88 let (width, height) = texture.dimensions();
89 RgbaImage::from_fn(width + 2 * margin, height + 2 * margin, |x, y| {
90 let source_x = x.saturating_sub(margin).min(width - 1);
91 let source_y = y.saturating_sub(margin).min(height - 1);
92 *texture.get_pixel(source_x, source_y)
93 })
94 }
95
84 96 pub fn square_texture(bytes: &[u8], side: u32) -> Option<RgbaImage> {
85 97 let decoded = image::load_from_memory(bytes).ok()?.to_rgba8();
86 98 let crop = decoded.width().min(decoded.height());
@@ -126,6 +138,17 @@ mod tests {
126 138 assert_eq!(alpha, 255);
127 139 }
128 140
141 #[test]
142 fn the_margin_repeats_the_edge_texels() {
143 let mut texture = RgbaImage::from_pixel(3, 3, Rgba([1, 2, 3, 255]));
144 texture.put_pixel(0, 0, Rgba([9, 9, 9, 255]));
145 let padded = with_edge_margin(&texture, 2);
146 assert_eq!(padded.dimensions(), (7, 7));
147 assert_eq!(*padded.get_pixel(0, 0), Rgba([9, 9, 9, 255]));
148 assert_eq!(*padded.get_pixel(1, 2), Rgba([9, 9, 9, 255]));
149 assert_eq!(*padded.get_pixel(6, 6), Rgba([1, 2, 3, 255]));
150 }
151
129 152 #[test]
130 153 fn non_square_photos_are_center_cropped() {
131 154 assert_eq!(
@@ -171,7 +194,10 @@ mod tests {
171 194 assert_eq!(dimensions, (SOURCE_SIDE, SOURCE_SIDE));
172 195 let texture = photo.texture(20).unwrap().image;
173 196 let raw = texture.as_bytes(0).unwrap().to_vec();
174 let Rgba([blue, _, red, _]) = *RgbaImage::from_raw(20, 20, raw).unwrap().get_pixel(10, 10);
197 let padded_side = 20 + 2 * TEXTURE_MARGIN;
198 let Rgba([blue, _, red, _]) = *RgbaImage::from_raw(padded_side, padded_side, raw)
199 .unwrap()
200 .get_pixel(10, 10);
175 201 assert!(
176 202 blue.abs_diff(50) <= 3 && red.abs_diff(200) <= 3,
177 203 "{blue} {red}"
@@ -8,6 +8,7 @@ use image::{Frame, ImageFormat, ImageReader, RgbaImage};
8const MAX_TEXTURES: usize = 8;
8const MAX_TEXTURES: usize = 8;
9const SOURCE_SIDE: u32 = 240;
9const SOURCE_SIDE: u32 = 240;
10const SOURCE_LIMIT: u32 = 2 * SOURCE_SIDE;
10const SOURCE_LIMIT: u32 = 2 * SOURCE_SIDE;
11pub const TEXTURE_MARGIN: u32 = 2;
11
12
12pub struct AvatarPhoto {
13pub struct AvatarPhoto {
13 bytes: Vec<u8>,
14 bytes: Vec<u8>,
@@ -51,7 +52,7 @@ impl AvatarPhoto {
51 evicted: None,
52 evicted: None,
52 });
53 });
53 }
54 }
54 let pixels = square_texture(&self.bytes, side)?;
55 let pixels = with_edge_margin(&square_texture(&self.bytes, side)?, TEXTURE_MARGIN);
55 let image = Arc::new(RenderImage::new(vec![Frame::new(pixels)]));
56 let image = Arc::new(RenderImage::new(vec![Frame::new(pixels)]));
56 let evicted = (textures.len() >= MAX_TEXTURES).then(|| textures.remove(0).1);
57 let evicted = (textures.len() >= MAX_TEXTURES).then(|| textures.remove(0).1);
57 textures.push((side, image.clone()));
58 textures.push((side, image.clone()));
@@ -81,6 +82,17 @@ fn round_half_toward_zero(value: f32) -> f32 {
81 value.signum() * (value.abs() - 0.5).ceil()
82 value.signum() * (value.abs() - 0.5).ceil()
82}
83}
83
84
85/// The sprite atlas packs images without gaps and samples linearly, so the outer texels
86/// of a photo blend with its neighbour. Repeated edge texels keep that blend inside the photo.
87pub fn with_edge_margin(texture: &RgbaImage, margin: u32) -> RgbaImage {
88 let (width, height) = texture.dimensions();
89 RgbaImage::from_fn(width + 2 * margin, height + 2 * margin, |x, y| {
90 let source_x = x.saturating_sub(margin).min(width - 1);
91 let source_y = y.saturating_sub(margin).min(height - 1);
92 *texture.get_pixel(source_x, source_y)
93 })
94}
95
84pub fn square_texture(bytes: &[u8], side: u32) -> Option<RgbaImage> {
96pub fn square_texture(bytes: &[u8], side: u32) -> Option<RgbaImage> {
85 let decoded = image::load_from_memory(bytes).ok()?.to_rgba8();
97 let decoded = image::load_from_memory(bytes).ok()?.to_rgba8();
86 let crop = decoded.width().min(decoded.height());
98 let crop = decoded.width().min(decoded.height());
@@ -126,6 +138,17 @@ mod tests {
126 assert_eq!(alpha, 255);
138 assert_eq!(alpha, 255);
127 }
139 }
128
140
141 #[test]
142 fn the_margin_repeats_the_edge_texels() {
143 let mut texture = RgbaImage::from_pixel(3, 3, Rgba([1, 2, 3, 255]));
144 texture.put_pixel(0, 0, Rgba([9, 9, 9, 255]));
145 let padded = with_edge_margin(&texture, 2);
146 assert_eq!(padded.dimensions(), (7, 7));
147 assert_eq!(*padded.get_pixel(0, 0), Rgba([9, 9, 9, 255]));
148 assert_eq!(*padded.get_pixel(1, 2), Rgba([9, 9, 9, 255]));
149 assert_eq!(*padded.get_pixel(6, 6), Rgba([1, 2, 3, 255]));
150 }
151
129 #[test]
152 #[test]
130 fn non_square_photos_are_center_cropped() {
153 fn non_square_photos_are_center_cropped() {
131 assert_eq!(
154 assert_eq!(
@@ -171,7 +194,10 @@ mod tests {
171 assert_eq!(dimensions, (SOURCE_SIDE, SOURCE_SIDE));
194 assert_eq!(dimensions, (SOURCE_SIDE, SOURCE_SIDE));
172 let texture = photo.texture(20).unwrap().image;
195 let texture = photo.texture(20).unwrap().image;
173 let raw = texture.as_bytes(0).unwrap().to_vec();
196 let raw = texture.as_bytes(0).unwrap().to_vec();
174 let Rgba([blue, _, red, _]) = *RgbaImage::from_raw(20, 20, raw).unwrap().get_pixel(10, 10);
197 let padded_side = 20 + 2 * TEXTURE_MARGIN;
198 let Rgba([blue, _, red, _]) = *RgbaImage::from_raw(padded_side, padded_side, raw)
199 .unwrap()
200 .get_pixel(10, 10);
175 assert!(
201 assert!(
176 blue.abs_diff(50) <= 3 && red.abs_diff(200) <= 3,
202 blue.abs_diff(50) <= 3 && red.abs_diff(200) <= 3,
177 "{blue} {red}"
203 "{blue} {red}"
crates/app/src/views/avatar.rs +3 −2
@@ -6,7 +6,7 @@ use std::time::Duration;
6 6
7 7 use gpui_kit::assets::IconName;
8 8
9 use crate::avatar_image::{AvatarPhoto, device_span};
9 use crate::avatar_image::{AvatarPhoto, TEXTURE_MARGIN, device_span};
10 10 use crate::card_state::BotIdentity;
11 11 use crate::data::{AvatarState, Directory, Presence, PresenceKind};
12 12 use crate::format;
@@ -89,9 +89,10 @@ fn photo_disc(photo: &Arc<AvatarPhoto>, size: f32, radius: f32) -> AnyElement {
89 89 if let Some(evicted) = texture.evicted {
90 90 cx.drop_image(evicted, Some(window));
91 91 }
92 let margin = px(TEXTURE_MARGIN as f32 / scale_factor);
92 93 let _ = window.paint_image(
93 94 bounds,
94 bounds,
95 bounds.dilate(margin),
95 96 Corners::all(px(radius)),
96 97 texture.image,
97 98 0,
@@ -6,7 +6,7 @@ use std::time::Duration;
6
6
7use gpui_kit::assets::IconName;
7use gpui_kit::assets::IconName;
8
8
9use crate::avatar_image::{AvatarPhoto, device_span};
9use crate::avatar_image::{AvatarPhoto, TEXTURE_MARGIN, device_span};
10use crate::card_state::BotIdentity;
10use crate::card_state::BotIdentity;
11use crate::data::{AvatarState, Directory, Presence, PresenceKind};
11use crate::data::{AvatarState, Directory, Presence, PresenceKind};
12use crate::format;
12use crate::format;
@@ -89,9 +89,10 @@ fn photo_disc(photo: &Arc<AvatarPhoto>, size: f32, radius: f32) -> AnyElement {
89 if let Some(evicted) = texture.evicted {
89 if let Some(evicted) = texture.evicted {
90 cx.drop_image(evicted, Some(window));
90 cx.drop_image(evicted, Some(window));
91 }
91 }
92 let margin = px(TEXTURE_MARGIN as f32 / scale_factor);
92 let _ = window.paint_image(
93 let _ = window.paint_image(
93 bounds,
94 bounds,
94 bounds,
95 bounds.dilate(margin),
95 Corners::all(px(radius)),
96 Corners::all(px(radius)),
96 texture.image,
97 texture.image,
97 0,
98 0,