|
| 1 | +use pdf_object::{ |
| 2 | + dictionary::Dictionary, error::ObjectError, object_lookup::ObjectLookupExt, |
| 3 | + object_resolver::ObjectResolver, |
| 4 | +}; |
| 5 | + |
| 6 | +/// Parameters shared by the CalGray, CalRGB, and Lab color spaces. |
| 7 | +#[derive(Debug, PartialEq)] |
| 8 | +pub(crate) struct CieColorSpaceParams { |
| 9 | + /// Reference white point in CIE XYZ coordinates `[Xw, Yw, Zw]`. |
| 10 | + pub(crate) white_point: [f32; 3], |
| 11 | + /// Reference black point in CIE XYZ coordinates `[Xb, Yb, Zb]`. |
| 12 | + /// |
| 13 | + /// Defaults to `[0.0, 0.0, 0.0]` when `/BlackPoint` is absent. |
| 14 | + pub(crate) black_point: [f32; 3], |
| 15 | +} |
| 16 | + |
| 17 | +impl CieColorSpaceParams { |
| 18 | + pub(crate) fn from_dictionary( |
| 19 | + dictionary: &Dictionary, |
| 20 | + objects: &dyn ObjectResolver, |
| 21 | + ) -> Result<Self, ObjectError> { |
| 22 | + let white_point = dictionary.required_array_of::<f32, 3>("WhitePoint", objects)?; |
| 23 | + let black_point = dictionary |
| 24 | + .optional_array_of::<f32, 3>("BlackPoint", objects)? |
| 25 | + .unwrap_or_default(); |
| 26 | + |
| 27 | + Ok(Self { |
| 28 | + white_point, |
| 29 | + black_point, |
| 30 | + }) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +#[cfg(test)] |
| 35 | +mod tests { |
| 36 | + use std::collections::BTreeMap; |
| 37 | + |
| 38 | + use pdf_object::{ |
| 39 | + dictionary::Dictionary, object_resolver::PassthroughResolver, object_variant::ObjectVariant, |
| 40 | + }; |
| 41 | + |
| 42 | + use super::CieColorSpaceParams; |
| 43 | + |
| 44 | + fn array(values: &[f64]) -> ObjectVariant { |
| 45 | + ObjectVariant::Array(values.iter().copied().map(ObjectVariant::Real).collect()) |
| 46 | + } |
| 47 | + |
| 48 | + #[test] |
| 49 | + fn parses_white_and_black_points() { |
| 50 | + let dictionary = Dictionary::new(BTreeMap::from([ |
| 51 | + ("BlackPoint".to_owned(), array(&[0.1, 0.2, 0.3])), |
| 52 | + ("WhitePoint".to_owned(), array(&[0.9, 1.0, 0.8])), |
| 53 | + ])); |
| 54 | + |
| 55 | + let params = |
| 56 | + CieColorSpaceParams::from_dictionary(&dictionary, &PassthroughResolver).unwrap(); |
| 57 | + |
| 58 | + assert_eq!( |
| 59 | + params, |
| 60 | + CieColorSpaceParams { |
| 61 | + white_point: [0.9, 1.0, 0.8], |
| 62 | + black_point: [0.1, 0.2, 0.3], |
| 63 | + } |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn defaults_missing_black_point_to_zero() { |
| 69 | + let dictionary = Dictionary::new(BTreeMap::from([( |
| 70 | + "WhitePoint".to_owned(), |
| 71 | + array(&[0.9, 1.0, 0.8]), |
| 72 | + )])); |
| 73 | + |
| 74 | + let params = |
| 75 | + CieColorSpaceParams::from_dictionary(&dictionary, &PassthroughResolver).unwrap(); |
| 76 | + |
| 77 | + assert_eq!(params.black_point, [0.0, 0.0, 0.0]); |
| 78 | + } |
| 79 | +} |
0 commit comments