GitCorner

Code

ossartcraftfilmcraft🌐
f67d38b35cc5
//! DNG opcode lists (DNG 1.7.1.0 chapter 7, "Opcode List Processing"): OpcodeList1 on the raw
//! samples as stored, OpcodeList2 on the linear reference values (after linearisation, black and
//! white levels, before white balance), OpcodeList3 on the demosaiced picture.
//!
//! An opcode list is big-endian whatever the file's byte order: a count, then per opcode its ID,
//! the DNG version that defined it, flags (bit 0 optional, bit 1 skippable for preview quality)
//! and the size of its parameters. Implemented: WarpRectilinear (1), WarpFisheye (2),
//! FixVignetteRadial (3), FixBadPixelsConstant (4), FixBadPixelsList (5), TrimBounds (6),
//! MapTable (7), MapPolynomial (8), GainMap (9), DeltaPerRow / Column (10, 11), ScalePerRow /
//! Column (12, 13) and WarpRectilinear2 (14, with the skip rule for a following warp). An unknown
//! opcode, or one from a DNG version newer than 1.7.1.0, is skipped when it is optional and an
//! error when it is not. Values are clipped to the stage's logical range after every opcode
//! (0–65535 for OpcodeList1, 0–1 after).
//!
//! Interpretations where the specification leaves room (recorded in the crate README):
//! coordinates of OpcodeList1 are those of the stored raw image, of OpcodeList2 and 3 those of the
//! ActiveArea; GainMap positions are relative to the whole image of the stage (row / height,
//! column / width); Delta and MapPolynomial values are in the 0–1 range at every stage (scaled by
//! 65535 on OpcodeList1's integer samples), MapTable indexes 16-bit values (0–1 scaled by 65535
//! after OpcodeList1); warps resample with a Catmull–Rom cubic and are refused on CFA mosaics,
//! as is TrimBounds before OpcodeList3.
//!
//! Every count and size is checked against the bytes of the parameter area before anything is
//! allocated, and per-opcode work is bounded by the image size.

use crate::{Error, Result};

/// The newest DNG version whose opcodes this reader knows (1.7.1.0).
const KNOWN_VERSION: u32 = 0x0107_0100;

/// Most opcodes in one list (each may touch the whole picture).
pub const MAX_OPCODES: usize = 256;

pub use craft_raw::opcode_raw::Opcode;

/// The opcode lists of a raw IFD.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Lists {
    pub list1: Vec<Opcode>,
    pub list2: Vec<Opcode>,
    pub list3: Vec<Opcode>,
}

/// Parse an opcode list (big-endian).
pub fn parse(b: &[u8]) -> Result<Vec<Opcode>> {
    craft_raw::opcode_raw::parse(b, MAX_OPCODES).map_err(|_| Error::Malformed("opcode list"))
}

/// The picture an opcode list works on: `planes` interleaved samples per pixel.
pub struct Image<'a> {
    pub data: &'a mut Vec<f32>,
    pub width: usize,
    pub height: usize,
    pub planes: usize,
    /// The stage's logical maximum: 65535 for OpcodeList1 (integer samples, rounded after every
    /// opcode), 1 for OpcodeList2 and 3.
    pub max: f32,
    /// A CFA mosaic (one sample per pixel before the demosaic): warps are refused.
    pub mosaic: bool,
    /// TrimBounds is allowed (OpcodeList3 only: the picture may change size).
    pub allow_trim: bool,
    /// Origin of the picture after TrimBounds, relative to where it started.
    pub origin: (usize, usize),
}

impl Image<'_> {
    fn integer(&self) -> bool {
        self.max > 1.0
    }

    fn clip(&mut self) {
        let (max, int) = (self.max, self.integer());
        for v in self.data.iter_mut() {
            let c = v.clamp(0.0, max);
            *v = if int {
                c.round()
            } else if c.is_nan() {
                0.0
            } else {
                c
            };
        }
    }
}

/// Apply `list`. `preview` skips the opcodes marked as only needed for full quality.
pub fn apply(list: &[Opcode], img: &mut Image, preview: bool) -> Result<()> {
    let mut skip_warp = false;
    for op in list {
        let skip_this = std::mem::take(&mut skip_warp) && matches!(op.id, 1 | 2);
        if skip_this || (preview && op.preview_skippable()) {
            continue;
        }
        let known = op.version <= KNOWN_VERSION && (1..=14).contains(&op.id);
        if !known {
            if op.optional() {
                continue;
            }
            return Err(Error::Unsupported(format!("DNG opcode {} (version {:08x})", op.id, op.version)));
        }
        match run(op, img) {
            Ok(()) => {}
            // an optional opcode this reader cannot apply here is left out
            Err(Error::Unsupported(_)) if op.optional() => continue,
            Err(e) => return Err(e),
        }
        img.clip();
        if op.id == 14 && op.optional() {
            skip_warp = true;
        }
    }
    Ok(())
}

