GitCorner

Code

ossartcraftphotocraft🌐
856f865cf98d
//! Camera Raw Filter › Save Settings… / Load Settings… (#4274): Adobe Camera Raw settings files
//! (`.xmp`), the `crs:` namespace (`http://ns.adobe.com/camera-raw-settings/1.0/`) on an
//! `x:xmpmeta/rdf:RDF/rdf:Description`, as attributes or as child elements.
//!
//! Loading applies only the settings the file holds, over the current ones, as Camera Raw's
//! Load Settings does; settings PhotoCraft has no control for (lens corrections, geometry,
//! defringe, …) are ignored and named in the report. The same reader fills the dialog from a
//! raw file's sidecar (`IMG_0001.xmp` beside `IMG_0001.CR2`), and the writer keeps it. Saving writes every supported
//! control, so loading the file in Photoshop reproduces the dialog.
//!
//! White balance: a Camera Raw Filter on a rendered image uses the relative
//! `IncrementalTemperature` / `IncrementalTint` (−100..=100), which are PhotoCraft's Temperature
//! and Tint. The absolute `Temperature` (kelvin) / `Tint` of raw-file settings have no
//! counterpart and are ignored.
//!
//! Crop (#9985): `crs:HasCrop="True"` with `crs:CropTop` / `CropLeft` / `CropBottom` /
//! `CropRight`, fractions of the developed frame in the raw's stored orientation (before its
//! Orientation tag turns it upright), is read into [`LoadReport::crop`]; the raw open crops the
//! document with it ([`crate::raw::RawTuning::crop`]). The settings have no crop (Camera Raw
//! Filter has none). Without `HasCrop`, or with `HasCrop="False"`, the crop keys are stale and
//! mean nothing. A straightened crop (`CropAngle` other than 0) isn't applied.

use craft::imaging::raw::VignetteStyle;
pub use craft_crs::{CROP_KEYS, CRS_NS, Crop, MAX_XMP_BYTES};
use craft_crs::{CropState, Settings, Value, number, points, signed, toolkit};
use photocraft_algo::camera_raw::{CameraRaw, validate_curve};

type Field = fn(&mut CameraRaw) -> &mut f32;

