GitCorner

Code

ossartcraftlightcraft🌐
5a24289898fd
#![allow(dead_code)]
//! DNG Camera Profiles (`.dcp`): the user's own camera colour profiles, read at run time from
//! files they supply (DNG specification, chapter 6 "Camera Profiles"). A `.dcp` is a TIFF-structured
//! file whose header carries the magic `RC` (0x4352) in place of 42, with the profile tags in IFD 0:
//! `UniqueCameraModel`, `ProfileName`, the colour/forward matrices and calibration illuminants, the
//! hue/saturation map, look table and tone curve. Nothing here ships a profile.

// Keep shared RAW field types aligned; this TIFF source is unchanged across the pin.
use craft_raw_tiff as craft_tiff;
use craft_tiff::{ByteOrder, Ifd, ParseOptions, Tiff, tags as t};
use lightcraft_color::Xy;

use crate::gaintable::GainTableMap;
use crate::profile::{HsvTable, ProfileLook, ToneCurve};
use crate::{ColorData, Mat3};

/// The magic number of a DNG camera profile file (`IIRC` / `MMCR`).
pub const DCP_MAGIC: u16 = 0x4352;

/// A parsed camera profile.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct DcpProfile {
    /// `ProfileName` (empty when the file has none).
    pub name: String,
    /// `UniqueCameraModel`: the camera the profile was made for (empty when absent).
    pub camera_model: String,
    /// The colour model: illuminants, matrices and the profile look (hue/sat map, look table, tone curve).
    pub color: ColorData,
}

impl DcpProfile {
    /// `color` (a raw file's own colour facts) with this profile's colour model in place of the
    /// file's: its matrices, illuminants and look. The file's white balance (as-shot neutral or
    /// white, and its white-balance presets), analog balance and baseline exposure stay, plus its
    /// camera calibration when the profile has the same calibration illuminants (DNG 6.3:
    /// calibration belongs to the camera, the profile to the look).
    pub fn applied_to(&self, color: &ColorData) -> ColorData {
        let calibration = if self.color.illuminant == color.illuminant { color.camera_calibration } else { [None, None] };
        ColorData {
            illuminant: self.color.illuminant,
            color_matrix: self.color.color_matrix,
            forward_matrix: self.color.forward_matrix,
            camera_calibration: calibration,
            analog_balance: color.analog_balance,
            as_shot_neutral: color.as_shot_neutral,
            as_shot_white_xy: color.as_shot_white_xy,
            baseline_exposure: color.baseline_exposure + self.color.baseline_exposure,
            baseline_sharpness: color.baseline_sharpness,
            profile: crate::profile::ProfileLook { gain_table_map: color.profile.gain_table_map.clone(), ..self.color.profile.clone() },
            kelvin_presets: color.kelvin_presets.clone(),
        }
    }
}

/// Errors reading a `.dcp`.
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum DcpError {
    #[error("not a DNG camera profile (.dcp)")]
    NotDcp,
    #[error("damaged camera profile: {0}")]
    Damaged(String),
    #[error("the camera profile has no usable colour matrix")]
    NoMatrix,
}

/// Is `bytes` a DCP file (by its header)?
pub fn is_dcp(bytes: &[u8]) -> bool {
    matches!(bytes.get(0..4), Some([b'I', b'I', b'R', b'C'] | [b'M', b'M', b'C', b'R']))
}

/// The largest `.dcp` file read or parsed (real profiles are kilobytes to a few MB).
pub const MAX_DCP_BYTES: u64 = 64 << 20;

/// Read the `.dcp` file at `path`, reading at most [`MAX_DCP_BYTES`] + 1 bytes: a larger file is refused
/// before it is loaded into memory.
pub fn read_dcp(path: &std::path::Path) -> std::io::Result<Vec<u8>> {
    use std::io::Read;
    let mut bytes = Vec::new();
    std::fs::File::open(path)?.take(MAX_DCP_BYTES.saturating_add(1)).read_to_end(&mut bytes)?;
    if bytes.len() as u64 > MAX_DCP_BYTES {
        return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "the camera profile is larger than 64 MiB"));
    }
    Ok(bytes)
}

