BlueGit

ict rusty-teams

fix(avatars): trim the light outer edge of profile photos

- Many small Microsoft profile photos carry a light outer row or column; the round avatar touched it at the top and right edge midpoints
- One source pixel per 48 is cut from every side before scaling

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

metis Daniel Däschle · pushed by metis

Authored 2026-11-19 · 12 days ago

Hash
898839c63e93524fb04d7d83f5bf6c9ee04072a5
Parent
6be5cb02
1 file changed +29 −3
crates/app-base/src/avatar_image.rs +29 −3
@@ -97,11 +97,20 @@ pub fn with_edge_margin(texture: &RgbaImage, margin: u32) -> RgbaImage {
97 97 })
98 98 }
99 99
100 /// Microsoft's small profile photos often carry a light outer row or column; the disc touches it at the edge midpoints.
101 const EDGE_TRIM_PER: u32 = 48;
102
100 103 pub fn square_texture(bytes: &[u8], side: u32) -> Option<RgbaImage> {
101 104 let decoded = image::load_from_memory(bytes).ok()?.to_rgba8();
102 let crop = decoded.width().min(decoded.height());
103 let left = (decoded.width() - crop) / 2;
104 let top = (decoded.height() - crop) / 2;
105 let full = decoded.width().min(decoded.height());
106 let trim = if full > 2 {
107 (full / EDGE_TRIM_PER).max(1)
108 } else {
109 0
110 };
111 let crop = full - 2 * trim;
112 let left = (decoded.width() - full) / 2 + trim;
113 let top = (decoded.height() - full) / 2 + trim;
105 114 let square = image::imageops::crop_imm(&decoded, left, top, crop, crop).to_image();
106 115 let mut texture = if crop == side {
107 116 square
@@ -153,6 +162,23 @@ mod tests {
153 162 assert_eq!(*padded.get_pixel(6, 6), Rgba([1, 2, 3, 255]));
154 163 }
155 164
165 #[test]
166 fn a_light_outer_edge_of_the_photo_is_trimmed() {
167 let mut source = RgbaImage::from_pixel(48, 48, Rgba([40, 40, 40, 255]));
168 for index in 0..48 {
169 source.put_pixel(index, 0, Rgba([255, 255, 255, 255]));
170 source.put_pixel(47, index, Rgba([255, 255, 255, 255]));
171 }
172 let mut encoded = Vec::new();
173 image::DynamicImage::ImageRgba8(source)
174 .write_to(&mut Cursor::new(&mut encoded), ImageFormat::Png)
175 .unwrap();
176 let texture = square_texture(&encoded, 32).unwrap();
177 let Rgba([top, ..]) = *texture.get_pixel(16, 0);
178 let Rgba([right, ..]) = *texture.get_pixel(31, 16);
179 assert!(top < 80 && right < 80, "top {top}, right {right}");
180 }
181
156 182 #[test]
157 183 fn non_square_photos_are_center_cropped() {
158 184 assert_eq!(
@@ -97,11 +97,20 @@ pub fn with_edge_margin(texture: &RgbaImage, margin: u32) -> RgbaImage {
97 })
97 })
98}
98}
99
99
100/// Microsoft's small profile photos often carry a light outer row or column; the disc touches it at the edge midpoints.
101const EDGE_TRIM_PER: u32 = 48;
102
100pub fn square_texture(bytes: &[u8], side: u32) -> Option<RgbaImage> {
103pub fn square_texture(bytes: &[u8], side: u32) -> Option<RgbaImage> {
101 let decoded = image::load_from_memory(bytes).ok()?.to_rgba8();
104 let decoded = image::load_from_memory(bytes).ok()?.to_rgba8();
102 let crop = decoded.width().min(decoded.height());
105 let full = decoded.width().min(decoded.height());
103 let left = (decoded.width() - crop) / 2;
106 let trim = if full > 2 {
104 let top = (decoded.height() - crop) / 2;
107 (full / EDGE_TRIM_PER).max(1)
108 } else {
109 0
110 };
111 let crop = full - 2 * trim;
112 let left = (decoded.width() - full) / 2 + trim;
113 let top = (decoded.height() - full) / 2 + trim;
105 let square = image::imageops::crop_imm(&decoded, left, top, crop, crop).to_image();
114 let square = image::imageops::crop_imm(&decoded, left, top, crop, crop).to_image();
106 let mut texture = if crop == side {
115 let mut texture = if crop == side {
107 square
116 square
@@ -153,6 +162,23 @@ mod tests {
153 assert_eq!(*padded.get_pixel(6, 6), Rgba([1, 2, 3, 255]));
162 assert_eq!(*padded.get_pixel(6, 6), Rgba([1, 2, 3, 255]));
154 }
163 }
155
164
165 #[test]
166 fn a_light_outer_edge_of_the_photo_is_trimmed() {
167 let mut source = RgbaImage::from_pixel(48, 48, Rgba([40, 40, 40, 255]));
168 for index in 0..48 {
169 source.put_pixel(index, 0, Rgba([255, 255, 255, 255]));
170 source.put_pixel(47, index, Rgba([255, 255, 255, 255]));
171 }
172 let mut encoded = Vec::new();
173 image::DynamicImage::ImageRgba8(source)
174 .write_to(&mut Cursor::new(&mut encoded), ImageFormat::Png)
175 .unwrap();
176 let texture = square_texture(&encoded, 32).unwrap();
177 let Rgba([top, ..]) = *texture.get_pixel(16, 0);
178 let Rgba([right, ..]) = *texture.get_pixel(31, 16);
179 assert!(top < 80 && right < 80, "top {top}, right {right}");
180 }
181
156 #[test]
182 #[test]
157 fn non_square_photos_are_center_cropped() {
183 fn non_square_photos_are_center_cropped() {
158 assert_eq!(
184 assert_eq!(