From 7648317643418a127e8b5dcc60bc8ef8078e06d4 Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Sun, 22 Jun 2025 16:19:36 -0700 Subject: [PATCH] Stop doing color conversion when decoding JPEG compressed images (#274) --- src/decoder/image.rs | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/src/decoder/image.rs b/src/decoder/image.rs index ddfd624..4484642 100644 --- a/src/decoder/image.rs +++ b/src/decoder/image.rs @@ -370,7 +370,6 @@ impl Image { fn create_reader<'r, R: 'r + Read>( reader: R, - #[allow(unused_variables)] photometric_interpretation: PhotometricInterpretation, compression_method: CompressionMethod, compressed_length: u64, #[allow(unused_variables)] jpeg_tables: Option<&[u8]>, @@ -427,30 +426,14 @@ impl Image { let mut decoder = zune_jpeg::JpegDecoder::new(jpeg_data); let mut options: zune_core::options::DecoderOptions = Default::default(); - match photometric_interpretation { - PhotometricInterpretation::RGB => { - options = - options.jpeg_set_out_colorspace(zune_core::colorspace::ColorSpace::RGB); - } - PhotometricInterpretation::CMYK => { - options = options - .jpeg_set_out_colorspace(zune_core::colorspace::ColorSpace::CMYK); - } - PhotometricInterpretation::YCbCr => { - options = options - .jpeg_set_out_colorspace(zune_core::colorspace::ColorSpace::YCbCr); - } - PhotometricInterpretation::WhiteIsZero - | PhotometricInterpretation::BlackIsZero - | PhotometricInterpretation::TransparencyMask => {} - PhotometricInterpretation::RGBPalette | PhotometricInterpretation::CIELab => { - return Err(TiffError::UnsupportedError( - TiffUnsupportedError::UnsupportedInterpretation( - photometric_interpretation, - ), - )); - } + + // Disable color conversion by setting the output colorspace to the input + // colorspace. + decoder.decode_headers()?; + if let Some(colorspace) = decoder.get_input_colorspace() { + options = options.jpeg_set_out_colorspace(colorspace); } + decoder.set_options(options); let data = decoder.decode()?; @@ -653,7 +636,6 @@ impl Image { let mut reader = Self::create_reader( reader, - photometric_interpretation, compression_method, *compressed_bytes, self.jpeg_tables.as_deref().map(|a| &**a),