fn run(op: &Opcode, img: &mut Image) -> Result<()> {
    use craft_raw::opcode_params::{Context, Params as P, ParseError};
    let context = Context { width: img.width, height: img.height, planes: img.planes, mosaic: img.mosaic, allow_trim: img.allow_trim };
    let params = craft_raw::opcode_params::parse(op.id, &op.params, context).map_err(|e| match e {
        ParseError::Malformed(s) => Error::Malformed(s),
        ParseError::Unsupported(s) => Error::Unsupported(s.into()),
    })?;
    match params {
        P::WarpRectilinear { planes, center } => {
            warp(planes.into_iter().map(|v| Radial::Rectilinear([v[0], v[1], v[2], v[3]], [v[4], v[5]])).collect(), center, img)
        }
        P::WarpFisheye { planes, center } => warp(planes.into_iter().map(Radial::Fisheye).collect(), center, img),
        P::WarpRectilinear2 { planes, center, reciprocal } => warp(
            planes
                .into_iter()
                .map(|v| {
                    let mut kr = [0.0; 15];
                    kr.copy_from_slice(&v[..15]);
                    Radial::Rectilinear2 { kr, kt: [v[15], v[16]], min: v[17], max: v[18], reciprocal }
                })
                .collect(),
            center,
            img,
        ),
        P::FixVignetteRadial { k, center } => vignette(k, center, img),
        P::FixBadPixelsConstant { constant, bayer_phase } => {
            let c = if img.integer() { constant as f32 } else { constant as f32 / 65_535.0 };
            let bad: Vec<usize> = img.data.iter().enumerate().filter_map(|(i, px)| (*px == c).then_some(i)).collect();
            fix_bad(img, bayer_phase, &bad)
        }
        P::FixBadPixelsList { bayer_phase, points, rects } => {
            let (w, h) = (img.width, img.height);
            let mut mask = vec![false; w * h];
            for (y, x) in points.iter() {
                let (y, x) = (y as usize, x as usize);
                if x < w && y < h {
                    mask[y * w + x] = true;
                }
            }
            for [t, l, b, r] in rects.iter() {
                for y in (t as usize).min(h)..(b as usize).min(h) {
                    for x in (l as usize).min(w)..(r as usize).min(w) {
                        mask[y * w + x] = true;
                    }
                }
            }
            let bad: Vec<usize> = mask.iter().enumerate().filter_map(|(i, &b)| b.then_some(i)).collect();
            fix_bad(img, bayer_phase, &bad)
        }
        P::TrimBounds { top, left, bottom, right } => trim(top, left, bottom, right, img),
        p => area_op(p, img),
    }
}

/// The area fields of opcodes 7–13, clamped to the picture.
struct Area {
    top: usize,
    left: usize,
    bottom: usize,
    right: usize,
    plane: usize,
    planes: usize,
    row_pitch: usize,
    col_pitch: usize,
}

impl Area {
    fn from_shared(a: craft_raw::opcode_params::Area) -> Area {
        Area { top: a.top, left: a.left, bottom: a.bottom, right: a.right, plane: a.plane, planes: a.planes, row_pitch: a.row_pitch, col_pitch: a.col_pitch }
    }

    /// Rows of the area, in order.
    fn rows(&self) -> impl Iterator<Item = usize> + use<> {
        (self.top..self.bottom.max(self.top)).step_by(self.row_pitch)
    }

    fn cols(&self) -> impl Iterator<Item = usize> + use<> {
        (self.left..self.right.max(self.left)).step_by(self.col_pitch)
    }

    /// Visit every affected sample: f(row index in area, col index in area, x, y, plane index in
    /// area, value).
    fn each(&self, img: &mut Image, mut f: impl FnMut(usize, usize, usize, usize, usize, &mut f32)) {
        let (w, np) = (img.width, img.planes);
        for (ri, y) in self.rows().enumerate() {
            for (ci, x) in self.cols().enumerate() {
                for p in 0..self.planes {
                    if let Some(v) = img.data.get_mut((y * w + x) * np + self.plane + p) {
                        f(ri, ci, x, y, p, v);
                    }
                }
            }
        }
    }
}