/// Parse a `.dcp` file. Files above 64 MiB ([`MAX_DCP_BYTES`]) are refused.
pub fn parse_dcp(bytes: &[u8]) -> Result<DcpProfile, DcpError> {
    if !is_dcp(bytes) {
        return Err(DcpError::NotDcp);
    }
    if bytes.len() as u64 > MAX_DCP_BYTES {
        return Err(DcpError::Damaged("larger than 64 MiB".into()));
    }
    // the TIFF reader only knows 42 / BigTIFF / raw magics: give it a header with 42 in place of RC
    let mut data = bytes.to_vec();
    let forty_two: [u8; 2] = if bytes.first() == Some(&b'I') { [42, 0] } else { [0, 42] };
    if let Some(m) = data.get_mut(2..4) {
        m.copy_from_slice(&forty_two);
    }
    let opts = ParseOptions { follow_children: false, max_ifds: 4, value_budget: Some(64 << 20), ..Default::default() };
    let tiff = Tiff::parse_with(&data, &opts).map_err(|e| DcpError::Damaged(e.to_string()))?;
    let ifd: &Ifd = tiff.ifds.first().ok_or_else(|| DcpError::Damaged("no IFD".into()))?;
    let mut color = color_data(ifd, ifd, tiff.order);
    // a profile carries no white balance or calibration of a particular shot
    color.as_shot_neutral = None;
    color.as_shot_white_xy = None;
    color.analog_balance = None;
    color.camera_calibration = [None, None];
    color.baseline_sharpness = None;
    color.profile.gain_table_map = None;
    // only BaselineExposureOffset belongs to a profile (BaselineExposure is the camera's)
    color.baseline_exposure =
        ifd.f64s(t::BASELINE_EXPOSURE_OFFSET).and_then(|v| v.first().copied()).filter(|v| v.is_finite()).unwrap_or(0.0).clamp(-4.0, 4.0);
    // a single-illuminant profile names one illuminant; its second matrix slot stays empty
    if color.color_matrix[0].is_none() {
        // the first slot is the one every profile must fill (DNG: ColorMatrix1 is required)
        return Err(DcpError::NoMatrix);
    }
    let text = |tag: u16| ifd.string(tag).map(|s| s.trim_matches(char::from(0)).trim().to_string()).unwrap_or_default();
    Ok(DcpProfile { name: text(t::PROFILE_NAME), camera_model: text(t::UNIQUE_CAMERA_MODEL), color })
}