/// `crs:` scalar name, PhotoCraft field, and the slider range values are clamped to.
const SCALARS: &[(&str, Field, f32, f32)] = &[
    ("IncrementalTemperature", |p| &mut p.temperature, -100.0, 100.0),
    ("IncrementalTint", |p| &mut p.tint, -100.0, 100.0),
    ("Exposure2012", |p| &mut p.exposure, -5.0, 5.0),
    ("Contrast2012", |p| &mut p.contrast, -100.0, 100.0),
    ("Highlights2012", |p| &mut p.highlights, -100.0, 100.0),
    ("Shadows2012", |p| &mut p.shadows, -100.0, 100.0),
    ("Whites2012", |p| &mut p.whites, -100.0, 100.0),
    ("Blacks2012", |p| &mut p.blacks, -100.0, 100.0),
    ("Texture", |p| &mut p.texture, -100.0, 100.0),
    ("Clarity2012", |p| &mut p.clarity, -100.0, 100.0),
    ("Dehaze", |p| &mut p.dehaze, -100.0, 100.0),
    ("Vibrance", |p| &mut p.vibrance, -100.0, 100.0),
    ("Saturation", |p| &mut p.saturation, -100.0, 100.0),
    ("ParametricShadows", |p| &mut p.curve_shadows, -100.0, 100.0),
    ("ParametricDarks", |p| &mut p.curve_darks, -100.0, 100.0),
    ("ParametricLights", |p| &mut p.curve_lights, -100.0, 100.0),
    ("ParametricHighlights", |p| &mut p.curve_highlights, -100.0, 100.0),
    ("ParametricShadowSplit", |p| &mut p.curve_splits[0], 0.0, 100.0),
    ("ParametricMidtoneSplit", |p| &mut p.curve_splits[1], 0.0, 100.0),
    ("ParametricHighlightSplit", |p| &mut p.curve_splits[2], 0.0, 100.0),
    ("HueAdjustmentRed", |p| &mut p.hsl_hue[0], -100.0, 100.0),
    ("HueAdjustmentOrange", |p| &mut p.hsl_hue[1], -100.0, 100.0),
    ("HueAdjustmentYellow", |p| &mut p.hsl_hue[2], -100.0, 100.0),
    ("HueAdjustmentGreen", |p| &mut p.hsl_hue[3], -100.0, 100.0),
    ("HueAdjustmentAqua", |p| &mut p.hsl_hue[4], -100.0, 100.0),
    ("HueAdjustmentBlue", |p| &mut p.hsl_hue[5], -100.0, 100.0),
    ("HueAdjustmentPurple", |p| &mut p.hsl_hue[6], -100.0, 100.0),
    ("HueAdjustmentMagenta", |p| &mut p.hsl_hue[7], -100.0, 100.0),
    ("SaturationAdjustmentRed", |p| &mut p.hsl_sat[0], -100.0, 100.0),
    ("SaturationAdjustmentOrange", |p| &mut p.hsl_sat[1], -100.0, 100.0),
    ("SaturationAdjustmentYellow", |p| &mut p.hsl_sat[2], -100.0, 100.0),
    ("SaturationAdjustmentGreen", |p| &mut p.hsl_sat[3], -100.0, 100.0),
    ("SaturationAdjustmentAqua", |p| &mut p.hsl_sat[4], -100.0, 100.0),
    ("SaturationAdjustmentBlue", |p| &mut p.hsl_sat[5], -100.0, 100.0),
    ("SaturationAdjustmentPurple", |p| &mut p.hsl_sat[6], -100.0, 100.0),
    ("SaturationAdjustmentMagenta", |p| &mut p.hsl_sat[7], -100.0, 100.0),
    ("LuminanceAdjustmentRed", |p| &mut p.hsl_lum[0], -100.0, 100.0),
    ("LuminanceAdjustmentOrange", |p| &mut p.hsl_lum[1], -100.0, 100.0),
    ("LuminanceAdjustmentYellow", |p| &mut p.hsl_lum[2], -100.0, 100.0),
    ("LuminanceAdjustmentGreen", |p| &mut p.hsl_lum[3], -100.0, 100.0),
    ("LuminanceAdjustmentAqua", |p| &mut p.hsl_lum[4], -100.0, 100.0),
    ("LuminanceAdjustmentBlue", |p| &mut p.hsl_lum[5], -100.0, 100.0),
    ("LuminanceAdjustmentPurple", |p| &mut p.hsl_lum[6], -100.0, 100.0),
    ("LuminanceAdjustmentMagenta", |p| &mut p.hsl_lum[7], -100.0, 100.0),
    // Color Grading keeps the split-toning names for the shadow and highlight wheels' hue and
    // saturation, and the balance.
    ("SplitToningShadowHue", |p| &mut p.grade_shadows.hue, 0.0, 360.0),
    ("SplitToningShadowSaturation", |p| &mut p.grade_shadows.sat, 0.0, 100.0),
    ("ColorGradeShadowLum", |p| &mut p.grade_shadows.lum, -100.0, 100.0),
    ("ColorGradeMidtoneHue", |p| &mut p.grade_midtones.hue, 0.0, 360.0),
    ("ColorGradeMidtoneSat", |p| &mut p.grade_midtones.sat, 0.0, 100.0),
    ("ColorGradeMidtoneLum", |p| &mut p.grade_midtones.lum, -100.0, 100.0),
    ("SplitToningHighlightHue", |p| &mut p.grade_highlights.hue, 0.0, 360.0),
    ("SplitToningHighlightSaturation", |p| &mut p.grade_highlights.sat, 0.0, 100.0),
    ("ColorGradeHighlightLum", |p| &mut p.grade_highlights.lum, -100.0, 100.0),
    ("ColorGradeGlobalHue", |p| &mut p.grade_global.hue, 0.0, 360.0),
    ("ColorGradeGlobalSat", |p| &mut p.grade_global.sat, 0.0, 100.0),
    ("ColorGradeGlobalLum", |p| &mut p.grade_global.lum, -100.0, 100.0),
    ("ColorGradeBlending", |p| &mut p.grade_blending, 0.0, 100.0),
    ("SplitToningBalance", |p| &mut p.grade_balance, -100.0, 100.0),
    ("Sharpness", |p| &mut p.sharpen_amount, 0.0, 150.0),
    ("SharpenRadius", |p| &mut p.sharpen_radius, 0.5, 3.0),
    ("SharpenDetail", |p| &mut p.sharpen_detail, 0.0, 100.0),
    ("SharpenEdgeMasking", |p| &mut p.sharpen_masking, 0.0, 100.0),
    ("LuminanceSmoothing", |p| &mut p.noise_luminance, 0.0, 100.0),
    ("LuminanceNoiseReductionDetail", |p| &mut p.noise_luminance_detail, 0.0, 100.0),
    ("ColorNoiseReduction", |p| &mut p.noise_color, 0.0, 100.0),
    ("ColorNoiseReductionDetail", |p| &mut p.noise_color_detail, 0.0, 100.0),
    ("GrainAmount", |p| &mut p.grain_amount, 0.0, 100.0),
    ("GrainSize", |p| &mut p.grain_size, 0.0, 100.0),
    ("GrainFrequency", |p| &mut p.grain_roughness, 0.0, 100.0),
    ("PostCropVignetteAmount", |p| &mut p.vignette_amount, -100.0, 100.0),
    ("PostCropVignetteMidpoint", |p| &mut p.vignette_midpoint, 0.0, 100.0),
    ("PostCropVignetteRoundness", |p| &mut p.vignette_roundness, -100.0, 100.0),
    ("PostCropVignetteFeather", |p| &mut p.vignette_feather, 0.0, 100.0),
    ("PostCropVignetteHighlightContrast", |p| &mut p.vignette_highlights, 0.0, 100.0),
    // Calibration
    ("ShadowTint", |p| &mut p.shadow_tint, -100.0, 100.0),
    ("RedHue", |p| &mut p.calibration_hue[0], -100.0, 100.0),
    ("RedSaturation", |p| &mut p.calibration_sat[0], -100.0, 100.0),
    ("GreenHue", |p| &mut p.calibration_hue[1], -100.0, 100.0),
    ("GreenSaturation", |p| &mut p.calibration_sat[1], -100.0, 100.0),
    ("BlueHue", |p| &mut p.calibration_hue[2], -100.0, 100.0),
    ("BlueSaturation", |p| &mut p.calibration_sat[2], -100.0, 100.0),
];