fn area_op(params: craft_raw::opcode_params::Params<'_>, img: &mut Image) -> Result<()> {
    use craft_raw::opcode_params::Params as P;
    let scale = img.max;
    match params {
        P::MapTable { area, table } => {
            let a = Area::from_shared(area);
            let last = table.last().copied().unwrap_or(0) as f32;
            a.each(img, |_, _, _, _, _, v| {
                let code = (*v / scale * 65_535.0).round().clamp(0.0, 65_535.0) as usize;
                *v = table.get(code).map_or(last, |x| f32::from(*x)) / 65_535.0 * scale;
            });
        }
        P::MapPolynomial { area, coefficients } => {
            let a = Area::from_shared(area);
            a.each(img, |_, _, _, _, _, v| {
                let x = f64::from(*v / scale);
                let y = coefficients.iter().rev().fold(0.0, |acc, k| acc * x + k);
                *v = (y * f64::from(scale)) as f32;
            });
        }
        P::GainMap { area, points_v, points_h, spacing, origin, map_planes, gains } => {
            return gain_map(&Area::from_shared(area), GainMap { points_v, points_h, spacing, origin, map_planes, gains }, img);
        }
        P::DeltaPerRow { area, deltas } => adjust(&Area::from_shared(area), &deltas, true, true, img),
        P::DeltaPerColumn { area, deltas } => adjust(&Area::from_shared(area), &deltas, false, true, img),
        P::ScalePerRow { area, scales } => adjust(&Area::from_shared(area), &scales, true, false, img),
        P::ScalePerColumn { area, scales } => adjust(&Area::from_shared(area), &scales, false, false, img),
        _ => return Err(Error::Malformed("opcode parameters")),
    }
    Ok(())
}
fn adjust(a: &Area, vals: &[f32], per_row: bool, delta: bool, img: &mut Image) {
    let scale = img.max;
    a.each(img, |ri, ci, _, _, _, v| {
        let k = vals.get(if per_row { ri } else { ci }).copied().unwrap_or(if delta { 0.0 } else { 1.0 });
        *v = if delta { *v + k * scale } else { *v * k };
    });
}
struct GainMap {
    points_v: usize,
    points_h: usize,
    spacing: [f64; 2],
    origin: [f64; 2],
    map_planes: usize,
    gains: Vec<f32>,
}
fn gain_map(a: &Area, map: GainMap, img: &mut Image) -> Result<()> {
    let GainMap { points_v, points_h, spacing, origin, map_planes, gains } = map;
    let [spacing_v, spacing_h] = spacing;
    let [origin_v, origin_h] = origin;
    let (w, h) = (img.width.max(1) as f64, img.height.max(1) as f64);
    let pos = |rel: f64, origin: f64, spacing: f64, points: usize| {
        if points <= 1 {
            return 0.0;
        }
        ((rel - origin) / spacing).clamp(0.0, (points - 1) as f64)
    };
    let gain = |p: usize, mv: f64, mh: f64| -> f32 {
        let mp = p.min(map_planes - 1);
        let (v0, h0) = (mv.floor() as usize, mh.floor() as usize);
        let (v1, h1) = ((v0 + 1).min(points_v - 1), (h0 + 1).min(points_h - 1));
        let (fv, fh) = (mv - v0 as f64, mh - h0 as f64);
        let g = |v: usize, hh: usize| f64::from(gains.get((v * points_h + hh) * map_planes + mp).copied().unwrap_or(1.0));
        let top = g(v0, h0) * (1.0 - fh) + g(v0, h1) * fh;
        let bottom = g(v1, h0) * (1.0 - fh) + g(v1, h1) * fh;
        (top * (1.0 - fv) + bottom * fv) as f32
    };
    a.each(img, |_, _, x, y, p, v| {
        let mv = pos(y as f64 / h, origin_v, spacing_v, points_v);
        let mh = pos(x as f64 / w, origin_h, spacing_h, points_h);
        *v *= gain(p, mv, mh);
    });
    Ok(())
}

/// The optical centre in pixels and the distance to the farthest corner (chapter 7).
fn centre(img: &Image, cx: f64, cy: f64) -> (f64, f64, f64) {
    let (x1, y1) = (img.width.saturating_sub(1) as f64, img.height.saturating_sub(1) as f64);
    let (cx, cy) = (cx * x1, cy * y1);
    let mx = cx.abs().max((x1 - cx).abs());
    let my = cy.abs().max((y1 - cy).abs());
    (cx, cy, (mx * mx + my * my).sqrt())
}

fn vignette(k: [f64; 5], center: [f64; 2], img: &mut Image) -> Result<()> {
    let [cx, cy] = center;
    let (cx, cy, m) = centre(img, cx, cy);
    if m <= 0.0 {
        return Ok(());
    }
    let (w, np) = (img.width, img.planes);
    for (i, px) in img.data.chunks_exact_mut(np.max(1)).enumerate() {
        let (x, y) = ((i % w) as f64, (i / w) as f64);
        let r2 = ((x - cx).powi(2) + (y - cy).powi(2)) / (m * m);
        let g = 1.0 + r2 * (k[0] + r2 * (k[1] + r2 * (k[2] + r2 * (k[3] + r2 * k[4]))));
        for v in px {
            *v = (f64::from(*v) * g) as f32;
        }
    }
    Ok(())
}

