From c950dacd39ebfe014dff47271d35ca270f858f4d Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Fri, 13 Nov 2026 23:50:23 -0500 Subject: [PATCH 01/25] craft-xlsx: read row heights, column widths and default sizes of a binary sheet BrtRowHdr fills Row's height, custom height, hidden, outline level, collapsed and style (with thickTop, thickBot and ph kept as the XML reader keeps them); BrtColInfo fills SheetPart::columns; BrtWsFmtInfo fills the new typed Features::format (SheetFormat, mirroring sheetFormatPr), which the .xlsx reader now fills too and to_xml writes from the field. A row's ht follows Excel's .xlsx: set by hand, or differing from the default. Column ranges outside the grid are skipped with a warning, column records are capped at 16,384, and heights past 8,192 twips are ignored. Closes #1254. --- crates/xlsx/README.md | 17 +++-- crates/xlsx/src/features.rs | 32 ++++++++- crates/xlsx/src/lib.rs | 4 +- crates/xlsx/src/xlsb.rs | 130 +++++++++++++++++++++++++++++++++--- crates/xlsx/tests/xlsb.rs | 129 ++++++++++++++++++++++++++++++++++- 5 files changed, 291 insertions(+), 21 deletions(-) diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index 1dc30574d..367056ef9 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -36,8 +36,9 @@ were. Part names and relationship ids are kept, so a file read and written witho where it did, and writing it twice gives the same bytes. The calculation chain is dropped (Excel rebuilds it). `XlsxKind` is the workbook part's content type (`.xlsx`, `.xlsm`, `.xltx`, `.xltm`). Typed worksheet features (`SheetPart::features`), each mirroring its element with optional -attributes and the unnamed ones kept: sheet views (panes, selections), print setup (options, -margins, page setup, header and footer, page breaks), data validation (also read from the `x14` +attributes and the unnamed ones kept: sheet views (panes, selections), the default sizes +(`sheetFormatPr`), print setup (options, margins, page setup, header and footer, page breaks), +data validation (also read from the `x14` extension), conditional formats (colour scales, data bars, icon sets, rule extensions kept), hyperlinks (with their URLs), the legacy drawing. Comments (`Comments`, from the comments part) and the note shapes of the VML drawing (`NoteShape`). A sheet that gains comments and has no VML @@ -83,8 +84,11 @@ for new pictures (`vm = i + 1`) and the `XLDAPR` dynamic-array metadata (`cm = 1 The parts this crate writes use the transitional namespaces; a strict file's kept parts stay as they were. A binary workbook is not written. -A binary workbook gives the same `Workbook`, strings, rows and merged cells. Its styles are not -read (each cell keeps its format index). Its cell formulas are decoded from their tokens into the +A binary workbook gives the same `Workbook`, strings, rows (with their heights, outline levels, +hidden flags and styles), columns, default sizes and merged cells. Its styles are not read (each +cell keeps its format index). The descent Excel records for rows and the sheet +(`x14ac:dyDescent`) is in alternate-content blocks, which are skipped. Its cell formulas are +decoded from their tokens into the text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, no implicit `@`), with `FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell metadata index. Every cell of a shared formula gets its own text, decoded at the cell, since the @@ -102,8 +106,9 @@ other workbooks, Python code stored outside the cell, data tables. The XLSB func (40 million records, 4 million cells and as many shared strings, 256 MiB of text, as `craft-xls` allows). `binary_formula_text`: decoded formula text over the workbook (128 MiB); past it, formulas keep their saved results. A formula's tokens are bounded at 16 KB, its - nesting at 256 levels and an array constant at 65,536 values. Fixed caps: 1024 sheets, 100,000 merged ranges per sheet, 64 nested - extension blocks. + nesting at 256 levels and an array constant at 65,536 values. Fixed caps: 1024 sheets, 100,000 + merged ranges and 16,384 column records per sheet, 64 nested extension blocks. A row taller + than 8,192 twips keeps the default height. - Cells and rows outside the 1,048,576 × 16,384 grid are dropped and counted. SpreadsheetML is read leniently: a damaged optional part is a warning, a damaged worksheet keeps diff --git a/crates/xlsx/src/features.rs b/crates/xlsx/src/features.rs index 17c2c2aab..9bae562b5 100644 --- a/crates/xlsx/src/features.rs +++ b/crates/xlsx/src/features.rs @@ -1,6 +1,7 @@ -//! The worksheet's typed features, each mirroring its SpreadsheetML element: sheet views, print -//! setup, data validation, conditional formatting, hyperlinks (ECMA-376 Part 1, 18.3), and the -//! comments part (18.7) with the legacy note shapes Excel draws comments with. +//! The worksheet's typed features, each mirroring its SpreadsheetML element: sheet views, the +//! sheet's default sizes, print setup, data validation, conditional formatting, hyperlinks +//! (ECMA-376 Part 1, 18.3), and the comments part (18.7) with the legacy note shapes Excel draws +//! comments with. //! //! An attribute the file may leave out is an `Option` (`None`: not written, the schema's //! default applies), so a feature reads and writes back as it was. Attributes a type does not @@ -213,6 +214,27 @@ impl SheetView { } } +// ------------------------------------------------------------------------------- sheet format + +element! { + /// ``: the sheet's default column width and row height, and its outline depth. + pub struct SheetFormat { + /// Characters of the default font's maximum digit width, padding excluded (8 when absent). + base_col_width: u32 = "baseColWidth", + /// Characters, padding included, like [`crate::Column::width`]. + default_col_width: f64 = "defaultColWidth", + /// Points. + default_row_height: f64 = "defaultRowHeight", + custom_height: bool = "customHeight", + /// Rows are hidden unless they say otherwise. + zero_height: bool = "zeroHeight", + thick_top: bool = "thickTop", + thick_bottom: bool = "thickBottom", + outline_level_row: u32 = "outlineLevelRow", + outline_level_col: u32 = "outlineLevelCol", + } +} + // ------------------------------------------------------------------------------- printing element! { @@ -643,6 +665,8 @@ impl Hyperlink { #[derive(Clone, Debug, Default, PartialEq)] pub struct Features { pub views: Vec, + /// `sheetFormatPr`. + pub format: Option, pub print: Print, pub validations: Option, pub conditional_formats: Vec, @@ -660,6 +684,7 @@ impl Features { let attrs_of = |n: &str| root.child(n); Features { views: root.child("sheetViews").map(|v| v.children("sheetView").map(SheetView::read).collect()).unwrap_or_default(), + format: attrs_of("sheetFormatPr").map(SheetFormat::read_attrs), print: Print { options: attrs_of("printOptions").map(PrintOptions::read_attrs), margins: attrs_of("pageMargins").map(PageMargins::read_attrs), @@ -708,6 +733,7 @@ impl Features { let p = &self.print; vec![ ("sheetViews", Vec::from_iter((!self.views.is_empty()).then_some(views))), + ("sheetFormatPr", Vec::from_iter(self.format.as_ref().map(|f| el("sheetFormatPr", &|e| f.write_attrs(e))))), ("conditionalFormatting", self.conditional_formats.iter().map(ConditionalFormatting::to_el).collect()), ("dataValidations", Vec::from_iter(validations)), ("hyperlinks", Vec::from_iter((!self.hyperlinks.is_empty()).then_some(links))), diff --git a/crates/xlsx/src/lib.rs b/crates/xlsx/src/lib.rs index 5a64ae0ab..9105ba886 100644 --- a/crates/xlsx/src/lib.rs +++ b/crates/xlsx/src/lib.rs @@ -75,8 +75,8 @@ pub use document::{Document, DrawingPart, KeptPart, PivotCachePart, PivotTablePa pub use drawing::{Anchor, Drawing, Item, Marker, ObjectKind, Placement}; pub use features::{ Break, CfRule, Cfvo, ColorScale, Comment, Comments, ConditionalFormatting, DataBar, DataValidation, DataValidations, Features, HeaderFooter, - Hyperlink, IconSet, NoteShape, PageMargins, PageSetup, Pane, Print, PrintOptions, Selection, SheetView, Table, TableColumn, TableStyleInfo, - notes_vml, parse_sqref, range_ref, + Hyperlink, IconSet, NoteShape, PageMargins, PageSetup, Pane, Print, PrintOptions, Selection, SheetFormat, SheetView, Table, TableColumn, + TableStyleInfo, notes_vml, parse_sqref, range_ref, }; pub use package::{Package, is_xlsb}; pub use pivot::{ diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 2fdde59be..20953cbad 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -1,7 +1,8 @@ -//! Binary workbooks ([MS-XLSB]): the workbook part, shared strings and worksheet cells, from -//! their records. Cell formulas are decoded from their tokens ([`formula`]) into the text an -//! `.xlsx` file holds; one that can't be keeps only its saved result, with a warning saying why. -//! Styles are not read. A record that does not parse fails the read. +//! Binary workbooks ([MS-XLSB]): the workbook part, shared strings and worksheets (rows, cells, +//! columns, default sizes, merged cells), from their records. Cell formulas are decoded from +//! their tokens ([`formula`]) into the text an `.xlsx` file holds; one that can't be keeps only +//! its saved result, with a warning saying why. Styles are not read. A record that does not parse +//! fails the read. mod expr; mod formula; @@ -13,7 +14,8 @@ use std::sync::Arc; use craft_opc::{ContentTypes, Rels}; use craft_sheet::{CellError, CellRef, MAX_COLS, MAX_ROWS, Range, Visibility}; -use crate::sheet::{Cell, CellFormula, CellValue, FormulaKind, Row, SheetPart}; +use crate::features::SheetFormat; +use crate::sheet::{Cell, CellFormula, CellValue, Column, FormulaKind, Row, SheetPart}; use crate::strings::StringItem; use crate::workbook::{SheetEntry, SheetKind, Workbook}; use crate::{Error, Limits, Warnings}; @@ -28,6 +30,10 @@ pub(crate) const STRINGS: &str = "application/vnd.ms-excel.sharedStrings"; const MAX_SHEETS: usize = 1024; /// Most merged ranges in one worksheet. const MAX_MERGES: usize = 100_000; +/// Most column records in one worksheet: one per column. +const MAX_COLUMNS: usize = MAX_COLS as usize; +/// The tallest row, in twips (409.6 points). +const MAX_ROW_TWIPS: u16 = 8192; /// Defined names (Excel's hidden ones for newer functions included) read at most; past it, the /// workbook's formulas keep their saved results. const MAX_NAMES: usize = 65_536; @@ -731,7 +737,87 @@ fn value(id: u16, b: &mut Bytes<'_>, strings: usize, limits: &Limits, budget: &B Ok(value) } -/// A worksheet part: rows (BrtRowHdr) and cells inside BrtBeginSheetData, merged cells after. +/// `BrtRowHdr` after `rw`, up to its column spans: the row's height, flags and style, as the +/// `.xlsx` row Excel writes. `miyRw` is the height of every row: it is `ht` when set by hand +/// (`fUnsynced`) or when it differs from the sheet's default, and past 8192 twips it is ignored. +fn row_header(index: u32, b: &mut Bytes<'_>, default_twips: Option) -> Result { + let ixfe = b.u32()?; + let twips = b.u16()?; + let [borders, flags, phonetic] = [b.u8()?, b.u8()?, b.u8()?]; + let custom = flags & 0x20 != 0; + let height = (twips <= MAX_ROW_TWIPS && (custom || default_twips.is_some_and(|d| d != twips))).then(|| f64::from(twips) / 20.0); + let mut attrs = vec![]; + for (bit, byte, name) in [(1, borders, "thickTop"), (2, borders, "thickBot"), (1, phonetic, "ph")] { + if byte & bit != 0 { + attrs.push((name.to_string(), "1".to_string())); + } + } + Ok(Row { + index, + height, + custom_height: custom && height.is_some(), + hidden: flags & 0x10 != 0, + outline_level: flags & 7, + collapsed: flags & 0x08 != 0, + style: (flags & 0x40 != 0).then_some(ixfe), + cells: vec![], + attrs, + }) +} + +/// `BrtWsFmtInfo`: `sheetFormatPr` as Excel writes it, and the default row height in twips. +/// `dxGCol` is the default width in 256ths of a character (`0xFFFFFFFF`: none set); +/// `cchDefColWidth` is the base width, left out at its default of 8. +fn sheet_format(b: &mut Bytes<'_>) -> Result<(SheetFormat, u16), Error> { + let width = b.u32()?; + let base = b.u16()?; + let twips = b.u16()?; + let flags = b.u16()?; + let rows = b.u8()?; + let cols = b.u8()?; + let set = |bit: u16| (flags & bit != 0).then_some(true); + let level = |n: u8| (n > 0).then_some(u32::from(n.min(7))); + let format = SheetFormat { + base_col_width: (base != 8).then_some(u32::from(base)), + default_col_width: (width != u32::MAX).then(|| f64::from(width) / 256.0), + default_row_height: Some(f64::from(twips) / 20.0), + custom_height: set(1), + zero_height: set(2), + thick_top: set(4), + thick_bottom: set(8), + outline_level_row: level(rows), + outline_level_col: level(cols), + attrs: vec![], + }; + Ok((format, twips)) +} + +/// `BrtColInfo`: `None` for a range outside the grid or backwards. `coldx` is the width in 256ths +/// of a character, which is the `.xlsx` width exactly. +fn column(b: &mut Bytes<'_>) -> Result, Error> { + let first = b.u32()?; + let last = b.u32()?; + let width = b.u32()?; + let style = b.u32()?; + let flags = b.u16()?; + if first > last || last >= MAX_COLS { + return Ok(None); + } + Ok(Some(Column { + first, + last, + width: Some(f64::from(width) / 256.0), + custom_width: flags & 2 != 0, + best_fit: flags & 4 != 0, + hidden: flags & 1 != 0, + outline_level: ((flags >> 8) & 7) as u8, + collapsed: flags & 0x1000 != 0, + style: (style != 0).then_some(style), + })) +} + +/// A worksheet part: the sheet's default sizes (BrtWsFmtInfo) and columns (BrtColInfo) before +/// BrtBeginSheetData, rows (BrtRowHdr) and cells inside it, merged cells after. /// Formulas refer to the workbook's `links`; with none, each keeps its saved result. #[allow(clippy::too_many_arguments)] pub(crate) fn worksheet( @@ -750,6 +836,9 @@ pub(crate) fn worksheet( let mut data_seen = false; let mut row: Option = None; let mut col = None; + // The default row height in twips: a row of another height has `ht` even when it was not set + // by hand (a large font, a thick border), as Excel writes it in `.xlsx`. + let mut default_twips: Option = None; // BrtCellMeta applies to the cell record that follows it (a dynamic-array formula), as `cm` // does in `.xlsx`: a one-based index into the metadata part's cell metadata. Excel may write // the cell's BrtValueMeta (a rich value, such as a spill error) in between. @@ -818,15 +907,14 @@ pub(crate) fn worksheet( if r >= MAX_ROWS || row.as_ref().is_some_and(|previous| r <= previous.index) { return Err(invalid("row is out of bounds or unordered")); } - // ixfe, miyRw and the flags are not read. - b.take(9)?; + let next = row_header(r, &mut b, default_twips)?; let spans = b.u32()?; if spans > 16 { return Err(invalid("too many row spans")); } b.take(spans as usize * 8)?; b.finish()?; - if let Some(previous) = row.replace(Row { index: r, ..Row::default() }) { + if let Some(previous) = row.replace(next) { each(previous)?; } sheet.rows += 1; @@ -885,6 +973,30 @@ pub(crate) fn worksheet( }); col = Some(c); } + 485 => { + if data_seen { + return Err(invalid("sheet format after worksheet data")); + } + let (format, twips) = sheet_format(&mut b)?; + b.finish()?; + default_twips = Some(twips); + sheet.features.format = Some(format); + } + 60 => { + if data_seen { + return Err(invalid("column after worksheet data")); + } + let column = column(&mut b)?; + b.finish()?; + let Some(column) = column else { + warnings.warn("invalid XLSB column ranges were skipped"); + continue; + }; + if sheet.columns.len() >= MAX_COLUMNS { + return Err(Error::TooLarge(format!("XLSB exceeds {MAX_COLUMNS} column records"))); + } + sheet.columns.push(column); + } 176 => { if data { return Err(invalid("merge inside worksheet data")); diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index ee9ac3afd..0cf2c5c14 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -5,7 +5,7 @@ mod common; use common::{read, read_with}; use craft_xlsx::testing::zip; -use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Range, SheetKind, Value, Visibility, is_xlsb}; +use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Package, Range, Row, SheetKind, SheetPart, Value, Visibility, is_xlsb}; const ROOT: &str = r#""#; const RELS: &str = r#""#; @@ -930,3 +930,130 @@ fn a_dynamic_array_kept_as_its_value_has_no_cell_metadata() { assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&at("C1"), &1)]); assert!(warned(&wb, "missing from the file"), "{:?}", wb.warnings); } + +/// `BrtRowHdr` with its style, height and three flag bytes, and no column spans. +fn row_hdr(r: u32, ixfe: u32, twips: u16, flags: [u8; 3]) -> Vec { + record(0, &[&r.to_le_bytes()[..], &ixfe.to_le_bytes(), &twips.to_le_bytes(), &flags, &0u32.to_le_bytes()].concat()) +} +/// `BrtColInfo`. +fn col_info(first: u32, last: u32, coldx: u32, ixfe: u32, flags: u16) -> Vec { + record(60, &[&first.to_le_bytes()[..], &last.to_le_bytes(), &coldx.to_le_bytes(), &ixfe.to_le_bytes(), &flags.to_le_bytes()].concat()) +} +/// `BrtWsFmtInfo`. +fn ws_fmt_info(dx: u32, base: u16, twips: u16, flags: u16, levels: [u8; 2]) -> Vec { + record(485, &[&dx.to_le_bytes()[..], &base.to_le_bytes(), &twips.to_le_bytes(), &flags.to_le_bytes(), &levels].concat()) +} +/// A worksheet with `head` before its data (the sheet format and columns) and `rows` in it. +fn sized_sheet(head: &[u8], rows: &[u8]) -> Vec { + [&record(129, &[])[..], head, &record(145, &[]), rows, &record(146, &[]), &record(130, &[])].concat() +} +fn sizes_of(bytes: &[u8]) -> Result<(Vec, SheetPart, Vec), Error> { + let mut p = Package::open(bytes, &Limits::default())?; + let wb = p.workbook()?; + let mut rows = vec![]; + let part = p.sheet(&wb.sheets[0], |r| { + rows.push(r); + Ok(()) + })?; + Ok((rows, part, p.warnings().to_vec())) +} +fn xml_sheet(xml: &str) -> (Vec, SheetPart) { + let types = r#""#; + let root = r#""#; + let rels = r#""#; + let book = r#""#; + let bytes = zip(&[ + ("[Content_Types].xml", types.as_bytes()), + ("_rels/.rels", root.as_bytes()), + ("xl/workbook.xml", book.as_bytes()), + ("xl/_rels/workbook.xml.rels", rels.as_bytes()), + ("xl/sheet.xml", xml.as_bytes()), + ]); + let (rows, part, _) = sizes_of(&bytes).unwrap(); + (rows, part) +} + +/// The binary sheet below as Excel spells it in `.xlsx`. Excel writes `ht` for a row whose +/// height differs from the default even when it was not set by hand. +const SIZES_XML: &str = r#"1"#; + +fn sizes_bin() -> Vec { + let head = [ + ws_fmt_info(3225, 10, 285, 0x0A, [2, 1]), + record(390, &[]), + col_info(2, 2, 5273, 0, 0x0002), + col_info(3, 5, 0, 0, 0x0103), + col_info(7, 7, 2321, 7, 0), + col_info(8, 8, 6075, 0, 0x0006), + col_info(9, 9, 2321, 0, 0x1000), + col_info(16_383, 16_383, 7833, 0, 0x0002), + record(391, &[]), + ] + .concat(); + let rows = [ + row_hdr(0, 0, 285, [0, 0, 0]), + cell(2, 0, &((1u32 << 2) | 2).to_le_bytes()), + row_hdr(1, 0, 600, [0, 0x20, 0]), + row_hdr(2, 0, 533, [0, 0, 0]), + row_hdr(3, 0, 285, [0, 0x10, 0]), + row_hdr(4, 0, 300, [3, 0, 0]), + row_hdr(5, 0, 285, [0, 2, 0]), + row_hdr(6, 0, 285, [0, 0x11, 0]), + row_hdr(7, 0, 285, [0, 0x08, 0]), + row_hdr(8, 3, 285, [0, 0x40, 0]), + // A style without fGhostDirty is not applied. + row_hdr(9, 5, 285, [0, 0, 0]), + row_hdr(10, 0, 285, [0, 0, 1]), + // Past 8192 twips the height is ignored, set by hand or not. + row_hdr(11, 0, 9000, [0, 0x20, 0]), + ] + .concat(); + sized_sheet(&head, &rows) +} + +#[test] +fn row_heights_and_column_widths_are_read_as_their_xlsx_spelling() { + let (rows, part, _) = sizes_of(&file(&sizes_bin())).unwrap(); + let (xml_rows, xml_part) = xml_sheet(SIZES_XML); + assert_eq!(rows, xml_rows); + assert_eq!(part.columns, xml_part.columns); + assert_eq!(part.features.format, xml_part.features.format); + // A few of the conversions, spelled out. + assert_eq!((rows[1].height, rows[1].custom_height), (Some(30.0), true)); + assert_eq!((rows[2].height, rows[2].custom_height), (Some(26.65), false)); + assert_eq!(rows[8].style, Some(3)); + assert_eq!(part.columns[0].width, Some(5273.0 / 256.0)); + let format = part.features.format.clone().unwrap(); + assert_eq!((format.default_col_width, format.default_row_height), (Some(12.59765625), Some(14.25))); + // Written as SpreadsheetML, the binary sheet has its default sizes and columns. + let written = String::from_utf8(part.to_xml(&rows)).unwrap(); + assert!(written.contains(r#""#), "{written}"); + assert!(written.contains(r#""#), "{written}"); +} + +#[test] +fn damaged_sizes_are_skipped_or_refused() { + // A column range outside the grid, or backwards, is skipped with a warning. + let head = [record(390, &[]), col_info(0, 16_384, 2321, 0, 0), col_info(5, 4, 2321, 0, 0), col_info(1, 1, 512, 0, 2), record(391, &[])].concat(); + let (_, part, warnings) = sizes_of(&file(&sized_sheet(&head, &row(0)))).unwrap(); + assert_eq!(part.columns.len(), 1); + assert_eq!((part.columns[0].first, part.columns[0].width), (1, Some(2.0))); + assert!(warnings.iter().any(|w| w.contains("column ranges")), "{warnings:?}"); + // Without a sheet format, only a height set by hand is a row's `ht`. + let (rows, _, _) = sizes_of(&file(&sized_sheet(&[], &[row_hdr(0, 0, 300, [0, 0, 0]), row_hdr(1, 0, 400, [0, 0x20, 0])].concat()))).unwrap(); + assert_eq!((rows[0].height, rows[1].height), (None, Some(20.0))); + // More column records than columns are refused. + let many = [&record(390, &[])[..], &col_info(0, 0, 2321, 0, 0).repeat(16_385), &record(391, &[])].concat(); + assert!(matches!(sizes_of(&file(&sized_sheet(&many, &[]))), Err(Error::TooLarge(_)))); + // A sheet format or column after the data, or a record of the wrong size, fails the read. + for (why, sheet) in [ + ("format after data", worksheet(&[], &ws_fmt_info(u32::MAX, 8, 285, 0, [0, 0]))), + ("column after data", worksheet(&[], &col_info(0, 0, 2321, 0, 0))), + ("short column", sized_sheet(&record(60, &[0; 16]), &[])), + ("short format", sized_sheet(&record(485, &[0; 11]), &[])), + ("long row header", sized_sheet(&[], &record(0, &[0; 18]))), + ] { + assert!(matches!(sizes_of(&file(&sheet)), Err(Error::Invalid(_))), "{why}"); + } +} From eca1698ef11bc0a523dea6da95cdc613b11a405b Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 12:38:58 -0500 Subject: [PATCH 02/25] craft-xlsx: review fix: skipped column records count toward the column cap A sheet of 16,385 reversed or out-of-grid BrtColInfo ranges was skipped one by one without reaching the cap, since only kept columns were counted. Every column record now counts. --- crates/xlsx/src/xlsb.rs | 9 ++++++--- crates/xlsx/tests/xlsb.rs | 3 +++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index fdda0d5cb..73755c3dc 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -840,6 +840,8 @@ pub(crate) fn worksheet( let mut sheet = SheetPart::default(); let mut data = false; let mut data_seen = false; + // Every BrtColInfo counts toward the cap, a skipped one too. + let mut column_records = 0usize; let mut row: Option = None; let mut col = None; // The default row height in twips: a row of another height has `ht` even when it was not set @@ -992,15 +994,16 @@ pub(crate) fn worksheet( if data_seen { return Err(invalid("column after worksheet data")); } + if column_records >= MAX_COLUMNS { + return Err(Error::TooLarge(format!("XLSB exceeds {MAX_COLUMNS} column records"))); + } + column_records += 1; let column = column(&mut b)?; b.finish()?; let Some(column) = column else { warnings.warn("invalid XLSB column ranges were skipped"); continue; }; - if sheet.columns.len() >= MAX_COLUMNS { - return Err(Error::TooLarge(format!("XLSB exceeds {MAX_COLUMNS} column records"))); - } sheet.columns.push(column); } 176 => { diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index d1e7feafc..4f6886b26 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1046,6 +1046,9 @@ fn damaged_sizes_are_skipped_or_refused() { // More column records than columns are refused. let many = [&record(390, &[])[..], &col_info(0, 0, 2321, 0, 0).repeat(16_385), &record(391, &[])].concat(); assert!(matches!(sizes_of(&file(&sized_sheet(&many, &[]))), Err(Error::TooLarge(_)))); + // Skipped (reversed) column records count toward that cap too. + let reversed = [&record(390, &[])[..], &col_info(5, 4, 2321, 0, 0).repeat(16_385), &record(391, &[])].concat(); + assert!(matches!(sizes_of(&file(&sized_sheet(&reversed, &[]))), Err(Error::TooLarge(_)))); // A sheet format or column after the data, or a record of the wrong size, fails the read. for (why, sheet) in [ ("format after data", worksheet(&[], &ws_fmt_info(u32::MAX, 8, 285, 0, [0, 0]))), From 90b6cbef8a8703d561440be340920fd3677d886d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 13:13:48 -0800 Subject: [PATCH 03/25] craft-exr: deep chunk offsets must follow every part's offset table In a multipart file the chunks start after the offset tables of all parts. The deep readers in craft-exr and craft-image-codecs took the end of the deep part's own table as the start of the chunk area, so an offset into a later part's table passed the chunk-area check and its bytes were parsed as a chunk. Both readers now compute where the deep part's table starts and where the last table ends with checked arithmetic, and refuse a file whose tables run past its end before reading any offsets. The earlier tables' sizes are no longer capped or saturated. Tests build a two-part file from scratch (a deep part and a 1x1 flat part, in both orders): the deep part decodes as in a single-part file, an offset into the second table or one byte before the end of the tables is refused, and a file that ends inside the second table is refused. --- crates/exr/src/deep.rs | 29 ++++-- crates/exr/src/deep_tests.rs | 100 +++++++++++++++++++++ crates/image-codecs/src/codecs/deep_exr.rs | 29 ++++-- crates/image-codecs/tests/it/deep_exr.rs | 100 +++++++++++++++++++++ 4 files changed, 244 insertions(+), 14 deletions(-) diff --git a/crates/exr/src/deep.rs b/crates/exr/src/deep.rs index b86d497c8..ad2ab00b5 100644 --- a/crates/exr/src/deep.rs +++ b/crates/exr/src/deep.rs @@ -391,19 +391,14 @@ fn decode_deep_meta(meta: &MetaData, bytes: &[u8], limits: &Limits) -> Result Result { Ok(cur.position()) } +/// Where part `header_index`'s offset table starts and where the last part's table ends. The +/// tables follow the headers, `chunk_count` u64 per header in header order, and the chunks +/// follow the last table. Sizes that overflow, or tables that run past the end of the file, +/// are errors. +fn offset_tables(meta: &MetaData, bytes: &[u8], header_index: usize) -> Result<(u64, u64)> { + let mut table_pos = header_end(bytes)?; + let mut tables_end = table_pos; + for (i, h) in meta.headers.iter().enumerate() { + let len = u64::try_from(h.chunk_count).ok().and_then(|n| n.checked_mul(8)); + tables_end = len.and_then(|len| tables_end.checked_add(len)).ok_or_else(|| err("the offset tables are too large"))?; + if i < header_index { + table_pos = tables_end; + } + } + if tables_end > bytes.len() as u64 { + return Err(err(format!("the offset tables end at byte {tables_end}, past the end of the file"))); + } + Ok((table_pos, tables_end)) +} + /// Composite the deep samples into a flat image: per pixel, samples sorted by Z (stable; the /// file is assumed messy), front-to-back `over` with premultiplied colour, un-premultiplied /// into straight alpha at the end. Without any alpha channel the front-most sample wins, diff --git a/crates/exr/src/deep_tests.rs b/crates/exr/src/deep_tests.rs index cb1721809..bdbfa0d68 100644 --- a/crates/exr/src/deep_tests.rs +++ b/crates/exr/src/deep_tests.rs @@ -886,6 +886,106 @@ fn inconsistent_count_table_errors() { assert!(matches!(decode_deep_exr(&bytes, &Limits::none()), Err(CodecError::Malformed(_)))); } +/// A two-part file made from the single-part deep file `deep` and a 1×1 flat scan-line part, +/// the deep part first when `deep_first`: each header gains `name` and `chunkCount`, the +/// version field the multipart bit, and each chunk its part number. The offset tables follow +/// the headers in header order, then the chunks. Returns the file, where the first table +/// starts, where the second table starts and where the last table ends. +fn two_part(deep: &[u8], deep_first: bool) -> (Vec, usize, usize, usize) { + let end = header_end_of(deep); + let first = u64::from_le_bytes(deep[end..end + 8].try_into().expect("8")) as usize; + let offsets: Vec = + (0..(first - end) / 8).map(|i| u64::from_le_bytes(deep[end + 8 * i..end + 8 * i + 8].try_into().expect("8")) as usize).collect(); + let deep_chunks: Vec<&[u8]> = offsets.iter().enumerate().map(|(i, &o)| &deep[o..offsets.get(i + 1).copied().unwrap_or(deep.len())]).collect(); + + let mut deep_header = deep[8..end - 1].to_vec(); + attr(&mut deep_header, "name", "string", b"deep"); + attr(&mut deep_header, "chunkCount", "int", &(deep_chunks.len() as i32).to_le_bytes()); + let mut flat_header = Vec::new(); + let mut chlist = b"R\0".to_vec(); + chlist.extend_from_slice(&1i32.to_le_bytes()); // half + chlist.extend_from_slice(&[0u8; 4]); // pLinear, reserved + chlist.extend_from_slice(&1i32.to_le_bytes()); + chlist.extend_from_slice(&1i32.to_le_bytes()); + chlist.push(0); + attr(&mut flat_header, "channels", "chlist", &chlist); + attr(&mut flat_header, "compression", "compression", &[0]); + let dw: Vec = [0i32; 4].iter().flat_map(|v| v.to_le_bytes()).collect(); + attr(&mut flat_header, "dataWindow", "box2i", &dw); + attr(&mut flat_header, "displayWindow", "box2i", &dw); + attr(&mut flat_header, "lineOrder", "lineOrder", &[0]); + attr(&mut flat_header, "pixelAspectRatio", "float", &1f32.to_le_bytes()); + attr(&mut flat_header, "screenWindowCenter", "v2f", &[0f32.to_le_bytes(), 0f32.to_le_bytes()].concat()); + attr(&mut flat_header, "screenWindowWidth", "float", &1f32.to_le_bytes()); + attr(&mut flat_header, "name", "string", b"flat"); + attr(&mut flat_header, "type", "string", b"scanlineimage"); + attr(&mut flat_header, "chunkCount", "int", &1i32.to_le_bytes()); + // The flat part's one chunk: y, the data size, one half sample. + let mut flat_chunk = 0i32.to_le_bytes().to_vec(); + flat_chunk.extend_from_slice(&2i32.to_le_bytes()); + flat_chunk.extend_from_slice(&f16::from_f32(0.5).to_le_bytes()); + let flat_chunks: Vec<&[u8]> = vec![&flat_chunk]; + + let parts: [(&[u8], &[&[u8]]); 2] = if deep_first { + [(&deep_header, &deep_chunks), (&flat_header, &flat_chunks)] + } else { + [(&flat_header, &flat_chunks), (&deep_header, &deep_chunks)] + }; + let mut out = deep[..4].to_vec(); + out.extend_from_slice(&(2u32 | 0x800 | 0x1000).to_le_bytes()); + for (header, _) in &parts { + out.extend_from_slice(header); + out.push(0); // end of this header + } + out.push(0); // end of the headers + let first_table = out.len(); + let second_table = first_table + parts[0].1.len() * 8; + let tables_end = second_table + parts[1].1.len() * 8; + let mut pos = tables_end; + for (_, chunks) in &parts { + for c in chunks.iter() { + out.extend_from_slice(&(pos as u64).to_le_bytes()); + pos += 4 + c.len(); + } + } + for (part, (_, chunks)) in parts.iter().enumerate() { + for c in chunks.iter() { + out.extend_from_slice(&(part as i32).to_le_bytes()); + out.extend_from_slice(c); + } + } + (out, first_table, second_table, tables_end) +} + +/// The deep part of a two-part file decodes as the single-part file does, first or second. +/// Its chunks start after every part's offset table: a chunk offset into the other part's +/// table, or one byte before the end of the last table, is refused as table data, and a file +/// that ends inside the last table is refused even when the deep part's own table is whole. +#[test] +fn multipart_deep_chunks_start_after_every_offset_table() { + let single = gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba); + let want = decode_deep_exr(&single, &Limits::none()).expect("single part"); + for deep_first in [true, false] { + let (bytes, ..) = two_part(&single, deep_first); + let deep = decode_deep_exr(&bytes, &Limits::none()).expect("two parts"); + assert_eq!(deep.counts, want.counts); + assert_eq!(deep.channel("Z").expect("Z").samples, want.channel("Z").expect("Z").samples); + } + + let refused = |bytes: &[u8], what: &str| match decode_deep_exr(bytes, &Limits::none()) { + Err(e @ CodecError::Malformed(_)) => assert!(e.to_string().contains(what), "{e}"), + Err(e) => panic!("{e}, want an error about {what}"), + Ok(_) => panic!("decoded, want an error about {what}"), + }; + let (bytes, first_table, second_table, tables_end) = two_part(&single, true); + for off in [second_table, tables_end - 1] { + let mut into_table = bytes.clone(); + into_table[first_table..first_table + 8].copy_from_slice(&(off as u64).to_le_bytes()); + refused(&into_table, "outside the chunk area"); + } + refused(&bytes[..tables_end - 4], "past the end of the file"); +} + /// `gen_deep` for a cropped render: the data window sits at `(dwx, dwy)` inside a larger /// display window of `sw × sh` (the stage the renderer composed onto). Scanline chunk Y /// coordinates are absolute display rows, so they carry the data-window offset. diff --git a/crates/image-codecs/src/codecs/deep_exr.rs b/crates/image-codecs/src/codecs/deep_exr.rs index 0cc69e261..634737816 100644 --- a/crates/image-codecs/src/codecs/deep_exr.rs +++ b/crates/image-codecs/src/codecs/deep_exr.rs @@ -304,19 +304,14 @@ pub(crate) fn decode_deep(meta: &MetaData, bytes: &[u8], limits: &Limits) -> Res let bytes_per_sample: usize = exr_types.iter().map(|t| sample_bytes(*t)).sum(); let multipart = meta.requirements.has_multiple_layers; - // The offset tables follow the headers: `chunk_count` u64 per header, in header order. - // Our part's table starts after the tables of all earlier parts. - let mut table_pos = header_end(bytes)?; - for h_ in meta.headers.iter().take(header_index) { - table_pos = table_pos.saturating_add((h_.chunk_count.min(1 << 24) as u64).saturating_mul(8)); - } + // A chunk offset before the end of the last offset table points into table data. + let (table_pos, tables_end) = offset_tables(meta, bytes, header_index)?; let mut cur = Cursor::new(bytes); seek_to(&mut cur, table_pos)?; let mut offsets = Vec::with_capacity(header.chunk_count.min(1 << 20)); for _ in 0..header.chunk_count { offsets.push(u64(&mut cur)?); } - let tables_end = cur.position(); // Block geometry. let (tiles_x, tile_w, tile_h, slots) = match &header.blocks { @@ -565,6 +560,26 @@ fn header_end(bytes: &[u8]) -> Result { Ok(cur.position()) } +/// Where part `header_index`'s offset table starts and where the last part's table ends. The +/// tables follow the headers, `chunk_count` u64 per header in header order, and the chunks +/// follow the last table. Sizes that overflow, or tables that run past the end of the file, +/// are errors. +fn offset_tables(meta: &MetaData, bytes: &[u8], header_index: usize) -> Result<(u64, u64), CodecError> { + let mut table_pos = header_end(bytes)?; + let mut tables_end = table_pos; + for (i, h) in meta.headers.iter().enumerate() { + let len = u64::try_from(h.chunk_count).ok().and_then(|n| n.checked_mul(8)); + tables_end = len.and_then(|len| tables_end.checked_add(len)).ok_or_else(|| err("the offset tables are too large"))?; + if i < header_index { + table_pos = tables_end; + } + } + if tables_end > bytes.len() as u64 { + return Err(err(format!("the offset tables end at byte {tables_end}, past the end of the file"))); + } + Ok((table_pos, tables_end)) +} + /// Composite the deep samples into a flat image: per pixel, samples sorted by Z (stable; the /// file is assumed messy), front-to-back `over` with premultiplied colour, un-premultiplied /// into straight alpha at the end. Without any alpha channel the front-most sample wins, diff --git a/crates/image-codecs/tests/it/deep_exr.rs b/crates/image-codecs/tests/it/deep_exr.rs index e614f865d..2e1105634 100644 --- a/crates/image-codecs/tests/it/deep_exr.rs +++ b/crates/image-codecs/tests/it/deep_exr.rs @@ -799,6 +799,106 @@ fn inconsistent_count_table_errors() { assert!(matches!(decode_deep_exr(&bytes, &Limits::none()), Err(CodecError::Malformed { .. }))); } +/// A two-part file made from the single-part deep file `deep` and a 1×1 flat scan-line part, +/// the deep part first when `deep_first`: each header gains `name` and `chunkCount`, the +/// version field the multipart bit, and each chunk its part number. The offset tables follow +/// the headers in header order, then the chunks. Returns the file, where the first table +/// starts, where the second table starts and where the last table ends. +fn two_part(deep: &[u8], deep_first: bool) -> (Vec, usize, usize, usize) { + let end = header_end_of(deep); + let first = u64::from_le_bytes(deep[end..end + 8].try_into().expect("8")) as usize; + let offsets: Vec = + (0..(first - end) / 8).map(|i| u64::from_le_bytes(deep[end + 8 * i..end + 8 * i + 8].try_into().expect("8")) as usize).collect(); + let deep_chunks: Vec<&[u8]> = offsets.iter().enumerate().map(|(i, &o)| &deep[o..offsets.get(i + 1).copied().unwrap_or(deep.len())]).collect(); + + let mut deep_header = deep[8..end - 1].to_vec(); + attr(&mut deep_header, "name", "string", b"deep"); + attr(&mut deep_header, "chunkCount", "int", &(deep_chunks.len() as i32).to_le_bytes()); + let mut flat_header = Vec::new(); + let mut chlist = b"R\0".to_vec(); + chlist.extend_from_slice(&1i32.to_le_bytes()); // half + chlist.extend_from_slice(&[0u8; 4]); // pLinear, reserved + chlist.extend_from_slice(&1i32.to_le_bytes()); + chlist.extend_from_slice(&1i32.to_le_bytes()); + chlist.push(0); + attr(&mut flat_header, "channels", "chlist", &chlist); + attr(&mut flat_header, "compression", "compression", &[0]); + let dw: Vec = [0i32; 4].iter().flat_map(|v| v.to_le_bytes()).collect(); + attr(&mut flat_header, "dataWindow", "box2i", &dw); + attr(&mut flat_header, "displayWindow", "box2i", &dw); + attr(&mut flat_header, "lineOrder", "lineOrder", &[0]); + attr(&mut flat_header, "pixelAspectRatio", "float", &1f32.to_le_bytes()); + attr(&mut flat_header, "screenWindowCenter", "v2f", &[0f32.to_le_bytes(), 0f32.to_le_bytes()].concat()); + attr(&mut flat_header, "screenWindowWidth", "float", &1f32.to_le_bytes()); + attr(&mut flat_header, "name", "string", b"flat"); + attr(&mut flat_header, "type", "string", b"scanlineimage"); + attr(&mut flat_header, "chunkCount", "int", &1i32.to_le_bytes()); + // The flat part's one chunk: y, the data size, one half sample. + let mut flat_chunk = 0i32.to_le_bytes().to_vec(); + flat_chunk.extend_from_slice(&2i32.to_le_bytes()); + flat_chunk.extend_from_slice(&f16::from_f32(0.5).to_le_bytes()); + let flat_chunks: Vec<&[u8]> = vec![&flat_chunk]; + + let parts: [(&[u8], &[&[u8]]); 2] = if deep_first { + [(&deep_header, &deep_chunks), (&flat_header, &flat_chunks)] + } else { + [(&flat_header, &flat_chunks), (&deep_header, &deep_chunks)] + }; + let mut out = deep[..4].to_vec(); + out.extend_from_slice(&(2u32 | 0x800 | 0x1000).to_le_bytes()); + for (header, _) in &parts { + out.extend_from_slice(header); + out.push(0); // end of this header + } + out.push(0); // end of the headers + let first_table = out.len(); + let second_table = first_table + parts[0].1.len() * 8; + let tables_end = second_table + parts[1].1.len() * 8; + let mut pos = tables_end; + for (_, chunks) in &parts { + for c in chunks.iter() { + out.extend_from_slice(&(pos as u64).to_le_bytes()); + pos += 4 + c.len(); + } + } + for (part, (_, chunks)) in parts.iter().enumerate() { + for c in chunks.iter() { + out.extend_from_slice(&(part as i32).to_le_bytes()); + out.extend_from_slice(c); + } + } + (out, first_table, second_table, tables_end) +} + +/// The deep part of a two-part file decodes as the single-part file does, first or second. +/// Its chunks start after every part's offset table: a chunk offset into the other part's +/// table, or one byte before the end of the last table, is refused as table data, and a file +/// that ends inside the last table is refused even when the deep part's own table is whole. +#[test] +fn multipart_deep_chunks_start_after_every_offset_table() { + let single = gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba); + let want = decode_deep_exr(&single, &Limits::none()).expect("single part"); + for deep_first in [true, false] { + let (bytes, ..) = two_part(&single, deep_first); + let deep = decode_deep_exr(&bytes, &Limits::none()).expect("two parts"); + assert_eq!(deep.counts, want.counts); + assert_eq!(deep.channel("Z").expect("Z").samples, want.channel("Z").expect("Z").samples); + } + + let refused = |bytes: &[u8], what: &str| match decode_deep_exr(bytes, &Limits::none()) { + Err(CodecError::Malformed { message, .. }) => assert!(message.contains(what), "{message}"), + Err(e) => panic!("{e}, want an error about {what}"), + Ok(_) => panic!("decoded, want an error about {what}"), + }; + let (bytes, first_table, second_table, tables_end) = two_part(&single, true); + for off in [second_table, tables_end - 1] { + let mut into_table = bytes.clone(); + into_table[first_table..first_table + 8].copy_from_slice(&(off as u64).to_le_bytes()); + refused(&into_table, "outside the chunk area"); + } + refused(&bytes[..tables_end - 4], "past the end of the file"); +} + // --------------------------------------------------------------------------------------------- // Real reference files: the deep test images of the OpenEXR repository at commit // `69b2604fc76e370615438bdc8d2cd95b9349c12e`, read from `corpus/exr/` at the repo root with the From cb3f76848bcbfcc889fa501a6908399d1d463a01 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 11 Oct 2026 16:49:47 +0200 Subject: [PATCH 04/25] #1065: DNG opcode lists (OpcodeList1/2/3) Problem: DNG opcode lists were ignored, so lens shading gain maps (phones and many cameras), bad pixel maps, vignette and distortion corrections and trims that a file asks for were silently left out, and an opcode a reader must apply could not be refused. Fix (DNG 1.7.1.0 chapter 7): a big-endian opcode list parser (at most 256 opcodes, every count checked against the parameter bytes before allocating) and the opcodes WarpRectilinear, WarpFisheye, WarpRectilinear2 (with the skip rule), FixVignetteRadial, FixBadPixelsConstant / List, TrimBounds, MapTable, MapPolynomial, GainMap, DeltaPerRow / Column and ScalePerRow / Column. OpcodeList1 runs on the stored samples (0-65535), OpcodeList2 on the linear values before white balance (the fused normalise splits only then), OpcodeList3 on the demosaiced camera values; values are clipped after each opcode. Unknown opcodes or ones newer than 1.7.1.0 are skipped when optional and an error naming them when mandatory; preview-only opcodes are skipped on the reduced-resolution path, and a mandatory OpcodeList3 keeps full resolution. Interpretations where the spec leaves room are recorded in the README. Files without opcode lists develop exactly as before. Tests: unit tests per opcode with known results (tables, polynomials, deltas, scales, gain map interpolation and edge replication, vignette, warps reproducing a ramp, fisheye, the skip rule, bad pixels, trim, hostile parameters) and whole-file tests of each list's stage, the optional flag, the preview path, and 3000 mutations under catch_unwind. Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/opcode_params.rs | 884 ++++++++++++++++++++++++++++++++ 1 file changed, 884 insertions(+) create mode 100644 crates/raw/src/opcode_params.rs diff --git a/crates/raw/src/opcode_params.rs b/crates/raw/src/opcode_params.rs new file mode 100644 index 000000000..bb05c650a --- /dev/null +++ b/crates/raw/src/opcode_params.rs @@ -0,0 +1,884 @@ +//! 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; + +const OPTIONAL: u32 = 1; +const PREVIEW_SKIPPABLE: u32 = 2; + +/// One opcode of a list. +#[derive(Clone, Debug, PartialEq)] +pub struct Opcode { + pub id: u32, + /// The DNG version that defined it, as four bytes in a big-endian u32 (1.3.0.0 = 0x01030000). + pub version: u32, + pub flags: u32, + pub params: Vec, +} + +impl Opcode { + pub fn optional(&self) -> bool { + self.flags & OPTIONAL != 0 + } + + pub fn preview_skippable(&self) -> bool { + self.flags & PREVIEW_SKIPPABLE != 0 + } +} + +/// The opcode lists of a raw IFD. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Lists { + pub list1: Vec, + pub list2: Vec, + pub list3: Vec, +} + +/// Parse an opcode list (big-endian). +pub fn parse(b: &[u8]) -> Result> { + let bad = || Error::Malformed("opcode list"); + let mut r = Reader::new(b); + let count = r.u32().map_err(|_| bad())? as usize; + // every opcode takes at least 16 bytes + if count > b.len() / 16 || count > MAX_OPCODES { + return Err(bad()); + } + let mut out = Vec::with_capacity(count); + for _ in 0..count { + let id = r.u32().map_err(|_| bad())?; + let version = r.u32().map_err(|_| bad())?; + let flags = r.u32().map_err(|_| bad())?; + let len = r.u32().map_err(|_| bad())? as usize; + let params = r.bytes(len).map_err(|_| bad())?.to_vec(); + out.push(Opcode { id, version, flags, params }); + } + Ok(out) +} + +/// Big-endian parameter reader. +struct Reader<'a> { + b: &'a [u8], + pos: usize, +} + +impl<'a> Reader<'a> { + fn new(b: &'a [u8]) -> Self { + Reader { b, pos: 0 } + } + + fn bytes(&mut self, n: usize) -> Result<&'a [u8]> { + let end = self.pos.checked_add(n).ok_or(Error::Malformed("opcode parameters"))?; + let s = self.b.get(self.pos..end).ok_or(Error::Malformed("opcode parameters too short"))?; + self.pos = end; + Ok(s) + } + + fn u32(&mut self) -> Result { + let s = self.bytes(4)?; + Ok(u32::from_be_bytes([s[0], s[1], s[2], s[3]])) + } + + fn f32(&mut self) -> Result { + let v = f32::from_bits(self.u32()?); + if v.is_finite() { Ok(v) } else { Err(Error::Malformed("non-finite opcode parameter")) } + } + + fn f64(&mut self) -> Result { + let s = self.bytes(8)?; + let v = f64::from_be_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]]); + if v.is_finite() { Ok(v) } else { Err(Error::Malformed("non-finite opcode parameter")) } + } + + fn remaining(&self) -> usize { + self.b.len().saturating_sub(self.pos) + } + + /// A count of items of `size` bytes each that must fit in what is left. + fn count(&mut self, size: usize) -> Result { + let n = self.u32()? as usize; + if n.saturating_mul(size) > self.remaining() { + return Err(Error::Malformed("opcode count larger than its parameters")); + } + Ok(n) + } +} + +/// The picture an opcode list works on: `planes` interleaved samples per pixel. +pub struct Image<'a> { + pub data: &'a mut Vec, + 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<()> { + let mut r = Reader::new(&op.params); + match op.id { + 1 | 2 | 14 => warp(op.id, &mut r, img), + 3 => vignette(&mut r, img), + 4 => { + let constant = r.u32()?; + let phase = r.u32()?; + let mut bad = Vec::new(); + let c = if img.integer() { constant as f32 } else { constant as f32 / 65_535.0 }; + for (i, px) in img.data.iter().enumerate() { + if *px == c { + bad.push(i); + } + } + fix_bad(img, phase, &bad) + } + 5 => { + let phase = r.u32()?; + let points = r.u32()? as usize; + let rects = r.u32()? as usize; + if points.saturating_mul(8).saturating_add(rects.saturating_mul(16)) > r.remaining() { + return Err(Error::Malformed("FixBadPixelsList counts larger than its parameters")); + } + let (w, h) = (img.width, img.height); + let mut mask = vec![false; w * h]; + for _ in 0..points { + let (y, x) = (r.u32()? as usize, r.u32()? as usize); + if x < w && y < h { + mask[y * w + x] = true; + } + } + for _ in 0..rects { + let (t, l, b, rt) = (r.u32()? as usize, r.u32()? as usize, r.u32()? as usize, r.u32()? as usize); + for y in t.min(h)..b.min(h) { + for x in l.min(w)..rt.min(w) { + mask[y * w + x] = true; + } + } + } + let bad: Vec = mask.iter().enumerate().filter_map(|(i, &b)| b.then_some(i)).collect(); + fix_bad(img, phase, &bad) + } + 6 => trim(&mut r, img), + _ => area_op(op.id, &mut r, 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 read(r: &mut Reader, img: &Image) -> Result { + let mut v = [0usize; 8]; + for x in &mut v { + *x = r.u32()? as usize; + } + let [top, left, bottom, right, plane, planes, row_pitch, col_pitch] = v; + if row_pitch == 0 || col_pitch == 0 { + return Err(Error::Malformed("opcode area with a zero pitch")); + } + let plane = plane.min(img.planes); + Ok(Area { + top: top.min(img.height), + left: left.min(img.width), + bottom: bottom.min(img.height), + right: right.min(img.width), + plane, + planes: planes.min(img.planes - plane), + row_pitch, + col_pitch, + }) + } + + /// Rows of the area, in order. + fn rows(&self) -> impl Iterator + use<> { + (self.top..self.bottom.max(self.top)).step_by(self.row_pitch) + } + + fn cols(&self) -> impl Iterator + 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(id: u32, r: &mut Reader, img: &mut Image) -> Result<()> { + let a = Area::read(r, img)?; + let scale = img.max; + match id { + 7 => { + let n = r.count(2)?; + if n == 0 { + return Err(Error::Malformed("MapTable without entries")); + } + let table: Vec = (0..n).map(|_| r.bytes(2).map(|b| f32::from(u16::from_be_bytes([b[0], b[1]])))).collect::>()?; + let last = table.last().copied().unwrap_or(0.0); + a.each(img, |_, _, _, _, _, v| { + let code = (*v / scale * 65_535.0).round().clamp(0.0, 65_535.0) as usize; + *v = table.get(code).copied().unwrap_or(last) / 65_535.0 * scale; + }); + } + 8 => { + let degree = r.u32()? as usize; + if degree > 8 { + return Err(Error::Malformed("MapPolynomial degree above 8")); + } + let c: Vec = (0..=degree).map(|_| r.f64()).collect::>()?; + a.each(img, |_, _, _, _, _, v| { + let x = f64::from(*v / scale); + let y = c.iter().rev().fold(0.0, |acc, k| acc * x + k); + *v = (y * f64::from(scale)) as f32; + }); + } + 9 => gain_map(&a, r, img)?, + 10..=13 => { + let n = r.count(4)?; + let vals: Vec = (0..n).map(|_| r.f32()).collect::>()?; + let per_row = matches!(id, 10 | 12); + let need = if per_row { a.rows().count() } else { a.cols().count() }; + if vals.len() < need { + return Err(Error::Malformed("opcode with fewer values than rows or columns")); + } + let delta = id <= 11; + 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 }; + }); + } + _ => return Err(Error::Unsupported(format!("DNG opcode {id}"))), + } + Ok(()) +} + +fn gain_map(a: &Area, r: &mut Reader, img: &mut Image) -> Result<()> { + let points_v = r.u32()? as usize; + let points_h = r.u32()? as usize; + let spacing_v = r.f64()?; + let spacing_h = r.f64()?; + let origin_v = r.f64()?; + let origin_h = r.f64()?; + let map_planes = r.u32()? as usize; + let n = points_v.saturating_mul(points_h).saturating_mul(map_planes); + if points_v == 0 || points_h == 0 || map_planes == 0 || n.saturating_mul(4) > r.remaining() { + return Err(Error::Malformed("GainMap size")); + } + if (points_v > 1 && spacing_v <= 0.0) || (points_h > 1 && spacing_h <= 0.0) { + return Err(Error::Malformed("GainMap spacing")); + } + let gains: Vec = (0..n).map(|_| r.f32()).collect::>()?; + 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(r: &mut Reader, img: &mut Image) -> Result<()> { + let k: Vec = (0..5).map(|_| r.f64()).collect::>()?; + let (cx, cy) = (r.f64()?, r.f64()?); + 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(r: &mut Reader, img: &mut Image) -> Result<()> { + let (t, l, b, rt) = (r.u32()? as usize, r.u32()? as usize, r.u32()? as usize, r.u32()? as usize); + if !img.allow_trim { + return Err(Error::Unsupported("TrimBounds before the demosaic".into())); + } + let (b, rt) = (b.min(img.height), rt.min(img.width)); + if t >= b || l >= rt { + return Err(Error::Malformed("TrimBounds outside the picture")); + } + 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(id: u32, r: &mut Reader, img: &mut Image) -> Result<()> { + if img.mosaic { + return Err(Error::Unsupported("a warp opcode on a CFA mosaic".into())); + } + let per_set = match id { + 1 => 6, + 2 => 4, + _ => 19, + }; + let n = r.count(per_set * 8)?; + if n != 1 && n != img.planes { + return Err(Error::Malformed("warp opcode coefficient sets")); + } + let mut sets = Vec::with_capacity(n); + for _ in 0..n { + let v: Vec = (0..per_set).map(|_| r.f64()).collect::>()?; + sets.push(match id { + 1 => Radial::Rectilinear([v[0], v[1], v[2], v[3]], [v[4], v[5]]), + 2 => Radial::Fisheye([v[0], v[1], v[2], v[3]]), + _ => { + let mut kr = [0f64; 15]; + kr.copy_from_slice(&v[..15]); + let (min, max) = (v[17].clamp(0.0, 1.0), v[18].clamp(0.0, 1.0)); + if min >= max { + return Err(Error::Malformed("WarpRectilinear2 valid radius range")); + } + Radial::Rectilinear2 { kr, kt: [v[15], v[16]], min, max, reciprocal: false } + } + }); + } + let (cx, cy) = (r.f64()?, r.f64()?); + if id == 14 { + let reciprocal = r.u32()? != 0; + for s in &mut sets { + if let Radial::Rectilinear2 { reciprocal: rr, .. } = s { + *rr = reciprocal; + } + } + } + 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)]) -> Vec { + let mut out = (list.len() as u32).to_be_bytes().to_vec(); + for (id, flags, params) in list { + let version: u32 = if *id == 14 { 0x0106_0000 } else { 0x0103_0000 }; + for v in [*id, version, *flags, params.len() as u32] { + out.extend(v.to_be_bytes()); + } + out.extend(params); + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + fn u(v: &[u32]) -> Vec { + v.iter().flat_map(|x| x.to_be_bytes()).collect() + } + + fn d(v: &[f64]) -> Vec { + v.iter().flat_map(|x| x.to_be_bytes()).collect() + } + + fn f(v: &[f32]) -> Vec { + v.iter().flat_map(|x| x.to_be_bytes()).collect() + } + + fn area(t: u32, l: u32, b: u32, r: u32, plane: u32, planes: u32, rp: u32, cp: u32) -> Vec { + u(&[t, l, b, r, plane, planes, rp, cp]) + } + + /// Run one opcode on a float picture (range 0–1). + fn run1(id: u32, params: Vec, data: &mut Vec, 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::>()).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 = (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 = (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 = (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 = (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 = 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 = (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()); + } +} From 75cff558e58eaa18577932507c57ad9b30fe52c8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:04:29 +0000 Subject: [PATCH 05/25] raw: parse DNG opcode parameters into typed values --- crates/raw/src/lib.rs | 1 + crates/raw/src/opcode_params.rs | 1124 ++++++++----------------------- 2 files changed, 294 insertions(+), 831 deletions(-) diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index 8f31f2f6e..308167c16 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -119,6 +119,7 @@ mod nef; mod nef_lens; #[cfg(feature = "nef")] mod nefc; +pub mod opcode_params; /// Bounded raw DNG opcode-list envelopes. pub mod opcode_raw; pub mod opcodes; diff --git a/crates/raw/src/opcode_params.rs b/crates/raw/src/opcode_params.rs index bb05c650a..0aef54d09 100644 --- a/crates/raw/src/opcode_params.rs +++ b/crates/raw/src/opcode_params.rs @@ -1,884 +1,346 @@ -//! 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. +//! Context-aware parsing of DNG opcode parameters. Image processing remains the caller's job. -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; - -const OPTIONAL: u32 = 1; -const PREVIEW_SKIPPABLE: u32 = 2; - -/// One opcode of a list. -#[derive(Clone, Debug, PartialEq)] -pub struct Opcode { - pub id: u32, - /// The DNG version that defined it, as four bytes in a big-endian u32 (1.3.0.0 = 0x01030000). - pub version: u32, - pub flags: u32, - pub params: Vec, -} - -impl Opcode { - pub fn optional(&self) -> bool { - self.flags & OPTIONAL != 0 - } - - pub fn preview_skippable(&self) -> bool { - self.flags & PREVIEW_SKIPPABLE != 0 - } -} - -/// The opcode lists of a raw IFD. -#[derive(Clone, Debug, Default, PartialEq)] -pub struct Lists { - pub list1: Vec, - pub list2: Vec, - pub list3: Vec, -} - -/// Parse an opcode list (big-endian). -pub fn parse(b: &[u8]) -> Result> { - let bad = || Error::Malformed("opcode list"); - let mut r = Reader::new(b); - let count = r.u32().map_err(|_| bad())? as usize; - // every opcode takes at least 16 bytes - if count > b.len() / 16 || count > MAX_OPCODES { - return Err(bad()); - } - let mut out = Vec::with_capacity(count); - for _ in 0..count { - let id = r.u32().map_err(|_| bad())?; - let version = r.u32().map_err(|_| bad())?; - let flags = r.u32().map_err(|_| bad())?; - let len = r.u32().map_err(|_| bad())? as usize; - let params = r.bytes(len).map_err(|_| bad())?.to_vec(); - out.push(Opcode { id, version, flags, params }); - } - Ok(out) -} - -/// Big-endian parameter reader. -struct Reader<'a> { - b: &'a [u8], - pos: usize, -} - -impl<'a> Reader<'a> { - fn new(b: &'a [u8]) -> Self { - Reader { b, pos: 0 } - } - - fn bytes(&mut self, n: usize) -> Result<&'a [u8]> { - let end = self.pos.checked_add(n).ok_or(Error::Malformed("opcode parameters"))?; - let s = self.b.get(self.pos..end).ok_or(Error::Malformed("opcode parameters too short"))?; - self.pos = end; - Ok(s) - } - - fn u32(&mut self) -> Result { - let s = self.bytes(4)?; - Ok(u32::from_be_bytes([s[0], s[1], s[2], s[3]])) - } - - fn f32(&mut self) -> Result { - let v = f32::from_bits(self.u32()?); - if v.is_finite() { Ok(v) } else { Err(Error::Malformed("non-finite opcode parameter")) } - } - - fn f64(&mut self) -> Result { - let s = self.bytes(8)?; - let v = f64::from_be_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]]); - if v.is_finite() { Ok(v) } else { Err(Error::Malformed("non-finite opcode parameter")) } - } - - fn remaining(&self) -> usize { - self.b.len().saturating_sub(self.pos) - } - - /// A count of items of `size` bytes each that must fit in what is left. - fn count(&mut self, size: usize) -> Result { - let n = self.u32()? as usize; - if n.saturating_mul(size) > self.remaining() { - return Err(Error::Malformed("opcode count larger than its parameters")); - } - Ok(n) - } -} - -/// The picture an opcode list works on: `planes` interleaved samples per pixel. -pub struct Image<'a> { - pub data: &'a mut Vec, +/// Image properties used to validate and clamp opcode parameters. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Context { 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 - }; - } - } +/// A parameter failure, retaining the diagnostic used by the caller. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ParseError { + Malformed(&'static str), + Unsupported(&'static str), } - -/// 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(()) +/// A clamped image area and its sample pitches. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Area { + pub top: usize, + pub left: usize, + pub bottom: usize, + pub right: usize, + pub plane: usize, + pub planes: usize, + pub row_pitch: usize, + pub col_pitch: usize, } - -fn run(op: &Opcode, img: &mut Image) -> Result<()> { - let mut r = Reader::new(&op.params); - match op.id { - 1 | 2 | 14 => warp(op.id, &mut r, img), - 3 => vignette(&mut r, img), - 4 => { - let constant = r.u32()?; - let phase = r.u32()?; - let mut bad = Vec::new(); - let c = if img.integer() { constant as f32 } else { constant as f32 / 65_535.0 }; - for (i, px) in img.data.iter().enumerate() { - if *px == c { - bad.push(i); - } - } - fix_bad(img, phase, &bad) - } - 5 => { - let phase = r.u32()?; - let points = r.u32()? as usize; - let rects = r.u32()? as usize; - if points.saturating_mul(8).saturating_add(rects.saturating_mul(16)) > r.remaining() { - return Err(Error::Malformed("FixBadPixelsList counts larger than its parameters")); - } - let (w, h) = (img.width, img.height); - let mut mask = vec![false; w * h]; - for _ in 0..points { - let (y, x) = (r.u32()? as usize, r.u32()? as usize); - if x < w && y < h { - mask[y * w + x] = true; - } - } - for _ in 0..rects { - let (t, l, b, rt) = (r.u32()? as usize, r.u32()? as usize, r.u32()? as usize, r.u32()? as usize); - for y in t.min(h)..b.min(h) { - for x in l.min(w)..rt.min(w) { - mask[y * w + x] = true; - } - } - } - let bad: Vec = mask.iter().enumerate().filter_map(|(i, &b)| b.then_some(i)).collect(); - fix_bad(img, phase, &bad) - } - 6 => trim(&mut r, img), - _ => area_op(op.id, &mut r, 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 read(r: &mut Reader, img: &Image) -> Result { - let mut v = [0usize; 8]; - for x in &mut v { - *x = r.u32()? as usize; + pub fn rows(&self) -> impl Iterator + use<> { + (self.top..self.bottom.max(self.top)).step_by(self.row_pitch) + } + pub fn cols(&self) -> impl Iterator + use<> { + (self.left..self.right.max(self.left)).step_by(self.col_pitch) + } +} +/// Borrowed bad-pixel coordinates, decoded lazily without copying the payload. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct PointRecords<'a>(&'a [u8]); +impl PointRecords<'_> { + pub fn iter(&self) -> impl Iterator + '_ { + self.0.chunks_exact(8).map(|b| (u32::from_be_bytes([b[0], b[1], b[2], b[3]]), u32::from_be_bytes([b[4], b[5], b[6], b[7]]))) + } +} +/// Borrowed bad-pixel rectangles, decoded lazily without copying the payload. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct RectangleRecords<'a>(&'a [u8]); +impl RectangleRecords<'_> { + pub fn iter(&self) -> impl Iterator + '_ { + self.0.chunks_exact(16).map(|b| { + [ + u32::from_be_bytes([b[0], b[1], b[2], b[3]]), + u32::from_be_bytes([b[4], b[5], b[6], b[7]]), + u32::from_be_bytes([b[8], b[9], b[10], b[11]]), + u32::from_be_bytes([b[12], b[13], b[14], b[15]]), + ] + }) + } +} +/// Parameters for the fourteen defined opcodes. +#[derive(Clone, Debug, PartialEq)] +pub enum Params<'a> { + WarpRectilinear { planes: Vec<[f64; 6]>, center: [f64; 2] }, + WarpFisheye { planes: Vec<[f64; 4]>, center: [f64; 2] }, + FixVignetteRadial { k: [f64; 5], center: [f64; 2] }, + FixBadPixelsConstant { constant: u32, bayer_phase: u32 }, + FixBadPixelsList { bayer_phase: u32, points: PointRecords<'a>, rects: RectangleRecords<'a> }, + TrimBounds { top: usize, left: usize, bottom: usize, right: usize }, + MapTable { area: Area, table: Vec }, + MapPolynomial { area: Area, coefficients: Vec }, + GainMap { area: Area, points_v: usize, points_h: usize, spacing: [f64; 2], origin: [f64; 2], map_planes: usize, gains: Vec }, + DeltaPerRow { area: Area, deltas: Vec }, + DeltaPerColumn { area: Area, deltas: Vec }, + ScalePerRow { area: Area, scales: Vec }, + ScalePerColumn { area: Area, scales: Vec }, + WarpRectilinear2 { planes: Vec<[f64; 19]>, center: [f64; 2], reciprocal: bool }, +} +struct Reader<'a> { + bytes: &'a [u8], + pos: usize, +} +impl<'a> Reader<'a> { + fn take(&mut self, n: usize) -> Result<&'a [u8], ParseError> { + let end = self.pos.checked_add(n).ok_or(ParseError::Malformed("opcode parameters"))?; + let b = self.bytes.get(self.pos..end).ok_or(ParseError::Malformed("opcode parameters too short"))?; + self.pos = end; + Ok(b) + } + fn u32(&mut self) -> Result { + let b = self.take(4)?; + Ok(u32::from_be_bytes([b[0], b[1], b[2], b[3]])) + } + fn f32(&mut self) -> Result { + let v = f32::from_bits(self.u32()?); + if v.is_finite() { Ok(v) } else { Err(ParseError::Malformed("non-finite opcode parameter")) } + } + fn f64(&mut self) -> Result { + let b = self.take(8)?; + let v = f64::from_be_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]); + if v.is_finite() { Ok(v) } else { Err(ParseError::Malformed("non-finite opcode parameter")) } + } + fn remaining(&self) -> usize { + self.bytes.len().saturating_sub(self.pos) + } + fn count(&mut self, size: usize) -> Result { + let n = self.u32()? as usize; + if n.saturating_mul(size) > self.remaining() { Err(ParseError::Malformed("opcode count larger than its parameters")) } else { Ok(n) } + } + fn array(&mut self) -> Result<[f64; N], ParseError> { + let mut a = [0.0; N]; + for x in &mut a { + *x = self.f64()?; } - let [top, left, bottom, right, plane, planes, row_pitch, col_pitch] = v; + Ok(a) + } + fn area(&mut self, c: Context) -> Result { + let mut a = [0usize; 8]; + for v in &mut a { + *v = self.u32()? as usize; + } + let [top, left, bottom, right, plane, planes, row_pitch, col_pitch] = a; if row_pitch == 0 || col_pitch == 0 { - return Err(Error::Malformed("opcode area with a zero pitch")); + return Err(ParseError::Malformed("opcode area with a zero pitch")); } - let plane = plane.min(img.planes); + let plane = plane.min(c.planes); Ok(Area { - top: top.min(img.height), - left: left.min(img.width), - bottom: bottom.min(img.height), - right: right.min(img.width), + top: top.min(c.height), + left: left.min(c.width), + bottom: bottom.min(c.height), + right: right.min(c.width), plane, - planes: planes.min(img.planes - plane), + planes: planes.min(c.planes - plane), row_pitch, col_pitch, }) } - - /// Rows of the area, in order. - fn rows(&self) -> impl Iterator + use<> { - (self.top..self.bottom.max(self.top)).step_by(self.row_pitch) - } - - fn cols(&self) -> impl Iterator + 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(id: u32, r: &mut Reader, img: &mut Image) -> Result<()> { - let a = Area::read(r, img)?; - let scale = img.max; +/// Parse a known opcode's parameters. Unknown identifiers return an unsupported error. +pub fn parse(id: u32, bytes: &[u8], context: Context) -> Result, ParseError> { + let mut r = Reader { bytes, pos: 0 }; match id { - 7 => { - let n = r.count(2)?; - if n == 0 { - return Err(Error::Malformed("MapTable without entries")); + 1 | 2 | 14 => { + if context.mosaic { + return Err(ParseError::Unsupported("a warp opcode on a CFA mosaic")); } - let table: Vec = (0..n).map(|_| r.bytes(2).map(|b| f32::from(u16::from_be_bytes([b[0], b[1]])))).collect::>()?; - let last = table.last().copied().unwrap_or(0.0); - a.each(img, |_, _, _, _, _, v| { - let code = (*v / scale * 65_535.0).round().clamp(0.0, 65_535.0) as usize; - *v = table.get(code).copied().unwrap_or(last) / 65_535.0 * scale; - }); - } - 8 => { - let degree = r.u32()? as usize; - if degree > 8 { - return Err(Error::Malformed("MapPolynomial degree above 8")); + let per = match id { + 1 => 6, + 2 => 4, + _ => 19, + }; + let n = r.count(per * 8)?; + if n != 1 && n != context.planes { + return Err(ParseError::Malformed("warp opcode coefficient sets")); } - let c: Vec = (0..=degree).map(|_| r.f64()).collect::>()?; - a.each(img, |_, _, _, _, _, v| { - let x = f64::from(*v / scale); - let y = c.iter().rev().fold(0.0, |acc, k| acc * x + k); - *v = (y * f64::from(scale)) as f32; - }); - } - 9 => gain_map(&a, r, img)?, - 10..=13 => { - let n = r.count(4)?; - let vals: Vec = (0..n).map(|_| r.f32()).collect::>()?; - let per_row = matches!(id, 10 | 12); - let need = if per_row { a.rows().count() } else { a.cols().count() }; - if vals.len() < need { - return Err(Error::Malformed("opcode with fewer values than rows or columns")); - } - let delta = id <= 11; - 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 }; - }); - } - _ => return Err(Error::Unsupported(format!("DNG opcode {id}"))), - } - Ok(()) -} - -fn gain_map(a: &Area, r: &mut Reader, img: &mut Image) -> Result<()> { - let points_v = r.u32()? as usize; - let points_h = r.u32()? as usize; - let spacing_v = r.f64()?; - let spacing_h = r.f64()?; - let origin_v = r.f64()?; - let origin_h = r.f64()?; - let map_planes = r.u32()? as usize; - let n = points_v.saturating_mul(points_h).saturating_mul(map_planes); - if points_v == 0 || points_h == 0 || map_planes == 0 || n.saturating_mul(4) > r.remaining() { - return Err(Error::Malformed("GainMap size")); - } - if (points_v > 1 && spacing_v <= 0.0) || (points_h > 1 && spacing_h <= 0.0) { - return Err(Error::Malformed("GainMap spacing")); - } - let gains: Vec = (0..n).map(|_| r.f32()).collect::>()?; - 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(r: &mut Reader, img: &mut Image) -> Result<()> { - let k: Vec = (0..5).map(|_| r.f64()).collect::>()?; - let (cx, cy) = (r.f64()?, r.f64()?); - 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; + match id { + 1 => { + let planes = (0..n).map(|_| r.array()).collect::>()?; + let center = r.array()?; + Ok(Params::WarpRectilinear { planes, center }) + } + 2 => { + let planes = (0..n).map(|_| r.array()).collect::>()?; + let center = r.array()?; + Ok(Params::WarpFisheye { planes, center }) + } + _ => { + let mut planes = Vec::with_capacity(n); + for _ in 0..n { + let mut p: [f64; 19] = r.array()?; + p[17] = p[17].clamp(0.0, 1.0); + p[18] = p[18].clamp(0.0, 1.0); + if p[17] >= p[18] { + return Err(ParseError::Malformed("WarpRectilinear2 valid radius range")); + } + planes.push(p); } - 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; + let center = r.array()?; + let reciprocal = r.u32()? != 0; + Ok(Params::WarpRectilinear2 { planes, center, reciprocal }) + } + } + } + 3 => Ok(Params::FixVignetteRadial { k: r.array()?, center: r.array()? }), + 4 => Ok(Params::FixBadPixelsConstant { constant: r.u32()?, bayer_phase: r.u32()? }), + 5 => { + let bayer_phase = r.u32()?; + let points = r.u32()? as usize; + let rects = r.u32()? as usize; + if points.saturating_mul(8).saturating_add(rects.saturating_mul(16)) > r.remaining() { + return Err(ParseError::Malformed("FixBadPixelsList counts larger than its parameters")); + } + let points = + PointRecords(r.take(points.checked_mul(8).ok_or(ParseError::Malformed("FixBadPixelsList counts larger than its parameters"))?)?); + let rects = + RectangleRecords(r.take(rects.checked_mul(16).ok_or(ParseError::Malformed("FixBadPixelsList counts larger than its parameters"))?)?); + Ok(Params::FixBadPixelsList { bayer_phase, points, rects }) + } + 6 => { + let top = r.u32()? as usize; + let left = r.u32()? as usize; + let bottom = r.u32()? as usize; + let right = r.u32()? as usize; + if !context.allow_trim { + return Err(ParseError::Unsupported("TrimBounds before the demosaic")); + } + let bottom = bottom.min(context.height); + let right = right.min(context.width); + if top >= bottom || left >= right { + return Err(ParseError::Malformed("TrimBounds outside the picture")); + } + Ok(Params::TrimBounds { top, left, bottom, right }) + } + 7..=13 => { + let area = r.area(context)?; + match id { + 7 => { + let n = r.count(2)?; + if n == 0 { + return Err(ParseError::Malformed("MapTable without entries")); } + let table = (0..n) + .map(|_| { + let b = r.take(2)?; + Ok(u16::from_be_bytes([b[0], b[1]])) + }) + .collect::>()?; + Ok(Params::MapTable { area, table }) } - } - if n > 0 { - if let Some(v) = img.data.get_mut(i) { - *v = (sum / f64::from(n)) as f32; - } - break; - } - } - } - Ok(()) -} - -fn trim(r: &mut Reader, img: &mut Image) -> Result<()> { - let (t, l, b, rt) = (r.u32()? as usize, r.u32()? as usize, r.u32()? as usize, r.u32()? as usize); - if !img.allow_trim { - return Err(Error::Unsupported("TrimBounds before the demosaic".into())); - } - let (b, rt) = (b.min(img.height), rt.min(img.width)); - if t >= b || l >= rt { - return Err(Error::Malformed("TrimBounds outside the picture")); - } - 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); + 8 => { + let degree = r.u32()? as usize; + if degree > 8 { + return Err(ParseError::Malformed("MapPolynomial degree above 8")); } - 1.0 / f - } else { - f - }; - let (tx, ty) = tangential(kt); - (f * dx + tx, f * dy + ty) - } - } - } -} - -fn warp(id: u32, r: &mut Reader, img: &mut Image) -> Result<()> { - if img.mosaic { - return Err(Error::Unsupported("a warp opcode on a CFA mosaic".into())); - } - let per_set = match id { - 1 => 6, - 2 => 4, - _ => 19, - }; - let n = r.count(per_set * 8)?; - if n != 1 && n != img.planes { - return Err(Error::Malformed("warp opcode coefficient sets")); - } - let mut sets = Vec::with_capacity(n); - for _ in 0..n { - let v: Vec = (0..per_set).map(|_| r.f64()).collect::>()?; - sets.push(match id { - 1 => Radial::Rectilinear([v[0], v[1], v[2], v[3]], [v[4], v[5]]), - 2 => Radial::Fisheye([v[0], v[1], v[2], v[3]]), - _ => { - let mut kr = [0f64; 15]; - kr.copy_from_slice(&v[..15]); - let (min, max) = (v[17].clamp(0.0, 1.0), v[18].clamp(0.0, 1.0)); - if min >= max { - return Err(Error::Malformed("WarpRectilinear2 valid radius range")); + let coefficients = (0..=degree).map(|_| r.f64()).collect::>()?; + Ok(Params::MapPolynomial { area, coefficients }) } - Radial::Rectilinear2 { kr, kt: [v[15], v[16]], min, max, reciprocal: false } - } - }); - } - let (cx, cy) = (r.f64()?, r.f64()?); - if id == 14 { - let reciprocal = r.u32()? != 0; - for s in &mut sets { - if let Radial::Rectilinear2 { reciprocal: rr, .. } = s { - *rr = reciprocal; - } - } - } - 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); + 9 => { + let points_v = r.u32()? as usize; + let points_h = r.u32()? as usize; + let spacing = r.array()?; + let origin = r.array()?; + let map_planes = r.u32()? as usize; + let n = points_v.saturating_mul(points_h).saturating_mul(map_planes); + if points_v == 0 || points_h == 0 || map_planes == 0 || n.saturating_mul(4) > r.remaining() { + return Err(ParseError::Malformed("GainMap size")); + } + if (points_v > 1 && spacing[0] <= 0.0) || (points_h > 1 && spacing[1] <= 0.0) { + return Err(ParseError::Malformed("GainMap spacing")); + } + let gains = (0..n).map(|_| r.f32()).collect::>()?; + Ok(Params::GainMap { area, points_v, points_h, spacing, origin, map_planes, gains }) + } + _ => { + let n = r.count(4)?; + let vals = (0..n).map(|_| r.f32()).collect::, _>>()?; + let need = if matches!(id, 10 | 12) { area.rows().count() } else { area.cols().count() }; + if vals.len() < need { + return Err(ParseError::Malformed("opcode with fewer values than rows or columns")); + } + Ok(match id { + 10 => Params::DeltaPerRow { area, deltas: vals }, + 11 => Params::DeltaPerColumn { area, deltas: vals }, + 12 => Params::ScalePerRow { area, scales: vals }, + _ => Params::ScalePerColumn { area, scales: vals }, + }) } - acc += wyj * row; } - *v = acc as f32; } + _ => Err(ParseError::Unsupported("unknown DNG opcode")), } - Ok(()) -} - -#[cfg(any(test, feature = "writer"))] -/// Encode an opcode list (big-endian), for tests: (id, flags, parameters). -pub fn encode(list: &[(u32, u32, Vec)]) -> Vec { - let mut out = (list.len() as u32).to_be_bytes().to_vec(); - for (id, flags, params) in list { - let version: u32 = if *id == 14 { 0x0106_0000 } else { 0x0103_0000 }; - for v in [*id, version, *flags, params.len() as u32] { - out.extend(v.to_be_bytes()); - } - out.extend(params); - } - out } #[cfg(test)] mod tests { use super::*; - fn u(v: &[u32]) -> Vec { v.iter().flat_map(|x| x.to_be_bytes()).collect() } - fn d(v: &[f64]) -> Vec { v.iter().flat_map(|x| x.to_be_bytes()).collect() } - fn f(v: &[f32]) -> Vec { v.iter().flat_map(|x| x.to_be_bytes()).collect() } - - fn area(t: u32, l: u32, b: u32, r: u32, plane: u32, planes: u32, rp: u32, cp: u32) -> Vec { - u(&[t, l, b, r, plane, planes, rp, cp]) + fn ctx() -> Context { + Context { width: 4, height: 3, planes: 2, mosaic: false, allow_trim: true } } - - /// Run one opcode on a float picture (range 0–1). - fn run1(id: u32, params: Vec, data: &mut Vec, 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}"); + fn parses_all_variants_and_clamps_areas() -> Result<(), ParseError> { + let area = u(&[0, 1, 8, 9, 1, 5, 1, 2]); + let cases = vec![ + (1, [u(&[1]), d(&[1., 0., 0., 0., 0., 0., 0.5, 0.5])].concat()), + (2, [u(&[1]), d(&[1., 0., 0., 0., 0.5, 0.5])].concat()), + (3, d(&[0., 0., 0., 0., 0., 0.5, 0.5])), + (4, u(&[0, 0])), + (5, u(&[0, 1, 1, 1, 2, 0, 0, 1, 1])), + (6, u(&[0, 1, 8, 9])), + (7, [area.clone(), u(&[1]), vec![0, 1]].concat()), + (8, [area.clone(), u(&[0]), d(&[1.])].concat()), + (9, [area.clone(), u(&[1, 1]), d(&[0., 0., 0., 0.]), u(&[1]), f(&[1.])].concat()), + (10, [area.clone(), u(&[3]), f(&[1., 2., 3.])].concat()), + (11, [area.clone(), u(&[2]), f(&[1., 2.])].concat()), + (12, [area.clone(), u(&[3]), f(&[1., 2., 3.])].concat()), + (13, [area.clone(), u(&[2]), f(&[1., 2.])].concat()), + (14, [u(&[1]), d(&[0.; 17]), d(&[-1., 2., 0.5, 0.5]), u(&[1])].concat()), + ]; + for (id, bytes) in cases { + let p = parse(id, &bytes, ctx())?; + assert!(matches!( + (id, p), + (1, Params::WarpRectilinear { .. }) + | (2, Params::WarpFisheye { .. }) + | (3, Params::FixVignetteRadial { .. }) + | (4, Params::FixBadPixelsConstant { .. }) + | (5, Params::FixBadPixelsList { .. }) + | (6, Params::TrimBounds { .. }) + | (7, Params::MapTable { .. }) + | (8, Params::MapPolynomial { .. }) + | (9, Params::GainMap { .. }) + | (10, Params::DeltaPerRow { .. }) + | (11, Params::DeltaPerColumn { .. }) + | (12, Params::ScalePerRow { .. }) + | (13, Params::ScalePerColumn { .. }) + | (14, Params::WarpRectilinear2 { .. }) + )); } - // 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::>()).is_err()); - assert_eq!(parse(&u(&[0])).unwrap(), vec![]); + assert!(matches!( + parse(7, &[area, u(&[1]), vec![0, 1]].concat(), ctx()), + Ok(Params::MapTable { area: Area { bottom: 3, right: 4, plane: 1, planes: 1, .. }, .. }) + )); + Ok(()) } - #[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 = (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 = (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 = (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 = (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 = 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 = (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()); + fn validates_in_application_order() { + assert_eq!(parse(1, &[], Context { mosaic: true, ..ctx() }), Err(ParseError::Unsupported("a warp opcode on a CFA mosaic"))); + assert_eq!(parse(6, &[], Context { allow_trim: false, ..ctx() }), Err(ParseError::Malformed("opcode parameters too short"))); + assert_eq!( + parse(6, &u(&[0, 0, 1, 1]), Context { allow_trim: false, ..ctx() }), + Err(ParseError::Unsupported("TrimBounds before the demosaic")) + ); + assert_eq!(parse(5, &u(&[0, u32::MAX, u32::MAX]), ctx()), Err(ParseError::Malformed("FixBadPixelsList counts larger than its parameters"))); + let a = u(&[0, 0, 3, 4, 0, 1, 1, 1]); + assert_eq!(parse(10, &[a, u(&[1]), f(&[f32::NAN])].concat(), ctx()), Err(ParseError::Malformed("non-finite opcode parameter"))); } } From fab94c80feaba03a7a2cd006c4684b0d00296d90 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:06:20 +0000 Subject: [PATCH 06/25] raw: bound opcode area count validation --- crates/raw/src/opcode_params.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/raw/src/opcode_params.rs b/crates/raw/src/opcode_params.rs index 0aef54d09..4f2433269 100644 --- a/crates/raw/src/opcode_params.rs +++ b/crates/raw/src/opcode_params.rs @@ -254,7 +254,11 @@ pub fn parse(id: u32, bytes: &[u8], context: Context) -> Result, Pars _ => { let n = r.count(4)?; let vals = (0..n).map(|_| r.f32()).collect::, _>>()?; - let need = if matches!(id, 10 | 12) { area.rows().count() } else { area.cols().count() }; + let need = if matches!(id, 10 | 12) { + area.bottom.saturating_sub(area.top).div_ceil(area.row_pitch) + } else { + area.right.saturating_sub(area.left).div_ceil(area.col_pitch) + }; if vals.len() < need { return Err(ParseError::Malformed("opcode with fewer values than rows or columns")); } From 7ad0385abd69e99f1ea33a56914369cc15a6f70e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:11:58 +0000 Subject: [PATCH 07/25] raw: order opcode modules for rustfmt From 440e14c7f25110602c2c02f7f1d5462757a860c6 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 11 Oct 2026 15:44:00 +0200 Subject: [PATCH 08/25] #1065: clean-room DNG / CinemaDNG decoder crate Problem: camera RAW is at 0% (docs/codec-parity.md, #383). CinemaDNG, the one cinema RAW format with a public specification, could not be read at all. Fix: new L0 crate filmcraft-dng, written from the public DNG 1.4-1.7, TIFF 6.0 / TIFF-EP, ITU-T T.81 and CinemaDNG specifications only (no other decoder's source read; editions in the crate README): - TIFF container with SubIFDs, bounded (64 IFDs, 4 levels, cycles end the walk) and checked offsets; the main raw IFD is the largest NewSubFileType 0 CFA / LinearRaw image. - Raw data: 1-16-bit uncompressed (MSB-first packing) and lossless JPEG (process 14, predictors 1-7, 1-4 components, point transform, line restarts), strips and tiles, tiles decoded in parallel; allocations capped by size and by the data present. - Processing per the DNG model: ActiveArea, LinearizationTable, per-site black levels with deltas, white levels, as-shot white balance with neutral highlight clipping, Malvar-He-Cutler Bayer demosaic (bilinear for other patterns), DefaultCrop, ColorMatrix / CameraCalibration / ForwardMatrix with illuminant interpolation, Bradford to linear Rec. 709, BaselineExposure; output scene-linear RGBA floats. - CinemaDNG FrameRate, TimeCodes and ReelName tags. - A test-only writer (feature `writer`) synthesises DNGs. Tests: raw samples bit-exact for every depth / predictor / layout, processing against hand-computed values, hostile files (every truncation, 12k mutations, cyclic IFDs, huge sizes, bad offsets), and LibRaw's unprocessed_raw as an external oracle reading the same samples (skipped when not installed). Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/dng_metadata.rs | 358 ++++++++++++++++ crates/raw/src/dng_metadata_source_color.rs | 385 ++++++++++++++++++ .../raw/src/dng_metadata_source_demosaic.rs | 255 ++++++++++++ crates/raw/src/dng_metadata_source_process.rs | 264 ++++++++++++ 4 files changed, 1262 insertions(+) create mode 100644 crates/raw/src/dng_metadata.rs create mode 100644 crates/raw/src/dng_metadata_source_color.rs create mode 100644 crates/raw/src/dng_metadata_source_demosaic.rs create mode 100644 crates/raw/src/dng_metadata_source_process.rs diff --git a/crates/raw/src/dng_metadata.rs b/crates/raw/src/dng_metadata.rs new file mode 100644 index 000000000..c0b3e9e0b --- /dev/null +++ b/crates/raw/src/dng_metadata.rs @@ -0,0 +1,358 @@ +//! Clean-room DNG and CinemaDNG decoder, implemented from the public specifications only (see the +//! crate README for editions): the TIFF 6.0 / TIFF-EP container, uncompressed and lossless JPEG +//! (ITU-T T.81 process 14) raw data, and the DNG processing model (linearisation, black and white +//! levels, white balance, demosaic, default crop, colour matrices) to a scene-linear Rec. 709 +//! picture. +//! +//! - [`sniff`]: is this a DNG (a TIFF with a DNGVersion tag)? +//! - [`Dng::parse`] → [`Dng::info`] (size, bit depth, CinemaDNG frame rate and time code), +//! [`Dng::raw`] (the raw samples) and [`Dng::develop`] (linear RGBA floats). +//! - [`decode`]: parse and develop in one call. +//! +//! Every number in the file is hostile: offsets, counts and sizes are checked, allocations are +//! capped by [`raw::MAX_SAMPLES`] and by the amount of data present, and the IFD walk is bounded. + +#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))] + +pub mod color; +pub mod demosaic; +pub mod ljpeg; +pub mod process; +pub mod raw; +pub mod tiff; +#[cfg(any(test, feature = "writer"))] +pub mod writer; + +use color::{Calibration, M3, V3}; +use demosaic::Pattern; +use process::Params; +use raw::{PHOTOMETRIC_CFA, RawImage}; +use tiff::{Entry, Ifd, Tiff, tags}; + +/// DNG decoding errors. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + #[error("not a DNG file")] + NotDng, + #[error("truncated DNG data")] + Truncated, + #[error("raw image too large")] + TooLarge, + #[error("malformed DNG: {0}")] + Malformed(&'static str), + #[error("unsupported DNG: {0}")] + Unsupported(String), +} + +pub type Result = std::result::Result; + +/// Whether `b` is a DNG: a TIFF file whose first IFD has a DNGVersion tag. +pub fn sniff(b: &[u8]) -> bool { + let Ok((t, first)) = Tiff::new(b) else { return false }; + t.read_ifd(first, 0).is_ok_and(|(ifd, _)| ifd.get(tags::DNG_VERSION).is_some()) +} + +/// SMPTE ST 12-1 time code of a CinemaDNG frame. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Timecode { + pub hours: u8, + pub minutes: u8, + pub seconds: u8, + pub frames: u8, + pub drop_frame: bool, +} + +impl Timecode { + /// The first time code of a CinemaDNG TimeCodes tag (SMPTE ST 331 / 12-1 packing: BCD frames, + /// seconds, minutes, hours in bytes 0–3, the drop-frame flag in bit 6 of byte 0). + pub fn from_bytes(b: &[u8]) -> Option { + let bcd = |v: u8, tens_mask: u8| { + let (u, t) = (v & 0x0F, (v >> 4) & tens_mask); + (u <= 9).then_some(t * 10 + u) + }; + let [f, s, m, h, ..] = *b else { return None }; + let tc = Timecode { frames: bcd(f, 0x3)?, seconds: bcd(s, 0x7)?, minutes: bcd(m, 0x7)?, hours: bcd(h, 0x3)?, drop_frame: f & 0x40 != 0 }; + (tc.seconds < 60 && tc.minutes < 60 && tc.hours < 24).then_some(tc) + } +} + +/// What a DNG holds, without decoding the pixels. +#[derive(Clone, Debug, PartialEq)] +pub struct Info { + /// Size of the developed picture (the default crop). + pub width: usize, + pub height: usize, + /// Size of the stored raw image. + pub raw_width: usize, + pub raw_height: usize, + pub bits: u32, + pub samples_per_pixel: usize, + /// Colour filter array data (else LinearRaw). + pub cfa: bool, + /// "uncompressed" or "lossless JPEG". + pub compression: &'static str, + pub dng_version: [u8; 4], + /// CinemaDNG FrameRate (numerator, denominator). + pub frame_rate: Option<(i64, i64)>, + /// CinemaDNG time code of this frame. + pub timecode: Option, + /// UniqueCameraModel, else Make and Model. + pub camera: Option, + /// CinemaDNG ReelName. + pub reel: Option, +} + +/// A developed DNG: premultiplied, scene-linear Rec. 709 RGBA floats (alpha 1), top row first. +/// A white surface the sensor saw at its clipping level is 1.0 times 2^BaselineExposure. +pub struct Image { + pub info: Info, + pub width: usize, + pub height: usize, + pub rgba: Vec, +} + +/// A parsed DNG file. +pub struct Dng<'a> { + tiff: Tiff<'a>, + ifds: Vec, + /// Index of the main raw IFD in `ifds` (the first is IFD 0). + raw: usize, +} + +impl<'a> Dng<'a> { + /// Parse the container and find the main raw image. + pub fn parse(bytes: &'a [u8]) -> Result> { + let (tiff, first) = Tiff::new(bytes)?; + let ifds = tiff.all_ifds(first)?; + if ifds.first().and_then(|i| i.get(tags::DNG_VERSION)).is_none() { + return Err(Error::NotDng); + } + let main = raw::find_raw_ifd(&tiff, &ifds)?; + let raw = ifds.iter().position(|i| i.offset == main.offset).ok_or(Error::Malformed("raw IFD"))?; + Ok(Dng { tiff, ifds, raw }) + } + + fn ifd0(&self) -> &Ifd { + static EMPTY: Ifd = Ifd { offset: 0, entries: Vec::new(), depth: 0 }; + self.ifds.first().unwrap_or(&EMPTY) + } + + fn raw_ifd(&self) -> &Ifd { + self.ifds.get(self.raw).unwrap_or(self.ifd0()) + } + + /// A raw-image tag: from the raw IFD, else IFD 0. + fn raw_tag(&self, tag: u16) -> Option<&Entry> { + self.raw_ifd().get(tag).or_else(|| self.ifd0().get(tag)) + } + + /// A camera-wide tag: from IFD 0, else the raw IFD. + fn main_tag(&self, tag: u16) -> Option<&Entry> { + self.ifd0().get(tag).or_else(|| self.raw_ifd().get(tag)) + } + + /// A numeric tag's values; a value outside the file (or a zero denominator) is an error, so a + /// damaged file does not silently lose its white balance. + fn floats(&self, e: Option<&Entry>) -> Result>> { + e.map(|e| self.tiff.floats(e)).transpose() + } + + /// A 3 × 3 matrix tag (`None` when absent or not 3 × 3, e.g. for other colour counts). + fn matrix(&self, tag: u16) -> Result> { + let Some(v) = self.floats(self.main_tag(tag))? else { return Ok(None) }; + let Ok(v) = <[f64; 9]>::try_from(v.as_slice()) else { return Ok(None) }; + Ok(Some([[v[0], v[1], v[2]], [v[3], v[4], v[5]], [v[6], v[7], v[8]]])) + } + + fn vec3(&self, tag: u16) -> Result> { + Ok(self.floats(self.main_tag(tag))?.and_then(|v| <[f64; 3]>::try_from(v.as_slice()).ok())) + } + + fn text(&self, tag: u16) -> Option { + self.main_tag(tag).and_then(|e| self.tiff.ascii(e).ok()).filter(|s| !s.is_empty()) + } + + /// The CFA pattern in camera planes (`None` for LinearRaw). + fn cfa(&self) -> Result> { + let l = raw::layout(&self.tiff, self.raw_ifd())?; + if l.photometric != PHOTOMETRIC_CFA { + return Ok(None); + } + let dims = self.raw_tag(tags::CFA_REPEAT_PATTERN_DIM).map(|e| self.tiff.uints(e)).transpose()?.unwrap_or_else(|| vec![2, 2]); + let (rows, cols) = match dims[..] { + [r, c, ..] => (r as usize, c as usize), + _ => return Err(Error::Malformed("CFARepeatPatternDim")), + }; + if !(1..=8).contains(&rows) || !(1..=8).contains(&cols) { + return Err(Error::Unsupported(format!("{rows} × {cols} CFA pattern"))); + } + let pattern = self.tiff.uints(self.raw_tag(tags::CFA_PATTERN).ok_or(Error::Malformed("no CFAPattern"))?)?; + if pattern.len() < rows * cols { + return Err(Error::Malformed("CFAPattern shorter than its repeat")); + } + let planes = match self.raw_tag(tags::CFA_PLANE_COLOR) { + Some(e) => self.tiff.uints(e)?, + None => vec![0, 1, 2], + }; + let mut sorted = planes.clone(); + sorted.sort_unstable(); + if sorted != [0, 1, 2] { + return Err(Error::Unsupported("CFA colours other than red, green and blue".into())); + } + if let Some(e) = self.raw_tag(tags::CFA_LAYOUT) + && self.tiff.uint(e)? != 1 + { + return Err(Error::Unsupported("a non-rectangular CFA layout".into())); + } + let colors = pattern[..rows * cols] + .iter() + .map(|&v| planes.iter().position(|&p| p == v).map(|i| i as u8).ok_or(Error::Malformed("CFAPattern colour not in CFAPlaneColor"))) + .collect::>>()?; + Ok(Some(Pattern { rows, cols, colors })) + } + + /// The processing tags of the raw image. + pub fn params(&self) -> Result { + let uint_pair = |tag| -> Result> { + match self.raw_tag(tag) { + Some(e) => match self.tiff.uints(e)?[..] { + [a, b, ..] => Ok(Some((a as usize, b as usize))), + _ => Err(Error::Malformed("short field")), + }, + None => Ok(None), + } + }; + let active_area = match self.raw_tag(tags::ACTIVE_AREA) { + Some(e) => match self.tiff.uints(e)?[..] { + [t, l, b, r, ..] => Some([t, l, b, r].map(|v| usize::try_from(v).unwrap_or(usize::MAX))), + _ => return Err(Error::Malformed("ActiveArea")), + }, + None => None, + }; + let linearization = match self.raw_tag(tags::LINEARIZATION_TABLE) { + Some(e) => { + let v = self.tiff.uints(e)?; + if v.len() > 65_536 { + return Err(Error::Malformed("LinearizationTable longer than 65536")); + } + Some(v.into_iter().map(|x| x.min(65_535) as u16).collect()) + } + None => None, + }; + let pair = |tag| -> Result> { + match self.raw_tag(tag) { + Some(e) => match self.tiff.floats(e)?[..] { + [a, b, ..] => Ok(Some((a, b))), + _ => Err(Error::Malformed("short field")), + }, + None => Ok(None), + } + }; + let list = |tag| -> Result> { self.raw_tag(tag).map(|e| self.tiff.floats(e)).transpose().map(Option::unwrap_or_default) }; + Ok(Params { + active_area, + linearization, + black_repeat: uint_pair(tags::BLACK_LEVEL_REPEAT_DIM)?.unwrap_or((1, 1)), + black: list(tags::BLACK_LEVEL)?, + black_delta_h: list(tags::BLACK_LEVEL_DELTA_H)?, + black_delta_v: list(tags::BLACK_LEVEL_DELTA_V)?, + white: list(tags::WHITE_LEVEL)?, + crop_origin: pair(tags::DEFAULT_CROP_ORIGIN)?, + crop_size: pair(tags::DEFAULT_CROP_SIZE)?, + cfa: self.cfa()?, + }) + } + + /// The colour tags. + pub fn calibration(&self) -> Result { + let illum = |tag| self.main_tag(tag).map(|e| self.tiff.uint(e)).transpose(); + let white_xy = self.floats(self.main_tag(tags::AS_SHOT_WHITE_XY))?.and_then(|v| match v[..] { + [x, y] => Some((x, y)), + _ => None, + }); + Ok(Calibration { + color_matrix: [self.matrix(tags::COLOR_MATRIX_1)?, self.matrix(tags::COLOR_MATRIX_2)?], + camera_calibration: [self.matrix(tags::CAMERA_CALIBRATION_1)?, self.matrix(tags::CAMERA_CALIBRATION_2)?], + forward_matrix: [self.matrix(tags::FORWARD_MATRIX_1)?, self.matrix(tags::FORWARD_MATRIX_2)?], + illuminant: [illum(tags::CALIBRATION_ILLUMINANT_1)?, illum(tags::CALIBRATION_ILLUMINANT_2)?], + analog_balance: self.vec3(tags::ANALOG_BALANCE)?, + as_shot_neutral: self.vec3(tags::AS_SHOT_NEUTRAL)?, + as_shot_white_xy: white_xy, + baseline_exposure: self.floats(self.main_tag(tags::BASELINE_EXPOSURE))?.and_then(|v| v.first().copied()).unwrap_or(0.0), + }) + } + + /// Size, depth and CinemaDNG metadata. + pub fn info(&self) -> Result { + let l = raw::layout(&self.tiff, self.raw_ifd())?; + let p = self.params()?; + let (_, _, aw, ah) = process::active_rect(&p, l.width, l.height)?; + let (_, _, width, height) = process::crop_rect(&p, aw, ah); + let dng_version = self + .main_tag(tags::DNG_VERSION) + .and_then(|e| self.tiff.uints(e).ok()) + .and_then(|v| v.get(..4).map(|v| [0, 1, 2, 3].map(|i| v[i] as u8))) + .unwrap_or_default(); + let frame_rate = self.main_tag(tags::FRAME_RATE).and_then(|e| { + let b = self.tiff.value_bytes(e).ok()?; + if !matches!(e.ty, tiff::Type::SRational | tiff::Type::Rational) { + return None; + } + let n = i64::from(self.tiff.u32_of(b.get(0..4)?.try_into().ok()?) as i32); + let d = i64::from(self.tiff.u32_of(b.get(4..8)?.try_into().ok()?) as i32); + (n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d).then_some((n, d)) + }); + let timecode = self.main_tag(tags::TIME_CODES).and_then(|e| self.tiff.value_bytes(e).ok()).and_then(Timecode::from_bytes); + let camera = self.text(tags::UNIQUE_CAMERA_MODEL).or_else(|| { + let mm = [self.text(tags::MAKE), self.text(tags::MODEL)].into_iter().flatten().collect::>().join(" "); + (!mm.is_empty()).then_some(mm) + }); + Ok(Info { + width, + height, + raw_width: l.width, + raw_height: l.height, + bits: l.bits, + samples_per_pixel: l.samples_per_pixel, + cfa: p.cfa.is_some(), + compression: if l.compression == 7 { "lossless JPEG" } else { "uncompressed" }, + dng_version, + frame_rate, + timecode, + camera, + reel: self.text(tags::REEL_NAME), + }) + } + + /// The raw samples of the main image, as stored (before any processing). + pub fn raw(&self) -> Result { + raw::read(&self.tiff, self.raw_ifd()) + } + + /// Develop the main image to scene-linear Rec. 709. + pub fn develop(&self) -> Result { + let info = self.info()?; + let p = self.params()?; + let raw = self.raw()?; + let calibration = self.calibration()?; + let solved = if p.cfa.is_none() && raw.samples_per_pixel == 1 { + // monochrome: no colour, only the exposure + let e = 2f64.powf(calibration.baseline_exposure.clamp(-16.0, 16.0)); + color::Solved { neutral: [1.0; 3], matrix: color::scale(&color::IDENTITY, e), white_xy: color::D65 } + } else { + calibration.solve()? + }; + let d = process::develop(&raw, &p, &solved.neutral, &solved.matrix)?; + Ok(Image { info, width: d.width, height: d.height, rgba: d.rgba }) + } +} + +/// Parse and develop a DNG file. +pub fn decode(bytes: &[u8]) -> Result { + Dng::parse(bytes)?.develop() +} + +/// Parse a DNG file's metadata. +pub fn probe(bytes: &[u8]) -> Result { + Dng::parse(bytes)?.info() +} diff --git a/crates/raw/src/dng_metadata_source_color.rs b/crates/raw/src/dng_metadata_source_color.rs new file mode 100644 index 000000000..fb02f2c8d --- /dev/null +++ b/crates/raw/src/dng_metadata_source_color.rs @@ -0,0 +1,385 @@ +//! Camera colour to linear Rec. 709, following the DNG specification's "Mapping Camera Color Space +//! to CIE XYZ Space" (DNG 1.4–1.7 chapter 6): +//! +//! - The two calibrations (ColorMatrix, CameraCalibration, ForwardMatrix 1 and 2) are blended +//! linearly in inverse correlated colour temperature between their CalibrationIlluminants. +//! - The white balance is AsShotNeutral (found as a white xy by the specification's iteration) or +//! AsShotWhiteXY; without either, D65. +//! - With ForwardMatrix: CameraToXYZ_D50 = FM · D · (AB · CC)⁻¹, D = diag((AB · CC)⁻¹ · neutral)⁻¹. +//! Without: CameraToXYZ_D50 = CA · (AB · CC · CM)⁻¹, CA a linear Bradford adaptation from the +//! white to D50. +//! - XYZ (D50) goes to linear Rec. 709 / sRGB primaries (D65 white) through Bradford again. +//! +//! The correlated colour temperature of an xy white comes from McCamy's cubic approximation +//! (C. S. McCamy, "Correlated color temperature as an explicit function of chromaticity +//! coordinates", Color Research & Application 17(2), 1992); the illuminant temperatures are the +//! CIE / EXIF 2.3 LightSource values. + +use crate::{Error, Result}; + +pub type M3 = [[f64; 3]; 3]; +pub type V3 = [f64; 3]; + +pub const IDENTITY: M3 = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]; + +/// CIE D50 and D65 white chromaticities (the DNG specification's D50). +pub const D50: (f64, f64) = (0.3457, 0.3585); +pub const D65: (f64, f64) = (0.3127, 0.3290); + +/// Linear Bradford cone response matrix. +const BRADFORD: M3 = [[0.8951, 0.2664, -0.1614], [-0.7502, 1.7135, 0.0367], [0.0389, -0.0685, 1.0296]]; + +pub fn mul(a: &M3, b: &M3) -> M3 { + let mut m = [[0.0; 3]; 3]; + for (i, row) in m.iter_mut().enumerate() { + for (j, v) in row.iter_mut().enumerate() { + *v = (0..3).map(|k| a[i][k] * b[k][j]).sum(); + } + } + m +} + +pub fn mulv(a: &M3, v: &V3) -> V3 { + [0, 1, 2].map(|i| a[i][0] * v[0] + a[i][1] * v[1] + a[i][2] * v[2]) +} + +pub fn diag(v: &V3) -> M3 { + [[v[0], 0.0, 0.0], [0.0, v[1], 0.0], [0.0, 0.0, v[2]]] +} + +pub fn scale(a: &M3, s: f64) -> M3 { + a.map(|r| r.map(|v| v * s)) +} + +fn blend(a: &M3, b: &M3, w: f64) -> M3 { + let mut m = [[0.0; 3]; 3]; + for i in 0..3 { + for j in 0..3 { + m[i][j] = w * a[i][j] + (1.0 - w) * b[i][j]; + } + } + m +} + +/// The inverse of `a`, or `None` when it is (nearly) singular. +pub fn inverse(a: &M3) -> Option { + let c = |r: usize, s: usize| a[(r + 1) % 3][(s + 1) % 3] * a[(r + 2) % 3][(s + 2) % 3] - a[(r + 1) % 3][(s + 2) % 3] * a[(r + 2) % 3][(s + 1) % 3]; + let det = a[0][0] * c(0, 0) + a[0][1] * c(0, 1) + a[0][2] * c(0, 2); + let norm: f64 = a.iter().flatten().map(|v| v.abs()).fold(0.0, f64::max); + if !det.is_finite() || det.abs() <= 1e-12 * norm.powi(3).max(1e-300) { + return None; + } + let mut m = [[0.0; 3]; 3]; + for (i, row) in m.iter_mut().enumerate() { + for (j, v) in row.iter_mut().enumerate() { + *v = c(j, i) / det; + } + } + Some(m) +} + +/// XYZ (Y = 1) of a chromaticity. +pub fn xy_to_xyz((x, y): (f64, f64)) -> V3 { + let y = y.max(1e-6); + [x / y, 1.0, (1.0 - x - y) / y] +} + +pub fn xyz_to_xy(v: &V3) -> Option<(f64, f64)> { + let s = v[0] + v[1] + v[2]; + (s.is_finite() && s > 1e-12).then(|| (v[0] / s, v[1] / s)) +} + +/// Correlated colour temperature (kelvin) of a chromaticity, McCamy (1992), limited to +/// 1 000–50 000 K. +pub fn cct((x, y): (f64, f64)) -> f64 { + let d = 0.1858 - y; + if d.abs() < 1e-9 { + return 50_000.0; + } + let n = (x - 0.3320) / d; + let t = 449.0 * n.powi(3) + 3525.0 * n.powi(2) + 6823.3 * n + 5520.33; + if t.is_finite() { t.clamp(1_000.0, 50_000.0) } else { 50_000.0 } +} + +/// Colour temperature of an EXIF LightSource code used as a DNG CalibrationIlluminant. +pub fn illuminant_kelvin(code: u64) -> Option { + Some(match code { + 1 | 4 | 9 => 5500.0, // daylight, flash, fine weather + 2 | 14 => 4150.0, // fluorescent, cool white fluorescent (W 3800–4500 K) + 3 => 2850.0, // tungsten + 10 => 6500.0, // cloudy + 11 => 7500.0, // shade + 12 => 6430.0, // daylight fluorescent (D 5700–7100 K) + 13 => 5050.0, // day white fluorescent (N 4600–5500 K) + 15 => 3525.0, // white fluorescent (WW 3250–3800 K) + 16 => 2925.0, // warm white fluorescent (L 2600–3250 K) + 17 => 2856.0, // standard light A + 18 => 4874.0, // standard light B + 19 => 6774.0, // standard light C + 20 => 5503.0, // D55 + 21 => 6504.0, // D65 + 22 => 7504.0, // D75 + 23 => 5003.0, // D50 + 24 => 3200.0, // ISO studio tungsten + _ => return None, + }) +} + +/// Linear Bradford adaptation from one white (XYZ) to another. +pub fn bradford(src: &V3, dst: &V3) -> Option { + let s = mulv(&BRADFORD, src); + let d = mulv(&BRADFORD, dst); + if s.iter().any(|v| v.abs() < 1e-12) { + return None; + } + let g = [d[0] / s[0], d[1] / s[1], d[2] / s[2]]; + Some(mul(&inverse(&BRADFORD)?, &mul(&diag(&g), &BRADFORD))) +} + +/// Linear RGB (Rec. 709 / sRGB primaries, D65 white) → XYZ. +pub fn rec709_to_xyz() -> Option { + let p = [(0.64, 0.33), (0.30, 0.60), (0.15, 0.06)].map(xy_to_xyz); + let pm: M3 = [[p[0][0], p[1][0], p[2][0]], [p[0][1], p[1][1], p[2][1]], [p[0][2], p[1][2], p[2][2]]]; + let s = mulv(&inverse(&pm)?, &xy_to_xyz(D65)); + Some(mul(&pm, &diag(&s))) +} + +/// XYZ relative to D50 → linear Rec. 709. +pub fn xyz_d50_to_rec709() -> Option { + let to_d65 = bradford(&xy_to_xyz(D50), &xy_to_xyz(D65))?; + Some(mul(&inverse(&rec709_to_xyz()?)?, &to_d65)) +} + +/// The colour tags of a three-colour DNG. +#[derive(Clone, Debug, Default)] +pub struct Calibration { + pub color_matrix: [Option; 2], + pub camera_calibration: [Option; 2], + pub forward_matrix: [Option; 2], + /// EXIF LightSource codes of the two calibrations. + pub illuminant: [Option; 2], + pub analog_balance: Option, + pub as_shot_neutral: Option, + pub as_shot_white_xy: Option<(f64, f64)>, + pub baseline_exposure: f64, +} + +/// The result of [`Calibration::solve`]. +#[derive(Clone, Debug, PartialEq)] +pub struct Solved { + /// Camera neutral: raw (normalised) camera values of a white surface. Dividing by it white + /// balances the camera values. + pub neutral: V3, + /// White-balanced camera values → linear Rec. 709, including the baseline exposure. The white + /// balanced white (1, 1, 1) goes to Y = 2^BaselineExposure. + pub matrix: M3, + /// The white point found, for information. + pub white_xy: (f64, f64), +} + +impl Calibration { + /// Weight of calibration 1 for a white of temperature `t` (1 when only one is usable). + fn weight(&self, t: f64) -> f64 { + let (Some(_), Some(_)) = (self.color_matrix[0], self.color_matrix[1]) else { return 1.0 }; + let (Some(t1), Some(t2)) = (self.illuminant[0].and_then(illuminant_kelvin), self.illuminant[1].and_then(illuminant_kelvin)) else { + return 1.0; + }; + if (t1 - t2).abs() < 1.0 { + return 1.0; + } + // the formula needs t1 < t2; swap the roles otherwise + let (lo, hi, lo_is_1) = if t1 < t2 { (t1, t2, true) } else { (t2, t1, false) }; + let w_lo = if t <= lo { + 1.0 + } else if t >= hi { + 0.0 + } else { + (1.0 / t - 1.0 / hi) / (1.0 / lo - 1.0 / hi) + }; + if lo_is_1 { w_lo } else { 1.0 - w_lo } + } + + fn pick(&self, m: &[Option; 2], w: f64, default: M3) -> M3 { + match (m[0], m[1]) { + (Some(a), Some(b)) => blend(&a, &b, w), + (Some(a), None) => a, + (None, Some(b)) if self.color_matrix[0].is_none() => b, + _ => default, + } + } + + /// XYZ → camera at weight `w`: AB · CC · CM. + fn xyz_to_camera(&self, w: f64) -> Option { + let cm = match self.color_matrix { + [None, None] => return None, + _ => self.pick(&self.color_matrix, w, IDENTITY), + }; + let cc = self.pick(&self.camera_calibration, w, IDENTITY); + let ab = diag(&self.analog_balance.unwrap_or([1.0; 3])); + Some(mul(&ab, &mul(&cc, &cm))) + } + + /// Work out the white balance and the camera → linear Rec. 709 matrix. + pub fn solve(&self) -> Result { + let out_matrix = xyz_d50_to_rec709().ok_or(Error::Malformed("colour maths"))?; + let exposure = 2f64.powf(self.baseline_exposure.clamp(-16.0, 16.0)); + if self.color_matrix.iter().all(Option::is_none) { + // no colour calibration: the camera values are taken as Rec. 709, white balanced + let neutral = self.as_shot_neutral.unwrap_or([1.0; 3]); + if neutral.iter().any(|v| !v.is_finite() || *v <= 1e-9) { + return Err(Error::Malformed("camera neutral must be positive")); + } + return Ok(Solved { neutral, matrix: scale(&IDENTITY, exposure), white_xy: D65 }); + } + let bad = || Error::Malformed("singular colour matrix"); + // the white point and the camera neutral + let (white_xy, neutral) = if let Some(n) = self.as_shot_neutral { + let mut xy = D50; + for _ in 0..50 { + let w = self.weight(cct(xy)); + let to_xyz = inverse(&self.xyz_to_camera(w).ok_or_else(bad)?).ok_or_else(bad)?; + let next = xyz_to_xy(&mulv(&to_xyz, &n)).ok_or(Error::Malformed("AsShotNeutral is not a colour"))?; + let done = (next.0 - xy.0).abs() + (next.1 - xy.1).abs() < 1e-9; + xy = next; + if done { + break; + } + } + (xy, n) + } else { + let xy = self.as_shot_white_xy.unwrap_or(D65); + let w = self.weight(cct(xy)); + let n = mulv(&self.xyz_to_camera(w).ok_or_else(bad)?, &xy_to_xyz(xy)); + (xy, n) + }; + if neutral.iter().any(|v| !v.is_finite() || *v <= 1e-9) { + return Err(Error::Malformed("camera neutral must be positive")); + } + let w = self.weight(cct(white_xy)); + let ab_cc = mul(&diag(&self.analog_balance.unwrap_or([1.0; 3])), &self.pick(&self.camera_calibration, w, IDENTITY)); + let camera_to_xyz_d50 = if self.forward_matrix.iter().any(Option::is_some) { + let fm = self.pick(&self.forward_matrix, w, IDENTITY); + let inv_ab_cc = inverse(&ab_cc).ok_or_else(bad)?; + let reference = mulv(&inv_ab_cc, &neutral); + if reference.iter().any(|v| v.abs() < 1e-12) { + return Err(bad()); + } + mul(&fm, &mul(&diag(&reference.map(|v| 1.0 / v)), &inv_ab_cc)) + } else { + let to_xyz = inverse(&self.xyz_to_camera(w).ok_or_else(bad)?).ok_or_else(bad)?; + let ca = bradford(&xy_to_xyz(white_xy), &xy_to_xyz(D50)).ok_or_else(bad)?; + mul(&ca, &to_xyz) + }; + // white-balanced camera values: camera = neutral · wb + let wb_to_xyz = mul(&camera_to_xyz_d50, &diag(&neutral)); + let y = mulv(&wb_to_xyz, &[1.0; 3])[1]; + if !y.is_finite() || y <= 1e-9 { + return Err(bad()); + } + let matrix = scale(&mul(&out_matrix, &wb_to_xyz), exposure / y); + if matrix.iter().flatten().any(|v| !v.is_finite()) { + return Err(bad()); + } + Ok(Solved { neutral, matrix, white_xy }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn close(a: &M3, b: &M3, tol: f64) -> bool { + a.iter().flatten().zip(b.iter().flatten()).all(|(x, y)| (x - y).abs() <= tol) + } + + #[test] + fn rec709_matrix_matches_the_published_one() { + // IEC 61966-2-1 (sRGB) linear RGB → XYZ, four decimals + let m = rec709_to_xyz().unwrap(); + let want = [[0.4124, 0.3576, 0.1805], [0.2126, 0.7152, 0.0722], [0.0193, 0.1192, 0.9505]]; + assert!(close(&m, &want, 1e-3), "{m:?}"); + } + + #[test] + fn inverse_round_trips_and_refuses_singular() { + let a = [[2.0, 1.0, 0.5], [0.1, 3.0, 0.2], [0.3, 0.4, 1.5]]; + assert!(close(&mul(&a, &inverse(&a).unwrap()), &IDENTITY, 1e-12)); + assert!(inverse(&[[1.0, 2.0, 3.0], [2.0, 4.0, 6.0], [0.0, 0.0, 1.0]]).is_none()); + assert!(inverse(&[[f64::NAN; 3]; 3]).is_none()); + } + + #[test] + fn mccamy_temperatures() { + assert!((cct(D65) - 6504.0).abs() < 15.0, "{}", cct(D65)); + assert!((cct(D50) - 5003.0).abs() < 15.0, "{}", cct(D50)); + assert!((cct((0.44757, 0.40745)) - 2856.0).abs() < 15.0, "illuminant A"); + assert_eq!(cct((0.3320, 0.1858)), 50_000.0, "no division by zero"); + } + + #[test] + fn interpolation_weight_follows_inverse_temperature() { + let c = Calibration { color_matrix: [Some(IDENTITY), Some(IDENTITY)], illuminant: [Some(17), Some(21)], ..Default::default() }; + assert_eq!(c.weight(2000.0), 1.0); + assert_eq!(c.weight(9000.0), 0.0); + let mid = c.weight(4000.0); + let want = (1.0 / 4000.0 - 1.0 / 6504.0) / (1.0 / 2856.0 - 1.0 / 6504.0); + assert!((mid - want).abs() < 1e-12); + // swapped order: weight of calibration 1 (now D65) + let s = Calibration { illuminant: [Some(21), Some(17)], ..c.clone() }; + assert!((s.weight(4000.0) - (1.0 - want)).abs() < 1e-12); + // unknown illuminant: calibration 1 only + let u = Calibration { illuminant: [Some(0), Some(21)], ..c }; + assert_eq!(u.weight(4000.0), 1.0); + } + + #[test] + fn an_srgb_camera_maps_to_itself() { + // ColorMatrix = XYZ → linear sRGB, white D65: the camera is Rec. 709 already + let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); + let c = Calibration { color_matrix: [Some(cm), None], illuminant: [Some(21), None], as_shot_neutral: Some([1.0; 3]), ..Default::default() }; + let s = c.solve().unwrap(); + assert!(close(&s.matrix, &IDENTITY, 1e-6), "{:?}", s.matrix); + assert!((s.white_xy.0 - D65.0).abs() < 1e-4 && (s.white_xy.1 - D65.1).abs() < 1e-4, "{:?}", s.white_xy); + // a baseline exposure of +1 doubles everything + let e = Calibration { baseline_exposure: 1.0, ..c }.solve().unwrap(); + assert!(close(&e.matrix, &scale(&IDENTITY, 2.0), 1e-6)); + } + + #[test] + fn forward_matrix_white_balances_by_the_neutral() { + // FM = linear Rec. 709 → XYZ D50: the white-balanced camera values come out unchanged + let fm = mul(&bradford(&xy_to_xyz(D65), &xy_to_xyz(D50)).unwrap(), &rec709_to_xyz().unwrap()); + let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); + let c = Calibration { + color_matrix: [Some(cm), None], + forward_matrix: [Some(fm), None], + illuminant: [Some(21), None], + as_shot_neutral: Some([0.5, 1.0, 0.8]), + ..Default::default() + }; + let s = c.solve().unwrap(); + assert_eq!(s.neutral, [0.5, 1.0, 0.8]); + assert!(close(&s.matrix, &IDENTITY, 1e-6), "{:?}", s.matrix); + } + + #[test] + fn white_xy_gives_the_neutral() { + let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); + let c = Calibration { color_matrix: [Some(cm), None], illuminant: [Some(21), None], as_shot_white_xy: Some(D65), ..Default::default() }; + let s = c.solve().unwrap(); + for v in s.neutral { + assert!((v - s.neutral[1]).abs() < 1e-3, "D65 is neutral for an sRGB camera: {:?}", s.neutral); + } + } + + #[test] + fn hostile_calibrations_are_errors() { + let zero = Calibration { color_matrix: [Some([[0.0; 3]; 3]), None], as_shot_neutral: Some([1.0; 3]), ..Default::default() }; + assert!(zero.solve().is_err()); + let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); + let neg = Calibration { color_matrix: [Some(cm), None], as_shot_neutral: Some([-1.0, 1.0, 1.0]), ..Default::default() }; + assert!(neg.solve().is_err()); + let none = Calibration { as_shot_neutral: Some([0.5, 1.0, 0.5]), ..Default::default() }.solve().unwrap(); + assert_eq!(none.matrix, IDENTITY); + } +} diff --git a/crates/raw/src/dng_metadata_source_demosaic.rs b/crates/raw/src/dng_metadata_source_demosaic.rs new file mode 100644 index 000000000..911236929 --- /dev/null +++ b/crates/raw/src/dng_metadata_source_demosaic.rs @@ -0,0 +1,255 @@ +//! Demosaicing: one colour sample per pixel (a colour filter array) to three. +//! +//! - A 2 × 2 Bayer pattern (one red, two green, one blue) uses the gradient-corrected linear +//! interpolation of H. S. Malvar, L.-W. He and R. Cutler, "High-quality linear interpolation for +//! demosaicing of Bayer-patterned color images", IEEE ICASSP 2004: bilinear interpolation plus a +//! Laplacian correction from the pixel's own colour, as 5 × 5 filters. +//! - Any other pattern (up to 8 × 8) uses bilinear interpolation: the mean of the nearest samples +//! of each missing colour in the 3 × 3 neighbourhood, else the 5 × 5 one. +//! +//! The picture is extended by two pixels on every side by mirroring (without repeating the edge), +//! which keeps a 2 × 2 pattern's phase. + +#[cfg(feature = "threads")] +use rayon::prelude::*; + +/// A CFA pattern: `rows` × `cols` colour indices (0 red, 1 green, 2 blue), origin at the top left +/// of the image. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Pattern { + pub rows: usize, + pub cols: usize, + pub colors: Vec, +} + +impl Pattern { + pub fn color(&self, x: usize, y: usize) -> u8 { + self.colors.get((y % self.rows) * self.cols + x % self.cols).copied().unwrap_or(1) + } + + /// Whether this is a 2 × 2 Bayer pattern. + pub fn is_bayer(&self) -> bool { + if (self.rows, self.cols) != (2, 2) { + return false; + } + let [a, b, c, d] = self.colors[..] else { return false }; + let count = |k: u8| [a, b, c, d].iter().filter(|&&v| v == k).count(); + // the two greens sit on a diagonal + count(0) == 1 && count(1) == 2 && count(2) == 1 && ((a == 1 && d == 1) || (b == 1 && c == 1)) + } +} + +const PAD: usize = 2; + +/// Mirror index `i` (which may be up to PAD outside) into 0..n. +fn mirror(i: isize, n: usize) -> usize { + let n = n as isize; + let mut i = i; + if i < 0 { + i = -i; + } + if i >= n { + i = 2 * (n - 1) - i; + } + i.clamp(0, n - 1) as usize +} + +/// The plane extended by PAD mirrored pixels on each side. +fn padded(plane: &[f32], w: usize, h: usize) -> (Vec, usize) { + let pw = w + 2 * PAD; + let ph = h + 2 * PAD; + let mut out = vec![0f32; pw * ph]; + for y in 0..ph { + let sy = mirror(y as isize - PAD as isize, h); + for x in 0..pw { + let sx = mirror(x as isize - PAD as isize, w); + out[y * pw + x] = plane.get(sy * w + sx).copied().unwrap_or(0.0); + } + } + (out, pw) +} + +/// Demosaic `plane` (`w` × `h`, one sample per pixel) into interleaved RGB. +pub fn demosaic(plane: &[f32], w: usize, h: usize, pattern: &Pattern) -> Vec { + let mut out = vec![0f32; w * h * 3]; + if w == 0 || h == 0 || plane.len() < w * h { + return out; + } + let (p, pw) = padded(plane, w, h); + let bayer = pattern.is_bayer() && w >= 3 && h >= 3; + let row = |y: usize, o: &mut [f32]| { + if bayer { + malvar_row(&p, pw, y, w, pattern, o); + } else { + bilinear_row(&p, pw, y, w, h, pattern, o); + } + }; + #[cfg(feature = "threads")] + out.par_chunks_mut(w * 3).enumerate().for_each(|(y, o)| row(y, o)); + #[cfg(not(feature = "threads"))] + out.chunks_mut(w * 3).enumerate().for_each(|(y, o)| row(y, o)); + out +} + +fn malvar_row(p: &[f32], pw: usize, y: usize, w: usize, pat: &Pattern, out: &mut [f32]) { + let py = y + PAD; + for x in 0..w { + let px = x + PAD; + let at = |dx: isize, dy: isize| p[((py as isize + dy) as usize) * pw + (px as isize + dx) as usize]; + let c = at(0, 0); + let cross1 = at(-1, 0) + at(1, 0) + at(0, -1) + at(0, 1); + let cross2 = at(-2, 0) + at(2, 0) + at(0, -2) + at(0, 2); + let diag = at(-1, -1) + at(1, -1) + at(-1, 1) + at(1, 1); + let me = pat.color(x, y); + let rgb = match me { + 1 => { + let h1 = at(-1, 0) + at(1, 0); + let h2 = at(-2, 0) + at(2, 0); + let v1 = at(0, -1) + at(0, 1); + let v2 = at(0, -2) + at(0, 2); + // colour along the row and along the column + let along_row = (5.0 * c + 4.0 * h1 - h2 - diag + 0.5 * v2) / 8.0; + let along_col = (5.0 * c + 4.0 * v1 - v2 - diag + 0.5 * h2) / 8.0; + if pat.color(x + 1, y) == 0 { [along_row, c, along_col] } else { [along_col, c, along_row] } + } + _ => { + let g = (4.0 * c + 2.0 * cross1 - cross2) / 8.0; + let other = (6.0 * c + 2.0 * diag - 1.5 * cross2) / 8.0; + if me == 0 { [c, g, other] } else { [other, g, c] } + } + }; + if let Some(o) = out.get_mut(x * 3..x * 3 + 3) { + o.copy_from_slice(&rgb); + } + } +} + +fn bilinear_row(p: &[f32], pw: usize, y: usize, w: usize, h: usize, pat: &Pattern, out: &mut [f32]) { + for x in 0..w { + let me = pat.color(x, y); + let mut rgb = [0f32; 3]; + for (k, v) in rgb.iter_mut().enumerate() { + if usize::from(me) == k { + *v = p[(y + PAD) * pw + x + PAD]; + continue; + } + for r in 1..=PAD as isize { + let (mut sum, mut n) = (0f32, 0u32); + for dy in -r..=r { + for dx in -r..=r { + // the colour of a mirrored neighbour is the colour where it was mirrored from + let sx = mirror(x as isize + dx, w); + let sy = mirror(y as isize + dy, h); + if usize::from(pat.color(sx, sy)) == k { + sum += p[(sy + PAD) * pw + sx + PAD]; + n += 1; + } + } + } + if n > 0 { + *v = sum / n as f32; + break; + } + } + } + if let Some(o) = out.get_mut(x * 3..x * 3 + 3) { + o.copy_from_slice(&rgb); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn rggb() -> Pattern { + Pattern { rows: 2, cols: 2, colors: vec![0, 1, 1, 2] } + } + + /// A mosaic of an RGB picture given per pixel. + fn mosaic(w: usize, h: usize, pat: &Pattern, rgb: impl Fn(usize, usize) -> [f32; 3]) -> Vec { + (0..h).flat_map(|y| (0..w).map(move |x| (x, y))).map(|(x, y)| rgb(x, y)[usize::from(pat.color(x, y))]).collect() + } + + #[test] + fn bayer_detection() { + assert!(rggb().is_bayer()); + for c in [[1, 0, 2, 1], [1, 2, 0, 1], [2, 1, 1, 0]] { + assert!(Pattern { rows: 2, cols: 2, colors: c.to_vec() }.is_bayer(), "{c:?}"); + } + assert!(!Pattern { rows: 2, cols: 2, colors: vec![0, 1, 2, 1] }.is_bayer(), "greens side by side"); + assert!(!Pattern { rows: 2, cols: 2, colors: vec![0, 0, 1, 2] }.is_bayer()); + assert!(!Pattern { rows: 1, cols: 3, colors: vec![0, 1, 2] }.is_bayer()); + } + + #[test] + fn flat_fields_stay_flat_for_every_bayer_phase_and_other_patterns() { + let pats = [[0, 1, 1, 2], [1, 0, 2, 1], [1, 2, 0, 1], [2, 1, 1, 0]].map(|c| Pattern { rows: 2, cols: 2, colors: c.to_vec() }); + let stripes = Pattern { rows: 1, cols: 3, colors: vec![0, 1, 2] }; + for pat in pats.iter().chain([&stripes]) { + let (w, h) = (9, 7); + let m = mosaic(w, h, pat, |_, _| [0.25, 0.5, 0.75]); + let out = demosaic(&m, w, h, pat); + for (i, px) in out.chunks(3).enumerate() { + for (k, want) in [0.25, 0.5, 0.75].iter().enumerate() { + assert!((px[k] - want).abs() < 1e-6, "{pat:?} pixel {i} channel {k}: {}", px[k]); + } + } + } + } + + #[test] + fn known_samples_are_kept_and_linear_ramps_are_exact() { + // a horizontal ramp is reconstructed exactly away from the border (all filters are + // symmetric and sum to one, the correction terms sum to zero) + let pat = rggb(); + let (w, h) = (16, 12); + let f = |x: usize, _y: usize| { + let v = x as f32 / 16.0; + [v, 0.5 * v + 0.1, 0.2 + 0.25 * v] + }; + let m = mosaic(w, h, &pat, f); + let out = demosaic(&m, w, h, &pat); + for y in 0..h { + for x in 0..w { + let px = &out[(y * w + x) * 3..][..3]; + let own = usize::from(pat.color(x, y)); + assert_eq!(px[own], m[y * w + x], "own sample kept"); + if (2..w - 2).contains(&x) { + let want = f(x, y); + for k in 0..3 { + assert!((px[k] - want[k]).abs() < 1e-5, "({x},{y}) ch {k}: {} vs {}", px[k], want[k]); + } + } + } + } + } + + #[test] + fn a_vertical_edge_does_not_smear_far() { + // white left half, black right half: pixels two columns from the edge are untouched + let pat = rggb(); + let (w, h) = (12, 8); + let m = mosaic(w, h, &pat, |x, _| if x < 6 { [1.0; 3] } else { [0.0; 3] }); + let out = demosaic(&m, w, h, &pat); + for y in 0..h { + for x in (0..3).chain(9..12) { + let want = if x < 6 { 1.0 } else { 0.0 }; + for k in 0..3 { + assert!((out[(y * w + x) * 3 + k] - want).abs() < 1e-6, "({x},{y}) ch {k}"); + } + } + } + } + + #[test] + fn tiny_and_empty_pictures_do_not_panic() { + let pat = rggb(); + for (w, h) in [(0, 0), (1, 1), (2, 1), (1, 5), (2, 2), (3, 2)] { + let m = vec![0.5; w * h]; + let out = demosaic(&m, w, h, &pat); + assert_eq!(out.len(), w * h * 3); + } + assert_eq!(demosaic(&[0.5; 3], 4, 4, &pat).len(), 48, "short plane"); + } +} diff --git a/crates/raw/src/dng_metadata_source_process.rs b/crates/raw/src/dng_metadata_source_process.rs new file mode 100644 index 000000000..41d9b25ae --- /dev/null +++ b/crates/raw/src/dng_metadata_source_process.rs @@ -0,0 +1,264 @@ +//! Raw samples to a linear Rec. 709 picture, in the order of the DNG specification's chapter 5 +//! ("Mapping Raw Values to Linear Reference Values") and chapter 6 (colour): +//! +//! 1. ActiveArea: only the active (unmasked) sensor area is used; black levels, the CFA pattern +//! and the default crop are relative to its top left corner. +//! 2. LinearizationTable maps each sample. +//! 3. The black level (BlackLevel over its BlackLevelRepeatDim pattern, plus BlackLevelDeltaH for +//! the column and BlackLevelDeltaV for the row) is subtracted and the range up to WhiteLevel +//! scaled to 0–1 (clipped there). +//! 4. White balance: each sample is divided by the camera neutral of its colour and clipped at +//! 1, so highlights the sensor clipped stay neutral. +//! 5. A CFA is demosaiced ([`crate::demosaic`]). +//! 6. DefaultCropOrigin / DefaultCropSize cut the picture. +//! 7. The colour matrix of [`crate::color`] takes the white-balanced camera values to linear +//! Rec. 709 (including BaselineExposure); negative results are clipped to 0. + +#[cfg(feature = "threads")] +use rayon::prelude::*; + +use crate::color::{M3, V3}; +use crate::demosaic::{Pattern, demosaic}; +use crate::raw::RawImage; +use crate::{Error, Result}; + +/// The processing tags of a raw image. +#[derive(Clone, Debug, Default)] +pub struct Params { + /// top, left, bottom, right + pub active_area: Option<[usize; 4]>, + pub linearization: Option>, + /// rows, columns of the BlackLevel pattern + pub black_repeat: (usize, usize), + /// rows × columns × samples per pixel values + pub black: Vec, + pub black_delta_h: Vec, + pub black_delta_v: Vec, + /// per sample + pub white: Vec, + pub crop_origin: Option<(f64, f64)>, + pub crop_size: Option<(f64, f64)>, + /// CFA pattern in camera planes (`None`: LinearRaw). + pub cfa: Option, +} + +/// A rectangle: x, y, width, height. +type Rect = (usize, usize, usize, usize); + +/// The active area of a `w` × `h` raw image. +pub fn active_rect(p: &Params, w: usize, h: usize) -> Result { + match p.active_area { + None => Ok((0, 0, w, h)), + Some([top, left, bottom, right]) => { + if top >= bottom || left >= right || bottom > h || right > w { + return Err(Error::Malformed("ActiveArea outside the image")); + } + Ok((left, top, right - left, bottom - top)) + } + } +} + +/// The default crop inside an active area of `aw` × `ah`. +pub fn crop_rect(p: &Params, aw: usize, ah: usize) -> Rect { + let (ox, oy) = p.crop_origin.unwrap_or((0.0, 0.0)); + let (sw, sh) = p.crop_size.unwrap_or((aw as f64, ah as f64)); + let to = |v: f64, max: usize| if v.is_finite() && v > 0.0 { (v.min(max as f64)) as usize } else { 0 }; + let x = to(ox.floor(), aw.saturating_sub(1)); + let y = to(oy.floor(), ah.saturating_sub(1)); + let w = to(sw.round(), aw - x).max(1); + let h = to(sh.round(), ah - y).max(1); + (x, y, w, h) +} + +/// Normalised, white-balanced samples of the active area (steps 1–4), one plane per sample. +fn normalise(raw: &RawImage, p: &Params, (ax, ay, aw, ah): Rect, neutral: &V3, planes: usize) -> Result> { + let spp = raw.samples_per_pixel; + let (br, bc) = (p.black_repeat.0.max(1), p.black_repeat.1.max(1)); + if br > 64 || bc > 64 { + return Err(Error::Unsupported("a BlackLevelRepeatDim larger than 64".into())); + } + let need = br * bc * spp; + let black: Vec = match p.black.len() { + 0 => vec![0.0; need], + 1 => vec![p.black[0]; need], + n if n >= need => p.black[..need].to_vec(), + _ => return Err(Error::Malformed("BlackLevel has too few values")), + }; + let max_code = ((1u32 << raw.bits) - 1) as f64; + let white: Vec = (0..spp).map(|s| p.white.get(s).or(p.white.first()).copied().unwrap_or(max_code)).collect(); + let lin = p.linearization.as_deref().filter(|t| !t.is_empty()); + let inv_neutral: Vec = (0..planes).map(|c| (1.0 / neutral.get(c).copied().unwrap_or(1.0)) as f32).collect(); + let cfa = p.cfa.as_ref(); + let mut out = vec![0f32; aw * ah * spp]; + let row = |y: usize, o: &mut [f32]| { + let dv = p.black_delta_v.get(y).copied().unwrap_or(0.0); + let src_row = (ay + y) * raw.width * spp; + for x in 0..aw { + let dh = p.black_delta_h.get(x).copied().unwrap_or(0.0); + for s in 0..spp { + let code = raw.data.get(src_row + (ax + x) * spp + s).copied().unwrap_or(0); + let v = match lin { + Some(t) => f64::from(t.get(usize::from(code)).or(t.last()).copied().unwrap_or(0)), + None => f64::from(code), + }; + let b = black[((y % br) * bc + x % bc) * spp + s] + dh + dv; + let range = white[s] - b; + let n = if range > 0.0 { ((v - b) / range).clamp(0.0, 1.0) } else { 0.0 }; + let plane = match cfa { + Some(c) => usize::from(c.color(x, y)), + None => s, + }; + let wb = inv_neutral.get(plane).copied().unwrap_or(1.0); + if let Some(o) = o.get_mut(x * spp + s) { + *o = (n as f32 * wb).min(1.0); + } + } + } + }; + #[cfg(feature = "threads")] + out.par_chunks_mut((aw * spp).max(1)).enumerate().for_each(|(y, o)| row(y, o)); + #[cfg(not(feature = "threads"))] + out.chunks_mut((aw * spp).max(1)).enumerate().for_each(|(y, o)| row(y, o)); + Ok(out) +} + +/// A developed picture: premultiplied linear Rec. 709 RGBA floats (alpha 1). +pub struct Developed { + pub width: usize, + pub height: usize, + pub rgba: Vec, +} + +/// Develop `raw` (steps 1–7). `neutral` and `matrix` come from [`crate::color::Calibration::solve`]. +pub fn develop(raw: &RawImage, p: &Params, neutral: &V3, matrix: &M3) -> Result { + let spp = raw.samples_per_pixel; + if p.cfa.is_some() && spp != 1 { + return Err(Error::Malformed("a CFA image with more than one sample per pixel")); + } + if p.cfa.is_none() && !matches!(spp, 1 | 3) { + return Err(Error::Unsupported(format!("LinearRaw with {spp} samples per pixel"))); + } + let active = active_rect(p, raw.width, raw.height)?; + let (_, _, aw, ah) = active; + let planes = if p.cfa.is_some() { 3 } else { spp }; + let norm = normalise(raw, p, active, neutral, planes)?; + // three samples per pixel + let rgb = match (&p.cfa, spp) { + (Some(pat), _) => demosaic(&norm, aw, ah, pat), + (None, 3) => norm, + _ => norm.iter().flat_map(|&v| [v, v, v]).collect(), + }; + let (cx, cy, cw, ch) = crop_rect(p, aw, ah); + let m: [[f32; 3]; 3] = matrix.map(|r| r.map(|v| v as f32)); + let mono = p.cfa.is_none() && spp == 1; + let mut rgba = vec![0f32; cw * ch * 4]; + let row = |y: usize, o: &mut [f32]| { + let src = (cy + y) * aw + cx; + for x in 0..cw { + let Some(c) = rgb.get((src + x) * 3..(src + x) * 3 + 3) else { continue }; + let px = if mono { + // grey: the matrix's luminance gain only + let g = c[0] * (m[1][0] + m[1][1] + m[1][2]); + [g, g, g] + } else { + [0, 1, 2].map(|i| m[i][0] * c[0] + m[i][1] * c[1] + m[i][2] * c[2]) + }; + if let Some(d) = o.get_mut(x * 4..x * 4 + 4) { + d.copy_from_slice(&[px[0].max(0.0), px[1].max(0.0), px[2].max(0.0), 1.0]); + } + } + }; + #[cfg(feature = "threads")] + rgba.par_chunks_mut(cw * 4).enumerate().for_each(|(y, o)| row(y, o)); + #[cfg(not(feature = "threads"))] + rgba.chunks_mut(cw * 4).enumerate().for_each(|(y, o)| row(y, o)); + Ok(Developed { width: cw, height: ch, rgba }) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::color::IDENTITY; + + fn raw(w: usize, h: usize, spp: usize, data: Vec) -> RawImage { + RawImage { width: w, height: h, samples_per_pixel: spp, bits: 12, data } + } + + #[test] + fn black_white_linearisation_and_white_balance() { + // LinearRaw 2 × 1: black 100, white 1100; neutral (0.5, 1, 0.25) + let r = raw(2, 1, 3, vec![600, 600, 600, 100, 1100, 2000]); + let p = Params { black: vec![100.0], white: vec![1100.0], ..Default::default() }; + let d = develop(&r, &p, &[0.5, 1.0, 0.25], &IDENTITY).unwrap(); + assert_eq!((d.width, d.height), (2, 1)); + // 0.5 / 0.5 = 1, 0.5 / 1, 0.5 / 0.25 = 2 → clipped to 1 + assert_eq!(&d.rgba[..4], &[1.0, 0.5, 1.0, 1.0]); + // below black → 0, at white → 1, above white → 1 + assert_eq!(&d.rgba[4..], &[0.0, 1.0, 1.0, 1.0]); + // a linearisation table doubling the codes + let t: Vec = (0..4096u32).map(|v| (v * 2).min(65_535) as u16).collect(); + let p = Params { linearization: Some(t), black: vec![200.0], white: vec![2200.0], ..p }; + let d = develop(&r, &p, &[1.0; 3], &IDENTITY).unwrap(); + assert_eq!(&d.rgba[..3], &[0.5, 0.5, 0.5]); + } + + #[test] + fn per_cfa_black_levels_and_deltas() { + // 2 × 2 CFA, black repeat 2 × 2 with a different black per site, plus row/column deltas + let pat = Pattern { rows: 2, cols: 2, colors: vec![0, 1, 1, 2] }; + let p = Params { + black_repeat: (2, 2), + black: vec![10.0, 20.0, 30.0, 40.0], + black_delta_h: vec![0.0, 5.0], + black_delta_v: vec![0.0, 1.0], + white: vec![1010.0], + cfa: Some(pat), + ..Default::default() + }; + let r = raw(2, 2, 1, vec![510, 525, 531, 546]); + let n = normalise(&r, &p, (0, 0, 2, 2), &[1.0; 3], 3).unwrap(); + // (v - black) / (white - black) for black 10, 25, 31, 46 + let want = [500.0 / 1000.0, 500.0 / 985.0, 500.0 / 979.0, 500.0 / 964.0]; + for (a, b) in n.iter().zip(want) { + assert!((a - b).abs() < 1e-6, "{a} vs {b}"); + } + } + + #[test] + fn active_area_and_default_crop() { + // 6 × 4 LinearRaw grey ramp; active area cuts a 1-pixel border, crop takes 2 × 1 at (1, 1) + let data: Vec = (0..24u16).flat_map(|i| [i * 10; 3]).collect(); + let r = raw(6, 4, 3, data); + let p = + Params { active_area: Some([1, 1, 3, 5]), white: vec![4095.0], crop_origin: Some((1.0, 1.0)), crop_size: Some((2.0, 1.0)), ..Default::default() }; + let d = develop(&r, &p, &[1.0; 3], &IDENTITY).unwrap(); + assert_eq!((d.width, d.height), (2, 1)); + // active (1,1)–(5,3); crop origin (1,1) inside it → raw (2, 2) and (3, 2): samples 14, 15 + assert!((d.rgba[0] - 140.0 / 4095.0).abs() < 1e-6); + assert!((d.rgba[4] - 150.0 / 4095.0).abs() < 1e-6); + // hostile active areas and crops + let bad = Params { active_area: Some([3, 0, 1, 6]), ..Default::default() }; + assert!(develop(&r, &bad, &[1.0; 3], &IDENTITY).is_err()); + let bad = Params { active_area: Some([0, 0, 4, 7]), ..Default::default() }; + assert!(develop(&r, &bad, &[1.0; 3], &IDENTITY).is_err()); + let huge = Params { crop_origin: Some((1e12, -5.0)), crop_size: Some((f64::NAN, 1e9)), white: vec![4095.0], ..Default::default() }; + let d = develop(&r, &huge, &[1.0; 3], &IDENTITY).unwrap(); + assert!(d.width >= 1 && d.width <= 6 && d.height >= 1 && d.height <= 4); + } + + #[test] + fn matrix_and_clipping_of_negatives() { + let r = raw(1, 1, 3, vec![4095, 0, 0]); + let p = Params { white: vec![4095.0], ..Default::default() }; + let m = [[1.0, 0.0, 0.0], [-0.5, 1.0, 0.0], [0.0, 0.0, 2.0]]; + let d = develop(&r, &p, &[1.0; 3], &m).unwrap(); + assert_eq!(&d.rgba, &[1.0, 0.0, 0.0, 1.0]); + } + + #[test] + fn short_black_level_is_an_error() { + let r = raw(2, 2, 1, vec![0; 4]); + let p = Params { black_repeat: (2, 2), black: vec![1.0, 2.0], cfa: Some(Pattern { rows: 2, cols: 2, colors: vec![0, 1, 1, 2] }), ..Default::default() }; + assert!(develop(&r, &p, &[1.0; 3], &IDENTITY).is_err()); + } +} From 79c6602a357154b374d024d76b09c64db41c93ce Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 11 Oct 2026 15:44:00 +0200 Subject: [PATCH 09/25] #1065: clean-room DNG / CinemaDNG decoder crate Problem: camera RAW is at 0% (docs/codec-parity.md, #383). CinemaDNG, the one cinema RAW format with a public specification, could not be read at all. Fix: new L0 crate filmcraft-dng, written from the public DNG 1.4-1.7, TIFF 6.0 / TIFF-EP, ITU-T T.81 and CinemaDNG specifications only (no other decoder's source read; editions in the crate README): - TIFF container with SubIFDs, bounded (64 IFDs, 4 levels, cycles end the walk) and checked offsets; the main raw IFD is the largest NewSubFileType 0 CFA / LinearRaw image. - Raw data: 1-16-bit uncompressed (MSB-first packing) and lossless JPEG (process 14, predictors 1-7, 1-4 components, point transform, line restarts), strips and tiles, tiles decoded in parallel; allocations capped by size and by the data present. - Processing per the DNG model: ActiveArea, LinearizationTable, per-site black levels with deltas, white levels, as-shot white balance with neutral highlight clipping, Malvar-He-Cutler Bayer demosaic (bilinear for other patterns), DefaultCrop, ColorMatrix / CameraCalibration / ForwardMatrix with illuminant interpolation, Bradford to linear Rec. 709, BaselineExposure; output scene-linear RGBA floats. - CinemaDNG FrameRate, TimeCodes and ReelName tags. - A test-only writer (feature `writer`) synthesises DNGs. Tests: raw samples bit-exact for every depth / predictor / layout, processing against hand-computed values, hostile files (every truncation, 12k mutations, cyclic IFDs, huge sizes, bad offsets), and LibRaw's unprocessed_raw as an external oracle reading the same samples (skipped when not installed). Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/dng_metadata.rs | 285 +++++++++++++++++++++++++-------- 1 file changed, 220 insertions(+), 65 deletions(-) diff --git a/crates/raw/src/dng_metadata.rs b/crates/raw/src/dng_metadata.rs index b8ba5077f..c0b3e9e0b 100644 --- a/crates/raw/src/dng_metadata.rs +++ b/crates/raw/src/dng_metadata.rs @@ -1,62 +1,160 @@ -//! Bounded DNG processing and camera metadata, before application rendering. -use crate::dng_lines::{self, Error, PHOTOMETRIC_CFA, Result}; -use craft_tiff::classic::{Entry, Ifd, Tiff, tags}; -type M3 = [[f64; 3]; 3]; -type V3 = [f64; 3]; -/// Camera-plane colour filter pattern. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Pattern { - pub rows: usize, - pub cols: usize, - pub colors: Vec, +//! Clean-room DNG and CinemaDNG decoder, implemented from the public specifications only (see the +//! crate README for editions): the TIFF 6.0 / TIFF-EP container, uncompressed and lossless JPEG +//! (ITU-T T.81 process 14) raw data, and the DNG processing model (linearisation, black and white +//! levels, white balance, demosaic, default crop, colour matrices) to a scene-linear Rec. 709 +//! picture. +//! +//! - [`sniff`]: is this a DNG (a TIFF with a DNGVersion tag)? +//! - [`Dng::parse`] → [`Dng::info`] (size, bit depth, CinemaDNG frame rate and time code), +//! [`Dng::raw`] (the raw samples) and [`Dng::develop`] (linear RGBA floats). +//! - [`decode`]: parse and develop in one call. +//! +//! Every number in the file is hostile: offsets, counts and sizes are checked, allocations are +//! capped by [`raw::MAX_SAMPLES`] and by the amount of data present, and the IFD walk is bounded. + +#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))] + +pub mod color; +pub mod demosaic; +pub mod ljpeg; +pub mod process; +pub mod raw; +pub mod tiff; +#[cfg(any(test, feature = "writer"))] +pub mod writer; + +use color::{Calibration, M3, V3}; +use demosaic::Pattern; +use process::Params; +use raw::{PHOTOMETRIC_CFA, RawImage}; +use tiff::{Entry, Ifd, Tiff, tags}; + +/// DNG decoding errors. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + #[error("not a DNG file")] + NotDng, + #[error("truncated DNG data")] + Truncated, + #[error("raw image too large")] + TooLarge, + #[error("malformed DNG: {0}")] + Malformed(&'static str), + #[error("unsupported DNG: {0}")] + Unsupported(String), } -/// Parsed image processing tags. -#[derive(Clone, Debug, Default)] -pub struct Params { - pub active_area: Option<[usize; 4]>, - pub linearization: Option>, - pub black_repeat: (usize, usize), - pub black: Vec, - pub black_delta_h: Vec, - pub black_delta_v: Vec, - pub white: Vec, - pub crop_origin: Option<(f64, f64)>, - pub crop_size: Option<(f64, f64)>, - pub cfa: Option, + +pub type Result = std::result::Result; + +/// Whether `b` is a DNG: a TIFF file whose first IFD has a DNGVersion tag. +pub fn sniff(b: &[u8]) -> bool { + let Ok((t, first)) = Tiff::new(b) else { return false }; + t.read_ifd(first, 0).is_ok_and(|(ifd, _)| ifd.get(tags::DNG_VERSION).is_some()) } -/// Parsed camera colour calibration. -#[derive(Clone, Debug, Default)] -pub struct Calibration { - pub color_matrix: [Option; 2], - pub camera_calibration: [Option; 2], - pub forward_matrix: [Option; 2], - pub illuminant: [Option; 2], - pub analog_balance: Option, - pub as_shot_neutral: Option, - pub as_shot_white_xy: Option<(f64, f64)>, - pub baseline_exposure: f64, + +/// SMPTE ST 12-1 time code of a CinemaDNG frame. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Timecode { + pub hours: u8, + pub minutes: u8, + pub seconds: u8, + pub frames: u8, + pub drop_frame: bool, } -/// Metadata reader with the file's main and raw IFDs. -pub struct Metadata<'a, 'i> { - pub tiff: Tiff<'a>, - pub main: &'i Ifd, - pub raw: &'i Ifd, + +impl Timecode { + /// The first time code of a CinemaDNG TimeCodes tag (SMPTE ST 331 / 12-1 packing: BCD frames, + /// seconds, minutes, hours in bytes 0–3, the drop-frame flag in bit 6 of byte 0). + pub fn from_bytes(b: &[u8]) -> Option { + let bcd = |v: u8, tens_mask: u8| { + let (u, t) = (v & 0x0F, (v >> 4) & tens_mask); + (u <= 9).then_some(t * 10 + u) + }; + let [f, s, m, h, ..] = *b else { return None }; + let tc = Timecode { frames: bcd(f, 0x3)?, seconds: bcd(s, 0x7)?, minutes: bcd(m, 0x7)?, hours: bcd(h, 0x3)?, drop_frame: f & 0x40 != 0 }; + (tc.seconds < 60 && tc.minutes < 60 && tc.hours < 24).then_some(tc) + } } -impl Metadata<'_, '_> { + +/// What a DNG holds, without decoding the pixels. +#[derive(Clone, Debug, PartialEq)] +pub struct Info { + /// Size of the developed picture (the default crop). + pub width: usize, + pub height: usize, + /// Size of the stored raw image. + pub raw_width: usize, + pub raw_height: usize, + pub bits: u32, + pub samples_per_pixel: usize, + /// Colour filter array data (else LinearRaw). + pub cfa: bool, + /// "uncompressed" or "lossless JPEG". + pub compression: &'static str, + pub dng_version: [u8; 4], + /// CinemaDNG FrameRate (numerator, denominator). + pub frame_rate: Option<(i64, i64)>, + /// CinemaDNG time code of this frame. + pub timecode: Option, + /// UniqueCameraModel, else Make and Model. + pub camera: Option, + /// CinemaDNG ReelName. + pub reel: Option, +} + +/// A developed DNG: premultiplied, scene-linear Rec. 709 RGBA floats (alpha 1), top row first. +/// A white surface the sensor saw at its clipping level is 1.0 times 2^BaselineExposure. +pub struct Image { + pub info: Info, + pub width: usize, + pub height: usize, + pub rgba: Vec, +} + +/// A parsed DNG file. +pub struct Dng<'a> { + tiff: Tiff<'a>, + ifds: Vec, + /// Index of the main raw IFD in `ifds` (the first is IFD 0). + raw: usize, +} + +impl<'a> Dng<'a> { + /// Parse the container and find the main raw image. + pub fn parse(bytes: &'a [u8]) -> Result> { + let (tiff, first) = Tiff::new(bytes)?; + let ifds = tiff.all_ifds(first)?; + if ifds.first().and_then(|i| i.get(tags::DNG_VERSION)).is_none() { + return Err(Error::NotDng); + } + let main = raw::find_raw_ifd(&tiff, &ifds)?; + let raw = ifds.iter().position(|i| i.offset == main.offset).ok_or(Error::Malformed("raw IFD"))?; + Ok(Dng { tiff, ifds, raw }) + } + + fn ifd0(&self) -> &Ifd { + static EMPTY: Ifd = Ifd { offset: 0, entries: Vec::new(), depth: 0 }; + self.ifds.first().unwrap_or(&EMPTY) + } + + fn raw_ifd(&self) -> &Ifd { + self.ifds.get(self.raw).unwrap_or(self.ifd0()) + } + /// A raw-image tag: from the raw IFD, else IFD 0. fn raw_tag(&self, tag: u16) -> Option<&Entry> { - self.raw.get(tag).or_else(|| self.main.get(tag)) + self.raw_ifd().get(tag).or_else(|| self.ifd0().get(tag)) } /// A camera-wide tag: from IFD 0, else the raw IFD. fn main_tag(&self, tag: u16) -> Option<&Entry> { - self.main.get(tag).or_else(|| self.raw.get(tag)) + self.ifd0().get(tag).or_else(|| self.raw_ifd().get(tag)) } /// A numeric tag's values; a value outside the file (or a zero denominator) is an error, so a /// damaged file does not silently lose its white balance. fn floats(&self, e: Option<&Entry>) -> Result>> { - Ok(e.map(|e| self.tiff.floats(e)).transpose()?) + e.map(|e| self.tiff.floats(e)).transpose() } /// A 3 × 3 matrix tag (`None` when absent or not 3 × 3, e.g. for other colour counts). @@ -70,9 +168,13 @@ impl Metadata<'_, '_> { Ok(self.floats(self.main_tag(tag))?.and_then(|v| <[f64; 3]>::try_from(v.as_slice()).ok())) } + fn text(&self, tag: u16) -> Option { + self.main_tag(tag).and_then(|e| self.tiff.ascii(e).ok()).filter(|s| !s.is_empty()) + } + /// The CFA pattern in camera planes (`None` for LinearRaw). - pub fn cfa(&self) -> Result> { - let l = dng_lines::layout(&self.tiff, self.raw)?; + fn cfa(&self) -> Result> { + let l = raw::layout(&self.tiff, self.raw_ifd())?; if l.photometric != PHOTOMETRIC_CFA { return Ok(None); } @@ -109,6 +211,7 @@ impl Metadata<'_, '_> { Ok(Some(Pattern { rows, cols, colors })) } + /// The processing tags of the raw image. pub fn params(&self) -> Result { let uint_pair = |tag| -> Result> { match self.raw_tag(tag) { @@ -145,7 +248,7 @@ impl Metadata<'_, '_> { None => Ok(None), } }; - let list = |tag| -> Result> { Ok(self.raw_tag(tag).map(|e| self.tiff.floats(e)).transpose()?.unwrap_or_default()) }; + let list = |tag| -> Result> { self.raw_tag(tag).map(|e| self.tiff.floats(e)).transpose().map(Option::unwrap_or_default) }; Ok(Params { active_area, linearization, @@ -178,26 +281,78 @@ impl Metadata<'_, '_> { baseline_exposure: self.floats(self.main_tag(tags::BASELINE_EXPOSURE))?.and_then(|v| v.first().copied()).unwrap_or(0.0), }) } -} -#[cfg(test)] -mod tests { - use super::*; + /// Size, depth and CinemaDNG metadata. + pub fn info(&self) -> Result { + let l = raw::layout(&self.tiff, self.raw_ifd())?; + let p = self.params()?; + let (_, _, aw, ah) = process::active_rect(&p, l.width, l.height)?; + let (_, _, width, height) = process::crop_rect(&p, aw, ah); + let dng_version = self + .main_tag(tags::DNG_VERSION) + .and_then(|e| self.tiff.uints(e).ok()) + .and_then(|v| v.get(..4).map(|v| [0, 1, 2, 3].map(|i| v[i] as u8))) + .unwrap_or_default(); + let frame_rate = self.main_tag(tags::FRAME_RATE).and_then(|e| { + let b = self.tiff.value_bytes(e).ok()?; + if !matches!(e.ty, tiff::Type::SRational | tiff::Type::Rational) { + return None; + } + let n = i64::from(self.tiff.u32_of(b.get(0..4)?.try_into().ok()?) as i32); + let d = i64::from(self.tiff.u32_of(b.get(4..8)?.try_into().ok()?) as i32); + (n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d).then_some((n, d)) + }); + let timecode = self.main_tag(tags::TIME_CODES).and_then(|e| self.tiff.value_bytes(e).ok()).and_then(Timecode::from_bytes); + let camera = self.text(tags::UNIQUE_CAMERA_MODEL).or_else(|| { + let mm = [self.text(tags::MAKE), self.text(tags::MODEL)].into_iter().flatten().collect::>().join(" "); + (!mm.is_empty()).then_some(mm) + }); + Ok(Info { + width, + height, + raw_width: l.width, + raw_height: l.height, + bits: l.bits, + samples_per_pixel: l.samples_per_pixel, + cfa: p.cfa.is_some(), + compression: if l.compression == 7 { "lossless JPEG" } else { "uncompressed" }, + dng_version, + frame_rate, + timecode, + camera, + reel: self.text(tags::REEL_NAME), + }) + } - #[test] - fn calibration_defaults_and_rejects_bad_rational() -> Result<()> { - let empty = Ifd::default(); - let tiff = Tiff { data: &[], big_endian: false }; - let metadata = Metadata { tiff, main: &empty, raw: &empty }; - let calibration = metadata.calibration()?; - assert_eq!(calibration.baseline_exposure, 0.0); - assert_eq!(calibration.color_matrix, [None, None]); + /// The raw samples of the main image, as stored (before any processing). + pub fn raw(&self) -> Result { + raw::read(&self.tiff, self.raw_ifd()) + } - // IFD with one BaselineExposure SRational pointing past the end of the file. - let data = [b'I', b'I', 42, 0, 8, 0, 0, 0, 1, 0, 42, 198, 10, 0, 1, 0, 0, 0, 200, 0, 0, 0, 0, 0, 0, 0]; - let (tiff, first) = Tiff::new(&data)?; - let (ifd, _) = tiff.read_ifd(first, 0)?; - assert!(Metadata { tiff, main: &ifd, raw: &ifd }.calibration().is_err()); - Ok(()) + /// Develop the main image to scene-linear Rec. 709. + pub fn develop(&self) -> Result { + let info = self.info()?; + let p = self.params()?; + let raw = self.raw()?; + let calibration = self.calibration()?; + let solved = if p.cfa.is_none() && raw.samples_per_pixel == 1 { + // monochrome: no colour, only the exposure + let e = 2f64.powf(calibration.baseline_exposure.clamp(-16.0, 16.0)); + color::Solved { neutral: [1.0; 3], matrix: color::scale(&color::IDENTITY, e), white_xy: color::D65 } + } else { + calibration.solve()? + }; + let d = process::develop(&raw, &p, &solved.neutral, &solved.matrix)?; + Ok(Image { info, width: d.width, height: d.height, rgba: d.rgba }) } } + +/// Parse and develop a DNG file. +pub fn decode(bytes: &[u8]) -> Result { + Dng::parse(bytes)?.develop() +} + +/// Parse a DNG file's metadata. +pub fn probe(bytes: &[u8]) -> Result { + Dng::parse(bytes)?.info() +} From b00cb4533af191de874a4303c2f0a149d7ed299d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:17:42 +0000 Subject: [PATCH 10/25] raw: use fixed-size opcode record chunks --- crates/raw/src/opcode_params.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/raw/src/opcode_params.rs b/crates/raw/src/opcode_params.rs index 4f2433269..7c6997c98 100644 --- a/crates/raw/src/opcode_params.rs +++ b/crates/raw/src/opcode_params.rs @@ -40,7 +40,7 @@ impl Area { pub struct PointRecords<'a>(&'a [u8]); impl PointRecords<'_> { pub fn iter(&self) -> impl Iterator + '_ { - self.0.chunks_exact(8).map(|b| (u32::from_be_bytes([b[0], b[1], b[2], b[3]]), u32::from_be_bytes([b[4], b[5], b[6], b[7]]))) + self.0.as_chunks::<8>().0.iter().map(|b| (u32::from_be_bytes([b[0], b[1], b[2], b[3]]), u32::from_be_bytes([b[4], b[5], b[6], b[7]]))) } } /// Borrowed bad-pixel rectangles, decoded lazily without copying the payload. @@ -48,7 +48,7 @@ impl PointRecords<'_> { pub struct RectangleRecords<'a>(&'a [u8]); impl RectangleRecords<'_> { pub fn iter(&self) -> impl Iterator + '_ { - self.0.chunks_exact(16).map(|b| { + self.0.as_chunks::<16>().0.iter().map(|b| { [ u32::from_be_bytes([b[0], b[1], b[2], b[3]]), u32::from_be_bytes([b[4], b[5], b[6], b[7]]), From 61a9330f4e71cd20c38d505abc33b9fd42c1da44 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 21:49:46 -0500 Subject: [PATCH 11/25] Adapt attributed FilmCraft metadata to bounded shared reader Preserve vangel76 original FilmCraft source commit 28247afae7806590dcdb149d744408739fb738df before the shared metadata extraction. Final metadata implementation is unchanged from the existing PR. --- crates/raw/src/dng_metadata.rs | 287 +++---------- crates/raw/src/dng_metadata_source_color.rs | 385 ------------------ .../raw/src/dng_metadata_source_demosaic.rs | 255 ------------ crates/raw/src/dng_metadata_source_process.rs | 264 ------------ crates/raw/src/lib.rs | 3 + 5 files changed, 69 insertions(+), 1125 deletions(-) delete mode 100644 crates/raw/src/dng_metadata_source_color.rs delete mode 100644 crates/raw/src/dng_metadata_source_demosaic.rs delete mode 100644 crates/raw/src/dng_metadata_source_process.rs diff --git a/crates/raw/src/dng_metadata.rs b/crates/raw/src/dng_metadata.rs index c0b3e9e0b..b8ba5077f 100644 --- a/crates/raw/src/dng_metadata.rs +++ b/crates/raw/src/dng_metadata.rs @@ -1,160 +1,62 @@ -//! Clean-room DNG and CinemaDNG decoder, implemented from the public specifications only (see the -//! crate README for editions): the TIFF 6.0 / TIFF-EP container, uncompressed and lossless JPEG -//! (ITU-T T.81 process 14) raw data, and the DNG processing model (linearisation, black and white -//! levels, white balance, demosaic, default crop, colour matrices) to a scene-linear Rec. 709 -//! picture. -//! -//! - [`sniff`]: is this a DNG (a TIFF with a DNGVersion tag)? -//! - [`Dng::parse`] → [`Dng::info`] (size, bit depth, CinemaDNG frame rate and time code), -//! [`Dng::raw`] (the raw samples) and [`Dng::develop`] (linear RGBA floats). -//! - [`decode`]: parse and develop in one call. -//! -//! Every number in the file is hostile: offsets, counts and sizes are checked, allocations are -//! capped by [`raw::MAX_SAMPLES`] and by the amount of data present, and the IFD walk is bounded. - -#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))] - -pub mod color; -pub mod demosaic; -pub mod ljpeg; -pub mod process; -pub mod raw; -pub mod tiff; -#[cfg(any(test, feature = "writer"))] -pub mod writer; - -use color::{Calibration, M3, V3}; -use demosaic::Pattern; -use process::Params; -use raw::{PHOTOMETRIC_CFA, RawImage}; -use tiff::{Entry, Ifd, Tiff, tags}; - -/// DNG decoding errors. -#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] -pub enum Error { - #[error("not a DNG file")] - NotDng, - #[error("truncated DNG data")] - Truncated, - #[error("raw image too large")] - TooLarge, - #[error("malformed DNG: {0}")] - Malformed(&'static str), - #[error("unsupported DNG: {0}")] - Unsupported(String), +//! Bounded DNG processing and camera metadata, before application rendering. +use crate::dng_lines::{self, Error, PHOTOMETRIC_CFA, Result}; +use craft_tiff::classic::{Entry, Ifd, Tiff, tags}; +type M3 = [[f64; 3]; 3]; +type V3 = [f64; 3]; +/// Camera-plane colour filter pattern. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Pattern { + pub rows: usize, + pub cols: usize, + pub colors: Vec, } - -pub type Result = std::result::Result; - -/// Whether `b` is a DNG: a TIFF file whose first IFD has a DNGVersion tag. -pub fn sniff(b: &[u8]) -> bool { - let Ok((t, first)) = Tiff::new(b) else { return false }; - t.read_ifd(first, 0).is_ok_and(|(ifd, _)| ifd.get(tags::DNG_VERSION).is_some()) +/// Parsed image processing tags. +#[derive(Clone, Debug, Default)] +pub struct Params { + pub active_area: Option<[usize; 4]>, + pub linearization: Option>, + pub black_repeat: (usize, usize), + pub black: Vec, + pub black_delta_h: Vec, + pub black_delta_v: Vec, + pub white: Vec, + pub crop_origin: Option<(f64, f64)>, + pub crop_size: Option<(f64, f64)>, + pub cfa: Option, } - -/// SMPTE ST 12-1 time code of a CinemaDNG frame. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct Timecode { - pub hours: u8, - pub minutes: u8, - pub seconds: u8, - pub frames: u8, - pub drop_frame: bool, +/// Parsed camera colour calibration. +#[derive(Clone, Debug, Default)] +pub struct Calibration { + pub color_matrix: [Option; 2], + pub camera_calibration: [Option; 2], + pub forward_matrix: [Option; 2], + pub illuminant: [Option; 2], + pub analog_balance: Option, + pub as_shot_neutral: Option, + pub as_shot_white_xy: Option<(f64, f64)>, + pub baseline_exposure: f64, } - -impl Timecode { - /// The first time code of a CinemaDNG TimeCodes tag (SMPTE ST 331 / 12-1 packing: BCD frames, - /// seconds, minutes, hours in bytes 0–3, the drop-frame flag in bit 6 of byte 0). - pub fn from_bytes(b: &[u8]) -> Option { - let bcd = |v: u8, tens_mask: u8| { - let (u, t) = (v & 0x0F, (v >> 4) & tens_mask); - (u <= 9).then_some(t * 10 + u) - }; - let [f, s, m, h, ..] = *b else { return None }; - let tc = Timecode { frames: bcd(f, 0x3)?, seconds: bcd(s, 0x7)?, minutes: bcd(m, 0x7)?, hours: bcd(h, 0x3)?, drop_frame: f & 0x40 != 0 }; - (tc.seconds < 60 && tc.minutes < 60 && tc.hours < 24).then_some(tc) - } +/// Metadata reader with the file's main and raw IFDs. +pub struct Metadata<'a, 'i> { + pub tiff: Tiff<'a>, + pub main: &'i Ifd, + pub raw: &'i Ifd, } - -/// What a DNG holds, without decoding the pixels. -#[derive(Clone, Debug, PartialEq)] -pub struct Info { - /// Size of the developed picture (the default crop). - pub width: usize, - pub height: usize, - /// Size of the stored raw image. - pub raw_width: usize, - pub raw_height: usize, - pub bits: u32, - pub samples_per_pixel: usize, - /// Colour filter array data (else LinearRaw). - pub cfa: bool, - /// "uncompressed" or "lossless JPEG". - pub compression: &'static str, - pub dng_version: [u8; 4], - /// CinemaDNG FrameRate (numerator, denominator). - pub frame_rate: Option<(i64, i64)>, - /// CinemaDNG time code of this frame. - pub timecode: Option, - /// UniqueCameraModel, else Make and Model. - pub camera: Option, - /// CinemaDNG ReelName. - pub reel: Option, -} - -/// A developed DNG: premultiplied, scene-linear Rec. 709 RGBA floats (alpha 1), top row first. -/// A white surface the sensor saw at its clipping level is 1.0 times 2^BaselineExposure. -pub struct Image { - pub info: Info, - pub width: usize, - pub height: usize, - pub rgba: Vec, -} - -/// A parsed DNG file. -pub struct Dng<'a> { - tiff: Tiff<'a>, - ifds: Vec, - /// Index of the main raw IFD in `ifds` (the first is IFD 0). - raw: usize, -} - -impl<'a> Dng<'a> { - /// Parse the container and find the main raw image. - pub fn parse(bytes: &'a [u8]) -> Result> { - let (tiff, first) = Tiff::new(bytes)?; - let ifds = tiff.all_ifds(first)?; - if ifds.first().and_then(|i| i.get(tags::DNG_VERSION)).is_none() { - return Err(Error::NotDng); - } - let main = raw::find_raw_ifd(&tiff, &ifds)?; - let raw = ifds.iter().position(|i| i.offset == main.offset).ok_or(Error::Malformed("raw IFD"))?; - Ok(Dng { tiff, ifds, raw }) - } - - fn ifd0(&self) -> &Ifd { - static EMPTY: Ifd = Ifd { offset: 0, entries: Vec::new(), depth: 0 }; - self.ifds.first().unwrap_or(&EMPTY) - } - - fn raw_ifd(&self) -> &Ifd { - self.ifds.get(self.raw).unwrap_or(self.ifd0()) - } - +impl Metadata<'_, '_> { /// A raw-image tag: from the raw IFD, else IFD 0. fn raw_tag(&self, tag: u16) -> Option<&Entry> { - self.raw_ifd().get(tag).or_else(|| self.ifd0().get(tag)) + self.raw.get(tag).or_else(|| self.main.get(tag)) } /// A camera-wide tag: from IFD 0, else the raw IFD. fn main_tag(&self, tag: u16) -> Option<&Entry> { - self.ifd0().get(tag).or_else(|| self.raw_ifd().get(tag)) + self.main.get(tag).or_else(|| self.raw.get(tag)) } /// A numeric tag's values; a value outside the file (or a zero denominator) is an error, so a /// damaged file does not silently lose its white balance. fn floats(&self, e: Option<&Entry>) -> Result>> { - e.map(|e| self.tiff.floats(e)).transpose() + Ok(e.map(|e| self.tiff.floats(e)).transpose()?) } /// A 3 × 3 matrix tag (`None` when absent or not 3 × 3, e.g. for other colour counts). @@ -168,13 +70,9 @@ impl<'a> Dng<'a> { Ok(self.floats(self.main_tag(tag))?.and_then(|v| <[f64; 3]>::try_from(v.as_slice()).ok())) } - fn text(&self, tag: u16) -> Option { - self.main_tag(tag).and_then(|e| self.tiff.ascii(e).ok()).filter(|s| !s.is_empty()) - } - /// The CFA pattern in camera planes (`None` for LinearRaw). - fn cfa(&self) -> Result> { - let l = raw::layout(&self.tiff, self.raw_ifd())?; + pub fn cfa(&self) -> Result> { + let l = dng_lines::layout(&self.tiff, self.raw)?; if l.photometric != PHOTOMETRIC_CFA { return Ok(None); } @@ -211,7 +109,6 @@ impl<'a> Dng<'a> { Ok(Some(Pattern { rows, cols, colors })) } - /// The processing tags of the raw image. pub fn params(&self) -> Result { let uint_pair = |tag| -> Result> { match self.raw_tag(tag) { @@ -248,7 +145,7 @@ impl<'a> Dng<'a> { None => Ok(None), } }; - let list = |tag| -> Result> { self.raw_tag(tag).map(|e| self.tiff.floats(e)).transpose().map(Option::unwrap_or_default) }; + let list = |tag| -> Result> { Ok(self.raw_tag(tag).map(|e| self.tiff.floats(e)).transpose()?.unwrap_or_default()) }; Ok(Params { active_area, linearization, @@ -281,78 +178,26 @@ impl<'a> Dng<'a> { baseline_exposure: self.floats(self.main_tag(tags::BASELINE_EXPOSURE))?.and_then(|v| v.first().copied()).unwrap_or(0.0), }) } - - /// Size, depth and CinemaDNG metadata. - pub fn info(&self) -> Result { - let l = raw::layout(&self.tiff, self.raw_ifd())?; - let p = self.params()?; - let (_, _, aw, ah) = process::active_rect(&p, l.width, l.height)?; - let (_, _, width, height) = process::crop_rect(&p, aw, ah); - let dng_version = self - .main_tag(tags::DNG_VERSION) - .and_then(|e| self.tiff.uints(e).ok()) - .and_then(|v| v.get(..4).map(|v| [0, 1, 2, 3].map(|i| v[i] as u8))) - .unwrap_or_default(); - let frame_rate = self.main_tag(tags::FRAME_RATE).and_then(|e| { - let b = self.tiff.value_bytes(e).ok()?; - if !matches!(e.ty, tiff::Type::SRational | tiff::Type::Rational) { - return None; - } - let n = i64::from(self.tiff.u32_of(b.get(0..4)?.try_into().ok()?) as i32); - let d = i64::from(self.tiff.u32_of(b.get(4..8)?.try_into().ok()?) as i32); - (n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d).then_some((n, d)) - }); - let timecode = self.main_tag(tags::TIME_CODES).and_then(|e| self.tiff.value_bytes(e).ok()).and_then(Timecode::from_bytes); - let camera = self.text(tags::UNIQUE_CAMERA_MODEL).or_else(|| { - let mm = [self.text(tags::MAKE), self.text(tags::MODEL)].into_iter().flatten().collect::>().join(" "); - (!mm.is_empty()).then_some(mm) - }); - Ok(Info { - width, - height, - raw_width: l.width, - raw_height: l.height, - bits: l.bits, - samples_per_pixel: l.samples_per_pixel, - cfa: p.cfa.is_some(), - compression: if l.compression == 7 { "lossless JPEG" } else { "uncompressed" }, - dng_version, - frame_rate, - timecode, - camera, - reel: self.text(tags::REEL_NAME), - }) - } - - /// The raw samples of the main image, as stored (before any processing). - pub fn raw(&self) -> Result { - raw::read(&self.tiff, self.raw_ifd()) - } - - /// Develop the main image to scene-linear Rec. 709. - pub fn develop(&self) -> Result { - let info = self.info()?; - let p = self.params()?; - let raw = self.raw()?; - let calibration = self.calibration()?; - let solved = if p.cfa.is_none() && raw.samples_per_pixel == 1 { - // monochrome: no colour, only the exposure - let e = 2f64.powf(calibration.baseline_exposure.clamp(-16.0, 16.0)); - color::Solved { neutral: [1.0; 3], matrix: color::scale(&color::IDENTITY, e), white_xy: color::D65 } - } else { - calibration.solve()? - }; - let d = process::develop(&raw, &p, &solved.neutral, &solved.matrix)?; - Ok(Image { info, width: d.width, height: d.height, rgba: d.rgba }) - } } -/// Parse and develop a DNG file. -pub fn decode(bytes: &[u8]) -> Result { - Dng::parse(bytes)?.develop() -} +#[cfg(test)] +mod tests { + use super::*; -/// Parse a DNG file's metadata. -pub fn probe(bytes: &[u8]) -> Result { - Dng::parse(bytes)?.info() + #[test] + fn calibration_defaults_and_rejects_bad_rational() -> Result<()> { + let empty = Ifd::default(); + let tiff = Tiff { data: &[], big_endian: false }; + let metadata = Metadata { tiff, main: &empty, raw: &empty }; + let calibration = metadata.calibration()?; + assert_eq!(calibration.baseline_exposure, 0.0); + assert_eq!(calibration.color_matrix, [None, None]); + + // IFD with one BaselineExposure SRational pointing past the end of the file. + let data = [b'I', b'I', 42, 0, 8, 0, 0, 0, 1, 0, 42, 198, 10, 0, 1, 0, 0, 0, 200, 0, 0, 0, 0, 0, 0, 0]; + let (tiff, first) = Tiff::new(&data)?; + let (ifd, _) = tiff.read_ifd(first, 0)?; + assert!(Metadata { tiff, main: &ifd, raw: &ifd }.calibration().is_err()); + Ok(()) + } } diff --git a/crates/raw/src/dng_metadata_source_color.rs b/crates/raw/src/dng_metadata_source_color.rs deleted file mode 100644 index fb02f2c8d..000000000 --- a/crates/raw/src/dng_metadata_source_color.rs +++ /dev/null @@ -1,385 +0,0 @@ -//! Camera colour to linear Rec. 709, following the DNG specification's "Mapping Camera Color Space -//! to CIE XYZ Space" (DNG 1.4–1.7 chapter 6): -//! -//! - The two calibrations (ColorMatrix, CameraCalibration, ForwardMatrix 1 and 2) are blended -//! linearly in inverse correlated colour temperature between their CalibrationIlluminants. -//! - The white balance is AsShotNeutral (found as a white xy by the specification's iteration) or -//! AsShotWhiteXY; without either, D65. -//! - With ForwardMatrix: CameraToXYZ_D50 = FM · D · (AB · CC)⁻¹, D = diag((AB · CC)⁻¹ · neutral)⁻¹. -//! Without: CameraToXYZ_D50 = CA · (AB · CC · CM)⁻¹, CA a linear Bradford adaptation from the -//! white to D50. -//! - XYZ (D50) goes to linear Rec. 709 / sRGB primaries (D65 white) through Bradford again. -//! -//! The correlated colour temperature of an xy white comes from McCamy's cubic approximation -//! (C. S. McCamy, "Correlated color temperature as an explicit function of chromaticity -//! coordinates", Color Research & Application 17(2), 1992); the illuminant temperatures are the -//! CIE / EXIF 2.3 LightSource values. - -use crate::{Error, Result}; - -pub type M3 = [[f64; 3]; 3]; -pub type V3 = [f64; 3]; - -pub const IDENTITY: M3 = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]; - -/// CIE D50 and D65 white chromaticities (the DNG specification's D50). -pub const D50: (f64, f64) = (0.3457, 0.3585); -pub const D65: (f64, f64) = (0.3127, 0.3290); - -/// Linear Bradford cone response matrix. -const BRADFORD: M3 = [[0.8951, 0.2664, -0.1614], [-0.7502, 1.7135, 0.0367], [0.0389, -0.0685, 1.0296]]; - -pub fn mul(a: &M3, b: &M3) -> M3 { - let mut m = [[0.0; 3]; 3]; - for (i, row) in m.iter_mut().enumerate() { - for (j, v) in row.iter_mut().enumerate() { - *v = (0..3).map(|k| a[i][k] * b[k][j]).sum(); - } - } - m -} - -pub fn mulv(a: &M3, v: &V3) -> V3 { - [0, 1, 2].map(|i| a[i][0] * v[0] + a[i][1] * v[1] + a[i][2] * v[2]) -} - -pub fn diag(v: &V3) -> M3 { - [[v[0], 0.0, 0.0], [0.0, v[1], 0.0], [0.0, 0.0, v[2]]] -} - -pub fn scale(a: &M3, s: f64) -> M3 { - a.map(|r| r.map(|v| v * s)) -} - -fn blend(a: &M3, b: &M3, w: f64) -> M3 { - let mut m = [[0.0; 3]; 3]; - for i in 0..3 { - for j in 0..3 { - m[i][j] = w * a[i][j] + (1.0 - w) * b[i][j]; - } - } - m -} - -/// The inverse of `a`, or `None` when it is (nearly) singular. -pub fn inverse(a: &M3) -> Option { - let c = |r: usize, s: usize| a[(r + 1) % 3][(s + 1) % 3] * a[(r + 2) % 3][(s + 2) % 3] - a[(r + 1) % 3][(s + 2) % 3] * a[(r + 2) % 3][(s + 1) % 3]; - let det = a[0][0] * c(0, 0) + a[0][1] * c(0, 1) + a[0][2] * c(0, 2); - let norm: f64 = a.iter().flatten().map(|v| v.abs()).fold(0.0, f64::max); - if !det.is_finite() || det.abs() <= 1e-12 * norm.powi(3).max(1e-300) { - return None; - } - let mut m = [[0.0; 3]; 3]; - for (i, row) in m.iter_mut().enumerate() { - for (j, v) in row.iter_mut().enumerate() { - *v = c(j, i) / det; - } - } - Some(m) -} - -/// XYZ (Y = 1) of a chromaticity. -pub fn xy_to_xyz((x, y): (f64, f64)) -> V3 { - let y = y.max(1e-6); - [x / y, 1.0, (1.0 - x - y) / y] -} - -pub fn xyz_to_xy(v: &V3) -> Option<(f64, f64)> { - let s = v[0] + v[1] + v[2]; - (s.is_finite() && s > 1e-12).then(|| (v[0] / s, v[1] / s)) -} - -/// Correlated colour temperature (kelvin) of a chromaticity, McCamy (1992), limited to -/// 1 000–50 000 K. -pub fn cct((x, y): (f64, f64)) -> f64 { - let d = 0.1858 - y; - if d.abs() < 1e-9 { - return 50_000.0; - } - let n = (x - 0.3320) / d; - let t = 449.0 * n.powi(3) + 3525.0 * n.powi(2) + 6823.3 * n + 5520.33; - if t.is_finite() { t.clamp(1_000.0, 50_000.0) } else { 50_000.0 } -} - -/// Colour temperature of an EXIF LightSource code used as a DNG CalibrationIlluminant. -pub fn illuminant_kelvin(code: u64) -> Option { - Some(match code { - 1 | 4 | 9 => 5500.0, // daylight, flash, fine weather - 2 | 14 => 4150.0, // fluorescent, cool white fluorescent (W 3800–4500 K) - 3 => 2850.0, // tungsten - 10 => 6500.0, // cloudy - 11 => 7500.0, // shade - 12 => 6430.0, // daylight fluorescent (D 5700–7100 K) - 13 => 5050.0, // day white fluorescent (N 4600–5500 K) - 15 => 3525.0, // white fluorescent (WW 3250–3800 K) - 16 => 2925.0, // warm white fluorescent (L 2600–3250 K) - 17 => 2856.0, // standard light A - 18 => 4874.0, // standard light B - 19 => 6774.0, // standard light C - 20 => 5503.0, // D55 - 21 => 6504.0, // D65 - 22 => 7504.0, // D75 - 23 => 5003.0, // D50 - 24 => 3200.0, // ISO studio tungsten - _ => return None, - }) -} - -/// Linear Bradford adaptation from one white (XYZ) to another. -pub fn bradford(src: &V3, dst: &V3) -> Option { - let s = mulv(&BRADFORD, src); - let d = mulv(&BRADFORD, dst); - if s.iter().any(|v| v.abs() < 1e-12) { - return None; - } - let g = [d[0] / s[0], d[1] / s[1], d[2] / s[2]]; - Some(mul(&inverse(&BRADFORD)?, &mul(&diag(&g), &BRADFORD))) -} - -/// Linear RGB (Rec. 709 / sRGB primaries, D65 white) → XYZ. -pub fn rec709_to_xyz() -> Option { - let p = [(0.64, 0.33), (0.30, 0.60), (0.15, 0.06)].map(xy_to_xyz); - let pm: M3 = [[p[0][0], p[1][0], p[2][0]], [p[0][1], p[1][1], p[2][1]], [p[0][2], p[1][2], p[2][2]]]; - let s = mulv(&inverse(&pm)?, &xy_to_xyz(D65)); - Some(mul(&pm, &diag(&s))) -} - -/// XYZ relative to D50 → linear Rec. 709. -pub fn xyz_d50_to_rec709() -> Option { - let to_d65 = bradford(&xy_to_xyz(D50), &xy_to_xyz(D65))?; - Some(mul(&inverse(&rec709_to_xyz()?)?, &to_d65)) -} - -/// The colour tags of a three-colour DNG. -#[derive(Clone, Debug, Default)] -pub struct Calibration { - pub color_matrix: [Option; 2], - pub camera_calibration: [Option; 2], - pub forward_matrix: [Option; 2], - /// EXIF LightSource codes of the two calibrations. - pub illuminant: [Option; 2], - pub analog_balance: Option, - pub as_shot_neutral: Option, - pub as_shot_white_xy: Option<(f64, f64)>, - pub baseline_exposure: f64, -} - -/// The result of [`Calibration::solve`]. -#[derive(Clone, Debug, PartialEq)] -pub struct Solved { - /// Camera neutral: raw (normalised) camera values of a white surface. Dividing by it white - /// balances the camera values. - pub neutral: V3, - /// White-balanced camera values → linear Rec. 709, including the baseline exposure. The white - /// balanced white (1, 1, 1) goes to Y = 2^BaselineExposure. - pub matrix: M3, - /// The white point found, for information. - pub white_xy: (f64, f64), -} - -impl Calibration { - /// Weight of calibration 1 for a white of temperature `t` (1 when only one is usable). - fn weight(&self, t: f64) -> f64 { - let (Some(_), Some(_)) = (self.color_matrix[0], self.color_matrix[1]) else { return 1.0 }; - let (Some(t1), Some(t2)) = (self.illuminant[0].and_then(illuminant_kelvin), self.illuminant[1].and_then(illuminant_kelvin)) else { - return 1.0; - }; - if (t1 - t2).abs() < 1.0 { - return 1.0; - } - // the formula needs t1 < t2; swap the roles otherwise - let (lo, hi, lo_is_1) = if t1 < t2 { (t1, t2, true) } else { (t2, t1, false) }; - let w_lo = if t <= lo { - 1.0 - } else if t >= hi { - 0.0 - } else { - (1.0 / t - 1.0 / hi) / (1.0 / lo - 1.0 / hi) - }; - if lo_is_1 { w_lo } else { 1.0 - w_lo } - } - - fn pick(&self, m: &[Option; 2], w: f64, default: M3) -> M3 { - match (m[0], m[1]) { - (Some(a), Some(b)) => blend(&a, &b, w), - (Some(a), None) => a, - (None, Some(b)) if self.color_matrix[0].is_none() => b, - _ => default, - } - } - - /// XYZ → camera at weight `w`: AB · CC · CM. - fn xyz_to_camera(&self, w: f64) -> Option { - let cm = match self.color_matrix { - [None, None] => return None, - _ => self.pick(&self.color_matrix, w, IDENTITY), - }; - let cc = self.pick(&self.camera_calibration, w, IDENTITY); - let ab = diag(&self.analog_balance.unwrap_or([1.0; 3])); - Some(mul(&ab, &mul(&cc, &cm))) - } - - /// Work out the white balance and the camera → linear Rec. 709 matrix. - pub fn solve(&self) -> Result { - let out_matrix = xyz_d50_to_rec709().ok_or(Error::Malformed("colour maths"))?; - let exposure = 2f64.powf(self.baseline_exposure.clamp(-16.0, 16.0)); - if self.color_matrix.iter().all(Option::is_none) { - // no colour calibration: the camera values are taken as Rec. 709, white balanced - let neutral = self.as_shot_neutral.unwrap_or([1.0; 3]); - if neutral.iter().any(|v| !v.is_finite() || *v <= 1e-9) { - return Err(Error::Malformed("camera neutral must be positive")); - } - return Ok(Solved { neutral, matrix: scale(&IDENTITY, exposure), white_xy: D65 }); - } - let bad = || Error::Malformed("singular colour matrix"); - // the white point and the camera neutral - let (white_xy, neutral) = if let Some(n) = self.as_shot_neutral { - let mut xy = D50; - for _ in 0..50 { - let w = self.weight(cct(xy)); - let to_xyz = inverse(&self.xyz_to_camera(w).ok_or_else(bad)?).ok_or_else(bad)?; - let next = xyz_to_xy(&mulv(&to_xyz, &n)).ok_or(Error::Malformed("AsShotNeutral is not a colour"))?; - let done = (next.0 - xy.0).abs() + (next.1 - xy.1).abs() < 1e-9; - xy = next; - if done { - break; - } - } - (xy, n) - } else { - let xy = self.as_shot_white_xy.unwrap_or(D65); - let w = self.weight(cct(xy)); - let n = mulv(&self.xyz_to_camera(w).ok_or_else(bad)?, &xy_to_xyz(xy)); - (xy, n) - }; - if neutral.iter().any(|v| !v.is_finite() || *v <= 1e-9) { - return Err(Error::Malformed("camera neutral must be positive")); - } - let w = self.weight(cct(white_xy)); - let ab_cc = mul(&diag(&self.analog_balance.unwrap_or([1.0; 3])), &self.pick(&self.camera_calibration, w, IDENTITY)); - let camera_to_xyz_d50 = if self.forward_matrix.iter().any(Option::is_some) { - let fm = self.pick(&self.forward_matrix, w, IDENTITY); - let inv_ab_cc = inverse(&ab_cc).ok_or_else(bad)?; - let reference = mulv(&inv_ab_cc, &neutral); - if reference.iter().any(|v| v.abs() < 1e-12) { - return Err(bad()); - } - mul(&fm, &mul(&diag(&reference.map(|v| 1.0 / v)), &inv_ab_cc)) - } else { - let to_xyz = inverse(&self.xyz_to_camera(w).ok_or_else(bad)?).ok_or_else(bad)?; - let ca = bradford(&xy_to_xyz(white_xy), &xy_to_xyz(D50)).ok_or_else(bad)?; - mul(&ca, &to_xyz) - }; - // white-balanced camera values: camera = neutral · wb - let wb_to_xyz = mul(&camera_to_xyz_d50, &diag(&neutral)); - let y = mulv(&wb_to_xyz, &[1.0; 3])[1]; - if !y.is_finite() || y <= 1e-9 { - return Err(bad()); - } - let matrix = scale(&mul(&out_matrix, &wb_to_xyz), exposure / y); - if matrix.iter().flatten().any(|v| !v.is_finite()) { - return Err(bad()); - } - Ok(Solved { neutral, matrix, white_xy }) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - fn close(a: &M3, b: &M3, tol: f64) -> bool { - a.iter().flatten().zip(b.iter().flatten()).all(|(x, y)| (x - y).abs() <= tol) - } - - #[test] - fn rec709_matrix_matches_the_published_one() { - // IEC 61966-2-1 (sRGB) linear RGB → XYZ, four decimals - let m = rec709_to_xyz().unwrap(); - let want = [[0.4124, 0.3576, 0.1805], [0.2126, 0.7152, 0.0722], [0.0193, 0.1192, 0.9505]]; - assert!(close(&m, &want, 1e-3), "{m:?}"); - } - - #[test] - fn inverse_round_trips_and_refuses_singular() { - let a = [[2.0, 1.0, 0.5], [0.1, 3.0, 0.2], [0.3, 0.4, 1.5]]; - assert!(close(&mul(&a, &inverse(&a).unwrap()), &IDENTITY, 1e-12)); - assert!(inverse(&[[1.0, 2.0, 3.0], [2.0, 4.0, 6.0], [0.0, 0.0, 1.0]]).is_none()); - assert!(inverse(&[[f64::NAN; 3]; 3]).is_none()); - } - - #[test] - fn mccamy_temperatures() { - assert!((cct(D65) - 6504.0).abs() < 15.0, "{}", cct(D65)); - assert!((cct(D50) - 5003.0).abs() < 15.0, "{}", cct(D50)); - assert!((cct((0.44757, 0.40745)) - 2856.0).abs() < 15.0, "illuminant A"); - assert_eq!(cct((0.3320, 0.1858)), 50_000.0, "no division by zero"); - } - - #[test] - fn interpolation_weight_follows_inverse_temperature() { - let c = Calibration { color_matrix: [Some(IDENTITY), Some(IDENTITY)], illuminant: [Some(17), Some(21)], ..Default::default() }; - assert_eq!(c.weight(2000.0), 1.0); - assert_eq!(c.weight(9000.0), 0.0); - let mid = c.weight(4000.0); - let want = (1.0 / 4000.0 - 1.0 / 6504.0) / (1.0 / 2856.0 - 1.0 / 6504.0); - assert!((mid - want).abs() < 1e-12); - // swapped order: weight of calibration 1 (now D65) - let s = Calibration { illuminant: [Some(21), Some(17)], ..c.clone() }; - assert!((s.weight(4000.0) - (1.0 - want)).abs() < 1e-12); - // unknown illuminant: calibration 1 only - let u = Calibration { illuminant: [Some(0), Some(21)], ..c }; - assert_eq!(u.weight(4000.0), 1.0); - } - - #[test] - fn an_srgb_camera_maps_to_itself() { - // ColorMatrix = XYZ → linear sRGB, white D65: the camera is Rec. 709 already - let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); - let c = Calibration { color_matrix: [Some(cm), None], illuminant: [Some(21), None], as_shot_neutral: Some([1.0; 3]), ..Default::default() }; - let s = c.solve().unwrap(); - assert!(close(&s.matrix, &IDENTITY, 1e-6), "{:?}", s.matrix); - assert!((s.white_xy.0 - D65.0).abs() < 1e-4 && (s.white_xy.1 - D65.1).abs() < 1e-4, "{:?}", s.white_xy); - // a baseline exposure of +1 doubles everything - let e = Calibration { baseline_exposure: 1.0, ..c }.solve().unwrap(); - assert!(close(&e.matrix, &scale(&IDENTITY, 2.0), 1e-6)); - } - - #[test] - fn forward_matrix_white_balances_by_the_neutral() { - // FM = linear Rec. 709 → XYZ D50: the white-balanced camera values come out unchanged - let fm = mul(&bradford(&xy_to_xyz(D65), &xy_to_xyz(D50)).unwrap(), &rec709_to_xyz().unwrap()); - let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); - let c = Calibration { - color_matrix: [Some(cm), None], - forward_matrix: [Some(fm), None], - illuminant: [Some(21), None], - as_shot_neutral: Some([0.5, 1.0, 0.8]), - ..Default::default() - }; - let s = c.solve().unwrap(); - assert_eq!(s.neutral, [0.5, 1.0, 0.8]); - assert!(close(&s.matrix, &IDENTITY, 1e-6), "{:?}", s.matrix); - } - - #[test] - fn white_xy_gives_the_neutral() { - let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); - let c = Calibration { color_matrix: [Some(cm), None], illuminant: [Some(21), None], as_shot_white_xy: Some(D65), ..Default::default() }; - let s = c.solve().unwrap(); - for v in s.neutral { - assert!((v - s.neutral[1]).abs() < 1e-3, "D65 is neutral for an sRGB camera: {:?}", s.neutral); - } - } - - #[test] - fn hostile_calibrations_are_errors() { - let zero = Calibration { color_matrix: [Some([[0.0; 3]; 3]), None], as_shot_neutral: Some([1.0; 3]), ..Default::default() }; - assert!(zero.solve().is_err()); - let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); - let neg = Calibration { color_matrix: [Some(cm), None], as_shot_neutral: Some([-1.0, 1.0, 1.0]), ..Default::default() }; - assert!(neg.solve().is_err()); - let none = Calibration { as_shot_neutral: Some([0.5, 1.0, 0.5]), ..Default::default() }.solve().unwrap(); - assert_eq!(none.matrix, IDENTITY); - } -} diff --git a/crates/raw/src/dng_metadata_source_demosaic.rs b/crates/raw/src/dng_metadata_source_demosaic.rs deleted file mode 100644 index 911236929..000000000 --- a/crates/raw/src/dng_metadata_source_demosaic.rs +++ /dev/null @@ -1,255 +0,0 @@ -//! Demosaicing: one colour sample per pixel (a colour filter array) to three. -//! -//! - A 2 × 2 Bayer pattern (one red, two green, one blue) uses the gradient-corrected linear -//! interpolation of H. S. Malvar, L.-W. He and R. Cutler, "High-quality linear interpolation for -//! demosaicing of Bayer-patterned color images", IEEE ICASSP 2004: bilinear interpolation plus a -//! Laplacian correction from the pixel's own colour, as 5 × 5 filters. -//! - Any other pattern (up to 8 × 8) uses bilinear interpolation: the mean of the nearest samples -//! of each missing colour in the 3 × 3 neighbourhood, else the 5 × 5 one. -//! -//! The picture is extended by two pixels on every side by mirroring (without repeating the edge), -//! which keeps a 2 × 2 pattern's phase. - -#[cfg(feature = "threads")] -use rayon::prelude::*; - -/// A CFA pattern: `rows` × `cols` colour indices (0 red, 1 green, 2 blue), origin at the top left -/// of the image. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Pattern { - pub rows: usize, - pub cols: usize, - pub colors: Vec, -} - -impl Pattern { - pub fn color(&self, x: usize, y: usize) -> u8 { - self.colors.get((y % self.rows) * self.cols + x % self.cols).copied().unwrap_or(1) - } - - /// Whether this is a 2 × 2 Bayer pattern. - pub fn is_bayer(&self) -> bool { - if (self.rows, self.cols) != (2, 2) { - return false; - } - let [a, b, c, d] = self.colors[..] else { return false }; - let count = |k: u8| [a, b, c, d].iter().filter(|&&v| v == k).count(); - // the two greens sit on a diagonal - count(0) == 1 && count(1) == 2 && count(2) == 1 && ((a == 1 && d == 1) || (b == 1 && c == 1)) - } -} - -const PAD: usize = 2; - -/// Mirror index `i` (which may be up to PAD outside) into 0..n. -fn mirror(i: isize, n: usize) -> usize { - let n = n as isize; - let mut i = i; - if i < 0 { - i = -i; - } - if i >= n { - i = 2 * (n - 1) - i; - } - i.clamp(0, n - 1) as usize -} - -/// The plane extended by PAD mirrored pixels on each side. -fn padded(plane: &[f32], w: usize, h: usize) -> (Vec, usize) { - let pw = w + 2 * PAD; - let ph = h + 2 * PAD; - let mut out = vec![0f32; pw * ph]; - for y in 0..ph { - let sy = mirror(y as isize - PAD as isize, h); - for x in 0..pw { - let sx = mirror(x as isize - PAD as isize, w); - out[y * pw + x] = plane.get(sy * w + sx).copied().unwrap_or(0.0); - } - } - (out, pw) -} - -/// Demosaic `plane` (`w` × `h`, one sample per pixel) into interleaved RGB. -pub fn demosaic(plane: &[f32], w: usize, h: usize, pattern: &Pattern) -> Vec { - let mut out = vec![0f32; w * h * 3]; - if w == 0 || h == 0 || plane.len() < w * h { - return out; - } - let (p, pw) = padded(plane, w, h); - let bayer = pattern.is_bayer() && w >= 3 && h >= 3; - let row = |y: usize, o: &mut [f32]| { - if bayer { - malvar_row(&p, pw, y, w, pattern, o); - } else { - bilinear_row(&p, pw, y, w, h, pattern, o); - } - }; - #[cfg(feature = "threads")] - out.par_chunks_mut(w * 3).enumerate().for_each(|(y, o)| row(y, o)); - #[cfg(not(feature = "threads"))] - out.chunks_mut(w * 3).enumerate().for_each(|(y, o)| row(y, o)); - out -} - -fn malvar_row(p: &[f32], pw: usize, y: usize, w: usize, pat: &Pattern, out: &mut [f32]) { - let py = y + PAD; - for x in 0..w { - let px = x + PAD; - let at = |dx: isize, dy: isize| p[((py as isize + dy) as usize) * pw + (px as isize + dx) as usize]; - let c = at(0, 0); - let cross1 = at(-1, 0) + at(1, 0) + at(0, -1) + at(0, 1); - let cross2 = at(-2, 0) + at(2, 0) + at(0, -2) + at(0, 2); - let diag = at(-1, -1) + at(1, -1) + at(-1, 1) + at(1, 1); - let me = pat.color(x, y); - let rgb = match me { - 1 => { - let h1 = at(-1, 0) + at(1, 0); - let h2 = at(-2, 0) + at(2, 0); - let v1 = at(0, -1) + at(0, 1); - let v2 = at(0, -2) + at(0, 2); - // colour along the row and along the column - let along_row = (5.0 * c + 4.0 * h1 - h2 - diag + 0.5 * v2) / 8.0; - let along_col = (5.0 * c + 4.0 * v1 - v2 - diag + 0.5 * h2) / 8.0; - if pat.color(x + 1, y) == 0 { [along_row, c, along_col] } else { [along_col, c, along_row] } - } - _ => { - let g = (4.0 * c + 2.0 * cross1 - cross2) / 8.0; - let other = (6.0 * c + 2.0 * diag - 1.5 * cross2) / 8.0; - if me == 0 { [c, g, other] } else { [other, g, c] } - } - }; - if let Some(o) = out.get_mut(x * 3..x * 3 + 3) { - o.copy_from_slice(&rgb); - } - } -} - -fn bilinear_row(p: &[f32], pw: usize, y: usize, w: usize, h: usize, pat: &Pattern, out: &mut [f32]) { - for x in 0..w { - let me = pat.color(x, y); - let mut rgb = [0f32; 3]; - for (k, v) in rgb.iter_mut().enumerate() { - if usize::from(me) == k { - *v = p[(y + PAD) * pw + x + PAD]; - continue; - } - for r in 1..=PAD as isize { - let (mut sum, mut n) = (0f32, 0u32); - for dy in -r..=r { - for dx in -r..=r { - // the colour of a mirrored neighbour is the colour where it was mirrored from - let sx = mirror(x as isize + dx, w); - let sy = mirror(y as isize + dy, h); - if usize::from(pat.color(sx, sy)) == k { - sum += p[(sy + PAD) * pw + sx + PAD]; - n += 1; - } - } - } - if n > 0 { - *v = sum / n as f32; - break; - } - } - } - if let Some(o) = out.get_mut(x * 3..x * 3 + 3) { - o.copy_from_slice(&rgb); - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - fn rggb() -> Pattern { - Pattern { rows: 2, cols: 2, colors: vec![0, 1, 1, 2] } - } - - /// A mosaic of an RGB picture given per pixel. - fn mosaic(w: usize, h: usize, pat: &Pattern, rgb: impl Fn(usize, usize) -> [f32; 3]) -> Vec { - (0..h).flat_map(|y| (0..w).map(move |x| (x, y))).map(|(x, y)| rgb(x, y)[usize::from(pat.color(x, y))]).collect() - } - - #[test] - fn bayer_detection() { - assert!(rggb().is_bayer()); - for c in [[1, 0, 2, 1], [1, 2, 0, 1], [2, 1, 1, 0]] { - assert!(Pattern { rows: 2, cols: 2, colors: c.to_vec() }.is_bayer(), "{c:?}"); - } - assert!(!Pattern { rows: 2, cols: 2, colors: vec![0, 1, 2, 1] }.is_bayer(), "greens side by side"); - assert!(!Pattern { rows: 2, cols: 2, colors: vec![0, 0, 1, 2] }.is_bayer()); - assert!(!Pattern { rows: 1, cols: 3, colors: vec![0, 1, 2] }.is_bayer()); - } - - #[test] - fn flat_fields_stay_flat_for_every_bayer_phase_and_other_patterns() { - let pats = [[0, 1, 1, 2], [1, 0, 2, 1], [1, 2, 0, 1], [2, 1, 1, 0]].map(|c| Pattern { rows: 2, cols: 2, colors: c.to_vec() }); - let stripes = Pattern { rows: 1, cols: 3, colors: vec![0, 1, 2] }; - for pat in pats.iter().chain([&stripes]) { - let (w, h) = (9, 7); - let m = mosaic(w, h, pat, |_, _| [0.25, 0.5, 0.75]); - let out = demosaic(&m, w, h, pat); - for (i, px) in out.chunks(3).enumerate() { - for (k, want) in [0.25, 0.5, 0.75].iter().enumerate() { - assert!((px[k] - want).abs() < 1e-6, "{pat:?} pixel {i} channel {k}: {}", px[k]); - } - } - } - } - - #[test] - fn known_samples_are_kept_and_linear_ramps_are_exact() { - // a horizontal ramp is reconstructed exactly away from the border (all filters are - // symmetric and sum to one, the correction terms sum to zero) - let pat = rggb(); - let (w, h) = (16, 12); - let f = |x: usize, _y: usize| { - let v = x as f32 / 16.0; - [v, 0.5 * v + 0.1, 0.2 + 0.25 * v] - }; - let m = mosaic(w, h, &pat, f); - let out = demosaic(&m, w, h, &pat); - for y in 0..h { - for x in 0..w { - let px = &out[(y * w + x) * 3..][..3]; - let own = usize::from(pat.color(x, y)); - assert_eq!(px[own], m[y * w + x], "own sample kept"); - if (2..w - 2).contains(&x) { - let want = f(x, y); - for k in 0..3 { - assert!((px[k] - want[k]).abs() < 1e-5, "({x},{y}) ch {k}: {} vs {}", px[k], want[k]); - } - } - } - } - } - - #[test] - fn a_vertical_edge_does_not_smear_far() { - // white left half, black right half: pixels two columns from the edge are untouched - let pat = rggb(); - let (w, h) = (12, 8); - let m = mosaic(w, h, &pat, |x, _| if x < 6 { [1.0; 3] } else { [0.0; 3] }); - let out = demosaic(&m, w, h, &pat); - for y in 0..h { - for x in (0..3).chain(9..12) { - let want = if x < 6 { 1.0 } else { 0.0 }; - for k in 0..3 { - assert!((out[(y * w + x) * 3 + k] - want).abs() < 1e-6, "({x},{y}) ch {k}"); - } - } - } - } - - #[test] - fn tiny_and_empty_pictures_do_not_panic() { - let pat = rggb(); - for (w, h) in [(0, 0), (1, 1), (2, 1), (1, 5), (2, 2), (3, 2)] { - let m = vec![0.5; w * h]; - let out = demosaic(&m, w, h, &pat); - assert_eq!(out.len(), w * h * 3); - } - assert_eq!(demosaic(&[0.5; 3], 4, 4, &pat).len(), 48, "short plane"); - } -} diff --git a/crates/raw/src/dng_metadata_source_process.rs b/crates/raw/src/dng_metadata_source_process.rs deleted file mode 100644 index 41d9b25ae..000000000 --- a/crates/raw/src/dng_metadata_source_process.rs +++ /dev/null @@ -1,264 +0,0 @@ -//! Raw samples to a linear Rec. 709 picture, in the order of the DNG specification's chapter 5 -//! ("Mapping Raw Values to Linear Reference Values") and chapter 6 (colour): -//! -//! 1. ActiveArea: only the active (unmasked) sensor area is used; black levels, the CFA pattern -//! and the default crop are relative to its top left corner. -//! 2. LinearizationTable maps each sample. -//! 3. The black level (BlackLevel over its BlackLevelRepeatDim pattern, plus BlackLevelDeltaH for -//! the column and BlackLevelDeltaV for the row) is subtracted and the range up to WhiteLevel -//! scaled to 0–1 (clipped there). -//! 4. White balance: each sample is divided by the camera neutral of its colour and clipped at -//! 1, so highlights the sensor clipped stay neutral. -//! 5. A CFA is demosaiced ([`crate::demosaic`]). -//! 6. DefaultCropOrigin / DefaultCropSize cut the picture. -//! 7. The colour matrix of [`crate::color`] takes the white-balanced camera values to linear -//! Rec. 709 (including BaselineExposure); negative results are clipped to 0. - -#[cfg(feature = "threads")] -use rayon::prelude::*; - -use crate::color::{M3, V3}; -use crate::demosaic::{Pattern, demosaic}; -use crate::raw::RawImage; -use crate::{Error, Result}; - -/// The processing tags of a raw image. -#[derive(Clone, Debug, Default)] -pub struct Params { - /// top, left, bottom, right - pub active_area: Option<[usize; 4]>, - pub linearization: Option>, - /// rows, columns of the BlackLevel pattern - pub black_repeat: (usize, usize), - /// rows × columns × samples per pixel values - pub black: Vec, - pub black_delta_h: Vec, - pub black_delta_v: Vec, - /// per sample - pub white: Vec, - pub crop_origin: Option<(f64, f64)>, - pub crop_size: Option<(f64, f64)>, - /// CFA pattern in camera planes (`None`: LinearRaw). - pub cfa: Option, -} - -/// A rectangle: x, y, width, height. -type Rect = (usize, usize, usize, usize); - -/// The active area of a `w` × `h` raw image. -pub fn active_rect(p: &Params, w: usize, h: usize) -> Result { - match p.active_area { - None => Ok((0, 0, w, h)), - Some([top, left, bottom, right]) => { - if top >= bottom || left >= right || bottom > h || right > w { - return Err(Error::Malformed("ActiveArea outside the image")); - } - Ok((left, top, right - left, bottom - top)) - } - } -} - -/// The default crop inside an active area of `aw` × `ah`. -pub fn crop_rect(p: &Params, aw: usize, ah: usize) -> Rect { - let (ox, oy) = p.crop_origin.unwrap_or((0.0, 0.0)); - let (sw, sh) = p.crop_size.unwrap_or((aw as f64, ah as f64)); - let to = |v: f64, max: usize| if v.is_finite() && v > 0.0 { (v.min(max as f64)) as usize } else { 0 }; - let x = to(ox.floor(), aw.saturating_sub(1)); - let y = to(oy.floor(), ah.saturating_sub(1)); - let w = to(sw.round(), aw - x).max(1); - let h = to(sh.round(), ah - y).max(1); - (x, y, w, h) -} - -/// Normalised, white-balanced samples of the active area (steps 1–4), one plane per sample. -fn normalise(raw: &RawImage, p: &Params, (ax, ay, aw, ah): Rect, neutral: &V3, planes: usize) -> Result> { - let spp = raw.samples_per_pixel; - let (br, bc) = (p.black_repeat.0.max(1), p.black_repeat.1.max(1)); - if br > 64 || bc > 64 { - return Err(Error::Unsupported("a BlackLevelRepeatDim larger than 64".into())); - } - let need = br * bc * spp; - let black: Vec = match p.black.len() { - 0 => vec![0.0; need], - 1 => vec![p.black[0]; need], - n if n >= need => p.black[..need].to_vec(), - _ => return Err(Error::Malformed("BlackLevel has too few values")), - }; - let max_code = ((1u32 << raw.bits) - 1) as f64; - let white: Vec = (0..spp).map(|s| p.white.get(s).or(p.white.first()).copied().unwrap_or(max_code)).collect(); - let lin = p.linearization.as_deref().filter(|t| !t.is_empty()); - let inv_neutral: Vec = (0..planes).map(|c| (1.0 / neutral.get(c).copied().unwrap_or(1.0)) as f32).collect(); - let cfa = p.cfa.as_ref(); - let mut out = vec![0f32; aw * ah * spp]; - let row = |y: usize, o: &mut [f32]| { - let dv = p.black_delta_v.get(y).copied().unwrap_or(0.0); - let src_row = (ay + y) * raw.width * spp; - for x in 0..aw { - let dh = p.black_delta_h.get(x).copied().unwrap_or(0.0); - for s in 0..spp { - let code = raw.data.get(src_row + (ax + x) * spp + s).copied().unwrap_or(0); - let v = match lin { - Some(t) => f64::from(t.get(usize::from(code)).or(t.last()).copied().unwrap_or(0)), - None => f64::from(code), - }; - let b = black[((y % br) * bc + x % bc) * spp + s] + dh + dv; - let range = white[s] - b; - let n = if range > 0.0 { ((v - b) / range).clamp(0.0, 1.0) } else { 0.0 }; - let plane = match cfa { - Some(c) => usize::from(c.color(x, y)), - None => s, - }; - let wb = inv_neutral.get(plane).copied().unwrap_or(1.0); - if let Some(o) = o.get_mut(x * spp + s) { - *o = (n as f32 * wb).min(1.0); - } - } - } - }; - #[cfg(feature = "threads")] - out.par_chunks_mut((aw * spp).max(1)).enumerate().for_each(|(y, o)| row(y, o)); - #[cfg(not(feature = "threads"))] - out.chunks_mut((aw * spp).max(1)).enumerate().for_each(|(y, o)| row(y, o)); - Ok(out) -} - -/// A developed picture: premultiplied linear Rec. 709 RGBA floats (alpha 1). -pub struct Developed { - pub width: usize, - pub height: usize, - pub rgba: Vec, -} - -/// Develop `raw` (steps 1–7). `neutral` and `matrix` come from [`crate::color::Calibration::solve`]. -pub fn develop(raw: &RawImage, p: &Params, neutral: &V3, matrix: &M3) -> Result { - let spp = raw.samples_per_pixel; - if p.cfa.is_some() && spp != 1 { - return Err(Error::Malformed("a CFA image with more than one sample per pixel")); - } - if p.cfa.is_none() && !matches!(spp, 1 | 3) { - return Err(Error::Unsupported(format!("LinearRaw with {spp} samples per pixel"))); - } - let active = active_rect(p, raw.width, raw.height)?; - let (_, _, aw, ah) = active; - let planes = if p.cfa.is_some() { 3 } else { spp }; - let norm = normalise(raw, p, active, neutral, planes)?; - // three samples per pixel - let rgb = match (&p.cfa, spp) { - (Some(pat), _) => demosaic(&norm, aw, ah, pat), - (None, 3) => norm, - _ => norm.iter().flat_map(|&v| [v, v, v]).collect(), - }; - let (cx, cy, cw, ch) = crop_rect(p, aw, ah); - let m: [[f32; 3]; 3] = matrix.map(|r| r.map(|v| v as f32)); - let mono = p.cfa.is_none() && spp == 1; - let mut rgba = vec![0f32; cw * ch * 4]; - let row = |y: usize, o: &mut [f32]| { - let src = (cy + y) * aw + cx; - for x in 0..cw { - let Some(c) = rgb.get((src + x) * 3..(src + x) * 3 + 3) else { continue }; - let px = if mono { - // grey: the matrix's luminance gain only - let g = c[0] * (m[1][0] + m[1][1] + m[1][2]); - [g, g, g] - } else { - [0, 1, 2].map(|i| m[i][0] * c[0] + m[i][1] * c[1] + m[i][2] * c[2]) - }; - if let Some(d) = o.get_mut(x * 4..x * 4 + 4) { - d.copy_from_slice(&[px[0].max(0.0), px[1].max(0.0), px[2].max(0.0), 1.0]); - } - } - }; - #[cfg(feature = "threads")] - rgba.par_chunks_mut(cw * 4).enumerate().for_each(|(y, o)| row(y, o)); - #[cfg(not(feature = "threads"))] - rgba.chunks_mut(cw * 4).enumerate().for_each(|(y, o)| row(y, o)); - Ok(Developed { width: cw, height: ch, rgba }) -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::color::IDENTITY; - - fn raw(w: usize, h: usize, spp: usize, data: Vec) -> RawImage { - RawImage { width: w, height: h, samples_per_pixel: spp, bits: 12, data } - } - - #[test] - fn black_white_linearisation_and_white_balance() { - // LinearRaw 2 × 1: black 100, white 1100; neutral (0.5, 1, 0.25) - let r = raw(2, 1, 3, vec![600, 600, 600, 100, 1100, 2000]); - let p = Params { black: vec![100.0], white: vec![1100.0], ..Default::default() }; - let d = develop(&r, &p, &[0.5, 1.0, 0.25], &IDENTITY).unwrap(); - assert_eq!((d.width, d.height), (2, 1)); - // 0.5 / 0.5 = 1, 0.5 / 1, 0.5 / 0.25 = 2 → clipped to 1 - assert_eq!(&d.rgba[..4], &[1.0, 0.5, 1.0, 1.0]); - // below black → 0, at white → 1, above white → 1 - assert_eq!(&d.rgba[4..], &[0.0, 1.0, 1.0, 1.0]); - // a linearisation table doubling the codes - let t: Vec = (0..4096u32).map(|v| (v * 2).min(65_535) as u16).collect(); - let p = Params { linearization: Some(t), black: vec![200.0], white: vec![2200.0], ..p }; - let d = develop(&r, &p, &[1.0; 3], &IDENTITY).unwrap(); - assert_eq!(&d.rgba[..3], &[0.5, 0.5, 0.5]); - } - - #[test] - fn per_cfa_black_levels_and_deltas() { - // 2 × 2 CFA, black repeat 2 × 2 with a different black per site, plus row/column deltas - let pat = Pattern { rows: 2, cols: 2, colors: vec![0, 1, 1, 2] }; - let p = Params { - black_repeat: (2, 2), - black: vec![10.0, 20.0, 30.0, 40.0], - black_delta_h: vec![0.0, 5.0], - black_delta_v: vec![0.0, 1.0], - white: vec![1010.0], - cfa: Some(pat), - ..Default::default() - }; - let r = raw(2, 2, 1, vec![510, 525, 531, 546]); - let n = normalise(&r, &p, (0, 0, 2, 2), &[1.0; 3], 3).unwrap(); - // (v - black) / (white - black) for black 10, 25, 31, 46 - let want = [500.0 / 1000.0, 500.0 / 985.0, 500.0 / 979.0, 500.0 / 964.0]; - for (a, b) in n.iter().zip(want) { - assert!((a - b).abs() < 1e-6, "{a} vs {b}"); - } - } - - #[test] - fn active_area_and_default_crop() { - // 6 × 4 LinearRaw grey ramp; active area cuts a 1-pixel border, crop takes 2 × 1 at (1, 1) - let data: Vec = (0..24u16).flat_map(|i| [i * 10; 3]).collect(); - let r = raw(6, 4, 3, data); - let p = - Params { active_area: Some([1, 1, 3, 5]), white: vec![4095.0], crop_origin: Some((1.0, 1.0)), crop_size: Some((2.0, 1.0)), ..Default::default() }; - let d = develop(&r, &p, &[1.0; 3], &IDENTITY).unwrap(); - assert_eq!((d.width, d.height), (2, 1)); - // active (1,1)–(5,3); crop origin (1,1) inside it → raw (2, 2) and (3, 2): samples 14, 15 - assert!((d.rgba[0] - 140.0 / 4095.0).abs() < 1e-6); - assert!((d.rgba[4] - 150.0 / 4095.0).abs() < 1e-6); - // hostile active areas and crops - let bad = Params { active_area: Some([3, 0, 1, 6]), ..Default::default() }; - assert!(develop(&r, &bad, &[1.0; 3], &IDENTITY).is_err()); - let bad = Params { active_area: Some([0, 0, 4, 7]), ..Default::default() }; - assert!(develop(&r, &bad, &[1.0; 3], &IDENTITY).is_err()); - let huge = Params { crop_origin: Some((1e12, -5.0)), crop_size: Some((f64::NAN, 1e9)), white: vec![4095.0], ..Default::default() }; - let d = develop(&r, &huge, &[1.0; 3], &IDENTITY).unwrap(); - assert!(d.width >= 1 && d.width <= 6 && d.height >= 1 && d.height <= 4); - } - - #[test] - fn matrix_and_clipping_of_negatives() { - let r = raw(1, 1, 3, vec![4095, 0, 0]); - let p = Params { white: vec![4095.0], ..Default::default() }; - let m = [[1.0, 0.0, 0.0], [-0.5, 1.0, 0.0], [0.0, 0.0, 2.0]]; - let d = develop(&r, &p, &[1.0; 3], &m).unwrap(); - assert_eq!(&d.rgba, &[1.0, 0.0, 0.0, 1.0]); - } - - #[test] - fn short_black_level_is_an_error() { - let r = raw(2, 2, 1, vec![0; 4]); - let p = Params { black_repeat: (2, 2), black: vec![1.0, 2.0], cfa: Some(Pattern { rows: 2, cols: 2, colors: vec![0, 1, 1, 2] }), ..Default::default() }; - assert!(develop(&r, &p, &[1.0; 3], &IDENTITY).is_err()); - } -} diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index bfc098734..8f31f2f6e 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -93,6 +93,9 @@ mod arw_lens_rm6; /// Bounded DNG strip and tile sample reader. #[cfg(feature = "line-dng")] pub mod dng_lines; +/// Bounded DNG camera and processing metadata. +#[cfg(feature = "line-dng")] +pub mod dng_metadata; #[allow(dead_code)] mod huffman; #[cfg(feature = "jxl")] From a8fc359bc5a8fa0a951753b3792b8be17c04091d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:57:01 +0000 Subject: [PATCH 12/25] raw: share CinemaDNG capture metadata --- crates/raw/src/dng_metadata.rs | 298 ++++++++++++--------------------- 1 file changed, 107 insertions(+), 191 deletions(-) diff --git a/crates/raw/src/dng_metadata.rs b/crates/raw/src/dng_metadata.rs index c0b3e9e0b..9b74d8e6e 100644 --- a/crates/raw/src/dng_metadata.rs +++ b/crates/raw/src/dng_metadata.rs @@ -1,57 +1,41 @@ -//! Clean-room DNG and CinemaDNG decoder, implemented from the public specifications only (see the -//! crate README for editions): the TIFF 6.0 / TIFF-EP container, uncompressed and lossless JPEG -//! (ITU-T T.81 process 14) raw data, and the DNG processing model (linearisation, black and white -//! levels, white balance, demosaic, default crop, colour matrices) to a scene-linear Rec. 709 -//! picture. -//! -//! - [`sniff`]: is this a DNG (a TIFF with a DNGVersion tag)? -//! - [`Dng::parse`] → [`Dng::info`] (size, bit depth, CinemaDNG frame rate and time code), -//! [`Dng::raw`] (the raw samples) and [`Dng::develop`] (linear RGBA floats). -//! - [`decode`]: parse and develop in one call. -//! -//! Every number in the file is hostile: offsets, counts and sizes are checked, allocations are -//! capped by [`raw::MAX_SAMPLES`] and by the amount of data present, and the IFD walk is bounded. - -#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))] - -pub mod color; -pub mod demosaic; -pub mod ljpeg; -pub mod process; -pub mod raw; -pub mod tiff; -#[cfg(any(test, feature = "writer"))] -pub mod writer; - -use color::{Calibration, M3, V3}; -use demosaic::Pattern; -use process::Params; -use raw::{PHOTOMETRIC_CFA, RawImage}; -use tiff::{Entry, Ifd, Tiff, tags}; - -/// DNG decoding errors. -#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] -pub enum Error { - #[error("not a DNG file")] - NotDng, - #[error("truncated DNG data")] - Truncated, - #[error("raw image too large")] - TooLarge, - #[error("malformed DNG: {0}")] - Malformed(&'static str), - #[error("unsupported DNG: {0}")] - Unsupported(String), +//! Bounded DNG processing and camera metadata, before application rendering. +use crate::dng_lines::{self, Error, PHOTOMETRIC_CFA, Result}; +use craft_tiff::classic::{Entry, Ifd, Tiff, tags}; +type M3 = [[f64; 3]; 3]; +type V3 = [f64; 3]; +/// Camera-plane colour filter pattern. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Pattern { + pub rows: usize, + pub cols: usize, + pub colors: Vec, } - -pub type Result = std::result::Result; - -/// Whether `b` is a DNG: a TIFF file whose first IFD has a DNGVersion tag. -pub fn sniff(b: &[u8]) -> bool { - let Ok((t, first)) = Tiff::new(b) else { return false }; - t.read_ifd(first, 0).is_ok_and(|(ifd, _)| ifd.get(tags::DNG_VERSION).is_some()) +/// Parsed image processing tags. +#[derive(Clone, Debug, Default)] +pub struct Params { + pub active_area: Option<[usize; 4]>, + pub linearization: Option>, + pub black_repeat: (usize, usize), + pub black: Vec, + pub black_delta_h: Vec, + pub black_delta_v: Vec, + pub white: Vec, + pub crop_origin: Option<(f64, f64)>, + pub crop_size: Option<(f64, f64)>, + pub cfa: Option, +} +/// Parsed camera colour calibration. +#[derive(Clone, Debug, Default)] +pub struct Calibration { + pub color_matrix: [Option; 2], + pub camera_calibration: [Option; 2], + pub forward_matrix: [Option; 2], + pub illuminant: [Option; 2], + pub analog_balance: Option, + pub as_shot_neutral: Option, + pub as_shot_white_xy: Option<(f64, f64)>, + pub baseline_exposure: f64, } - /// SMPTE ST 12-1 time code of a CinemaDNG frame. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Timecode { @@ -76,85 +60,36 @@ impl Timecode { } } -/// What a DNG holds, without decoding the pixels. +/// File-level capture fields, without image geometry or rendering policy. #[derive(Clone, Debug, PartialEq)] -pub struct Info { - /// Size of the developed picture (the default crop). - pub width: usize, - pub height: usize, - /// Size of the stored raw image. - pub raw_width: usize, - pub raw_height: usize, - pub bits: u32, - pub samples_per_pixel: usize, - /// Colour filter array data (else LinearRaw). - pub cfa: bool, - /// "uncompressed" or "lossless JPEG". - pub compression: &'static str, +pub struct CaptureInfo { pub dng_version: [u8; 4], - /// CinemaDNG FrameRate (numerator, denominator). pub frame_rate: Option<(i64, i64)>, - /// CinemaDNG time code of this frame. pub timecode: Option, - /// UniqueCameraModel, else Make and Model. pub camera: Option, - /// CinemaDNG ReelName. pub reel: Option, } - -/// A developed DNG: premultiplied, scene-linear Rec. 709 RGBA floats (alpha 1), top row first. -/// A white surface the sensor saw at its clipping level is 1.0 times 2^BaselineExposure. -pub struct Image { - pub info: Info, - pub width: usize, - pub height: usize, - pub rgba: Vec, +/// Metadata reader with the file's main and raw IFDs. +pub struct Metadata<'a, 'i> { + pub tiff: Tiff<'a>, + pub main: &'i Ifd, + pub raw: &'i Ifd, } - -/// A parsed DNG file. -pub struct Dng<'a> { - tiff: Tiff<'a>, - ifds: Vec, - /// Index of the main raw IFD in `ifds` (the first is IFD 0). - raw: usize, -} - -impl<'a> Dng<'a> { - /// Parse the container and find the main raw image. - pub fn parse(bytes: &'a [u8]) -> Result> { - let (tiff, first) = Tiff::new(bytes)?; - let ifds = tiff.all_ifds(first)?; - if ifds.first().and_then(|i| i.get(tags::DNG_VERSION)).is_none() { - return Err(Error::NotDng); - } - let main = raw::find_raw_ifd(&tiff, &ifds)?; - let raw = ifds.iter().position(|i| i.offset == main.offset).ok_or(Error::Malformed("raw IFD"))?; - Ok(Dng { tiff, ifds, raw }) - } - - fn ifd0(&self) -> &Ifd { - static EMPTY: Ifd = Ifd { offset: 0, entries: Vec::new(), depth: 0 }; - self.ifds.first().unwrap_or(&EMPTY) - } - - fn raw_ifd(&self) -> &Ifd { - self.ifds.get(self.raw).unwrap_or(self.ifd0()) - } - +impl Metadata<'_, '_> { /// A raw-image tag: from the raw IFD, else IFD 0. fn raw_tag(&self, tag: u16) -> Option<&Entry> { - self.raw_ifd().get(tag).or_else(|| self.ifd0().get(tag)) + self.raw.get(tag).or_else(|| self.main.get(tag)) } /// A camera-wide tag: from IFD 0, else the raw IFD. fn main_tag(&self, tag: u16) -> Option<&Entry> { - self.ifd0().get(tag).or_else(|| self.raw_ifd().get(tag)) + self.main.get(tag).or_else(|| self.raw.get(tag)) } /// A numeric tag's values; a value outside the file (or a zero denominator) is an error, so a /// damaged file does not silently lose its white balance. fn floats(&self, e: Option<&Entry>) -> Result>> { - e.map(|e| self.tiff.floats(e)).transpose() + Ok(e.map(|e| self.tiff.floats(e)).transpose()?) } /// A 3 × 3 matrix tag (`None` when absent or not 3 × 3, e.g. for other colour counts). @@ -168,13 +103,9 @@ impl<'a> Dng<'a> { Ok(self.floats(self.main_tag(tag))?.and_then(|v| <[f64; 3]>::try_from(v.as_slice()).ok())) } - fn text(&self, tag: u16) -> Option { - self.main_tag(tag).and_then(|e| self.tiff.ascii(e).ok()).filter(|s| !s.is_empty()) - } - /// The CFA pattern in camera planes (`None` for LinearRaw). - fn cfa(&self) -> Result> { - let l = raw::layout(&self.tiff, self.raw_ifd())?; + pub fn cfa(&self) -> Result> { + let l = dng_lines::layout(&self.tiff, self.raw)?; if l.photometric != PHOTOMETRIC_CFA { return Ok(None); } @@ -211,7 +142,6 @@ impl<'a> Dng<'a> { Ok(Some(Pattern { rows, cols, colors })) } - /// The processing tags of the raw image. pub fn params(&self) -> Result { let uint_pair = |tag| -> Result> { match self.raw_tag(tag) { @@ -248,7 +178,7 @@ impl<'a> Dng<'a> { None => Ok(None), } }; - let list = |tag| -> Result> { self.raw_tag(tag).map(|e| self.tiff.floats(e)).transpose().map(Option::unwrap_or_default) }; + let list = |tag| -> Result> { Ok(self.raw_tag(tag).map(|e| self.tiff.floats(e)).transpose()?.unwrap_or_default()) }; Ok(Params { active_area, linearization, @@ -263,6 +193,33 @@ impl<'a> Dng<'a> { }) } + fn text(&self, tag: u16) -> Option { + self.main_tag(tag).and_then(|e| self.tiff.ascii(e).ok()).filter(|s| !s.is_empty()) + } + /// Read file-level CinemaDNG and camera fields. + pub fn capture_info(&self) -> CaptureInfo { + let dng_version = self + .main_tag(tags::DNG_VERSION) + .and_then(|e| self.tiff.uints(e).ok()) + .and_then(|v| v.get(..4).map(|v| [0, 1, 2, 3].map(|i| v[i] as u8))) + .unwrap_or_default(); + let frame_rate = self.main_tag(tags::FRAME_RATE).and_then(|e| { + let b = self.tiff.value_bytes(e).ok()?; + if !matches!(e.ty, craft_tiff::classic::Type::SRational | craft_tiff::classic::Type::Rational) { + return None; + } + let n = i64::from(self.tiff.u32_of(b.get(0..4)?.try_into().ok()?) as i32); + let d = i64::from(self.tiff.u32_of(b.get(4..8)?.try_into().ok()?) as i32); + (n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d).then_some((n, d)) + }); + let timecode = self.main_tag(tags::TIME_CODES).and_then(|e| self.tiff.value_bytes(e).ok()).and_then(Timecode::from_bytes); + let camera = self.text(tags::UNIQUE_CAMERA_MODEL).or_else(|| { + let mm = [self.text(tags::MAKE), self.text(tags::MODEL)].into_iter().flatten().collect::>().join(" "); + (!mm.is_empty()).then_some(mm) + }); + CaptureInfo { dng_version, frame_rate, timecode, camera, reel: self.text(tags::REEL_NAME) } + } + /// The colour tags. pub fn calibration(&self) -> Result { let illum = |tag| self.main_tag(tag).map(|e| self.tiff.uint(e)).transpose(); @@ -281,78 +238,37 @@ impl<'a> Dng<'a> { baseline_exposure: self.floats(self.main_tag(tags::BASELINE_EXPOSURE))?.and_then(|v| v.first().copied()).unwrap_or(0.0), }) } - - /// Size, depth and CinemaDNG metadata. - pub fn info(&self) -> Result { - let l = raw::layout(&self.tiff, self.raw_ifd())?; - let p = self.params()?; - let (_, _, aw, ah) = process::active_rect(&p, l.width, l.height)?; - let (_, _, width, height) = process::crop_rect(&p, aw, ah); - let dng_version = self - .main_tag(tags::DNG_VERSION) - .and_then(|e| self.tiff.uints(e).ok()) - .and_then(|v| v.get(..4).map(|v| [0, 1, 2, 3].map(|i| v[i] as u8))) - .unwrap_or_default(); - let frame_rate = self.main_tag(tags::FRAME_RATE).and_then(|e| { - let b = self.tiff.value_bytes(e).ok()?; - if !matches!(e.ty, tiff::Type::SRational | tiff::Type::Rational) { - return None; - } - let n = i64::from(self.tiff.u32_of(b.get(0..4)?.try_into().ok()?) as i32); - let d = i64::from(self.tiff.u32_of(b.get(4..8)?.try_into().ok()?) as i32); - (n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d).then_some((n, d)) - }); - let timecode = self.main_tag(tags::TIME_CODES).and_then(|e| self.tiff.value_bytes(e).ok()).and_then(Timecode::from_bytes); - let camera = self.text(tags::UNIQUE_CAMERA_MODEL).or_else(|| { - let mm = [self.text(tags::MAKE), self.text(tags::MODEL)].into_iter().flatten().collect::>().join(" "); - (!mm.is_empty()).then_some(mm) - }); - Ok(Info { - width, - height, - raw_width: l.width, - raw_height: l.height, - bits: l.bits, - samples_per_pixel: l.samples_per_pixel, - cfa: p.cfa.is_some(), - compression: if l.compression == 7 { "lossless JPEG" } else { "uncompressed" }, - dng_version, - frame_rate, - timecode, - camera, - reel: self.text(tags::REEL_NAME), - }) - } - - /// The raw samples of the main image, as stored (before any processing). - pub fn raw(&self) -> Result { - raw::read(&self.tiff, self.raw_ifd()) - } - - /// Develop the main image to scene-linear Rec. 709. - pub fn develop(&self) -> Result { - let info = self.info()?; - let p = self.params()?; - let raw = self.raw()?; - let calibration = self.calibration()?; - let solved = if p.cfa.is_none() && raw.samples_per_pixel == 1 { - // monochrome: no colour, only the exposure - let e = 2f64.powf(calibration.baseline_exposure.clamp(-16.0, 16.0)); - color::Solved { neutral: [1.0; 3], matrix: color::scale(&color::IDENTITY, e), white_xy: color::D65 } - } else { - calibration.solve()? - }; - let d = process::develop(&raw, &p, &solved.neutral, &solved.matrix)?; - Ok(Image { info, width: d.width, height: d.height, rgba: d.rgba }) - } } -/// Parse and develop a DNG file. -pub fn decode(bytes: &[u8]) -> Result { - Dng::parse(bytes)?.develop() -} +#[cfg(test)] +mod tests { + use super::*; -/// Parse a DNG file's metadata. -pub fn probe(bytes: &[u8]) -> Result { - Dng::parse(bytes)?.info() + #[test] + fn timecode_bcd_and_limits() { + assert_eq!( + Timecode::from_bytes(&[0x52, 0x34, 0x12, 0x09]), + Some(Timecode { frames: 12, seconds: 34, minutes: 12, hours: 9, drop_frame: true }) + ); + assert_eq!(Timecode::from_bytes(&[0, 0x60, 0, 0]), None); + assert_eq!(Timecode::from_bytes(&[0x0a, 0, 0, 0]), None); + assert_eq!(Timecode::from_bytes(&[0, 0, 0]), None); + } + + #[test] + fn calibration_defaults_and_rejects_bad_rational() -> Result<()> { + let empty = Ifd::default(); + let tiff = Tiff { data: &[], big_endian: false }; + let metadata = Metadata { tiff, main: &empty, raw: &empty }; + let calibration = metadata.calibration()?; + assert_eq!(calibration.baseline_exposure, 0.0); + assert_eq!(calibration.color_matrix, [None, None]); + + // IFD with one BaselineExposure SRational pointing past the end of the file. + let data = [b'I', b'I', 42, 0, 8, 0, 0, 0, 1, 0, 42, 198, 10, 0, 1, 0, 0, 0, 200, 0, 0, 0, 0, 0, 0, 0]; + let (tiff, first) = Tiff::new(&data)?; + let (ifd, _) = tiff.read_ifd(first, 0)?; + assert!(Metadata { tiff, main: &ifd, raw: &ifd }.calibration().is_err()); + Ok(()) + } } From 4374003c36d0dd595a2aa2ffeefce17c428b7422 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 23:26:23 -0500 Subject: [PATCH 13/25] raw: reject zero-pitch public opcode area iterators --- crates/raw/src/opcode_params.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/crates/raw/src/opcode_params.rs b/crates/raw/src/opcode_params.rs index 7c6997c98..7695992ad 100644 --- a/crates/raw/src/opcode_params.rs +++ b/crates/raw/src/opcode_params.rs @@ -28,11 +28,19 @@ pub struct Area { pub col_pitch: usize, } impl Area { - pub fn rows(&self) -> impl Iterator + use<> { - (self.top..self.bottom.max(self.top)).step_by(self.row_pitch) + /// Iterate sampled rows, rejecting a zero pitch even for caller-constructed areas. + pub fn rows(&self) -> Result + use<>, ParseError> { + if self.row_pitch == 0 { + return Err(ParseError::Malformed("opcode area with a zero pitch")); + } + Ok((self.top..self.bottom.max(self.top)).step_by(self.row_pitch)) } - pub fn cols(&self) -> impl Iterator + use<> { - (self.left..self.right.max(self.left)).step_by(self.col_pitch) + /// Iterate sampled columns, rejecting a zero pitch even for caller-constructed areas. + pub fn cols(&self) -> Result + use<>, ParseError> { + if self.col_pitch == 0 { + return Err(ParseError::Malformed("opcode area with a zero pitch")); + } + Ok((self.left..self.right.max(self.left)).step_by(self.col_pitch)) } } /// Borrowed bad-pixel coordinates, decoded lazily without copying the payload. @@ -290,6 +298,23 @@ mod tests { fn ctx() -> Context { Context { width: 4, height: 3, planes: 2, mosaic: false, allow_trim: true } } + #[test] + fn caller_constructed_areas_reject_zero_pitch() -> Result<(), ParseError> { + let mut area = Area { top: 1, left: 2, bottom: 7, right: 9, plane: 0, planes: 1, row_pitch: 2, col_pitch: 3 }; + assert_eq!(area.rows()?.collect::>(), vec![1, 3, 5]); + assert_eq!(area.cols()?.collect::>(), vec![2, 5, 8]); + area.row_pitch = 0; + area.col_pitch = 0; + assert!(area.rows().is_err()); + assert!(area.cols().is_err()); + // Empty/reversed bounds must not allow an invalid pitch through either. + area.bottom = 0; + area.right = 0; + assert!(area.rows().is_err()); + assert!(area.cols().is_err()); + Ok(()) + } + #[test] fn parses_all_variants_and_clamps_areas() -> Result<(), ParseError> { let area = u(&[0, 1, 8, 9, 1, 5, 1, 2]); From 741192a57c016918badb5e2f63125721aa4d28d2 Mon Sep 17 00:00:00 2001 From: Andy Ching <53651198+andyching168@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:15:06 +0800 Subject: [PATCH 14/25] craft-docx: write Word 2010 shapes inside mc:AlternateContent with a VML fallback A shape was written as a bare `wps:wsp` in `w:drawing`. Word 2007, and Word 2000-2003 with the Office Compatibility Pack, don't know `wps` and refuse the whole file. Word writes such shapes in `mc:AlternateContent`. `write_shape` now guards the drawing with `mc:Choice Requires="wps"` and, for rectangles, text boxes, rounded rectangles and ellipses, adds an `mc:Fallback` with the shape as VML: size, position, z-order, wrapping, fill, outline and the text box's text (copied from the choice's output, so the story isn't written twice). Other kinds get no fallback; an old reader skips that shape and still opens the file. Refs storytold/wordcraft#6849 --- crates/docx/src/write/story/drawing.rs | 19 ++- crates/docx/src/write/story/object.rs | 159 ++++++++++++++++++++++++- crates/docx/tests/it/main.rs | 1 + crates/docx/tests/it/roundtrip.rs | 5 +- crates/docx/tests/it/text_boxes.rs | 3 +- crates/docx/tests/it/wps_fallback.rs | 96 +++++++++++++++ 6 files changed, 275 insertions(+), 8 deletions(-) create mode 100644 crates/docx/tests/it/wps_fallback.rs diff --git a/crates/docx/src/write/story/drawing.rs b/crates/docx/src/write/story/drawing.rs index 7c03cca6e..94154f649 100644 --- a/crates/docx/src/write/story/drawing.rs +++ b/crates/docx/src/write/story/drawing.rs @@ -69,9 +69,18 @@ impl Writer<'_> { } /// A shape's or text box's `wps:wsp`, at `off` in its group (or 0, 0). In a group it carries - /// its own `wps:cNvPr` with drawing id `id`. - pub(super) fn wsp(&mut self, w: &mut W, o: &InlineObject, id: Option<&str>, off: (f32, f32), rels: &mut PartRels, depth: usize) { - let InlineObject::Shape { kind, w: sw, h: sh, fill, stroke, stroke_width, story, effects, float, freeform, extra } = o else { return }; + /// its own `wps:cNvPr` with drawing id `id`. Returns where in `w` its `w:txbxContent` was + /// written, for a VML fallback to repeat. + pub(super) fn wsp( + &mut self, + w: &mut W, + o: &InlineObject, + id: Option<&str>, + off: (f32, f32), + rels: &mut PartRels, + depth: usize, + ) -> Option> { + let InlineObject::Shape { kind, w: sw, h: sh, fill, stroke, stroke_width, story, effects, float, freeform, extra } = o else { return None }; let geom = freeform.as_deref().filter(|_| *kind == ShapeKind::Freeform); w.open("wps:wsp", &[]); if let Some(id) = id { @@ -181,6 +190,7 @@ impl Writer<'_> { // its place in it instead. let part = (*story).and_then(|id| self.doc.parts.get(&id).filter(|p| p.kind == craft_wordmodel::PartKind::TextBox).map(|p| (id, p))); let link = part.and_then(|(id, _)| self.txbx_links.get(&id).copied()); + let mut content = None; if let Some((n, seq)) = link.filter(|(_, seq)| *seq > 0) { w.empty("wps:linkedTxbx", &[("id", &n.to_string()), ("seq", &seq.to_string())]); } else if let Some((id, part)) = part @@ -190,6 +200,7 @@ impl Writer<'_> { Some((n, _)) => w.open("wps:txbx", &[("id", &n.to_string())]), None => w.open("wps:txbx", &[]), } + let start = w.s.len(); w.open("w:txbxContent", &[]); if self.written_boxes.insert(id) { let blocks = part.blocks.clone(); @@ -198,6 +209,7 @@ impl Writer<'_> { w.empty("w:p", &[]); } w.close("w:txbxContent"); + content = Some(start..w.s.len()); w.close("wps:txbx"); self.boxes.leave(); } @@ -231,6 +243,7 @@ impl Writer<'_> { w.close("wps:bodyPr"); } w.close("wps:wsp"); + content } /// A `w:drawing` holding `graphic` (an `a:graphic`) in a frame of the object's size and place. diff --git a/crates/docx/src/write/story/object.rs b/crates/docx/src/write/story/object.rs index 6cdd54f46..10f5ce03c 100644 --- a/crates/docx/src/write/story/object.rs +++ b/crates/docx/src/write/story/object.rs @@ -1,7 +1,7 @@ use super::drawing::{DrawingInfo, shape_name, spin_attrs, write_fill, xfrm_spin_only}; use super::field::{FieldData, field_run}; use super::section::comment_reference_style; -use super::{CharProps, Fill, InlineObject, NoteKind, PartRels, TextColor, W, Wrap, Writer, emu, n, rpr}; +use super::{Anchor, CharProps, Fill, Float, InlineObject, NoteKind, PartRels, ShapeKind, TextColor, W, Wrap, Writer, emu, n, rpr}; impl Writer<'_> { /// Write an inline object (`Bookmark`, `Comment`, `Equation`, `Field`, `Shape`, etc.). @@ -366,6 +366,11 @@ impl Writer<'_> { self.rev_open(w, props); w.open("w:r", &[]); self.rpr(w, props); + // A Word 2010 shape (`wps`) goes in `mc:AlternateContent`, as Word writes it: a reader + // that doesn't know `wps` (Word 2007, the Office Compatibility Pack) takes the VML + // fallback, or skips the shape when VML has no form for it, instead of refusing the file. + w.open("mc:AlternateContent", &[]); + w.open("mc:Choice", &[("Requires", "wps")]); w.open("w:drawing", &[]); let docpr = self.shape_docpr(extra.id); let link = Self::drawing_link(props, rels); @@ -387,15 +392,138 @@ impl Writer<'_> { w.empty("wp:cNvGraphicFramePr", &[]); w.open("a:graphic", &[]); w.open("a:graphicData", &[("uri", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape")]); - self.wsp(w, o, None, (0.0, 0.0), rels, depth); + let content = self.wsp(w, o, None, (0.0, 0.0), rels, depth); w.close("a:graphicData"); w.close("a:graphic"); w.close(if float.wrap == Wrap::Inline { "wp:inline" } else { "wp:anchor" }); w.close("w:drawing"); + w.close("mc:Choice"); + if freeform.is_none() && vml_tag(*kind).is_some() { + // The same text again: the fallback is a copy of the choice, not more of the story. + let content = content.and_then(|r| w.s.get(r)).map(str::to_owned); + w.open("mc:Fallback", &[]); + self.vml_shape(w, o, float, content.as_deref()); + w.close("mc:Fallback"); + } + w.close("mc:AlternateContent"); w.close("w:r"); self.rev_close(w, props); } + /// A shape as VML (`w:pict`), for readers older than Word 2010 shapes: its frame, place, + /// wrapping, fill, outline and text box. Only for a kind [`vml_tag`] names. + fn vml_shape(&self, w: &mut W, o: &InlineObject, float: &Float, content: Option<&str>) { + let InlineObject::Shape { kind, w: sw, h: sh, fill, stroke, stroke_width, story, extra, .. } = o else { return }; + let Some(tag) = vml_tag(*kind) else { return }; + let pt = |v: f32| format!("{:.2}pt", craft_wordmodel::geom::finite(v)); + let mut style = Vec::new(); + if float.wrap != Wrap::Inline { + style.push("position:absolute".to_string()); + style.push(format!("margin-left:{}", pt(float.x))); + style.push(format!("margin-top:{}", pt(float.y))); + } + style.push(format!("width:{}", pt(sw.max(0.0)))); + style.push(format!("height:{}", pt(sh.max(0.0)))); + let spin = float.spin(); + if spin.deg != 0.0 { + style.push(format!("rotation:{}", spin.deg)); + } + match (spin.flip_h, spin.flip_v) { + (true, true) => style.push("flip:x y".into()), + (true, false) => style.push("flip:x".into()), + (false, true) => style.push("flip:y".into()), + (false, false) => {} + } + if float.wrap != Wrap::Inline { + // The stacking order the choice was given; behind the text is below it. + let z = i64::from(self.z); + style.push(format!("z-index:{}", if float.wrap == Wrap::BehindText { -z } else { z })); + if let Some(a) = float.h_align { + style.push(format!("mso-position-horizontal:{}", a.ooxml(true))); + } + style.push(format!("mso-position-horizontal-relative:{}", vml_rel(float.h_rel, true))); + if let Some(a) = float.v_align { + style.push(format!("mso-position-vertical:{}", a.ooxml(false))); + } + style.push(format!("mso-position-vertical-relative:{}", vml_rel(float.v_rel, false))); + } + let body = (*story).and_then(|id| self.doc.parts.get(&id)).map(|p| p.body).unwrap_or_default(); + style.push( + match body.anchor { + craft_wordmodel::props::VAlign::Top => "v-text-anchor:top", + craft_wordmodel::props::VAlign::Center => "v-text-anchor:middle", + craft_wordmodel::props::VAlign::Bottom => "v-text-anchor:bottom", + } + .into(), + ); + let style = style.join(";"); + let mut attrs: Vec<(&str, String)> = vec![("style", style)]; + if *kind == ShapeKind::RoundedRectangle { + // The same fraction of the shorter side: VML's in 1/65536ths, DrawingML's in 1/100000ths. + let c = match extra.corner { + Some(c) => c.min(craft_wordmodel::connector::MAX_CORNER) as f32 / 100_000.0, + None => craft_wordmodel::connector::DEFAULT_CORNER, + }; + attrs.push(("arcsize", format!("{}f", (c * 65_536.0).round() as u32))); + } + if float.wrap != Wrap::Inline { + attrs.push(("o:allowincell", (if float.outside_cell { "f" } else { "t" }).into())); + } + let opacity = match fill.as_ref() { + Some(f) => { + attrs.push(("fillcolor", format!("#{}", f.main_color().hex()))); + match f { + Fill::Solid { transparency, .. } if *transparency > 0.0 => Some(format!("{:.4}", (1.0 - transparency / 100.0).clamp(0.0, 1.0))), + _ => None, + } + } + None => { + attrs.push(("filled", "f".into())); + None + } + }; + match stroke { + Some(c) => { + attrs.push(("strokecolor", format!("#{}", c.hex()))); + attrs.push(("strokeweight", pt(stroke_width.clamp(0.0, 100.0)))); + } + None => attrs.push(("stroked", "f".into())), + } + let attrs: Vec<(&str, &str)> = attrs.iter().map(|(k, v)| (*k, v.as_str())).collect(); + w.open("w:pict", &[]); + w.open(tag, &attrs); + if let Some(opacity) = &opacity { + w.empty("v:fill", &[("opacity", opacity)]); + } + if let Some(content) = content { + let inset = body.inset.map(|i| i.map(|v| pt(v.min(craft_wordmodel::MAX_TEXT_BOX_INSET) as f32 / 12_700.0)).join(",")); + let mut tb: Vec<(&str, &str)> = Vec::new(); + if let Some(inset) = &inset { + tb.push(("inset", inset)); + } + if body.auto_fit { + tb.push(("style", "mso-fit-shape-to-text:t")); + } + w.open("v:textbox", &tb); + w.raw(content); + w.close("v:textbox"); + } + let wrap = match float.wrap { + Wrap::Square => Some("square"), + Wrap::Tight => Some("tight"), + Wrap::Through => Some("through"), + Wrap::TopAndBottom => Some("topAndBottom"), + Wrap::Inline | Wrap::BehindText | Wrap::InFrontOfText => None, + }; + if let Some(wrap) = wrap { + let side = float.wrap_text.ooxml(); + let attrs: &[(&str, &str)] = if float.wrap_text.is_both() { &[("type", wrap)] } else { &[("type", wrap), ("side", side)] }; + w.empty("w10:wrap", attrs); + } + w.close(tag); + w.close("w:pict"); + } + fn write_group(&mut self, w: &mut W, o: &InlineObject, props: &CharProps, rels: &mut PartRels, depth: usize) { let InlineObject::Group { w: gw, h: gh, float, ch_w, ch_h, children, canvas } = o else { return }; self.rev_open(w, props); @@ -487,6 +615,33 @@ impl Writer<'_> { } } +/// The VML element that draws a shape kind, for the kinds VML has one for. +fn vml_tag(kind: ShapeKind) -> Option<&'static str> { + match kind { + ShapeKind::Rectangle | ShapeKind::TextBox => Some("v:rect"), + ShapeKind::RoundedRectangle => Some("v:roundrect"), + ShapeKind::Ellipse => Some("v:oval"), + _ => None, + } +} + +/// A floating object's anchor as VML names it (`mso-position-horizontal-relative`, `-vertical-`). +fn vml_rel(a: Anchor, horiz: bool) -> &'static str { + match (a, horiz) { + (Anchor::Margin, _) => "margin", + (Anchor::Page, _) => "page", + (Anchor::InsideMargin, _) => "inner-margin-area", + (Anchor::OutsideMargin, _) => "outer-margin-area", + (Anchor::LeftMargin, true) => "left-margin-area", + (Anchor::RightMargin, true) => "right-margin-area", + (Anchor::Character, true) => "char", + (Anchor::TopMargin, false) => "top-margin-area", + (Anchor::BottomMargin, false) => "bottom-margin-area", + (Anchor::Line, false) => "line", + _ => "text", + } +} + pub(super) fn ink_name(tool: craft_wordmodel::freeform::InkTool) -> &'static str { match tool { craft_wordmodel::freeform::InkTool::Pen => "Pen", diff --git a/crates/docx/tests/it/main.rs b/crates/docx/tests/it/main.rs index b0bab5833..d1def3f8c 100644 --- a/crates/docx/tests/it/main.rs +++ b/crates/docx/tests/it/main.rs @@ -49,3 +49,4 @@ mod undefined_style; mod vml_shapes; mod word_xml; mod wordml2003_write; +mod wps_fallback; diff --git a/crates/docx/tests/it/roundtrip.rs b/crates/docx/tests/it/roundtrip.rs index 8f3bbdd0b..65a01e6b4 100644 --- a/crates/docx/tests/it/roundtrip.rs +++ b/crates/docx/tests/it/roundtrip.rs @@ -956,7 +956,8 @@ fn toc_field_skips_nested_stories() { let mut xml = String::new(); std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); let boxes: Vec<(usize, usize)> = xml.match_indices("").map(|(a, _)| (a, a + xml[a..].find("").unwrap())).collect(); - assert_eq!(boxes.len(), 2, "{xml}"); + // Each box twice: its shape and its VML fallback. + assert_eq!(boxes.len(), 4, "{xml}"); let in_box = |i: usize| boxes.iter().any(|(a, b)| (*a..*b).contains(&i)); for kind in ["begin", "separate", "end"] { let at: Vec = xml.match_indices(&format!(r#"fldCharType="{kind}""#)).map(|(i, _)| i).collect(); @@ -964,7 +965,7 @@ fn toc_field_skips_nested_stories() { assert!(!in_box(at[0]), "field {kind} written inside a text box: {xml}"); } let (begin, end) = (xml.find(r#"fldCharType="begin""#).unwrap(), xml.find(r#"fldCharType="end""#).unwrap()); - assert!(begin > boxes[0].1 && end > boxes[1].1 && end < xml.find("").unwrap(), "{xml}"); + assert!(begin > boxes[1].1 && end > boxes[3].1 && end < xml.find("").unwrap(), "{xml}"); } #[test] diff --git a/crates/docx/tests/it/text_boxes.rs b/crates/docx/tests/it/text_boxes.rs index 608d22d33..a5824cafc 100644 --- a/crates/docx/tests/it/text_boxes.rs +++ b/crates/docx/tests/it/text_boxes.rs @@ -257,5 +257,6 @@ fn story_shared_by_many_shapes_is_written_once() { } let xml = document_xml(&craft_docx::write(&d).unwrap()); assert!(xml.len() < 2_000_000, "document.xml is {} bytes", xml.len()); - assert_eq!(xml.matches(text.as_str()).count(), 1); + // Once in the first shape and once in its VML fallback; the other shapes don't repeat it. + assert_eq!(xml.matches(text.as_str()).count(), 2); } diff --git a/crates/docx/tests/it/wps_fallback.rs b/crates/docx/tests/it/wps_fallback.rs new file mode 100644 index 000000000..ed9d36555 --- /dev/null +++ b/crates/docx/tests/it/wps_fallback.rs @@ -0,0 +1,96 @@ +//! Word 2010 shapes (`wps:wsp`) are written inside `mc:AlternateContent` with a VML fallback +//! (ECMA-376 Part 3, Markup Compatibility; Part 4 §14.1, VML), so a reader that doesn't know +//! `wps` — Word 2007, or Word 2000-2003 with the Office Compatibility Pack — takes the fallback +//! or skips the shape instead of refusing the whole file. + +use std::io::Read; + +use craft_wordmodel::para::{Anchor, FloatAlign, ShapeKind, Wrap}; +use craft_wordmodel::{Document, InlineObject, PartKind, TextBody, para_block}; + +fn document_xml(bytes: &[u8]) -> String { + let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let mut s = String::new(); + z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); + s +} + +fn shape(kind: ShapeKind, story: Option) -> InlineObject { + let float = craft_wordmodel::para::Float { + wrap: Wrap::Square, + h_rel: Anchor::Margin, + h_align: Some(FloatAlign::End), + v_rel: Anchor::Paragraph, + y: 0.05, + ..Default::default() + }; + InlineObject::Shape { + kind, + w: 10.7, + h: 12.2, + fill: None, + stroke: None, + stroke_width: 0.75, + float, + story, + effects: Default::default(), + freeform: None, + extra: Default::default(), + } +} + +/// A page number in a floating text box, as in a footer: the shape that made the +/// Compatibility Pack refuse the file. +#[test] +fn a_text_box_gets_a_vml_fallback_with_its_text() { + let mut d = Document::from_text("a"); + let id = d.add_part(PartKind::TextBox, vec![para_block(craft_wordmodel::Paragraph::with_text("29", Default::default()))]); + d.parts.get_mut(&id).unwrap().body = TextBody { auto_fit: true, inset: Some([0; 4]), ..Default::default() }; + d.insert_object(&craft_wordmodel::Pos::body(0, 1), shape(ShapeKind::TextBox, Some(id)), &Default::default()).unwrap(); + let bytes = craft_docx::write(&d).unwrap(); + let xml = document_xml(&bytes); + assert!(xml.contains(r#""#), "{xml}"); + assert!(!xml.contains(""), "no bare Word 2010 shape: {xml}"); + let fallback = &xml[xml.find("").expect("a fallback")..]; + assert!(fallback.starts_with(""#, + r#""#, + ] { + assert!(fallback.contains(want), "{want} in {fallback}"); + } + assert_eq!(xml.matches(">29").count(), 2, "the text in the choice and again in the fallback"); + + // Read back: the choice, once. + let r = craft_docx::read(&bytes).unwrap(); + let mut boxes = Vec::new(); + r.objects_in_reading_order(&mut |o| boxes.extend(o.text_boxes())); + boxes.retain(|id| r.parts.get(id).is_some_and(|p| p.kind == PartKind::TextBox)); + assert_eq!(boxes.len(), 1, "one text box, not the fallback as well"); + assert_eq!(r.plain_text(craft_wordmodel::StoryRef::Part(boxes[0])), "29"); +} + +/// A shape VML has no simple form for is still guarded, with no fallback: an old reader skips it. +#[test] +fn a_shape_without_a_vml_form_is_guarded_without_a_fallback() { + let mut d = Document::from_text("a"); + d.insert_object(&craft_wordmodel::Pos::body(0, 1), shape(ShapeKind::Star, None), &Default::default()).unwrap(); + let xml = document_xml(&craft_docx::write(&d).unwrap()); + assert!(xml.contains(r#""#), "{xml}"); + assert!(xml.contains(""), "{xml}"); + assert!(!xml.contains(""), "{xml}"); + let r = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap(); + let mut kinds = Vec::new(); + r.objects_in_reading_order(&mut |o| { + if let InlineObject::Shape { kind, .. } = o { + kinds.push(*kind); + } + }); + assert_eq!(kinds, vec![ShapeKind::Star]); +} From 489f3cbc129170b39b388e087b5ff3fb4fc2ccc2 Mon Sep 17 00:00:00 2001 From: Andy Ching <53651198+andyching168@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:32:11 +0800 Subject: [PATCH 15/25] craft-docx: guard a shape only when it gets a VML fallback; keep more in the fallback A shape guarded by `mc:AlternateContent` with no fallback is skipped by a reader that doesn't know `wps`, and dropped on that reader's next save: worse than refusing the file. Such shapes are written bare again; only shapes with a VML form are guarded. The VML fallback also keeps the shape's own stacking order (a floating object in its text box moved `self.z` on), its wrap distances, its hidden flag and its text box's text direction. --- crates/docx/src/write/story/object.rs | 50 +++++++++++++++++++------ crates/docx/tests/it/wps_fallback.rs | 54 ++++++++++++++++++++++----- 2 files changed, 83 insertions(+), 21 deletions(-) diff --git a/crates/docx/src/write/story/object.rs b/crates/docx/src/write/story/object.rs index 10f5ce03c..6b73aacd8 100644 --- a/crates/docx/src/write/story/object.rs +++ b/crates/docx/src/write/story/object.rs @@ -366,11 +366,16 @@ impl Writer<'_> { self.rev_open(w, props); w.open("w:r", &[]); self.rpr(w, props); - // A Word 2010 shape (`wps`) goes in `mc:AlternateContent`, as Word writes it: a reader - // that doesn't know `wps` (Word 2007, the Office Compatibility Pack) takes the VML - // fallback, or skips the shape when VML has no form for it, instead of refusing the file. - w.open("mc:AlternateContent", &[]); - w.open("mc:Choice", &[("Requires", "wps")]); + // A Word 2010 shape (`wps`) with a VML form goes in `mc:AlternateContent`, as Word writes + // it: a reader that doesn't know `wps` (Word 2007, the Office Compatibility Pack) takes the + // VML fallback instead of refusing the file. A shape without one stays bare: guarded with + // no fallback, that reader would open the file without the shape and drop it on its next + // save, which is worse than refusing the file. + let fallback = freeform.is_none() && vml_tag(*kind).is_some(); + if fallback { + w.open("mc:AlternateContent", &[]); + w.open("mc:Choice", &[("Requires", "wps")]); + } w.open("w:drawing", &[]); let docpr = self.shape_docpr(extra.id); let link = Self::drawing_link(props, rels); @@ -389,6 +394,8 @@ impl Writer<'_> { } let float = &float; self.drawing_open(w, DrawingInfo { float, size: (*sw, *sh), docpr: &docpr, name: &name, alt: o.alt_text(), link: link.as_deref() }); + // This shape's stacking order: floating objects in its text box move `self.z` on. + let z = self.z; w.empty("wp:cNvGraphicFramePr", &[]); w.open("a:graphic", &[]); w.open("a:graphicData", &[("uri", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape")]); @@ -397,22 +404,22 @@ impl Writer<'_> { w.close("a:graphic"); w.close(if float.wrap == Wrap::Inline { "wp:inline" } else { "wp:anchor" }); w.close("w:drawing"); - w.close("mc:Choice"); - if freeform.is_none() && vml_tag(*kind).is_some() { + if fallback { + w.close("mc:Choice"); // The same text again: the fallback is a copy of the choice, not more of the story. let content = content.and_then(|r| w.s.get(r)).map(str::to_owned); w.open("mc:Fallback", &[]); - self.vml_shape(w, o, float, content.as_deref()); + self.vml_shape(w, o, float, z, content.as_deref()); w.close("mc:Fallback"); + w.close("mc:AlternateContent"); } - w.close("mc:AlternateContent"); w.close("w:r"); self.rev_close(w, props); } /// A shape as VML (`w:pict`), for readers older than Word 2010 shapes: its frame, place, /// wrapping, fill, outline and text box. Only for a kind [`vml_tag`] names. - fn vml_shape(&self, w: &mut W, o: &InlineObject, float: &Float, content: Option<&str>) { + fn vml_shape(&self, w: &mut W, o: &InlineObject, float: &Float, z: u32, content: Option<&str>) { let InlineObject::Shape { kind, w: sw, h: sh, fill, stroke, stroke_width, story, extra, .. } = o else { return }; let Some(tag) = vml_tag(*kind) else { return }; let pt = |v: f32| format!("{:.2}pt", craft_wordmodel::geom::finite(v)); @@ -436,8 +443,14 @@ impl Writer<'_> { } if float.wrap != Wrap::Inline { // The stacking order the choice was given; behind the text is below it. - let z = i64::from(self.z); + let z = i64::from(z); style.push(format!("z-index:{}", if float.wrap == Wrap::BehindText { -z } else { z })); + // The same clearances from the text as the choice's `distL`/`distR`/`distT`/`distB`. + let d = pt(float.dist.clamp(0.0, 1584.0)); + style.push(format!("mso-wrap-distance-left:{d}")); + style.push(format!("mso-wrap-distance-right:{d}")); + style.push(format!("mso-wrap-distance-top:{}", pt(float.dist_top.clamp(0.0, 1584.0)))); + style.push(format!("mso-wrap-distance-bottom:{}", pt(float.dist_bottom.clamp(0.0, 1584.0)))); if let Some(a) = float.h_align { style.push(format!("mso-position-horizontal:{}", a.ooxml(true))); } @@ -456,6 +469,9 @@ impl Writer<'_> { } .into(), ); + if float.hidden { + style.push("visibility:hidden".into()); + } let style = style.join(";"); let mut attrs: Vec<(&str, String)> = vec![("style", style)]; if *kind == ShapeKind::RoundedRectangle { @@ -501,8 +517,18 @@ impl Writer<'_> { if let Some(inset) = &inset { tb.push(("inset", inset)); } + // Text direction as the VML reader takes it (stacked letters have no VML form). + let mut tb_style = match body.vert { + craft_wordmodel::props::TextVert::Vert => vec!["layout-flow:vertical"], + craft_wordmodel::props::TextVert::Vert270 => vec!["layout-flow:vertical", "mso-layout-flow-alt:bottom-to-top"], + craft_wordmodel::props::TextVert::Horz | craft_wordmodel::props::TextVert::Stacked => Vec::new(), + }; if body.auto_fit { - tb.push(("style", "mso-fit-shape-to-text:t")); + tb_style.push("mso-fit-shape-to-text:t"); + } + let tb_style = tb_style.join(";"); + if !tb_style.is_empty() { + tb.push(("style", &tb_style)); } w.open("v:textbox", &tb); w.raw(content); diff --git a/crates/docx/tests/it/wps_fallback.rs b/crates/docx/tests/it/wps_fallback.rs index ed9d36555..c22a31b4e 100644 --- a/crates/docx/tests/it/wps_fallback.rs +++ b/crates/docx/tests/it/wps_fallback.rs @@ -1,11 +1,12 @@ //! Word 2010 shapes (`wps:wsp`) are written inside `mc:AlternateContent` with a VML fallback //! (ECMA-376 Part 3, Markup Compatibility; Part 4 §14.1, VML), so a reader that doesn't know //! `wps` — Word 2007, or Word 2000-2003 with the Office Compatibility Pack — takes the fallback -//! or skips the shape instead of refusing the whole file. +//! instead of refusing the whole file. A shape with no VML form is left bare. use std::io::Read; use craft_wordmodel::para::{Anchor, FloatAlign, ShapeKind, Wrap}; +use craft_wordmodel::props::TextVert; use craft_wordmodel::{Document, InlineObject, PartKind, TextBody, para_block}; fn document_xml(bytes: &[u8]) -> String { @@ -46,7 +47,11 @@ fn a_text_box_gets_a_vml_fallback_with_its_text() { let mut d = Document::from_text("a"); let id = d.add_part(PartKind::TextBox, vec![para_block(craft_wordmodel::Paragraph::with_text("29", Default::default()))]); d.parts.get_mut(&id).unwrap().body = TextBody { auto_fit: true, inset: Some([0; 4]), ..Default::default() }; - d.insert_object(&craft_wordmodel::Pos::body(0, 1), shape(ShapeKind::TextBox, Some(id)), &Default::default()).unwrap(); + let mut o = shape(ShapeKind::TextBox, Some(id)); + if let InlineObject::Shape { float, .. } = &mut o { + (float.dist, float.dist_top, float.dist_bottom) = (18.0, 12.0, 6.0); + } + d.insert_object(&craft_wordmodel::Pos::body(0, 1), o, &Default::default()).unwrap(); let bytes = craft_docx::write(&d).unwrap(); let xml = document_xml(&bytes); assert!(xml.contains(r#""#), "{xml}"); @@ -58,6 +63,7 @@ fn a_text_box_gets_a_vml_fallback_with_its_text() { "mso-position-horizontal:right", "mso-position-horizontal-relative:margin", "mso-position-vertical-relative:text", + "mso-wrap-distance-left:18.00pt;mso-wrap-distance-right:18.00pt;mso-wrap-distance-top:12.00pt;mso-wrap-distance-bottom:6.00pt", r#"filled="f""#, r#"stroked="f""#, r#""#, @@ -76,16 +82,46 @@ fn a_text_box_gets_a_vml_fallback_with_its_text() { assert_eq!(r.plain_text(craft_wordmodel::StoryRef::Part(boxes[0])), "29"); } -/// A shape VML has no simple form for is still guarded, with no fallback: an old reader skips it. +/// The fallback keeps the box's own stacking order (not that of a floating object inside it), +/// its hidden flag and its text direction. #[test] -fn a_shape_without_a_vml_form_is_guarded_without_a_fallback() { +fn the_fallback_keeps_order_visibility_and_direction() { + let mut d = Document::from_text("a"); + let inner = d.add_part(PartKind::TextBox, vec![para_block(craft_wordmodel::Paragraph::with_text("inner", Default::default()))]); + let mut nested = shape(ShapeKind::TextBox, Some(inner)); + if let InlineObject::Shape { float, .. } = &mut nested { + float.z = 100; + } + let mut p = craft_wordmodel::Paragraph::with_text("outer", Default::default()); + p.insert_object(0, nested, &Default::default()).unwrap(); + let outer = d.add_part(PartKind::TextBox, vec![para_block(p)]); + d.parts.get_mut(&outer).unwrap().body = TextBody { vert: TextVert::Vert270, ..Default::default() }; + let mut o = shape(ShapeKind::TextBox, Some(outer)); + if let InlineObject::Shape { float, .. } = &mut o { + float.z = 10; + float.hidden = true; + } + d.insert_object(&craft_wordmodel::Pos::body(0, 1), o, &Default::default()).unwrap(); + let xml = document_xml(&craft_docx::write(&d).unwrap()); + // The outer box's VML: the `v:rect` holding the vertical text box (the inner box's fallback + // is copied into the outer one's text, so it comes after it). + let tb = xml.find(r#""#).expect("the outer box's text, turned"); + let rect = &xml[xml[..tb].rfind(""#), "{xml}"); - assert!(xml.contains(""), "{xml}"); - assert!(!xml.contains(""), "{xml}"); - let r = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap(); + let bytes = craft_docx::write(&d).unwrap(); + let xml = document_xml(&bytes); + assert!(xml.contains("") && xml.contains(r#""#), "{xml}"); + assert!(!xml.contains(""), "{xml}"); + let r = craft_docx::read(&bytes).unwrap(); let mut kinds = Vec::new(); r.objects_in_reading_order(&mut |o| { if let InlineObject::Shape { kind, .. } = o { From 6225363b1fdd580241bc214268a3c71a6309233b Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:33:43 +0100 Subject: [PATCH 16/25] raw: dng_metadata tests against hand-built DNGs and the full reader; document why it keeps its own tag reads --- crates/raw/src/dng_metadata.rs | 7 ++ crates/raw/tests/it/dng.rs | 2 +- crates/raw/tests/it/dng_metadata.rs | 114 ++++++++++++++++++++++++++++ crates/raw/tests/it/main.rs | 2 + 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 crates/raw/tests/it/dng_metadata.rs diff --git a/crates/raw/src/dng_metadata.rs b/crates/raw/src/dng_metadata.rs index b8ba5077f..4b1dda130 100644 --- a/crates/raw/src/dng_metadata.rs +++ b/crates/raw/src/dng_metadata.rs @@ -1,4 +1,11 @@ //! Bounded DNG processing and camera metadata, before application rendering. +//! +//! This is the [`crate::dng_lines`] companion on the same `craft_tiff::classic` IFDs. The full +//! reader's `dng::color_data` works on `craft_tiff::Tiff` and returns the values it will render +//! with (raw IFD first, implausible or singular values dropped, `BaselineExposureOffset` added). +//! This reader returns the tags as written, IFD 0 first, and fails on a value outside the file, +//! so a damaged file does not silently lose its white balance. For a well-formed file both read the +//! same matrices and neutral (see `tests/it/dng_metadata.rs`). use crate::dng_lines::{self, Error, PHOTOMETRIC_CFA, Result}; use craft_tiff::classic::{Entry, Ifd, Tiff, tags}; type M3 = [[f64; 3]; 3]; diff --git a/crates/raw/tests/it/dng.rs b/crates/raw/tests/it/dng.rs index 450b8ea96..21074a828 100644 --- a/crates/raw/tests/it/dng.rs +++ b/crates/raw/tests/it/dng.rs @@ -214,7 +214,7 @@ pub(super) fn dng(raw: IfdBuilder, order: ByteOrder) -> Vec { } /// [`dng`] with extra IFD 0 tags (where DNG writers put camera-profile tags). -fn dng_with(raw: IfdBuilder, order: ByteOrder, extra: impl FnOnce(&mut IfdBuilder)) -> Vec { +pub(super) fn dng_with(raw: IfdBuilder, order: ByteOrder, extra: impl FnOnce(&mut IfdBuilder)) -> Vec { let mut ifd0 = IfdBuilder::new(); ifd0.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![1])); ifd0.set(t::DNG_VERSION, Value::Byte(vec![1, 6, 0, 0])); diff --git a/crates/raw/tests/it/dng_metadata.rs b/crates/raw/tests/it/dng_metadata.rs new file mode 100644 index 000000000..5cf75ae2a --- /dev/null +++ b/crates/raw/tests/it/dng_metadata.rs @@ -0,0 +1,114 @@ +//! The bounded DNG metadata reader (`dng_metadata`) on hand-built DNGs, checked against the tags +//! written and, for the colour tags, against the full reader. + +use super::dng::{base_ifd, dng, dng_with}; +use craft_raw::dng_lines::{self, Error}; +use craft_raw::dng_metadata::{Metadata, Pattern}; +use craft_tiff::classic::{Tiff, tags as ct}; +use craft_tiff::{ByteOrder, IfdBuilder, ImageData, Value}; + +fn cfa_raw(edit: impl FnOnce(&mut IfdBuilder)) -> IfdBuilder { + let mut raw = base_ifd(8, 4, 16, 1, true); + raw.set(ct::COMPRESSION, Value::Short(vec![1])); + raw.set_image(ImageData::Strips { rows_per_strip: 4, strips: vec![vec![0; 64]] }); + edit(&mut raw); + raw +} + +fn with_metadata(bytes: &[u8], f: impl FnOnce(&Metadata) -> T) -> T { + let (tiff, first) = Tiff::new(bytes).unwrap(); + let ifds = tiff.all_ifds(first).unwrap(); + let raw = dng_lines::find_raw_ifd(&tiff, &ifds).unwrap(); + f(&Metadata { tiff, main: &ifds[0], raw }) +} + +#[test] +fn processing_tags_are_read_from_the_raw_ifd() { + let raw = cfa_raw(|r| { + r.set(ct::ACTIVE_AREA, Value::Long(vec![1, 2, 3, 7])); + r.set(ct::LINEARIZATION_TABLE, Value::Short(vec![0, 10, 20])); + r.set(ct::BLACK_LEVEL_REPEAT_DIM, Value::Short(vec![2, 2])); + r.set(ct::BLACK_LEVEL, Value::Rational(vec![(64, 1), (65, 1), (66, 1), (67, 1)])); + r.set(ct::BLACK_LEVEL_DELTA_H, Value::SRational(vec![(1, 2); 8])); + r.set(ct::BLACK_LEVEL_DELTA_V, Value::SRational(vec![(-1, 2); 4])); + r.set(ct::WHITE_LEVEL, Value::Long(vec![4000])); + r.set(ct::DEFAULT_CROP_ORIGIN, Value::Rational(vec![(1, 2), (1, 1)])); + r.set(ct::DEFAULT_CROP_SIZE, Value::Long(vec![6, 2])); + }); + for order in [ByteOrder::Little, ByteOrder::Big] { + let p = with_metadata(&dng(raw.clone(), order), |m| m.params().unwrap()); + assert_eq!(p.active_area, Some([1, 2, 3, 7])); + assert_eq!(p.linearization, Some(vec![0, 10, 20])); + assert_eq!(p.black_repeat, (2, 2)); + assert_eq!(p.black, [64.0, 65.0, 66.0, 67.0]); + assert_eq!(p.black_delta_h, [0.5; 8]); + assert_eq!(p.black_delta_v, [-0.5; 4]); + assert_eq!(p.white, [4000.0]); + assert_eq!((p.crop_origin, p.crop_size), (Some((0.5, 1.0)), Some((6.0, 2.0)))); + // base_ifd writes CFAPattern 1 0 2 1 (GRBG) + assert_eq!(p.cfa, Some(Pattern { rows: 2, cols: 2, colors: vec![1, 0, 2, 1] })); + } + // absent tags take the DNG defaults + let p = with_metadata(&dng(cfa_raw(|_| {}), ByteOrder::Little), |m| m.params().unwrap()); + assert_eq!((p.active_area, p.linearization, p.black_repeat), (None, None, (1, 1))); + assert!(p.black.is_empty() && p.white.is_empty() && p.crop_origin.is_none()); +} + +#[test] +fn cfa_patterns_map_plane_colours_and_refuse_odd_layouts() { + let pattern = |edit: fn(&mut IfdBuilder)| with_metadata(&dng(cfa_raw(edit), ByteOrder::Little), |m| m.cfa()); + // CFAPlaneColor reorders the planes: file colour 2 is plane 0 + let swapped = pattern(|r| { + r.set(ct::CFA_PLANE_COLOR, Value::Byte(vec![2, 1, 0])); + r.set(ct::CFA_LAYOUT, Value::Short(vec![1])); + }); + assert_eq!(swapped.unwrap(), Some(Pattern { rows: 2, cols: 2, colors: vec![1, 2, 0, 1] })); + let unsupported = |r: Result, Error>| matches!(r, Err(Error::Unsupported(_))); + let malformed = |r: Result, Error>| matches!(r, Err(Error::Malformed(_))); + assert!(unsupported(pattern(|r| { + r.set(ct::CFA_LAYOUT, Value::Short(vec![2])); + }))); + assert!(unsupported(pattern(|r| { + r.set(ct::CFA_PLANE_COLOR, Value::Byte(vec![0, 1, 3])); + }))); + assert!(unsupported(pattern(|r| { + r.set(ct::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![9, 2])); + }))); + assert!(malformed(pattern(|r| { + r.set(ct::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![2])); + }))); + assert!(malformed(pattern(|r| { + r.set(ct::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![4, 4])); + }))); + assert!(malformed(pattern(|r| { + r.set(ct::CFA_PATTERN, Value::Byte(vec![0, 1, 1, 3])); + }))); + // LinearRaw has no pattern + let mut linear = base_ifd(4, 2, 16, 3, false); + linear.set(ct::COMPRESSION, Value::Short(vec![1])); + linear.set_image(ImageData::Strips { rows_per_strip: 2, strips: vec![vec![0; 48]] }); + assert_eq!(with_metadata(&dng(linear, ByteOrder::Big), |m| m.cfa()).unwrap(), None); +} + +#[test] +fn calibration_matches_the_full_reader() { + let bytes = dng_with(cfa_raw(|_| {}), ByteOrder::Little, |ifd0| { + ifd0.set(ct::COLOR_MATRIX_2, Value::SRational(vec![(2, 1), (0, 1), (0, 1), (0, 1), (2, 1), (0, 1), (0, 1), (0, 1), (2, 1)])); + ifd0.set(ct::CALIBRATION_ILLUMINANT_2, Value::Short(vec![17])); + ifd0.set(ct::ANALOG_BALANCE, Value::Rational(vec![(1, 1); 3])); + ifd0.set(ct::AS_SHOT_NEUTRAL, Value::Rational(vec![(1, 2), (1, 1), (3, 4)])); + ifd0.set(ct::BASELINE_EXPOSURE, Value::SRational(vec![(1, 4)])); + // not 3 x 3 (a four-colour camera): not a matrix here + ifd0.set(ct::FORWARD_MATRIX_1, Value::SRational(vec![(1, 1); 12])); + }); + let c = with_metadata(&bytes, |m| m.calibration().unwrap()); + let full = craft_raw::decode(&bytes).unwrap().color; + assert_eq!(c.color_matrix, full.color_matrix); + assert_eq!(c.illuminant, [Some(21), Some(17)]); + assert_eq!(c.as_shot_neutral, full.as_shot_neutral); + assert_eq!(c.analog_balance, Some([1.0; 3])); + assert_eq!(c.as_shot_white_xy, Some((0.3127, 0.329))); + assert_eq!(c.baseline_exposure, 0.25); + assert_eq!(c.forward_matrix, [None, None]); + assert_eq!(c.camera_calibration, [None, None]); +} diff --git a/crates/raw/tests/it/main.rs b/crates/raw/tests/it/main.rs index 2f1b1061f..5a9386eb0 100644 --- a/crates/raw/tests/it/main.rs +++ b/crates/raw/tests/it/main.rs @@ -4,6 +4,8 @@ mod dng; #[cfg(feature = "line-dng")] mod dng_lines; +#[cfg(feature = "line-dng")] +mod dng_metadata; #[cfg(feature = "dng")] mod robust; mod vendor_robust; From 7feff2a02dac8ec7a4274d5ebc13db614c74d73d Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:35:12 +0100 Subject: [PATCH 17/25] raw: dng_metadata::Timecode is the crate Timecode; share the CinemaDNG frame-rate range with dng.rs Removes the duplicated Timecode type and BCD decoder (Sonar duplication 28%) and the copied frame-rate plausibility check; capture_info is tested against the full reader's CinemaTags. --- crates/raw/src/dng.rs | 8 +++++- crates/raw/src/dng_metadata.rs | 41 +++++++---------------------- crates/raw/tests/it/dng_metadata.rs | 31 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index 13bf252f3..efa1ec4f5 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -500,6 +500,12 @@ const FRAME_RATE: u16 = 51044; /// CinemaDNG `ReelName` (tag 51081). const REEL_NAME: u16 = 51081; +/// A CinemaDNG `FrameRate` of `n / d` frames a second between 1/1000 and 1000 (shared with the +/// bounded `dng_metadata` reader). +pub(crate) fn plausible_frame_rate(n: i64, d: i64) -> bool { + n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d +} + /// The CinemaDNG tags of a frame, from IFD 0 (where the specification puts them), else the raw IFD. A frame rate /// outside 1/1000 to 1000 frames a second and a time code that is not valid BCD are ignored. fn cinema_tags(ifd0: &Ifd, raw: &Ifd) -> CinemaTags { @@ -510,7 +516,7 @@ fn cinema_tags(ifd0: &Ifd, raw: &Ifd) -> CinemaTags { _ => None, }); CinemaTags { - frame_rate: frame_rate.filter(|&(n, d)| n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d), + frame_rate: frame_rate.filter(|&(n, d)| plausible_frame_rate(n, d)), timecode: pick(TIME_CODES).bytes(TIME_CODES).and_then(Timecode::from_bytes), reel_name: pick(REEL_NAME).string(REEL_NAME).map(|s| s.trim().to_owned()).filter(|s| !s.is_empty()), } diff --git a/crates/raw/src/dng_metadata.rs b/crates/raw/src/dng_metadata.rs index 105bb77d2..4b2249c43 100644 --- a/crates/raw/src/dng_metadata.rs +++ b/crates/raw/src/dng_metadata.rs @@ -43,29 +43,9 @@ pub struct Calibration { pub as_shot_white_xy: Option<(f64, f64)>, pub baseline_exposure: f64, } -/// SMPTE ST 12-1 time code of a CinemaDNG frame. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct Timecode { - pub hours: u8, - pub minutes: u8, - pub seconds: u8, - pub frames: u8, - pub drop_frame: bool, -} - -impl Timecode { - /// The first time code of a CinemaDNG TimeCodes tag (SMPTE ST 331 / 12-1 packing: BCD frames, - /// seconds, minutes, hours in bytes 0–3, the drop-frame flag in bit 6 of byte 0). - pub fn from_bytes(b: &[u8]) -> Option { - let bcd = |v: u8, tens_mask: u8| { - let (u, t) = (v & 0x0F, (v >> 4) & tens_mask); - (u <= 9).then_some(t * 10 + u) - }; - let [f, s, m, h, ..] = *b else { return None }; - let tc = Timecode { frames: bcd(f, 0x3)?, seconds: bcd(s, 0x7)?, minutes: bcd(m, 0x7)?, hours: bcd(h, 0x3)?, drop_frame: f & 0x40 != 0 }; - (tc.seconds < 60 && tc.minutes < 60 && tc.hours < 24).then_some(tc) - } -} +/// SMPTE ST 12-1 time code of a CinemaDNG frame: the crate's [`crate::Timecode`], which the full +/// reader's `CinemaTags` uses too. +pub use crate::Timecode; /// File-level capture fields, without image geometry or rendering policy. #[derive(Clone, Debug, PartialEq)] @@ -217,7 +197,7 @@ impl Metadata<'_, '_> { } let n = i64::from(self.tiff.u32_of(b.get(0..4)?.try_into().ok()?) as i32); let d = i64::from(self.tiff.u32_of(b.get(4..8)?.try_into().ok()?) as i32); - (n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d).then_some((n, d)) + crate::dng::plausible_frame_rate(n, d).then_some((n, d)) }); let timecode = self.main_tag(tags::TIME_CODES).and_then(|e| self.tiff.value_bytes(e).ok()).and_then(Timecode::from_bytes); let camera = self.text(tags::UNIQUE_CAMERA_MODEL).or_else(|| { @@ -252,14 +232,11 @@ mod tests { use super::*; #[test] - fn timecode_bcd_and_limits() { - assert_eq!( - Timecode::from_bytes(&[0x52, 0x34, 0x12, 0x09]), - Some(Timecode { frames: 12, seconds: 34, minutes: 12, hours: 9, drop_frame: true }) - ); - assert_eq!(Timecode::from_bytes(&[0, 0x60, 0, 0]), None); - assert_eq!(Timecode::from_bytes(&[0x0a, 0, 0, 0]), None); - assert_eq!(Timecode::from_bytes(&[0, 0, 0]), None); + fn timecode_is_the_crate_timecode() { + let tc = Timecode { frames: 12, seconds: 34, minutes: 12, hours: 9, drop_frame: true }; + assert_eq!(Timecode::from_bytes(&[0x52, 0x34, 0x12, 0x09]), Some(tc)); + let shared: crate::Timecode = tc; + assert_eq!(Timecode::from_bytes(&shared.to_bytes()), Some(tc)); } #[test] diff --git a/crates/raw/tests/it/dng_metadata.rs b/crates/raw/tests/it/dng_metadata.rs index 5cf75ae2a..dcdb65454 100644 --- a/crates/raw/tests/it/dng_metadata.rs +++ b/crates/raw/tests/it/dng_metadata.rs @@ -112,3 +112,34 @@ fn calibration_matches_the_full_reader() { assert_eq!(c.forward_matrix, [None, None]); assert_eq!(c.camera_calibration, [None, None]); } + +#[test] +fn capture_info_matches_the_full_readers_cinema_tags() { + use craft_raw::dng_metadata::Timecode; + let tc = Timecode { hours: 10, minutes: 59, seconds: 7, frames: 23, drop_frame: true }; + let bytes = dng_with(cfa_raw(|_| {}), ByteOrder::Big, |ifd0| { + ifd0.set(ct::FRAME_RATE, Value::SRational(vec![(24000, 1001)])); + ifd0.set(ct::TIME_CODES, Value::Byte(tc.to_bytes().to_vec())); + ifd0.set(ct::REEL_NAME, Value::Ascii("A001".into())); + ifd0.set(ct::MODEL, Value::Ascii("Cam".into())); + }); + let info = with_metadata(&bytes, |m| m.capture_info()); + let cinema = craft_raw::decode(&bytes).unwrap().cinema; + assert_eq!(info.dng_version, [1, 6, 0, 0]); + assert_eq!(info.frame_rate, Some((24000, 1001))); + assert_eq!((info.frame_rate, info.timecode), (cinema.frame_rate, cinema.timecode)); + assert_eq!(info.timecode, Some(tc)); + assert_eq!(info.reel.as_deref(), cinema.reel_name.as_deref()); + assert_eq!(info.camera.as_deref(), Some("Hand Cam")); + // an implausible rate and a broken time code are dropped by both; UniqueCameraModel wins + let bytes = dng_with(cfa_raw(|_| {}), ByteOrder::Little, |ifd0| { + ifd0.set(ct::FRAME_RATE, Value::Rational(vec![(5000, 1)])); + ifd0.set(ct::TIME_CODES, Value::Byte(vec![0x0a, 0, 0, 0, 0, 0, 0, 0])); + ifd0.set(ct::UNIQUE_CAMERA_MODEL, Value::Ascii("Hand Cam Mk II".into())); + }); + let info = with_metadata(&bytes, |m| m.capture_info()); + assert_eq!((info.frame_rate, info.timecode, info.reel), (None, None, None)); + assert_eq!(info.camera.as_deref(), Some("Hand Cam Mk II")); + let cinema = craft_raw::decode(&bytes).unwrap().cinema; + assert_eq!((cinema.frame_rate, cinema.timecode), (None, None)); +} From 250fc97fa8911d122716e9d6d598cd2d00c2de78 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:36:26 +0100 Subject: [PATCH 18/25] raw: opcode_params reads areas as the shared opcodes::Area and clamps them (Area::clamp); document how it differs from the typed opcodes model --- crates/raw/src/opcode_params.rs | 71 ++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 14 deletions(-) diff --git a/crates/raw/src/opcode_params.rs b/crates/raw/src/opcode_params.rs index 7695992ad..4118d6cef 100644 --- a/crates/raw/src/opcode_params.rs +++ b/crates/raw/src/opcode_params.rs @@ -1,4 +1,12 @@ //! Context-aware parsing of DNG opcode parameters. Image processing remains the caller's job. +//! +//! The typed [`crate::opcodes`] model is the file's own view (owned, serialisable, lenient: a +//! malformed opcode becomes `Unknown`, a zero pitch reads as 1, coordinates stay as written). This +//! module answers a different question for a caller about to apply an opcode to a given image: +//! parameters borrowed from the record (bad-pixel lists are not copied), checked strictly (a zero +//! pitch, a count larger than its bytes or a warp on a mosaic is an error) and areas clamped to the +//! image. The record envelope comes from [`crate::opcode_raw`]; the rectangle on the wire is +//! [`crate::opcodes::Area`], which [`Area::clamp`] turns into image coordinates. /// Image properties used to validate and clamp opcode parameters. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -123,24 +131,38 @@ impl<'a> Reader<'a> { Ok(a) } fn area(&mut self, c: Context) -> Result { - let mut a = [0usize; 8]; - for v in &mut a { - *v = self.u32()? as usize; - } - let [top, left, bottom, right, plane, planes, row_pitch, col_pitch] = a; - if row_pitch == 0 || col_pitch == 0 { + let wire = crate::opcodes::Area { + top: self.u32()?, + left: self.u32()?, + bottom: self.u32()?, + right: self.u32()?, + plane: self.u32()?, + planes: self.u32()?, + row_pitch: self.u32()?, + col_pitch: self.u32()?, + }; + Area::clamp(wire, c) + } +} +impl Area { + /// The area of an opcode as written ([`crate::opcodes::Area`]) clamped to the image of `c`: + /// coordinates to its size, the plane range to its planes. A zero pitch is an error here (the + /// typed reader reads it as 1). + pub fn clamp(wire: crate::opcodes::Area, c: Context) -> Result { + if wire.row_pitch == 0 || wire.col_pitch == 0 { return Err(ParseError::Malformed("opcode area with a zero pitch")); } - let plane = plane.min(c.planes); + let px = |v: u32| v as usize; + let plane = px(wire.plane).min(c.planes); Ok(Area { - top: top.min(c.height), - left: left.min(c.width), - bottom: bottom.min(c.height), - right: right.min(c.width), + top: px(wire.top).min(c.height), + left: px(wire.left).min(c.width), + bottom: px(wire.bottom).min(c.height), + right: px(wire.right).min(c.width), plane, - planes: planes.min(c.planes - plane), - row_pitch, - col_pitch, + planes: px(wire.planes).min(c.planes - plane), + row_pitch: px(wire.row_pitch), + col_pitch: px(wire.col_pitch), }) } } @@ -372,4 +394,25 @@ mod tests { let a = u(&[0, 0, 3, 4, 0, 1, 1, 1]); assert_eq!(parse(10, &[a, u(&[1]), f(&[f32::NAN])].concat(), ctx()), Err(ParseError::Malformed("non-finite opcode parameter"))); } + + #[test] + fn typed_areas_clamp_to_the_image() { + let c = ctx(); + let wire = crate::opcodes::Area { top: 1, left: 2, bottom: 9_999, right: 9_999, plane: 9, planes: 9, row_pitch: 2, col_pitch: 1 }; + let a = Area::clamp(wire, c).unwrap(); + assert_eq!((a.bottom, a.right), (c.height, c.width)); + assert_eq!((a.plane, a.planes), (c.planes, 0)); + assert!(Area::clamp(crate::opcodes::Area { row_pitch: 0, ..wire }, c).is_err()); + // the typed reader and this one agree on a well-formed area + let typed = crate::opcodes::Opcode::MapTable { area: crate::opcodes::Area { col_pitch: 1, ..wire }, table: vec![1, 2] }; + let bytes = crate::opcodes::write_list(&[typed]); + let record = &crate::opcode_raw::parse(&bytes, 1).unwrap()[0]; + match parse(record.id, &record.params, c).unwrap() { + Params::MapTable { area, table } => { + assert_eq!(area, a); + assert_eq!(table, [1, 2]); + } + other => panic!("MapTable expected, got {other:?}"), + } + } } From 5033f10bf2127693d2d56fda2d86ffb6d4b46758 Mon Sep 17 00:00:00 2001 From: Andy Ching <53651198+andyching168@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:51:31 +0800 Subject: [PATCH 19/25] craft-docx: keep behind-text shapes' order in the VML fallback A behind-text shape's fallback `z-index` was the negated `z`, which reverses the order of overlapping behind-text shapes. It is now `z` shifted below zero within signed 32-bit range, so a higher `z` still stacks higher; other shapes' `z-index` is capped at `i32::MAX`. --- crates/docx/src/write/story/object.rs | 8 +++++--- crates/docx/tests/it/wps_fallback.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/crates/docx/src/write/story/object.rs b/crates/docx/src/write/story/object.rs index 6b73aacd8..820b85ee7 100644 --- a/crates/docx/src/write/story/object.rs +++ b/crates/docx/src/write/story/object.rs @@ -442,9 +442,11 @@ impl Writer<'_> { (false, false) => {} } if float.wrap != Wrap::Inline { - // The stacking order the choice was given; behind the text is below it. - let z = i64::from(z); - style.push(format!("z-index:{}", if float.wrap == Wrap::BehindText { -z } else { z })); + // The stacking order the choice was given, as a signed 32-bit `z-index`. Behind the text + // is negative, shifted down rather than negated, so a higher `z` still stacks higher. + let z = i64::from(z.min(i32::MAX as u32)); + let z = if float.wrap == Wrap::BehindText { z - (i64::from(i32::MAX) + 1) } else { z }; + style.push(format!("z-index:{z}")); // The same clearances from the text as the choice's `distL`/`distR`/`distT`/`distB`. let d = pt(float.dist.clamp(0.0, 1584.0)); style.push(format!("mso-wrap-distance-left:{d}")); diff --git a/crates/docx/tests/it/wps_fallback.rs b/crates/docx/tests/it/wps_fallback.rs index c22a31b4e..e2d960cf9 100644 --- a/crates/docx/tests/it/wps_fallback.rs +++ b/crates/docx/tests/it/wps_fallback.rs @@ -111,6 +111,32 @@ fn the_fallback_keeps_order_visibility_and_direction() { assert!(rect.contains("visibility:hidden"), "{rect}"); } +/// Shapes behind the text keep their order in the fallback: a higher `z` stacks higher, below +/// the text (a negative `z-index` in signed 32-bit range). +#[test] +fn behind_text_shapes_keep_their_order() { + let mut d = Document::from_text("a"); + for z in [10, 20, u32::MAX] { + let mut o = shape(ShapeKind::Rectangle, None); + if let InlineObject::Shape { float, .. } = &mut o { + float.wrap = Wrap::BehindText; + float.z = z; + } + d.insert_object(&craft_wordmodel::Pos::body(0, 1), o, &Default::default()).unwrap(); + } + let xml = document_xml(&craft_docx::write(&d).unwrap()); + let mut got: Vec = xml + .match_indices("z-index:") + .map(|(i, m)| { + let v = &xml[i + m.len()..]; + v[..v.find([';', '"']).unwrap()].parse().unwrap() + }) + .collect(); + got.sort_unstable(); + let low = -(1i64 << 31); + assert_eq!(got, vec![low + 10, low + 20, -1], "{xml}"); +} + /// A shape VML has no simple form for stays a bare `wps:wsp`: guarded without a fallback, an old /// reader would open the file without it and drop it on its next save. #[test] From 7adcc9a6c2bcad498b97a645ac3391b5afb25dc6 Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:05:03 +0100 Subject: [PATCH 20/25] image-codecs: BMP keeps its resolution in the header's pixels per metre A BMP stores its resolution in biXPelsPerMeter and biYPelsPerMeter (BITMAPINFOHEADER and later). The image crate writes 0 there and nothing read the fields, so a 300 ppi image saved as BMP reopened at 72 ppi and an opened BMP's resolution was lost (storytold/photocraft#13630). decode now reads the fields into Metadata::dpi, encode writes Metadata::dpi into them whatever embed_metadata says, and caps(Format::Bmp).dpi is true. encode_bmp_indexed_with_dpi is the palette writer with a resolution; encode_bmp_indexed keeps its signature and output. --- crates/image-codecs/README.md | 5 +- crates/image-codecs/src/codecs.rs | 1 + crates/image-codecs/src/codecs/bmp.rs | 106 ++++++++++++++++++ crates/image-codecs/src/format.rs | 3 +- crates/image-codecs/src/lib.rs | 34 +++++- .../image-codecs/tests/it/bmp_resolution.rs | 77 +++++++++++++ crates/image-codecs/tests/it/main.rs | 1 + 7 files changed, 219 insertions(+), 8 deletions(-) create mode 100644 crates/image-codecs/src/codecs/bmp.rs create mode 100644 crates/image-codecs/tests/it/bmp_resolution.rs diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index dbd7dca4c..bcd42e4e8 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -43,7 +43,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | TIFF | yes | yes | U8, U16, F32 | all six (CMYK, CMYK+A included) | yes | yes (tag 34675) | no | yes (tag 700) | yes | yes (Description, Make, Model, Software, DateTime, Artist, Copyright) | no | `tiff` | | WebP | yes | yes | U8 | RGB, RGBA | yes | yes | yes | yes | no | no | yes (VP8, when `webp_lossless` is off) | `image-webp` / built-in VP8 | | GIF | yes | yes | U8 | RGBA | 1-bit | no | no | no | no | no | yes (256-colour palette) | `image` | -| BMP | yes | yes | U8 | RGB, RGBA | yes | no | no | no | no | no | no | `image` | +| BMP | yes | yes | U8 | RGB, RGBA | yes | no | no | no | yes (header pixels per metre) | no | no | `image` | | TGA | yes | yes | U8 | Gray, GrayA, RGB, RGBA | yes | no | no | no | no | no | no | `image` (read) / built in (RLE write, one scanline per packet) | | ICO | yes | yes | U8 | RGBA (at most 256×256) | yes | no | no | no | no | no | no | `image` | | DDS | yes | yes | U8 | Gray, GrayA, RGB, RGBA | yes | no | no | no | no | no | yes (BC1–BC3, when `EncodeOptions::dds` asks for one) | built in | @@ -79,7 +79,8 @@ the same. Palette BMP and PNG pixels decode as RGB(A) in `Image`; `bmp_palette` and `png_palette` expose the embedded table (and a PNG's transparent entry) so `photocraft-io` can open them in Indexed Color mode. `bmp_palette` returns the colours the header declares, not the decoder's 256-entry padding. -`encode_bmp_indexed` writes the inverse: indices plus a table as an 8-bit palette BMP. +`encode_bmp_indexed` writes the inverse: indices plus a table as an 8-bit palette BMP; +`encode_bmp_indexed_with_dpi` also writes a resolution in the header. ## Documented asymmetries and limitations diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs index ef607d378..e2faaac4c 100644 --- a/crates/image-codecs/src/codecs.rs +++ b/crates/image-codecs/src/codecs.rs @@ -1,6 +1,7 @@ pub(crate) mod avif; #[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))] pub(crate) mod avif_decode; +pub(crate) mod bmp; pub(crate) mod dds; pub(crate) mod dds_bc; pub(crate) mod deep_exr; diff --git a/crates/image-codecs/src/codecs/bmp.rs b/crates/image-codecs/src/codecs/bmp.rs new file mode 100644 index 000000000..239794a2c --- /dev/null +++ b/crates/image-codecs/src/codecs/bmp.rs @@ -0,0 +1,106 @@ +//! A BMP's resolution: `biXPelsPerMeter` and `biYPelsPerMeter`, the pixels per metre of the +//! BITMAPINFOHEADER and every later header (V4, V5, OS/2 2.x), at file offsets 38 and 42. The +//! 12-byte BITMAPCOREHEADER of OS/2 1.x has no such fields. The `image` crate reads and writes +//! the pixels only, and writes 0 here, which readers take as "no resolution". + +use super::png::dpi_of_ppm; + +/// Bytes before the fields: the 14-byte BITMAPFILEHEADER, then the DIB header's size, width, +/// height, planes, bit count, compression and image size. +const X_PPM: usize = 38; +const Y_PPM: usize = 42; +/// The shortest DIB header that carries the resolution (BITMAPINFOHEADER). +const INFO_HEADER: u32 = 40; +const METERS_PER_INCH: f64 = 0.0254; + +/// The little-endian 4 bytes at `at`, or `None` past the end of `bytes`. +fn le4(bytes: &[u8], at: usize) -> Option<[u8; 4]> { + bytes.get(at..at.checked_add(4)?)?.try_into().ok() +} + +/// Whether `bytes` start a BMP whose header has the resolution fields. +fn has_resolution_fields(bytes: &[u8]) -> bool { + bytes.starts_with(b"BM") && le4(bytes, Y_PPM).is_some() && le4(bytes, 14).map(u32::from_le_bytes).is_some_and(|dib| dib >= INFO_HEADER) +} + +/// The resolution in pixels per inch a BMP records, or `None` when its header has no fields for it +/// or either is 0 or negative (most writers leave them 0). A whole number of ppi stored rounded to +/// whole pixels per metre (300 ppi = 11811) reads back whole, as a PNG's `pHYs` does. +pub(crate) fn resolution(bytes: &[u8]) -> Option<(f32, f32)> { + if !has_resolution_fields(bytes) { + return None; + } + let field = |at: usize| le4(bytes, at).map(i32::from_le_bytes).and_then(|v| u32::try_from(v).ok()); + let (x, y) = (field(X_PPM)?, field(Y_PPM)?); + (x > 0 && y > 0).then(|| (dpi_of_ppm(x), dpi_of_ppm(y))) +} + +/// Pixels per metre for `ppi`, rounded as Photoshop and the PNG `pHYs` writer do (300 ppi = 11811), +/// or `None` for a resolution that has no positive whole number of pixels per metre. +fn ppm_of_dpi(ppi: f32) -> Option { + let ppm = (f64::from(ppi) / METERS_PER_INCH).round(); + (ppm.is_finite() && ppm >= 1.0).then(|| ppm.min(f64::from(i32::MAX)) as i32) +} + +/// Writes `dpi` into the resolution fields of the BMP in `bytes`. A header without the fields, or +/// a resolution that cannot be stored, leaves the bytes as they are. +pub(crate) fn set_resolution(bytes: &mut [u8], dpi: Option<(f32, f32)>) { + let Some((x, y)) = dpi.and_then(|(x, y)| Some((ppm_of_dpi(x)?, ppm_of_dpi(y)?))) else { return }; + if !has_resolution_fields(bytes) { + return; + } + for (at, ppm) in [(X_PPM, x), (Y_PPM, y)] { + if let Some(field) = bytes.get_mut(at..at + 4) { + field.copy_from_slice(&ppm.to_le_bytes()); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A 46-byte BMP start: file header, a DIB header of `dib` bytes, 11811 px/m each way. + fn start(dib: u32) -> Vec { + let mut b = vec![0u8; Y_PPM + 4]; + b[..2].copy_from_slice(b"BM"); + b[14..18].copy_from_slice(&dib.to_le_bytes()); + b[X_PPM..X_PPM + 4].copy_from_slice(&11811i32.to_le_bytes()); + b[Y_PPM..Y_PPM + 4].copy_from_slice(&11811i32.to_le_bytes()); + b + } + + #[test] + fn only_headers_with_the_fields_are_read_or_written() { + assert_eq!(resolution(&start(40)), Some((300.0, 300.0))); + assert_eq!(resolution(&start(124)), Some((300.0, 300.0)), "BITMAPV5HEADER"); + // OS/2 1.x's 12-byte core header has no resolution: those bytes are pixels or palette. + let mut core = start(12); + assert_eq!(resolution(&core), None); + set_resolution(&mut core, Some((72.0, 72.0))); + assert_eq!(core, start(12), "left as it is"); + // Too short, or not a BMP: nothing read, nothing written, no panic. + let mut short = start(40)[..Y_PPM + 3].to_vec(); + assert_eq!(resolution(&short), None); + set_resolution(&mut short, Some((72.0, 72.0))); + assert_eq!(short, start(40)[..Y_PPM + 3]); + let mut other = start(40); + other[0] = b'X'; + assert_eq!(resolution(&other), None); + set_resolution(&mut other, Some((72.0, 72.0))); + assert_eq!(&other[X_PPM..X_PPM + 4], &11811i32.to_le_bytes(), "not a BMP: left as it is"); + } + + #[test] + fn resolutions_without_a_whole_pixel_per_metre_are_not_written() { + for bad in [0.0, -300.0, 0.01, f32::NAN, f32::INFINITY] { + let mut b = start(40); + set_resolution(&mut b, Some((bad, 300.0))); + assert_eq!(b, start(40), "{bad}"); + } + // A huge one is capped at the field's maximum rather than wrapping negative. + let mut b = start(40); + set_resolution(&mut b, Some((1e12, 300.0))); + assert_eq!(&b[X_PPM..X_PPM + 4], &i32::MAX.to_le_bytes()); + } +} diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs index 9e55dc3d2..983dab3dd 100644 --- a/crates/image-codecs/src/format.rs +++ b/crates/image-codecs/src/format.rs @@ -308,7 +308,8 @@ pub fn caps(format: Format) -> FormatCaps { // Resolution travels in EXIF (WebP has no field of its own). Format::WebP => FormatCaps { icc: true, exif: true, xmp: true, dpi: true, animation: true, ..base }, Format::Gif => FormatCaps { layouts: &[L::Rgba], animation: true, lossy: true, ..base }, - Format::Bmp => base, + // Resolution in the header's pixels per metre (BITMAPINFOHEADER and later). + Format::Bmp => FormatCaps { dpi: true, ..base }, Format::Tga => FormatCaps { layouts: RGB_GRAY, ..base }, Format::Ico => FormatCaps { layouts: &[L::Rgba], ..base }, Format::Pnm => FormatCaps { depths: &[S::U8, S::U16, S::F32], layouts: ALL_LAYOUTS, ..base }, diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 7e2d29362..8fa607f3c 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -73,7 +73,7 @@ pub use half::f16; use crate::codecs::sgi; #[cfg(feature = "sunraster")] use crate::codecs::sunraster; -use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jpegls, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; +use crate::codecs::{bmp, dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jpegls, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; /// Detect the format and decode with default [`Limits`]. pub fn decode(bytes: &[u8]) -> Result { @@ -141,7 +141,11 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res Format::Avif => codecs::avif_decode::decode(bytes, l, opts.keep_orientation), #[cfg(not(all(feature = "avif-decode", not(target_arch = "wasm32"))))] Format::Avif => via_image::decode(format, bytes, l), - Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr => via_image::decode(format, bytes, l), + Format::Bmp => via_image::decode(format, bytes, l).map(|mut img| { + img.meta.dpi = bmp::resolution(bytes); + img + }), + Format::Gif | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr => via_image::decode(format, bytes, l), }?; finish_format(format, bytes, img, opts) } @@ -324,10 +328,26 @@ pub fn bmp_palette(bytes: &[u8]) -> Result>, CodecError> { /// Encodes `indices` (row-major, one byte per pixel) as an 8-bit palette BMP with the colour /// table `palette` (1 to 256 entries). Used for Indexed Color documents; BMP has no transparency. +/// The header's resolution is left unset; [`encode_bmp_indexed_with_dpi`] writes one. pub fn encode_bmp_indexed(width: u32, height: u32, indices: &[u8], palette: &[[u8; 3]]) -> Result, CodecError> { via_image::encode_bmp_indexed(width, height, indices, palette) } +/// [`encode_bmp_indexed`] with the resolution `dpi` (pixels per inch, horizontal then vertical) +/// in the header's pixels per metre, as [`encode`] writes [`Metadata::dpi`] for a BMP. `None`, or +/// a resolution with no positive whole number of pixels per metre, leaves the fields unset. +pub fn encode_bmp_indexed_with_dpi( + width: u32, + height: u32, + indices: &[u8], + palette: &[[u8; 3]], + dpi: Option<(f32, f32)>, +) -> Result, CodecError> { + let mut bytes = via_image::encode_bmp_indexed(width, height, indices, palette)?; + bmp::set_resolution(&mut bytes, dpi); + Ok(bytes) +} + /// Lists the pages (image directories) of a TIFF or BigTIFF file without decoding pixels: the /// main IFD chain and its SubIFDs, each with its size, kind (page, reduced-resolution copy or /// mask) and storage. [`TiffInfo::default_page`] is the page [`decode`] opens. @@ -545,8 +565,12 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result Err(CodecError::unsupported(format, "encoding is not available in this build")), Format::Jp2 => Err(CodecError::unsupported(format, "JPEG 2000 is import-only; save as PNG or TIFF instead")), Format::Jxl => jxl::encode(image, plan), - Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => { - via_image::encode(format, image, plan, opts) - } + // The `image` crate leaves a BMP's resolution fields 0; Photoshop fills them in. They are + // part of the header, not metadata, so they are written whatever `embed_metadata` says. + Format::Bmp => via_image::encode(format, image, plan, opts).map(|mut bytes| { + bmp::set_resolution(&mut bytes, image.meta.dpi); + bytes + }), + Format::Gif | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::encode(format, image, plan, opts), } } diff --git a/crates/image-codecs/tests/it/bmp_resolution.rs b/crates/image-codecs/tests/it/bmp_resolution.rs new file mode 100644 index 000000000..2e35f222c --- /dev/null +++ b/crates/image-codecs/tests/it/bmp_resolution.rs @@ -0,0 +1,77 @@ +//! A BMP keeps its resolution in the header's `biXPelsPerMeter` and `biYPelsPerMeter`, which +//! Photoshop writes and reads (storytold/photocraft#13630). BMP exports left both 0, so a 300 ppi +//! document saved as BMP opened at 72 ppi elsewhere, and an opened BMP's resolution was ignored. + +use crate::common::test_image; +use craft_image_codecs::*; + +/// The DIB header's size and its pixels per metre, horizontal then vertical (file offsets 14, 38 +/// and 42, little-endian). +fn header(bytes: &[u8]) -> (u32, i32, i32) { + let at = |i: usize| [bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3]]; + (u32::from_le_bytes(at(14)), i32::from_le_bytes(at(38)), i32::from_le_bytes(at(42))) +} + +/// A test image of `layout` at resolution `dpi`. +fn image(layout: ChannelLayout, dpi: Option<(f32, f32)>) -> Image { + let mut img = test_image(layout, SampleType::U8); + img.meta.dpi = dpi; + img +} + +#[test] +fn bmp_export_writes_the_resolution_in_pixels_per_metre_and_reads_it_back() { + // RGB is written with a BITMAPINFOHEADER, RGBA with a BITMAPV4HEADER: same field offsets. + for (layout, dib) in [(ChannelLayout::Rgb, 40), (ChannelLayout::Rgba, 108)] { + // The fields are part of the header, not metadata: embed_metadata off keeps them. + for embed_metadata in [true, false] { + let opts = EncodeOptions { embed_metadata, ..Default::default() }; + let img = image(layout, Some((300.0, 150.0))); + let bytes = encode(&img, Format::Bmp, &opts).unwrap(); + // 300 / 0.0254 = 11811.02 and 150 / 0.0254 = 5905.51, rounded. + assert_eq!(header(&bytes), (dib, 11811, 5906), "{layout:?} metadata {embed_metadata}"); + assert_eq!(decode(&bytes).unwrap().meta.dpi, Some((300.0, 150.0)), "{layout:?} metadata {embed_metadata}"); + assert!(!fidelity_warnings_with(&img, Format::Bmp, &opts).contains(&FidelityWarning::DpiDropped), "{layout:?}"); + } + } +} + +#[test] +fn bmp_without_a_resolution_leaves_the_fields_unset() { + let bytes = encode(&image(ChannelLayout::Rgb, None), Format::Bmp, &EncodeOptions::default()).unwrap(); + assert_eq!(header(&bytes), (40, 0, 0)); + assert_eq!(decode(&bytes).unwrap().meta.dpi, None, "0 means no resolution: the caller's default applies"); +} + +#[test] +fn bmp_opens_at_the_resolution_its_header_records() { + let bytes = encode(&image(ChannelLayout::Rgb, None), Format::Bmp, &EncodeOptions::default()).unwrap(); + let with = |x: i32, y: i32| { + let mut b = bytes.clone(); + b[38..42].copy_from_slice(&x.to_le_bytes()); + b[42..46].copy_from_slice(&y.to_le_bytes()); + decode(&b).unwrap().meta.dpi + }; + // What other writers store for 96 and 72 ppi reads back whole. + assert_eq!(with(3780, 3780), Some((96.0, 96.0))); + assert_eq!(with(2835, 2835), Some((72.0, 72.0))); + // 40000 px/m is exactly 1016 ppi; a value no whole ppi rounds to stays as it is (71.98). + let (x, y) = with(40000, 2834).unwrap(); + assert_eq!(x, 1016.0); + assert!((y - 71.9836).abs() < 1e-3, "{y}"); + // An axis that is 0 or negative leaves the resolution unknown. + assert_eq!(with(11811, 0), None); + assert_eq!(with(-11811, 11811), None); +} + +#[test] +fn indexed_bmp_writes_the_resolution_it_is_given() { + let (idx, pal) = ([0u8, 1, 1, 0], [[0, 0, 0], [255, 0, 0]]); + let bytes = encode_bmp_indexed_with_dpi(2, 2, &idx, &pal, Some((300.0, 300.0))).unwrap(); + assert_eq!(header(&bytes), (40, 11811, 11811)); + assert_eq!(decode(&bytes).unwrap().meta.dpi, Some((300.0, 300.0))); + let unset = encode_bmp_indexed_with_dpi(2, 2, &idx, &pal, None).unwrap(); + assert_eq!(header(&unset), (40, 0, 0)); + // The existing function keeps writing no resolution, so its callers' files don't change. + assert_eq!(encode_bmp_indexed(2, 2, &idx, &pal).unwrap(), unset); +} diff --git a/crates/image-codecs/tests/it/main.rs b/crates/image-codecs/tests/it/main.rs index 380e37565..69be177db 100644 --- a/crates/image-codecs/tests/it/main.rs +++ b/crates/image-codecs/tests/it/main.rs @@ -1,5 +1,6 @@ //! The crate's integration tests, linked into one test binary. +mod bmp_resolution; mod camera_raw; mod common; mod dds; From 9b87ff0a0e81199a745e1ae6684bafc879555588 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:34:00 +0100 Subject: [PATCH 21/25] raw: dng_metadata checks the LinearizationTable count before reading its entries --- crates/raw/src/dng_metadata.rs | 5 +++-- crates/raw/tests/it/dng_metadata.rs | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/raw/src/dng_metadata.rs b/crates/raw/src/dng_metadata.rs index 4b1dda130..6d8e6d958 100644 --- a/crates/raw/src/dng_metadata.rs +++ b/crates/raw/src/dng_metadata.rs @@ -135,10 +135,11 @@ impl Metadata<'_, '_> { }; let linearization = match self.raw_tag(tags::LINEARIZATION_TABLE) { Some(e) => { - let v = self.tiff.uints(e)?; - if v.len() > 65_536 { + // checked before `uints` allocates a u64 per entry + if e.count > 65_536 { return Err(Error::Malformed("LinearizationTable longer than 65536")); } + let v = self.tiff.uints(e)?; Some(v.into_iter().map(|x| x.min(65_535) as u16).collect()) } None => None, diff --git a/crates/raw/tests/it/dng_metadata.rs b/crates/raw/tests/it/dng_metadata.rs index 5cf75ae2a..2aa0e6d80 100644 --- a/crates/raw/tests/it/dng_metadata.rs +++ b/crates/raw/tests/it/dng_metadata.rs @@ -48,6 +48,11 @@ fn processing_tags_are_read_from_the_raw_ifd() { // base_ifd writes CFAPattern 1 0 2 1 (GRBG) assert_eq!(p.cfa, Some(Pattern { rows: 2, cols: 2, colors: vec![1, 0, 2, 1] })); } + // a LinearizationTable longer than 65536 entries is refused + let long = cfa_raw(|r| { + r.set(ct::LINEARIZATION_TABLE, Value::Short(vec![1; 65_537])); + }); + assert!(matches!(with_metadata(&dng(long, ByteOrder::Little), |m| m.params()), Err(Error::Malformed(_)))); // absent tags take the DNG defaults let p = with_metadata(&dng(cfa_raw(|_| {}), ByteOrder::Little), |m| m.params().unwrap()); assert_eq!((p.active_area, p.linearization, p.black_repeat), (None, None, (1, 1))); From 37124f86d72113a8fb91f4bb5a5fe0fca4753129 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 06:02:29 -0500 Subject: [PATCH 22/25] feat: add craft-mcp-server (JSON-RPC 2.0 MCP protocol) (#1649) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add craft-mcp-server (JSON-RPC 2.0 MCP protocol) Port from solidcraft: MCP server machinery any app may share, independent of transport and tools. - JSON-RPC 2.0 over stdio (newline-delimited) - MCP lifecycle: initialize, initialized, ping, notifications - Tools: register, list, call with schema validation - Resources: register, list, get by URI or template - Prompts: register, list, get with argument completion - Content blocks: text, image (base64), progress tokens No external dependencies beyond serde_json. Follows craft-libs safety pattern: #![deny(clippy::unwrap_used, ...)] * retrigger CI * retrigger CI * Record MCP server dependencies in workspace lockfile * chore: update rustls-webpki 0.103.15→0.103.16 and zip 9.1.0→9.1.3 --- Cargo.lock | 8 + crates/mcp-server/Cargo.toml | 17 ++ crates/mcp-server/src/content.rs | 64 ++++++ crates/mcp-server/src/lib.rs | 21 ++ crates/mcp-server/src/schema.rs | 132 +++++++++++ crates/mcp-server/src/server.rs | 330 +++++++++++++++++++++++++++ crates/mcp-server/src/tests.rs | 368 +++++++++++++++++++++++++++++++ 7 files changed, 940 insertions(+) create mode 100644 crates/mcp-server/Cargo.toml create mode 100644 crates/mcp-server/src/content.rs create mode 100644 crates/mcp-server/src/lib.rs create mode 100644 crates/mcp-server/src/schema.rs create mode 100644 crates/mcp-server/src/server.rs create mode 100644 crates/mcp-server/src/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 480e000e5..13362ace1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2145,6 +2145,14 @@ dependencies = [ "serde_json", ] +[[package]] +name = "craft-mcp-server" +version = "0.1.0" +dependencies = [ + "proptest", + "serde_json", +] + [[package]] name = "craft-media" version = "0.1.0" diff --git a/crates/mcp-server/Cargo.toml b/crates/mcp-server/Cargo.toml new file mode 100644 index 000000000..9b034c7cc --- /dev/null +++ b/crates/mcp-server/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "craft-mcp-server" +description = "MCP server machinery any app may share: JSON-RPC 2.0 over stdio, the MCP lifecycle, tool and resource methods, tool schemas and argument checks, image results" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +serde_json = { workspace = true } + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/mcp-server/src/content.rs b/crates/mcp-server/src/content.rs new file mode 100644 index 000000000..a2a017dec --- /dev/null +++ b/crates/mcp-server/src/content.rs @@ -0,0 +1,64 @@ +//! The content blocks a tool result is made of (MCP `CallToolResult`), and the contents a resource +//! read sends (MCP `ReadResourceResult`). + +use serde_json::{Value, json}; + +/// What `resources/read` sends for one resource: text of a MIME type, or bytes of a MIME type, +/// which go out base64-encoded. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Contents { + Text { mime_type: String, text: String }, + Blob { mime_type: String, data: Vec }, +} + +impl Contents { + /// `value` as JSON text (`application/json`). + pub fn json(value: &Value) -> Self { + Self::Text { mime_type: "application/json".into(), text: value.to_string() } + } + + /// The MCP contents block of resource `uri`. + pub(crate) fn block(&self, uri: &str) -> Value { + match self { + Self::Text { mime_type, text } => json!({"uri": uri, "mimeType": mime_type, "text": text}), + Self::Blob { mime_type, data } => json!({"uri": uri, "mimeType": mime_type, "blob": base64(data)}), + } + } +} + +/// A tool result of one text block, flagged as an error when `is_error` (the model sees the +/// message; an error is still a result, not a JSON-RPC error). +pub fn text_result(text: impl Into, is_error: bool) -> Value { + let mut r = json!({"content": [{"type": "text", "text": text.into()}]}); + if is_error { + r["isError"] = json!(true); + } + r +} + +/// A tool result of an image (`data` of type `mime`, base64-encoded here) followed by a text +/// block saying what it shows. +pub fn image_result(data: &[u8], mime: &str, text: impl Into) -> Value { + json!({"content": [ + {"type": "image", "data": base64(data), "mimeType": mime}, + {"type": "text", "text": text.into()}, + ]}) +} + +/// Standard base64 with padding (RFC 4648), as MCP image content carries it. +pub fn base64(data: &[u8]) -> String { + const T: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + let mut out = String::with_capacity(data.len().div_ceil(3) * 4); + for chunk in data.chunks(3) { + let b = [chunk.first().copied().unwrap_or(0), chunk.get(1).copied().unwrap_or(0), chunk.get(2).copied().unwrap_or(0)]; + let n = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]); + for (i, shift) in [18u32, 12, 6, 0].into_iter().enumerate() { + if i <= chunk.len() { + out.push(char::from(T[((n >> shift) & 63) as usize])); + } else { + out.push('='); + } + } + } + out +} diff --git a/crates/mcp-server/src/lib.rs b/crates/mcp-server/src/lib.rs new file mode 100644 index 000000000..17e6827c4 --- /dev/null +++ b/crates/mcp-server/src/lib.rs @@ -0,0 +1,21 @@ +//! MCP server machinery any app may share: JSON-RPC 2.0 over stdio (newline-delimited), the MCP +//! lifecycle, the tools, resources, prompts and completion methods, tool schemas with the check of +//! a call's arguments against them, and the content blocks tools reply with (text, images). +//! +//! An app implements [`Tools`]: what its tools are, how each one runs, its instructions, and the +//! resources, resource templates and prompts it serves, if any. [`Server`] speaks the protocol +//! around them and tells a client at `initialize` only what the app serves. Nothing here knows any +//! app's design, engine or renderer. +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +mod content; +mod schema; +mod server; + +pub use content::{Contents, base64, image_result, text_result}; +pub use schema::{Effect, check_arguments, object, string, tool}; +pub use server::{Incoming, Info, PROTOCOL_VERSION, Reference, SUPPORTED_VERSIONS, Server, Tools, read_message, response}; + +#[cfg(test)] +mod tests; diff --git a/crates/mcp-server/src/schema.rs b/crates/mcp-server/src/schema.rs new file mode 100644 index 000000000..3cc72da34 --- /dev/null +++ b/crates/mcp-server/src/schema.rs @@ -0,0 +1,132 @@ +//! Tool definitions (MCP `Tool`: name, title, description, input schema, annotations) and the +//! check of a call's arguments against the definition it was listed with. + +use serde_json::{Value, json}; + +/// What a tool may do, for the client's approval prompts (MCP tool annotations). Clients that +/// auto-approve on these hints must not be told less than the truth. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Effect { + /// Only looks. + ReadOnly, + /// Changes the app's document in memory, undoably. + Edits, + /// Replaces the app's document in memory (unsaved changes are lost). + Replaces, + /// May read, write, overwrite or delete files on disk. + Files, +} + +/// A tool's definition, its annotations from `effect`. +pub fn tool(name: &str, title: &str, desc: &str, schema: Value, effect: Effect) -> Value { + let (read_only, destructive, open_world) = match effect { + Effect::ReadOnly => (true, false, false), + Effect::Edits => (false, false, false), + Effect::Replaces => (false, true, false), + Effect::Files => (false, true, true), + }; + json!({ + "name": name, + "title": title, + "description": desc, + "inputSchema": schema, + "annotations": {"title": title, "readOnlyHint": read_only, "destructiveHint": destructive, "idempotentHint": read_only, "openWorldHint": open_world}, + }) +} + +/// An object schema with these `props`, the `required` ones, and no other keys. +pub fn object(props: Value, required: &[&str]) -> Value { + let mut o = json!({"type": "object", "properties": props, "additionalProperties": false}); + if !required.is_empty() { + o["required"] = json!(required); + } + o +} + +/// A string property described by `desc`. +pub fn string(desc: &str) -> Value { + json!({"type": "string", "description": desc}) +} + +fn type_matches(t: &str, v: &Value) -> bool { + match t { + "string" => v.is_string(), + "number" => v.is_number(), + "integer" => v.is_i64() || v.is_u64(), + "boolean" => v.is_boolean(), + "array" => v.is_array(), + "object" => v.is_object(), + "null" => v.is_null(), + _ => true, + } +} + +/// Check `args` against tool `name`'s input schema as `defs` list it (top level): unknown tool, +/// missing required arguments, unknown arguments, type and enum mismatches. +pub fn check_arguments(defs: &[Value], name: &str, args: &Value) -> Result<(), String> { + let def = defs.iter().find(|d| d["name"] == name).ok_or_else(|| format!("unknown tool `{name}`"))?; + let schema = &def["inputSchema"]; + let Some(a) = args.as_object() else { return Err(format!("{name}: arguments must be an object")) }; + for r in schema["required"].as_array().into_iter().flatten().filter_map(Value::as_str) { + if a.get(r).is_none_or(Value::is_null) { + return Err(format!("{name}: missing required argument `{r}`")); + } + } + let props = schema["properties"].as_object(); + for (k, v) in a { + let Some(p) = props.and_then(|p| p.get(k)) else { + let valid: Vec<&str> = props.map(|p| p.keys().map(String::as_str).collect()).unwrap_or_default(); + let valid = if valid.is_empty() { "none".to_string() } else { valid.join(", ") }; + let k: String = k.chars().take(64).collect(); + return Err(format!("{name}: unknown argument `{k}`; its arguments are: {valid}")); + }; + if v.is_null() { + continue; + } + // An argument of several types lists them as `anyOf` (Gemini refuses a list of types). + let types: Vec<&Value> = match p["anyOf"].as_array() { + Some(alts) => alts.iter().map(|alt| &alt["type"]).collect(), + None => vec![&p["type"]], + }; + let ok = types.iter().any(|t| match t { + Value::String(t) => type_matches(t, v), + Value::Array(ts) => ts.iter().filter_map(Value::as_str).any(|t| type_matches(t, v)), + _ => true, + }); + if !ok { + let expected = if types.len() == 1 { types[0].clone() } else { Value::Array(types.into_iter().cloned().collect()) }; + return Err(format!("{name}: argument `{k}` must be of type {expected}")); + } + if let Some(allowed) = p["enum"].as_array() + && !allowed.contains(v) + { + return Err(format!("{name}: `{k}` must be one of {}", Value::Array(allowed.clone()))); + } + } + Ok(()) +} + +/// Check `args` against prompt `name` as `defs` list it (MCP `Prompt`): unknown prompt, missing +/// required arguments, unknown arguments, values that are not strings. +pub(crate) fn check_prompt_arguments(defs: &[Value], name: &str, args: &Value) -> Result<(), String> { + let def = defs.iter().find(|d| d["name"] == name).ok_or_else(|| format!("unknown prompt `{name}`"))?; + let declared = def["arguments"].as_array().map(Vec::as_slice).unwrap_or_default(); + let Some(a) = args.as_object() else { return Err(format!("{name}: arguments must be an object")) }; + for r in declared.iter().filter(|d| d["required"] == true).filter_map(|d| d["name"].as_str()) { + if !a.contains_key(r) { + return Err(format!("{name}: missing required argument `{r}`")); + } + } + for (k, v) in a { + if !declared.iter().any(|d| d["name"] == k.as_str()) { + let valid: Vec<&str> = declared.iter().filter_map(|d| d["name"].as_str()).collect(); + let valid = if valid.is_empty() { "none".to_string() } else { valid.join(", ") }; + let k: String = k.chars().take(64).collect(); + return Err(format!("{name}: unknown argument `{k}`; its arguments are: {valid}")); + } + if !v.is_string() { + return Err(format!("{name}: argument `{k}` must be a string")); + } + } + Ok(()) +} diff --git a/crates/mcp-server/src/server.rs b/crates/mcp-server/src/server.rs new file mode 100644 index 000000000..b7ed05027 --- /dev/null +++ b/crates/mcp-server/src/server.rs @@ -0,0 +1,330 @@ +//! JSON-RPC 2.0 framing and the MCP lifecycle / tools / resources / prompts / completion methods. + +use std::io::{BufRead, Write}; + +use serde_json::{Value, json}; + +use crate::content::Contents; +use crate::schema::{check_arguments, check_prompt_arguments}; + +/// The newest protocol revision we speak; offered when the client asks for one we don't know. +pub const PROTOCOL_VERSION: &str = "2025-06-18"; +pub const SUPPORTED_VERSIONS: &[&str] = &[PROTOCOL_VERSION, "2025-03-26", "2024-11-05"]; + +const PARSE_ERROR: i64 = -32700; +const INVALID_REQUEST: i64 = -32600; +const METHOD_NOT_FOUND: i64 = -32601; +const INVALID_PARAMS: i64 = -32602; +const INTERNAL_ERROR: i64 = -32603; +const RESOURCE_NOT_FOUND: i64 = -32002; + +/// Most messages in one JSON-RPC batch. +const MAX_BATCH: usize = 1000; +/// Most suggestions in one `completion/complete` reply (the MCP limit). +const MAX_COMPLETIONS: usize = 100; + +/// What `completion/complete` asks about: a prompt by its name, or a resource by its URI or URI +/// template. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Reference<'a> { + Prompt(&'a str), + Resource(&'a str), +} + +/// An app's tools, served by [`Server`]. +pub trait Tools { + /// What every client is told at `initialize`. + fn instructions(&self) -> String; + /// Every tool's definition ([`crate::tool`]), as `tools/list` serves it now. + fn list(&mut self) -> Vec; + /// Run tool `name`. The server calls it right after the [`Tools::list`] its arguments were + /// checked against ([`crate::check_arguments`]), so it runs the tool as that listing defined + /// it. Failures are tool results with `isError: true` ([`crate::text_result`]); it never + /// panics on hostile arguments. + fn call(&mut self, name: &str, args: &Value) -> Value; + /// The resources `resources/list` serves (MCP `Resource`: uri, name, description, mimeType). + fn resources(&self) -> Vec { + Vec::new() + } + /// The resource templates `resources/templates/list` serves (MCP `ResourceTemplate`: + /// uriTemplate, name, description, mimeType). A URI made from one is read with + /// [`Tools::read_resource`]. + fn resource_templates(&self) -> Vec { + Vec::new() + } + /// The contents of resource `uri`: `None` when there is no such resource, `Err` when it could + /// not be read now. + fn read_resource(&mut self, uri: &str) -> Option> { + let _ = uri; + None + } + /// The prompts `prompts/list` serves (MCP `Prompt`: name, title, description, arguments). + fn prompts(&self) -> Vec { + Vec::new() + } + /// Prompt `name` filled in with `args` (MCP `GetPromptResult`: description, messages). The + /// server calls it only for a listed prompt, with `args` checked against that listing: an + /// object of strings with every required argument and no other. `Err` is an internal error. + fn get_prompt(&mut self, name: &str, args: &Value) -> Result { + let _ = args; + Err(format!("prompt `{name}` cannot be filled in")) + } + /// What argument `argument` of `reference` could be, given what has been typed (`value`). + /// Asked only of an app that serves prompts or resource templates; a prompt reference is + /// always a listed prompt. The reply holds the first 100. + fn complete(&mut self, reference: Reference<'_>, argument: &str, value: &str) -> Vec { + let _ = (reference, argument, value); + Vec::new() + } +} + +/// Who the server says it is at `initialize`, and the longest request line it reads. +#[derive(Clone, Copy, Debug)] +pub struct Info { + pub name: &'static str, + pub title: &'static str, + pub version: &'static str, + /// Longest accepted request line, in bytes; a longer one is refused in bounded space. + pub max_line: usize, +} + +/// An MCP server over the app's [`Tools`]. +pub struct Server { + info: Info, + tools: T, + initialized: bool, +} + +fn error(id: Value, code: i64, message: impl Into) -> Value { + json!({"jsonrpc": "2.0", "id": id, "error": {"code": code, "message": message.into()}}) +} + +/// One message read from a newline-delimited stream by [`read_message`]. +#[derive(Debug, PartialEq, Eq)] +pub enum Incoming { + /// A line to handle ([`Server::handle_line`]), without its newline. + Line(String), + /// A line refused unread (too long, not UTF-8): the error reply to send for it. + Refused(String), +} + +/// Read the next line of newline-delimited JSON-RPC from `input`; `None` once it closes. A line +/// longer than `max_line` is consumed in bounded space, so a client cannot exhaust memory by +/// sending an arbitrarily long request without a newline, and an unterminated last line counts. +pub fn read_message(input: &mut impl BufRead, max_line: usize) -> std::io::Result> { + let mut line = Vec::new(); + let mut oversized = false; + let mut received = false; + loop { + let chunk = input.fill_buf()?; + if chunk.is_empty() { + break; + } + let count = chunk.iter().position(|&byte| byte == b'\n').map_or(chunk.len(), |i| i + 1); + let newline = chunk[count - 1] == b'\n'; + let content_len = count - usize::from(newline); + if !oversized { + if line.len().saturating_add(content_len) > max_line { + oversized = true; + line.clear(); + } else { + line.extend_from_slice(&chunk[..content_len]); + } + } + input.consume(count); + received = true; + if newline { + break; + } + } + if !received { + return Ok(None); + } + if oversized { + return Ok(Some(Incoming::Refused(error(Value::Null, INVALID_REQUEST, "request too large").to_string()))); + } + Ok(Some(match String::from_utf8(line) { + Ok(line) => Incoming::Line(line), + // Not UTF-8: answer with a parse error and keep serving. + Err(_) => Incoming::Refused(error(Value::Null, PARSE_ERROR, "parse error: input is not UTF-8").to_string()), + })) +} + +/// The JSON-RPC response to request `id`, as [`Server::handle`] sends it. +pub fn response(id: Value, result: Value) -> Value { + json!({"jsonrpc": "2.0", "id": id, "result": result}) +} + +impl Server { + pub fn new(info: Info, tools: T) -> Self { + Self { info, tools, initialized: false } + } + pub fn tools(&mut self) -> &mut T { + &mut self.tools + } + pub fn is_initialized(&self) -> bool { + self.initialized + } + + /// Serve newline-delimited JSON-RPC until `input` closes. Only protocol messages go to + /// `output`; diagnostics belong on stderr. Consume oversized lines in bounded space so a + /// client cannot exhaust memory by sending an arbitrarily long request without a newline. + pub fn serve(&mut self, mut input: impl BufRead, mut output: impl Write) -> std::io::Result<()> { + while let Some(incoming) = read_message(&mut input, self.info.max_line)? { + let reply = match incoming { + Incoming::Line(line) => self.handle_line(&line), + Incoming::Refused(reply) => Some(reply), + }; + if let Some(reply) = reply { + output.write_all(reply.as_bytes())?; + output.write_all(b"\n")?; + output.flush()?; + } + } + Ok(()) + } + + /// One line in, at most one line out (notifications get no reply). + pub fn handle_line(&mut self, line: &str) -> Option { + // Check the raw input before trimming: a huge whitespace prefix is still a huge line. + if line.len() > self.info.max_line { + return Some(error(Value::Null, INVALID_REQUEST, "request too large").to_string()); + } + let line = line.trim(); + if line.is_empty() { + return None; + } + let reply = match serde_json::from_str::(line) { + Ok(Value::Array(batch)) if batch.is_empty() => Some(error(Value::Null, INVALID_REQUEST, "empty batch")), + Ok(Value::Array(batch)) if batch.len() > MAX_BATCH => Some(error(Value::Null, INVALID_REQUEST, "batch too large")), + Ok(Value::Array(batch)) => { + let replies: Vec = batch.into_iter().filter_map(|m| self.handle(m)).collect(); + (!replies.is_empty()).then_some(Value::Array(replies)) + } + Ok(msg) => self.handle(msg), + Err(e) => Some(error(Value::Null, PARSE_ERROR, format!("parse error: {e}"))), + }; + reply.map(|r| r.to_string()) + } + + pub fn handle(&mut self, msg: Value) -> Option { + let Value::Object(o) = &msg else { return Some(error(Value::Null, INVALID_REQUEST, "message must be an object")) }; + let id = o.get("id").cloned(); + let Some(method) = o.get("method").and_then(Value::as_str) else { + // A response to a request of ours (we send none) or junk. + if o.contains_key("result") || o.contains_key("error") { + return None; + } + return Some(error(id.unwrap_or(Value::Null), INVALID_REQUEST, "missing `method`")); + }; + let params = o.get("params").cloned().unwrap_or(Value::Null); + let Some(id) = id else { + if method == "notifications/initialized" { + self.initialized = true; + } + return None; + }; + Some(match self.request(method, ¶ms) { + Ok(r) => response(id, r), + Err((code, m)) => error(id, code, m), + }) + } + + fn request(&mut self, method: &str, params: &Value) -> Result { + match method { + "initialize" => { + let asked = params.get("protocolVersion").and_then(Value::as_str).unwrap_or(PROTOCOL_VERSION); + let version = if SUPPORTED_VERSIONS.contains(&asked) { asked } else { PROTOCOL_VERSION }; + let Info { name, title, version: server_version, .. } = self.info; + Ok(json!({ + "protocolVersion": version, + "capabilities": self.capabilities(), + "serverInfo": {"name": name, "title": title, "version": server_version}, + "instructions": self.tools.instructions(), + })) + } + "ping" => Ok(json!({})), + "tools/list" => Ok(json!({"tools": self.tools.list()})), + "tools/call" => { + let name = params.get("name").and_then(Value::as_str).ok_or((INVALID_PARAMS, "missing tool `name`".to_string()))?; + let args = match params.get("arguments") { + None | Some(Value::Null) => json!({}), + Some(a) => a.clone(), + }; + check_arguments(&self.tools.list(), name, &args).map_err(|e| (INVALID_PARAMS, e))?; + Ok(self.tools.call(name, &args)) + } + "resources/list" => Ok(json!({"resources": self.tools.resources()})), + "resources/templates/list" => Ok(json!({"resourceTemplates": self.tools.resource_templates()})), + "prompts/list" => Ok(json!({"prompts": self.tools.prompts()})), + "resources/read" => { + let uri = params.get("uri").and_then(Value::as_str).unwrap_or(""); + let contents = self + .tools + .read_resource(uri) + .ok_or_else(|| (RESOURCE_NOT_FOUND, format!("unknown resource `{uri}`")))? + .map_err(|e| (INTERNAL_ERROR, e))?; + Ok(json!({"contents": [contents.block(uri)]})) + } + "prompts/get" if !self.tools.prompts().is_empty() => { + let name = params.get("name").and_then(Value::as_str).ok_or((INVALID_PARAMS, "missing prompt `name`".to_string()))?; + let args = match params.get("arguments") { + None | Some(Value::Null) => json!({}), + Some(a) => a.clone(), + }; + check_prompt_arguments(&self.tools.prompts(), name, &args).map_err(|e| (INVALID_PARAMS, e))?; + self.tools.get_prompt(name, &args).map_err(|e| (INTERNAL_ERROR, e)) + } + "completion/complete" if self.completes() => { + let reference = params.get("ref").unwrap_or(&Value::Null); + let reference = match (reference.get("type").and_then(Value::as_str), reference.get("name"), reference.get("uri")) { + (Some("ref/prompt"), Some(Value::String(name)), _) => { + if !self.tools.prompts().iter().any(|p| p["name"] == name.as_str()) { + return Err((INVALID_PARAMS, format!("unknown prompt `{name}`"))); + } + Reference::Prompt(name) + } + (Some("ref/resource"), _, Some(Value::String(uri))) => Reference::Resource(uri), + _ => { + return Err(( + INVALID_PARAMS, + "`ref` must be a prompt's {type: ref/prompt, name} or a resource's {type: ref/resource, uri}".into(), + )); + } + }; + let argument = params.get("argument"); + let (Some(argument), Some(value)) = + (argument.and_then(|a| a.get("name")).and_then(Value::as_str), argument.and_then(|a| a.get("value")).and_then(Value::as_str)) + else { + return Err((INVALID_PARAMS, "`argument` must have a `name` and a `value`, both strings".into())); + }; + let mut values = self.tools.complete(reference, argument, value); + let total = values.len(); + values.truncate(MAX_COMPLETIONS); + Ok(json!({"completion": {"values": values, "total": total, "hasMore": total > MAX_COMPLETIONS}})) + } + _ => Err((METHOD_NOT_FOUND, format!("method `{method}` not found"))), + } + } + + /// What the server tells a client at `initialize` that it serves: the tools always, and the + /// rest only when the app has some. + fn capabilities(&self) -> Value { + let mut capabilities = json!({"tools": {"listChanged": false}}); + if !self.tools.resources().is_empty() || !self.tools.resource_templates().is_empty() { + capabilities["resources"] = json!({"listChanged": false}); + } + if !self.tools.prompts().is_empty() { + capabilities["prompts"] = json!({"listChanged": false}); + } + if self.completes() { + capabilities["completions"] = json!({}); + } + capabilities + } + + /// Whether the app has arguments to complete: those of its prompts and resource templates. + fn completes(&self) -> bool { + !self.tools.prompts().is_empty() || !self.tools.resource_templates().is_empty() + } +} diff --git a/crates/mcp-server/src/tests.rs b/crates/mcp-server/src/tests.rs new file mode 100644 index 000000000..b687ee2e8 --- /dev/null +++ b/crates/mcp-server/src/tests.rs @@ -0,0 +1,368 @@ +use serde_json::{Value, json}; + +use crate::{ + Contents, Effect, Info, PROTOCOL_VERSION, Reference, SUPPORTED_VERSIONS, Server, Tools, check_arguments, object, string, text_result, tool, +}; + +/// One app's tools: `echo` and `level`, nothing behind them (no engine, no design). `log` records +/// every listing and call in order. +#[derive(Default)] +struct Echo { + calls: Vec, + log: Vec, +} + +impl Tools for Echo { + fn instructions(&self) -> String { + "Echo test tools.".into() + } + fn list(&mut self) -> Vec { + self.log.push("list".into()); + vec![ + tool("echo", "Echo", "Say `text` back.", object(json!({"text": string("What to say")}), &["text"]), Effect::ReadOnly), + tool("level", "Level", "Set the level.", object(json!({"level": {"type": "string", "enum": ["low", "high"]}}), &[]), Effect::Edits), + ] + } + fn call(&mut self, name: &str, args: &Value) -> Value { + self.log.push(format!("call {name}")); + self.calls.push(json!({"name": name, "args": args})); + text_result(args["text"].as_str().unwrap_or_default(), false) + } +} + +fn info() -> Info { + Info { name: "echo", title: "Echo", version: "1.0.0", max_line: 1 << 16 } +} + +fn serve(lines: &[Value]) -> (Server, Vec) { + serve_with(Echo::default(), lines) +} + +fn serve_with(tools: T, lines: &[Value]) -> (Server, Vec) { + let input: String = lines.iter().map(|l| format!("{l}\n")).collect(); + let mut output = Vec::new(); + let mut s = Server::new(info(), tools); + s.serve(std::io::Cursor::new(input), &mut output).unwrap(); + let replies = String::from_utf8(output).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect(); + (s, replies) +} + +/// A registered tool is listed and called over the stdio transport, with no engine behind it. +#[test] +fn a_registered_tool_is_served_over_stdio() { + let (mut s, replies) = serve(&[ + json!({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-03-26"}}), + json!({"jsonrpc": "2.0", "method": "notifications/initialized"}), + json!({"jsonrpc": "2.0", "id": 2, "method": "tools/list"}), + json!({"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "echo", "arguments": {"text": "hi"}}}), + ]); + assert_eq!(replies.len(), 3, "the notification gets no reply: {replies:?}"); + let init = &replies[0]["result"]; + assert_eq!(init["protocolVersion"], "2025-03-26"); + assert_eq!(init["serverInfo"], json!({"name": "echo", "title": "Echo", "version": "1.0.0"})); + assert_eq!(init["instructions"], "Echo test tools."); + assert!(s.is_initialized()); + assert_eq!(replies[1]["result"]["tools"][0]["name"], "echo"); + assert_eq!(replies[1]["result"]["tools"][0]["annotations"]["readOnlyHint"], true); + assert_eq!(replies[2], json!({"jsonrpc": "2.0", "id": 3, "result": {"content": [{"type": "text", "text": "hi"}]}})); + assert_eq!(s.tools().calls, vec![json!({"name": "echo", "args": {"text": "hi"}})]); +} + +/// Arguments are checked against the tool's schema before the tool runs; unknown tools and +/// methods are JSON-RPC errors. +#[test] +fn arguments_break_the_schema_before_the_tool_runs() { + let (mut s, replies) = serve(&[ + json!({"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "echo", "arguments": {}}}), + json!({"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "echo", "arguments": {"text": 3}}}), + json!({"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "echo", "arguments": {"text": "a", "loud": true}}}), + json!({"jsonrpc": "2.0", "id": 4, "method": "tools/call", "params": {"name": "shout", "arguments": {}}}), + json!({"jsonrpc": "2.0", "id": 5, "method": "nope"}), + json!({"jsonrpc": "2.0", "id": 6, "method": "resources/read", "params": {"uri": "x://y"}}), + ]); + let codes: Vec<&Value> = replies.iter().map(|r| &r["error"]["code"]).collect(); + assert_eq!(codes, [&json!(-32602), &json!(-32602), &json!(-32602), &json!(-32602), &json!(-32601), &json!(-32002)], "{replies:?}"); + assert_eq!(replies[0]["error"]["message"], "echo: missing required argument `text`"); + assert_eq!(replies[2]["error"]["message"], "echo: unknown argument `loud`; its arguments are: text"); + assert!(s.tools().calls.is_empty()); + let defs = s.tools().list(); + assert!(check_arguments(&defs, "echo", &json!({"text": "a"})).is_ok()); +} + +/// Hostile framing: oversized lines, bad UTF-8 and bad JSON are answered and the server goes on. +#[test] +fn hostile_lines_are_answered_and_serving_goes_on() { + let mut input = vec![b'x'; (1 << 16) + 10]; + input.push(b'\n'); + input.extend_from_slice(b"\xff\xfe\n{not json\n[]\n"); + input.extend_from_slice(json!({"jsonrpc": "2.0", "id": 9, "method": "ping"}).to_string().as_bytes()); + let mut output = Vec::new(); + Server::new(info(), Echo::default()).serve(std::io::Cursor::new(input), &mut output).unwrap(); + let replies: Vec = String::from_utf8(output).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect(); + let codes: Vec<&Value> = replies.iter().map(|r| r.get("error").map_or(&r["result"], |e| &e["code"])).collect(); + assert_eq!(codes, [&json!(-32600), &json!(-32700), &json!(-32700), &json!(-32600), &json!({})], "{replies:?}"); +} + +#[test] +fn base64_matches_rfc4648() { + for (i, o) in [("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")] { + assert_eq!(crate::base64(i.as_bytes()), o); + } + let r = crate::image_result(b"foo", "image/png", "info"); + assert_eq!(r, json!({"content": [{"type": "image", "data": "Zm9v", "mimeType": "image/png"}, {"type": "text", "text": "info"}]})); +} + +fn ping(id: usize) -> Value { + json!({"jsonrpc": "2.0", "id": id, "method": "ping"}) +} + +fn notification() -> Value { + json!({"jsonrpc": "2.0", "method": "notifications/progress"}) +} + +fn reply(s: &mut Server, msg: &Value) -> Option { + s.handle_line(&msg.to_string()).map(|r| serde_json::from_str(&r).unwrap()) +} + +/// A batch answers each request in it and none of its notifications; a batch of notifications +/// only gets no reply at all; a batch holds at most 1000 messages. +#[test] +fn batches_answer_their_requests_up_to_the_limit() { + let mut s = Server::new(info(), Echo::default()); + let mixed = + json!([ping(1), notification(), {"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "echo", "arguments": {"text": "b"}}}]); + let replies = reply(&mut s, &mixed).unwrap(); + assert_eq!( + replies, + json!([{"jsonrpc": "2.0", "id": 1, "result": {}}, {"jsonrpc": "2.0", "id": 2, "result": {"content": [{"type": "text", "text": "b"}]}}]) + ); + assert_eq!(reply(&mut s, &json!([notification(), notification()])), None); + let full: Vec = (0..1000).map(ping).collect(); + assert_eq!(reply(&mut s, &Value::Array(full)).unwrap().as_array().map(Vec::len), Some(1000)); + let over: Vec = (0..1001).map(ping).collect(); + let r = reply(&mut s, &Value::Array(over)).unwrap(); + assert_eq!(r, json!({"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "batch too large"}})); +} + +/// We send no requests, so a response is never answered, alone or in a batch; a message with +/// neither a method nor a result, or one that is not an object, is refused; a blank line is skipped. +#[test] +fn a_response_we_never_asked_for_is_ignored() { + let mut s = Server::new(info(), Echo::default()); + assert_eq!(reply(&mut s, &json!({"jsonrpc": "2.0", "id": 1, "result": {}})), None); + assert_eq!(reply(&mut s, &json!({"jsonrpc": "2.0", "id": 2, "error": {"code": -1, "message": "no"}})), None); + assert_eq!( + reply(&mut s, &json!([{"jsonrpc": "2.0", "id": 3, "result": {}}, ping(4)])).unwrap(), + json!([{"jsonrpc": "2.0", "id": 4, "result": {}}]) + ); + let r = reply(&mut s, &json!({"jsonrpc": "2.0", "id": 5})).unwrap(); + assert_eq!(r, json!({"jsonrpc": "2.0", "id": 5, "error": {"code": -32600, "message": "missing `method`"}})); + let r = reply(&mut s, &json!(42)).unwrap(); + assert_eq!(r, json!({"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "message must be an object"}})); + assert_eq!(s.handle_line(" "), None); + assert!(s.tools().log.is_empty()); +} + +/// A version we speak is echoed; one we don't, or none, gets ours. +#[test] +fn an_unknown_protocol_version_gets_ours() { + let mut s = Server::new(info(), Echo::default()); + let version = |s: &mut Server, params: Value| { + reply(s, &json!({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": params})).unwrap()["result"]["protocolVersion"].clone() + }; + for v in SUPPORTED_VERSIONS { + assert_eq!(version(&mut s, json!({"protocolVersion": v})), json!(v)); + } + assert_eq!(version(&mut s, json!({"protocolVersion": "1999-01-01"})), json!(PROTOCOL_VERSION)); + assert_eq!(version(&mut s, json!({"protocolVersion": 7})), json!(PROTOCOL_VERSION)); + assert_eq!(version(&mut s, json!({})), json!(PROTOCOL_VERSION)); + assert!(!s.is_initialized(), "initialized only by the client's notification"); +} + +/// `arguments: null` is no arguments; a value outside a property's `enum` is refused before the +/// tool runs. +#[test] +fn null_arguments_are_none_and_enums_are_checked() { + let mut s = Server::new(info(), Echo::default()); + let call = |s: &mut Server, id: u64, args: Value| { + reply(s, &json!({"jsonrpc": "2.0", "id": id, "method": "tools/call", "params": {"name": "level", "arguments": args}})).unwrap() + }; + assert!(call(&mut s, 1, Value::Null).get("result").is_some()); + let none = reply(&mut s, &json!({"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "level"}})).unwrap(); + assert!(none.get("result").is_some(), "{none}"); + assert!(call(&mut s, 3, json!({"level": "high"})).get("result").is_some()); + let r = call(&mut s, 4, json!({"level": "loud"})); + assert_eq!(r["error"], json!({"code": -32602, "message": "level: `level` must be one of [\"low\",\"high\"]"})); + let r = call(&mut s, 5, json!([])); + assert_eq!(r["error"], json!({"code": -32602, "message": "level: arguments must be an object"})); + let args: Vec<&Value> = s.tools().calls.iter().map(|c| &c["args"]).collect(); + assert_eq!(args, [&json!({}), &json!({}), &json!({"level": "high"})]); +} + +/// An app that serves no resources, templates or prompts lists none of each, says it serves only +/// tools, and has no prompt to fill in or argument to complete. +#[test] +fn the_default_lists_are_empty() { + let mut s = Server::new(info(), Echo::default()); + let result = |s: &mut Server, method: &str| reply(s, &json!({"jsonrpc": "2.0", "id": 1, "method": method})).unwrap()["result"].clone(); + assert_eq!(result(&mut s, "resources/list"), json!({"resources": []})); + assert_eq!(result(&mut s, "resources/templates/list"), json!({"resourceTemplates": []})); + assert_eq!(result(&mut s, "prompts/list"), json!({"prompts": []})); + assert_eq!(result(&mut s, "initialize")["capabilities"], json!({"tools": {"listChanged": false}})); + for method in ["prompts/get", "completion/complete"] { + let r = reply(&mut s, &json!({"jsonrpc": "2.0", "id": 2, "method": method, "params": {"name": "describe"}})).unwrap(); + assert_eq!(r["error"]["code"], -32601, "{method}: {r}"); + } +} + +/// An app that serves more than tools: a prompt with a required and an optional argument, a +/// resource template, a Markdown resource and a PNG, and suggestions for any argument. `asked` +/// records every prompt filled in and every completion asked for. +#[derive(Default)] +struct Library { + asked: Vec, +} + +impl Tools for Library { + fn instructions(&self) -> String { + "Library test.".into() + } + fn list(&mut self) -> Vec { + Vec::new() + } + fn call(&mut self, name: &str, _: &Value) -> Value { + text_result(format!("no tool `{name}`"), true) + } + fn resources(&self) -> Vec { + vec![ + json!({"uri": "lib://readme", "name": "Readme", "mimeType": "text/markdown"}), + json!({"uri": "lib://logo", "name": "Logo", "mimeType": "image/png"}), + ] + } + fn resource_templates(&self) -> Vec { + vec![json!({"uriTemplate": "lib://parts/{name}", "name": "Part", "mimeType": "text/markdown"})] + } + fn read_resource(&mut self, uri: &str) -> Option> { + let markdown = |text: String| Contents::Text { mime_type: "text/markdown".into(), text }; + Some(Ok(match uri { + "lib://readme" => markdown("# Parts\n\nA \"quoted\" line.".into()), + "lib://logo" => Contents::Blob { mime_type: "image/png".into(), data: b"foo".to_vec() }, + "lib://parts/broken" => return Some(Err("the part is being rebuilt".into())), + _ => markdown(format!("# {}", uri.strip_prefix("lib://parts/")?)), + })) + } + fn prompts(&self) -> Vec { + vec![json!({"name": "describe", "title": "Describe a part", "arguments": [{"name": "part", "required": true}, {"name": "tone"}]})] + } + fn get_prompt(&mut self, name: &str, args: &Value) -> Result { + self.asked.push(format!("{name} {args}")); + let text = format!("Describe {} in a {} tone.", args["part"].as_str().unwrap_or_default(), args["tone"].as_str().unwrap_or("plain")); + Ok(json!({"messages": [{"role": "user", "content": {"type": "text", "text": text}}]})) + } + fn complete(&mut self, reference: Reference<'_>, argument: &str, value: &str) -> Vec { + self.asked.push(format!("{reference:?} {argument}={value}")); + (0..150).map(|i| format!("{value}{i}")).collect() + } +} + +/// The shared crate serves an app's prompt, resource template and Markdown and binary resources, +/// and tells the client at `initialize` that it does (#13336). +#[test] +fn an_app_serves_prompts_templates_and_markdown_or_binary_resources() { + let request = |id: u64, method: &str, params: Value| json!({"jsonrpc": "2.0", "id": id, "method": method, "params": params}); + let (mut s, replies) = serve_with( + Library::default(), + &[ + request(1, "initialize", json!({"protocolVersion": PROTOCOL_VERSION})), + request(2, "prompts/list", json!({})), + request(3, "prompts/get", json!({"name": "describe", "arguments": {"part": "bracket"}})), + request(4, "resources/templates/list", json!({})), + request(5, "resources/read", json!({"uri": "lib://readme"})), + request(6, "resources/read", json!({"uri": "lib://logo"})), + request(7, "resources/read", json!({"uri": "lib://parts/bracket"})), + request(8, "resources/read", json!({"uri": "lib://parts/broken"})), + request(9, "resources/read", json!({"uri": "lib://nope"})), + ], + ); + let result = |i: usize| &replies[i]["result"]; + assert_eq!( + result(0)["capabilities"], + json!({"tools": {"listChanged": false}, "resources": {"listChanged": false}, "prompts": {"listChanged": false}, "completions": {}}) + ); + assert_eq!(result(1)["prompts"][0]["name"], "describe"); + assert_eq!(result(2), &json!({"messages": [{"role": "user", "content": {"type": "text", "text": "Describe bracket in a plain tone."}}]})); + assert_eq!(result(3), &json!({"resourceTemplates": [{"uriTemplate": "lib://parts/{name}", "name": "Part", "mimeType": "text/markdown"}]})); + // The Markdown goes out as it is, not as a JSON string of it. + assert_eq!(result(4), &json!({"contents": [{"uri": "lib://readme", "mimeType": "text/markdown", "text": "# Parts\n\nA \"quoted\" line."}]})); + assert_eq!(result(5), &json!({"contents": [{"uri": "lib://logo", "mimeType": "image/png", "blob": "Zm9v"}]})); + assert_eq!(result(6), &json!({"contents": [{"uri": "lib://parts/bracket", "mimeType": "text/markdown", "text": "# bracket"}]})); + assert_eq!(replies[7]["error"], json!({"code": -32603, "message": "the part is being rebuilt"})); + assert_eq!(replies[8]["error"]["code"], -32002); + assert_eq!(s.tools().asked, [r#"describe {"part":"bracket"}"#]); +} + +/// A prompt is filled in only with the arguments its listing declares, each a string, the +/// required ones present; anything else is invalid params and the app is never asked. +#[test] +fn prompt_arguments_are_checked_before_the_prompt_is_filled_in() { + let mut s = Server::new(info(), Library::default()); + let get = + |s: &mut Server, params: Value| reply(s, &json!({"jsonrpc": "2.0", "id": 1, "method": "prompts/get", "params": params})).unwrap(); + for (params, message) in [ + (json!({"arguments": {"part": "a"}}), "missing prompt `name`"), + (json!({"name": "summarise"}), "unknown prompt `summarise`"), + (json!({"name": "describe"}), "describe: missing required argument `part`"), + (json!({"name": "describe", "arguments": ["a"]}), "describe: arguments must be an object"), + (json!({"name": "describe", "arguments": {"part": 3}}), "describe: argument `part` must be a string"), + (json!({"name": "describe", "arguments": {"part": null}}), "describe: argument `part` must be a string"), + (json!({"name": "describe", "arguments": {"part": "a", "size": "big"}}), "describe: unknown argument `size`; its arguments are: part, tone"), + ] { + assert_eq!(get(&mut s, params.clone())["error"], json!({"code": -32602, "message": message}), "{params}"); + } + assert!(s.tools().asked.is_empty()); + let filled = get(&mut s, json!({"name": "describe", "arguments": {"part": "hinge", "tone": "dry"}})); + assert_eq!(filled["result"]["messages"][0]["content"]["text"], "Describe hinge in a dry tone."); +} + +/// Completion asks the app about a listed prompt or a resource, and sends the first 100 +/// suggestions with how many there are; a bad reference or argument is invalid params. +#[test] +fn completion_suggests_up_to_a_hundred_values() { + let mut s = Server::new(info(), Library::default()); + let complete = |s: &mut Server, params: Value| { + reply(s, &json!({"jsonrpc": "2.0", "id": 1, "method": "completion/complete", "params": params})).unwrap() + }; + let r = complete(&mut s, json!({"ref": {"type": "ref/prompt", "name": "describe"}, "argument": {"name": "part", "value": "br"}})); + let completion = &r["result"]["completion"]; + assert_eq!(completion["values"].as_array().map(Vec::len), Some(100), "{r}"); + assert_eq!((&completion["values"][0], &completion["total"], &completion["hasMore"]), (&json!("br0"), &json!(150), &json!(true))); + let r = complete(&mut s, json!({"ref": {"type": "ref/resource", "uri": "lib://parts/{name}"}, "argument": {"name": "name", "value": ""}})); + assert_eq!(r["result"]["completion"]["values"][99], "99"); + assert_eq!(s.tools().asked, [r#"Prompt("describe") part=br"#, r#"Resource("lib://parts/{name}") name="#]); + let bad_ref = "`ref` must be a prompt's {type: ref/prompt, name} or a resource's {type: ref/resource, uri}"; + let bad_argument = "`argument` must have a `name` and a `value`, both strings"; + for (params, message) in [ + (json!({"ref": {"type": "ref/prompt", "name": "summarise"}, "argument": {"name": "a", "value": ""}}), "unknown prompt `summarise`"), + (json!({"ref": {"type": "ref/tool", "name": "x"}, "argument": {"name": "a", "value": ""}}), bad_ref), + (json!({"ref": {"type": "ref/resource"}, "argument": {"name": "a", "value": ""}}), bad_ref), + (json!({"argument": {"name": "a", "value": ""}}), bad_ref), + (json!({"ref": {"type": "ref/prompt", "name": "describe"}, "argument": {"name": "part"}}), bad_argument), + (json!({"ref": {"type": "ref/prompt", "name": "describe"}}), bad_argument), + ] { + assert_eq!(complete(&mut s, params.clone())["error"], json!({"code": -32602, "message": message}), "{params}"); + } + assert_eq!(s.tools().asked.len(), 2, "a refused completion never reaches the app"); +} + +/// `Tools::call` runs right after the listing its arguments were checked against, every time, +/// whatever was listed before it; a refused call is listed and never run. +#[test] +fn every_call_runs_right_after_its_listing() { + let mut s = Server::new(info(), Echo::default()); + let call = |name: &str, args: Value| json!({"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": name, "arguments": args}}); + reply(&mut s, &json!({"jsonrpc": "2.0", "id": 1, "method": "tools/list"})); + reply(&mut s, &call("echo", json!({"text": "a"}))); + reply(&mut s, &call("echo", json!({}))); + reply(&mut s, &json!([call("level", json!({})), call("echo", json!({"text": "b"}))])); + assert_eq!(s.tools().log, ["list", "list", "call echo", "list", "list", "call level", "list", "call echo"]); +} From 070005125f0af1a5ae4a59002270e5849505bfc0 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:38:32 +0100 Subject: [PATCH 23/25] Add craft-sniff: name a file's format, media type and extension from its first bytes (#1841) --- Cargo.lock | 4 + Cargo.toml | 1 + README.md | 1 + crates/sniff/Cargo.toml | 11 ++ crates/sniff/src/detect.rs | 264 +++++++++++++++++++++++++++++++ crates/sniff/src/kind.rs | 270 ++++++++++++++++++++++++++++++++ crates/sniff/src/lib.rs | 32 ++++ crates/sniff/src/tests.rs | 311 +++++++++++++++++++++++++++++++++++++ 8 files changed, 894 insertions(+) create mode 100644 crates/sniff/Cargo.toml create mode 100644 crates/sniff/src/detect.rs create mode 100644 crates/sniff/src/kind.rs create mode 100644 crates/sniff/src/lib.rs create mode 100644 crates/sniff/src/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 13362ace1..ee16611e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2736,6 +2736,10 @@ dependencies = [ "zeroize", ] +[[package]] +name = "craft-sniff" +version = "0.1.0" + [[package]] name = "craft-stencilcraft-document" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index bd08fa725..4850d30c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -174,6 +174,7 @@ craft-xcf = { path = "crates/xcf" } craft-xml = { path = "crates/xml" } craft-zip = { path = "crates/zip" } craft-y4m = { path = "crates/y4m" } +craft-sniff = { path = "crates/sniff" } craft-epub = { path = "crates/epub" } craft-ews = { path = "crates/ews" } craft-video = { path = "crates/video" } diff --git a/README.md b/README.md index 8150dfea9..f4cd1ae9a 100644 --- a/README.md +++ b/README.md @@ -386,6 +386,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit text-format crates read into and write from. WordCraft's model without its editing code. - [`craft-xlsx`](crates/xlsx/README.md): Excel workbooks (`.xlsx`, `.xlsm`, SpreadsheetML, transitional and strict) and binary workbooks (`.xlsb`) on `craft-opc`: the workbook part, shared strings, the styles tables and worksheet rows streamed one at a time, with limits on the package, the XML and the binary records. - [`craft-ora`](crates/ora/README.md): OpenRaster documents (`.ora`): the ZIP archive and the `stack.xml` layer tree as written, read with bounded sizes and written. From PhotoCraft. +- [`craft-sniff`](crates/sniff/src/lib.rs): what a file is from its first bytes: one dependency-free, wasm-safe signature table naming the format, its media type, extension and family for images, audio, video, 3D models, archives, compressed streams, executables and databases. Identifies only; parsing stays with each format's reader. - [`craft-y4m`](crates/y4m/src/lib.rs): YUV4MPEG2 (`.y4m`) reader, from FilmCraft: the stream header, colour space layouts at 8 to 16 bits, a frame index built from the `FRAME` lines without loading the file, and plane access. Bounded on hostile input. diff --git a/crates/sniff/Cargo.toml b/crates/sniff/Cargo.toml new file mode 100644 index 000000000..5fd0955d5 --- /dev/null +++ b/crates/sniff/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "craft-sniff" +description = "What a file is from its first bytes: a dependency-free signature table naming the format, its media type, extension and family, for images, audio, video, 3D models, archives, executables and databases." +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true diff --git a/crates/sniff/src/detect.rs b/crates/sniff/src/detect.rs new file mode 100644 index 000000000..d97baf126 --- /dev/null +++ b/crates/sniff/src/detect.rs @@ -0,0 +1,264 @@ +//! The signature checks behind [`identify`]. + +use crate::Kind; + +/// How many leading bytes [`identify`] reads at most. Every signature it knows lies within them. +pub const HEAD: usize = 4096; + +/// The largest universal Mach-O architecture count taken as Mach-O rather than a Java class. +/// Both start `CAFEBABE`; a class file's next four bytes are its minor and major version, and +/// every released class version (major 45, JDK 1.0, onwards) reads as at least 45 there, while +/// real universal binaries hold a handful of architectures. +const MAX_FAT_ARCHES: u32 = 44; + +/// The ISO BMFF brands read from an `ftyp` box: the major brand and up to this many compatible ones. +const MAX_BRANDS: usize = 32; + +/// The MPEG transport stream packet size and the sync byte each packet starts with. +const TS_PACKET: usize = 188; +const TS_SYNC: u8 = 0x47; + +/// Name the format of a file from its leading bytes, or `None` when no signature matches. +/// +/// Only the first [`HEAD`] bytes are read. Signatures at the start of the file are checked +/// before the weaker ones further in or built from few bits (TAR at byte 257, MP3 and AAC frame +/// headers, MPEG transport stream packets), so a file is never named by a weak signature when a +/// strong one matches. +/// +/// `CAFEBABE` starts both a universal Mach-O and a Java class file. The next big-endian 32-bit +/// value is a Mach-O architecture count or a class file's version: up to 44 is taken as +/// [`Kind::MachOFat`], 45 and over as [`Kind::JavaClass`]. A caller that knows the file name +/// (`.class`) may prefer that. +pub fn identify(bytes: &[u8]) -> Option { + let head = bytes.get(..HEAD).unwrap_or(bytes); + leading(head).or_else(|| container(head)).or_else(|| trailing(head)) +} + +/// Signatures at byte 0 that need nothing more than a fixed prefix. +fn leading(b: &[u8]) -> Option { + const PREFIXES: &[(&[u8], Kind)] = &[ + (b"\x89PNG\r\n\x1a\n", Kind::Png), + (b"\xff\xd8\xff", Kind::Jpeg), + (b"GIF87a", Kind::Gif), + (b"GIF89a", Kind::Gif), + (b"\xff\x0a", Kind::Jxl), + (b"\x00\x00\x00\x0cJXL \r\n\x87\n", Kind::Jxl), + (b"qoif", Kind::Qoi), + (b"\x76\x2f\x31\x01", Kind::OpenExr), + (b"fLaC", Kind::Flac), + (b"MThd\x00\x00\x00\x06", Kind::Midi), + (b"#!AMR", Kind::Amr), + (b"FLV\x01", Kind::Flv), + (b"\x00\x00\x01\xba", Kind::MpegPs), + (b"glTF", Kind::Glb), + (b"Kaydara FBX Binary ", Kind::Fbx), + (b"ply\n", Kind::Ply), + (b"ply\r", Kind::Ply), + (b"PK\x03\x04", Kind::Zip), + (b"PK\x05\x06", Kind::Zip), + (b"7z\xbc\xaf\x27\x1c", Kind::SevenZip), + (b"Rar!\x1a\x07\x00", Kind::Rar), + (b"Rar!\x1a\x07\x01\x00", Kind::Rar), + (b"\x1f\x8b\x08", Kind::Gzip), + (b"\xfd7zXZ\x00", Kind::Xz), + (b"\x28\xb5\x2f\xfd", Kind::Zstd), + (b"070701", Kind::Cpio), + (b"070702", Kind::Cpio), + (b"070707", Kind::Cpio), + (b"\xc7\x71", Kind::Cpio), + (b"\x71\xc7", Kind::Cpio), + (b"\x7fELF", Kind::Elf), + (b"\xfe\xed\xfa\xce", Kind::MachO), + (b"\xfe\xed\xfa\xcf", Kind::MachO), + (b"\xce\xfa\xed\xfe", Kind::MachO), + (b"\xcf\xfa\xed\xfe", Kind::MachO), + (b"\xca\xfe\xba\xbf", Kind::MachOFat), + (b"\0asm", Kind::Wasm), + (b"dex\n", Kind::Dex), + (b"SQLite format 3\0", Kind::Sqlite), + (b"bplist00", Kind::BinaryPlist), + (b"%PDF-", Kind::Pdf), + (b"ID3", Kind::Mp3), + ]; + PREFIXES.iter().find(|(magic, _)| b.starts_with(magic)).map(|&(_, kind)| kind).or_else(|| checked(b)) +} + +/// Signatures at byte 0 whose prefix is short or shared, confirmed by a field after it. +fn checked(b: &[u8]) -> Option { + if b.starts_with(b"MZ") { + return Some(dos(b)); + } + match b.get(..4)? { + [0xca, 0xfe, 0xba, 0xbe] => Some(if be32(b, 4)? <= MAX_FAT_ARCHES { Kind::MachOFat } else { Kind::JavaClass }), + b"II*\0" | b"MM\0*" | b"II+\0" | b"MM\0+" => Some(Kind::Tiff), + b"8BPS" => matches!(be16(b, 4)?, 1 | 2).then_some(Kind::Psd), + [0, 0, 1, 0] => (le16(b, 4)? > 0 && b.get(9).copied()? == 0).then_some(Kind::Ico), + [b'B', b'Z', b'h', level] => (b'1'..=b'9').contains(level).then_some(Kind::Bzip2), + [b'B', b'M', ..] => bmp(b), + _ => None, + } +} + +/// `MZ`: a PE file when `e_lfanew` (at 0x3C) points at `PE\0\0` within the bytes given. +fn dos(b: &[u8]) -> Kind { + let pe = le32(b, 0x3c) + .and_then(|offset| usize::try_from(offset).ok()) + .and_then(|offset| b.get(offset..offset.checked_add(4)?)) + .is_some_and(|sig| sig == b"PE\0\0"); + if pe { Kind::Pe } else { Kind::DosExecutable } +} + +/// `BM` alone is two letters of text, so a BMP also needs zero reserved fields and a known +/// DIB header size. +fn bmp(b: &[u8]) -> Option { + let reserved = b.get(6..10)? == [0, 0, 0, 0]; + let dib = matches!(le32(b, 14)?, 12 | 16 | 40 | 52 | 56 | 64 | 108 | 124); + (reserved && dib).then_some(Kind::Bmp) +} + +/// Container formats named by what the container says it holds. +fn container(b: &[u8]) -> Option { + match (b.get(..4)?, b.get(8..12)) { + (b"RIFF", Some(b"WAVE")) => Some(Kind::Wav), + (b"RIFF", Some(b"WEBP")) => Some(Kind::WebP), + (b"RIFF", Some(b"AVI ")) => Some(Kind::Avi), + (b"FORM", Some(b"AIFF" | b"AIFC")) => Some(Kind::Aiff), + (b"OggS", _) => ogg(b), + ([0x1a, 0x45, 0xdf, 0xa3], _) => Some(ebml(b)), + _ => iso_bmff(b), + } +} + +/// An Ogg stream is named by its first packet: Opus and Theora have their own types. +fn ogg(b: &[u8]) -> Option { + let segments = usize::from(*b.get(26)?); + let packet = b.get(27usize.checked_add(segments)?..).unwrap_or_default(); + Some(if packet.starts_with(b"OpusHead") { + Kind::Opus + } else if packet.starts_with(b"\x80theora") { + Kind::OggVideo + } else { + Kind::Ogg + }) +} + +/// EBML: WebM and Matroska differ by the `DocType` element (ID 0x4282) in the EBML header. +fn ebml(b: &[u8]) -> Kind { + let header = b.get(..64).unwrap_or(b); + let doc_type = header.windows(2).position(|w| w == [0x42, 0x82]).and_then(|at| { + let size = header.get(at.checked_add(2)?).copied()?; + // A one-byte EBML size has its top bit set; the value is the remaining seven bits. + let len = usize::from(size & 0x7f); + let start = at.checked_add(3)?; + if size & 0x80 == 0 { + return None; + } + header.get(start..start.checked_add(len)?) + }); + if doc_type == Some(b"webm") { Kind::WebM } else { Kind::Matroska } +} + +/// ISO base media files: an `ftyp` box whose brands say what it holds, or an old QuickTime +/// movie starting with one of its own top-level atoms. +fn iso_bmff(b: &[u8]) -> Option { + let box_type = b.get(4..8)?; + let size = usize::try_from(be32(b, 0)?).ok()?; + if box_type != b"ftyp" { + let atom = matches!(box_type, b"moov" | b"mdat" | b"wide") && size >= 8; + return atom.then_some(Kind::QuickTime); + } + if size < 16 { + return None; + } + let end = size.min(b.len()).min(16usize.saturating_add(MAX_BRANDS.saturating_mul(4))); + let major: &[u8; 4] = b.get(8..12)?.try_into().ok()?; + let compatible = b.get(16..end).unwrap_or_default().as_chunks::<4>().0; + let brands = core::iter::once(major).chain(compatible.iter()); + // A still image also lists generic brands, so image brands win wherever they are listed: + // AVIF and JPEG XL over HEVC-coded HEIF, over the bare HEIF structure brands, over video. + let mut best: Option<(u8, Kind)> = None; + for kind in brands.filter_map(brand_kind) { + let rank = brand_rank(kind); + if best.is_none_or(|(top, _)| rank > top) { + best = Some((rank, kind)); + } + } + best.map(|(_, kind)| kind) +} + +fn brand_rank(kind: Kind) -> u8 { + match kind { + Kind::Avif | Kind::Jxl => 3, + Kind::Heif => 2, + _ => 0, + } +} + +fn brand_kind(brand: &[u8; 4]) -> Option { + Some(match brand { + b"avif" | b"avis" => Kind::Avif, + b"heic" | b"heix" | b"heim" | b"heis" | b"hevc" | b"hevx" => Kind::Heif, + // The HEIF structure brands. AVIF files list them too, and AVIF outranks them. + b"mif1" | b"msf1" => Kind::Heif, + b"jxl " => Kind::Jxl, + b"qt " => Kind::QuickTime, + b"M4A " | b"M4B " | b"M4P " => Kind::M4a, + b"3gp4" | b"3gp5" | b"3gp6" | b"3gp7" | b"3gg6" | b"3g2a" | b"3g2b" | b"3g2c" => Kind::ThreeGp, + b"isom" | b"iso2" | b"iso3" | b"iso4" | b"iso5" | b"iso6" | b"mp41" | b"mp42" | b"avc1" | b"dash" | b"mmp4" | b"M4V " | b"M4VH" | b"M4VP" + | b"MSNV" | b"f4v " => Kind::Mp4, + _ => return None, + }) +} + +/// Signatures that sit past byte 0 or are made of a few bits, checked last. +fn trailing(b: &[u8]) -> Option { + if let Some(magic) = b.get(257..263) + && (magic == b"ustar\0" || magic == b"ustar ") + { + return Some(Kind::Tar); + } + if transport_stream(b) { + return Some(Kind::MpegTs); + } + frame_sync(b) +} + +/// Three transport stream packets in a row, each starting with the sync byte. +fn transport_stream(b: &[u8]) -> bool { + (0..3).all(|n| b.get(n * TS_PACKET).copied() == Some(TS_SYNC)) +} + +/// An MPEG audio frame header: 11 sync bits, then the layer bits tell MP3 (layer III) from AAC +/// in ADTS (layer 00). Bitrate and sampling indexes must not be the reserved ones. +fn frame_sync(b: &[u8]) -> Option { + let [0xff, second, third, ..] = *b else { + return None; + }; + if second & 0xe0 != 0xe0 { + return None; + } + let version = (second >> 3) & 0b11; + let layer = (second >> 1) & 0b11; + match layer { + // ADTS uses only the MPEG-4 (0) and MPEG-2 (1) id bit, so the second byte is 0xFx. + 0b00 if second & 0xf0 == 0xf0 && (third >> 2) & 0x0f < 13 => Some(Kind::Aac), + 0b01 if version != 0b01 && third >> 4 != 0x0f && (third >> 2) & 0b11 != 0b11 => Some(Kind::Mp3), + _ => None, + } +} + +fn be16(b: &[u8], at: usize) -> Option { + Some(u16::from_be_bytes(b.get(at..at.checked_add(2)?)?.try_into().ok()?)) +} + +fn le16(b: &[u8], at: usize) -> Option { + Some(u16::from_le_bytes(b.get(at..at.checked_add(2)?)?.try_into().ok()?)) +} + +fn be32(b: &[u8], at: usize) -> Option { + Some(u32::from_be_bytes(b.get(at..at.checked_add(4)?)?.try_into().ok()?)) +} + +fn le32(b: &[u8], at: usize) -> Option { + Some(u32::from_le_bytes(b.get(at..at.checked_add(4)?)?.try_into().ok()?)) +} diff --git a/crates/sniff/src/kind.rs b/crates/sniff/src/kind.rs new file mode 100644 index 000000000..da98b15dc --- /dev/null +++ b/crates/sniff/src/kind.rs @@ -0,0 +1,270 @@ +//! The kinds [`identify`](crate::identify) names, and what each one is. + +/// The broad kind of content a file holds. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum Family { + /// Still or layered pictures. + Image, + /// Sound with no pictures. + Audio, + /// Moving pictures, with or without sound. Container formats that may hold only sound + /// (Matroska, MP4, Ogg Theora) are named by what their signature promises. + Video, + /// 3D scenes and meshes. + Model, + /// Several files packed together. + Archive, + /// One compressed stream. + Compressed, + /// Machine code or bytecode a runtime loads. + Executable, + /// Structured stores: databases and binary property lists. + Database, + /// Page-description documents. + Document, +} + +/// A file format named by its signature. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum Kind { + // Images. + Png, + Jpeg, + Gif, + WebP, + Bmp, + Tiff, + /// Windows icon (`.ico`). + Ico, + /// HEIF still images, HEVC-coded (`.heic`) or generic. + Heif, + Avif, + /// JPEG XL, as a bare codestream or in its ISO BMFF container. + Jxl, + /// Photoshop document or large document (`.psd`, `.psb`). + Psd, + Qoi, + OpenExr, + // Audio. + /// MPEG-1/2 Audio Layer III, with or without an ID3v2 tag. + Mp3, + /// AAC in ADTS framing. + Aac, + Flac, + /// RIFF WAVE. + Wav, + /// AIFF or AIFF-C. + Aiff, + /// Ogg holding anything but Opus or Theora (usually Vorbis). + Ogg, + /// Ogg Opus. + Opus, + /// MPEG-4 audio (`.m4a`, `.m4b`). + M4a, + /// Standard MIDI file. + Midi, + /// AMR narrowband or wideband speech. + Amr, + // Video. + /// MPEG-4 Part 14 and the ISO base media brands it shares. + Mp4, + /// QuickTime movie. + QuickTime, + /// 3GPP or 3GPP2 movie. + ThreeGp, + WebM, + Matroska, + /// Ogg holding Theora video. + OggVideo, + /// RIFF AVI. + Avi, + /// Flash video. + Flv, + /// MPEG program stream. + MpegPs, + /// MPEG transport stream (188-byte packets). + MpegTs, + // 3D models. + /// Binary glTF. + Glb, + /// Binary FBX. ASCII FBX has no signature. + Fbx, + /// PLY, ASCII or binary. + Ply, + // Archives and compressed streams. + Zip, + /// POSIX or GNU tar. + Tar, + /// cpio, binary or ASCII ("newc", CRC and "odc"). + Cpio, + SevenZip, + Rar, + Gzip, + Bzip2, + Xz, + Zstd, + // Executables. + Elf, + /// Portable Executable: a DOS header whose `e_lfanew` reaches a `PE\0\0` signature. + Pe, + /// A DOS `MZ` header with no PE signature in the bytes given (a DOS program, or a PE file + /// whose header lies past them). + DosExecutable, + /// A single-architecture Mach-O file. + MachO, + /// A universal ("fat") Mach-O file. + MachOFat, + /// A Java class file. It shares `CAFEBABE` with universal Mach-O; see [`identify`](crate::identify). + JavaClass, + /// WebAssembly binary module. + Wasm, + /// Dalvik executable. + Dex, + // Databases. + Sqlite, + /// Apple binary property list. + BinaryPlist, + // Documents. + Pdf, +} + +impl Kind { + /// Every kind, in the order [`identify`](crate::identify) documents them. + pub const ALL: [Kind; 56] = [ + Kind::Png, + Kind::Jpeg, + Kind::Gif, + Kind::WebP, + Kind::Bmp, + Kind::Tiff, + Kind::Ico, + Kind::Heif, + Kind::Avif, + Kind::Jxl, + Kind::Psd, + Kind::Qoi, + Kind::OpenExr, + Kind::Mp3, + Kind::Aac, + Kind::Flac, + Kind::Wav, + Kind::Aiff, + Kind::Ogg, + Kind::Opus, + Kind::M4a, + Kind::Midi, + Kind::Amr, + Kind::Mp4, + Kind::QuickTime, + Kind::ThreeGp, + Kind::WebM, + Kind::Matroska, + Kind::OggVideo, + Kind::Avi, + Kind::Flv, + Kind::MpegPs, + Kind::MpegTs, + Kind::Glb, + Kind::Fbx, + Kind::Ply, + Kind::Zip, + Kind::Tar, + Kind::Cpio, + Kind::SevenZip, + Kind::Rar, + Kind::Gzip, + Kind::Bzip2, + Kind::Xz, + Kind::Zstd, + Kind::Elf, + Kind::Pe, + Kind::DosExecutable, + Kind::MachO, + Kind::MachOFat, + Kind::JavaClass, + Kind::Wasm, + Kind::Dex, + Kind::Sqlite, + Kind::BinaryPlist, + Kind::Pdf, + ]; + + /// The media type to send or store with the file: the IANA registration where one exists, + /// otherwise the name in common use (`audio/wav`, `video/x-matroska`, `model/fbx`). + pub fn mime_type(self) -> &'static str { + self.info().0 + } + + /// The usual file extension, lower case, without the leading dot. + pub fn extension(self) -> &'static str { + self.info().1 + } + + /// The broad kind of content. + pub fn family(self) -> Family { + self.info().2 + } + + fn info(self) -> (&'static str, &'static str, Family) { + use Family::{Archive, Audio, Compressed, Database, Document, Executable, Image, Model, Video}; + match self { + Kind::Png => ("image/png", "png", Image), + Kind::Jpeg => ("image/jpeg", "jpg", Image), + Kind::Gif => ("image/gif", "gif", Image), + Kind::WebP => ("image/webp", "webp", Image), + Kind::Bmp => ("image/bmp", "bmp", Image), + Kind::Tiff => ("image/tiff", "tif", Image), + Kind::Ico => ("image/vnd.microsoft.icon", "ico", Image), + Kind::Heif => ("image/heif", "heic", Image), + Kind::Avif => ("image/avif", "avif", Image), + Kind::Jxl => ("image/jxl", "jxl", Image), + Kind::Psd => ("image/vnd.adobe.photoshop", "psd", Image), + Kind::Qoi => ("image/qoi", "qoi", Image), + Kind::OpenExr => ("image/x-exr", "exr", Image), + Kind::Mp3 => ("audio/mpeg", "mp3", Audio), + Kind::Aac => ("audio/aac", "aac", Audio), + Kind::Flac => ("audio/flac", "flac", Audio), + Kind::Wav => ("audio/wav", "wav", Audio), + Kind::Aiff => ("audio/aiff", "aiff", Audio), + Kind::Ogg => ("audio/ogg", "ogg", Audio), + Kind::Opus => ("audio/opus", "opus", Audio), + Kind::M4a => ("audio/mp4", "m4a", Audio), + Kind::Midi => ("audio/midi", "mid", Audio), + Kind::Amr => ("audio/amr", "amr", Audio), + Kind::Mp4 => ("video/mp4", "mp4", Video), + Kind::QuickTime => ("video/quicktime", "mov", Video), + Kind::ThreeGp => ("video/3gpp", "3gp", Video), + Kind::WebM => ("video/webm", "webm", Video), + Kind::Matroska => ("video/x-matroska", "mkv", Video), + Kind::OggVideo => ("video/ogg", "ogv", Video), + Kind::Avi => ("video/x-msvideo", "avi", Video), + Kind::Flv => ("video/x-flv", "flv", Video), + Kind::MpegPs => ("video/mpeg", "mpg", Video), + Kind::MpegTs => ("video/mp2t", "ts", Video), + Kind::Glb => ("model/gltf-binary", "glb", Model), + Kind::Fbx => ("model/fbx", "fbx", Model), + Kind::Ply => ("model/ply", "ply", Model), + Kind::Zip => ("application/zip", "zip", Archive), + Kind::Tar => ("application/x-tar", "tar", Archive), + Kind::Cpio => ("application/x-cpio", "cpio", Archive), + Kind::SevenZip => ("application/x-7z-compressed", "7z", Archive), + Kind::Rar => ("application/vnd.rar", "rar", Archive), + Kind::Gzip => ("application/gzip", "gz", Compressed), + Kind::Bzip2 => ("application/x-bzip2", "bz2", Compressed), + Kind::Xz => ("application/x-xz", "xz", Compressed), + Kind::Zstd => ("application/zstd", "zst", Compressed), + Kind::Elf => ("application/x-elf", "elf", Executable), + Kind::Pe => ("application/vnd.microsoft.portable-executable", "exe", Executable), + Kind::DosExecutable => ("application/x-msdownload", "exe", Executable), + Kind::MachO | Kind::MachOFat => ("application/x-mach-binary", "macho", Executable), + Kind::JavaClass => ("application/java-vm", "class", Executable), + Kind::Wasm => ("application/wasm", "wasm", Executable), + Kind::Dex => ("application/vnd.android.dex", "dex", Executable), + Kind::Sqlite => ("application/vnd.sqlite3", "sqlite", Database), + Kind::BinaryPlist => ("application/x-bplist", "plist", Database), + Kind::Pdf => ("application/pdf", "pdf", Document), + } + } +} diff --git a/crates/sniff/src/lib.rs b/crates/sniff/src/lib.rs new file mode 100644 index 000000000..b95e11591 --- /dev/null +++ b/crates/sniff/src/lib.rs @@ -0,0 +1,32 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![deny(clippy::indexing_slicing)] +#![forbid(unsafe_code)] + +//! What a file is, from its first bytes. +//! +//! [`identify`] reads a file's leading bytes and names its [`Kind`]: the format, with its media +//! type ([`Kind::mime_type`]), usual extension ([`Kind::extension`]) and [`Family`]. It covers the +//! formats apps most often have to tell apart before they open anything: images, audio, video, +//! 3D models, archives and compressed streams, executables and databases. +//! +//! Only signatures are read. Nothing is parsed, validated or decoded, so a file that `identify` +//! names can still be damaged, and a file it doesn't name may still be readable by its own +//! reader. Formats with no signature (plain text, CSV, ASCII FBX, binary STL) are never named. +//! Which kinds an app supports, and what it does when the name and the bytes disagree, stay +//! with the app. +//! +//! [`HEAD`] bytes are enough for every signature; passing more changes nothing, passing fewer +//! only loses the signatures that sit further in (TAR's at byte 257, MPEG transport streams' +//! third packet). Any input is fine: nothing here allocates, panics or reads past the slice. +//! +//! Written from the formats' public specifications and registrations (IANA media types, ISO/IEC +//! 14496-12 brands, the EBML and Matroska specifications, RIFF, the ELF, PE and Mach-O headers). + +mod detect; +mod kind; + +pub use detect::{HEAD, identify}; +pub use kind::{Family, Kind}; + +#[cfg(test)] +mod tests; diff --git a/crates/sniff/src/tests.rs b/crates/sniff/src/tests.rs new file mode 100644 index 000000000..2c5d2da8d --- /dev/null +++ b/crates/sniff/src/tests.rs @@ -0,0 +1,311 @@ +use crate::{Family, HEAD, Kind, identify}; + +/// `prefix` followed by zeros up to `len` bytes. +fn padded(prefix: &[u8], len: usize) -> Vec { + let mut bytes = prefix.to_vec(); + bytes.resize(len.max(prefix.len()), 0); + bytes +} + +/// An ISO BMFF `ftyp` box with the given major and compatible brands. +fn ftyp(major: &[u8; 4], compatible: &[&[u8; 4]]) -> Vec { + let size = u32::try_from(16 + compatible.len() * 4).unwrap(); + let mut bytes = size.to_be_bytes().to_vec(); + bytes.extend_from_slice(b"ftyp"); + bytes.extend_from_slice(major); + bytes.extend_from_slice(&[0, 0, 0, 0]); + for brand in compatible { + bytes.extend_from_slice(*brand); + } + bytes +} + +/// A RIFF or FORM container header of the given form type. +fn riff(chunk: &[u8; 4], form: &[u8; 4]) -> Vec { + let mut bytes = chunk.to_vec(); + bytes.extend_from_slice(&[0x24, 0, 0, 0]); + bytes.extend_from_slice(form); + bytes +} + +/// An Ogg page whose first packet starts with `packet`. +fn ogg(packet: &[u8]) -> Vec { + let mut bytes = b"OggS\0\x02".to_vec(); + bytes.resize(26, 0); + bytes.push(1); + bytes.push(u8::try_from(packet.len()).unwrap()); + bytes.extend_from_slice(packet); + bytes +} + +/// An EBML header naming `doc_type`. +fn ebml(doc_type: &[u8]) -> Vec { + let mut bytes = vec![0x1a, 0x45, 0xdf, 0xa3, 0x9f, 0x42, 0x86, 0x81, 0x01, 0x42, 0x82]; + bytes.push(0x80 | u8::try_from(doc_type.len()).unwrap()); + bytes.extend_from_slice(doc_type); + bytes +} + +#[test] +fn names_fixed_prefixes() { + let cases: &[(&[u8], Kind)] = &[ + (b"\x89PNG\r\n\x1a\n\0\0\0\rIHDR", Kind::Png), + (b"\xff\xd8\xff\xe0\0\x10JFIF", Kind::Jpeg), + (b"GIF89a\x01\0\x01\0", Kind::Gif), + (b"GIF87a", Kind::Gif), + (b"\xff\x0a\xfa", Kind::Jxl), + (b"\x00\x00\x00\x0cJXL \r\n\x87\n", Kind::Jxl), + (b"qoif\0\0\0\x01", Kind::Qoi), + (b"\x76\x2f\x31\x01\x02\0\0\0", Kind::OpenExr), + (b"fLaC\0\0\0\x22", Kind::Flac), + (b"MThd\0\0\0\x06\0\x01", Kind::Midi), + (b"#!AMR\n", Kind::Amr), + (b"#!AMR-WB\n", Kind::Amr), + (b"FLV\x01\x05", Kind::Flv), + (b"\x00\x00\x01\xba\x44", Kind::MpegPs), + (b"glTF\x02\0\0\0", Kind::Glb), + (b"Kaydara FBX Binary \0\x1a\0", Kind::Fbx), + (b"ply\nformat ascii 1.0\n", Kind::Ply), + (b"ply\r\nformat binary_little_endian 1.0\r\n", Kind::Ply), + (b"PK\x03\x04\x14\0", Kind::Zip), + (b"PK\x05\x06\0\0\0\0", Kind::Zip), + (b"7z\xbc\xaf\x27\x1c\0\x04", Kind::SevenZip), + (b"Rar!\x1a\x07\x00", Kind::Rar), + (b"Rar!\x1a\x07\x01\x00", Kind::Rar), + (b"\x1f\x8b\x08\0", Kind::Gzip), + (b"\xfd7zXZ\x00\x00", Kind::Xz), + (b"\x28\xb5\x2f\xfd\x24", Kind::Zstd), + (b"070701000000", Kind::Cpio), + (b"070707000000", Kind::Cpio), + (b"\xc7\x71\0\0", Kind::Cpio), + (b"\x7fELF\x02\x01\x01", Kind::Elf), + (b"\xcf\xfa\xed\xfe\x07\0\0\x01", Kind::MachO), + (b"\xfe\xed\xfa\xce", Kind::MachO), + (b"\xca\xfe\xba\xbf\0\0\0\x02", Kind::MachOFat), + (b"\0asm\x01\0\0\0", Kind::Wasm), + (b"dex\n035\0", Kind::Dex), + (b"SQLite format 3\0\x10\0", Kind::Sqlite), + (b"bplist00\xd1", Kind::BinaryPlist), + (b"%PDF-1.7\n", Kind::Pdf), + (b"ID3\x04\0\0\0\0\0\0", Kind::Mp3), + ]; + for &(bytes, kind) in cases { + assert_eq!(identify(bytes), Some(kind), "{bytes:02x?}"); + } +} + +#[test] +fn java_class_and_universal_mach_o_share_cafebabe() { + assert_eq!(identify(b"\xca\xfe\xba\xbe\0\0\0\x02"), Some(Kind::MachOFat)); + assert_eq!(identify(b"\xca\xfe\xba\xbe\0\0\0\x2c"), Some(Kind::MachOFat)); + // JDK 1.1 (45.3) and Java 21 (65.0). + assert_eq!(identify(b"\xca\xfe\xba\xbe\0\x03\0\x2d"), Some(Kind::JavaClass)); + assert_eq!(identify(b"\xca\xfe\xba\xbe\0\0\0\x41"), Some(Kind::JavaClass)); + assert_eq!(identify(b"\xca\xfe\xba\xbe"), None); +} + +#[test] +fn checks_fields_after_short_prefixes() { + assert_eq!(identify(b"II*\0\x08\0\0\0"), Some(Kind::Tiff)); + assert_eq!(identify(b"MM\0*\0\0\0\x08"), Some(Kind::Tiff)); + assert_eq!(identify(b"II+\0\x08\0\0\0"), Some(Kind::Tiff)); + assert_eq!(identify(b"8BPS\0\x01\0\0"), Some(Kind::Psd)); + assert_eq!(identify(b"8BPS\0\x02\0\0"), Some(Kind::Psd)); + assert_eq!(identify(b"8BPS\0\x03\0\0"), None); + assert_eq!(identify(b"\0\0\x01\0\x01\0\x10\x10\0\0"), Some(Kind::Ico)); + assert_eq!(identify(b"\0\0\x01\0\0\0\x10\x10\0\0"), None); + assert_eq!(identify(b"BZh91AY&SY"), Some(Kind::Bzip2)); + assert_eq!(identify(b"BZh0"), None); +} + +#[test] +fn tells_pe_from_dos_programs() { + let mut pe = padded(b"MZ\x90\0", 0x84); + pe[0x3c] = 0x80; + pe[0x80..0x84].copy_from_slice(b"PE\0\0"); + assert_eq!(identify(&pe), Some(Kind::Pe)); + // The PE header lies past the bytes given, or isn't there at all. + pe[0x3c] = 0xf0; + assert_eq!(identify(&pe), Some(Kind::DosExecutable)); + let mut far = padded(b"MZ", 0x40); + far[0x3c..0x40].copy_from_slice(&u32::MAX.to_le_bytes()); + assert_eq!(identify(&far), Some(Kind::DosExecutable)); + assert_eq!(identify(b"MZ"), Some(Kind::DosExecutable)); +} + +#[test] +fn bmp_needs_reserved_zeros_and_a_known_dib_header() { + let mut bmp = padded(b"BM\x36\0\0\0", 54); + bmp[10] = 54; + bmp[14] = 40; + assert_eq!(identify(&bmp), Some(Kind::Bmp)); + bmp[14] = 41; + assert_eq!(identify(&bmp), None); + assert_eq!(identify(b"BMW is a car brand, not a bitmap."), None); +} + +#[test] +fn names_riff_and_form_containers_by_form_type() { + assert_eq!(identify(&riff(b"RIFF", b"WAVE")), Some(Kind::Wav)); + assert_eq!(identify(&riff(b"RIFF", b"WEBP")), Some(Kind::WebP)); + assert_eq!(identify(&riff(b"RIFF", b"AVI ")), Some(Kind::Avi)); + assert_eq!(identify(&riff(b"FORM", b"AIFF")), Some(Kind::Aiff)); + assert_eq!(identify(&riff(b"FORM", b"AIFC")), Some(Kind::Aiff)); + assert_eq!(identify(&riff(b"RIFF", b"CDXA")), None); +} + +#[test] +fn names_ogg_streams_by_first_packet() { + assert_eq!(identify(&ogg(b"OpusHead\x01\x02")), Some(Kind::Opus)); + assert_eq!(identify(&ogg(b"\x80theora\x03\x02")), Some(Kind::OggVideo)); + assert_eq!(identify(&ogg(b"\x01vorbis\0\0")), Some(Kind::Ogg)); + // A page cut off before its segment table is still Ogg. + assert_eq!(identify(&ogg(b"")[..27]), Some(Kind::Ogg)); + assert_eq!(identify(b"OggS\0\x02"), None); +} + +#[test] +fn names_ebml_by_doc_type() { + assert_eq!(identify(&ebml(b"webm")), Some(Kind::WebM)); + assert_eq!(identify(&ebml(b"matroska")), Some(Kind::Matroska)); + // No readable DocType: still Matroska's container. + assert_eq!(identify(&[0x1a, 0x45, 0xdf, 0xa3, 0x42, 0x82, 0x04]), Some(Kind::Matroska)); + assert_eq!(identify(&[0x1a, 0x45, 0xdf, 0xa3, 0x42, 0x82, 0x84, b'w']), Some(Kind::Matroska)); +} + +#[test] +fn names_iso_bmff_by_brand() { + assert_eq!(identify(&ftyp(b"isom", &[b"isom", b"iso2", b"avc1", b"mp41"])), Some(Kind::Mp4)); + assert_eq!(identify(&ftyp(b"mp42", &[])), Some(Kind::Mp4)); + assert_eq!(identify(&ftyp(b"qt ", &[b"qt "])), Some(Kind::QuickTime)); + assert_eq!(identify(&ftyp(b"M4A ", &[b"isom"])), Some(Kind::M4a)); + assert_eq!(identify(&ftyp(b"3gp5", &[b"isom"])), Some(Kind::ThreeGp)); + assert_eq!(identify(&ftyp(b"heic", &[b"mif1"])), Some(Kind::Heif)); + assert_eq!(identify(&ftyp(b"jxl ", &[])), Some(Kind::Jxl)); + // A still image lists generic brands too; the image brand wins wherever it is. + assert_eq!(identify(&ftyp(b"mif1", &[b"avif", b"miaf"])), Some(Kind::Avif)); + assert_eq!(identify(&ftyp(b"isom", &[b"heic"])), Some(Kind::Heif)); + // Unknown brands (a camera's raw container, say) are not guessed at. + assert_eq!(identify(&ftyp(b"crx ", &[b"isom"])), Some(Kind::Mp4)); + assert_eq!(identify(&ftyp(b"crx ", &[])), None); + let mut short = ftyp(b"isom", &[]); + short[3] = 12; + assert_eq!(identify(&short), None); +} + +#[test] +fn names_old_quicktime_atoms() { + assert_eq!(identify(b"\0\0\0\x08wide\0\0\0\0mdat"), Some(Kind::QuickTime)); + assert_eq!(identify(b"\0\0\x10\0moov"), Some(Kind::QuickTime)); + assert_eq!(identify(b"\0\0\0\x04moov"), None); + assert_eq!(identify(b"\0\0\0\x10free"), None); +} + +#[test] +fn names_tar_by_its_ustar_magic() { + let mut tar = padded(b"hello.txt", 512); + tar[257..263].copy_from_slice(b"ustar\0"); + assert_eq!(identify(&tar), Some(Kind::Tar)); + tar[257..263].copy_from_slice(b"ustar "); + assert_eq!(identify(&tar), Some(Kind::Tar)); + tar[257..263].copy_from_slice(b"ustaR "); + assert_eq!(identify(&tar), None); +} + +#[test] +fn transport_streams_need_three_packets() { + let mut ts = vec![0u8; 3 * 188]; + for n in 0..3 { + ts[n * 188] = 0x47; + } + assert_eq!(identify(&ts), Some(Kind::MpegTs)); + assert_eq!(identify(&ts[..377]), Some(Kind::MpegTs)); + assert_eq!(identify(&ts[..376]), None); +} + +#[test] +fn reads_mpeg_audio_frame_headers() { + // MPEG-1, MPEG-2 and MPEG-2.5 layer III. + assert_eq!(identify(b"\xff\xfb\x90\x64"), Some(Kind::Mp3)); + assert_eq!(identify(b"\xff\xf3\x48\xc4"), Some(Kind::Mp3)); + assert_eq!(identify(b"\xff\xe3\x18\xc4"), Some(Kind::Mp3)); + // Reserved version, bad bitrate, reserved sampling rate, layers I and II. + assert_eq!(identify(b"\xff\xeb\x90\x64"), None); + assert_eq!(identify(b"\xff\xfb\xf0\x64"), None); + assert_eq!(identify(b"\xff\xfb\x9c\x64"), None); + assert_eq!(identify(b"\xff\xff\x90\x64"), None); + assert_eq!(identify(b"\xff\xfd\x90\x64"), None); + // ADTS AAC, MPEG-4 and MPEG-2 ids; a reserved sampling index is not AAC. + assert_eq!(identify(b"\xff\xf1\x50\x80"), Some(Kind::Aac)); + assert_eq!(identify(b"\xff\xf9\x4c\x80"), Some(Kind::Aac)); + assert_eq!(identify(b"\xff\xf1\x7c\x80"), None); + assert_eq!(identify(b"\xff\x81\x50"), None); + assert_eq!(identify(b"\xff\xf1"), None); +} + +#[test] +fn names_nothing_without_a_signature() { + for bytes in [&b""[..], b"\0", b"hello, world\n", b"; FBX 7.3.0 project file", b"plywood", b"name,value\n1,2\n", b""] { + assert_eq!(identify(bytes), None, "{bytes:?}"); + } +} + +#[test] +fn reads_only_the_head() { + let mut late = vec![0u8; HEAD + 600]; + late[HEAD + 257..HEAD + 263].copy_from_slice(b"ustar\0"); + assert_eq!(identify(&late), None); + let mut png = b"\x89PNG\r\n\x1a\n".to_vec(); + png.resize(HEAD * 4, 0xff); + assert_eq!(identify(&png), Some(Kind::Png)); +} + +#[test] +fn every_prefix_of_every_sample_is_safe() { + let mut samples = vec![ftyp(b"mif1", &[b"avif"]), ebml(b"webm"), ogg(b"OpusHead"), riff(b"RIFF", b"WAVE")]; + samples.push(padded(b"MZ", 0x100)); + samples.push((0..=255).collect()); + for sample in &samples { + for end in 0..=sample.len() { + let _ = identify(&sample[..end]); + } + } +} + +#[test] +fn every_kind_has_a_type_extension_and_family() { + let mut seen = std::collections::HashSet::new(); + for kind in Kind::ALL { + assert!(seen.insert(kind), "{kind:?} listed twice"); + let mime = kind.mime_type(); + let (top, sub) = mime.split_once('/').unwrap(); + assert!(!top.is_empty() && !sub.is_empty(), "{kind:?}: {mime}"); + assert!(!kind.extension().is_empty() && !kind.extension().starts_with('.')); + assert_eq!(kind.extension(), kind.extension().to_ascii_lowercase()); + let expected = match top { + "image" => Family::Image, + "audio" => Family::Audio, + "video" => Family::Video, + "model" => Family::Model, + _ => kind.family(), + }; + assert_eq!(kind.family(), expected, "{kind:?}"); + } +} + +#[test] +fn media_types_apps_send() { + assert_eq!(Kind::Png.mime_type(), "image/png"); + assert_eq!(Kind::Jpeg.extension(), "jpg"); + assert_eq!(Kind::Wav.mime_type(), "audio/wav"); + assert_eq!(Kind::Flac.mime_type(), "audio/flac"); + assert_eq!(Kind::Glb.mime_type(), "model/gltf-binary"); + assert_eq!(Kind::Fbx.mime_type(), "model/fbx"); + assert_eq!(Kind::Ply.mime_type(), "model/ply"); + assert_eq!(Kind::Mp4.family(), Family::Video); + assert_eq!(Kind::Zip.family(), Family::Archive); + assert_eq!(Kind::Gzip.family(), Family::Compressed); + assert_eq!(Kind::Elf.family(), Family::Executable); + assert_eq!(Kind::Sqlite.family(), Family::Database); + assert_eq!(Kind::Pdf.family(), Family::Document); +} From e409e74333287e7b4216066011bb879b9d581348 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 06:49:02 -0500 Subject: [PATCH 24/25] fix(print): opt-in bounded discovery for larger CUPS fleets (#1734) * fix(print): allow explicitly bounded fleet discovery * print: frame fleet discovery fixture as an HTTP reply --- crates/print/src/cups/mod.rs | 38 +++++++++++++++++++++++++++++++--- crates/print/src/cups/tests.rs | 34 ++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/crates/print/src/cups/mod.rs b/crates/print/src/cups/mod.rs index 1a0a9360b..d7bcd478e 100644 --- a/crates/print/src/cups/mod.rs +++ b/crates/print/src/cups/mod.rs @@ -33,6 +33,8 @@ const CUPS_SOCKET: &str = "/run/cups/cups.sock"; const MAX_BODY: usize = 1 << 20; const MAX_HEADERS: usize = 8 << 10; const MAX_PRINTERS: usize = 256; +// Even opted-in fleet discovery has a fixed record ceiling in addition to MAX_BODY. +const MAX_DISCOVERY_QUEUES: usize = 65_536; const MAX_NAME: usize = 256; /// A job's title on the wire; IPP values are u16-length, and no real title is this long. const MAX_JOB_NAME: usize = 255; @@ -338,9 +340,24 @@ pub fn destinations_at(endpoints: &[Server]) -> Result, Error> { /// explicitly choose whether an optional class/default query failure should hide otherwise /// usable printer queues. No error is swallowed here except CUPS's absent/unsupported list. pub fn queues_at(endpoints: &[Server], is_class: bool) -> Result, Error> { + queues_at_with_limit(endpoints, is_class, MAX_PRINTERS) +} + +/// List queues or classes with an explicit fleet-size budget (1 through 65,536). +/// +/// The existing [`queues_at`] API keeps its 256-queue default. Increasing this budget does +/// not increase the one-MiB response, HTTP header, name-length or exchange-time limits. +/// An excessive response is refused in full, never silently truncated. This changes only +/// local parsing: it does not invent pagination or retry a possibly submitted print job. +/// +/// # Errors +/// Returns an error for an invalid budget, an over-budget or malformed response, or a +/// transport/server error, with the same absent-list handling as [`queues_at`]. +pub fn queues_at_with_limit(endpoints: &[Server], is_class: bool, max_queues: usize) -> Result, Error> { + check_queue_limit(max_queues)?; let operation = if is_class { 0x4005 } else { OPERATION_CUPS_GET_PRINTERS }; let reply = post(endpoints, "/", &discovery_request(operation), DISCOVERY_TIMEOUT, true)?; - let parsed = parse_reply(&reply)?; + let parsed = parse_reply_with_limit(&reply, max_queues)?; if !status_ok(parsed.status) { if parsed.status == 0x0406 || (is_class && parsed.status == 0x0501) { return Ok(Vec::new()); @@ -834,7 +851,22 @@ impl<'a> Reader<'a> { } } +fn check_queue_limit(max_queues: usize) -> Result<(), Error> { + if !(1..=MAX_DISCOVERY_QUEUES).contains(&max_queues) { + return Err(Error::Spool("CUPS discovery queue limit must be 1 through 65536".into())); + } + Ok(()) +} + fn parse_reply(reply: &[u8]) -> Result { + parse_reply_with_limit(reply, MAX_PRINTERS) +} + +fn parse_reply_with_limit(reply: &[u8], max_queues: usize) -> Result { + check_queue_limit(max_queues)?; + if reply.len() > MAX_BODY { + return Err(Error::Spool("CUPS reply exceeds limit".into())); + } let malformed = || Error::Spool("the print server sent a malformed reply".into()); let mut r = Reader { bytes: reply, pos: 0 }; let _version = r.array::<2>().ok_or_else(malformed)?; @@ -854,7 +886,7 @@ fn parse_reply(reply: &[u8]) -> Result { } GROUP_OPERATION | GROUP_JOB | GROUP_PRINTER | 0x05 => { if !queue.name.is_empty() { - if queues.len() >= MAX_PRINTERS { + if queues.len() >= max_queues { return Err(malformed()); } queues.push(std::mem::take(&mut queue)); @@ -879,7 +911,7 @@ fn parse_reply(reply: &[u8]) -> Result { match (group, name, tag) { (GROUP_PRINTER, "printer-name", TAG_NAME) => { if let Some(printer) = std::str::from_utf8(value).ok().filter(|n| !n.is_empty() && n.len() <= MAX_NAME) { - if names.len() >= MAX_PRINTERS { + if names.len() >= max_queues { return Err(Error::Spool("too many CUPS destinations".into())); } if !queue.name.is_empty() { diff --git a/crates/print/src/cups/tests.rs b/crates/print/src/cups/tests.rs index 978246e56..252e1bba8 100644 --- a/crates/print/src/cups/tests.rs +++ b/crates/print/src/cups/tests.rs @@ -591,3 +591,37 @@ fn stalled_unix_peer_still_fails_within_the_absolute_read_deadline() { assert!(matches!(error.kind(), std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock)); assert!(start.elapsed() < Duration::from_secs(3)); } + +#[test] +fn fleet_discovery_opt_in_preserves_all_257_queues_and_default_limit() { + let mut body = vec![1, 1, 0, 0, 0, 0, 0, 1]; + for index in 0..257 { + body.push(4); + body.extend(printer_name(&format!("Printer{index}"))); + } + body.push(3); + assert!(parse_reply(&body).is_err()); + assert_eq!(parse_reply_with_limit(&body, 257).unwrap().queues.len(), 257); + assert!(parse_reply_with_limit(&body, 256).is_err()); + for is_class in [false, true] { + let server = FakeCups::on_tcp(vec![http_reply(&body)]).unwrap(); + let queues = queues_at_with_limit(server.endpoint(), is_class, 257).unwrap(); + assert_eq!(queues.len(), 257); + assert_eq!(queues[256].name, "Printer256"); + assert!(queues.iter().all(|q| q.is_class == is_class)); + let request = server.request().unwrap(); + assert!(has(&request, b"requested-attributes")); + } +} + +#[test] +fn fleet_discovery_keeps_absolute_resource_limits() { + for limit in [0, MAX_DISCOVERY_QUEUES + 1, usize::MAX] { + assert!(queues_at_with_limit(&[], false, limit).is_err()); + } + assert!(parse_reply_with_limit(&vec![0; MAX_BODY + 1], MAX_DISCOVERY_QUEUES).is_err()); + let mut body = vec![1, 1, 0, 0, 0, 0, 0, 1, 4]; + body.extend(attr(0x42, &"x".repeat(MAX_NAME + 1), b"value")); + body.push(3); + assert!(parse_reply_with_limit(&body, MAX_DISCOVERY_QUEUES).is_err()); +} From b836a718563925ab27835cb83cdceb001abb8371 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:54:06 +0100 Subject: [PATCH 25/25] craft-svg: a paths feature, and CSS colours with their alpha (#1848) The geometry reader (craft_svg::paths) is the only part of the crate that needs vectorcraft-geom and vectorcraft-pathops, and the tree is the only part that needs craft-image-codecs. Both are now optional: paths is a default feature and tree turns it on, so existing users build the same crate, while an app that only needs SVG text, guards, path data, colours and the writer can turn default features off and take no app crates or image codecs. parse_color_alpha is parse_color keeping the alpha of #rgba, #rrggbbaa and the fourth value of rgb()/hsl(). parse_color now calls it and still drops it. Refs #778, #789 --- crates/svg/Cargo.toml | 15 ++++++--- crates/svg/src/color.rs | 74 ++++++++++++++++++++++++++++------------- crates/svg/src/lib.rs | 5 +-- crates/svg/src/write.rs | 1 + 4 files changed, 65 insertions(+), 30 deletions(-) diff --git a/crates/svg/Cargo.toml b/crates/svg/Cargo.toml index 3b778da47..b35c4de41 100644 --- a/crates/svg/Cargo.toml +++ b/crates/svg/Cargo.toml @@ -13,14 +13,16 @@ workspace = true [dependencies] craft-xml = { workspace = true } quick-xml = { version = "0.41", default-features = false } -craft-image-codecs = { workspace = true } +# Probing and bounding raster images embedded in a parsed tree (`tree`). +craft-image-codecs = { workspace = true, optional = true } # SVGZ (gzip), pure-Rust backend. flate2 = { version = "1", default-features = false, features = ["rust_backend"] } kurbo = { workspace = true } roxmltree = "0.21.2" thiserror = { workspace = true } -vectorcraft-geom = { workspace = true } -vectorcraft-pathops = { workspace = true } +# The geometry reader (`paths`) builds and cleans its outlines with these. +vectorcraft-geom = { workspace = true, optional = true } +vectorcraft-pathops = { workspace = true, optional = true } # The full SVG parser (Apache-2.0 OR MIT). The one version every Craft app uses: apps depend on # craft-svg's `tree` feature and take usvg from its re-export, never directly. usvg = { version = "=0.48.1", default-features = false, optional = true } @@ -31,8 +33,13 @@ resvg = { version = "=0.48.1", default-features = false, features = ["text", "ra data-url = { version = "0.3.2", optional = true } [features] +default = ["paths"] +# `craft_svg::paths`: the geometry reader (Bézier paths in millimetres) for cutters, plotters and +# CAD import. Off, the crate is SVG text, guards, path data, colours and the writer only, with no +# app crates (vectorcraft) among its dependencies. +paths = ["dep:vectorcraft-geom", "dep:vectorcraft-pathops"] # `craft_svg::tree`: usvg behind the bounded entry point. -tree = ["dep:usvg", "dep:data-url"] +tree = ["paths", "dep:usvg", "dep:data-url", "dep:craft-image-codecs"] # Text converted to paths (needs a font database in the options). tree-text = ["tree", "usvg/text"] # The system's fonts in `usvg::fontdb`. diff --git a/crates/svg/src/color.rs b/crates/svg/src/color.rs index 14588bc90..610ca6aa1 100644 --- a/crates/svg/src/color.rs +++ b/crates/svg/src/color.rs @@ -65,12 +65,20 @@ pub struct ColorParseError { /// and SVG 2 paint both defer to): the 148 named colours, `#rgb`, `#rgba`, `#rrggbb`, /// `#rrggbbaa`, `rgb()`/`rgba()` with integer 0–255 or percentage channels, and /// `hsl()`/`hsla()`, in comma or space syntax with an optional alpha (`/ a` or a fourth -/// comma value). Alpha is checked and then dropped. +/// comma value). Alpha is checked and then dropped; [`parse_color_alpha`] keeps it. /// /// Anything else — `transparent`, `currentColor`, `color()`, out-of-range channels — /// is an error, not a guess: silent color loss is worse than a reported failure. /// Callers that understand `none`/`currentColor` (SVG paint) resolve them first. pub fn parse_color(input: &str) -> Result { + parse_color_alpha(input).map(|(color, _)| color) +} + +/// [`parse_color`], with the colour's alpha in `[0, 1]`: from `#rgba` and `#rrggbbaa` (in +/// 255ths), from the fourth value of `rgb()`/`hsl()` (a number or a percentage), else 1. A +/// colour with an alpha of its own (a fill of `rgba(…)`) multiplies with `fill-opacity`; that +/// is the caller's to do. +pub fn parse_color_alpha(input: &str) -> Result<(Color, f32), ColorParseError> { let err = || ColorParseError { input: input.to_string() }; // Cap input length before work: a caller feeding untrusted text cannot // make this quadratic. @@ -84,30 +92,27 @@ pub fn parse_color(input: &str) -> Result { if let Some((name, rest)) = lower.split_once('(') { return parse_function(name.trim(), rest).ok_or_else(err); } - NAMED.iter().find(|(name, _)| *name == lower).map(|(_, rgb)| Color::new((rgb >> 16) as u8, (rgb >> 8) as u8, *rgb as u8)).ok_or_else(err) + NAMED.iter().find(|(name, _)| *name == lower).map(|(_, rgb)| (Color::new((rgb >> 16) as u8, (rgb >> 8) as u8, *rgb as u8), 1.0)).ok_or_else(err) } -/// Hex digits after `#`: 3 or 4 (each doubled), 6 or 8; the alpha digits are dropped. -fn parse_hex(digits: &str) -> Option { +/// Hex digits after `#`: 3 or 4 (each doubled), 6 or 8; the fourth (or fourth pair) is alpha. +fn parse_hex(digits: &str) -> Option<(Color, f32)> { if !digits.bytes().all(|b| b.is_ascii_hexdigit()) { return None; } let value = |s: &str| u8::from_str_radix(s, 16).ok(); - match digits.len() { - 3 | 4 => { - let d = |i: usize| value(digits.get(i..=i)?).map(|v| v * 17); - Some(Color::new(d(0)?, d(1)?, d(2)?)) - } - 6 | 8 => { - let d = |i: usize| value(digits.get(i..i + 2)?); - Some(Color::new(d(0)?, d(2)?, d(4)?)) - } - _ => None, - } + let channels: Vec = match digits.len() { + 3 | 4 => (0..digits.len()).map(|i| value(digits.get(i..=i)?).map(|v| v * 17)).collect::>()?, + 6 | 8 => (0..digits.len() / 2).map(|i| value(digits.get(2 * i..2 * i + 2)?)).collect::>()?, + _ => return None, + }; + let (&r, &g, &b) = (channels.first()?, channels.get(1)?, channels.get(2)?); + let alpha = channels.get(3).map_or(1.0, |a| f32::from(*a) / 255.0); + Some((Color::new(r, g, b), alpha)) } /// `rgb(…)`, `rgba(…)`, `hsl(…)`, `hsla(…)` after the `(`, which must close with `)`. -fn parse_function(name: &str, rest: &str) -> Option { +fn parse_function(name: &str, rest: &str) -> Option<(Color, f32)> { let body = rest.trim().strip_suffix(')')?; // Legacy comma syntax, or modern space syntax with `/ alpha`. let parts: Vec<&str> = if body.contains(',') { @@ -127,26 +132,29 @@ fn parse_function(name: &str, rest: &str) -> Option { if !(parts.len() == 3 || parts.len() == 4) { return None; } - if let Some(alpha) = parts.get(3) { - let a = match alpha.strip_suffix('%') { + let mut alpha = 1.0; + if let Some(text) = parts.get(3) { + let a = match text.strip_suffix('%') { Some(p) => number(p)? / 100.0, - None => number(alpha)?, + None => number(text)?, }; if !(0.0..=1.0).contains(&a) { return None; } + alpha = a as f32; } let (Some(a), Some(b), Some(c)) = (parts.first(), parts.get(1), parts.get(2)) else { return None; }; - match name { - "rgb" | "rgba" => Some(Color::new(rgb_channel(a)?, rgb_channel(b)?, rgb_channel(c)?)), + let color = match name { + "rgb" | "rgba" => Color::new(rgb_channel(a)?, rgb_channel(b)?, rgb_channel(c)?), "hsl" | "hsla" => { let hue = number(a.strip_suffix("deg").unwrap_or(a))?; - Some(hsl_to_rgb(hue.rem_euclid(360.0), percent(b)?, percent(c)?)) + hsl_to_rgb(hue.rem_euclid(360.0), percent(b)?, percent(c)?) } - _ => None, - } + _ => return None, + }; + Some((color, alpha)) } /// A finite plain decimal number. @@ -372,6 +380,24 @@ mod tests { assert_eq!(c("#abc"), Color::new(0xaa, 0xbb, 0xcc)); } + #[test] + fn alpha_is_kept_by_parse_color_alpha() { + let a = |input: &str| parse_color_alpha(input).ok().expect("test input should parse"); + assert_eq!(a("#ff000080"), (Color::new(255, 0, 0), 128.0 / 255.0)); + assert_eq!(a("#f008"), (Color::new(255, 0, 0), 136.0 / 255.0)); + assert_eq!(a("#f00"), (Color::new(255, 0, 0), 1.0)); + assert_eq!(a("rgba(255, 0, 0, 0.25)"), (Color::new(255, 0, 0), 0.25)); + assert_eq!(a("rgb(0 0 255 / 50%)"), (Color::new(0, 0, 255), 0.5)); + assert_eq!(a("hsla(120, 100%, 50%, 0)"), (Color::new(0, 255, 0), 0.0)); + assert_eq!(a("SteelBlue"), (Color::new(0x46, 0x82, 0xb4), 1.0)); + // parse_color keeps dropping it, and both refuse the same input. + assert_eq!(c("#ff000080"), Color::new(255, 0, 0)); + for bad in ["rgba(255, 0, 0, 1.5)", "#ff00000", "rgb(0 0 0 / -1)", "transparent", "#ggg"] { + assert!(parse_color_alpha(bad).is_err(), "{bad}"); + assert!(parse_color(bad).is_err(), "{bad}"); + } + } + #[test] fn parses_css_keywords() { assert_eq!(c("black"), Color::new(0, 0, 0)); diff --git a/crates/svg/src/lib.rs b/crates/svg/src/lib.rs index 25bf880cd..7cd36692e 100644 --- a/crates/svg/src/lib.rs +++ b/crates/svg/src/lib.rs @@ -5,8 +5,8 @@ //! UTF-16 and ISO 8859-1 decoded; and SVGZ compression. //! - [`guard`]: checks on the text before any XML reader or SVG converter sees it: how deep the //! elements nest, and what the document type's entities expand to. -//! - [`paths`]: the geometry of a document as Bézier paths in millimetres, for cutters, plotters -//! and CAD import. +//! - `paths` (feature `paths`, on by default): the geometry of a document as Bézier paths in +//! millimetres, for cutters, plotters and CAD import. //! - [`color`]: CSS colours. //! - [`write`]: a writer for SVG documents, their path data and numbers. //! - `tree` (feature `tree`): the full parser. usvg at the one version the Craft apps share, @@ -22,6 +22,7 @@ pub mod drawing_write; pub mod guard; pub mod neutral_document; pub mod path_data; +#[cfg(feature = "paths")] pub mod paths; pub mod preflight; pub mod sniff; diff --git a/crates/svg/src/write.rs b/crates/svg/src/write.rs index 66cb1d6a0..4051c081f 100644 --- a/crates/svg/src/write.rs +++ b/crates/svg/src/write.rs @@ -362,6 +362,7 @@ mod tests { } #[test] + #[cfg(feature = "paths")] fn writes_a_document_the_reader_reads() { let mut w = SvgWriter::new(); assert!(!w.is_empty());