Skip to content

Commit

Permalink
png: Add CICP support
Browse files Browse the repository at this point in the history
  • Loading branch information
sophie-h committed Nov 21, 2024
1 parent 1f34dff commit 4f079b6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
19 changes: 11 additions & 8 deletions gufo-common/src/cicp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use crate::utils;

/// Coding-independent code point
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Cicp {
pub colour_primaries: ColourPrimaries,
pub transfer_characteristics: TransferCharacteristics,
Expand Down Expand Up @@ -42,7 +43,7 @@ impl Cicp {

utils::convertible_enum!(
#[repr(u8)]
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ColourPrimaries {
Srgb = 1,
Unspecified = 2,
Expand All @@ -53,17 +54,19 @@ utils::convertible_enum!(

utils::convertible_enum!(
#[repr(u8)]
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum TransferCharacteristics {
/// Standard dynamic range
Sdr = 1,
Gamma22 = 1,
Unspecified = 2,
/// Standard dynamic range 10 bit
Sdr_ = 6,
Gamma22_ = 6,
/// Gamma=2.4 curve per IEC 61966-2-1 sRGB
Gamma24 = 13,
/// Standard dynamic range 10 bit
Sdr10 = 14,
Gamma22Bit10 = 14,
/// Standard dynamic range 12 bit
Sdr12 = 15,
Gamma22Bit12 = 15,
/// Perceptual quantization (PQ) system
Pq = 16,
/// Hybrid log-gamma (HLG) system
Expand All @@ -73,7 +76,7 @@ utils::convertible_enum!(

utils::convertible_enum!(
#[repr(u8)]
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum MatrixCoefficients {
Identity = 0,
Unspecified = 2,
Expand All @@ -83,7 +86,7 @@ utils::convertible_enum!(

utils::maybe_convertible_enum!(
#[repr(u8)]
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum VideoRangeFlag {
Narrow = 0,
Full = 1,
Expand Down
3 changes: 3 additions & 0 deletions gufo-png/src/chunk_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ gufo_common::utils::convertible_enum!(
eXIf = b(b"eXIf"),
/// Embedded ICC profile
iCCP = b(b"iCCP"),
/// Coding-independent code points
cICP = b(b"cICP"),

/// Apple proprietary, information for faster image loading
///
/// See <https://www.hackerfactor.com/blog/index.php?/archives/895-Connecting-the-iDOTs.html>
Expand Down
32 changes: 32 additions & 0 deletions gufo-png/src/png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::io::{Cursor, Read};
use std::ops::Range;
use std::slice::SliceIndex;

use gufo_common::cicp::Cicp;

pub use super::*;

pub const MAGIC_BYTES: &[u8] = &[137, 80, 78, 71, 13, 10, 26, 10];
Expand Down Expand Up @@ -86,6 +88,15 @@ impl Png {
}
}

pub fn cicp(&self) -> Option<Cicp> {
let cicp_raw = self
.chunks()
.into_iter()
.find(|x| x.chunk_type() == ChunkType::cICP)?;

Cicp::from_bytes(cicp_raw.chunk_data().get(0..4)?.try_into().ok()?).ok()
}

/// List all chunks in the data
fn find_chunks(data: &[u8]) -> Result<Vec<RawChunk>, Error> {
let mut cur = Cursor::new(data);
Expand Down Expand Up @@ -221,3 +232,24 @@ impl Png {
.ok_or(Error::IndexNotInData(index))
}
}

#[cfg(test)]
mod tests {
use gufo_common::cicp::*;

#[test]
fn x() {
let data = std::fs::read("../test-images/images/cicp-p3/cicp-p3.png").unwrap();
let png = crate::Png::new(data).unwrap();
dbg!(png.cicp());
assert_eq!(
png.cicp(),
Some(Cicp {
colour_primaries: ColourPrimaries::DciP3,
transfer_characteristics: TransferCharacteristics::Gamma24,
matrix_coefficients: MatrixCoefficients::Identity,
video_full_range_flag: VideoRangeFlag::Full,
})
);
}
}
2 changes: 1 addition & 1 deletion test-images
Submodule test-images updated from 8376ea to 67f1cf

0 comments on commit 4f079b6

Please sign in to comment.