From 5651419214bcbd4df2b3f46f4690f0e5ff250582 Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 1 Dec 2026 08:15:54 -0600 Subject: [PATCH] fix(data): turn a JPEG GeoTIFF's YCbCr into colours when it is read in place The whole-file reader turned JPEG-compressed YCbCr bands into red, green and blue (#692); reading a tile at a time kept them as they are, so an orthophoto read in place, or from its web address, drew in wrong colours and Identify gave luma and chroma as its values. Each level is now converted by its own header, before no-data is matched. Part of #2761. --- crates/data/src/raster/cog.rs | 10 ++++++- crates/data/src/raster/geotiff.rs | 48 +++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/crates/data/src/raster/cog.rs b/crates/data/src/raster/cog.rs index bed6e247ce..c0e6b56181 100644 --- a/crates/data/src/raster/cog.rs +++ b/crates/data/src/raster/cog.rs @@ -365,7 +365,11 @@ fn read_layout(r: Sparse) -> Result { ColorType::Gray(_) => (1, ColorHint::Gray), ColorType::GrayA(_) => (2, ColorHint::GrayAlpha), ColorType::RGB(_) => (3, ColorHint::Rgb { alpha: false }), - ColorType::YCbCr(_) if dec.find_tag_unsigned::(Tag::Compression).ok().flatten() == Some(7) => { + // A chunk's three bands are turned into colours together, so they are read together. + ColorType::YCbCr(_) + if super::geotiff::is_ycbcr_jpeg(&mut dec) + && dec.find_tag_unsigned::(Tag::PlanarConfiguration).ok().flatten() != Some(2) => + { (3, ColorHint::Rgb { alpha: false }) } ColorType::RGBA(_) => (4, ColorHint::Rgb { alpha: true }), @@ -761,6 +765,10 @@ pub fn decode_tile_counted(header: &Sparse, layout: &Layout, level: usize, index } } } + // Each level by its own header, as the whole-file reader reads the level it picks (#2761). + if super::geotiff::is_ycbcr_jpeg(&mut dec) { + super::geotiff::ycbcr_to_rgb(&mut out); + } if let Some(nd) = layout.nodata { let nd = nd as f32; for band in &mut out { diff --git a/crates/data/src/raster/geotiff.rs b/crates/data/src/raster/geotiff.rs index 9b9e5c1333..140b95fc3a 100644 --- a/crates/data/src/raster/geotiff.rs +++ b/crates/data/src/raster/geotiff.rs @@ -749,7 +749,7 @@ pub(crate) fn read_chunk( /// Turns a JPEG-compressed image's three YCbCr bands into red, green and blue in place, with the /// JFIF (full-range) coefficients JPEG-in-TIFF files use. -fn ycbcr_to_rgb(bands: &mut [Vec]) { +pub(super) fn ycbcr_to_rgb(bands: &mut [Vec]) { let [y, cb, cr] = bands else { return }; for ((y, cb), cr) in y.iter_mut().zip(cb.iter_mut()).zip(cr.iter_mut()) { let (luma, blue, red) = (*y, *cb - 128.0, *cr - 128.0); @@ -759,6 +759,12 @@ fn ycbcr_to_rgb(bands: &mut [Vec]) { } } +/// Whether the current image is JPEG-compressed YCbCr, which the decoder leaves as it is: its bands +/// are turned into colours with [`ycbcr_to_rgb`] (#692). +pub(super) fn is_ycbcr_jpeg(dec: &mut Decoder) -> bool { + matches!(dec.colortype(), Ok(ColorType::YCbCr(_))) && dec.find_tag_unsigned::(Tag::Compression).ok().flatten() == Some(7) +} + /// Bits per sample of the current image (the most of any sample). fn bits(dec: &mut Decoder) -> u16 { dec.find_tag_unsigned_vec::(Tag::BitsPerSample).ok().flatten().and_then(|v| v.into_iter().max()).unwrap_or(8) @@ -1866,7 +1872,7 @@ fn read_image( // From here on, tags (colour type, planar layout) are those of the chosen image. let ct = dec.colortype().map_err(err)?; // JPEG-compressed YCbCr, the only YCbCr read (the colour match below rejects the rest). - let ycbcr = matches!(ct, ColorType::YCbCr(_)) && dec.find_tag_unsigned::(Tag::Compression).ok().flatten() == Some(7); + let ycbcr = is_ycbcr_jpeg(&mut dec); // A palette image (shown to the decoder as grey) is coloured by its table. One whose table // doesn't decode, or is too short, is drawn grey, and says so (`Raster::lost`). let (colour_map, map_why) = if palette { @@ -5427,6 +5433,44 @@ mod tests { check(&r, 32, 32); } + /// Read in place, a chunk at a time, every level is turned into colours as the whole file is + /// (#2761), and no-data is matched with the colours, not with the stored luma. + #[test] + fn ycbcr_jpeg_is_shown_as_rgb_read_in_place() { + use super::super::cog; + let colors = [[200, 30, 30], [30, 160, 40], [20, 40, 220], [230, 230, 60]]; + let file = ycbcr_jpeg_tiff(&[(128, 128, 64), (64, 64, 64)], colors, true); + let header = Sparse::default().with(0, file.clone()); + let cog::Parse::Done(mut layout) = cog::layout(&header).unwrap() else { panic!("not read in place") }; + assert_eq!(layout.levels.len(), 2); + let chunk = |layout: &cog::Layout, level: usize, c: usize| { + let l = &layout.levels[level]; + let (at, n) = (l.offsets[c] as usize, l.counts[c] as usize); + cog::decode_tile(&header, layout, level, c as u32, file[at..at + n].to_vec()).unwrap() + }; + for (level, size) in [(0, 128usize), (1, 64)] { + for (i, rgb) in colors.iter().enumerate() { + let (x, y) = (size / 4 + size / 2 * (i % 2), size / 4 + size / 2 * (i / 2)); + let bands = chunk(&layout, level, y / 64); + for (band, want) in bands.iter().zip(rgb) { + let got = band[(y % 64) * size + x]; + assert!((got - f32::from(*want)).abs() <= 4.0, "level {level}, quarter {i}: {got} for {want}"); + } + } + } + // The first quarter's red as it decodes, then its luma, as the file's no-data value. + let red = chunk(&layout, 1, 0)[0][16 * 64 + 16]; + layout.nodata = Some(f64::from(red)); + assert!(chunk(&layout, 1, 0)[0][16 * 64 + 16].is_nan()); + let mut dec = Decoder::new(std::io::Cursor::new(&file)).unwrap(); + dec.seek_to_image(1).unwrap(); + let DecodingResult::U8(stored) = dec.read_chunk(0).unwrap() else { panic!("not bytes") }; + let luma = stored[(16 * 64 + 16) * 3]; + assert_ne!(f32::from(luma), red); + layout.nodata = Some(f64::from(luma)); + assert_eq!(chunk(&layout, 1, 0)[0][16 * 64 + 16], red); + } + #[test] fn lzw_chunks_decoded_here_match_the_tiff_crate() { // Every predictor, padded edge tiles on the right and at the bottom.