/// Write `p` as a `.dcp` file (little-endian): its name, camera, illuminants, matrices, hue/saturation
/// map, look table, tone curve and baseline exposure offset.
pub fn write_dcp(p: &DcpProfile) -> Result<Vec<u8>, DcpError> {
    use craft_tiff::writer::srational;
    use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value};
    let c = &p.color;
    let mut ifd = IfdBuilder::new();
    ifd.set(t::UNIQUE_CAMERA_MODEL, Value::Ascii(p.camera_model.clone()));
    ifd.set(t::PROFILE_NAME, Value::Ascii(p.name.clone()));
    let mat = |m: &lightcraft_color::Mat3| Value::SRational(m.0.iter().flatten().map(|v| srational(*v)).collect());
    for i in 0..2 {
        if let Some(m) = &c.color_matrix[i] {
            ifd.set([t::CALIBRATION_ILLUMINANT_1, t::CALIBRATION_ILLUMINANT_2][i], Value::Short(vec![c.illuminant[i]]));
            ifd.set([t::COLOR_MATRIX_1, t::COLOR_MATRIX_2][i], mat(m));
        }
        if let Some(m) = &c.forward_matrix[i] {
            ifd.set([t::FORWARD_MATRIX_1, t::FORWARD_MATRIX_2][i], mat(m));
        }
    }
    let flat = |table: &crate::profile::HsvTable| Value::Float(table.data.iter().flatten().copied().collect());
    let dims =
        |table: &crate::profile::HsvTable| Value::Long(vec![table.hue_divisions as u32, table.sat_divisions as u32, table.val_divisions as u32]);
    if let Some(a) = c.profile.hue_sat_map[0].as_ref() {
        ifd.set(t::PROFILE_HUE_SAT_MAP_DIMS, dims(a));
        ifd.set(t::PROFILE_HUE_SAT_MAP_DATA_1, flat(a));
        if a.srgb_value {
            ifd.set(t::PROFILE_HUE_SAT_MAP_ENCODING, Value::Long(vec![1]));
        }
        if let Some(b) = c.profile.hue_sat_map[1]
            .as_ref()
            .filter(|b| (b.hue_divisions, b.sat_divisions, b.val_divisions) == (a.hue_divisions, a.sat_divisions, a.val_divisions))
        {
            ifd.set(t::PROFILE_HUE_SAT_MAP_DATA_2, flat(b));
        }
    }
    if let Some(l) = &c.profile.look_table {
        ifd.set(t::PROFILE_LOOK_TABLE_DIMS, dims(l));
        ifd.set(t::PROFILE_LOOK_TABLE_DATA, flat(l));
        if l.srgb_value {
            ifd.set(t::PROFILE_LOOK_TABLE_ENCODING, Value::Long(vec![1]));
        }
    }
    if let Some(tc) = &c.profile.tone_curve {
        ifd.set(t::PROFILE_TONE_CURVE, Value::Float(tc.points.iter().flatten().copied().collect()));
    }
    if c.baseline_exposure != 0.0 {
        ifd.set(t::BASELINE_EXPOSURE_OFFSET, Value::SRational(vec![srational(c.baseline_exposure)]));
    }
    let mut out = TiffWriter::new(ByteOrder::Little, false).write(&[ifd]).map_err(|e| DcpError::Damaged(e.to_string()))?;
    // the DCP magic in place of 42
    if let Some(m) = out.get_mut(2..4) {
        m.copy_from_slice(&DCP_MAGIC.to_le_bytes());
    }
    Ok(out)
}

// The colour tags a profile shares with DNG, read as DNG reads them (a DNG's own are read by `craft-raw`).

fn mat3(v: Option<Vec<f64>>) -> Option<Mat3> {
    let v = v?;
    if v.len() != 9 || v.iter().any(|x| !x.is_finite()) {
        return None;
    }
    let m = Mat3([[v[0], v[1], v[2]], [v[3], v[4], v[5]], [v[6], v[7], v[8]]]);
    (m.determinant().abs() > 1e-12).then_some(m)
}

fn vec3(v: Option<Vec<f64>>) -> Option<[f64; 3]> {
    let v = v?;
    (v.len() == 3 && v.iter().all(|x| x.is_finite() && *x > 0.0)).then(|| [v[0], v[1], v[2]])
}