type Curve = fn(&mut CameraRaw) -> &mut Vec<[f32; 2]>;
const CURVES: &[(&str, Curve)] = &[
    ("ToneCurvePV2012", |p| &mut p.point_curve),
    ("ToneCurvePV2012Red", |p| &mut p.point_curve_red),
    ("ToneCurvePV2012Green", |p| &mut p.point_curve_green),
    ("ToneCurvePV2012Blue", |p| &mut p.point_curve_blue),
];

const STYLES: &[(&str, VignetteStyle)] = &[("1", VignetteStyle::HighlightPriority), ("2", VignetteStyle::ColorPriority), ("3", VignetteStyle::PaintOverlay)];

/// Bookkeeping fields Camera Raw writes that carry no setting.
const METADATA: &[&str] = &[
    "Version",
    "ProcessVersion",
    "HasSettings",
    "WhiteBalance",
    "ToneCurveName2012",
    "CameraProfile",
    "PresetType",
    "UUID",
    "SupportsAmount",
    "SupportsColor",
    "SupportsMonochrome",
    "SupportsHighDynamicRange",
    "SupportsNormalDynamicRange",
    "SupportsSceneReferred",
    "SupportsOutputReferred",
    "CameraModelRestriction",
    "Copyright",
    "ContactInfo",
    "Name",
    "ShortName",
    "SortName",
    "Group",
    "Description",
    // The raw file the sidecar belongs to (Lightroom and Camera Raw name it).
    "RawFileName",
];

/// What [`load`] applied.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct LoadReport {
    /// How many settings were applied (a crop counts as one).
    pub applied: usize,
    /// `crs:` settings without a PhotoCraft control (set to something other than their neutral
    /// value), or with an unusable value, in file order; then the keys of a crop that isn't
    /// applied (straightened, unreadable or empty).
    pub ignored: Vec<String>,
    /// The file's crop, when it has one that isn't the whole frame. The settings hold no crop,
    /// so the caller applies it (a raw open crops the document) or reports it.
    pub crop: Option<Crop>,
}

/// `"0, 0"` items of an `rdf:Seq` → curve points.
fn curve(items: &[String]) -> Option<Vec<[f32; 2]>> {
    let mut pts = points(items)?;
    validate_curve(&pts).ok()?;
    // Camera Raw's linear curve is the identity's two end points; PhotoCraft's is empty.
    if pts == [[0.0, 0.0], [255.0, 255.0]] {
        pts.clear();
    }
    Some(pts)
}