/// Interpolate over `bad` pixels of a Bayer mosaic: the mean of the nearest good pixels of the
/// same colour (within 2, else 4, … up to 8 pixels).
fn fix_bad(img: &mut Image, phase: u32, bad: &[usize]) -> Result<()> {
    if bad.is_empty() {
        return Ok(());
    }
    // a real sensor has few bad pixels; a list covering much of the picture is not one
    if bad.len() > img.data.len() / 16 + 64 {
        return Err(Error::Unsupported("bad pixel opcode covering much of the picture".into()));
    }
    if img.planes != 1 {
        return Err(Error::Unsupported("bad pixel opcodes on a picture that is not a mosaic".into()));
    }
    // BayerPhase: the colour of the top-left pixel (0 red, 1 green in a red row, 2 green in a
    // blue row, 3 blue)
    let quad: [u8; 4] = match phase {
        0 => [0, 1, 1, 2],
        1 => [1, 0, 2, 1],
        2 => [1, 2, 0, 1],
        3 => [2, 1, 1, 0],
        _ => return Err(Error::Malformed("BayerPhase")),
    };
    let (w, h) = (img.width, img.height);
    let mut is_bad = vec![false; w * h];
    for &i in bad {
        if let Some(b) = is_bad.get_mut(i) {
            *b = true;
        }
    }
    let color = |x: usize, y: usize| quad[(y & 1) * 2 + (x & 1)];
    let src = img.data.clone();
    for &i in bad {
        let (x, y) = (i % w, i / w);
        let me = color(x, y);
        for radius in [2isize, 4, 6, 8] {
            let (mut sum, mut n) = (0f64, 0u32);
            for dy in -radius..=radius {
                for dx in -radius..=radius {
                    let (sx, sy) = (x as isize + dx, y as isize + dy);
                    if sx < 0 || sy < 0 || sx as usize >= w || sy as usize >= h || (dx == 0 && dy == 0) {
                        continue;
                    }
                    let (sx, sy) = (sx as usize, sy as usize);
                    if color(sx, sy) == me && !is_bad[sy * w + sx] {
                        sum += f64::from(src[sy * w + sx]);
                        n += 1;
                    }
                }
            }
            if n > 0 {
                if let Some(v) = img.data.get_mut(i) {
                    *v = (sum / f64::from(n)) as f32;
                }
                break;
            }
        }
    }
    Ok(())
}

fn trim(t: usize, l: usize, b: usize, rt: usize, img: &mut Image) -> Result<()> {
    let (np, w) = (img.planes, img.width);
    let nw = rt - l;
    let mut out = Vec::with_capacity(nw * (b - t) * np);
    for y in t..b {
        out.extend_from_slice(img.data.get((y * w + l) * np..(y * w + rt) * np).ok_or(Error::Malformed("TrimBounds"))?);
    }
    *img.data = out;
    img.width = nw;
    img.height = b - t;
    img.origin = (img.origin.0 + l, img.origin.1 + t);
    Ok(())
}

/// Catmull–Rom weights for a fractional position `t` in 0–1.
fn cubic(t: f64) -> [f64; 4] {
    let (t2, t3) = (t * t, t * t * t);
    [(-t3 + 2.0 * t2 - t) / 2.0, (3.0 * t3 - 5.0 * t2 + 2.0) / 2.0, (-3.0 * t3 + 4.0 * t2 + t) / 2.0, (t3 - t2) / 2.0]
}

/// The radial model of a warp opcode for one plane.
enum Radial {
    /// WarpRectilinear: f = kr0 + kr1 r² + kr2 r⁴ + kr3 r⁶, tangential kt0, kt1.
    Rectilinear([f64; 4], [f64; 2]),
    /// WarpFisheye: f = (kr0 θ + kr1 θ³ + kr2 θ⁵ + kr3 θ⁷) / r, θ = atan r.
    Fisheye([f64; 4]),
    /// WarpRectilinear2: f(r) = Σ kr_p r^p (p ≤ 14) at r clamped to the valid range, multiplied
    /// (or divided, reciprocal) into r; tangential kt0, kt1.
    Rectilinear2 { kr: [f64; 15], kt: [f64; 2], min: f64, max: f64, reciprocal: bool },
}