/// Colour tags: DNG puts them in IFD0, but some writers use the raw IFD; prefer the raw IFD.
/// `order` is the file's byte order (for the binary gain-table-map tags).
fn color_data(ifd0: &Ifd, raw: &Ifd, order: ByteOrder) -> ColorData {
    let get = |tag: u16| raw.f64s(tag).or_else(|| ifd0.f64s(tag));
    let geti = |tag: u16| raw.u16(tag).or_else(|| ifd0.u16(tag));
    ColorData {
        illuminant: [geti(t::CALIBRATION_ILLUMINANT_1).unwrap_or(0), geti(t::CALIBRATION_ILLUMINANT_2).unwrap_or(0)],
        color_matrix: [mat3(get(t::COLOR_MATRIX_1)), mat3(get(t::COLOR_MATRIX_2))],
        forward_matrix: [mat3(get(t::FORWARD_MATRIX_1)), mat3(get(t::FORWARD_MATRIX_2))],
        camera_calibration: [mat3(get(t::CAMERA_CALIBRATION_1)), mat3(get(t::CAMERA_CALIBRATION_2))],
        analog_balance: vec3(get(t::ANALOG_BALANCE)),
        as_shot_neutral: vec3(get(t::AS_SHOT_NEUTRAL)),
        as_shot_white_xy: get(t::AS_SHOT_WHITE_XY).filter(|v| v.len() == 2 && v[0] > 0.0 && v[1] > 0.0).map(|v| Xy::new(v[0], v[1])),
        baseline_exposure: get(t::BASELINE_EXPOSURE).and_then(|v| v.first().copied()).filter(|v| v.is_finite()).unwrap_or(0.0)
            + get(t::BASELINE_EXPOSURE_OFFSET).and_then(|v| v.first().copied()).filter(|v| v.is_finite()).unwrap_or(0.0),
        // DNG 1.7 BaselineSharpness (IFD 0 or the enhanced IFD); implausible values are ignored
        baseline_sharpness: get(t::BASELINE_SHARPNESS).and_then(|v| v.first().copied()).filter(|v| v.is_finite() && *v > 0.0 && *v <= 16.0),
        profile: profile_look(ifd0, raw, order),
        kelvin_presets: None,
    }
}

/// The profile look tags (`ProfileHueSatMap*`, `ProfileLookTable*`, `ProfileToneCurve`,
/// `ProfileGainTableMap*`); malformed ones are ignored. Like the colour tags, read from the raw IFD
/// first, else IFD 0.
fn profile_look(ifd0: &Ifd, raw: &Ifd, order: ByteOrder) -> ProfileLook {
    let pick = |tag: u16| if raw.contains(tag) { raw } else { ifd0 };
    let table = |dims: u16, data: u16, enc: u16| {
        let ifd = pick(dims);
        let dims = ifd.u64s(dims)?;
        let data = ifd.f64s(data)?;
        HsvTable::from_tags(&dims, &data, ifd.u64s(enc).and_then(|v| v.first().copied()).unwrap_or(0))
    };
    ProfileLook {
        hue_sat_map: [
            table(t::PROFILE_HUE_SAT_MAP_DIMS, t::PROFILE_HUE_SAT_MAP_DATA_1, t::PROFILE_HUE_SAT_MAP_ENCODING),
            table(t::PROFILE_HUE_SAT_MAP_DIMS, t::PROFILE_HUE_SAT_MAP_DATA_2, t::PROFILE_HUE_SAT_MAP_ENCODING),
        ],
        look_table: table(t::PROFILE_LOOK_TABLE_DIMS, t::PROFILE_LOOK_TABLE_DATA, t::PROFILE_LOOK_TABLE_ENCODING),
        tone_curve: pick(t::PROFILE_TONE_CURVE).f64s(t::PROFILE_TONE_CURVE).and_then(|v| ToneCurve::from_tag(&v)),
        gain_table_map: gain_table_map(ifd0, raw, order),
    }
}

/// The gain table map (DNG 1.7 precedence): `ProfileGainTableMap2` from IFD 0 (the embedded
/// camera profile), else from the raw IFD; else `ProfileGainTableMap` from the raw IFD (where DNG
/// 1.6 put it, and where Apple writes it), else from IFD 0 (where DNG 1.7 says it was meant to be).
/// A malformed tag falls through to the next one.
fn gain_table_map(ifd0: &Ifd, raw: &Ifd, order: ByteOrder) -> Option<GainTableMap> {
    let parse = |ifd: &Ifd, tag: u16| ifd.bytes(tag).and_then(|b| GainTableMap::parse(b, order, tag == t::PROFILE_GAIN_TABLE_MAP_2));
    parse(ifd0, t::PROFILE_GAIN_TABLE_MAP_2)
        .or_else(|| parse(raw, t::PROFILE_GAIN_TABLE_MAP_2))
        .or_else(|| parse(raw, t::PROFILE_GAIN_TABLE_MAP))
        .or_else(|| parse(ifd0, t::PROFILE_GAIN_TABLE_MAP))
}