/// An unsupported setting at its neutral value changes nothing, so it isn't reported: zero,
/// `False`, and the few neutral values that aren't zero.
fn neutral(name: &str, value: &str) -> bool {
    let v = value.trim();
    if v.eq_ignore_ascii_case("false") || number(v) == Some(0.0) {
        return true;
    }
    match name {
        "GrainSeed" => true,
        "VignetteMidpoint" => number(v) == Some(50.0),
        "PerspectiveScale" => number(v) == Some(100.0),
        _ => name.starts_with("Defringe") && name.contains("Hue"),
    }
}

fn apply_text(params: &mut CameraRaw, report: &mut LoadReport, name: &str, value: &str) {
    if name == "HasCrop" || CROP_KEYS.contains(&name) {
        return;
    }
    if let Some((_, field, lo, hi)) = SCALARS.iter().find(|(n, ..)| *n == name) {
        match number(value) {
            Some(v) => {
                *field(params) = v.clamp(*lo, *hi);
                report.applied += 1;
            }
            None => report.ignored.push(name.to_string()),
        }
    } else if name == "PostCropVignetteStyle" {
        match STYLES.iter().find(|(n, _)| *n == value.trim()) {
            Some((_, style)) => {
                params.vignette_style = *style;
                report.applied += 1;
            }
            None => report.ignored.push(name.to_string()),
        }
    } else if !METADATA.contains(&name) && !neutral(name, value) {
        report.ignored.push(name.to_string());
    }
}

/// Apply a Camera Raw settings file over `params`. Fails on input that isn't XMP or holds no
/// Camera Raw settings; never panics on malformed input.
pub fn load(params: &mut CameraRaw, bytes: &[u8]) -> Result<LoadReport, String> {
    let settings = Settings::parse(bytes).map_err(|e| e.to_string())?;
    let mut next = params.clone();
    let mut report = LoadReport::default();
    for (name, val) in &settings.entries {
        if let Some((_, field)) = CURVES.iter().find(|(n, _)| *n == name.as_str()) {
            match val {
                Value::Seq(items) => match curve(items) {
                    Some(pts) => {
                        *field(&mut next) = pts;
                        report.applied += 1;
                    }
                    None => report.ignored.push(name.clone()),
                },
                _ => report.ignored.push(name.clone()),
            }
        } else {
            match val {
                Value::Text(t) => apply_text(&mut next, &mut report, name, t),
                Value::Seq(_) | Value::Structured(_) => {
                    if !METADATA.contains(&name.as_str()) {
                        report.ignored.push(name.clone());
                    }
                }
            }
        }
    }
    match settings.crop() {
        CropState::None => {}
        CropState::Crop(c) => {
            report.crop = Some(c);
            report.applied += 1;
        }
        CropState::Unusable(keys) => {
            report.ignored.extend(keys.into_iter().map(str::to_string));
        }
    }
    *params = next;
    Ok(report)
}

/// The XMP toolkit [`save`] names on `x:xmpmeta`.
const TOOLKIT: &str = "PhotoCraft";

/// Did [`save`] write this file? True when its `x:xmpmeta` names PhotoCraft as the toolkit
/// (`x:xmptk`). Lightroom, Bridge and Camera Raw put their own toolkit there when they write a
/// file, so a sidecar another application touched since reads false. Never panics.
pub fn is_photocraft_xmp(bytes: &[u8]) -> bool {
    toolkit(bytes).as_deref() == Some(TOOLKIT)
}

/// Write `params` as a Camera Raw settings file Photoshop's Load Settings… reads.
pub fn save(params: &CameraRaw) -> String {
    save_with_crop(params, None)
}