impl Radial {
    /// Source offset (normalised) of destination offset (dx, dy).
    fn map(&self, dx: f64, dy: f64) -> (f64, f64) {
        let r2 = dx * dx + dy * dy;
        let tangential = |kt: &[f64; 2]| (kt[0] * (2.0 * dx * dy) + kt[1] * (r2 + 2.0 * dx * dx), kt[1] * (2.0 * dx * dy) + kt[0] * (r2 + 2.0 * dy * dy));
        match self {
            Radial::Rectilinear(kr, kt) => {
                let f = kr[0] + r2 * (kr[1] + r2 * (kr[2] + r2 * kr[3]));
                let (tx, ty) = tangential(kt);
                (f * dx + tx, f * dy + ty)
            }
            Radial::Fisheye(kr) => {
                let r = r2.sqrt();
                let f = if r < 1e-12 {
                    kr[0]
                } else {
                    let t = r.atan();
                    let t2 = t * t;
                    t * (kr[0] + t2 * (kr[1] + t2 * (kr[2] + t2 * kr[3]))) / r
                };
                (f * dx, f * dy)
            }
            Radial::Rectilinear2 { kr, kt, min, max, reciprocal } => {
                let r = r2.sqrt().clamp(*min, *max);
                let f = kr.iter().rev().fold(0.0, |acc, k| acc * r + k);
                let f = if *reciprocal {
                    if f.abs() < 1e-12 {
                        return (dx, dy);
                    }
                    1.0 / f
                } else {
                    f
                };
                let (tx, ty) = tangential(kt);
                (f * dx + tx, f * dy + ty)
            }
        }
    }
}

fn warp(sets: Vec<Radial>, center: [f64; 2], img: &mut Image) -> Result<()> {
    let [cx, cy] = center;
    let (cx, cy, m) = centre(img, cx, cy);
    if m <= 0.0 {
        return Ok(());
    }
    let (w, h, np) = (img.width, img.height, img.planes);
    let src = img.data.clone();
    let sample = |p: usize, x: isize, y: isize| {
        let (x, y) = (x.clamp(0, w as isize - 1) as usize, y.clamp(0, h as isize - 1) as usize);
        f64::from(src.get((y * w + x) * np + p).copied().unwrap_or(0.0))
    };
    for (i, px) in img.data.chunks_exact_mut(np.max(1)).enumerate() {
        let (x, y) = ((i % w) as f64, (i / w) as f64);
        let (dx, dy) = ((x - cx) / m, (y - cy) / m);
        for (p, v) in px.iter_mut().enumerate() {
            let model = sets.get(p).or(sets.first());
            let Some(model) = model else { continue };
            let (ox, oy) = model.map(dx, dy);
            let (sx, sy) = (cx + m * ox, cy + m * oy);
            if !sx.is_finite() || !sy.is_finite() {
                continue;
            }
            let (fx, fy) = (sx.floor(), sy.floor());
            let (wx, wy) = (cubic(sx - fx), cubic(sy - fy));
            let (ix, iy) = (fx.clamp(-4.0, w as f64 + 4.0) as isize, fy.clamp(-4.0, h as f64 + 4.0) as isize);
            let mut acc = 0.0;
            for (j, wyj) in wy.iter().enumerate() {
                let mut row = 0.0;
                for (k, wxk) in wx.iter().enumerate() {
                    row += wxk * sample(p, ix + k as isize - 1, iy + j as isize - 1);
                }
                acc += wyj * row;
            }
            *v = acc as f32;
        }
    }
    Ok(())
}

