Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Save for Web: JPEG chroma is full (4:4:4) above quality 50, as in Photoshop (#4823)

Gert Pellin - GPE88ed837a86e5
diff --git a/crates/codecs/src/web.rs b/crates/codecs/src/web.rs
index f48de8634..584ceb9d6 100644
--- a/crates/codecs/src/web.rs
+++ b/crates/codecs/src/web.rs
@@ -307,7 +307,9 @@ pub fn encode_jpeg_rgb8(
}
let mut out = Vec::new();
let mut enc = jpeg_encoder::Encoder::new(&mut out, quality.clamp(1, 100));
- let subsampled = quality < 90;
+ // Like Photoshop's Save for Web: 4:2:0 chroma up to quality 50, 4:4:4 from 51 (measured on
+ // Photoshop 2025's output; its Save As keeps 4:2:0 at every quality).
+ let subsampled = quality <= 50;
enc.set_progressive(progressive);
// jpeg-encoder 0.6 writes corrupt baseline scans when optimised Huffman tables meet 4:2:0
// (both zune-jpeg and image-rs decode them green); progressive output is fine.
@@ -345,6 +347,24 @@ mod tests {
(0..w * h).map(|i| (((i % w) * 7 + (i / w) * 13 + (i * i) % 5) % n) as u8).collect()
}
+ /// The luma sampling factors (h, v) of a baseline or progressive JPEG.
+ fn luma_sampling(bytes: &[u8]) -> Option<(u8, u8)> {
+ let i = bytes.windows(2).position(|w| w[0] == 0xFF && matches!(w[1], 0xC0..=0xC2))?;
+ let f = *bytes.get(i + 11)?;
+ Some((f >> 4, f & 15))
+ }
+
+ #[test]
+ fn jpeg_chroma_is_full_above_quality_50() {
+ let rgb: Vec<u8> = (0..16 * 16 * 3).map(|i| (i * 37 % 256) as u8).collect();
+ for (q, want) in [(1, (2, 2)), (50, (2, 2)), (51, (1, 1)), (60, (1, 1)), (89, (1, 1)), (100, (1, 1))] {
+ for progressive in [false, true] {
+ let bytes = encode_jpeg_rgb8(16, 16, &rgb, q, progressive, false, None, None, None).unwrap();
+ assert_eq!(luma_sampling(&bytes), Some(want), "quality {q} progressive {progressive}");
+ }
+ }
+ }
+
#[test]
fn gif_roundtrips_through_the_decoder() {
let pal: Vec<[u8; 3]> = (0..200).map(|i| [i as u8, (255 - i) as u8, (i * 3 % 256) as u8]).collect();