/// [`save`] with a raw file's crop, so rewriting a sidecar keeps the crop it was opened with.
pub fn save_with_crop(params: &CameraRaw, crop: Option<Crop>) -> String {
    let mut p = params.clone();
    let wb = if p.temperature != 0.0 || p.tint != 0.0 { "Custom" } else { "As Shot" };
    let mut settings = Settings::default();
    settings.set("Version", "18.4");
    settings.set("ProcessVersion", "15.4");
    settings.set("WhiteBalance", wb);
    for (name, field, lo, _) in SCALARS {
        let v = *field(&mut p);
        let text = match *name {
            "Exposure2012" => signed(v, 2),
            "SharpenRadius" => signed(v, 1),
            _ if *lo < 0.0 => signed(v.round(), 0),
            _ => format!("{:.0}", v.round()),
        };
        settings.set(name, text);
    }
    let style = STYLES.iter().find(|(_, s)| *s == p.vignette_style).map_or("1", |(n, _)| n);
    settings.set("PostCropVignetteStyle", style);
    if let Some(c) = crop {
        settings.set_crop(c);
    }
    settings.set("ToneCurveName2012", "Custom");
    settings.set("HasSettings", "True");
    for (name, field) in CURVES {
        let pts = field(&mut p);
        let pts = if pts.is_empty() { vec![[0.0, 0.0], [255.0, 255.0]] } else { pts.clone() };
        let items = pts.into_iter().map(|[x, y]| format!("{}, {}", x.round(), y.round())).collect();
        settings.set_seq(name, items);
    }
    settings.to_xmp(TOOLKIT)
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    /// `CameraRaw` with the named fields set (the type is `#[non_exhaustive]`, so no struct expression), or a copy of a base with them set.
    macro_rules! cr {
        ($($f:ident : $v:expr),* $(,)?) => {{
            let mut s = CameraRaw::default();
            $(s.$f = $v;)*
            s
        }};
        ($base:expr => $($f:ident : $v:expr),* $(,)?) => {{
            let mut s: CameraRaw = $base;
            $(s.$f = $v;)*
            s
        }};
    }

    /// A hand-written settings file laid out as Photoshop 26's Camera Raw Filter saves one (the
    /// field set and order follow Camera Raw 18; the values are synthetic), plus element-form
    /// settings, which XMP allows and some writers use.
    const FIXTURE: &[u8] = br#"<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 7.0-c000 1.000000, 0000/00/00-00:00:00        ">
 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about=""
    xmlns:crs="http://ns.adobe.com/camera-raw-settings/1.0/"
   crs:Version="18.1.1" crs:ProcessVersion="15.4" crs:WhiteBalance="Custom"
   crs:IncrementalTemperature="0" crs:IncrementalTint="+10" crs:Exposure2012="+0.05"
   crs:Contrast2012="-5" crs:Highlights2012="-20" crs:Shadows2012="+10" crs:Whites2012="+35"
   crs:Blacks2012="0" crs:Texture="+30" crs:Clarity2012="+5" crs:Vibrance="+20" crs:Saturation="-5"
   crs:ParametricShadowSplit="25" crs:ParametricMidtoneSplit="50" crs:ParametricHighlightSplit="75"
   crs:Sharpness="15" crs:SharpenRadius="+1.0" crs:LuminanceSmoothing="5"
   crs:LuminanceNoiseReductionContrast="0" crs:HueAdjustmentGreen="+25" crs:SaturationAdjustmentGreen="+20"
   crs:ColorGradeBlending="50" crs:LensProfileEnable="0" crs:VignetteAmount="-20"
   crs:PerspectiveScale="100" crs:GrainAmount="10" crs:RedHue="-5" crs:GrainSeed="2834497962"
   crs:ToneCurveName2012="Linear" crs:CameraProfile="Default Color" crs:HasSettings="True" crs:HasCrop="False">
   <crs:Dehaze>+12</crs:Dehaze>
   <crs:ToneCurvePV2012>
    <rdf:Seq>
     <rdf:li>0, 0</rdf:li>
     <rdf:li>255, 255</rdf:li>
    </rdf:Seq>
   </crs:ToneCurvePV2012>
   <crs:ToneCurvePV2012Red>
    <rdf:Seq>
     <rdf:li>0, 0</rdf:li>
     <rdf:li>128, 140</rdf:li>
     <rdf:li>255, 255</rdf:li>
    </rdf:Seq>
   </crs:ToneCurvePV2012Red>
  </rdf:Description>
 </rdf:RDF>
</x:xmpmeta>"#;

    #[test]
    fn reads_a_photoshop_preset() {
        let mut p = CameraRaw::default();
        let r = load(&mut p, FIXTURE).unwrap();
        assert_eq!(p.tint, 10.0);
        assert_eq!(p.exposure, 0.05);
        assert_eq!((p.contrast, p.highlights, p.shadows, p.whites), (-5.0, -20.0, 10.0, 35.0));
        assert_eq!((p.texture, p.clarity, p.vibrance, p.saturation), (30.0, 5.0, 20.0, -5.0));
        assert_eq!((p.hsl_hue[3], p.hsl_sat[3]), (25.0, 20.0));
        assert_eq!((p.sharpen_amount, p.noise_luminance, p.grain_amount), (15.0, 5.0, 10.0));
        assert_eq!(p.grade_blending, 50.0);
        // The linear tone curve is PhotoCraft's empty one; an element-form setting is read too.
        assert!(p.point_curve.is_empty());
        assert_eq!(p.point_curve_red, vec![[0.0, 0.0], [128.0, 140.0], [255.0, 255.0]]);
        assert_eq!(p.dehaze, 12.0);
        // Lens vignetting, calibration and geometry have no counterpart.
        assert_eq!(r.ignored, vec!["VignetteAmount".to_string()], "the Calibration panel's keys load now");
        assert_eq!(p.calibration_hue[0], -5.0);
    }

    /// A settings file nested 20 000 deep overflowed the XML parser's recursion (an abort nothing
    /// can catch); it is an error now. The small stack makes a regression abort instead of passing.
    #[test]
    fn deeply_nested_settings_are_an_error_not_a_stack_overflow() {
        let xml = format!("<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">{}{}</x:xmpmeta>", "<a>".repeat(20_000), "</a>".repeat(20_000));
        let e =
            std::thread::Builder::new().stack_size(512 * 1024).spawn(move || load(&mut CameraRaw::default(), xml.as_bytes()).err()).unwrap().join().unwrap();
        assert!(e.as_deref().is_some_and(|m| m.contains("nested")), "{e:?}");
    }

    #[test]
    fn save_then_load_round_trips() {
        let mut p = cr! {
        temperature: -12.0,
        exposure: 1.25,
        contrast: 30.0,
        sharpen_radius: 1.4,
        vignette_amount: -40.0,
        vignette_style: VignetteStyle::PaintOverlay,
        curve_splits: [20.0, 45.0, 80.0],
        point_curve: vec![[0.0, 10.0], [128.0, 150.0], [255.0, 240.0]]};
        p.hsl_lum[5] = -33.0;
        p.grade_shadows.hue = 210.0;
        p.grade_shadows.sat = 40.0;
        p.grade_global.lum = 7.0;
        let text = save(&p);
        assert!(text.contains("crs:Exposure2012=\"+1.25\""));
        assert!(text.contains("crs:WhiteBalance=\"Custom\""));
        let mut back = CameraRaw::default();
        let r = load(&mut back, text.as_bytes()).unwrap();
        assert!(r.ignored.is_empty(), "{:?}", r.ignored);
        assert_eq!(back, p);
    }

    #[test]
    fn load_applies_only_the_settings_in_the_file() {
        let mut p = cr! { contrast: 40.0};
        let xmp = br#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
            <rdf:Description xmlns:crs="http://ns.adobe.com/camera-raw-settings/1.0/"><crs:Exposure2012>-9</crs:Exposure2012>
            <crs:Clarity2012>abc</crs:Clarity2012></rdf:Description></rdf:RDF></x:xmpmeta>"#;
        let r = load(&mut p, xmp).unwrap();
        assert_eq!((p.exposure, p.contrast, p.clarity), (-5.0, 40.0, 0.0));
        assert_eq!(r, LoadReport { applied: 1, ignored: vec!["Clarity2012".into()], crop: None });
    }

    #[test]
    fn malformed_input_is_an_error_and_leaves_settings_alone() {
        let before = cr! { exposure: 0.5};
        for bad in [
            &b""[..],
            b"\xff\xfe\x00garbage",
            b"<x:xmpmeta><unclosed",
            b"<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"/>",
            b"<?xml version=\"1.0\"?><!DOCTYPE a [<!ENTITY e \"x\">]><a>&e;</a>",
        ] {
            let mut p = before.clone();
            assert!(load(&mut p, bad).is_err());
            assert_eq!(p, before);
        }
        // A broken curve or NaN is skipped, not fatal.
        let xmp = br#"<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description
            xmlns:crs="http://ns.adobe.com/camera-raw-settings/1.0/" crs:Contrast2012="NaN"><crs:ToneCurvePV2012><rdf:Seq>
            <rdf:li>10</rdf:li></rdf:Seq></crs:ToneCurvePV2012></rdf:Description></rdf:RDF>"#;
        let mut p = before.clone();
        let r = load(&mut p, xmp).unwrap();
        assert_eq!(p, before);
        assert_eq!(r.ignored, vec!["Contrast2012".to_string(), "ToneCurvePV2012".to_string()]);
    }

    #[test]
    fn is_photocraft_xmp_reads_the_toolkit_marker() {
        let own = save(&cr! { contrast: 15.0});
        assert!(is_photocraft_xmp(own.as_bytes()));
        let wrapped = format!("\u{feff}<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n{own}<?xpacket end=\"w\"?>");
        assert!(is_photocraft_xmp(wrapped.as_bytes()), "an xpacket wrapper and a byte order mark");
        assert!(!is_photocraft_xmp(FIXTURE), "Camera Raw's own toolkit");
        for other in [
            &b""[..],
            b"<x:xmpmeta",
            b"<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"/>",
            b"<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" xmptk=\"PhotoCraft\"/>",
            b"<x:xmpmeta xmlns:x=\"other:ns/\" x:xmptk=\"PhotoCraft\"/>",
            b"<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"PhotoCraft 2\"/>",
            b"\xff\xfe",
        ] {
            assert!(!is_photocraft_xmp(other), "{}", String::from_utf8_lossy(other));
        }
    }

    /// A settings file with the `crs:` attributes `attrs`.
    fn with(attrs: &str) -> String {
        format!(
            "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"><rdf:Description xmlns:crs=\"{CRS_NS}\" crs:RawFileName=\"IMG_0004.CR2\" {attrs}/></rdf:RDF></x:xmpmeta>"
        )
    }

    fn report(attrs: &str) -> LoadReport {
        load(&mut CameraRaw::default(), with(attrs).as_bytes()).unwrap()
    }

    /// #9985: Lightroom's sidecar named the crop's bottom and right edges and the raw's file name
    /// as unsupported; the crop is read now, and the file name is bookkeeping.
    #[test]
    fn reads_the_crop_and_not_the_file_name() {
        let r = report(r#"crs:HasCrop="True" crs:CropTop="0.1" crs:CropLeft="0.2" crs:CropBottom="0.9" crs:CropRight="0.75" crs:CropAngle="0""#);
        assert!(r.ignored.is_empty(), "{:?}", r.ignored);
        assert_eq!(r.crop, Some(Crop { top: 0.1, left: 0.2, bottom: 0.9, right: 0.75 }));
        assert_eq!(r.applied, 1);
        // Element form, and the keys spread over two descriptions, read the same.
        let xmp = format!(
            "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"><rdf:Description xmlns:crs=\"{CRS_NS}\"><crs:HasCrop>True</crs:HasCrop><crs:CropBottom>0.5</crs:CropBottom></rdf:Description><rdf:Description xmlns:crs=\"{CRS_NS}\" crs:CropRight=\"0.5\"/></rdf:RDF>"
        );
        let r = load(&mut CameraRaw::default(), xmp.as_bytes()).unwrap();
        assert_eq!(r.crop, Some(Crop { top: 0.0, left: 0.0, bottom: 0.5, right: 0.5 }), "missing edges are the frame's");
        assert!(r.ignored.is_empty(), "{:?}", r.ignored);
    }

    #[test]
    fn no_crop_without_has_crop_true() {
        let keys = r#"crs:CropTop="0.1" crs:CropLeft="0.2" crs:CropBottom="0.9" crs:CropRight="0.75" crs:CropAngle="3""#;
        for has in ["", r#"crs:HasCrop="False""#, r#"crs:HasCrop="false""#] {
            let r = report(&format!("{has} {keys}"));
            assert_eq!((r.crop, r.ignored.clone(), r.applied), (None, Vec::new(), 0), "{has:?}: {r:?}");
        }
        // The whole frame is no crop, and nothing to report.
        let r = report(r#"crs:HasCrop="True" crs:CropTop="0" crs:CropLeft="0" crs:CropBottom="1" crs:CropRight="1" crs:CropAngle="0""#);
        assert_eq!((r.crop, r.ignored.len()), (None, 0));
        let r = report(r#"crs:HasCrop="yes" crs:CropBottom="0.5""#);
        assert_eq!((r.crop, r.ignored), (None, vec!["HasCrop".to_string()]));
    }

    /// Hostile and straightened crops never panic: out-of-range fractions are clamped to the
    /// frame; an inverted, empty, unreadable or straightened crop isn't applied and its keys are
    /// named.
    #[test]
    fn unusable_crops_are_reported_not_applied() {
        let r = report(r#"crs:HasCrop="True" crs:CropTop="-0.5" crs:CropLeft="0.25" crs:CropBottom="7" crs:CropRight="1.0000001""#);
        assert_eq!(r.crop, Some(Crop { top: 0.0, left: 0.25, bottom: 1.0, right: 1.0 }));
        assert!(r.ignored.is_empty(), "{:?}", r.ignored);
        let all = || CROP_KEYS.iter().map(|k| k.to_string()).collect::<Vec<_>>();
        for bad in [
            r#"crs:CropTop="0.8" crs:CropLeft="0.1" crs:CropBottom="0.2" crs:CropRight="0.9" crs:CropAngle="0""#,
            r#"crs:CropTop="0.5" crs:CropLeft="0.1" crs:CropBottom="0.5" crs:CropRight="0.9" crs:CropAngle="0""#,
            r#"crs:CropTop="2" crs:CropLeft="0.1" crs:CropBottom="3" crs:CropRight="0.9" crs:CropAngle="0""#,
            r#"crs:CropTop="NaN" crs:CropLeft="0.1" crs:CropBottom="0.9" crs:CropRight="0.9" crs:CropAngle="0""#,
            r#"crs:CropTop="0.1" crs:CropLeft="inf" crs:CropBottom="0.9" crs:CropRight="0.9" crs:CropAngle="0""#,
            r#"crs:CropTop="0.1" crs:CropLeft="0.1" crs:CropBottom="0.9" crs:CropRight="" crs:CropAngle="0""#,
            r#"crs:CropTop="0.1" crs:CropLeft="0.1" crs:CropBottom="0.9" crs:CropRight="0.9" crs:CropAngle="1.38""#,
            r#"crs:CropTop="0.1" crs:CropLeft="0.1" crs:CropBottom="0.9" crs:CropRight="0.9" crs:CropAngle="NaN""#,
        ] {
            let r = report(&format!(r#"crs:HasCrop="True" {bad}"#));
            assert_eq!((r.crop, r.ignored, r.applied), (None, all(), 0), "{bad}");
        }
        let r = report(r#"crs:HasCrop="True" crs:CropAngle="-2.5""#);
        assert_eq!(r.ignored, vec!["CropAngle".to_string()], "only the keys the file has");
    }

    /// Corners map as `photocraft-raw`'s orientation maps pixels (`develop::source_xy`).
    #[test]
    fn crops_follow_the_orientation() {
        let c = Crop { top: 0.1, left: 0.2, bottom: 0.6, right: 0.5 };
        let want = [
            (1, Crop { top: 0.1, left: 0.2, bottom: 0.6, right: 0.5 }),
            (2, Crop { top: 0.1, left: 0.5, bottom: 0.6, right: 0.8 }),
            (3, Crop { top: 0.4, left: 0.5, bottom: 0.9, right: 0.8 }),
            (4, Crop { top: 0.4, left: 0.2, bottom: 0.9, right: 0.5 }),
            (5, Crop { top: 0.2, left: 0.1, bottom: 0.5, right: 0.6 }),
            (6, Crop { top: 0.2, left: 0.4, bottom: 0.5, right: 0.9 }),
            (7, Crop { top: 0.5, left: 0.4, bottom: 0.8, right: 0.9 }),
            (8, Crop { top: 0.5, left: 0.1, bottom: 0.8, right: 0.6 }),
            (0, Crop { top: 0.1, left: 0.2, bottom: 0.6, right: 0.5 }),
        ];
        for (o, w) in want {
            let got = c.oriented(o);
            let close = [got.top - w.top, got.left - w.left, got.bottom - w.bottom, got.right - w.right].iter().all(|d| d.abs() < 1e-6);
            assert!(close, "orientation {o}: {got:?}");
        }
        assert_eq!(c.pixels(10, 20), Some((2, 2, 5, 12)));
        assert_eq!(Crop { top: 0.5, left: 0.5, bottom: 0.5001, right: 0.5001 }.pixels(10, 10), Some((5, 5, 6, 6)), "at least a pixel");
        assert_eq!(Crop { top: 0.999, left: 0.999, bottom: 1.0, right: 1.0 }.pixels(10, 10), Some((9, 9, 10, 10)), "inside the frame");
        assert_eq!(c.pixels(0, 10), None);
    }

    #[test]
    fn save_with_crop_round_trips() {
        let p = cr! { contrast: 20.0};
        let crop = Crop { top: 0.125, left: 0.25, bottom: 0.875, right: 0.5 };
        let text = save_with_crop(&p, Some(crop));
        assert!(is_photocraft_xmp(text.as_bytes()));
        let mut back = CameraRaw::default();
        let r = load(&mut back, text.as_bytes()).unwrap();
        assert_eq!((back, r.crop), (p.clone(), Some(crop)));
        assert!(r.ignored.is_empty(), "{:?}", r.ignored);
        assert_eq!(load(&mut CameraRaw::default(), save(&p).as_bytes()).unwrap().crop, None);
    }
}