#[cfg(any(test, feature = "writer"))]
/// Encode an opcode list (big-endian), for tests: (id, flags, parameters).
pub fn encode(list: &[(u32, u32, Vec<u8>)]) -> Result<Vec<u8>> {
    // staged with fallible allocations, so running out of memory is an error, not an abort
    let mut records: Vec<Opcode> = Vec::new();
    records.try_reserve_exact(list.len()).map_err(|_| Error::TooLarge)?;
    for (id, flags, params) in list {
        let mut copy = Vec::new();
        copy.try_reserve_exact(params.len()).map_err(|_| Error::TooLarge)?;
        copy.extend_from_slice(params);
        records.push(Opcode { id: *id, version: if *id == 14 { 0x0106_0000 } else { 0x0103_0000 }, flags: *flags, params: copy });
    }
    craft_raw::opcode_raw::encode(&records).map_err(|_| Error::Malformed("opcode list cannot be encoded"))
}

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

    fn encode(list: &[(u32, u32, Vec<u8>)]) -> Vec<u8> {
        super::encode(list).unwrap()
    }

    fn u(v: &[u32]) -> Vec<u8> {
        v.iter().flat_map(|x| x.to_be_bytes()).collect()
    }

    fn d(v: &[f64]) -> Vec<u8> {
        v.iter().flat_map(|x| x.to_be_bytes()).collect()
    }

    fn f(v: &[f32]) -> Vec<u8> {
        v.iter().flat_map(|x| x.to_be_bytes()).collect()
    }

    fn area(rect_plane_pitch: [u32; 8]) -> Vec<u8> {
        u(&rect_plane_pitch)
    }

    /// Run one opcode on a float picture (range 0–1).
    fn run1(id: u32, params: Vec<u8>, data: &mut Vec<f32>, w: usize, h: usize, planes: usize) -> Result<(usize, usize)> {
        let list = parse(&encode(&[(id, 0, params)]))?;
        let mut img = Image { data, width: w, height: h, planes, max: 1.0, mosaic: false, allow_trim: true, origin: (0, 0) };
        apply(&list, &mut img, false)?;
        Ok((img.width, img.height))
    }

    fn close(a: f32, b: f32) -> bool {
        (a - b).abs() < 1e-5
    }

    #[test]
    fn list_parsing_and_hostile_lists() {
        let l = encode(&[(9, 1, vec![1, 2, 3]), (3, 2, vec![])]);
        let p = parse(&l).unwrap();
        assert_eq!(p.len(), 2);
        assert_eq!((p[0].id, p[0].version, p[0].params.as_slice()), (9, 0x0103_0000, &[1u8, 2, 3][..]));
        assert!(p[0].optional() && !p[0].preview_skippable());
        assert!(!p[1].optional() && p[1].preview_skippable());
        // every truncation is an error, never a panic
        for n in 0..l.len() {
            assert!(parse(&l[..n]).is_err(), "{n}");
        }
        // counts and sizes beyond the data
        assert!(parse(&u(&[u32::MAX])).is_err());
        assert!(parse(&u(&[1, 9, 0, 0, u32::MAX])).is_err());
        assert!(parse(&u(&[(MAX_OPCODES + 1) as u32]).into_iter().chain(vec![0; 16 * (MAX_OPCODES + 1)]).collect::<Vec<_>>()).is_err());
        assert_eq!(parse(&u(&[0])).unwrap(), vec![]);
    }

    #[test]
    fn unknown_and_newer_opcodes() {
        let mut data = vec![0.5f32; 4];
        let mut img = Image { data: &mut data, width: 2, height: 2, planes: 1, max: 1.0, mosaic: false, allow_trim: false, origin: (0, 0) };
        // unknown id: skipped when optional, an error when not
        assert!(apply(&parse(&encode(&[(99, 1, vec![])])).unwrap(), &mut img, false).is_ok());
        assert!(matches!(apply(&parse(&encode(&[(99, 0, vec![])])).unwrap(), &mut img, false), Err(Error::Unsupported(_))));
        // a known id from a newer DNG version is unknown
        let newer = [Opcode { id: 3, version: 0x0108_0000, flags: 0, params: vec![] }];
        assert!(matches!(apply(&newer, &mut img, false), Err(Error::Unsupported(_))));
        // an opcode this stage cannot run: skipped when optional
        let trim = Opcode { id: 6, version: 0x0103_0000, flags: 1, params: u(&[0, 0, 1, 1]) };
        assert!(apply(std::slice::from_ref(&trim), &mut img, false).is_ok());
        assert!(apply(&[Opcode { flags: 0, ..trim }], &mut img, false).is_err());
        // preview quality skips flagged opcodes
        let scale = Opcode { id: 12, version: 0x0103_0000, flags: 2, params: [area([0, 0, 2, 2, 0, 1, 1, 1]), u(&[2]), f(&[0.5, 0.5])].concat() };
        apply(std::slice::from_ref(&scale), &mut img, true).unwrap();
        assert_eq!(img.data[0], 0.5);
        apply(&[scale], &mut img, false).unwrap();
        assert_eq!(img.data[0], 0.25);
        // malformed parameters are errors
        assert!(apply(&[Opcode { id: 10, version: 0x0103_0000, flags: 0, params: vec![0; 5] }], &mut img, false).is_err());
    }

    #[test]
    fn map_table_polynomial_delta_and_scale() {
        // 4 × 2, two planes; affect plane 1 only, every second column
        let mut data: Vec<f32> = (0..16).map(|i| i as f32 / 20.0).collect();
        let orig = data.clone();
        // MapTable: 16-bit identity reversed (v → 1 − v)
        let table: Vec<u8> = (0..=65_535u32).rev().flat_map(|v| (v as u16).to_be_bytes()).collect();
        run1(7, [area([0, 0, 2, 4, 1, 1, 1, 2]), u(&[65_536]), table].concat(), &mut data, 4, 2, 2).unwrap();
        for (i, (a, b)) in data.iter().zip(&orig).enumerate() {
            let (x, p) = ((i / 2) % 4, i % 2);
            let want = if p == 1 && x % 2 == 0 { 1.0 - (b * 65_535.0).round() / 65_535.0 } else { *b };
            assert!(close(*a, want), "{i}: {a} {want}");
        }
        // MapPolynomial 0.1 + 0.5 x on everything
        let mut data = orig.clone();
        run1(8, [area([0, 0, 2, 4, 0, 2, 1, 1]), u(&[1]), d(&[0.1, 0.5])].concat(), &mut data, 4, 2, 2).unwrap();
        for (a, b) in data.iter().zip(&orig) {
            assert!(close(*a, 0.1 + 0.5 * b));
        }
        // DeltaPerRow (rows 0, 1), ScalePerColumn (columns 1..3)
        let mut data = orig.clone();
        run1(10, [area([0, 0, 2, 4, 0, 2, 1, 1]), u(&[2]), f(&[0.01, -0.02])].concat(), &mut data, 4, 2, 2).unwrap();
        for (i, (a, b)) in data.iter().zip(&orig).enumerate() {
            let want = (b + if i < 8 { 0.01 } else { -0.02 }).clamp(0.0, 1.0);
            assert!(close(*a, want), "{i}");
        }
        let mut data = orig.clone();
        run1(13, [area([0, 1, 2, 3, 0, 2, 1, 1]), u(&[2]), f(&[2.0, 0.5])].concat(), &mut data, 4, 2, 2).unwrap();
        for (i, (a, b)) in data.iter().zip(&orig).enumerate() {
            let x = (i / 2) % 4;
            let want = match x {
                1 => (b * 2.0).min(1.0),
                2 => b * 0.5,
                _ => *b,
            };
            assert!(close(*a, want), "{i}");
        }
        // too few deltas for the rows is an error
        let mut data = orig.clone();
        assert!(run1(10, [area([0, 0, 2, 4, 0, 2, 1, 1]), u(&[1]), f(&[0.1])].concat(), &mut data, 4, 2, 2).is_err());
        // zero pitch is an error, areas outside the picture are clamped
        assert!(run1(12, [area([0, 0, 2, 4, 0, 1, 0, 1]), u(&[0])].concat(), &mut data, 4, 2, 2).is_err());
        let mut data = orig.clone();
        run1(12, [area([1, 3, 99, 99, 5, 9, 1, 1]), u(&[1]), f(&[0.0])].concat(), &mut data, 4, 2, 2).unwrap();
        assert_eq!(data, orig, "plane 5 does not exist");
    }

    #[test]
    fn gain_map_interpolates_and_replicates() {
        // 2 × 2 map at the corners of the picture's relative coordinates 0 and 0.5
        let (w, h) = (8, 8);
        let mut data = vec![0.25f32; w * h];
        let gains = f(&[1.0, 2.0, 3.0, 4.0]);
        run1(9, [area([0, 0, 8, 8, 0, 1, 1, 1]), u(&[2, 2]), d(&[0.5, 0.5, 0.0, 0.0]), u(&[1]), gains].concat(), &mut data, w, h, 1).unwrap();
        let g = |x: usize, y: usize| data[y * w + x] / 0.25;
        assert!(close(g(0, 0), 1.0));
        assert!(close(g(4, 0), 2.0), "{}", g(4, 0));
        assert!(close(g(0, 4), 3.0));
        assert!(close(g(2, 2), 2.5), "bilinear in the middle: {}", g(2, 2));
        assert!(close(g(7, 7), 4.0), "replicated past the map");
        // a map larger than its parameters is an error
        let mut data = vec![0.25f32; 4];
        assert!(
            run1(9, [area([0, 0, 2, 2, 0, 1, 1, 1]), u(&[1000, 1000]), d(&[0.1, 0.1, 0.0, 0.0]), u(&[1]), f(&[1.0])].concat(), &mut data, 2, 2, 1).is_err()
        );
    }

    #[test]
    fn vignette_and_warps() {
        // FixVignetteRadial k0 = 1: g = 1 + r², the centre unchanged, the corners doubled
        let (w, h) = (5, 5);
        let mut data = vec![0.25f32; w * h];
        run1(3, d(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5]), &mut data, w, h, 1).unwrap();
        assert!(close(data[2 * w + 2], 0.25));
        assert!(close(data[0], 0.5) && close(data[w * h - 1], 0.5));
        assert!(close(data[2 * w], 0.25 * 1.5), "r² = 0.5 at the edge middle: {}", data[2 * w]);
        // identity warps leave the picture alone; kr0 = 0.5 magnifies a ramp about the centre
        let (w, h) = (9, 7);
        let ramp: Vec<f32> = (0..w * h).flat_map(|i| [(i % w) as f32 / 10.0, (i / w) as f32 / 10.0]).collect();
        let mut data = ramp.clone();
        run1(1, [u(&[1]), d(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5])].concat(), &mut data, w, h, 2).unwrap();
        assert!(data.iter().zip(&ramp).all(|(a, b)| close(*a, *b)));
        let mut data = ramp.clone();
        run1(1, [u(&[1]), d(&[0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5])].concat(), &mut data, w, h, 2).unwrap();
        // x' = 4 + 0.5 (x − 4): the cubic reproduces a linear ramp exactly
        for x in 1..w - 1 {
            let want = (4.0 + 0.5 * (x as f32 - 4.0)) / 10.0;
            assert!(close(data[(3 * w + x) * 2], want), "x {x}: {} vs {want}", data[(3 * w + x) * 2]);
        }
        // one coefficient set per plane: plane 1 untouched
        let mut data = ramp.clone();
        run1(1, [u(&[2]), d(&[0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5])].concat(), &mut data, w, h, 2).unwrap();
        assert!(data.iter().skip(1).step_by(2).zip(ramp.iter().skip(1).step_by(2)).all(|(a, b)| close(*a, *b)));
        // three sets for two planes is malformed
        let mut data = ramp.clone();
        assert!(run1(1, [u(&[3]), vec![0; 3 * 48 + 16]].concat(), &mut data, w, h, 2).is_err());
        // WarpFisheye kr0 = 1: f = atan(r) / r pulls sources towards the centre
        let mut data = ramp.clone();
        run1(2, [u(&[1]), d(&[1.0, 0.0, 0.0, 0.0, 0.5, 0.5])].concat(), &mut data, w, h, 2).unwrap();
        let m = (16.0f64 + 9.0).sqrt();
        let dx = 3.0 / m;
        let want = (4.0 + m * dx.atan()) / 10.0;
        assert!((f64::from(data[(3 * w + 7) * 2]) - want).abs() < 1e-3, "{} vs {want}", data[(3 * w + 7) * 2]);
        // WarpRectilinear2 identity (kr0 = 1), and the skip rule for a following warp
        let wr2 = |flags| (14, flags, [u(&[1]), d(&[1.0]), d(&[0.0; 14]), d(&[0.0, 0.0, 0.0, 1.0, 0.5, 0.5]), u(&[0])].concat());
        let halve = (1, 0, [u(&[1]), d(&[0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5])].concat());
        for (flags, skipped) in [(1, true), (0, false)] {
            let list = parse(&encode(&[wr2(flags), halve.clone()])).unwrap();
            let mut data = ramp.clone();
            let mut img = Image { data: &mut data, width: w, height: h, planes: 2, max: 1.0, mosaic: false, allow_trim: false, origin: (0, 0) };
            apply(&list, &mut img, false).unwrap();
            let unchanged = data.iter().zip(&ramp).all(|(a, b)| close(*a, *b));
            assert_eq!(unchanged, skipped, "optional WarpRectilinear2 {flags}");
        }
        // warps are refused on a mosaic
        let mut data = vec![0.5f32; 16];
        let mut img = Image { data: &mut data, width: 4, height: 4, planes: 1, max: 1.0, mosaic: true, allow_trim: false, origin: (0, 0) };
        assert!(apply(&parse(&encode(&[halve])).unwrap(), &mut img, false).is_err());
    }

    #[test]
    fn bad_pixels_and_trim() {
        // RGGB 6 × 6 with each colour flat; two bad pixels and a bad 2 × 2 rectangle
        let (w, h) = (6, 6);
        let level = [0.2f32, 0.5, 0.5, 0.8];
        let flat: Vec<f32> = (0..w * h).map(|i| level[(i / w % 2) * 2 + i % 2]).collect();
        let mut data = flat.clone();
        data[2 * w + 2] = 0.0;
        data[3 * w + 4] = 0.0;
        // FixBadPixelsConstant 0, phase 0 (red top left), integer stage
        let mut ints: Vec<f32> = data.iter().map(|v| (v * 65_535.0).round()).collect();
        let list = parse(&encode(&[(4, 0, u(&[0, 0]))])).unwrap();
        let mut img = Image { data: &mut ints, width: w, height: h, planes: 1, max: 65_535.0, mosaic: true, allow_trim: false, origin: (0, 0) };
        apply(&list, &mut img, false).unwrap();
        for (a, b) in ints.iter().zip(&flat) {
            assert_eq!(*a, (b * 65_535.0).round());
        }
        // FixBadPixelsList: a point and a rectangle
        let mut data = flat.clone();
        for i in [w + 1, 4 * w + 3, 4 * w + 4, 5 * w + 3, 5 * w + 4] {
            data[i] = 0.9;
        }
        run1(5, u(&[0, 1, 1, 1, 1, 4, 3, 6, 5]), &mut data, w, h, 1).unwrap();
        assert!(data.iter().zip(&flat).all(|(a, b)| close(*a, *b)), "{data:?}");
        // a bad phase is malformed; huge lists are refused before allocating
        let mut data = flat.clone();
        assert!(run1(5, u(&[7, 1, 0, 1, 1]), &mut data, w, h, 1).is_err());
        assert!(run1(5, u(&[0, u32::MAX, u32::MAX]), &mut data, w, h, 1).is_err());
        // TrimBounds
        let mut data: Vec<f32> = (0..w * h * 3).map(|i| i as f32 / 200.0).collect();
        let (tw, th) = run1(6, u(&[1, 2, 4, 5]), &mut data, w, h, 3).unwrap();
        assert_eq!((tw, th, data.len()), (3, 3, 27));
        assert!(close(data[0], ((w + 2) * 3) as f32 / 200.0));
        let mut data = vec![0f32; 4];
        assert!(run1(6, u(&[1, 1, 1, 2]), &mut data, 2, 2, 1).is_err());
    }
}