Skip to content

Commit

Permalink
Merge pull request #1858 from sophie-h/icc-color-profile
Browse files Browse the repository at this point in the history
Add ICC profile to ImageDecoder
  • Loading branch information
fintelia authored Feb 10, 2023
2 parents 5518faa + 06b6052 commit 2ca903c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/codecs/avif/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct AvifDecoder<R> {
inner: PhantomData<R>,
picture: dav1d::Picture,
alpha_picture: Option<dav1d::Picture>,
icc_profile: Option<Vec<u8>>,
}

impl<R: Read> AvifDecoder<R> {
Expand All @@ -49,11 +50,13 @@ impl<R: Read> AvifDecoder<R> {
} else {
None
};
let icc_profile = ctx.icc_colour_information().ok().map(|x| x.to_vec());
assert_eq!(picture.bit_depth(), 8);
Ok(AvifDecoder {
inner: PhantomData,
picture,
alpha_picture,
icc_profile,
})
}
}
Expand Down Expand Up @@ -85,6 +88,10 @@ impl<'a, R: 'a + Read> ImageDecoder<'a> for AvifDecoder<R> {
ColorType::Rgba8
}

fn icc_profile(&mut self) -> Option<Vec<u8>> {
self.icc_profile.clone()
}

fn into_reader(self) -> ImageResult<Self::Reader> {
let plane = self.picture.plane(PlanarImageComponent::Y);
Ok(AvifReader(
Expand Down
4 changes: 4 additions & 0 deletions src/codecs/jpeg/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ impl<'a, R: 'a + Read> ImageDecoder<'a> for JpegDecoder<R> {
ColorType::from_jpeg(self.metadata.pixel_format)
}

fn icc_profile(&mut self) -> Option<Vec<u8>> {
self.decoder.icc_profile().clone()
}

fn into_reader(mut self) -> ImageResult<Self::Reader> {
let mut data = self.decoder.decode().map_err(ImageError::from_jpeg)?;
data = match self.decoder.info().unwrap().pixel_format {
Expand Down
4 changes: 4 additions & 0 deletions src/codecs/png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ impl<'a, R: 'a + Read> ImageDecoder<'a> for PngDecoder<R> {
self.color_type
}

fn icc_profile(&mut self) -> Option<Vec<u8>> {
self.reader.info().icc_profile.as_ref().map(|x| x.to_vec())
}

fn into_reader(self) -> ImageResult<Self::Reader> {
PngReader::new(self.reader)
}
Expand Down
8 changes: 8 additions & 0 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,14 @@ pub trait ImageDecoder<'a>: Sized {
self.color_type().into()
}

/// Returns the ICC color profile embedded in the image
///
/// For formats that don't support embedded profiles this function will always return `None`.
/// This feature is currently only supported for the JPEG, PNG, and AVIF formats.
fn icc_profile(&mut self) -> Option<Vec<u8>> {
None
}

/// Returns a reader that can be used to obtain the bytes of the image. For the best
/// performance, always try to read at least `scanline_bytes` from the reader at a time. Reading
/// fewer bytes will cause the reader to perform internal buffering.
Expand Down

0 comments on commit 2ca903c

Please sign in to comment.