From eb752a4209ca5b073b85df104a96da6b74331fbf Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 13 Nov 2026 05:10:23 -0800 Subject: [PATCH 1/8] craft-ui: visual regression tests take turns with the wgpu renderer The visual_regression test binary crashed with SIGSEGV now and then on the Ubuntu CI runner, where wgpu uses Vulkan (WGPU_BACKEND=vulkan) with Mesa's software driver. Four of its tests each create a WgpuTestRenderer, which creates its own Vulkan instance and device, and the test harness runs them on parallel threads. When one test uses its device while another creates or destroys one, the Vulkan loader can read a device entry that is being freed. Each of those tests now takes a shared lock before it creates its renderer and holds it until the renderer is dropped. A poisoned lock is taken over, so a failed test does not block the others. --- crates/ui/tests/visual_regression.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crates/ui/tests/visual_regression.rs b/crates/ui/tests/visual_regression.rs index 532c9dfc7..d9902965b 100644 --- a/crates/ui/tests/visual_regression.rs +++ b/crates/ui/tests/visual_regression.rs @@ -9,6 +9,7 @@ mod fixtures; use std::path::PathBuf; +use std::sync::{Mutex, MutexGuard, PoisonError}; use egui::{Color32, Context, Event, FullOutput, Modifiers, PointerButton, Pos2, RawInput, Rect, Response, TextureId, Ui}; use egui_kittest::{TestRenderer, wgpu::WgpuTestRenderer}; @@ -28,6 +29,17 @@ struct Capture { meshes: Option>, } +/// Lets one test at a time hold a wgpu renderer. With Mesa's software Vulkan driver, a test +/// binary that renders on two devices at once from parallel test threads now and then crashes +/// with SIGSEGV. +static GPU_LOCK: Mutex<()> = Mutex::new(()); + +/// Take this before creating a renderer (`let _gpu = gpu_lock();`), so it is released after the +/// renderer is dropped. A test that failed while holding it does not block the others. +fn gpu_lock() -> MutexGuard<'static, ()> { + GPU_LOCK.lock().unwrap_or_else(PoisonError::into_inner) +} + fn context(theme: Theme, scale: f32) -> Context { let ctx = Context::default(); ctx.set_theme(theme.egui()); @@ -184,6 +196,7 @@ impl Artifacts { #[test] fn rendered_controls_match_independent_reference_pixels_and_meshes() { + let _gpu = gpu_lock(); let mut renderer = WgpuTestRenderer::new(); let mut artifacts = Artifacts::new("controls"); let mut count = 0; @@ -213,6 +226,7 @@ fn rendered_controls_match_independent_reference_pixels_and_meshes() { #[test] fn visual_comparisons_detect_color_spacing_and_stroke_mutations() { + let _gpu = gpu_lock(); let mut renderer = WgpuTestRenderer::new(); let mut artifacts = Artifacts::new("negative-controls"); let mut count = 0; @@ -241,6 +255,7 @@ fn toolbar_fitting_matches_literal_group_and_overflow_positions() { use craft_ui::layout::fit_toolbar; use egui::{Align, Layout, Stroke, StrokeKind, UiBuilder, pos2, vec2}; + let _gpu = gpu_lock(); let mut renderer = WgpuTestRenderer::new(); let mut artifacts = Artifacts::new("toolbar"); let mut count = 0; @@ -295,6 +310,7 @@ fn widget_palette_renders_all_five_states_with_existing_borders_and_expansion() use craft_ui::theme::WidgetPalette; use egui::{CornerRadius, Stroke, StrokeKind, pos2, vec2}; + let _gpu = gpu_lock(); let mut renderer = WgpuTestRenderer::new(); let mut artifacts = Artifacts::new("widget-palette"); for theme in [Theme::Light, Theme::Dark] { From 23b10d78fb2d86a9648951c75d498cb911372510 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 13 Nov 2026 06:05:11 -0800 Subject: [PATCH 2/8] craft-ui: unit tests that render take turns with the wgpu renderer Each egui_kittest wgpu renderer creates its own Vulkan instance and device. When one test uses its device while a test on another thread creates or destroys one, the Vulkan loader now and then crashes the test binary with SIGSEGV. The previous commit serialized the four rendering tests in tests/visual_regression.rs; the craft-ui unit test binary has the same pattern in two tests: combo::tests::the_button_and_menu_look_like_eguis and tabs::dialog::tests::selected_tab_erases_a_baseline_centered_on_its_bottom_edge. Both now take crate::gpu_test_lock() before building their harness and hold it until the harness is dropped. The comment on the lock in visual_regression.rs now names the loader race instead of concurrent rendering alone. --- crates/ui/src/combo/tests.rs | 1 + crates/ui/src/lib.rs | 11 +++++++++++ crates/ui/src/tabs/dialog.rs | 1 + crates/ui/tests/visual_regression.rs | 6 +++--- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/ui/src/combo/tests.rs b/crates/ui/src/combo/tests.rs index 90e1afb15..fbef2413a 100644 --- a/crates/ui/src/combo/tests.rs +++ b/crates/ui/src/combo/tests.rs @@ -170,6 +170,7 @@ fn scene(ui: &mut egui::Ui, ours: bool) { } fn render(ours: bool, hover: Option<&str>, open: Option<&str>) -> image::RgbaImage { + let _gpu = crate::gpu_test_lock(); // Two of the combo boxes have no label: the scene compares looks, not names. let mut h = Harness::builder().with_size(egui::vec2(480.0, 700.0)).with_accessibility_check(false).build_ui(move |ui| scene(ui, ours)); h.run(); diff --git a/crates/ui/src/lib.rs b/crates/ui/src/lib.rs index e5a2d06c2..bbdd912ac 100644 --- a/crates/ui/src/lib.rs +++ b/crates/ui/src/lib.rs @@ -27,3 +27,14 @@ pub mod text; pub mod theme; pub use combo::ComboBox; + +/// Lets one unit test at a time hold a wgpu renderer. Each renderer creates its own Vulkan instance +/// and device. When one test uses its device while a test on another thread creates or destroys +/// one, the Vulkan loader now and then crashes the test binary with SIGSEGV. Take the lock before +/// building the harness (`let _gpu = crate::gpu_test_lock();`), so it is released after the harness +/// is dropped. A test that failed while holding it does not block the others. +#[cfg(test)] +pub(crate) fn gpu_test_lock() -> std::sync::MutexGuard<'static, ()> { + static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} diff --git a/crates/ui/src/tabs/dialog.rs b/crates/ui/src/tabs/dialog.rs index 292cbb4cf..e4820b042 100644 --- a/crates/ui/src/tabs/dialog.rs +++ b/crates/ui/src/tabs/dialog.rs @@ -123,6 +123,7 @@ mod tests { #[test] fn selected_tab_erases_a_baseline_centered_on_its_bottom_edge() { + let _gpu = crate::gpu_test_lock(); let mut h = egui_kittest::Harness::builder().with_size(Vec2::new(120.0, 80.0)).wgpu().build_ui(|ui| { ui.painter().rect_filled(ui.max_rect(), 0.0, Color32::WHITE); let rect = Rect::from_min_size(egui::pos2(10.0, 10.0), Vec2::new(100.0, 26.0)); diff --git a/crates/ui/tests/visual_regression.rs b/crates/ui/tests/visual_regression.rs index d9902965b..87edb8b4f 100644 --- a/crates/ui/tests/visual_regression.rs +++ b/crates/ui/tests/visual_regression.rs @@ -29,9 +29,9 @@ struct Capture { meshes: Option>, } -/// Lets one test at a time hold a wgpu renderer. With Mesa's software Vulkan driver, a test -/// binary that renders on two devices at once from parallel test threads now and then crashes -/// with SIGSEGV. +/// Lets one test at a time hold a wgpu renderer. Each renderer creates its own Vulkan instance and +/// device. When one test uses its device while a test on another thread creates or destroys one, +/// the Vulkan loader now and then crashes the test binary with SIGSEGV. static GPU_LOCK: Mutex<()> = Mutex::new(()); /// Take this before creating a renderer (`let _gpu = gpu_lock();`), so it is released after the From bb9f9a95cc6e8780b1fef0fb9d684536d8b2230a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 00:12:52 +0000 Subject: [PATCH 3/8] Add neutral XFL shape and media schema views --- crates/xfl/src/lib.rs | 4 + crates/xfl/src/media.rs | 190 +++++++++++++++ crates/xfl/src/shape.rs | 500 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 694 insertions(+) create mode 100644 crates/xfl/src/media.rs create mode 100644 crates/xfl/src/shape.rs diff --git a/crates/xfl/src/lib.rs b/crates/xfl/src/lib.rs index 4a957111f..9dd43fd5d 100644 --- a/crates/xfl/src/lib.rs +++ b/crates/xfl/src/lib.rs @@ -27,6 +27,10 @@ use std::collections::HashMap; pub mod document; /// XFL's edge-attribute grammar, independent of any consuming application's geometry types. pub mod edges; +/// Borrowed views of native XFL media library records. +pub mod media; +/// Borrowed views of native XFL shape and style elements. +pub mod shape; /// Largest single entry inflated (bytes). pub const MAX_ENTRY: usize = 256 * 1024 * 1024; diff --git a/crates/xfl/src/media.rs b/crates/xfl/src/media.rs new file mode 100644 index 000000000..a501e7888 --- /dev/null +++ b/crates/xfl/src/media.rs @@ -0,0 +1,190 @@ +//! Borrowed views of XFL's bitmap, sound, and video library-item records. +//! +//! These views expose native XFL metadata and archive member names. Decoding those bytes and +//! mapping the records into an application's media model remain consumer responsibilities. + +use crate::shape::XflElement; + +/// Archive member named by a library item's native `href` attribute. +pub fn library_member(href: &str) -> String { + format!("LIBRARY/{href}") +} + +/// Archive member named by an XFL item's native `*DataHRef` attribute. +pub fn data_member(href: &str) -> String { + format!("bin/{href}") +} + +/// A packaged `./...` external video source, normalized to ZIP's slash separators. +pub fn packaged_external_member(path: &str) -> Option { + path.strip_prefix("./").map(|path| path.replace('\\', "/")) +} + +/// Media library records in an XFL document, in source order. +#[derive(Clone, Copy, Debug)] +pub struct Catalog<'a, E: XflElement> { + media: Option<&'a E>, +} + +impl<'a, E: XflElement> Catalog<'a, E> { + pub fn new(document: &'a E) -> Self { + Self { media: document.child("media") } + } + + pub fn bitmaps(&self) -> impl Iterator> + 'a { + let media = self.media; + media.into_iter().flat_map(|media| media.elements_named("DOMBitmapItem")).map(|element| Bitmap { element }) + } + + pub fn sounds(&self) -> impl Iterator> + 'a { + let media = self.media; + media.into_iter().flat_map(|media| media.elements_named("DOMSoundItem")).map(|element| Sound { element }) + } + + pub fn videos(&self) -> impl Iterator> + 'a { + let media = self.media; + media.into_iter().flat_map(|media| media.elements_named("DOMVideoItem")).map(|element| Video { element }) + } +} + +/// Native bitmap library metadata. +#[derive(Clone, Copy, Debug)] +pub struct Bitmap<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> Bitmap<'a, E> { + pub fn name(&self) -> Option<&'a str> { + self.element.attr("name") + } + + pub fn href(&self) -> Option<&'a str> { + self.element.attr("href") + } + + pub fn data_href(&self) -> Option<&'a str> { + self.element.attr("bitmapDataHRef") + } + + pub fn allow_smoothing(&self) -> Option { + self.element.attr("allowSmoothing").map(|value| value.trim() == "true") + } +} + +/// Native sound library metadata. +#[derive(Clone, Copy, Debug)] +pub struct Sound<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> Sound<'a, E> { + pub fn name(&self) -> Option<&'a str> { + self.element.attr("name") + } + + pub fn href(&self) -> Option<&'a str> { + self.element.attr("href") + } + + pub fn data_href(&self) -> Option<&'a str> { + self.element.attr("soundDataHRef") + } +} + +/// Native video library metadata. +#[derive(Clone, Copy, Debug)] +pub struct Video<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> Video<'a, E> { + pub fn name(&self) -> Option<&'a str> { + self.element.attr("name") + } + + pub fn href(&self) -> Option<&'a str> { + self.element.attr("href") + } + + pub fn external_path(&self) -> Option<&'a str> { + self.element.attr("sourceExternalFilepath") + } + + pub fn data_href(&self) -> Option<&'a str> { + self.element.attr("videoDataHRef") + } + + pub fn length(&self) -> Option { + number(self.element, "length") + } + + pub fn frame_count(&self) -> Option { + number(self.element, "frameCount") + } + + pub fn frames_per_second(&self) -> Option { + number(self.element, "fps") + } +} + +fn number(element: &impl XflElement, name: &str) -> Option { + element.attr(name)?.trim().parse::().ok().filter(|value| value.is_finite()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn parse(source: &[u8]) -> craft_xml::El { + craft_xml::Options::default().parse(source).unwrap() + } + + #[test] + fn reads_native_media_metadata_in_order_and_ignores_other_records() { + let root = parse( + br#" + + + + + + "#, + ); + let catalog = Catalog::new(&root); + let bitmaps: Vec<_> = catalog.bitmaps().collect(); + assert_eq!(bitmaps.len(), 2); + assert_eq!(bitmaps[0].name(), Some("folder/a")); + assert_eq!(bitmaps[0].href(), Some("a.png")); + assert_eq!(bitmaps[0].data_href(), Some("Bitmap 1")); + assert_eq!(bitmaps[0].allow_smoothing(), Some(false)); + assert_eq!( + catalog.sounds().next().map(|item| (item.name(), item.href(), item.data_href())), + Some((Some("s"), Some("s.wav"), Some("Sound 1"))) + ); + let video = catalog.videos().next().expect("video record"); + assert_eq!(video.external_path(), Some("./LIBRARY/v.flv")); + assert_eq!(video.data_href(), Some("Video 1")); + assert_eq!(video.length(), Some(3.5)); + assert_eq!(video.frame_count(), Some(70.0)); + assert_eq!(video.frames_per_second(), Some(20.0)); + } + + #[test] + fn missing_or_invalid_media_metadata_stays_optional() { + let root = parse(br#""#); + let video = Catalog::new(&root).videos().next().expect("video record"); + assert_eq!(video.name(), Some("v")); + assert_eq!(video.length(), None); + assert_eq!(video.frames_per_second(), None); + let empty = parse(b""); + assert_eq!(Catalog::new(&empty).bitmaps().count(), 0); + } + + #[test] + fn native_media_references_resolve_to_the_legacy_archive_members() { + assert_eq!(library_member("folder/a.png"), "LIBRARY/folder/a.png"); + assert_eq!(data_member("Bitmap 1"), "bin/Bitmap 1"); + assert_eq!(packaged_external_member("./LIBRARY\\v.flv").as_deref(), Some("LIBRARY/v.flv")); + assert_eq!(packaged_external_member("/outside/v.flv"), None); + } +} diff --git a/crates/xfl/src/shape.rs b/crates/xfl/src/shape.rs new file mode 100644 index 000000000..ec7c0d8fc --- /dev/null +++ b/crates/xfl/src/shape.rs @@ -0,0 +1,500 @@ +//! Borrowed views of native XFL shape and style elements. +//! +//! These views recognize XFL's element names and attribute grammar without depending on an +//! application's geometry, paint, or animation types. They work with `craft_xml::El` and with +//! compatible borrowed element adapters implemented by consumers. + +use crate::edges::{self, Segment}; + +/// An element source used by the borrowed XFL schema views. +/// +/// Consumers with an existing XML facade can implement this small interface and use the same +/// XFL parser without converting or reparsing their tree. Attribute lookup must return the last +/// occurrence when an XML document repeats an attribute, matching the native XFL reader's +/// historical behavior. +pub trait XflElement { + /// Iterator type for direct child elements. + type Elements<'a>: Iterator + where + Self: 'a; + + /// The element's local name, with any namespace prefix removed. + fn local_name(&self) -> &str; + + /// The last attribute with this exact name. + fn attr(&self, name: &str) -> Option<&str>; + + /// Direct child elements in document order. + fn elements(&self) -> Self::Elements<'_>; + + /// The first direct child whose local name matches. + fn child(&self, name: &str) -> Option<&Self> { + self.elements().find(|element| element.local_name() == name) + } + + /// Direct children whose local name matches, in document order. + fn elements_named<'a>(&'a self, name: &'a str) -> impl Iterator + 'a { + self.elements().filter(move |element| element.local_name() == name) + } +} + +/// Direct child elements of `craft_xml::El`, used by its `XflElement` implementation. +pub struct XmlElements<'a> { + nodes: std::slice::Iter<'a, craft_xml::Node>, +} + +impl<'a> Iterator for XmlElements<'a> { + type Item = &'a craft_xml::El; + + fn next(&mut self) -> Option { + self.nodes.find_map(|node| match node { + craft_xml::Node::El(element) => Some(element), + _ => None, + }) + } +} + +impl XflElement for craft_xml::El { + type Elements<'a> = XmlElements<'a>; + + fn local_name(&self) -> &str { + self.name.rsplit(':').next().unwrap_or(&self.name) + } + + fn attr(&self, name: &str) -> Option<&str> { + self.attrs.iter().rev().find(|(key, _)| key == name).map(|(_, value)| value.as_str()) + } + + fn elements(&self) -> Self::Elements<'_> { + XmlElements { nodes: self.children.iter() } + } +} + +/// XFL matrix coefficients, in the native units stored in XML. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Matrix { + pub a: f32, + pub b: f32, + pub c: f32, + pub d: f32, + pub tx: f32, + pub ty: f32, +} + +/// A color encoded by an XFL color attribute and its optional alpha attribute. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Color { + pub rgb: u32, + pub alpha: f32, +} + +impl Matrix { + /// The matrix used when an XFL element omits its transform. + pub const IDENTITY: Matrix = Matrix { a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0 }; + + fn finite(self) -> bool { + [self.a, self.b, self.c, self.d, self.tx, self.ty].into_iter().all(f32::is_finite) + } +} + +fn number(element: &impl XflElement, name: &str) -> Option { + element.attr(name)?.trim().parse::().ok().filter(|value| value.is_finite()) +} + +fn integer(element: &impl XflElement, name: &str) -> Option { + element.attr(name)?.trim().parse::().ok() +} + +fn boolean(element: &impl XflElement, name: &str) -> Option { + element.attr(name).map(|value| value.trim() == "true") +} + +/// Parses XFL's hexadecimal color and alpha attributes. Missing or malformed colors use black; +/// missing, malformed, or non-finite alpha uses full opacity, matching the legacy importer. +pub fn color(element: &impl XflElement, color_attribute: &str, alpha_attribute: &str) -> Color { + let rgb = element.attr(color_attribute).and_then(|value| u32::from_str_radix(value.trim().trim_start_matches('#'), 16).ok()).unwrap_or(0); + let alpha = number(element, alpha_attribute).unwrap_or(1.0).clamp(0.0, 1.0); + Color { rgb, alpha } +} + +/// Reads the native `` transform, using XFL's identity defaults. +/// A non-finite coefficient makes the entire transform identity, matching the previous reader. +pub fn matrix(element: &impl XflElement) -> Matrix { + let Some(matrix) = element.child("matrix").and_then(|wrapper| wrapper.child("Matrix")) else { + return Matrix::IDENTITY; + }; + let parsed = Matrix { + a: number(matrix, "a").unwrap_or(1.0), + b: number(matrix, "b").unwrap_or(0.0), + c: number(matrix, "c").unwrap_or(0.0), + d: number(matrix, "d").unwrap_or(1.0), + tx: number(matrix, "tx").unwrap_or(0.0), + ty: number(matrix, "ty").unwrap_or(0.0), + }; + if parsed.finite() { parsed } else { Matrix::IDENTITY } +} + +/// A native `DOMShape` or drawing shape and its XFL style tables. +#[derive(Clone, Copy, Debug)] +pub struct Shape<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> Shape<'a, E> { + /// Views an element as an XFL shape when its local name is `DOMShape`. + pub fn new(element: &'a E) -> Option { + (element.local_name() == "DOMShape").then_some(Self { element }) + } + + /// Native shape transform. + pub fn matrix(&self) -> Matrix { + matrix(self.element) + } + + /// Whether Animate stores the shape as a drawing object. + pub fn is_drawing_object(&self) -> Option { + boolean(self.element, "isDrawingObject") + } + + /// Fill styles, preserving their native order and index values. + pub fn fills(&self) -> impl Iterator> + '_ { + self.element.child("fills").into_iter().flat_map(|table| table.elements_named("FillStyle")).map(|element| FillStyle { element }) + } + + /// Stroke styles, preserving their native order and index values. + pub fn strokes(&self) -> impl Iterator> + '_ { + self.element.child("strokes").into_iter().flat_map(|table| table.elements_named("StrokeStyle")).map(|element| StrokeStyle { element }) + } + + /// Native edge records, preserving malformed and unknown data for consumer policy. + pub fn edges(&self) -> impl Iterator> + '_ { + self.element.child("edges").into_iter().flat_map(|table| table.elements_named("Edge")).map(|element| Edge { element }) + } +} + +/// A native `FillStyle` entry. +#[derive(Clone, Copy, Debug)] +pub struct FillStyle<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> FillStyle<'a, E> { + pub fn index(&self) -> Option { + integer(self.element, "index") + } + + /// The first recognized fill definition inside this table entry. + pub fn definition(&self) -> Option> { + self.element.elements().next().map(|element| Fill { element }) + } +} + +/// Native fill definitions understood by XFL. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FillKind { + SolidColor, + LinearGradient, + RadialGradient, + Bitmap, + Other, +} + +/// A native XFL fill definition. +#[derive(Clone, Copy, Debug)] +pub struct Fill<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> Fill<'a, E> { + /// Views any native fill-definition element, including fills nested in primitives or strokes. + pub fn new(element: &'a E) -> Self { + Self { element } + } + + /// Underlying native definition element for consumers that map it into application types. + pub fn element(&self) -> &'a E { + self.element + } + + pub fn kind(&self) -> FillKind { + match self.element.local_name() { + "SolidColor" => FillKind::SolidColor, + "LinearGradient" => FillKind::LinearGradient, + "RadialGradient" => FillKind::RadialGradient, + "BitmapFill" => FillKind::Bitmap, + _ => FillKind::Other, + } + } + + pub fn color(&self) -> Option<&str> { + self.element.attr("color") + } + + pub fn color_value(&self) -> Color { + color(self.element, "color", "alpha") + } + + pub fn alpha(&self) -> Option { + number(self.element, "alpha") + } + + pub fn matrix(&self) -> Matrix { + matrix(self.element) + } + + pub fn spread_method(&self) -> Option<&str> { + self.element.attr("spreadMethod") + } + + pub fn interpolation_method(&self) -> Option<&str> { + self.element.attr("interpolationMethod") + } + + pub fn focal_point_ratio(&self) -> Option { + number(self.element, "focalPointRatio") + } + + pub fn bitmap_path(&self) -> Option<&str> { + self.element.attr("bitmapPath") + } + + pub fn bitmap_is_clipped(&self) -> Option { + boolean(self.element, "bitmapIsClipped") + } + + /// Gradient entries, preserving source order and raw native ratios. + pub fn gradient_entries(&self) -> impl Iterator> + '_ { + self.element.elements_named("GradientEntry").map(|element| GradientEntry { element }) + } +} + +/// A native gradient ramp entry. +#[derive(Clone, Copy, Debug)] +pub struct GradientEntry<'a, E: XflElement> { + element: &'a E, +} + +impl GradientEntry<'_, E> { + pub fn ratio(&self) -> Option { + number(self.element, "ratio") + } + + pub fn color(&self) -> Option<&str> { + self.element.attr("color") + } + + pub fn color_value(&self) -> Color { + color(self.element, "color", "alpha") + } + + pub fn alpha(&self) -> Option { + number(self.element, "alpha") + } +} + +/// Native stroke definitions understood by XFL. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum StrokeKind { + Solid, + Dashed, + Dotted, + Ragged, + Stipple, + Hatched, + Other, +} + +/// A native `StrokeStyle` table entry. +#[derive(Clone, Copy, Debug)] +pub struct StrokeStyle<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> StrokeStyle<'a, E> { + pub fn index(&self) -> Option { + integer(self.element, "index") + } + + /// The first stroke definition inside this table entry. + pub fn definition(&self) -> Option> { + self.element.elements().next().map(|element| Stroke { element }) + } +} + +/// A native XFL stroke definition. +#[derive(Clone, Copy, Debug)] +pub struct Stroke<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> Stroke<'a, E> { + /// Views a native stroke-definition element. + pub fn new(element: &'a E) -> Self { + Self { element } + } + + /// Underlying native definition element for consumers that map it into application types. + pub fn element(&self) -> &'a E { + self.element + } + + pub fn kind(&self) -> StrokeKind { + match self.element.local_name() { + "SolidStroke" => StrokeKind::Solid, + "DashedStroke" => StrokeKind::Dashed, + "DottedStroke" => StrokeKind::Dotted, + "RaggedStroke" => StrokeKind::Ragged, + "StippleStroke" => StrokeKind::Stipple, + "HatchedStroke" => StrokeKind::Hatched, + _ => StrokeKind::Other, + } + } + + pub fn weight(&self) -> Option { + number(self.element, "weight") + } + + pub fn caps(&self) -> Option<&str> { + self.element.attr("caps") + } + + pub fn joints(&self) -> Option<&str> { + self.element.attr("joints") + } + + pub fn miter_limit(&self) -> Option { + number(self.element, "miterLimit") + } + + pub fn dash1(&self) -> Option { + number(self.element, "dash1") + } + + pub fn dash2(&self) -> Option { + number(self.element, "dash2") + } + + pub fn dot_space(&self) -> Option { + number(self.element, "dotSpace") + } + + /// The first fill definition nested in the stroke. + pub fn fill(&self) -> Option> { + self.element.child("fill")?.elements().next().map(|element| Fill { element }) + } +} + +/// A native `` record and its style indices. +#[derive(Clone, Copy, Debug)] +pub struct Edge<'a, E: XflElement> { + element: &'a E, +} + +impl Edge<'_, E> { + pub fn fill_style0(&self) -> Option { + integer(self.element, "fillStyle0") + } + + pub fn fill_style1(&self) -> Option { + integer(self.element, "fillStyle1") + } + + pub fn stroke_style(&self) -> Option { + integer(self.element, "strokeStyle") + } + + pub fn encoded_segments(&self) -> Option<&str> { + self.element.attr("edges") + } + + /// Parsed XFL path segments, using the shared bounded edge grammar. + pub fn segments(&self) -> Vec { + self.encoded_segments().map(edges::parse_edge_segments).unwrap_or_default() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn parse(source: &[u8]) -> craft_xml::El { + craft_xml::Options::default().parse(source).unwrap() + } + + #[test] + fn reads_prefixed_shape_fill_stroke_and_edge_schema() { + let root = parse(br##" + + + + + + + "##); + + let shape = Shape::new(&root).unwrap(); + assert_eq!(shape.matrix(), Matrix { a: 2.0, b: 0.0, c: 0.0, d: 1.0, tx: 12.0, ty: 0.0 }); + assert_eq!(shape.is_drawing_object(), Some(false)); + let fills: Vec<_> = shape.fills().collect(); + assert_eq!(fills.iter().map(FillStyle::index).collect::>(), [Some(1), Some(2)]); + let gradient = fills[0].definition().unwrap(); + assert_eq!(gradient.kind(), FillKind::LinearGradient); + assert_eq!(gradient.spread_method(), Some("reflect")); + assert_eq!(gradient.interpolation_method(), Some("linearRGB")); + assert_eq!(gradient.gradient_entries().map(|entry| entry.ratio()).collect::>(), [Some(0.0), Some(255.0)]); + let bitmap = fills[1].definition().unwrap(); + assert_eq!(bitmap.kind(), FillKind::Bitmap); + assert_eq!(bitmap.bitmap_path(), Some("Library/a.png")); + assert_eq!(bitmap.bitmap_is_clipped(), Some(true)); + + let stroke_style = shape.strokes().next().unwrap(); + let stroke = stroke_style.definition().unwrap(); + assert_eq!(stroke.kind(), StrokeKind::Dashed); + assert_eq!(stroke.weight(), Some(2.0)); + assert_eq!((stroke.dash1(), stroke.dash2()), (Some(8.0), Some(3.0))); + assert_eq!(stroke.fill().unwrap().color(), Some("#abcdef")); + let color = stroke.fill().unwrap().color_value(); + assert_eq!(color, Color { rgb: 0xabcdef, alpha: 1.0 }); + + let edge = shape.edges().next().unwrap(); + assert_eq!((edge.fill_style0(), edge.fill_style1(), edge.stroke_style()), (Some(2), Some(1), Some(3))); + assert_eq!(edge.segments().len(), 1); + } + + #[test] + fn duplicate_attributes_use_last_value_and_invalid_numbers_are_absent() { + let root = parse(b""); + let shape = Shape::new(&root).unwrap(); + assert_eq!(shape.is_drawing_object(), Some(true)); + assert_eq!(shape.matrix(), Matrix::IDENTITY); + let fill = shape.fills().next().unwrap(); + assert_eq!(fill.index(), Some(4)); + let fill = fill.definition().unwrap(); + assert_eq!(fill.alpha(), None); + assert_eq!(fill.color_value(), Color { rgb: 0, alpha: 1.0 }); + } + + #[test] + fn an_application_element_adapter_can_use_the_same_views() { + #[derive(Debug)] + struct AppElement { + name: String, + attrs: Vec<(String, String)>, + children: Vec, + } + impl XflElement for AppElement { + type Elements<'a> = std::slice::Iter<'a, AppElement>; + fn local_name(&self) -> &str { + self.name.rsplit(':').next().unwrap_or(&self.name) + } + fn attr(&self, name: &str) -> Option<&str> { + self.attrs.iter().rev().find(|(key, _)| key == name).map(|(_, value)| value.as_str()) + } + fn elements(&self) -> Self::Elements<'_> { + self.children.iter() + } + } + let app = AppElement { name: "x:DOMShape".into(), attrs: vec![("isDrawingObject".into(), "true".into())], children: Vec::new() }; + assert_eq!(Shape::new(&app).unwrap().is_drawing_object(), Some(true)); + } +} From 352a410332c1c1d53bbb358b47cfd53ce18579b4 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 00:18:46 +0000 Subject: [PATCH 4/8] Expose native XFL drawing primitives --- crates/xfl/src/shape.rs | 80 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/crates/xfl/src/shape.rs b/crates/xfl/src/shape.rs index ec7c0d8fc..8b0667a1a 100644 --- a/crates/xfl/src/shape.rs +++ b/crates/xfl/src/shape.rs @@ -140,6 +140,68 @@ pub struct Shape<'a, E: XflElement> { element: &'a E, } +/// Kind of native drawable primitive stored in XFL. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PrimitiveKind { + Rectangle, + Oval, +} + +/// Native rectangle or oval object, including its native style definitions. +#[derive(Clone, Copy, Debug)] +pub struct Primitive<'a, E: XflElement> { + element: &'a E, + kind: PrimitiveKind, +} + +impl<'a, E: XflElement> Primitive<'a, E> { + /// Views a supported native primitive by its local element name. + pub fn new(element: &'a E) -> Option { + let kind = match element.local_name() { + "DOMRectangleObject" => PrimitiveKind::Rectangle, + "DOMOvalObject" => PrimitiveKind::Oval, + _ => return None, + }; + Some(Self { element, kind }) + } + + pub fn kind(&self) -> PrimitiveKind { + self.kind + } + + pub fn x(&self) -> Option { + number(self.element, "x") + } + + pub fn y(&self) -> Option { + number(self.element, "y") + } + + pub fn width(&self) -> Option { + number(self.element, "objectWidth") + } + + pub fn height(&self) -> Option { + number(self.element, "objectHeight") + } + + pub fn top_left_radius(&self) -> Option { + number(self.element, "topLeftRadius") + } + + pub fn matrix(&self) -> Matrix { + matrix(self.element) + } + + pub fn fill(&self) -> Option> { + self.element.child("fill")?.elements().next().map(|element| Fill { element }) + } + + pub fn stroke(&self) -> Option> { + self.element.child("stroke")?.elements().next().map(|element| Stroke { element }) + } +} + impl<'a, E: XflElement> Shape<'a, E> { /// Views an element as an XFL shape when its local name is `DOMShape`. pub fn new(element: &'a E) -> Option { @@ -497,4 +559,22 @@ mod tests { let app = AppElement { name: "x:DOMShape".into(), attrs: vec![("isDrawingObject".into(), "true".into())], children: Vec::new() }; assert_eq!(Shape::new(&app).unwrap().is_drawing_object(), Some(true)); } + + #[test] + fn reads_native_rectangle_and_oval_geometry_and_styles() { + let root = parse(br##""##); + let primitive = Primitive::new(&root).unwrap(); + assert_eq!(primitive.kind(), PrimitiveKind::Rectangle); + assert_eq!( + (primitive.x(), primitive.y(), primitive.width(), primitive.height(), primitive.top_left_radius()), + (Some(-2.0), Some(3.0), Some(20.0), Some(10.0), Some(4.0)) + ); + assert_eq!(primitive.matrix().tx, 7.0); + assert_eq!(primitive.fill().unwrap().kind(), FillKind::SolidColor); + assert_eq!(primitive.stroke().unwrap().kind(), StrokeKind::Dashed); + + let oval = parse(b""); + assert_eq!(Primitive::new(&oval).unwrap().kind(), PrimitiveKind::Oval); + assert!(Primitive::new(&parse(b"")).is_none()); + } } From e4eddc5fe55a2cced3854a14f1ef326a20ad449b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 00:23:42 +0000 Subject: [PATCH 5/8] Preserve legacy XFL style table permissiveness --- crates/xfl/src/shape.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/xfl/src/shape.rs b/crates/xfl/src/shape.rs index 8b0667a1a..24074e165 100644 --- a/crates/xfl/src/shape.rs +++ b/crates/xfl/src/shape.rs @@ -220,12 +220,12 @@ impl<'a, E: XflElement> Shape<'a, E> { /// Fill styles, preserving their native order and index values. pub fn fills(&self) -> impl Iterator> + '_ { - self.element.child("fills").into_iter().flat_map(|table| table.elements_named("FillStyle")).map(|element| FillStyle { element }) + self.element.child("fills").into_iter().flat_map(|table| table.elements()).map(|element| FillStyle { element }) } /// Stroke styles, preserving their native order and index values. pub fn strokes(&self) -> impl Iterator> + '_ { - self.element.child("strokes").into_iter().flat_map(|table| table.elements_named("StrokeStyle")).map(|element| StrokeStyle { element }) + self.element.child("strokes").into_iter().flat_map(|table| table.elements()).map(|element| StrokeStyle { element }) } /// Native edge records, preserving malformed and unknown data for consumer policy. @@ -245,7 +245,7 @@ impl<'a, E: XflElement> FillStyle<'a, E> { integer(self.element, "index") } - /// The first recognized fill definition inside this table entry. + /// The first native definition element inside this table entry. pub fn definition(&self) -> Option> { self.element.elements().next().map(|element| Fill { element }) } @@ -377,7 +377,7 @@ impl<'a, E: XflElement> StrokeStyle<'a, E> { integer(self.element, "index") } - /// The first stroke definition inside this table entry. + /// The first native definition element inside this table entry. pub fn definition(&self) -> Option> { self.element.elements().next().map(|element| Stroke { element }) } @@ -536,6 +536,18 @@ mod tests { assert_eq!(fill.color_value(), Color { rgb: 0, alpha: 1.0 }); } + #[test] + fn style_table_views_keep_the_legacy_first_child_and_index_behavior() { + let root = parse(b""); + let shape = Shape::new(&root).unwrap(); + let fill_style = shape.fills().next().unwrap(); + assert_eq!(fill_style.index(), Some(5)); + assert_eq!(fill_style.definition().map(|fill| fill.kind()), Some(FillKind::SolidColor)); + let stroke_style = shape.strokes().next().unwrap(); + assert_eq!(stroke_style.index(), Some(6)); + assert_eq!(stroke_style.definition().map(|stroke| stroke.kind()), Some(StrokeKind::Solid)); + } + #[test] fn an_application_element_adapter_can_use_the_same_views() { #[derive(Debug)] From d5a77b7dc1f8b6445f26581c0a4ea63de7d25186 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 00:33:05 +0000 Subject: [PATCH 6/8] Preserve legacy XFL matrix coefficient defaults --- crates/xfl/src/shape.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/crates/xfl/src/shape.rs b/crates/xfl/src/shape.rs index 24074e165..bcd3e9373 100644 --- a/crates/xfl/src/shape.rs +++ b/crates/xfl/src/shape.rs @@ -91,10 +91,6 @@ pub struct Color { impl Matrix { /// The matrix used when an XFL element omits its transform. pub const IDENTITY: Matrix = Matrix { a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0 }; - - fn finite(self) -> bool { - [self.a, self.b, self.c, self.d, self.tx, self.ty].into_iter().all(f32::is_finite) - } } fn number(element: &impl XflElement, name: &str) -> Option { @@ -117,21 +113,23 @@ pub fn color(element: &impl XflElement, color_attribute: &str, alpha_attribute: Color { rgb, alpha } } -/// Reads the native `` transform, using XFL's identity defaults. -/// A non-finite coefficient makes the entire transform identity, matching the previous reader. +/// Reads the native `` transform. +/// +/// Each absent, malformed, or non-finite coefficient uses its identity default independently. +/// This preserves the prior AniCraft reader's `Element::f32` behavior, including retaining other +/// valid coefficients when one coefficient is invalid. pub fn matrix(element: &impl XflElement) -> Matrix { let Some(matrix) = element.child("matrix").and_then(|wrapper| wrapper.child("Matrix")) else { return Matrix::IDENTITY; }; - let parsed = Matrix { + Matrix { a: number(matrix, "a").unwrap_or(1.0), b: number(matrix, "b").unwrap_or(0.0), c: number(matrix, "c").unwrap_or(0.0), d: number(matrix, "d").unwrap_or(1.0), tx: number(matrix, "tx").unwrap_or(0.0), ty: number(matrix, "ty").unwrap_or(0.0), - }; - if parsed.finite() { parsed } else { Matrix::IDENTITY } + } } /// A native `DOMShape` or drawing shape and its XFL style tables. @@ -525,10 +523,10 @@ mod tests { #[test] fn duplicate_attributes_use_last_value_and_invalid_numbers_are_absent() { - let root = parse(b""); + let root = parse(b""); let shape = Shape::new(&root).unwrap(); assert_eq!(shape.is_drawing_object(), Some(true)); - assert_eq!(shape.matrix(), Matrix::IDENTITY); + assert_eq!(shape.matrix(), Matrix { tx: 12.0, ..Matrix::IDENTITY }); let fill = shape.fills().next().unwrap(); assert_eq!(fill.index(), Some(4)); let fill = fill.definition().unwrap(); From 92c0b49b5e01762e0f2944a286865584fae009d2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 20:38:28 -0500 Subject: [PATCH 7/8] feat(video): support tiled AV1 for large frames (#1453) * feat(video): add tiled AV1 large-frame fallback * feat(video): expose tiled AV1 speed preset * fix(video): use clamped AV1 keyframe interval --- Cargo.lock | 1 + crates/video/Cargo.toml | 5 + crates/video/README.md | 2 +- crates/video/src/av1/encode/README.md | 9 ++ crates/video/src/av1/encode/large.rs | 155 ++++++++++++++++++++++++++ crates/video/src/av1/encode/mod.rs | 5 + 6 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 crates/video/src/av1/encode/large.rs diff --git a/Cargo.lock b/Cargo.lock index 6e48813da..fb1a3f862 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2732,6 +2732,7 @@ version = "0.1.0" dependencies = [ "craft-media", "craft-media-testkit", + "rav1e", "rayon", "thiserror 2.0.21", ] diff --git a/crates/video/Cargo.toml b/crates/video/Cargo.toml index e5bbf1c94..c3ebfd722 100644 --- a/crates/video/Cargo.toml +++ b/crates/video/Cargo.toml @@ -52,6 +52,8 @@ vp9-encode = ["craft-media/isobmff"] av1 = ["av1-decode", "av1-encode"] av1-decode = [] av1-encode = ["craft-media/isobmff"] +# Tiled AV1 fallback for frames that exceed the clean-room encoder's single-tile limits. +av1-large-encode = ["av1-encode", "dep:rav1e"] # Decode and encode across worker threads (rayon) in the codecs that support it. threads = ["dep:rayon"] @@ -60,6 +62,9 @@ craft-media = { workspace = true } thiserror = { workspace = true } rayon = { workspace = true, optional = true } +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +rav1e = { version = "0.8", default-features = false, optional = true } + [dev-dependencies] craft-media-testkit = { workspace = true } # The AVI muxer, for the MPEG-4 Part 2 encoder's end-to-end test. diff --git a/crates/video/README.md b/crates/video/README.md index 7de348bb6..248e922b3 100644 --- a/crates/video/README.md +++ b/crates/video/README.md @@ -27,5 +27,5 @@ No codec feature is on by default. | `h264` | [`h264`](src/h264/decode/README.md) | ITU-T H.264 / AVC: decoder (`h264-decode` alone) and encoder (`h264-encode` alone) | | `hevc` | [`hevc`](src/hevc/decode/README.md) | ITU-T H.265 / HEVC: decoder (`hevc-decode` alone) and encoder (`hevc-encode` alone) | | `vp9` | [`vp9`](src/vp9/decode/README.md) | VP9: decoder (`vp9-decode` alone) and encoder (`vp9-encode` alone) | -| `av1` | [`av1`](src/av1/decode/README.md) | AV1 (AOMedia Video 1): decoder (`av1-decode` alone) and encoder (`av1-encode` alone) | +| `av1` | [`av1`](src/av1/decode/README.md) | AV1 (AOMedia Video 1): decoder (`av1-decode` alone) and clean-room encoder (`av1-encode`); tiled rav1e fallback for large frames (`av1-large-encode`) | | `threads` | — | Multi-threaded slice/row parallelism via `rayon` | diff --git a/crates/video/src/av1/encode/README.md b/crates/video/src/av1/encode/README.md index aa1604706..2df88ba64 100644 --- a/crates/video/src/av1/encode/README.md +++ b/crates/video/src/av1/encode/README.md @@ -3,6 +3,15 @@ Clean-room, pure-Rust AV1 encoder for MP4 / WebM export (layer L0, `std` only, no `unsafe`, builds for `wasm32-unknown-unknown`). +## Large frame fallback + +The optional `av1-large-encode` feature exposes `LargeEncoder`, a tiled rav1e-backed encoder for +8-bit 4:2:0 frames up to 8192 samples per side. Use it when the clean-room `Encoder`'s one-tile +limits (4096 samples per side and 2304 superblocks) would reject the frame. It supports constant +quantizer rate control and emits one low-latency packet per input frame. The fallback is native +only; the clean-room encoder remains available without the feature and supports 8- and 10-bit +input. + ## What is implemented - **Stream**: Main profile (`seq_profile` 0), 4:2:0, 8 or 10 bit, the configured colour description (BT.709 by default), diff --git a/crates/video/src/av1/encode/large.rs b/crates/video/src/av1/encode/large.rs new file mode 100644 index 000000000..109a536dc --- /dev/null +++ b/crates/video/src/av1/encode/large.rs @@ -0,0 +1,155 @@ +//! Tiled AV1 encoding for frames outside the clean-room encoder's single-tile limits. + +use super::{EncoderConfig, Error, Frame, Packet, RateControl}; +use rav1e::prelude::{ + ChromaSampling, ColorDescription, ColorPrimaries, Config, Context, EncoderConfig as Rav1eConfig, FrameParameters, FrameType, FrameTypeOverride, + MatrixCoefficients, PixelRange, Rational, TransferCharacteristics, +}; + +/// Tiled AV1 encoder used when a frame is too large for [`super::Encoder`]. +pub struct LargeEncoder { + ctx: Context, + width: usize, + height: usize, + seq_header: Option>, +} + +impl LargeEncoder { + /// Creates a tiled 8-bit 4:2:0 encoder for dimensions up to 8192 samples per side. + pub fn new(cfg: &EncoderConfig) -> Result { + Self::with_speed(cfg, 6) + } + + /// Creates the tiled encoder with a rav1e speed preset from 1 (slower) to 10 (faster). + pub fn with_speed(cfg: &EncoderConfig, speed: u8) -> Result { + if cfg.width < 16 || cfg.height < 16 || cfg.width > 8192 || cfg.height > 8192 { + return Err(Error::InvalidConfig("large AV1 frame dimensions must be 16..=8192".into())); + } + if cfg.bit_depth != 8 || cfg.fps_num == 0 || cfg.fps_den == 0 { + return Err(Error::InvalidConfig("large AV1 encoding requires 8-bit input and a valid frame rate".into())); + } + let RateControl::ConstantQ(quality) = cfg.rate else { + return Err(Error::InvalidConfig("large AV1 encoding currently supports constant quantizer rate control".into())); + }; + let mut ec = Rav1eConfig::with_speed_preset(speed.clamp(1, 10)); + ec.width = cfg.width as usize; + ec.height = cfg.height as usize; + ec.time_base = Rational::new(u64::from(cfg.fps_den), u64::from(cfg.fps_num)); + ec.bit_depth = 8; + ec.chroma_sampling = ChromaSampling::Cs420; + ec.pixel_range = if cfg.full_range { PixelRange::Full } else { PixelRange::Limited }; + ec.color_description = Some(ColorDescription { + color_primaries: ColorPrimaries::BT709, + transfer_characteristics: TransferCharacteristics::BT709, + matrix_coefficients: MatrixCoefficients::BT709, + }); + ec.low_latency = true; + ec.quantizer = quality.max(1) as usize; + ec.min_quantizer = ec.quantizer.min(255) as u8; + ec.min_key_frame_interval = 1; + ec.max_key_frame_interval = cfg.keyint.clamp(1, 120) as u64; + ec.tiles = std::thread::available_parallelism().map_or(4, |n| n.get()).clamp(2, 16); + let ctx = Config::new().with_encoder_config(ec).new_context().map_err(|e| Error::InvalidConfig(format!("rav1e: {e}")))?; + Ok(Self { ctx, width: cfg.width as usize, height: cfg.height as usize, seq_header: None }) + } + + /// Encodes one frame. The frame planes must match the configured 4:2:0 dimensions. + pub fn encode_with_key(&mut self, frame: &Frame<'_>, force_key: bool) -> Result { + if frame.y_stride < self.width + || frame.uv_stride < self.width / 2 + || frame.y.len() < frame.y_stride.saturating_mul(self.height) + || frame.u.len() < frame.uv_stride.saturating_mul(self.height / 2) + || frame.v.len() < frame.uv_stride.saturating_mul(self.height / 2) + { + return Err(Error::InvalidFrame("plane dimensions or strides do not match the configured frame".into())); + } + let mut input = self.ctx.new_frame(); + copy_plane(&mut input.planes[0], frame.y, frame.y_stride, self.width, self.height)?; + copy_plane(&mut input.planes[1], frame.u, frame.uv_stride, self.width / 2, self.height / 2)?; + copy_plane(&mut input.planes[2], frame.v, frame.uv_stride, self.width / 2, self.height / 2)?; + let params = + FrameParameters { frame_type_override: if force_key { FrameTypeOverride::Key } else { FrameTypeOverride::No }, ..Default::default() }; + self.ctx.send_frame((input, params)).map_err(|e| Error::InvalidFrame(format!("rav1e: {e}")))?; + loop { + match self.ctx.receive_packet() { + Ok(packet) => { + let (data, seq) = sample_data(&packet.data)?; + if self.seq_header.is_none() { + self.seq_header = seq; + } + return Ok(Packet { data, keyframe: packet.frame_type == FrameType::KEY }); + } + Err(rav1e::prelude::EncoderStatus::Encoded) => continue, + Err(rav1e::prelude::EncoderStatus::NeedMoreData | rav1e::prelude::EncoderStatus::LimitReached) => { + return Err(Error::InvalidFrame("rav1e did not emit a low-latency frame".into())); + } + Err(e) => return Err(Error::InvalidFrame(format!("rav1e: {e}"))), + } + } + } + + /// Returns the AV1 configuration record after the first frame has been encoded. + pub fn av1c(&self) -> Option> { + self.seq_header.as_ref().map(|obu| { + let mut config = vec![0x81, 31, 0x0c, 0]; + config.extend_from_slice(obu); + config + }) + } +} + +fn copy_plane(plane: &mut rav1e::prelude::Plane, source: &[u16], stride: usize, width: usize, height: usize) -> Result<(), Error> { + let mut packed = Vec::with_capacity(width * height); + for row in 0..height { + let start = row * stride; + let pixels = &source[start..start + width]; + if pixels.iter().any(|&value| value > u8::MAX as u16) { + return Err(Error::InvalidFrame("8-bit AV1 samples must be in 0..=255".into())); + } + packed.extend(pixels.iter().map(|&value| value as u8)); + } + plane.copy_from_raw_u8(&packed, width, 1); + Ok(()) +} + +fn sample_data(data: &[u8]) -> Result<(Vec, Option>), Error> { + let mut pos = 0usize; + let mut sample = Vec::with_capacity(data.len()); + let mut sequence = None; + while pos < data.len() { + let start = pos; + let header = data[pos]; + if header & 0x80 != 0 { + return Err(Error::InvalidFrame("rav1e emitted an invalid OBU".into())); + } + let kind = (header >> 3) & 0x0f; + pos += 1 + usize::from(header & 0x04 != 0); + let end = if header & 0x02 != 0 { + let mut size = 0usize; + let mut shift = 0usize; + loop { + let byte = *data.get(pos).ok_or_else(|| Error::InvalidFrame("truncated OBU size".into()))?; + pos += 1; + size |= usize::from(byte & 0x7f).checked_shl(shift as u32).ok_or_else(|| Error::InvalidFrame("OBU size overflow".into()))?; + if byte & 0x80 == 0 { + break; + } + shift += 7; + if shift >= usize::BITS as usize { + return Err(Error::InvalidFrame("OBU size overflow".into())); + } + } + pos.checked_add(size).filter(|end| *end <= data.len()).ok_or_else(|| Error::InvalidFrame("OBU exceeds packet bounds".into()))? + } else { + data.len() + }; + if kind == 1 && sequence.is_none() { + sequence = Some(data[start..end].to_vec()); + } + if kind != 2 && kind != 15 { + sample.extend_from_slice(&data[start..end]); + } + pos = end; + } + Ok((sample, sequence)) +} diff --git a/crates/video/src/av1/encode/mod.rs b/crates/video/src/av1/encode/mod.rs index 10e88d0ad..3a954cef2 100644 --- a/crates/video/src/av1/encode/mod.rs +++ b/crates/video/src/av1/encode/mod.rs @@ -20,6 +20,11 @@ mod tables; mod transform; mod writer; +#[cfg(all(feature = "av1-large-encode", not(target_arch = "wasm32")))] +mod large; +#[cfg(all(feature = "av1-large-encode", not(target_arch = "wasm32")))] +pub use large::LargeEncoder; + use bitw::obu; use craft_media::bitstream::BitWriter; use craft_media::isobmff::Av1Config; From 5c0b0a3632d1f4ff5f870cd78db75f0547fb4a2e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 20:46:22 -0500 Subject: [PATCH 8/8] Deduplicate raster codecs via shared image implementations (successor to #1198) (#1331) * Share small raster format implementations (cherry picked from commit 7aedc1de73d5fe3e2a771696ddd5401abf665d63) * Preserve raster facade encode contracts (cherry picked from commit 2db7c82705077e0bdffbd388900d511d4949ce27) * Allow alpha in IFF compatibility encoder (cherry picked from commit 80b55dee5a4a33e5cedd38df16c10f2aebaeeb82) * Test shared raster facade contracts and refresh lockfile --- Cargo.lock | 1 - crates/image-codecs/src/codecs/pcx.rs | 35 +- crates/image-codecs/src/codecs/xpm.rs | 12 +- crates/image-codecs/src/lib.rs | 11 + crates/image-codecs/src/options.rs | 6 +- crates/raster-codecs/Cargo.toml | 4 - crates/raster-codecs/README.md | 16 +- crates/raster-codecs/src/bridge.rs | 79 ++ crates/raster-codecs/src/dds.rs | 621 +------------ crates/raster-codecs/src/dds_bc.rs | 369 -------- crates/raster-codecs/src/dicom.rs | 530 +---------- crates/raster-codecs/src/fits.rs | 860 +----------------- crates/raster-codecs/src/iff.rs | 544 +---------- crates/raster-codecs/src/lib.rs | 53 +- crates/raster-codecs/src/pcx.rs | 149 +-- crates/raster-codecs/src/pnm.rs | 363 +------- crates/raster-codecs/src/xpm.rs | 244 +---- crates/raster-codecs/src/xpm/x11_colors.rs | 666 -------------- crates/raster-codecs/tests/shared_contract.rs | 46 + 19 files changed, 296 insertions(+), 4313 deletions(-) create mode 100644 crates/raster-codecs/src/bridge.rs delete mode 100644 crates/raster-codecs/src/dds_bc.rs delete mode 100644 crates/raster-codecs/src/xpm/x11_colors.rs create mode 100644 crates/raster-codecs/tests/shared_contract.rs diff --git a/Cargo.lock b/Cargo.lock index fb1a3f862..9b4662232 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2430,7 +2430,6 @@ dependencies = [ "base64 0.23.1", "craft-image-codecs", "image", - "rayon", "thiserror 2.0.21", ] diff --git a/crates/image-codecs/src/codecs/pcx.rs b/crates/image-codecs/src/codecs/pcx.rs index 48dbf1169..bf1b2eee6 100644 --- a/crates/image-codecs/src/codecs/pcx.rs +++ b/crates/image-codecs/src/codecs/pcx.rs @@ -1,5 +1,5 @@ //! ZSoft PCX (Paintbrush) reader: strictly bounded scanline RLE, 16/256-colour -//! palettes and 8-bit three-plane RGB. Read-only: modern saves should use PNG. +//! palettes, 8-bit RGB and four-plane RGBA. Read-only: modern saves should use PNG. use crate::SampleType; use crate::error::CodecError; @@ -19,7 +19,7 @@ fn le16(b: &[u8], i: usize) -> u16 { /// PCX lacks a strong magic number. Require plausible complete header fields before /// identifying it, rather than classifying every file starting with 0x0a as PCX. pub(crate) fn looks_like_pcx(b: &[u8]) -> bool { - if b.len() < HEADER || b[0] != 0x0a || !matches!(b[1], 0 | 2 | 3 | 5) || b[2] != 1 { + if b.len() < HEADER || b[0] != 0x0a || !matches!(b[1], 0 | 2..=5) || b[2] != 1 { return false; } let bits = b[3]; @@ -31,7 +31,12 @@ pub(crate) fn looks_like_pcx(b: &[u8]) -> bool { } let width = u32::from(x1 - x0) + 1; let min_stride = (width * u32::from(bits)).div_ceil(8); - stride >= min_stride && stride > 0 && (bits != 8 || matches!(planes, 1 | 3)) && (bits == 8 || u32::from(bits) * u32::from(planes) <= 4) + stride >= min_stride && stride > 0 && (bits != 8 || matches!(planes, 1 | 3 | 4)) && (bits == 8 || u32::from(bits) * u32::from(planes) <= 4) +} + +/// The dimensions declared by a plausible PCX header, without decoding its pixels. +pub(crate) fn dimensions(b: &[u8]) -> Option<(u32, u32)> { + looks_like_pcx(b).then(|| (u32::from(le16(b, 8) - le16(b, 4)) + 1, u32::from(le16(b, 10) - le16(b, 6)) + 1)) } /// Read one compressed scanline, including all planes. RLE packets never cross a scanline. @@ -61,11 +66,12 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { if !looks_like_pcx(b) { return Err(bad("invalid PCX header or unsupported pixel layout")); } - let (w, h) = (u32::from(le16(b, 8) - le16(b, 4)) + 1, u32::from(le16(b, 10) - le16(b, 6)) + 1); - limits.check(w, h, ChannelLayout::Rgb, SampleType::U8)?; + let (w, h) = dimensions(b).ok_or_else(|| bad("invalid PCX header or unsupported pixel layout"))?; + let layout = if b[3] == 8 && b[65] == 4 { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; + limits.check(w, h, layout, SampleType::U8)?; let (bits, planes, stride) = (usize::from(b[3]), usize::from(b[65]), usize::from(le16(b, 66))); let row_len = stride.checked_mul(planes).ok_or_else(|| bad("row size overflow"))?; - let total = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(3)).ok_or_else(|| bad("pixel size overflow"))?; + let total = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(layout.channels())).ok_or_else(|| bad("pixel size overflow"))?; // The last 769 bytes are the optional VGA palette for 8-bit indexed images. let vga = bits == 8 && planes == 1 && b.len() >= HEADER + 769 && b[b.len() - 769] == 0x0c; if bits == 8 && planes == 1 && !vga && le16(b, 68) != 2 { @@ -102,8 +108,10 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { for y in 0..h as usize { read_row(&b[..image_end], &mut p, &mut row)?; for x in 0..width { - let rgb = if bits == 8 && planes == 3 { - [row[x], row[stride + x], row[2 * stride + x]] + let rgba = if bits == 8 && planes == 4 { + [row[x], row[stride + x], row[2 * stride + x], row[3 * stride + x]] + } else if bits == 8 && planes == 3 { + [row[x], row[stride + x], row[2 * stride + x], 255] } else { let index = if bits == 8 { usize::from(row[x]) @@ -118,19 +126,20 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { }; if vga { let o = image_end + 1 + index * 3; - [b[o], b[o + 1], b[o + 2]] + [b[o], b[o + 1], b[o + 2], 255] } else if bits == 8 { let g = index as u8; - [g, g, g] // palette-info 2: gray scale + [g, g, g, 255] // palette-info 2: gray scale } else { let o = 16 + index * 3; - [b[o], b[o + 1], b[o + 2]] + [b[o], b[o + 1], b[o + 2], 255] } }; - pixels[(y * width + x) * 3..(y * width + x + 1) * 3].copy_from_slice(&rgb); + let start = (y * width + x) * layout.channels(); + pixels[start..start + layout.channels()].copy_from_slice(&rgba[..layout.channels()]); } } - let mut image = Image::from_u8(w, h, ChannelLayout::Rgb, pixels)?; + let mut image = Image::from_u8(w, h, layout, pixels)?; let (xdpi, ydpi) = (le16(b, 12), le16(b, 14)); if xdpi > 0 && ydpi > 0 { image.meta.dpi = Some((f32::from(xdpi), f32::from(ydpi))); diff --git a/crates/image-codecs/src/codecs/xpm.rs b/crates/image-codecs/src/codecs/xpm.rs index c20f3c58e..c74b5f16b 100644 --- a/crates/image-codecs/src/codecs/xpm.rs +++ b/crates/image-codecs/src/codecs/xpm.rs @@ -96,7 +96,7 @@ fn channel(hex: &str) -> Option { Some(((v * 255 + max / 2) / max) as u8) } -fn named(name: &str) -> Option<[u8; 3]> { +pub(crate) fn named(name: &str) -> Option<[u8; 3]> { let s = name.to_ascii_lowercase().replace([' ', '-', '_'], ""); // XPM imports X11 names, whose RGB values differ from CSS for names // such as gray, maroon and purple. Keep these exact rather than guessing. @@ -114,6 +114,16 @@ fn named(name: &str) -> Option<[u8; 3]> { }) } +/// Exact, normalized X11 lookup for consumers that need table membership rather +/// than XPM's additional aliases and normalization rules. +pub(crate) fn exact_x11_name(name: &str) -> Option<[u8; 3]> { + if name.is_empty() || name.len() > 64 || !name.bytes().all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit()) { + return None; + } + let index = x11_colors::X11_COLORS.binary_search_by_key(&name, |(entry, _)| *entry).ok()?; + Some(x11_colors::X11_COLORS.get(index)?.1) +} + fn color(name: &str) -> Option<[u8; 4]> { if name.eq_ignore_ascii_case("none") { return Some([0, 0, 0, 0]); diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 61109faa1..21130e392 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -237,6 +237,17 @@ pub fn jpeg_info(bytes: &[u8]) -> Option { jpeg::info(bytes) } +/// PCX's dimensions are available from its fixed-size header without decoding the RLE rows. +pub fn pcx_dimensions(bytes: &[u8]) -> Option<(u32, u32)> { + codecs::pcx::dimensions(bytes) +} + +/// The X11 color-name lookup used by XPM files, expressed as normalized sRGB. +pub fn xpm_named_color_rgb(name: &str) -> Option<[f32; 3]> { + let rgb = codecs::xpm::exact_x11_name(name)?; + Some(rgb.map(|channel| f32::from(channel) / 255.0)) +} + /// The colour table of a GIF's first frame (its Local Color Table, else the Global one) and the /// entry its Graphic Control Extension makes transparent, read without decoding pixels. pub fn gif_palette(bytes: &[u8]) -> Option<(Vec<[u8; 3]>, Option)> { diff --git a/crates/image-codecs/src/options.rs b/crates/image-codecs/src/options.rs index ed96b8733..71f6030b3 100644 --- a/crates/image-codecs/src/options.rs +++ b/crates/image-codecs/src/options.rs @@ -32,6 +32,10 @@ impl Default for Limits { } impl Limits { + /// Construct explicit bounds for a caller-owned format adapter. + pub const fn new(max_width: u32, max_height: u32, max_pixels: u64, max_alloc: u64) -> Self { + Limits { max_width, max_height, max_pixels, max_alloc } + } /// For a decoder that writes a band of rows at a time into the document's tiles instead of /// one buffer (TIFF, #8169): the largest document size, 300,000 px a side, on 64-bit /// targets, with `max_alloc` bounding each group of bands. 32-bit targets keep the @@ -85,7 +89,7 @@ impl Limits { /// never beats `max_ratio`:1. Called before the output buffer is allocated, so a /// header-only hostile file cannot make the decoder reserve gigabytes. A fixed 1 MiB of /// slack keeps tiny images and headers with large metadata unaffected. - pub(crate) fn check_plausible(declared_bytes: u64, file_len: usize, max_ratio: u64) -> Result<(), CodecError> { + pub fn check_plausible(declared_bytes: u64, file_len: usize, max_ratio: u64) -> Result<(), CodecError> { let capacity = (file_len as u64).saturating_mul(max_ratio).saturating_add(1 << 20); if declared_bytes > capacity { return Err(CodecError::LimitExceeded(format!("a {file_len} byte file cannot hold {declared_bytes} bytes of image data"))); diff --git a/crates/raster-codecs/Cargo.toml b/crates/raster-codecs/Cargo.toml index 002012b80..50bb61de8 100644 --- a/crates/raster-codecs/Cargo.toml +++ b/crates/raster-codecs/Cargo.toml @@ -15,7 +15,3 @@ base64 = "0.23" craft-image-codecs = { workspace = true } thiserror = { workspace = true } image = { version = "0.25", default-features = false } - -# The DDS encoder compresses rows of blocks in parallel on native; wasm stays single-threaded. -[target.'cfg(not(target_arch = "wasm32"))'.dependencies] -rayon = { workspace = true } diff --git a/crates/raster-codecs/README.md b/crates/raster-codecs/README.md index 6735769ae..907e73763 100644 --- a/crates/raster-codecs/README.md +++ b/crates/raster-codecs/README.md @@ -1,12 +1,9 @@ # craft-raster-codecs -Small raster image formats for the Craft apps, from PhotoCraft's `photocraft-codecs`: one module -per format, one implementation of each. Decoders take untrusted bytes and `Limits`, check the -declared size before allocating, and never panic. No `unsafe`; it builds for -`wasm32-unknown-unknown`. The `common` RGBA image facade delegates PNG/JPEG/BMP/GIF/TIFF/WebP -decoding and PNG/JPEG encoding to [`craft-image-codecs`](../image-codecs/README.md), the shared -depth- and metadata-preserving codec implementation. Other dependencies are `thiserror`, and -`rayon` on native targets (the DDS encoder compresses rows of blocks in parallel). +Compatibility facade for the small raster-format modules in older Craft app versions. The +format readers and writers are implemented once in [`craft-image-codecs`](../image-codecs/README.md); +these modules forward calls and adapt the older `Image` and `Limits` types. The `common` RGBA +image facade also delegates to that crate. ```toml craft-raster-codecs = { git = "https://github.com/storytold/craft-libs", rev = "" } @@ -37,9 +34,8 @@ assert_eq!(image.data(), &[0, 255]); let bytes = pnm::encode(&image)?; ``` -Each module has a `looks_like_*` (or `is_dicom`) sniffer for the format's signature. `dds` and -`fits` also have `has_additional_images`, which says without decoding whether a file holds -more than the one image `decode` returns (cube faces, array slices, further HDUs). +The module sniffers and the `dds`/`fits` additional-image queries forward to shared detection and +metadata inspection in `craft-image-codecs`. `xpm::x11_color_rgb` also looks up exact normalized X11 names as RGB channels in `0..=1`. It accepts only 1–64 lowercase ASCII letters or digits, without normalization, XPM-only aliases, diff --git a/crates/raster-codecs/src/bridge.rs b/crates/raster-codecs/src/bridge.rs new file mode 100644 index 000000000..a1bd061db --- /dev/null +++ b/crates/raster-codecs/src/bridge.rs @@ -0,0 +1,79 @@ +//! Compatibility between this crate's small-format facade and the canonical image codec model. + +use crate::{ChannelLayout, CodecError, DecodeWarning, Image, Limits, SampleType}; +use craft_image_codecs::{DecodeOptions, Format, Metadata}; + +pub(crate) fn format(format: Format, bytes: &[u8], limits: &Limits) -> Result { + let shared_limits = craft_image_codecs::Limits::new(limits.max_width, limits.max_height, limits.max_pixels, limits.max_alloc); + let options = DecodeOptions { limits: shared_limits, ..DecodeOptions::default() }; + let image = craft_image_codecs::decode_as_with(format, bytes, &options).map_err(from_shared_error)?; + from_shared_image(image) +} + +pub(crate) fn shared_image(image: &Image) -> Result { + let layout = match image.layout() { + ChannelLayout::Gray => craft_image_codecs::ChannelLayout::Gray, + ChannelLayout::GrayA => craft_image_codecs::ChannelLayout::GrayA, + ChannelLayout::Rgb => craft_image_codecs::ChannelLayout::Rgb, + ChannelLayout::Rgba => craft_image_codecs::ChannelLayout::Rgba, + ChannelLayout::Cmyk => craft_image_codecs::ChannelLayout::Cmyk, + ChannelLayout::CmykA => craft_image_codecs::ChannelLayout::CmykA, + }; + let sample = match image.sample_type() { + SampleType::U8 => craft_image_codecs::SampleType::U8, + SampleType::U16 => craft_image_codecs::SampleType::U16, + SampleType::F32 => craft_image_codecs::SampleType::F32, + }; + let meta = Metadata { dpi: image.dpi, text: image.text.clone(), ..Metadata::default() }; + craft_image_codecs::Image::from_raw(image.width(), image.height(), layout, sample, image.data().to_vec()) + .map(|image| image.with_meta(meta)) + .map_err(from_shared_error) +} + +fn from_shared_image(image: craft_image_codecs::Image) -> Result { + let layout = match image.layout() { + craft_image_codecs::ChannelLayout::Gray => ChannelLayout::Gray, + craft_image_codecs::ChannelLayout::GrayA => ChannelLayout::GrayA, + craft_image_codecs::ChannelLayout::Rgb => ChannelLayout::Rgb, + craft_image_codecs::ChannelLayout::Rgba => ChannelLayout::Rgba, + craft_image_codecs::ChannelLayout::Cmyk => ChannelLayout::Cmyk, + craft_image_codecs::ChannelLayout::CmykA => ChannelLayout::CmykA, + _ => return Err(CodecError::InvalidImage(format!("unsupported small-codec layout {:?}", image.layout()))), + }; + let sample = match image.sample_type() { + craft_image_codecs::SampleType::U8 => SampleType::U8, + craft_image_codecs::SampleType::U16 => SampleType::U16, + craft_image_codecs::SampleType::F32 => SampleType::F32, + _ => return Err(CodecError::InvalidImage(format!("unsupported small-codec sample type {:?}", image.sample_type()))), + }; + let mut out = Image::from_raw(image.width(), image.height(), layout, sample, image.data().to_vec())?; + out.dpi = image.meta.dpi; + out.text = image.meta.text; + out.warnings = image + .warnings + .into_iter() + .filter_map(|warning| match warning { + craft_image_codecs::DecodeWarning::MorePages { total } | craft_image_codecs::DecodeWarning::MoreParts { total } => { + Some(DecodeWarning::MorePages { total }) + } + _ => None, + }) + .collect(); + Ok(out) +} + +pub(crate) fn from_shared_error(error: craft_image_codecs::CodecError) -> CodecError { + match error { + craft_image_codecs::CodecError::UnknownFormat => CodecError::unsupported("image", "unrecognized image format"), + craft_image_codecs::CodecError::Unsupported { format, reason } => CodecError::unsupported(format.name(), reason), + craft_image_codecs::CodecError::Malformed { format, message } => CodecError::malformed(format.name(), message), + craft_image_codecs::CodecError::LimitExceeded(message) => CodecError::LimitExceeded(message), + craft_image_codecs::CodecError::Encode { format, message } => CodecError::Encode { format: format.name(), message }, + craft_image_codecs::CodecError::InvalidImage(message) => CodecError::InvalidImage(message), + } +} + +pub(crate) fn encode(image: &Image, format: Format, options: &craft_image_codecs::EncodeOptions) -> Result, CodecError> { + let image = shared_image(image)?; + craft_image_codecs::encode(&image, format, options).map_err(from_shared_error) +} diff --git a/crates/raster-codecs/src/dds.rs b/crates/raster-codecs/src/dds.rs index 1249cb5b7..eb703a074 100644 --- a/crates/raster-codecs/src/dds.rs +++ b/crates/raster-codecs/src/dds.rs @@ -1,616 +1,75 @@ -//! DirectDraw Surface (DDS) textures, after Microsoft's public DDS documentation ("DDS", -//! "DDS_HEADER", "DDS_PIXELFORMAT", "DDS_HEADER_DXT10"). -//! -//! Reading: the top mip level of a 2D texture, either uncompressed (8 to 32 bits per pixel -//! described by channel masks, RGB or luminance) or block-compressed BC1/BC2/BC3 -//! (DXT1/DXT3/DXT5), with or without the DX10 header. Cubemaps, texture arrays and volume -//! textures are refused rather than flattened to one face or slice. -//! -//! Writing: a 2D texture, uncompressed (BGRA or BGRX, luminance with or without alpha) or -//! BC1/BC2/BC3, optionally with its full mip chain. The legacy header describes all of these, -//! so no DX10 header is written; formats only DXGI can name (BC4–BC7, sRGB) aren't written. +//! DDS compatibility facade; implementation and BC block codecs live in craft-image-codecs. +use crate::{CodecError, Image, Limits, SampleType, bridge}; +use craft_image_codecs::Format; -use std::borrow::Cow; - -use crate::dds_bc::{self, Block, Mask}; -use crate::{ChannelLayout, CodecError, Image, Limits, SampleType}; - -/// `DDS ` plus the 124-byte DDS_HEADER. -const HEADER: usize = 128; -/// DDS_HEADER_DXT10, present when the FourCC is `DX10`. -const DX10_HEADER: usize = 20; - -const DDSD_DEPTH: u32 = 0x80_0000; -const DDPF_ALPHAPIXELS: u32 = 0x1; -const DDPF_FOURCC: u32 = 0x4; -const DDPF_RGB: u32 = 0x40; -const DDPF_LUMINANCE: u32 = 0x2_0000; -const DDSCAPS2_CUBEMAP: u32 = 0x200; -const DDSCAPS2_CUBEMAP_FACES: u32 = 0xfc00; -const DDSCAPS2_VOLUME: u32 = 0x20_0000; -const DIMENSION_TEXTURE2D: u32 = 3; -const MISC_TEXTURECUBE: u32 = 0x4; - -fn bad(reason: impl std::fmt::Display) -> CodecError { - CodecError::malformed("DDS", reason) +pub fn looks_like_dds(bytes: &[u8]) -> bool { + craft_image_codecs::detect(bytes) == Some(Format::Dds) } -fn unsupported(reason: impl Into) -> CodecError { - CodecError::unsupported("DDS", reason) -} -fn le32(b: &[u8], at: usize) -> Result { - let bytes = b.get(at..at + 4).and_then(|s| <[u8; 4]>::try_from(s).ok()).ok_or_else(|| bad("truncated header"))?; - Ok(u32::from_le_bytes(bytes)) -} - -/// The `DDS ` magic. -pub fn looks_like_dds(b: &[u8]) -> bool { - b.starts_with(b"DDS ") -} - -/// Header-declared extra subresources cannot survive a single-image rewrite, even without presence flags. pub fn has_additional_images(bytes: &[u8]) -> Result { - let depth = le32(bytes, 24)?; - let mips = le32(bytes, 28)?; - let faces = (le32(bytes, 112)? & DDSCAPS2_CUBEMAP_FACES).count_ones(); - let dx10 = le32(bytes, 80)? & DDPF_FOURCC != 0 && bytes.get(84..88) == Some(b"DX10"); - let (cube, array) = if dx10 { (le32(bytes, 136)? & MISC_TEXTURECUBE != 0, le32(bytes, 140)?) } else { (false, 0) }; - Ok(depth > 1 || mips > 1 || faces > 1 || cube || array > 1) + craft_image_codecs::has_unretained_source_data(bytes).map_err(bridge::from_shared_error) +} +pub fn decode(bytes: &[u8], limits: &Limits) -> Result { + bridge::format(Format::Dds, bytes, limits) } -/// A FourCC as text when it is printable, else as the number legacy writers store there. -fn fourcc_name(fourcc: u32) -> String { - let bytes = fourcc.to_le_bytes(); - if bytes.iter().all(|c| c.is_ascii_graphic() || *c == b' ') { bytes.iter().map(|&c| char::from(c)).collect() } else { fourcc.to_string() } -} - -/// One channel of an uncompressed pixel: where it sits and its largest value. -#[derive(Clone, Copy)] -struct Field { - shift: u32, - max: u32, -} - -impl Field { - /// `None` for an empty mask; an error for one with gaps or wider than the pixel. - fn new(mask: u32, bits: u32) -> Result, CodecError> { - if mask == 0 { - return Ok(None); - } - let shift = mask.trailing_zeros(); - let max = mask >> shift; - if max & max.wrapping_add(1) != 0 || (bits < 32 && mask >> bits != 0) { - return Err(bad(format!("channel mask {mask:#x} is not a contiguous field of a {bits}-bit pixel"))); - } - Ok(Some(Field { shift, max })) - } - - fn get(self, pixel: u32) -> u8 { - let v = u64::from((pixel >> self.shift) & self.max); - let max = u64::from(self.max); - ((v * 255 + max / 2) / max) as u8 - } -} - -enum Encoding { - /// Uncompressed: bytes per pixel, then red (or luminance), green, blue and alpha. - Masks { - bytes: usize, - gray: bool, - fields: [Option; 4], - }, - Bc1, - Bc2, - Bc3, -} - -impl Encoding { - fn masks(bits: u32, masks: [u32; 4], gray: bool) -> Result { - if !matches!(bits, 8 | 16 | 24 | 32) { - return Err(bad(format!("{bits} bits per pixel is not 8, 16, 24 or 32"))); - } - let mut fields = [None; 4]; - for (field, mask) in fields.iter_mut().zip(masks) { - *field = Field::new(mask, bits)?; - } - let colour = if gray { fields[0].is_some() } else { fields[..3].iter().any(Option::is_some) }; - if !colour { - return Err(bad("the pixel format has no colour channel masks")); - } - Ok(Encoding::Masks { bytes: bits as usize / 8, gray, fields }) - } - - fn layout(&self) -> ChannelLayout { - match self { - Encoding::Masks { gray: true, fields, .. } if fields[3].is_some() => ChannelLayout::GrayA, - Encoding::Masks { gray: true, .. } => ChannelLayout::Gray, - Encoding::Masks { fields, .. } if fields[3].is_none() => ChannelLayout::Rgb, - _ => ChannelLayout::Rgba, - } - } - - /// Stored row stride and byte size of the top mip level (block rows for BC). - fn level_layout(&self, width: u32, height: u32, pitch: Option) -> Result<(usize, usize), CodecError> { - let overflow = || bad("pixel data size overflow"); - let (w, h) = (usize::try_from(width).map_err(|_| overflow())?, usize::try_from(height).map_err(|_| overflow())?); - let (row_bytes, rows) = match self { - Encoding::Masks { bytes, .. } => { - let packed = w.checked_mul(*bytes).ok_or_else(overflow)?; - let row_bytes = match pitch { - Some(pitch) => usize::try_from(pitch).map_err(|_| overflow())?, - None => packed, - }; - if row_bytes < packed { - return Err(bad(format!("row pitch {row_bytes} is smaller than the {packed}-byte pixel row"))); - } - (row_bytes, h) - } - Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 => { - let block = if matches!(self, Encoding::Bc1) { 8 } else { 16 }; - (w.div_ceil(4).checked_mul(block).ok_or_else(overflow)?, h.div_ceil(4)) - } - }; - let size = row_bytes.checked_mul(rows).ok_or_else(overflow)?; - Ok((row_bytes, size)) - } -} - -/// The legacy pixel format: a FourCC, or channel masks. -fn legacy_encoding(b: &[u8]) -> Result { - let (flags, fourcc, bits) = (le32(b, 80)?, le32(b, 84)?, le32(b, 88)?); - let alpha = if flags & DDPF_ALPHAPIXELS != 0 { le32(b, 104)? } else { 0 }; - if flags & DDPF_FOURCC != 0 { - return match &fourcc.to_le_bytes() { - b"DXT1" => Ok(Encoding::Bc1), - b"DXT3" => Ok(Encoding::Bc2), - b"DXT5" => Ok(Encoding::Bc3), - b"DXT2" | b"DXT4" => Err(unsupported("premultiplied-alpha DXT2/DXT4 textures are not supported")), - _ => Err(unsupported(format!("compression `{}` is not supported (DXT1, DXT3 and DXT5 are)", fourcc_name(fourcc)))), - }; - } - if flags & DDPF_RGB != 0 { - return Encoding::masks(bits, [le32(b, 92)?, le32(b, 96)?, le32(b, 100)?, alpha], false); - } - if flags & DDPF_LUMINANCE != 0 { - return Encoding::masks(bits, [le32(b, 92)?, 0, 0, alpha], true); - } - Err(unsupported(format!("pixel format flags {flags:#x} are not RGB, luminance or a supported compression"))) -} - -/// The DX10 header's DXGI format, refusing anything but a single 2D texture. -fn dx10_encoding(b: &[u8]) -> Result { - let (dxgi, dimension, misc, array) = (le32(b, HEADER)?, le32(b, HEADER + 4)?, le32(b, HEADER + 8)?, le32(b, HEADER + 12)?); - if misc & MISC_TEXTURECUBE != 0 { - return Err(unsupported("cubemaps are not supported")); - } - if dimension != DIMENSION_TEXTURE2D { - return Err(unsupported(format!("only 2D textures are supported (resource dimension {dimension})"))); - } - if array > 1 { - return Err(unsupported(format!("texture arrays are not supported ({array} textures)"))); - } - const RGBA: [u32; 4] = [0xff, 0xff00, 0xff_0000, 0xff00_0000]; - const BGRA: [u32; 4] = [0xff_0000, 0xff00, 0xff, 0xff00_0000]; - const BGRX: [u32; 4] = [0xff_0000, 0xff00, 0xff, 0]; - match dxgi { - 27..=29 => Encoding::masks(32, RGBA, false), - 87 | 90 | 91 => Encoding::masks(32, BGRA, false), - 88 | 92 | 93 => Encoding::masks(32, BGRX, false), - 70..=72 => Ok(Encoding::Bc1), - 73..=75 => Ok(Encoding::Bc2), - 76..=78 => Ok(Encoding::Bc3), - _ => Err(unsupported(format!("DXGI format {dxgi} is not supported (BC1, BC2, BC3 and 8-bit RGBA or BGRA are)"))), - } -} - -pub fn decode(b: &[u8], limits: &Limits) -> Result { - if !b.starts_with(b"DDS ") { - return Err(bad("missing the `DDS ` signature")); - } - if le32(b, 4)? != 124 { - return Err(bad("header size is not 124")); - } - let (flags, height, width, depth, caps2) = (le32(b, 8)?, le32(b, 12)?, le32(b, 16)?, le32(b, 24)?, le32(b, 112)?); - if caps2 & DDSCAPS2_CUBEMAP != 0 { - return Err(unsupported("cubemaps are not supported")); - } - if caps2 & DDSCAPS2_VOLUME != 0 || (flags & DDSD_DEPTH != 0 && depth > 1) { - return Err(unsupported("volume textures are not supported")); - } - let dx10 = le32(b, 80)? & DDPF_FOURCC != 0 && &le32(b, 84)?.to_le_bytes() == b"DX10"; - let (encoding, start) = if dx10 { (dx10_encoding(b)?, HEADER + DX10_HEADER) } else { (legacy_encoding(b)?, HEADER) }; - let layout = encoding.layout(); - limits.check(width, height, layout, SampleType::U8)?; - // The same header field is a total level size for BC, but a row stride when DDSD_PITCH is set. - let pitch = if flags & DDSD_PITCH != 0 { Some(le32(b, 20)?) } else { None }; - let (row_pitch, size) = encoding.level_layout(width, height, pitch)?; - let data = b.get(start..).and_then(|d| d.get(..size)).ok_or_else(|| bad(format!("truncated pixel data: {size} bytes needed")))?; - let (w, h) = (width as usize, height as usize); - let channels = layout.channels(); - let row_size = w.checked_mul(channels).ok_or_else(|| bad("pixel size overflow"))?; - let total = row_size.checked_mul(h).ok_or_else(|| bad("pixel size overflow"))?; - let mut pixels = vec![0u8; total]; - match encoding { - Encoding::Masks { bytes, gray, fields } => { - for (src_row, dst_row) in data.chunks_exact(row_pitch).zip(pixels.chunks_exact_mut(row_size)) { - // Stop at the output width, leaving any stored row padding unread. - for (src, dst) in src_row.chunks_exact(bytes).zip(dst_row.chunks_exact_mut(channels)) { - let pixel = src.iter().rev().fold(0u32, |v, &byte| v << 8 | u32::from(byte)); - let channel = |i: usize| fields[i].map_or(0, |f| f.get(pixel)); - let values = if gray { [channel(0), channel(3), 0, 0] } else { [channel(0), channel(1), channel(2), channel(3)] }; - dst.copy_from_slice(&values[..channels]); - } - } - } - Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 => { - let block_size = if matches!(encoding, Encoding::Bc1) { 8 } else { 16 }; - let blocks_x = w.div_ceil(4); - for (i, block) in data.chunks_exact(block_size).enumerate() { - let texels = match encoding { - Encoding::Bc1 => bc1(block, true), - Encoding::Bc2 => with_alpha(bc1(block.get(8..).unwrap_or_default(), false), bc2_alpha(block)), - _ => with_alpha(bc1(block.get(8..).unwrap_or_default(), false), bc3_alpha(block)), - }; - let (bx, by) = ((i % blocks_x) * 4, (i / blocks_x) * 4); - for (t, texel) in texels.iter().enumerate() { - let (x, y) = (bx + t % 4, by + t / 4); - if x < w && y < h { - let at = (y * w + x) * 4; - if let Some(dst) = pixels.get_mut(at..at + 4) { - dst.copy_from_slice(texel); - } - } - } - } - } - } - Image::from_u8(width, height, layout, pixels) -} - -/// A BC1 colour block. BC2 and BC3 colour blocks always have four colours; only BC1's -/// `c0 <= c1` order selects three colours and transparent black. -fn bc1(block: &[u8], one_bit_alpha: bool) -> [[u8; 4]; 16] { - let byte = |i: usize| block.get(i).copied().unwrap_or(0); - let (c0, c1) = (u16::from_le_bytes([byte(0), byte(1)]), u16::from_le_bytes([byte(2), byte(3)])); - let palette = dds_bc::colour_palette(c0, c1, c0 > c1 || !one_bit_alpha); - let indices = u32::from_le_bytes([byte(4), byte(5), byte(6), byte(7)]); - std::array::from_fn(|t| palette[(indices >> (2 * t)) as usize & 3]) -} - -/// BC2: four explicit bits of alpha per texel. -fn bc2_alpha(block: &[u8]) -> [u8; 16] { - let bits = u64::from_le_bytes(std::array::from_fn(|i| block.get(i).copied().unwrap_or(0))); - std::array::from_fn(|t| ((bits >> (4 * t)) & 0xf) as u8 * 17) -} - -/// BC3: two alpha endpoints and a 3-bit index per texel into eight values, or into six plus -/// 0 and 255 when `a0 <= a1`. -fn bc3_alpha(block: &[u8]) -> [u8; 16] { - let byte = |i: usize| block.get(i).copied().unwrap_or(0); - let palette = dds_bc::alpha_palette(byte(0), byte(1)); - let bits = u64::from_le_bytes([byte(2), byte(3), byte(4), byte(5), byte(6), byte(7), 0, 0]); - std::array::from_fn(|t| palette[((bits >> (3 * t)) & 7) as usize]) -} - -fn with_alpha(mut texels: [[u8; 4]; 16], alpha: [u8; 16]) -> [[u8; 4]; 16] { - for (texel, a) in texels.iter_mut().zip(alpha) { - texel[3] = a; - } - texels -} - -const DDSD_CAPS: u32 = 0x1; -const DDSD_HEIGHT: u32 = 0x2; -const DDSD_WIDTH: u32 = 0x4; -const DDSD_PITCH: u32 = 0x8; -const DDSD_PIXELFORMAT: u32 = 0x1000; -const DDSD_MIPMAPCOUNT: u32 = 0x2_0000; -const DDSD_LINEARSIZE: u32 = 0x8_0000; -const DDSCAPS_COMPLEX: u32 = 0x8; -const DDSCAPS_TEXTURE: u32 = 0x1000; -const DDSCAPS_MIPMAP: u32 = 0x40_0000; - -fn encode_err(reason: impl std::fmt::Display) -> CodecError { - CodecError::encode("DDS", reason) -} - -/// How a written texture stores its pixels. -#[derive(Clone, Copy, PartialEq, Eq)] -enum Store { - /// 8-bit luminance. - Luminance, - /// 8-bit luminance, then 8-bit alpha. - LuminanceAlpha, - /// Blue, green, red and an unused byte (written as 255, so a reader that takes it for - /// alpha still sees an opaque texture). - Bgrx, - Bgra, - Bc1, - Bc2, - Bc3, -} - -impl Store { - /// The storage for an image of `layout`. - fn new(compression: DdsCompression, layout: ChannelLayout) -> Result { - Ok(match (compression, layout) { - (DdsCompression::None, ChannelLayout::Gray) => Store::Luminance, - (DdsCompression::None, ChannelLayout::GrayA) => Store::LuminanceAlpha, - (DdsCompression::None, ChannelLayout::Rgb) => Store::Bgrx, - (DdsCompression::None, ChannelLayout::Rgba) => Store::Bgra, - (DdsCompression::Bc1, ChannelLayout::Rgba) => Store::Bc1, - (DdsCompression::Bc2, ChannelLayout::Rgba) => Store::Bc2, - (DdsCompression::Bc3, ChannelLayout::Rgba) => Store::Bc3, - (_, layout) => return Err(encode_err(format!("{layout:?} pixels can't be written as `{}`", compression.name()))), - }) - } - - /// Bytes per pixel, or per 4×4 block for a block compression. - fn unit(self) -> usize { - match self { - Store::Luminance => 1, - Store::LuminanceAlpha => 2, - Store::Bgrx | Store::Bgra => 4, - Store::Bc1 => 8, - Store::Bc2 | Store::Bc3 => 16, - } - } - - fn is_block(self) -> bool { - matches!(self, Store::Bc1 | Store::Bc2 | Store::Bc3) - } - - /// Bytes of one row of pixels (or of 4×4 blocks) of a level `width` wide. - fn row_bytes(self, width: usize) -> Option { - if self.is_block() { width.div_ceil(4).checked_mul(self.unit()) } else { width.checked_mul(self.unit()) } - } - - fn level_bytes(self, width: usize, height: usize) -> Option { - let rows = if self.is_block() { height.div_ceil(4) } else { height }; - self.row_bytes(width)?.checked_mul(rows) - } - - /// DDS_PIXELFORMAT: flags, FourCC, bits per pixel and the red, green, blue and alpha masks. - fn pixel_format(self) -> (u32, [u8; 4], u32, [u32; 4]) { - match self { - Store::Luminance => (DDPF_LUMINANCE, [0; 4], 8, [0xff, 0, 0, 0]), - Store::LuminanceAlpha => (DDPF_LUMINANCE | DDPF_ALPHAPIXELS, [0; 4], 16, [0xff, 0, 0, 0xff00]), - Store::Bgrx => (DDPF_RGB, [0; 4], 32, [0xff_0000, 0xff00, 0xff, 0]), - Store::Bgra => (DDPF_RGB | DDPF_ALPHAPIXELS, [0; 4], 32, [0xff_0000, 0xff00, 0xff, 0xff00_0000]), - Store::Bc1 => (DDPF_FOURCC, *b"DXT1", 0, [0; 4]), - Store::Bc2 => (DDPF_FOURCC, *b"DXT3", 0, [0; 4]), - Store::Bc3 => (DDPF_FOURCC, *b"DXT5", 0, [0; 4]), - } - } -} - -/// Levels in a full mip chain: the larger side halves down to 1. -fn mip_levels(width: u32, height: u32) -> u32 { - 32 - width.max(height).max(1).leading_zeros() -} - -fn half((w, h): (usize, usize)) -> (usize, usize) { - ((w / 2).max(1), (h / 2).max(1)) -} - -/// How a DDS texture stores its pixels. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub enum DdsCompression { - /// Uncompressed, 8 bits per channel: BGRA (BGRX without alpha), or luminance with or - /// without alpha for grayscale. Lossless. #[default] None, - /// BC1 (DXT1): 4 bits per pixel. Alpha is on or off: texels under half opacity become - /// transparent black. Bc1, - /// BC2 (DXT3): 8 bits per pixel, with 4 bits of explicit alpha per texel. Bc2, - /// BC3 (DXT5): 8 bits per pixel, with smooth interpolated alpha. Bc3, } - impl DdsCompression { - pub const ALL: [DdsCompression; 4] = [DdsCompression::None, DdsCompression::Bc1, DdsCompression::Bc2, DdsCompression::Bc3]; - - /// The name scripts and the CLI use: `none`, `bc1`, `bc2` or `bc3`. + pub const ALL: [Self; 4] = [Self::None, Self::Bc1, Self::Bc2, Self::Bc3]; pub fn name(self) -> &'static str { match self { - DdsCompression::None => "none", - DdsCompression::Bc1 => "bc1", - DdsCompression::Bc2 => "bc2", - DdsCompression::Bc3 => "bc3", + Self::None => "none", + Self::Bc1 => "bc1", + Self::Bc2 => "bc2", + Self::Bc3 => "bc3", } } - - /// [`Self::name`] back to a compression, also taking the DXT names (`dxt1`, `dxt3`, - /// `dxt5`), in any case. Anything else is `None`. pub fn from_name(name: &str) -> Option { match name.to_ascii_lowercase().as_str() { - "none" => Some(DdsCompression::None), - "bc1" | "dxt1" => Some(DdsCompression::Bc1), - "bc2" | "dxt3" => Some(DdsCompression::Bc2), - "bc3" | "dxt5" => Some(DdsCompression::Bc3), + "none" => Some(Self::None), + "bc1" | "dxt1" => Some(Self::Bc1), + "bc2" | "dxt3" => Some(Self::Bc2), + "bc3" | "dxt5" => Some(Self::Bc3), _ => None, } } - - /// A block compression (lossy), not uncompressed pixels. pub fn is_block(self) -> bool { - self != DdsCompression::None + self != Self::None } } - -/// What a DDS file is written with. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub struct DdsOptions { pub compression: DdsCompression, - /// Write the full mip chain down to 1×1, each level a box filter of the one above. pub mipmaps: bool, } - -/// Writes `img` (8 bits per channel) as a DDS texture. Uncompressed stores gray, gray with -/// alpha, RGB and RGBA; the block compressions take RGBA only. -pub fn encode(img: &Image, opts: DdsOptions) -> Result, CodecError> { - if img.sample_type() != SampleType::U8 { - return Err(encode_err("DDS is written at 8 bits per channel")); +pub fn encode(image: &Image, options: DdsOptions) -> Result, CodecError> { + if image.sample_type() != SampleType::U8 { + return Err(CodecError::encode("DDS", "DDS is written at 8 bits per channel")); } - let pixels = img.data(); - let store = Store::new(opts.compression, img.layout())?; - let (width, height) = (img.width(), img.height()); - let levels = if opts.mipmaps { mip_levels(width, height) } else { 1 }; - let top = (width as usize, height as usize); - // Every level's size, checked before anything is allocated. - let mut sizes = Vec::new(); - let mut size = top; - for _ in 0..levels { - sizes.push(store.level_bytes(size.0, size.1).ok_or_else(|| encode_err("texture size overflow"))?); - size = half(size); - } - let total = sizes.iter().try_fold(HEADER, |total, &s| total.checked_add(s)).ok_or_else(|| encode_err("texture size overflow"))?; - let header = header(width, height, store, levels, sizes.first().copied().unwrap_or(0))?; - let mut out = crate::alloc_zeroed::(total)?; - out.get_mut(..HEADER).ok_or_else(|| encode_err("output buffer too small"))?.copy_from_slice(&header); - - let channels = img.layout().channels(); - let mut level = Cow::Borrowed(pixels); - let (mut size, mut at) = (top, HEADER); - for (i, &bytes) in sizes.iter().enumerate() { - let dst = out.get_mut(at..at + bytes).ok_or_else(|| encode_err("output buffer too small"))?; - write_level(store, &level, size, dst)?; - at += bytes; - if i + 1 < sizes.len() { - let next = half(size); - level = Cow::Owned(downsample(&level, size, next, channels)?); - size = next; - } - } - Ok(out) -} - -/// The `DDS ` signature and DDS_HEADER. `top` is the top level's size in bytes. -fn header(width: u32, height: u32, store: Store, levels: u32, top: usize) -> Result<[u8; HEADER], CodecError> { - let mut b = [0u8; HEADER]; - let mut put = |at: usize, v: u32| { - if let Some(d) = b.get_mut(at..at + 4) { - d.copy_from_slice(&v.to_le_bytes()); - } + // BC storage encodes RGBA blocks. The generic image API can plan a + // conversion from other layouts, but this compatibility facade accepts + // only layouts its former writer stored directly. + let allowed = if options.compression.is_block() { + image.layout() == crate::ChannelLayout::Rgba + } else { + matches!(image.layout(), crate::ChannelLayout::Gray | crate::ChannelLayout::GrayA | crate::ChannelLayout::Rgb | crate::ChannelLayout::Rgba) }; - // Uncompressed: the pitch of one row. Compressed: the whole top level (its "linear size"). - let (size_flag, pitch_or_size) = if store.is_block() { (DDSD_LINEARSIZE, Some(top)) } else { (DDSD_PITCH, store.row_bytes(width as usize)) }; - let pitch_or_size = pitch_or_size - .and_then(|v| u32::try_from(v).ok()) - .ok_or_else(|| encode_err(format!("a {width}x{height} level is too large for a DDS header")))?; - let mipmapped = levels > 1; - put(4, 124); - put(8, DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | size_flag | if mipmapped { DDSD_MIPMAPCOUNT } else { 0 }); - put(12, height); - put(16, width); - put(20, pitch_or_size); - put(28, levels); - let (flags, fourcc, bits, masks) = store.pixel_format(); - put(76, 32); - put(80, flags); - put(84, u32::from_le_bytes(fourcc)); - put(88, bits); - for (i, mask) in masks.into_iter().enumerate() { - put(92 + 4 * i, mask); + if !allowed { + return Err(CodecError::encode("DDS", format!("{:?} pixels can't be written as `{}`", image.layout(), options.compression.name()))); } - put(108, DDSCAPS_TEXTURE | if mipmapped { DDSCAPS_COMPLEX | DDSCAPS_MIPMAP } else { 0 }); - b[..4].copy_from_slice(b"DDS "); - Ok(b) -} - -/// Runs `f` on each `len`-byte row of `out` with its index, in parallel on native targets. -fn rows(out: &mut [u8], len: usize, f: impl Fn(usize, &mut [u8]) + Send + Sync) { - #[cfg(not(target_arch = "wasm32"))] - { - use rayon::prelude::*; - out.par_chunks_mut(len.max(1)).enumerate().for_each(|(i, row)| f(i, row)); - } - #[cfg(target_arch = "wasm32")] - for (i, row) in out.chunks_mut(len.max(1)).enumerate() { - f(i, row); - } -} - -/// Writes one level of `width` × `height` pixels (in the image's layout) into `dst`. -fn write_level(store: Store, pixels: &[u8], (width, height): (usize, usize), dst: &mut [u8]) -> Result<(), CodecError> { - let row_bytes = store.row_bytes(width).ok_or_else(|| encode_err("texture size overflow"))?; - match store { - Store::Luminance | Store::LuminanceAlpha => { - // The image's gray (and alpha) samples, in the same order. - let src = pixels.get(..dst.len()).ok_or_else(|| encode_err("the level is smaller than its size"))?; - dst.copy_from_slice(src); - } - Store::Bgrx | Store::Bgra => { - let channels = if store == Store::Bgra { 4 } else { 3 }; - rows(dst, row_bytes, |y, row| { - let src = pixels.get(y * width * channels..).unwrap_or_default(); - for (d, s) in row.as_chunks_mut::<4>().0.iter_mut().zip(src.chunks_exact(channels)) { - let byte = |i: usize| s.get(i).copied().unwrap_or(255); - *d = [byte(2), byte(1), byte(0), byte(3)]; - } - }); - } - Store::Bc1 | Store::Bc2 | Store::Bc3 => { - let (texels, _) = pixels.as_chunks::<4>(); - rows(dst, row_bytes, |by, row| { - for (bx, d) in row.chunks_exact_mut(store.unit()).enumerate() { - let (block, inside) = gather(texels, (width, height), bx, by); - let encoded: &[u8] = match store { - Store::Bc1 => &dds_bc::encode_bc1(&block, inside), - Store::Bc2 => &dds_bc::encode_bc2(&block, inside), - _ => &dds_bc::encode_bc3(&block, inside), - }; - for (d, s) in d.iter_mut().zip(encoded) { - *d = *s; - } - } - }); - } - } - Ok(()) -} - -/// Block (`bx`, `by`)'s texels, and which of them lie inside the image. -fn gather(texels: &[[u8; 4]], (width, height): (usize, usize), bx: usize, by: usize) -> (Block, Mask) { - let mut block = [[0u8; 4]; 16]; - let mut inside: Mask = 0; - for (t, texel) in block.iter_mut().enumerate() { - let (x, y) = (bx * 4 + t % 4, by * 4 + t / 4); - if x < width - && y < height - && let Some(p) = texels.get(y * width + x) - { - *texel = *p; - inside |= 1 << t; - } - } - (block, inside) -} - -/// The next mip level: each pixel the average of the 2×2 pixels it covers, or of 2×3, 3×2 or -/// 3×3 along the last column and row of an odd-sized level, so no pixel is dropped. -fn downsample(src: &[u8], (w, h): (usize, usize), (nw, nh): (usize, usize), channels: usize) -> Result, CodecError> { - let len = nw.checked_mul(nh).and_then(|n| n.checked_mul(channels)).ok_or_else(|| encode_err("mip level size overflow"))?; - let mut dst = crate::alloc_zeroed::(len)?; - let span = |i: usize, n: usize, size: usize| 2 * i..=if i + 1 == n { size.saturating_sub(1) } else { 2 * i + 1 }; - rows(&mut dst, nw * channels, |y, row| { - let ys = span(y, nh, h); - for (x, px) in row.chunks_exact_mut(channels.max(1)).enumerate() { - let xs = span(x, nw, w); - for (c, v) in px.iter_mut().enumerate() { - let (mut sum, mut n) = (0u32, 0u32); - for sy in ys.clone() { - for sx in xs.clone() { - if let Some(&s) = src.get((sy * w + sx) * channels + c) { - sum += u32::from(s); - n += 1; - } - } - } - *v = ((sum + n / 2) / n.max(1)) as u8; - } - } - }); - Ok(dst) + let compression = match options.compression { + DdsCompression::None => craft_image_codecs::DdsCompression::None, + DdsCompression::Bc1 => craft_image_codecs::DdsCompression::Bc1, + DdsCompression::Bc2 => craft_image_codecs::DdsCompression::Bc2, + DdsCompression::Bc3 => craft_image_codecs::DdsCompression::Bc3, + }; + let opts = + craft_image_codecs::EncodeOptions { dds: craft_image_codecs::DdsOptions { compression, mipmaps: options.mipmaps }, ..Default::default() }; + bridge::encode(image, Format::Dds, &opts) } diff --git a/crates/raster-codecs/src/dds_bc.rs b/crates/raster-codecs/src/dds_bc.rs deleted file mode 100644 index df60e462c..000000000 --- a/crates/raster-codecs/src/dds_bc.rs +++ /dev/null @@ -1,369 +0,0 @@ -//! BC1, BC2 and BC3 (DXT1, DXT3, DXT5) blocks, after Microsoft's public block compression -//! documentation ("Block Compression (Direct3D 10)" and "Texture Block Compression in Direct3D -//! 11"): the palettes the DDS reader decodes with, and the block encoder the DDS writer uses. -//! -//! The encoder fits a block's colours with two RGB565 endpoints along their principal axis, -//! refines them by least squares, and scores every candidate against the exact palette the -//! reader rebuilds, so the indices it picks are the best ones for what a decoder shows. - -/// The 16 texels of a 4×4 block, row by row, as RGBA. -pub(crate) type Block = [[u8; 4]; 16]; - -/// Bit `t` set for each texel `t` of a block that counts: edge blocks of a texture whose size is -/// not a multiple of four have texels outside the image. -pub(crate) type Mask = u16; - -pub(crate) fn rgb565(c: u16) -> [u8; 3] { - let scale = |v: u16, max: u16| ((u32::from(v) * 255 + u32::from(max) / 2) / u32::from(max)) as u8; - [scale(c >> 11, 31), scale((c >> 5) & 63, 63), scale(c & 31, 31)] -} - -/// The colours a colour block's indices select. Four-colour mode (BC1 with `c0 > c1`, and every -/// BC2 and BC3 block) adds two colours at thirds; three-colour mode adds the midpoint and -/// transparent black. -pub(crate) fn colour_palette(c0: u16, c1: u16, four: bool) -> [[u8; 4]; 4] { - let (a, b) = (rgb565(c0), rgb565(c1)); - let mix = |wa: u32, wb: u32| { - let d = wa + wb; - let at = |i: usize| ((wa * u32::from(a[i]) + wb * u32::from(b[i]) + d / 2) / d) as u8; - [at(0), at(1), at(2), 255] - }; - if four { - [[a[0], a[1], a[2], 255], [b[0], b[1], b[2], 255], mix(2, 1), mix(1, 2)] - } else { - [[a[0], a[1], a[2], 255], [b[0], b[1], b[2], 255], mix(1, 1), [0; 4]] - } -} - -/// A BC3 alpha block's eight values: six interpolated at sevenths when `a0 > a1`, else four -/// interpolated at fifths followed by 0 and 255. -pub(crate) fn alpha_palette(a0: u8, a1: u8) -> [u8; 8] { - let (a0, a1) = (u32::from(a0), u32::from(a1)); - let mut palette = [a0, a1, 0, 0, 0, 0, 0, 255]; - if a0 > a1 { - for (k, v) in palette.iter_mut().enumerate().skip(2) { - let k = k as u32; - *v = ((8 - k) * a0 + (k - 1) * a1 + 3) / 7; - } - } else { - for (k, v) in palette.iter_mut().enumerate().take(6).skip(2) { - let k = k as u32; - *v = ((6 - k) * a0 + (k - 1) * a1 + 2) / 5; - } - } - palette.map(|v| v as u8) -} - -/// A BC1 block. Texels under half opacity become BC1's transparent black, which needs the -/// three-colour mode; an opaque block takes whichever mode fits its colours better. -pub(crate) fn encode_bc1(block: &Block, inside: Mask) -> [u8; 8] { - let transparent = select(block, inside, |t| t[3] < 128); - colour_block(block, inside & !transparent, transparent, true) -} - -/// A BC2 block: four bits of alpha per texel, then a four-colour block. -pub(crate) fn encode_bc2(block: &Block, inside: Mask) -> [u8; 16] { - let bits = block.iter().enumerate().fold(0u64, |bits, (t, texel)| bits | ((u64::from(texel[3]) * 15 + 127) / 255) << (4 * t)); - join(bits.to_le_bytes(), colour_block(block, inside, 0, false)) -} - -/// A BC3 block: an interpolated alpha block, then a four-colour block. -pub(crate) fn encode_bc3(block: &Block, inside: Mask) -> [u8; 16] { - join(alpha_block(block, inside), colour_block(block, inside, 0, false)) -} - -fn join(a: [u8; 8], b: [u8; 8]) -> [u8; 16] { - std::array::from_fn(|i| if i < 8 { a.get(i) } else { b.get(i - 8) }.copied().unwrap_or(0)) -} - -/// The texels of `inside` for which `f` holds. -fn select(block: &Block, inside: Mask, f: impl Fn(&[u8; 4]) -> bool) -> Mask { - block.iter().enumerate().filter(|(t, texel)| inside >> t & 1 != 0 && f(texel)).fold(0, |m, (t, _)| m | 1 << t) -} - -fn rgb_of(texel: &[u8; 4]) -> [f32; 3] { - [f32::from(texel[0]), f32::from(texel[1]), f32::from(texel[2])] -} - -/// RGB565 of a colour, rounding each channel to the nearest level. -fn to565(c: [f32; 3]) -> u16 { - let q = |v: f32, max: f32| (v.clamp(0.0, 255.0) * max / 255.0).round() as u16; - q(c[0], 31.0) << 11 | q(c[1], 63.0) << 5 | q(c[2], 31.0) -} - -fn distance(a: &[u8; 4], b: &[u8; 4]) -> u32 { - (0..3).map(|i| u32::from(a[i].abs_diff(b[i])).pow(2)).sum() -} - -/// A colour block (endpoints, then 2-bit indices) for the `opaque` texels' colours, with the -/// `transparent` ones on BC1's transparent index. `bc1` allows the three-colour mode; BC2 and -/// BC3 always decode four colours. -fn colour_block(block: &Block, opaque: Mask, transparent: Mask, bc1: bool) -> [u8; 8] { - let colours: Vec<[f32; 3]> = block.iter().enumerate().filter(|(t, _)| opaque >> t & 1 != 0).map(|(_, texel)| rgb_of(texel)).collect(); - if colours.is_empty() { - // Nothing visible: every texel on the transparent index (or index 0 of black). - let indices = if transparent != 0 { u32::MAX } else { 0 }; - return pack(0, 0, indices); - } - let (lo, hi) = principal_endpoints(&colours); - let mut candidates = vec![(to565(lo), to565(hi))]; - let modes: &[bool] = if transparent != 0 { - &[false] - } else if bc1 { - &[true, false] - } else { - &[true] - }; - let mut best: Option<(u32, [u8; 8])> = None; - // Each refinement starts from the best block so far: its indices give the least-squares - // endpoints, which become one more candidate. - for round in 0..3 { - for &(e0, e1) in &candidates { - for &four in modes { - let (bytes, error) = fit(block, opaque, transparent, e0, e1, four); - if best.is_none_or(|(e, _)| error < e) { - best = Some((error, bytes)); - } - } - } - let Some((error, bytes)) = best else { break }; - if error == 0 || round == 2 { - break; - } - candidates = least_squares(block, opaque, &bytes).into_iter().collect(); - if candidates.is_empty() { - break; - } - } - best.map_or([0; 8], |(_, bytes)| bytes) -} - -/// Orders endpoints `e0`, `e1` for the mode (`c0 > c1` for four colours, `c0 <= c1` for three) -/// and picks each texel's nearest palette colour. Returns the block and its squared error. -fn fit(block: &Block, opaque: Mask, transparent: Mask, e0: u16, e1: u16, four: bool) -> ([u8; 8], u32) { - let (c0, c1) = if four == (e0 >= e1) { (e0, e1) } else { (e1, e0) }; - // Equal endpoints can't be in four-colour order; the palette is then one colour either way. - let palette = colour_palette(c0, c1, four && c0 > c1); - let choices = if four && c0 > c1 { 4 } else { 3 }; - let mut indices = 0u32; - let mut error = 0u32; - for (t, texel) in block.iter().enumerate() { - let index = if transparent >> t & 1 != 0 { - 3 - } else if opaque >> t & 1 != 0 { - let (i, d) = palette.iter().take(choices).enumerate().map(|(i, p)| (i, distance(texel, p))).min_by_key(|&(_, d)| d).unwrap_or((0, 0)); - error += d; - i as u32 - } else { - 0 - }; - indices |= index << (2 * t); - } - (pack(c0, c1, indices), error) -} - -fn pack(c0: u16, c1: u16, indices: u32) -> [u8; 8] { - let (c0, c1, i) = (c0.to_le_bytes(), c1.to_le_bytes(), indices.to_le_bytes()); - [c0[0], c0[1], c1[0], c1[1], i[0], i[1], i[2], i[3]] -} - -/// The ends of the colours' spread along their principal axis (the covariance's dominant -/// eigenvector, by power iteration), clamped to the RGB cube. -fn principal_endpoints(colours: &[[f32; 3]]) -> ([f32; 3], [f32; 3]) { - let n = colours.len().max(1) as f32; - let mean = [0, 1, 2].map(|i| colours.iter().map(|c| c[i]).sum::() / n); - let mut cov = [[0f32; 3]; 3]; - for c in colours { - let d = [c[0] - mean[0], c[1] - mean[1], c[2] - mean[2]]; - for (i, row) in cov.iter_mut().enumerate() { - for (j, v) in row.iter_mut().enumerate() { - *v += d[i] * d[j]; - } - } - } - let times = |a: [f32; 3]| [0, 1, 2].map(|i| cov[i][0] * a[0] + cov[i][1] * a[1] + cov[i][2] * a[2]); - let iterate = |start: usize| { - let mut axis = [0f32; 3]; - axis[start] = 1.0; - for _ in 0..8 { - let next = times(axis); - let len = next.iter().map(|v| v * v).sum::().sqrt(); - if !len.is_normal() { - break; - } - axis = next.map(|v| v / len); - } - axis - }; - // From each channel's axis, keeping the one the colours spread along most: a channel that - // varies on its own is already an eigenvector, so a single start there would never leave it - // even when the others vary more together. - let spread = |a: [f32; 3]| times(a).iter().zip(a).map(|(x, y)| x * y).sum::(); - let axis = [0, 1, 2].map(iterate).into_iter().max_by(|&a, &b| spread(a).total_cmp(&spread(b))).unwrap_or([1.0, 0.0, 0.0]); - let along = |c: &[f32; 3]| (c[0] - mean[0]) * axis[0] + (c[1] - mean[1]) * axis[1] + (c[2] - mean[2]) * axis[2]; - let (lo, hi) = colours.iter().map(along).fold((f32::MAX, f32::MIN), |(lo, hi), t| (lo.min(t), hi.max(t))); - let at = |t: f32| [0, 1, 2].map(|i| (mean[i] + t * axis[i]).clamp(0.0, 255.0)); - if lo > hi { (mean, mean) } else { (at(lo), at(hi)) } -} - -/// The endpoints that minimise the squared error for the indices `bytes` already chose: each -/// texel's index fixes its weights between the endpoints, giving 2×2 normal equations per -/// channel. `None` when the indices don't constrain both endpoints. -fn least_squares(block: &Block, opaque: Mask, bytes: &[u8; 8]) -> Option<(u16, u16)> { - let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]])); - let indices = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]); - let four = c0 > c1; - // How much of c0 each index holds. - let weight = |i: u32| match (four, i) { - (_, 0) => Some(1.0f32), - (_, 1) => Some(0.0), - (true, 2) => Some(2.0 / 3.0), - (true, _) => Some(1.0 / 3.0), - (false, 2) => Some(0.5), - (false, _) => None, - }; - let (mut aa, mut ab, mut bb) = (0f32, 0f32, 0f32); - let (mut ax, mut bx) = ([0f32; 3], [0f32; 3]); - for (t, texel) in block.iter().enumerate() { - if opaque >> t & 1 == 0 { - continue; - } - let Some(a) = weight(indices >> (2 * t) & 3) else { continue }; - let b = 1.0 - a; - let c = rgb_of(texel); - aa += a * a; - ab += a * b; - bb += b * b; - for ((x0, x1), v) in ax.iter_mut().zip(bx.iter_mut()).zip(c) { - *x0 += a * v; - *x1 += b * v; - } - } - let det = aa * bb - ab * ab; - if det.abs() < 1e-3 { - return None; - } - let e0 = [0, 1, 2].map(|i| (ax[i] * bb - bx[i] * ab) / det); - let e1 = [0, 1, 2].map(|i| (bx[i] * aa - ax[i] * ab) / det); - Some((to565(e0), to565(e1))) -} - -/// A BC3 alpha block: whichever of the two modes (eight values between the extremes, or six -/// between the extremes other than 0 and 255, plus exact 0 and 255) fits better. -fn alpha_block(block: &Block, inside: Mask) -> [u8; 8] { - let alphas = || block.iter().enumerate().filter(move |(t, _)| inside >> t & 1 != 0).map(|(_, texel)| texel[3]); - let range = - |values: &mut dyn Iterator| values.fold(None, |r: Option<(u8, u8)>, a| Some(r.map_or((a, a), |(lo, hi)| (lo.min(a), hi.max(a))))); - let (lo, hi) = range(&mut alphas()).unwrap_or((0, 0)); - // Eight values need a0 > a1; equal ends fall to the six-value mode, which starts with a0 too. - let eight = alpha_fit(block, inside, hi, lo); - let (mid_lo, mid_hi) = range(&mut alphas().filter(|a| !matches!(a, 0 | 255))).unwrap_or((0, 0)); - let six = alpha_fit(block, inside, mid_lo, mid_hi); - if six.1 < eight.1 { six.0 } else { eight.0 } -} - -fn alpha_fit(block: &Block, inside: Mask, a0: u8, a1: u8) -> ([u8; 8], u32) { - let palette = alpha_palette(a0, a1); - let mut bits = 0u64; - let mut error = 0u32; - for (t, texel) in block.iter().enumerate() { - if inside >> t & 1 == 0 { - continue; - } - let (i, d) = palette.iter().enumerate().map(|(i, &p)| (i, u32::from(p.abs_diff(texel[3])).pow(2))).min_by_key(|&(_, d)| d).unwrap_or((0, 0)); - error += d; - bits |= (i as u64) << (3 * t); - } - let b = bits.to_le_bytes(); - ([a0, a1, b[0], b[1], b[2], b[3], b[4], b[5]], error) -} - -#[cfg(test)] -mod tests { - use super::*; - - fn decode_colour(bytes: &[u8; 8], bc1: bool) -> Block { - let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]])); - let palette = colour_palette(c0, c1, c0 > c1 || !bc1); - let indices = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]); - std::array::from_fn(|t| palette[(indices >> (2 * t)) as usize & 3]) - } - - #[test] - fn solid_blocks_land_on_the_nearest_565_colour() { - for colour in [[0, 0, 0, 255], [255, 255, 255, 255], [12, 200, 77, 255]] { - let block = [colour; 16]; - let out = decode_colour(&encode_bc1(&block, u16::MAX), true); - for texel in out { - assert!(texel.iter().zip(colour).all(|(a, b)| a.abs_diff(b) <= 4), "{texel:?} for {colour:?}"); - } - } - } - - #[test] - fn two_colour_blocks_are_exact_when_the_colours_are_565() { - let (a, b) = ([255, 0, 0, 255], [0, 0, 255, 255]); - let block: Block = std::array::from_fn(|t| if t % 3 == 0 { a } else { b }); - for bc1 in [true, false] { - let bytes = if bc1 { encode_bc1(&block, u16::MAX) } else { colour_block(&block, u16::MAX, 0, false) }; - assert_eq!(decode_colour(&bytes, bc1), block); - } - } - - #[test] - fn bc1_punches_through_texels_under_half_alpha() { - let block: Block = std::array::from_fn(|t| if t < 8 { [200, 100, 50, 255] } else { [9, 9, 9, 127] }); - let bytes = encode_bc1(&block, u16::MAX); - let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]])); - assert!(c0 <= c1, "three-colour mode"); - let out = decode_colour(&bytes, true); - assert!(out[..8].iter().all(|t| t[3] == 255) && out[8..].iter().all(|t| *t == [0; 4]), "{out:?}"); - } - - #[test] - fn four_colour_blocks_keep_c0_above_c1() { - // A ramp needs all four colours; BC2 and BC3 blocks must not rely on the order, but - // older decoders read c0 <= c1 as three colours, so the encoder never writes that. - let block: Block = std::array::from_fn(|t| [(t * 16) as u8, 0, 0, 255]); - let bytes = colour_block(&block, u16::MAX, 0, false); - assert!(u16::from_le_bytes([bytes[0], bytes[1]]) > u16::from_le_bytes([bytes[2], bytes[3]])); - } - - #[test] - fn alpha_blocks_pick_the_mode_that_keeps_0_and_255() { - // 0, 255 and a cluster in between: the six-value mode stores 0 and 255 exactly. - let block: Block = std::array::from_fn(|t| [0, 0, 0, [0, 255, 100, 110][t % 4]]); - let bytes = alpha_block(&block, u16::MAX); - assert!(bytes[0] <= bytes[1], "six-value mode"); - let palette = alpha_palette(bytes[0], bytes[1]); - let bits = u64::from_le_bytes([bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], 0, 0]); - for (t, texel) in block.iter().enumerate() { - let got = palette[(bits >> (3 * t)) as usize & 7]; - assert!(got.abs_diff(texel[3]) <= 2, "texel {t}: {got} for {}", texel[3]); - } - } - - #[test] - fn the_principal_axis_is_found_when_one_channel_varies_on_its_own() { - // Red alternates on its own (variance 64); green and blue alternate together (variance 49 - // each, 98 along their shared axis), uncorrelated with red. Starting on red alone would - // keep red's axis and flatten green and blue; the block must do better than its bounding box. - let block: Block = std::array::from_fn(|t| { - let gb = if (t / 2) % 2 == 0 { 121 } else { 135 }; - [if t % 2 == 0 { 120 } else { 136 }, gb, gb, 255] - }); - let error = |bytes: &[u8; 8]| decode_colour(bytes, false).iter().zip(&block).map(|(a, b)| distance(a, b)).sum::(); - let (_, bounding_box) = fit(&block, u16::MAX, 0, to565([120.0, 121.0, 121.0]), to565([136.0, 135.0, 135.0]), true); - let ours = error(&colour_block(&block, u16::MAX, 0, false)); - assert!(ours < bounding_box, "{ours} vs the bounding box's {bounding_box}"); - } - - #[test] - fn texels_outside_the_image_are_ignored() { - // Only the top-left texel is inside: its colour alone decides the endpoints. - let mut block: Block = [[0, 255, 0, 255]; 16]; - block[0] = [255, 0, 255, 255]; - let out = decode_colour(&encode_bc1(&block, 1), true); - assert_eq!(out[0], [255, 0, 255, 255]); - } -} diff --git a/crates/raster-codecs/src/dicom.rs b/crates/raster-codecs/src/dicom.rs index 31f99839f..ee6c73208 100644 --- a/crates/raster-codecs/src/dicom.rs +++ b/crates/raster-codecs/src/dicom.rs @@ -1,526 +1,10 @@ -//! DICOM (medical imaging) reader, uncompressed transfer syntaxes only. -//! -//! Supports what a viewer needs from a single-frame image: MONOCHROME1/2 (gray) and RGB or -//! YBR_FULL, 8- and 16-bit samples, interleaved or planar, both byte orders and explicit or -//! implicit VR. Undefined-length sequences outside the pixel data are skipped (clinical files -//! contain them); compressed transfer syntaxes, encapsulated pixel data and other photometric -//! interpretations are refused with a clear error. Read-only in this crate. +//! DICOM compatibility facade; implementation and detection live in craft-image-codecs. +use crate::{CodecError, Image, Limits, bridge}; +use craft_image_codecs::Format; -use std::collections::HashMap; - -use crate::{ChannelLayout, CodecError, Image, Limits, SampleType}; - -const F: &str = "DICOM"; -const PREAMBLE: usize = 132; // 128 zero bytes + "DICM" -const UNDEFINED: u32 = 0xFFFF_FFFF; - -fn bad(msg: impl std::fmt::Display) -> CodecError { - CodecError::malformed(F, msg) +pub fn is_dicom(bytes: &[u8]) -> bool { + craft_image_codecs::detect(bytes) == Some(Format::Dicom) } - -/// A DICOM file begins with a 128-byte preamble and the `DICM` magic. -pub fn is_dicom(b: &[u8]) -> bool { - b.get(128..132) == Some(b"DICM") -} - -#[derive(Clone, Copy)] -struct Endian { - big: bool, -} - -/// The transfer syntax: byte order and whether value representations are explicit. -#[derive(Clone, Copy)] -struct Syntax { - endian: Endian, - explicit: bool, -} - -/// The value representations whose length field is 2 reserved bytes + `u32` in explicit VR. -fn long_vr(vr: &[u8]) -> bool { - matches!(vr, b"OB" | b"OW" | b"OF" | b"SQ" | b"UT" | b"UN" | b"OD" | b"OL" | b"OV" | b"UC" | b"UR") -} - -/// A cursor over the byte stream. -struct Cur<'a> { - b: &'a [u8], - pos: usize, - big: bool, -} - -impl<'a> Cur<'a> { - fn take(&mut self, n: usize) -> Option<&'a [u8]> { - let s = self.b.get(self.pos..self.pos.checked_add(n)?)?; - self.pos += n; - Some(s) - } - fn u16(&mut self) -> Option { - let c = self.take(2)?; - Some(if self.big { u16::from_be_bytes([c[0], c[1]]) } else { u16::from_le_bytes([c[0], c[1]]) }) - } - fn u32(&mut self) -> Option { - let c = self.take(4)?; - Some(if self.big { u32::from_be_bytes([c[0], c[1], c[2], c[3]]) } else { u32::from_le_bytes([c[0], c[1], c[2], c[3]]) }) - } - /// Skip `len` bytes, refusing to run past the buffer. - fn skip(&mut self, len: usize) -> Result<(), CodecError> { - self.pos = self.pos.checked_add(len).filter(|p| *p <= self.b.len()).ok_or_else(|| bad("a value runs past the end of the file"))?; - Ok(()) - } -} - -/// Read one element's tag and value length, leaving the cursor at the value. Item and delimiter -/// tags (group `FFFE`) carry a `u32` length with no VR even under explicit VR. -fn read_header(c: &mut Cur, syn: &Syntax) -> Result<(u16, u16, [u8; 2], u32), CodecError> { - let group = c.u16().ok_or_else(|| bad("truncated element tag"))?; - let elem = c.u16().ok_or_else(|| bad("truncated element tag"))?; - let (vr, len) = if group == 0xFFFE { - ([0, 0], c.u32().ok_or_else(|| bad("truncated item length"))?) - } else if syn.explicit { - let vr = c.take(2).ok_or_else(|| bad("truncated VR"))?; - let v = [vr[0], vr[1]]; - let len = if long_vr(&v) { - c.take(2).ok_or_else(|| bad("truncated VR"))?; // reserved - c.u32().ok_or_else(|| bad("truncated length"))? - } else { - u32::from(c.u16().ok_or_else(|| bad("truncated length"))?) - }; - (v, len) - } else { - ([0, 0], c.u32().ok_or_else(|| bad("truncated length"))?) - }; - Ok((group, elem, vr, len)) -} - -/// Skip an undefined-length sequence, ending just after its `FFFE,E0DD` delimiter. Nesting is -/// tracked through undefined-length items and nested sequences; everything else is skipped by -/// its length. Returns the position after the delimiter. -fn skip_undefined(c: &mut Cur, syn: &Syntax) -> Result<(), CodecError> { - let mut depth = 1u32; - while depth > 0 { - let (group, elem, _, len) = read_header(c, syn)?; - // Item (E00D) and sequence (E0DD) delimiters close one undefined length; they carry no - // content of their own. - if group == 0xFFFE && matches!(elem, 0xE00D | 0xE0DD) { - depth -= 1; - continue; - } - if len == UNDEFINED { - depth += 1; - } else { - c.skip(len as usize)?; - } - } - Ok(()) -} - -/// Walk the elements from `start`, calling `f` with each `(group, element, value)`. An -/// undefined-length sequence outside the pixel data is skipped; encapsulated (undefined-length) -/// pixel data is refused. Returns the number of bytes walked. -fn elements(b: &[u8], start: usize, syn: &Syntax, mut f: impl FnMut(u16, u16, &Elem) -> Result<(), CodecError>) -> Result { - let mut c = Cur { b, pos: start, big: syn.endian.big }; - loop { - if c.pos >= b.len() { - return Ok(c.pos - start); - } - let (group, elem, vr, len) = read_header(&mut c, syn)?; - if len == UNDEFINED { - if (group, elem) == (0x7FE0, 0x0010) { - return Err(CodecError::unsupported(F, "encapsulated (compressed) pixel data")); - } - skip_undefined(&mut c, syn)?; - continue; - } - let len = len as usize; - let start = c.pos; - if b.len() < start.saturating_add(len) { - return Err(bad("an element value runs past the end of the file")); - } - f(group, elem, &Elem { vr, start, len })?; - c.pos = start + len; - } -} - -/// A parsed element: its VR and the byte range of its value. -struct Elem { - vr: [u8; 2], - start: usize, - len: usize, -} - -fn u16_val(b: &[u8], e: &Elem, big: bool) -> Option { - let c = b.get(e.start..e.start.checked_add(2)?)?; - Some(if big { u16::from_be_bytes([c[0], c[1]]) } else { u16::from_le_bytes([c[0], c[1]]) }) -} - -/// The transfer syntax (byte order and explicit/implicit VR) and the data-set start. The file -/// meta group is always explicit VR little-endian; the data set follows its `(0002,0010)` -/// TransferSyntaxUID. -fn transfer_syntax(b: &[u8]) -> Result<(Syntax, usize), CodecError> { - let meta = Syntax { endian: Endian { big: false }, explicit: true }; - let mut ts: Option = None; - let mut data_start = b.len(); - let mut c = Cur { b, pos: PREAMBLE, big: false }; - while c.pos + 8 <= b.len() { - let save = c.pos; - let (group, elem, _, len) = read_header(&mut c, &meta)?; - if group != 0x0002 { - // A non-meta group begins the data set. - data_start = save; - break; - } - if len == UNDEFINED { - return Err(CodecError::unsupported(F, "undefined-length meta element")); - } - let start = c.pos; - if b.len() < start.saturating_add(len as usize) { - return Err(bad("a meta element runs past the end of the file")); - } - if elem == 0x0010 { - ts = Some(String::from_utf8_lossy(&b[start..start + len as usize]).trim_end_matches(['\0', ' ']).to_string()); - } - c.pos = start + len as usize; - } - let syn = match ts.as_deref() { - Some("1.2.840.10008.1.2") => Syntax { endian: Endian { big: false }, explicit: false }, - Some("1.2.840.10008.1.2.1") => Syntax { endian: Endian { big: false }, explicit: true }, - Some("1.2.840.10008.1.2.2") => Syntax { endian: Endian { big: true }, explicit: true }, - Some(other) => return Err(CodecError::unsupported(F, format!("transfer syntax {other} (only uncompressed is read)"))), - // No meta: the historical default is implicit VR little-endian. - None => Syntax { endian: Endian { big: false }, explicit: false }, - }; - Ok((syn, data_start)) -} - -pub fn decode(b: &[u8], limits: &Limits) -> Result { - if !is_dicom(b) { - return Err(bad("not a DICOM file (no DICM magic)")); - } - let (syn, start) = transfer_syntax(b)?; - let mut tags: HashMap<(u16, u16), Elem> = HashMap::new(); - elements(b, start, &syn, |g, e, el| { - // Later duplicates must not silently override the first (DICOM forbids duplicates). - tags.entry((g, e)).or_insert_with(|| Elem { vr: el.vr, start: el.start, len: el.len }); - Ok(()) - })?; - let big = syn.endian.big; - let get_u16 = |tag: (u16, u16)| tags.get(&tag).and_then(|e| u16_val(b, e, big)); - let rows = u32::from(get_u16((0x0028, 0x0010)).ok_or_else(|| bad("missing Rows (0028,0010)"))?); - let cols = u32::from(get_u16((0x0028, 0x0011)).ok_or_else(|| bad("missing Columns (0028,0011)"))?); - let samples = u32::from(get_u16((0x0028, 0x0002)).unwrap_or(1)); - let bits = get_u16((0x0028, 0x0100)).ok_or_else(|| bad("missing BitsAllocated (0028,0100)"))?; - let bits_stored = get_u16((0x0028, 0x0101)).unwrap_or(bits); - let high_bit = get_u16((0x0028, 0x0102)).unwrap_or(bits_stored.saturating_sub(1)); - if u32::from(high_bit) >= u32::from(bits) { - return Err(bad("HighBit is outside the allocated bits")); - } - let signed = get_u16((0x0028, 0x0103)).unwrap_or(0) == 1; - let planar = get_u16((0x0028, 0x0006)).unwrap_or(0); - let photometric = tags - .get(&(0x0028, 0x0004)) - .and_then(|e| b.get(e.start..e.start.checked_add(e.len)?)) - .map(|v| String::from_utf8_lossy(v).trim_end_matches(['\0', ' ']).to_string()) - .ok_or_else(|| bad("missing PhotometricInterpretation (0028,0004)"))?; - let pixel = tags.get(&(0x7FE0, 0x0010)).ok_or_else(|| CodecError::unsupported(F, "no pixel data (7FE0,0010)"))?; - if !matches!(bits, 8 | 16) || !matches!(bits_stored, 1..=16) { - return Err(CodecError::unsupported(F, format!("{bits}-bit allocated / {bits_stored}-bit stored samples"))); - } - let mono = matches!(photometric.as_str(), "MONOCHROME1" | "MONOCHROME2"); - let rgb = photometric == "RGB"; - let ybr = photometric == "YBR_FULL"; - if !(mono || rgb || ybr) { - return Err(CodecError::unsupported(F, format!("photometric interpretation {photometric}"))); - } - // Mono stores one sample; RGB and YBR_FULL store three. Any other count would index past the - // pixel data, so it is refused rather than misread. - let want_samples = if mono { 1 } else { 3 }; - if samples != want_samples { - return Err(CodecError::unsupported(F, format!("{samples} samples per pixel with {photometric}"))); - } - if ybr && bits != 8 { - return Err(CodecError::unsupported(F, format!("{bits}-bit {photometric} (8-bit only)"))); - } - let layout = if mono { ChannelLayout::Gray } else { ChannelLayout::Rgb }; - let out_sample = if bits == 8 { SampleType::U8 } else { SampleType::U16 }; - limits.check(cols, rows, layout, out_sample)?; - - let pixels = (cols as usize).checked_mul(rows as usize).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; - let n = pixels.checked_mul(samples as usize).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; - let bytes_per = if bits == 16 { 2usize } else { 1 }; - let need = n.checked_mul(bytes_per).ok_or_else(|| CodecError::LimitExceeded("pixel data too large".into()))?; - let data = b - .get(pixel.start..pixel.start.checked_add(need).ok_or_else(|| CodecError::LimitExceeded("pixel data too large".into()))?) - .ok_or_else(|| bad("pixel data is truncated"))?; - let out_channels = layout.channels(); - let out_len = - pixels.checked_mul(out_channels).and_then(|k| k.checked_mul(bytes_per)).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; - let mut out = Vec::new(); - out.try_reserve_exact(out_len).map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?; - // Raw sample of channel `ch` at pixel `p`, honouring the planar configuration. - let sample_at = |p: usize, ch: usize| -> Option { - let idx = if planar == 1 { ch.checked_mul(pixels)?.checked_add(p)? } else { p.checked_mul(samples as usize)?.checked_add(ch)? }; - let off = idx.checked_mul(bytes_per)?; - if bits == 16 { - let c = data.get(off..off.checked_add(2)?)?; - Some(if big { u16::from_be_bytes([c[0], c[1]]) } else { u16::from_le_bytes([c[0], c[1]]) }.into()) - } else { - Some(u32::from(*data.get(off)?)) - } - }; - let stored_max = f64::from((1u32 << bits_stored) - 1); - // Extract the `bits_stored` samples (the least significant one at `high_bit`) and scale to - // [0, 1]; signed values are shifted up by half a range first. `high_bit` is 1-based here. - let shift = (u32::from(high_bit) + 1).saturating_sub(u32::from(bits_stored)); - let mask = (1u32 << bits_stored) - 1; - let half = 1u32 << (bits_stored - 1); - let norm = move |raw: u32| -> f64 { - let v = (raw >> shift) & mask; - // Signed: flip the sign bit to map [-2^(b-1), 2^(b-1)-1] onto [0, 2^b-1]. - let v = if signed { v ^ half } else { v }; - (f64::from(v) / stored_max).clamp(0.0, 1.0) - }; - let q = |x: f64| -> u16 { (x * 65535.0).round() as u16 }; - let push = |out: &mut Vec, x: f64| { - if bits == 8 { - out.push((x * 255.0).round() as u8); - } else { - out.extend_from_slice(&q(x).to_ne_bytes()); - } - }; - for p in 0..pixels { - let s = |ch: usize| sample_at(p, ch).ok_or_else(|| bad("pixel data is shorter than the image")); - if mono { - let x = norm(s(0)?); - push(&mut out, if photometric == "MONOCHROME1" { 1.0 - x } else { x }); - } else if rgb { - for ch in 0..3 { - push(&mut out, norm(s(ch)?)); - } - } else { - // YBR_FULL (8-bit): BT.601 full-range to RGB. - let y = f64::from(s(0)?); - let cb = f64::from(s(1)?) - 128.0; - let cr = f64::from(s(2)?) - 128.0; - let r = (y + 1.402 * cr).clamp(0.0, 255.0); - let g = (y - 0.344_136 * cb - 0.714_136 * cr).clamp(0.0, 255.0); - let bl = (y + 1.772 * cb).clamp(0.0, 255.0); - for v in [r, g, bl] { - push(&mut out, v / 255.0); - } - } - } - Image::from_raw(cols, rows, layout, out_sample, out) -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Build the file meta group (explicit VR little-endian), including the given extra - /// `(element, ascii)` pairs before the TransferSyntaxUID. - fn meta(b: &mut Vec, ts: &str, extra: &[(u16, &str)]) { - let ui = |e: u16, v: &str, b: &mut Vec| { - let mut s = v.as_bytes().to_vec(); - if s.len() % 2 == 1 { - s.push(0); - } - b.extend_from_slice(&0x0002u16.to_le_bytes()); - b.extend_from_slice(&e.to_le_bytes()); - b.extend_from_slice(b"UI"); - b.extend_from_slice(&(s.len() as u16).to_le_bytes()); - b.extend_from_slice(&s); - }; - for (e, v) in extra { - ui(*e, v, b); - } - ui(0x0010, ts, b); - } - - fn us(b: &mut Vec, g: u16, e: u16, v: u16, explicit: bool) { - b.extend_from_slice(&g.to_le_bytes()); - b.extend_from_slice(&e.to_le_bytes()); - if explicit { - b.extend_from_slice(b"US"); - b.extend_from_slice(&2u16.to_le_bytes()); - } else { - b.extend_from_slice(&2u32.to_le_bytes()); - } - b.extend_from_slice(&v.to_le_bytes()); - } - - fn cs(b: &mut Vec, g: u16, e: u16, v: &str, explicit: bool) { - let mut s = v.as_bytes().to_vec(); - if s.len() % 2 == 1 { - s.push(0); - } - b.extend_from_slice(&g.to_le_bytes()); - b.extend_from_slice(&e.to_le_bytes()); - if explicit { - b.extend_from_slice(b"CS"); - b.extend_from_slice(&(s.len() as u16).to_le_bytes()); - } else { - b.extend_from_slice(&(s.len() as u32).to_le_bytes()); - } - b.extend_from_slice(&s); - } - - /// A DICOM with the given transfer syntax UID and data-set encoding. - #[allow(clippy::too_many_arguments)] - fn synth( - ts: &str, - explicit: bool, - big: bool, - planar: u16, - rows: u16, - cols: u16, - samples: u16, - bits: u16, - photometric: &str, - pixels: &[u8], - ) -> Vec { - let mut b = vec![0u8; 128]; - b.extend_from_slice(b"DICM"); - // One extra meta element before the syntax, so a reader that only looks at the last - // group-0002 element takes the wrong one. - meta(&mut b, ts, &[(0x0002, "1.2.840.10008.5.1.4.1.1.7")]); - let _ = big; - us(&mut b, 0x0028, 0x0010, rows, explicit); - us(&mut b, 0x0028, 0x0011, cols, explicit); - us(&mut b, 0x0028, 0x0002, samples, explicit); - us(&mut b, 0x0028, 0x0006, planar, explicit); - cs(&mut b, 0x0028, 0x0004, photometric, explicit); - us(&mut b, 0x0028, 0x0100, bits, explicit); - us(&mut b, 0x0028, 0x0101, bits, explicit); - us(&mut b, 0x0028, 0x0102, bits - 1, explicit); - us(&mut b, 0x0028, 0x0103, 0, explicit); - b.extend_from_slice(&0x7FE0u16.to_le_bytes()); - b.extend_from_slice(&0x0010u16.to_le_bytes()); - if explicit { - b.extend_from_slice(b"OB"); - b.extend_from_slice(&0u16.to_le_bytes()); - } - b.extend_from_slice(&(pixels.len() as u32).to_le_bytes()); - b.extend_from_slice(pixels); - b - } - - #[test] - fn decodes_8bit_monochrome() { - let b = synth("1.2.840.10008.1.2.1", true, false, 0, 2, 2, 1, 8, "MONOCHROME2", &[0, 51, 102, 255]); - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!((img.width(), img.height(), img.layout(), img.sample_type()), (2, 2, ChannelLayout::Gray, SampleType::U8)); - assert_eq!(img.data(), &[0, 51, 102, 255]); - } - - #[test] - fn decodes_16bit_monochrome_and_scales() { - let px: Vec = [0u16, 32768, 65535].iter().flat_map(|v| v.to_le_bytes()).collect(); - let b = synth("1.2.840.10008.1.2.1", true, false, 0, 3, 1, 1, 16, "MONOCHROME2", &px); - let img = decode(&b, &Limits::default()).unwrap(); - let v = img.to_u16_samples().unwrap(); - assert_eq!(v[0], 0); - assert!((i32::from(v[1]) - 32768).abs() <= 1); - assert_eq!(v[2], 65535); - } - - #[test] - fn monochrome1_is_inverted() { - let b = synth("1.2.840.10008.1.2.1", true, false, 0, 2, 1, 1, 8, "MONOCHROME1", &[0, 255]); - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!(img.data(), &[255, 0]); - } - - #[test] - fn decodes_rgb_interleaved_and_planar() { - // 2 pixels: (10,20,30) and (40,50,60). - let interleaved = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 2, 3, 8, "RGB", &[10, 20, 30, 40, 50, 60]); - let img = decode(&interleaved, &Limits::default()).unwrap(); - assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgb, &[10u8, 20, 30, 40, 50, 60][..])); - // Planar 0: R plane, then G, then B. - let planar = synth("1.2.840.10008.1.2.1", true, false, 1, 1, 2, 3, 8, "RGB", &[10, 40, 20, 50, 30, 60]); - let img = decode(&planar, &Limits::default()).unwrap(); - assert_eq!(img.data(), &[10, 20, 30, 40, 50, 60]); - } - - #[test] - fn decodes_ybr_full_to_rgb() { - // White (Y=128, Cb=Cr=128) stays neutral grey. - let b = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 1, 3, 8, "YBR_FULL", &[128, 128, 128]); - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!(img.layout(), ChannelLayout::Rgb); - let d = img.data(); - assert!(d[0].abs_diff(d[1]) <= 1 && d[1].abs_diff(d[2]) <= 1, "{d:?}"); - } - - #[test] - fn decodes_implicit_vr() { - let b = synth("1.2.840.10008.1.2", false, false, 0, 2, 2, 1, 8, "MONOCHROME2", &[0, 51, 102, 255]); - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!(img.layout(), ChannelLayout::Gray); - assert_eq!(img.data(), &[0, 51, 102, 255]); - } - - #[test] - fn skips_an_undefined_length_sequence_outside_the_pixel_data() { - // Insert an undefined-length SQ (0008,2218) with one item, then a defined-length one, - // before the pixel data: the reader must skip both and still decode the image. - let mut b = vec![0u8; 128]; - b.extend_from_slice(b"DICM"); - meta(&mut b, "1.2.840.10008.1.2.1", &[]); - for (g, e, v) in [ - (0x0028, 0x0010, 1u16), - (0x0028, 0x0011, 1), - (0x0028, 0x0002, 1), - (0x0028, 0x0100, 8), - (0x0028, 0x0101, 8), - (0x0028, 0x0102, 7), - (0x0028, 0x0103, 0), - ] { - us(&mut b, g, e, v, true); - } - cs(&mut b, 0x0028, 0x0004, "MONOCHROME2", true); - // Undefined-length sequence (0008,2211), one undefined-length item containing a nested - // defined-length sequence, then the delimiters. - b.extend_from_slice(&[0x08, 0x00, 0x11, 0x22, b'S', b'Q', 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF]); - b.extend_from_slice(&[0xFE, 0xFF, 0x00, 0xE0, 0xFF, 0xFF, 0xFF, 0xFF]); // item, undefined - b.extend_from_slice(&[0x08, 0x00, 0x55, 0x22, b'S', b'Q', 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 1, 2, 3, 4]); // nested, defined - b.extend_from_slice(&[0xFE, 0xFF, 0x0D, 0xE0, 0x00, 0x00, 0x00, 0x00]); // item delimitation - b.extend_from_slice(&[0xFE, 0xFF, 0xDD, 0xE0, 0x00, 0x00, 0x00, 0x00]); // sequence delimitation - // Pixel data (defined length). - b.extend_from_slice(&0x7FE0u16.to_le_bytes()); - b.extend_from_slice(&0x0010u16.to_le_bytes()); - b.extend_from_slice(b"OB"); - b.extend_from_slice(&0u16.to_le_bytes()); - b.extend_from_slice(&1u32.to_le_bytes()); - b.push(200); - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!(img.data(), &[200]); - } - - #[test] - fn truncated_and_bad_is_error_not_panic() { - let b = synth("1.2.840.10008.1.2.1", true, false, 0, 2, 2, 1, 8, "MONOCHROME2", &[0, 1, 2, 3]); - for cut in [0, 4, 128, 132, 140, b.len() - 1] { - assert!(decode(&b[..cut.min(b.len())], &Limits::default()).is_err(), "cut {cut}"); - } - assert!(decode(b"not dicom", &Limits::default()).is_err()); - } - - #[test] - fn high_bit_outside_the_allocated_bits_is_an_error_not_a_panic() { - let mut b = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 1, 1, 8, "MONOCHROME2", &[7]); - let tag = [0x28, 0x00, 0x02, 0x01, b'U', b'S', 0x02, 0x00]; - let at = b.windows(tag.len()).position(|w| w == tag).unwrap() + tag.len(); - b[at..at + 2].copy_from_slice(&200u16.to_le_bytes()); - assert!(decode(&b, &Limits::default()).is_err()); - } - - #[test] - fn rejects_compressed_transfer_syntax() { - let mut b = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 1, 1, 8, "MONOCHROME2", &[0]); - let uncompressed = b"1.2.840.10008.1.2.1\0"; - let compressed = b"1.2.840.10008.1.2.4\0"; // same length, a compressed family - let at = b.windows(uncompressed.len()).position(|w| w == uncompressed).expect("transfer syntax in the fixture"); - b[at..at + compressed.len()].copy_from_slice(compressed); - assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. }))); - } +pub fn decode(bytes: &[u8], limits: &Limits) -> Result { + bridge::format(Format::Dicom, bytes, limits) } diff --git a/crates/raster-codecs/src/fits.rs b/crates/raster-codecs/src/fits.rs index 0d05c0fa1..5b82541c9 100644 --- a/crates/raster-codecs/src/fits.rs +++ b/crates/raster-codecs/src/fits.rs @@ -1,857 +1,19 @@ -//! FITS (Flexible Image Transport System, FITS Standard 4.0), implemented here: the image in the -//! primary HDU, or the first `IMAGE` extension when the primary HDU holds no data. -//! -//! Reading: -//! -//! * `BITPIX` 8, 16, 32, 64, -32 and -64, with `BSCALE`/`BZERO` and integer `BLANK`. -//! * `NAXIS3 = 3` opens as RGB (the planes are R, G, B); any other plane count opens the first -//! plane as gray with [`crate::DecodeWarning::MorePages`]. -//! * Rows are stored bottom-to-top, as the standard's convention has it (Siril, DS9, Astropy), -//! unless `ROWORDER = 'TOP-DOWN'`. -//! * Physical values that fit (unsigned 8-bit, or integers in 0..=65535 such as a camera's -//! `BITPIX = 16`, `BZERO = 32768`) are kept exactly as U8/U16. Everything else becomes F32: -//! values up to 2 are taken as already normalised (Siril, PixInsight), larger ones are scaled by -//! 65535 when they fit in it (DeepSkyStacker, most stackers) and by the maximum otherwise. NaN -//! and blank pixels read as 0. -//! * The other header cards are kept as text: `KEY` → value (strings unquoted), `COMMENT` and -//! `HISTORY` by their text, `HIERARCH` keywords by their long name. Long strings continued -//! on `CONTINUE` cards are joined. -//! -//! Writing: gray or RGB, U8 (`BITPIX = 8`), U16 (`BITPIX = 16`, `BZERO = 32768`) or F32 -//! (`BITPIX = -32`, 0..1), bottom-to-top with `ROWORDER = 'BOTTOM-UP'`, plus the text cards -//! (strings longer than one card on `CONTINUE` cards). -//! Tile-compressed files (fpack `ZIMAGE` tables) are recognised and refused. +//! FITS compatibility facade; implementation lives in craft-image-codecs. +use crate::{CodecError, Image, Limits, bridge}; +use craft_image_codecs::Format; -use std::collections::HashMap; - -use crate::{ChannelLayout, CodecError, DecodeWarning, Image, Limits, SampleType}; - -const F: &str = "FITS"; -const BLOCK: usize = 2880; -const CARD: usize = 80; -/// Most HDUs walked looking for an image; FITS files from cameras and stackers hold a handful. -const MAX_HDUS: usize = 1000; - -/// Keywords that describe the data layout. They are written from the image, never copied from -/// (or kept as) text metadata: after decoding they no longer describe the pixels. -const STRUCTURAL: &[&str] = &[ - "SIMPLE", "BITPIX", "NAXIS", "EXTEND", "BZERO", "BSCALE", "BLANK", "END", "XTENSION", "PCOUNT", "GCOUNT", "ROWORDER", "CHECKSUM", "DATASUM", - "DATAMIN", "DATAMAX", "ZIMAGE", -]; - -fn err(msg: impl std::fmt::Display) -> CodecError { - CodecError::malformed(F, msg) +pub fn looks_like_fits(bytes: &[u8]) -> bool { + craft_image_codecs::detect(bytes) == Some(Format::Fits) } - -#[derive(Debug, Clone, PartialEq)] -enum Value { - /// A quoted string, unescaped, trailing blanks removed. - Str(String), - /// Anything else (number, logical, complex) as written. - Raw(String), -} - -#[derive(Debug, Clone)] -struct Card { - key: String, - /// `None` for commentary cards (`COMMENT`, `HISTORY`, blank keyword). - value: Option, - /// The text of a commentary card. - text: String, - /// A `HIERARCH` keyword (`key` holds its long name). - hierarch: bool, -} - -fn parse_value(field: &str) -> Value { - let t = field.trim_start(); - if let Some(rest) = t.strip_prefix('\'') { - let mut s = String::new(); - let mut chars = rest.chars().peekable(); - while let Some(c) = chars.next() { - if c == '\'' { - if chars.peek() == Some(&'\'') { - chars.next(); - s.push('\''); - } else { - break; - } - } else { - s.push(c); - } - } - Value::Str(s.trim_end().to_string()) - } else { - Value::Raw(t.split('/').next().unwrap_or("").trim().to_string()) - } -} - -fn parse_card(raw: &[u8]) -> Card { - // Header bytes are ASCII 32–126 by the standard; anything else is replaced, never trusted. - let s: String = raw.iter().map(|&b| if (32..=126).contains(&b) { b as char } else { '?' }).collect(); - let key_field = s.get(..8).unwrap_or(&s).trim_end().to_string(); - if key_field == "HIERARCH" { - let rest = s.get(9..).unwrap_or(""); - if let Some((k, v)) = rest.split_once('=') { - return Card { key: k.trim().to_string(), value: Some(parse_value(v)), text: String::new(), hierarch: true }; - } - } - if s.get(8..10) == Some("= ") { - let v = parse_value(s.get(10..).unwrap_or("")); - return Card { key: key_field, value: Some(v), text: String::new(), hierarch: false }; - } - Card { key: key_field, value: None, text: s.get(8..).unwrap_or("").trim_end().to_string(), hierarch: false } -} - -struct Header { - cards: Vec, - /// First-occurrence index of each non-HIERARCH keyword, so `get` is O(1). The card - /// count and the number of looked-up keywords (NAXIS1..NAXISn) are both file-controlled, - /// so a linear scan per lookup lets a crafted header multiply commentary cards by axis - /// count into quadratic work. - index: HashMap, - /// Offset of the data unit. - data_start: usize, -} - -impl Header { - fn get(&self, key: &str) -> Option<&Value> { - let c = &self.cards[*self.index.get(key)?]; - c.value.as_ref() - } - - fn int(&self, key: &str) -> Option { - match self.get(key)? { - Value::Raw(r) => r.parse::().ok().or_else(|| self.float(key).filter(|f| f.fract() == 0.0 && f.abs() < 9.0e15).map(|f| f as i64)), - Value::Str(_) => None, - } - } - - fn float(&self, key: &str) -> Option { - match self.get(key)? { - // Fortran-style `D` exponents are allowed in FITS. - Value::Raw(r) => r.replace(['D', 'd'], "E").parse::().ok().filter(|f| f.is_finite()), - Value::Str(_) => None, - } - } - - fn logical(&self, key: &str) -> bool { - matches!(self.get(key), Some(Value::Raw(r)) if r == "T") - } - - fn string(&self, key: &str) -> Option<&str> { - match self.get(key)? { - Value::Str(s) => Some(s.trim()), - Value::Raw(_) => None, - } - } -} - -fn parse_header(b: &[u8], start: usize) -> Result { - let mut cards = Vec::new(); - let mut pos = start; - loop { - let raw = b.get(pos..pos.checked_add(CARD).ok_or_else(|| err("header too long"))?).ok_or_else(|| err("header ends before END"))?; - pos += CARD; - if raw.starts_with(b"END") && raw.get(3..).is_some_and(|r| r.iter().all(|&c| c == b' ')) { - break; - } - let card = parse_card(raw); - // Long strings (FITS 4.0 section 4.2.1.2): a string ending in '&' goes on in the next - // `CONTINUE` record. An orphaned `CONTINUE` stays a card of its own (commentary). - if let Some(Card { value: Some(Value::Str(s)), .. }) = cards.last_mut() - && s.ends_with('&') - && card.key == "CONTINUE" - && card.text.starts_with(" ") - && let Value::Str(more) = parse_value(&card.text) - { - s.pop(); - s.push_str(&more); - continue; - } - cards.push(card); - } - let data_start = pos.div_ceil(BLOCK).checked_mul(BLOCK).ok_or_else(|| err("header too long"))?; - // Index each non-HIERARCH keyword to its first occurrence, matching the previous - // `find(|c| !c.hierarch && c.key == key)` lookup while making `get` O(1). - let mut index = HashMap::with_capacity(cards.len()); - for (i, c) in cards.iter().enumerate() { - if !c.hierarch { - index.entry(c.key.clone()).or_insert(i); - } - } - Ok(Header { cards, index, data_start }) -} - -/// The axis lengths (`NAXIS1`, `NAXIS2`, …). -fn axes(h: &Header) -> Result, CodecError> { - let n = h.int("NAXIS").ok_or_else(|| err("missing NAXIS"))?; - if !(0..=999).contains(&n) { - return Err(err(format!("invalid NAXIS {n}"))); - } - (1..=n) - .map(|i| h.int(&format!("NAXIS{i}")).and_then(|v| u64::try_from(v).ok()).ok_or_else(|| err(format!("missing or negative NAXIS{i}")))) - .collect() -} - -/// Bytes in the data unit (before padding to a block). -fn data_len(h: &Header, bitpix: i64) -> Result { - let ax = axes(h)?; - if ax.is_empty() { - return Ok(0); - } - let pcount = u64::try_from(h.int("PCOUNT").unwrap_or(0)).map_err(|_| err("negative PCOUNT"))?; - let gcount = u64::try_from(h.int("GCOUNT").unwrap_or(1)).map_err(|_| err("negative GCOUNT"))?; - let elems = ax.iter().try_fold(1u64, |a, &n| a.checked_mul(n)).ok_or_else(|| err("data size overflows"))?; - elems - .checked_add(pcount) - .and_then(|v| v.checked_mul(gcount)) - .and_then(|v| v.checked_mul(bitpix.unsigned_abs() / 8)) - .ok_or_else(|| err("data size overflows")) -} - -/// A primary header's first card, `SIMPLE =`. -pub fn looks_like_fits(b: &[u8]) -> bool { - b.starts_with(b"SIMPLE =") -} - -/// Inspect every HDU without decoding pixels: one gray plane or three RGB planes are retained, -/// while other cubes, image extensions, tables and other data-bearing HDUs cannot survive a flat save. -/// Incomplete or over-limit walks fail closed rather than treating an unchecked suffix as safe. pub fn has_additional_images(bytes: &[u8]) -> Result { - let mut start = 0; - let mut image_seen = false; - for index in 0..MAX_HDUS { - let h = parse_header(bytes, start)?; - if index == 0 && h.get("SIMPLE").is_none() { - return Err(err("missing SIMPLE")); - } - let ax = axes(&h)?; - let size = data_len(&h, h.int("BITPIX").unwrap_or(8))?; - let image_hdu = index == 0 || h.string("XTENSION") == Some("IMAGE"); - if h.logical("ZIMAGE") { - return Ok(true); - } - if !image_hdu && size > 0 { - return Ok(true); - } - // An image HDU with a zero axis has no data (FITS 4.0, NAXISn) and holds no image, like - // the empty primary: `find_image` skips it too. - if image_hdu && size > 0 && ax.len() >= 2 && ax[..2].iter().all(|n| *n > 0) { - let planes = ax.iter().skip(2).try_fold(1u64, |n, axis| n.checked_mul(*axis)).ok_or_else(|| err("plane count overflows"))?; - let retained = planes == 1 || (ax.get(2) == Some(&3) && planes == 3); - if image_seen || !retained || h.int("GCOUNT").unwrap_or(1) > 1 || h.int("PCOUNT").unwrap_or(0) > 0 { - return Ok(true); - } - image_seen = true; - } else if size > 0 { - return Ok(true); - } - let padded = size.div_ceil(BLOCK as u64).checked_mul(BLOCK as u64).ok_or_else(|| err("data size overflows"))?; - let next = usize::try_from(padded).ok().and_then(|size| h.data_start.checked_add(size)).ok_or_else(|| err("data size overflows"))?; - if next > bytes.len() { - return Err(err("truncated HDU data")); - } - if next == bytes.len() { - return Ok(false); - } - start = next; - } - Err(err("too many HDUs to inspect every source image")) + craft_image_codecs::has_unretained_source_data(bytes).map_err(bridge::from_shared_error) } - -/// Whether an HDU with these axes stores pixels: at least two axes and none of them zero (FITS 4.0, -/// NAXISn: a zero axis means no data follow the header). -fn holds_pixels(ax: &[u64]) -> bool { - ax.len() >= 2 && ax.iter().all(|n| *n > 0) -} - -/// Finds the image HDU: the primary one when it holds pixels, else the first `IMAGE` extension -/// that does. Returns its header and the primary header (for its descriptive cards). -fn find_image(b: &[u8]) -> Result<(Header, Option
), CodecError> { - let primary = parse_header(b, 0)?; - if primary.get("SIMPLE").is_none() { - return Err(err("missing SIMPLE")); - } - if holds_pixels(&axes(&primary)?) { - return Ok((primary, None)); - } - let mut compressed = false; - let mut cur_start = primary.data_start; - let mut cur_len = data_len(&primary, primary.int("BITPIX").unwrap_or(8))?; - for _ in 0..MAX_HDUS { - let next = usize::try_from(cur_len.div_ceil(BLOCK as u64).saturating_mul(BLOCK as u64)) - .ok() - .and_then(|l| cur_start.checked_add(l)) - .ok_or_else(|| err("data size overflows"))?; - if next >= b.len() { - break; - } - let h = parse_header(b, next)?; - let kind = h.string("XTENSION").unwrap_or("").to_string(); - if kind == "IMAGE" && holds_pixels(&axes(&h)?) { - return Ok((h, Some(primary))); - } - if kind == "BINTABLE" && h.logical("ZIMAGE") { - compressed = true; - } - cur_len = data_len(&h, h.int("BITPIX").unwrap_or(8))?; - cur_start = h.data_start; - } - if compressed { - return Err(CodecError::unsupported(F, "tile-compressed FITS (fpack) is not supported; decompress it with funpack first")); - } - Err(err("the file holds no image")) -} - pub fn decode(bytes: &[u8], limits: &Limits) -> Result { - let (h, primary) = find_image(bytes)?; - let bitpix = h.int("BITPIX").ok_or_else(|| err("missing BITPIX"))?; - if !matches!(bitpix, 8 | 16 | 32 | 64 | -32 | -64) { - return Err(err(format!("invalid BITPIX {bitpix}"))); - } - let ax = axes(&h)?; - let (Some(&w), Some(&hgt)) = (ax.first(), ax.get(1)) else { return Err(err("the image has fewer than two axes")) }; - let w = u32::try_from(w).map_err(|_| CodecError::LimitExceeded(format!("width {w}")))?; - let hgt = u32::try_from(hgt).map_err(|_| CodecError::LimitExceeded(format!("height {hgt}")))?; - let planes = ax.iter().skip(2).try_fold(1u64, |a, &n| a.checked_mul(n)).ok_or_else(|| err("too many planes"))?; - let (layout, nplanes) = if ax.get(2) == Some(&3) && planes == 3 { (ChannelLayout::Rgb, 3usize) } else { (ChannelLayout::Gray, 1) }; - - let bscale = h.float("BSCALE").unwrap_or(1.0); - let bzero = h.float("BZERO").unwrap_or(0.0); - let blank = if bitpix > 0 { h.int("BLANK") } else { None }; - let integral = bscale == 1.0 && bzero.fract() == 0.0; - let candidate = match bitpix { - 8 if integral && bzero == 0.0 => SampleType::U8, - 8 | 16 if integral => SampleType::U16, - _ => SampleType::F32, - }; - limits.check(w, hgt, layout, candidate)?; - - let bps = (bitpix.unsigned_abs() / 8) as usize; - let plane_len = (w as usize).checked_mul(hgt as usize).ok_or_else(|| CodecError::LimitExceeded(format!("{w}x{hgt} does not fit in memory")))?; - let n = plane_len.checked_mul(nplanes).ok_or_else(|| CodecError::LimitExceeded("image does not fit in memory".into()))?; - let need = n.checked_mul(bps).and_then(|l| l.checked_add(h.data_start)).ok_or_else(|| err("data size overflows"))?; - let data = bytes.get(h.data_start..need).ok_or_else(|| err("truncated data"))?; - - // Physical value of sample `i` (planar order); `None` for a blank or NaN pixel. - let physical = |i: usize| -> Option { - let s = data.get(i * bps..(i + 1) * bps)?; - let raw = match bitpix { - 8 => s.first().map(|&v| v as i64)?, - 16 => i16::from_be_bytes([s[0], s[1]]) as i64, - 32 => i32::from_be_bytes([s[0], s[1], s[2], s[3]]) as i64, - 64 => i64::from_be_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]]), - -32 => return Some(f32::from_be_bytes([s[0], s[1], s[2], s[3]]) as f64 * bscale + bzero).filter(|v| v.is_finite()), - _ => return Some(f64::from_be_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]]) * bscale + bzero).filter(|v| v.is_finite()), - }; - if blank == Some(raw) { - return None; - } - Some(raw as f64 * bscale + bzero) - }; - - let top_down = h.string("ROWORDER").is_some_and(|r| r.eq_ignore_ascii_case("TOP-DOWN")); - let (wu, hu) = (w as usize, hgt as usize); - // Planar, bottom-up (by default) → interleaved, top-down. - let dest = |i: usize| -> usize { - let (p, rem) = (i / plane_len, i % plane_len); - let (y, x) = (rem / wu, rem % wu); - let y = if top_down { y } else { hu - 1 - y }; - (y * wu + x) * nplanes + p - }; - - let mut img = match candidate { - SampleType::U8 => { - let mut out = vec![0u8; n]; - for i in 0..n { - out[dest(i)] = physical(i).unwrap_or(0.0) as u8; - } - Image::from_u8(w, hgt, layout, out)? - } - _ => { - let mut out16 = if candidate == SampleType::U16 { Some(vec![0u16; n]) } else { None }; - if let Some(out) = out16.as_mut() { - for i in 0..n { - let v = physical(i).unwrap_or(0.0); - if !(0.0..=65535.0).contains(&v) { - // Signed data with negative values: fall back to float. - out16 = None; - break; - } - out[dest(i)] = v as u16; - } - } - match out16 { - Some(out) => Image::from_u16(w, hgt, layout, &out)?, - None => { - let max = (0..n).filter_map(physical).fold(0.0f64, f64::max); - let white = if max <= 2.0 { - 1.0 - } else if max <= 65535.0 { - 65535.0 - } else { - max - }; - let mut out = vec![0f32; n]; - for i in 0..n { - out[dest(i)] = (physical(i).unwrap_or(0.0) / white) as f32; - } - Image::from_f32(w, hgt, layout, &out)? - } - } - } - }; - - if planes > nplanes as u64 { - img.warnings.push(DecodeWarning::MorePages { total: u32::try_from(planes).ok() }); - } - let mut text = Vec::new(); - for c in primary.iter().flat_map(|p| p.cards.iter()).chain(h.cards.iter()) { - if !c.hierarch && (STRUCTURAL.contains(&c.key.as_str()) || is_naxis(&c.key)) { - continue; - } - let entry = match &c.value { - Some(Value::Str(s) | Value::Raw(s)) => (c.key.clone(), s.clone()), - None if c.key.is_empty() && c.text.trim().is_empty() => continue, - None => (if c.key.is_empty() { "COMMENT".to_string() } else { c.key.clone() }, c.text.trim().to_string()), - }; - text.push(entry); - } - img.text = text; - Ok(img) + bridge::format(Format::Fits, bytes, limits) } - -fn is_naxis(key: &str) -> bool { - key.strip_prefix("NAXIS").is_some_and(|d| d.chars().all(|c| c.is_ascii_digit())) -} - -/// A standard keyword: 1–8 characters from `A-Z 0-9 _ -`. -fn standard_key(key: &str) -> bool { - (1..=8).contains(&key.len()) && key.bytes().all(|b| b.is_ascii_uppercase() || b.is_ascii_digit() || b == b'_' || b == b'-') -} - -fn ascii(s: &str) -> String { - s.chars().map(|c| if (' '..='~').contains(&c) { c } else { '?' }).collect() -} - -/// A FITS literal (number or logical) that can be written unquoted. -fn literal(v: &str) -> bool { - v == "T" - || v == "F" - || (v.replace(['D', 'd'], "E").parse::().is_ok_and(|f| f.is_finite()) && v.bytes().all(|b| b"0123456789+-.EeDd".contains(&b))) -} - -fn quoted(v: &str, room: usize) -> String { - let mut s = String::from("'"); - for c in ascii(v).chars() { - let add = if c == '\'' { 2 } else { 1 }; - // Keep room for the closing quote. - if s.len() + add + 1 > room { - break; - } - s.push(c); - if c == '\'' { - s.push('\''); - } +pub fn encode(image: &Image) -> Result, CodecError> { + if !matches!(image.layout(), crate::ChannelLayout::Gray | crate::ChannelLayout::Rgb) { + return Err(CodecError::encode("FITS", format!("FITS cannot store {:?}", image.layout()))); } - // Strings are at least eight characters inside the quotes. - while s.len() < 9 { - s.push(' '); - } - s.push('\''); - s -} - -struct Writer { - out: Vec, -} - -impl Writer { - fn card(&mut self, text: &str) { - let mut c: Vec = ascii(text).into_bytes(); - c.truncate(CARD); - c.resize(CARD, b' '); - self.out.extend_from_slice(&c); - } - - fn value(&mut self, key: &str, v: &str) { - if literal(v) { - self.card(&format!("{key:<8}= {v:>20}")); - return; - } - // A string too long for one card goes on in `CONTINUE` cards, every piece but the last - // ending in '&' (FITS 4.0 section 4.2.1.2). A doubled quote is never split, and trailing - // blanks (not significant) are dropped so none are kept before an '&'. - let escaped = ascii(v).trim_end().replace('\'', "''"); - let mut rest = escaped.as_str(); - let mut head = format!("{key:<8}= "); - while rest.len() > CARD - 12 { - let cut = CARD - 13 - rest.get(..CARD - 13).unwrap_or("").bytes().rev().take_while(|&b| b == b'\'').count() % 2; - let (piece, tail) = rest.split_at_checked(cut).unwrap_or((rest, "")); - self.card(&format!("{head}'{piece}&'")); - head = "CONTINUE ".to_string(); - rest = tail; - } - self.card(&format!("{head}'{rest:<8}'")); - } - - fn commentary(&mut self, key: &str, text: &str) { - let chars: Vec = ascii(text).chars().collect(); - if chars.is_empty() { - self.card(key); - } - for chunk in chars.chunks(CARD - 8) { - self.card(&format!("{key:<8}{}", chunk.iter().collect::())); - } - } - - fn text(&mut self, key: &str, v: &str) { - let upper = key.to_ascii_uppercase(); - if matches!(upper.as_str(), "COMMENT" | "HISTORY") { - self.commentary(&upper, v); - } else if standard_key(key) { - // `CONTINUE` only ever carries a string's continuation, written by `value`. - if !STRUCTURAL.contains(&key) && !is_naxis(key) && key != "CONTINUE" { - self.value(key, v); - } - } else { - // Long or mixed-case names (`Description`, `Software`): the HIERARCH convention, - // when the card fits, else a comment. - let name = ascii(key); - let head = format!("HIERARCH {name} = "); - let fits_key = !name.contains('=') && !name.trim().is_empty(); - let body = if literal(v) { v.to_string() } else { quoted(v, CARD.saturating_sub(head.len())) }; - if fits_key && head.len() + body.len() <= CARD && (literal(v) || body.ends_with('\'')) && quoted_whole(v, &body) { - self.card(&format!("{head}{body}")); - } else { - self.commentary("COMMENT", &format!("{key} = {v}")); - } - } - } -} - -/// `body` holds all of `v` (it was not cut short to fit the card). -fn quoted_whole(v: &str, body: &str) -> bool { - literal(v) || body.trim_matches('\'').trim_end().replace("''", "'") == ascii(v).trim_end() -} - -/// Writes `img` (gray or RGB; 8-bit, 16-bit or float) as a single-HDU FITS file, with -/// `img.text` as header cards. -pub fn encode(img: &Image) -> Result, CodecError> { - let (w, h) = (img.width(), img.height()); - let layout = img.layout(); - let nc = match layout { - ChannelLayout::Gray => 1, - ChannelLayout::Rgb => 3, - l => return Err(CodecError::encode(F, format!("FITS cannot store {l:?}"))), - }; - let bitpix = match img.sample_type() { - SampleType::U8 => 8, - SampleType::U16 => 16, - SampleType::F32 => -32, - }; - let mut wr = Writer { out: Vec::new() }; - wr.value("SIMPLE", "T"); - wr.value("BITPIX", &bitpix.to_string()); - wr.value("NAXIS", if nc == 3 { "3" } else { "2" }); - wr.value("NAXIS1", &w.to_string()); - wr.value("NAXIS2", &h.to_string()); - if nc == 3 { - wr.value("NAXIS3", "3"); - } - if bitpix == 16 { - wr.value("BZERO", "32768"); - wr.value("BSCALE", "1"); - } - wr.value("ROWORDER", "BOTTOM-UP"); - for (k, v) in &img.text { - wr.text(k, v); - } - wr.card("END"); - let mut out = wr.out; - out.resize(out.len().div_ceil(BLOCK) * BLOCK, b' '); - - // Planar, bottom row first. - let (wu, hu) = (w as usize, h as usize); - let idx = |p: usize, y: usize, x: usize| ((hu - 1 - y) * wu + x) * nc + p; - match bitpix { - 8 => { - let d = img.data(); - for p in 0..nc { - for y in 0..hu { - for x in 0..wu { - out.push(d[idx(p, y, x)]); - } - } - } - } - 16 => { - let d = img.to_u16_samples().unwrap_or_default(); - for p in 0..nc { - for y in 0..hu { - for x in 0..wu { - out.extend_from_slice(&((d[idx(p, y, x)] as i32 - 32768) as i16).to_be_bytes()); - } - } - } - } - _ => { - let d = img.to_f32_samples().unwrap_or_default(); - for p in 0..nc { - for y in 0..hu { - for x in 0..wu { - // 0..1 as documented: a value above 2 would make readers (ours too) - // take the data as ADU and scale it by 65535. NaN is written as 0. - let v = d[idx(p, y, x)]; - let v = if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) }; - out.extend_from_slice(&v.to_be_bytes()); - } - } - } - } - } - out.resize(out.len().div_ceil(BLOCK) * BLOCK, 0); - Ok(out) -} - -#[cfg(test)] -mod tests { - use super::*; - - fn header(cards: &[&str]) -> Vec { - let mut w = Writer { out: Vec::new() }; - for c in cards { - w.card(c); - } - w.card("END"); - let mut out = w.out; - out.resize(out.len().div_ceil(BLOCK) * BLOCK, b' '); - out - } - - #[test] - fn continue_strings_decode() { - let mut b = header(&[ - "SIMPLE = T", - "BITPIX = 8", - "NAXIS = 2", - "NAXIS1 = 1", - "NAXIS2 = 1", - "OBJECT = 'aaa&'", - "CONTINUE 'bbb&' / a comment", - "CONTINUE '&'", - "CONTINUE ''", - "OBSERVER= 'R&'", - "COMMENT hi", - "CONTINUE 'orphan'", - ]); - b.push(0); - let img = decode(&b, &Limits::default()).unwrap(); - let text = [("OBJECT", "aaabbb"), ("OBSERVER", "R&"), ("COMMENT", "hi"), ("CONTINUE", "'orphan'")]; - assert_eq!(img.text, text.map(|(k, v)| (k.to_string(), v.to_string()))); - } - - #[test] - fn continue_strings_round_trip() { - for value in ["a".repeat(100), format!("{}' {}", "a".repeat(65), "b".repeat(100)), "'".repeat(100), "a".repeat(68)] { - let mut img = Image::from_u8(1, 1, ChannelLayout::Gray, vec![0]).unwrap(); - img.text = vec![("OBJECT".into(), value.clone()), ("CONTINUE".into(), "orphan".into())]; - let b = encode(&img).unwrap(); - assert!(!b.chunks(CARD).any(|c| c.starts_with(b"CONTINUE="))); - let decoded = decode(&b, &Limits::default()).unwrap(); - assert_eq!(decoded.text, vec![("OBJECT".into(), value)]); - } - } - - #[test] - fn camera_u16_with_bzero_reads_exactly_and_bottom_up() { - let mut b = header(&[ - "SIMPLE = T", - "BITPIX = 16", - "NAXIS = 2", - "NAXIS1 = 2", - "NAXIS2 = 2", - "BZERO = 32768", - "OBJECT = 'M 31 '", - "EXPTIME = 300.0 / seconds", - ]); - for v in [0u16, 1000, 40000, 65535] { - b.extend_from_slice(&((v as i32 - 32768) as i16).to_be_bytes()); - } - b.resize(b.len().div_ceil(BLOCK) * BLOCK, 0); - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!(img.sample_type(), SampleType::U16); - // The first stored row is the bottom one. - assert_eq!(img.to_u16_samples().unwrap(), vec![40000, 65535, 0, 1000]); - assert!(img.text.contains(&("OBJECT".into(), "M 31".into()))); - assert!(img.text.contains(&("EXPTIME".into(), "300.0".into()))); - assert!(!img.text.iter().any(|(k, _)| k == "BZERO" || k == "NAXIS1")); - } - - #[test] - fn roworder_top_down_is_honoured() { - let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 2", "ROWORDER= 'TOP-DOWN'"]); - b.extend_from_slice(&[10, 20]); - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!(img.data(), &[10, 20]); - } - - #[test] - fn stacked_float_in_adu_is_scaled_by_65535() { - let mut b = header(&["SIMPLE = T", "BITPIX = -32", "NAXIS = 2", "NAXIS1 = 2", "NAXIS2 = 1"]); - for v in [65535.0f32, f32::NAN] { - b.extend_from_slice(&v.to_be_bytes()); - } - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!(img.to_f32_samples().unwrap(), vec![1.0, 0.0]); - } - - #[test] - fn hdr_float_saves_clamped_and_reopens_as_saved() { - // One value above 2 used to make the reader scale everything by 65535 (all black). - let img = Image::from_f32(2, 1, ChannelLayout::Gray, &[0.5, 3.0]).unwrap(); - let bytes = encode(&img).unwrap(); - let back = decode(&bytes, &Limits::default()).unwrap().to_f32_samples().unwrap(); - assert!((back[0] - 0.5).abs() < 1e-6, "{back:?}"); - assert!((back[1] - 1.0).abs() < 1e-6, "{back:?}"); - } - - #[test] - fn signed_negative_data_falls_back_to_float() { - let mut b = header(&["SIMPLE = T", "BITPIX = 16", "NAXIS = 2", "NAXIS1 = 2", "NAXIS2 = 1"]); - for v in [-100i16, 1000] { - b.extend_from_slice(&v.to_be_bytes()); - } - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!(img.sample_type(), SampleType::F32); - } - - #[test] - fn image_extension_after_empty_primary() { - let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0", "EXTEND = T", "TELESCOP= 'Seestar S50'"]); - b.extend(header(&["XTENSION= 'IMAGE '", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 1", "PCOUNT = 0", "GCOUNT = 1"])); - b.push(42); - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!(img.data(), &[42]); - assert!(img.text.contains(&("TELESCOP".into(), "Seestar S50".into()))); - } - - /// A zero axis (`NAXIS3 = 0`) means the HDU has no data: it is neither the opened image nor - /// an additional one that a flat save would discard. - #[test] - fn zero_axis_image_hdus_are_empty() { - let empty = |primary: bool| { - let first = if primary { "SIMPLE = T" } else { "XTENSION= 'IMAGE '" }; - header(&[first, "BITPIX = 8", "NAXIS = 3", "NAXIS1 = 1", "NAXIS2 = 1", "NAXIS3 = 0", "PCOUNT = 0", "GCOUNT = 1"]) - }; - let image = || { - let mut b = header(&["XTENSION= 'IMAGE '", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 1", "PCOUNT = 0", "GCOUNT = 1"]); - b.push(42); - b.resize(b.len().div_ceil(BLOCK) * BLOCK, 0); - b - }; - let mut after_empty_primary = empty(true); - after_empty_primary.extend(image()); - let mut after_empty_extension = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0", "EXTEND = T"]); - after_empty_extension.extend(empty(false)); - after_empty_extension.extend(image()); - let mut before_empty_extension = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0", "EXTEND = T"]); - before_empty_extension.extend(image()); - before_empty_extension.extend(empty(false)); - for b in [&after_empty_primary, &after_empty_extension, &before_empty_extension] { - assert!(!has_additional_images(b).unwrap()); - assert_eq!(decode(b, &Limits::default()).unwrap().data(), &[42]); - } - // Only empty HDUs: nothing to open, nothing to lose. - assert!(!has_additional_images(&empty(true)).unwrap()); - assert!(decode(&empty(true), &Limits::default()).is_err()); - // A second non-empty image is still additional data. - let mut two = after_empty_primary.clone(); - two.extend(image()); - assert!(has_additional_images(&two).unwrap()); - } - - #[test] - fn fpack_is_refused_as_unsupported() { - let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0"]); - b.extend(header(&["XTENSION= 'BINTABLE'", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 0", "NAXIS2 = 0", "ZIMAGE = T"])); - b.extend(header(&["XTENSION= 'BINTABLE'", "BITPIX = 8", "NAXIS = 1", "NAXIS1 = 0"])); - assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. }))); - } - - /// Hostile headers: absurd or negative axes, bad BITPIX, overflowing sizes, missing END, - /// a long chain of empty HDUs and declared data past the end all error, never panic or - /// allocate the declared size. - #[test] - fn hostile_headers_error_without_panicking() { - let base = ["SIMPLE = T", "BITPIX = 16"]; - let cases: &[&[&str]] = &[ - &["NAXIS = 2", "NAXIS1 = 4294967296", "NAXIS2 = 1"], - &["NAXIS = 2", "NAXIS1 = -5", "NAXIS2 = 1"], - &["NAXIS = 2", "NAXIS1 = 2000000", "NAXIS2 = 2000000"], - &["NAXIS = 3", "NAXIS1 = 65536", "NAXIS2 = 65536", "NAXIS3 = 9223372036854775807"], - &["NAXIS = 1000"], - &["NAXIS = -1"], - &["NAXIS = 2", "NAXIS1 = 0", "NAXIS2 = 0"], - &["NAXIS = 2", "NAXIS1 = 1.5D300", "NAXIS2 = 1"], - &["NAXIS = 2"], - &["NAXIS = 2", "NAXIS1 = 100", "NAXIS2 = 100"], - ]; - for extra in cases { - let cards: Vec<&str> = base.iter().chain(extra.iter()).copied().collect(); - let b = header(&cards); - assert!(decode(&b, &Limits::default()).is_err(), "{extra:?}"); - } - for bitpix in ["0", "7", "-16", "128", "'X'"] { - let card = format!("BITPIX = {bitpix}"); - let b = header(&["SIMPLE = T", &card, "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 1"]); - assert!(decode(&b, &Limits::default()).is_err(), "BITPIX {bitpix}"); - } - // No END card: the header runs off the end of the file. - let mut w = Writer { out: Vec::new() }; - for c in ["SIMPLE = T", "BITPIX = 8", "NAXIS = 2"] { - w.card(c); - } - assert!(decode(&w.out, &Limits::default()).is_err()); - // An empty primary followed by many empty extensions: bounded walk, then "no image". - let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0"]); - for _ in 0..(MAX_HDUS + 5) { - b.extend(header(&["XTENSION= 'IMAGE '", "BITPIX = 8", "NAXIS = 0"])); - } - assert!(decode(&b, &Limits::default()).is_err()); - // An extension whose declared data size overflows. - let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0"]); - b.extend(header(&[ - "XTENSION= 'BINTABLE'", - "BITPIX = 64", - "NAXIS = 2", - "NAXIS1 = 9223372036854775807", - "NAXIS2 = 4", - "PCOUNT = 0", - "GCOUNT = 1", - ])); - assert!(decode(&b, &Limits::default()).is_err()); - } - - #[test] - fn extra_planes_warn() { - let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 3", "NAXIS1 = 1", "NAXIS2 = 1", "NAXIS3 = 4"]); - b.extend_from_slice(&[1, 2, 3, 4]); - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!(img.layout(), ChannelLayout::Gray); - assert_eq!(img.warnings, vec![DecodeWarning::MorePages { total: Some(4) }]); - } - - #[test] - fn strings_with_quotes_and_hierarch_round_trip() { - let mut w = Writer { out: Vec::new() }; - w.text("OBSERVER", "O'Brien"); - w.text("Software", "craft 1.0"); - w.text("HISTORY", "stacked"); - let cards: Vec = w.out.chunks(CARD).map(parse_card).collect(); - assert_eq!(cards[0].value, Some(Value::Str("O'Brien".into()))); - assert!(cards[1].hierarch && cards[1].key == "Software"); - assert_eq!(cards[1].value, Some(Value::Str("craft 1.0".into()))); - assert_eq!((cards[2].key.as_str(), cards[2].text.as_str()), ("HISTORY", "stacked")); - } + bridge::encode(image, Format::Fits, &Default::default()) } diff --git a/crates/raster-codecs/src/iff.rs b/crates/raster-codecs/src/iff.rs index 087ae5046..95e439a58 100644 --- a/crates/raster-codecs/src/iff.rs +++ b/crates/raster-codecs/src/iff.rs @@ -1,538 +1,16 @@ -//! Amiga IFF/ILBM reader and writer (EA "Interchange File Format", "InterLeaved BitMap" and the -//! chunky "PBM " variant). Written from the published IFF/ILBM layout: a `FORM` chunk of four-byte -//! chunks (`BMHD` bitmap header, `CMAP` palette, `CAMG` Amiga viewport mode, `BODY` pixel data). -//! -//! Read: 1–8 bitplanes with a palette (or a gray ramp when there is none) with masking 0/1/2, and -//! 24-bitplane "deep" ILBMs (8 planes each of red, green and blue, least significant bit first, -//! no palette) with masking 0/1, both stored (0) and ByteRun1/PackBits (1) compressed. Anything -//! else is a clear `Unsupported`/`Malformed` error, never a panic. -//! -//! Write: a 24-bitplane ILBM, ByteRun1 compressed one plane row at a time, with a mask plane -//! (masking 1) when the image has alpha. The mask plane is one bit per pixel: alpha of 128 or -//! more is opaque. +//! IFF/ILBM compatibility facade; implementation lives in craft-image-codecs. +use crate::{CodecError, Image, Limits, bridge}; +use craft_image_codecs::Format; -use crate::{ChannelLayout, CodecError, Image, Limits, SampleType}; - -const F: &str = "IFF"; - -fn bad(msg: impl std::fmt::Display) -> CodecError { - CodecError::malformed(F, msg) +pub fn looks_like_iff(bytes: &[u8]) -> bool { + craft_image_codecs::detect(bytes) == Some(Format::Iff) } - -fn unsupported(msg: impl Into) -> CodecError { - CodecError::unsupported(F, msg) +pub fn decode(bytes: &[u8], limits: &Limits) -> Result { + bridge::format(Format::Iff, bytes, limits) } - -fn be32(b: &[u8], at: usize) -> Option { - let s = b.get(at..at.checked_add(4)?)?.try_into().ok()?; - Some(u32::from_be_bytes(s)) -} - -fn be16(b: &[u8], at: usize) -> Option { - let s = b.get(at..at.checked_add(2)?)?.try_into().ok()?; - Some(u16::from_be_bytes(s)) -} - -/// `FORM` + a bitmap type (`ILBM` interleaved, `PBM ` chunky) at offset 8. -pub fn looks_like_iff(b: &[u8]) -> bool { - b.get(0..4) == Some(b"FORM") && matches!(b.get(8..12), Some(b"ILBM" | b"PBM ")) -} - -/// Bitplanes of a "deep" ILBM: red, green and blue, 8 each, least significant bit first. -const DEEP_PLANES: u8 = 24; - -/// The bitmap header fields decoding needs. -struct Header { - width: u32, - height: u32, - planes: u8, - masking: u8, - compression: u8, - transparent: u16, - /// `PBM ` stores one palette index per byte, padded to even row lengths. - chunky: bool, -} - -/// One four-byte chunk's id and byte range. -struct Chunk<'a> { - id: [u8; 4], - data: &'a [u8], -} - -/// Walk the `FORM`'s chunks from offset 12 to `end` (the form length), stopping at the first -/// malformed length so a hostile file can't run past the buffer. -fn chunks(b: &[u8]) -> impl Iterator> { - let end = be32(b, 4).map_or(b.len(), |n| (n as usize).saturating_add(8).min(b.len())); - let mut pos = 12usize; - std::iter::from_fn(move || { - if pos + 8 > end { - return None; - } - let id = b.get(pos..pos + 4)?.try_into().ok()?; - let len = be32(b, pos + 4)? as usize; - let start = pos + 8; - let data = b.get(start..start.checked_add(len)?)?; - // Chunks are padded to an even length. - pos = start.checked_add(len)?.checked_add(len & 1)?; - Some(Chunk { id, data }) - }) -} - -fn parse_header(b: &[u8], chunky: bool) -> Result { - let bmhd = chunks(b).find(|c| &c.id == b"BMHD").ok_or_else(|| bad("no BMHD chunk"))?; - let d = bmhd.data; - if d.len() < 20 { - return Err(bad("BMHD chunk is too short")); +pub fn encode(image: &Image) -> Result, CodecError> { + if !matches!(image.layout(), crate::ChannelLayout::Rgb | crate::ChannelLayout::Rgba) || image.sample_type() != crate::SampleType::U8 { + return Err(CodecError::encode("IFF", "ILBM writer stores 8-bit RGB pixels with optional alpha")); } - let width = u32::from(be16(d, 0).ok_or_else(|| bad("BMHD width"))?); - let height = u32::from(be16(d, 2).ok_or_else(|| bad("BMHD height"))?); - let planes = *d.get(8).ok_or_else(|| bad("BMHD planes"))?; - let masking = *d.get(9).ok_or_else(|| bad("BMHD masking"))?; - let compression = *d.get(10).ok_or_else(|| bad("BMHD compression"))?; - if width == 0 || height == 0 { - return Err(bad("zero-sized bitmap")); - } - if !(1..=8).contains(&planes) && planes != DEEP_PLANES { - return Err(unsupported(format!("{planes} bitplanes (1–8 with a palette, or 24)"))); - } - if masking > 2 { - return Err(unsupported(format!("masking {masking}"))); - } - if planes == DEEP_PLANES && masking == 2 { - return Err(unsupported("a transparent color in a 24-bitplane ILBM")); - } - if compression > 1 { - return Err(unsupported(format!("compression {compression} (only stored and ByteRun1)"))); - } - if chunky && (planes != 8 || masking == 1) { - return Err(unsupported("PBM requires 8-bit indices without a mask plane")); - } - let transparent = be16(d, 12).ok_or_else(|| bad("BMHD transparent colour"))?; - Ok(Header { width, height, planes, masking, compression, transparent, chunky }) -} - -/// ByteRun1 / PackBits decode of one plane row into `out`; returns the bytes consumed. A `0x80` -/// control byte is a no-op, as the layout specifies. -fn unpack_byte_run1(src: &[u8], out: &mut [u8]) -> Result { - let (mut i, mut o) = (0usize, 0usize); - while o < out.len() { - let n = *src.get(i).ok_or_else(|| bad("truncated ByteRun1 data"))? as i8; - i += 1; - if n >= 0 { - let count = n as usize + 1; - let lit = src.get(i..i.checked_add(count).ok_or_else(|| bad("ByteRun1 overflow"))?).ok_or_else(|| bad("truncated ByteRun1 run"))?; - if count > out.len() - o { - return Err(bad("ByteRun1 run crosses the plane row")); - } - out[o..o + count].copy_from_slice(lit); - i += count; - o += count; - } else if n != -128 { - let count = (1 - n as i32) as usize; - let value = *src.get(i).ok_or_else(|| bad("truncated ByteRun1 repeat"))?; - i += 1; - if count > out.len() - o { - return Err(bad("ByteRun1 repeat crosses the plane row")); - } - out[o..o + count].fill(value); - o += count; - } - } - Ok(i) -} - -pub fn decode(b: &[u8], limits: &Limits) -> Result { - if !looks_like_iff(b) { - return Err(bad("not an IFF/ILBM file (no FORM ... ILBM)")); - } - let h = parse_header(b, b.get(8..12) == Some(b"PBM "))?; - if let Some(camg) = chunks(b).find(|c| &c.id == b"CAMG") { - let mode = be32(camg.data, 0).ok_or_else(|| bad("short CAMG chunk"))?; - if mode & (0x80 | 0x800) != 0 { - return Err(unsupported("EHB and HAM viewport modes")); - } - } - let layout = if h.masking == 0 { ChannelLayout::Rgb } else { ChannelLayout::Rgba }; - limits.check(h.width, h.height, layout, SampleType::U8)?; - - // Palette: three bytes per colour; missing entries read as black. - let cmap = chunks(b).find(|c| &c.id == b"CMAP").map(|c| c.data).filter(|p| !p.is_empty()); - let colors = 1usize << h.planes; - let palette = |idx: usize| -> [u8; 3] { - if let Some(p) = cmap { - p.get(idx * 3..idx * 3 + 3).map(|c| [c[0], c[1], c[2]]).unwrap_or([0; 3]) - } else { - let v = ((idx as u32 * 255) / (colors as u32 - 1).max(1)) as u8; - [v, v, v] - } - }; - - let body = chunks(b).find(|c| &c.id == b"BODY").map(|c| c.data).ok_or_else(|| bad("no BODY chunk"))?; - let (w, hh) = (h.width as usize, h.height as usize); - // Each plane row is stored as a whole number of 16-bit words. - let plane_row = if h.chunky { w.div_ceil(2) * 2 } else { w.div_ceil(16) * 2 }; - // A masking type 1 bitmap carries one extra (mask) plane per row. - let rows_planes: usize = if h.chunky { 1 } else { usize::from(h.planes) + usize::from(h.masking == 1) }; - - // Decode every stored plane row up front, then recombine. - let total_rows = hh.checked_mul(rows_planes).ok_or_else(|| CodecError::LimitExceeded("bitmap too large".into()))?; - let total_bytes = total_rows.checked_mul(plane_row).ok_or_else(|| CodecError::LimitExceeded("bitmap too large".into()))?; - if total_bytes as u64 > limits.max_alloc { - return Err(CodecError::LimitExceeded("bitmap storage exceeds max_alloc".into())); - } - Limits::check_plausible(total_bytes as u64, body.len(), if h.compression == 0 { 1 } else { 128 })?; - let mut stored = crate::alloc_zeroed::(total_bytes)?; - let mut cursor = 0usize; - for row in stored.chunks_mut(plane_row.max(1)).take(total_rows) { - match h.compression { - 0 => { - let src = - body.get(cursor..cursor.checked_add(plane_row).ok_or_else(|| bad("BODY overflow"))?).ok_or_else(|| bad("truncated BODY data"))?; - let n = row.len().min(src.len()); - row[..n].copy_from_slice(&src[..n]); - cursor += plane_row; - } - _ => { - let tail = body.get(cursor..).ok_or_else(|| bad("truncated BODY data"))?; - cursor += unpack_byte_run1(tail, row)?; - } - } - } - - let mut out = Vec::new(); - out.try_reserve_exact(w.saturating_mul(hh).saturating_mul(layout.channels())) - .map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?; - let planes = usize::from(h.planes); - for y in 0..hh { - for x in 0..w { - let index = if h.chunky { - usize::from(*stored.get(y * plane_row + x).ok_or_else(|| bad("PBM pixel outside BODY"))?) - } else { - (0..planes).filter(|&p| bit_at(&stored, (y * rows_planes + p) * plane_row, x)).fold(0usize, |acc, p| acc | (1 << p)) - }; - let c = if h.planes == DEEP_PLANES { [index as u8, (index >> 8) as u8, (index >> 16) as u8] } else { palette(index) }; - out.extend_from_slice(&c); - if h.masking != 0 { - let opaque = - if h.masking == 1 { bit_at(&stored, (y * rows_planes + planes) * plane_row, x) } else { index != usize::from(h.transparent) }; - out.push(if opaque { 255 } else { 0 }); - } - } - } - Image::from_u8(h.width, h.height, layout, out) -} - -/// The bit for pixel `x` of a stored plane row at `base` (bits run MSB-first). -fn bit_at(stored: &[u8], base: usize, x: usize) -> bool { - stored.get(base + x / 8).is_some_and(|byte| byte & (0x80 >> (x % 8)) != 0) -} - -/// ByteRun1 / PackBits encode of one plane row, appended to `out`: three or more equal bytes -/// become a repeat (at most 128), everything else literal runs (at most 128 bytes). -fn pack_byte_run1(row: &[u8], out: &mut Vec) { - let mut i = 0usize; - while let Some(&value) = row.get(i) { - let repeat = row[i..].iter().take(128).take_while(|&&b| b == value).count(); - if repeat >= 3 { - out.push((1 - repeat as i32) as i8 as u8); - out.push(value); - i += repeat; - continue; - } - let start = i; - while i < row.len() && i - start < 128 { - if row.get(i..i + 3).is_some_and(|r| r[0] == r[1] && r[1] == r[2]) { - break; - } - i += 1; - } - out.push((i - start - 1) as u8); - out.extend_from_slice(&row[start..i]); - } -} - -fn push_chunk(out: &mut Vec, id: &[u8; 4], data: &[u8]) -> Result<(), CodecError> { - let len = u32::try_from(data.len()).map_err(|_| CodecError::encode(F, "chunk too large for an IFF file (4 GiB)"))?; - out.extend_from_slice(id); - out.extend_from_slice(&len.to_be_bytes()); - out.extend_from_slice(data); - // Chunks are padded to an even length. - if data.len() % 2 == 1 { - out.push(0); - } - Ok(()) -} - -/// Writes a 24-bitplane `FORM ILBM`: `BMHD` then a ByteRun1-compressed `BODY`, each row stored as -/// the red, green and blue planes (least significant bit first) and, for RGBA, a mask plane. -pub fn encode(img: &Image) -> Result, CodecError> { - let enc = |msg: String| CodecError::encode(F, msg); - let masked = match img.layout() { - ChannelLayout::Rgb => false, - ChannelLayout::Rgba => true, - l => return Err(enc(format!("IFF/ILBM cannot store {l:?}"))), - }; - if img.sample_type() != SampleType::U8 { - return Err(enc(format!("IFF/ILBM cannot store {:?} samples", img.sample_type()))); - } - let (w, h) = (img.width(), img.height()); - let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(h)) else { - return Err(enc("IFF/ILBM stores at most 65535 × 65535 pixels".into())); - }; - let (w, ch) = (w as usize, img.layout().channels()); - let data = img.data(); - let row_len = w.checked_mul(ch).ok_or_else(|| enc("image too large".into()))?; - if data.len() != row_len.checked_mul(h as usize).ok_or_else(|| enc("image too large".into()))? { - return Err(enc("the image has the wrong number of samples".into())); - } - let planes = usize::from(DEEP_PLANES) + usize::from(masked); - let mut plane_row = vec![0u8; w.div_ceil(16) * 2]; - let mut body = Vec::new(); - for px in data.chunks_exact(row_len.max(1)) { - for plane in 0..planes { - plane_row.fill(0); - for (x, p) in px.chunks_exact(ch).enumerate() { - let on = if plane == usize::from(DEEP_PLANES) { p[3] >= 128 } else { (p[plane / 8] >> (plane % 8)) & 1 == 1 }; - if on { - plane_row[x / 8] |= 0x80 >> (x % 8); - } - } - pack_byte_run1(&plane_row, &mut body); - } - } - - let mut bmhd = Vec::with_capacity(20); - bmhd.extend_from_slice(&w16.to_be_bytes()); - bmhd.extend_from_slice(&h16.to_be_bytes()); - bmhd.extend_from_slice(&[0, 0, 0, 0]); // x/y origin - bmhd.push(DEEP_PLANES); - bmhd.push(u8::from(masked)); // masking: 1 = a mask plane follows the color planes - bmhd.push(1); // ByteRun1 - bmhd.push(0); // pad - bmhd.extend_from_slice(&[0, 0]); // transparent color (unused) - bmhd.extend_from_slice(&[1, 1]); // square pixels - bmhd.extend_from_slice(&w16.to_be_bytes()); // page size - bmhd.extend_from_slice(&h16.to_be_bytes()); - - let mut out = b"FORM".to_vec(); - out.extend_from_slice(&[0; 4]); // patched below - out.extend_from_slice(b"ILBM"); - push_chunk(&mut out, b"BMHD", &bmhd)?; - push_chunk(&mut out, b"BODY", &body)?; - let form_len = u32::try_from(out.len() - 8).map_err(|_| enc("image too large for an IFF file (4 GiB)".into()))?; - out[4..8].copy_from_slice(&form_len.to_be_bytes()); - Ok(out) -} - -#[cfg(test)] -mod tests { - use super::*; - - /// A `FORM ILBM` with a `BMHD`, `CMAP` and `BODY`. `planes` plane rows per row, - /// `compression` 0 (stored) or 1 (ByteRun1), MSB-first. - fn ilbm(w: u16, h: u16, planes: u8, palette: &[[u8; 3]], compression: u8, body: &[u8]) -> Vec { - let mut b = b"FORM".to_vec(); - b.extend_from_slice(&0u32.to_be_bytes()); // patched below - b.extend_from_slice(b"ILBM"); - // BMHD (20 bytes) - b.extend_from_slice(b"BMHD"); - b.extend_from_slice(&20u32.to_be_bytes()); - b.extend_from_slice(&w.to_be_bytes()); - b.extend_from_slice(&h.to_be_bytes()); - b.extend_from_slice(&[0, 0, 0, 0]); // x/y origin - b.push(planes); - b.push(0); // masking - b.push(compression); - b.push(0); // pad - b.extend_from_slice(&[0, 0]); // transparent colour - b.extend_from_slice(&[1, 1]); // aspect - b.extend_from_slice(&[0, 0, 0, 0]); // page size - // CMAP - b.extend_from_slice(b"CMAP"); - b.extend_from_slice(&((palette.len() * 3) as u32).to_be_bytes()); - for c in palette { - b.extend_from_slice(c); - } - if palette.len() % 2 == 1 { - b.push(0); - } - // BODY - b.extend_from_slice(b"BODY"); - b.extend_from_slice(&(body.len() as u32).to_be_bytes()); - b.extend_from_slice(body); - if body.len() % 2 == 1 { - b.push(0); - } - let size = (b.len() - 8) as u32; - b[4..8].copy_from_slice(&size.to_be_bytes()); - b - } - - #[test] - fn reads_a_stored_two_plane_bitmap() { - // 2×1, 2 planes: pixel 0 index 0 (plane bits 0,0), pixel 1 index 3 (bits 1,1). - // Plane 0 = 0b0100_0000, plane 1 = 0b0100_0000, each padded to 2 bytes. - let body = [0b0100_0000, 0, 0b0100_0000, 0]; - let pal = [[10, 20, 30], [40, 50, 60], [70, 80, 90], [100, 110, 120]]; - let b = ilbm(2, 1, 2, &pal, 0, &body); - assert!(looks_like_iff(&b)); - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!((img.width(), img.height(), img.layout()), (2, 1, ChannelLayout::Rgb)); - assert_eq!(img.data(), &[10, 20, 30, 100, 110, 120]); - } - - #[test] - fn reads_byte_run1_compressed_rows() { - // Same image, ByteRun1: literal run of two bytes (0x01) then the two plane bytes each row. - let body = [0x01, 0b0100_0000, 0, 0x01, 0b0100_0000, 0]; - let pal = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [250, 251, 252]]; - let b = ilbm(2, 1, 2, &pal, 1, &body); - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!(img.data(), &[1, 2, 3, 250, 251, 252]); - } - - #[test] - fn no_palette_falls_back_to_a_grey_ramp() { - // 2×1, 1 plane, no CMAP: indices 0 and 1 → black and white. - let body = [0b0100_0000, 0]; - let b = ilbm(2, 1, 1, &[], 0, &body); - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!(img.data(), &[0, 0, 0, 255, 255, 255]); - } - - #[test] - fn pbm_reads_chunky_indices_with_odd_width_padding() { - let pal = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]; - for (compression, body) in [(0, vec![2, 0, 1, 255, 1, 2, 0, 255]), (1, vec![3, 2, 0, 1, 255, 3, 1, 2, 0, 255])] { - let mut b = ilbm(3, 2, 8, &pal, compression, &body); - b[8..12].copy_from_slice(b"PBM "); - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!(img.data(), &[70, 80, 90, 10, 20, 30, 40, 50, 60, 40, 50, 60, 70, 80, 90, 10, 20, 30]); - } - } - - #[test] - fn transparency_uses_the_mask_plane_or_transparent_index() { - for masking in [1, 2] { - let mut b = ilbm(2, 1, 1, &[[10, 20, 30], [40, 50, 60]], 0, if masking == 1 { &[0x40, 0, 0x40, 0] } else { &[0x40, 0] }); - b[29] = masking; - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!(img.layout(), ChannelLayout::Rgba); - assert_eq!(img.data(), &[10, 20, 30, 0, 40, 50, 60, 255]); - } - let mut b = ilbm(2, 1, 8, &[[10, 20, 30], [40, 50, 60]], 0, &[0, 1]); - b[8..12].copy_from_slice(b"PBM "); - b[29] = 2; - b[32..34].copy_from_slice(&1u16.to_be_bytes()); - assert_eq!(decode(&b, &Limits::default()).unwrap().data(), &[10, 20, 30, 255, 40, 50, 60, 0]); - } - - #[test] - fn rejects_ham_and_ehb_instead_of_returning_wrong_colours() { - for mode in [0x80u32, 0x800] { - let mut b = ilbm(1, 1, 1, &[], 0, &[0, 0]); - b.extend_from_slice(b"CAMG"); - b.extend_from_slice(&4u32.to_be_bytes()); - b.extend_from_slice(&mode.to_be_bytes()); - let len = (b.len() - 8) as u32; - b[4..8].copy_from_slice(&len.to_be_bytes()); - assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. }))); - } - } - - #[test] - fn missing_palette_entries_are_black() { - let b = ilbm(1, 1, 2, &[[10, 20, 30]], 0, &[0x80, 0, 0x80, 0]); - assert_eq!(decode(&b, &Limits::default()).unwrap().data(), &[0, 0, 0]); - } - - #[test] - fn reads_a_stored_24_plane_bitmap() { - // 2×1: pixel 0 = (1, 0, 128), pixel 1 = (0, 255, 0). Each plane row is 2 bytes; red bit 0 - // has pixel 0 set, green bits 0–7 have pixel 1 set, blue bit 7 has pixel 0 set. - let mut body = Vec::new(); - for plane in 0..24 { - let byte = match plane { - 0 | 23 => 0x80, - 8..=15 => 0x40, - _ => 0, - }; - body.extend_from_slice(&[byte, 0]); - } - let b = ilbm(2, 1, 24, &[], 0, &body); - let img = decode(&b, &Limits::default()).unwrap(); - assert_eq!((img.width(), img.height(), img.layout()), (2, 1, ChannelLayout::Rgb)); - assert_eq!(img.data(), &[1, 0, 128, 0, 255, 0]); - } - - #[test] - fn writes_24_plane_byte_run1_that_reads_back_exactly() { - // 37 pixels wide: three plane-row words, the last one partly padding. - let (w, h) = (37u32, 3u32); - let data: Vec = (0..w * h * 3).map(|i| (i * 7 % 256) as u8).collect(); - let img = Image::from_u8(w, h, ChannelLayout::Rgb, data.clone()).unwrap(); - let bytes = encode(&img).unwrap(); - assert!(looks_like_iff(&bytes)); - assert_eq!(be32(&bytes, 4).unwrap() as usize, bytes.len() - 8, "FORM length"); - let h = parse_header(&bytes, false).unwrap(); - assert_eq!((h.width, h.height, h.planes, h.masking, h.compression), (37, 3, 24, 0, 1)); - assert!(chunks(&bytes).all(|c| &c.id != b"CMAP"), "a deep ILBM has no palette"); - let back = decode(&bytes, &Limits::default()).unwrap(); - assert_eq!(back.layout(), ChannelLayout::Rgb); - assert_eq!(back.data(), &data[..]); - } - - #[test] - fn alpha_is_written_as_a_mask_plane() { - let data = [10, 20, 30, 0, 40, 50, 60, 127, 70, 80, 90, 128, 1, 2, 3, 255]; - let img = Image::from_u8(4, 1, ChannelLayout::Rgba, data.to_vec()).unwrap(); - let bytes = encode(&img).unwrap(); - assert_eq!(parse_header(&bytes, false).unwrap().masking, 1); - let back = decode(&bytes, &Limits::default()).unwrap(); - assert_eq!(back.layout(), ChannelLayout::Rgba); - assert_eq!(back.data(), &[10, 20, 30, 0, 40, 50, 60, 0, 70, 80, 90, 255, 1, 2, 3, 255]); - } - - #[test] - fn byte_run1_round_trips_runs_literals_and_long_rows() { - let mut rows: Vec> = vec![vec![], vec![5], vec![5, 5], vec![5, 5, 5], vec![1, 2, 2, 2, 3], vec![9; 300]]; - rows.push((0..300).map(|i| (i % 251) as u8).collect()); - rows.push([vec![1, 2], vec![7; 129], vec![3]].concat()); - for row in rows { - let mut packed = Vec::new(); - pack_byte_run1(&row, &mut packed); - let mut back = vec![0; row.len()]; - assert_eq!(unpack_byte_run1(&packed, &mut back).unwrap(), packed.len(), "{row:?}"); - assert_eq!(back, row); - } - // A long repeat packs into two-byte runs. - let mut packed = Vec::new(); - pack_byte_run1(&[0; 256], &mut packed); - assert_eq!(packed, [0x81, 0, 0x81, 0]); - } - - #[test] - fn rejects_what_ilbm_cannot_store() { - let gray = Image::from_u8(1, 1, ChannelLayout::Gray, vec![0]).unwrap(); - assert!(encode(&gray).is_err()); - let wide = Image::from_u8(65536, 1, ChannelLayout::Rgb, vec![0; 65536 * 3]).unwrap(); - assert!(encode(&wide).is_err()); - // A transparent color index has no meaning without a palette. - let mut b = ilbm(1, 1, 24, &[], 0, &[0; 48]); - b[29] = 2; - assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. }))); - } - - #[test] - fn hostile_input_errors_not_panics() { - // Truncations of a valid file never panic. - let body = [0b0100_0000, 0, 0b0100_0000, 0]; - let full = ilbm(2, 1, 2, &[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], 0, &body); - for cut in [0, 4, 8, 12, 40, full.len() - 1] { - assert!(decode(&full[..cut.min(full.len())], &Limits::default()).is_err(), "cut {cut}"); - } - // Too many planes is unsupported. - let bad = ilbm(1, 1, 9, &[], 0, &[0, 0]); - assert!(matches!(decode(&bad, &Limits::default()), Err(CodecError::Unsupported { .. }))); - } + bridge::encode(image, Format::Iff, &Default::default()) } diff --git a/crates/raster-codecs/src/lib.rs b/crates/raster-codecs/src/lib.rs index 5b335a30c..7e705da99 100644 --- a/crates/raster-codecs/src/lib.rs +++ b/crates/raster-codecs/src/lib.rs @@ -18,10 +18,10 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +mod bridge; /// Common bounded decoding and PNG/JPEG encoding for common image formats. pub mod common; pub mod dds; -mod dds_bc; pub mod dicom; pub mod fits; pub mod iff; @@ -215,47 +215,32 @@ impl Limits { /// Validates declared dimensions for an output buffer of the given layout and sample type. pub fn check(&self, width: u32, height: u32, layout: ChannelLayout, sample: SampleType) -> Result<(), CodecError> { - if width == 0 || height == 0 { - return Err(CodecError::InvalidImage(format!("zero-sized image {width}x{height}"))); - } - if width > self.max_width || height > self.max_height { - return Err(CodecError::LimitExceeded(format!("dimensions {width}x{height} exceed max {}x{}", self.max_width, self.max_height))); - } - let pixels = width as u64 * height as u64; - if pixels > self.max_pixels { - return Err(CodecError::LimitExceeded(format!("{pixels} pixels exceed max {}", self.max_pixels))); - } - let bytes = pixels.saturating_mul((layout.channels() * sample.bytes()) as u64); - if bytes > self.max_alloc { - return Err(CodecError::LimitExceeded(format!("{bytes} bytes exceed max_alloc {}", self.max_alloc))); - } - if usize::try_from(bytes).is_err() { - return Err(CodecError::LimitExceeded(format!("{bytes} bytes do not fit in memory"))); - } - Ok(()) + let layout = match layout { + ChannelLayout::Gray => craft_image_codecs::ChannelLayout::Gray, + ChannelLayout::GrayA => craft_image_codecs::ChannelLayout::GrayA, + ChannelLayout::Rgb => craft_image_codecs::ChannelLayout::Rgb, + ChannelLayout::Rgba => craft_image_codecs::ChannelLayout::Rgba, + ChannelLayout::Cmyk => craft_image_codecs::ChannelLayout::Cmyk, + ChannelLayout::CmykA => craft_image_codecs::ChannelLayout::CmykA, + }; + let sample = match sample { + SampleType::U8 => craft_image_codecs::SampleType::U8, + SampleType::U16 => craft_image_codecs::SampleType::U16, + SampleType::F32 => craft_image_codecs::SampleType::F32, + }; + craft_image_codecs::Limits::new(self.max_width, self.max_height, self.max_pixels, self.max_alloc) + .check(width, height, layout, sample) + .map_err(crate::bridge::from_shared_error) } /// Decompression-bomb guard for formats that compress: refuses a file too small to hold /// `declared_bytes` of stored image data when the compression never beats `max_ratio`:1. /// Called before the output buffer is allocated. 1 MiB of slack leaves tiny images alone. - pub(crate) fn check_plausible(declared_bytes: u64, file_len: usize, max_ratio: u64) -> Result<(), CodecError> { - let capacity = (file_len as u64).saturating_mul(max_ratio).saturating_add(1 << 20); - if declared_bytes > capacity { - return Err(CodecError::LimitExceeded(format!("a {file_len} byte file cannot hold {declared_bytes} bytes of image data"))); - } - Ok(()) + pub fn check_plausible(declared_bytes: u64, file_len: usize, max_ratio: u64) -> Result<(), CodecError> { + craft_image_codecs::Limits::check_plausible(declared_bytes, file_len, max_ratio).map_err(crate::bridge::from_shared_error) } } -/// A zero-filled buffer of `len` elements, allocated fallibly: an allocation failure is -/// [`CodecError::LimitExceeded`] instead of an abort. -pub(crate) fn alloc_zeroed(len: usize) -> Result, CodecError> { - let mut v = Vec::new(); - v.try_reserve_exact(len).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {len} elements for the image")))?; - v.resize(len, T::default()); - Ok(v) -} - #[cfg(test)] mod tests { use super::*; diff --git a/crates/raster-codecs/src/pcx.rs b/crates/raster-codecs/src/pcx.rs index 00117bcf1..61c476511 100644 --- a/crates/raster-codecs/src/pcx.rs +++ b/crates/raster-codecs/src/pcx.rs @@ -1,144 +1,13 @@ -//! ZSoft PCX (Paintbrush) reader: strictly bounded scanline RLE, 16/256-colour -//! palettes, 8-bit three-plane RGB and four-plane RGBA. Read-only: modern saves should use PNG. +//! PCX compatibility facade; implementation and header probing live in craft-image-codecs. +use crate::{CodecError, Image, Limits, bridge}; +use craft_image_codecs::Format; -use crate::{ChannelLayout, CodecError, Image, Limits, SampleType}; - -const HEADER: usize = 128; - -fn bad(reason: impl std::fmt::Display) -> CodecError { - CodecError::malformed("PCX", reason) +pub fn looks_like_pcx(bytes: &[u8]) -> bool { + craft_image_codecs::detect(bytes) == Some(Format::Pcx) } -fn le16(b: &[u8], i: usize) -> u16 { - u16::from_le_bytes([b[i], b[i + 1]]) +pub fn dimensions(bytes: &[u8]) -> Option<(u32, u32)> { + craft_image_codecs::pcx_dimensions(bytes) } - -/// PCX lacks a strong magic number. Require plausible complete header fields before -/// identifying it, rather than classifying every file starting with 0x0a as PCX. -pub fn looks_like_pcx(b: &[u8]) -> bool { - if b.len() < HEADER || b[0] != 0x0a || !matches!(b[1], 0 | 2..=5) || b[2] != 1 { - return false; - } - let bits = b[3]; - let planes = b[65]; - let stride = u32::from(le16(b, 66)); - let (x0, y0, x1, y1) = (le16(b, 4), le16(b, 6), le16(b, 8), le16(b, 10)); - if x1 < x0 || y1 < y0 || !matches!(bits, 1 | 2 | 4 | 8) || !(1..=4).contains(&planes) { - return false; - } - let width = u32::from(x1 - x0) + 1; - let min_stride = (width * u32::from(bits)).div_ceil(8); - stride >= min_stride && stride > 0 && (bits != 8 || matches!(planes, 1 | 3 | 4)) && (bits == 8 || u32::from(bits) * u32::from(planes) <= 4) -} - -/// Read one compressed scanline, including all planes. RLE packets never cross a scanline. -fn read_row(b: &[u8], pos: &mut usize, out: &mut [u8]) -> Result<(), CodecError> { - let mut n = 0; - while n < out.len() { - let code = *b.get(*pos).ok_or_else(|| bad("truncated pixel RLE"))?; - *pos += 1; - let (count, value) = if code >= 0xc0 { - let count = usize::from(code & 0x3f); - let value = *b.get(*pos).ok_or_else(|| bad("truncated RLE packet"))?; - *pos += 1; - (count, value) - } else { - (1, code) - }; - if count == 0 || count > out.len() - n { - return Err(bad("RLE packet crosses a scanline or has zero length")); - } - out[n..n + count].fill(value); - n += count; - } - Ok(()) -} - -/// The pixel size a PCX header declares, without decoding; `None` when [`looks_like_pcx`] is false. -pub fn dimensions(b: &[u8]) -> Option<(u32, u32)> { - looks_like_pcx(b).then(|| (u32::from(le16(b, 8) - le16(b, 4)) + 1, u32::from(le16(b, 10) - le16(b, 6)) + 1)) -} - -/// Decodes to 8-bit RGB, or RGBA for four 8-bit planes (red, green, blue, alpha). -pub fn decode(b: &[u8], limits: &Limits) -> Result { - let (w, h) = dimensions(b).ok_or_else(|| bad("invalid PCX header or unsupported pixel layout"))?; - let (bits, planes, stride) = (usize::from(b[3]), usize::from(b[65]), usize::from(le16(b, 66))); - let layout = if bits == 8 && planes == 4 { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; - let ch = layout.channels(); - limits.check(w, h, layout, SampleType::U8)?; - let row_len = stride.checked_mul(planes).ok_or_else(|| bad("row size overflow"))?; - let total = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(ch)).ok_or_else(|| bad("pixel size overflow"))?; - // The last 769 bytes are the optional VGA palette for 8-bit indexed images. - let vga = bits == 8 && planes == 1 && b.len() >= HEADER + 769 && b[b.len() - 769] == 0x0c; - if bits == 8 && planes == 1 && !vga && le16(b, 68) != 2 { - return Err(bad("8-bit indexed PCX has no 256-colour palette")); - } - let image_end = if vga { b.len() - 769 } else { b.len() }; - // A 63-byte RLE packet takes 2 bytes; a trailing remainder of 1 byte below 0xC0 - // can be encoded as a 1-byte literal, while any larger remainder takes at least 2 bytes. - // Because RLE packets may span planes within a scanline (`row_len`), compute the - // minimum encoded bytes across the full scanline (`row_len`) and multiply by `h`. - // Reject inputs that cannot possibly contain all scanlines before allocating - // the decoded image buffer. - let min_packets = (row_len / 63) - .checked_mul(2) - .and_then(|n| { - n.checked_add(match row_len % 63 { - 0 => 0, - 1 => 1, - _ => 2, - }) - }) - .ok_or_else(|| bad("compressed row size overflow"))?; - let min_encoded = min_packets.checked_mul(h as usize).ok_or_else(|| bad("compressed image size overflow"))?; - let encoded_len = image_end.saturating_sub(HEADER); - if encoded_len < min_encoded { - return Err(bad("PCX pixel data is too short for the declared image")); - } - let mut row = vec![0u8; row_len]; - let mut pixels = Vec::new(); - pixels.try_reserve_exact(total).map_err(|_| bad("PCX pixel buffer is too large"))?; - pixels.resize(total, 0); - let mut p = HEADER; - let width = w as usize; - for y in 0..h as usize { - read_row(&b[..image_end], &mut p, &mut row)?; - for x in 0..width { - let at = (y * width + x) * ch; - let rgb = if bits == 8 && planes >= 3 { - if planes == 4 { - pixels[at + 3] = row[3 * stride + x]; - } - [row[x], row[stride + x], row[2 * stride + x]] - } else { - let index = if bits == 8 { - usize::from(row[x]) - } else { - let bit = x * bits; - let shift = 8 - bits - (bit % 8); - let mask = (1usize << bits) - 1; - (0..planes).fold(0usize, |v, plane| { - let sample = (usize::from(row[plane * stride + bit / 8]) >> shift) & mask; - v | sample << (plane * bits) - }) - }; - if vga { - let o = image_end + 1 + index * 3; - [b[o], b[o + 1], b[o + 2]] - } else if bits == 8 { - let g = index as u8; - [g, g, g] // palette-info 2: gray scale - } else { - let o = 16 + index * 3; - [b[o], b[o + 1], b[o + 2]] - } - }; - pixels[at..at + 3].copy_from_slice(&rgb); - } - } - let mut image = Image::from_u8(w, h, layout, pixels)?; - let (xdpi, ydpi) = (le16(b, 12), le16(b, 14)); - if xdpi > 0 && ydpi > 0 { - image.dpi = Some((f32::from(xdpi), f32::from(ydpi))); - } - Ok(image) +pub fn decode(bytes: &[u8], limits: &Limits) -> Result { + bridge::format(Format::Pcx, bytes, limits) } diff --git a/crates/raster-codecs/src/pnm.rs b/crates/raster-codecs/src/pnm.rs index 6ef140902..338938700 100644 --- a/crates/raster-codecs/src/pnm.rs +++ b/crates/raster-codecs/src/pnm.rs @@ -1,357 +1,18 @@ -//! Netpbm family, implemented here: P1–P6 (PBM/PGM/PPM, ASCII and binary), -//! P7 (PAM, including the common `CMYK`/`CMYK_ALPHA` tuple types) and PFM -//! (`PF` colour / `Pf` gray float). -//! -//! Writing picks the subtype from the image: float → PFM (gray/RGB), gray -//! → P5, RGB → P6, anything with alpha or CMYK → P7. +//! Netpbm compatibility facade; implementation and bounds live in craft-image-codecs. +use crate::{CodecError, Image, Limits, bridge}; +use craft_image_codecs::Format; -use crate::{ChannelLayout, CodecError, Image, Limits, SampleType}; - -const F: &str = "PNM"; - -fn err(msg: impl std::fmt::Display) -> CodecError { - CodecError::malformed(F, msg) +pub fn looks_like_pnm(bytes: &[u8]) -> bool { + craft_image_codecs::detect(bytes) == Some(Format::Pnm) } - -struct Reader<'a> { - b: &'a [u8], - pos: usize, -} - -impl<'a> Reader<'a> { - fn skip_ws_and_comments(&mut self) { - while self.pos < self.b.len() { - let c = self.b[self.pos]; - if c == b'#' { - while self.pos < self.b.len() && self.b[self.pos] != b'\n' && self.b[self.pos] != b'\r' { - self.pos += 1; - } - } else if c.is_ascii_whitespace() { - self.pos += 1; - } else { - break; - } - } - } - - fn token(&mut self) -> Result<&'a [u8], CodecError> { - self.skip_ws_and_comments(); - let start = self.pos; - while self.pos < self.b.len() && !self.b[self.pos].is_ascii_whitespace() && self.b[self.pos] != b'#' { - self.pos += 1; - } - if start == self.pos { - return Err(err("unexpected end of header")); - } - Ok(&self.b[start..self.pos]) - } - - fn uint(&mut self) -> Result { - let t = self.token()?; - std::str::from_utf8(t).ok().and_then(|s| s.parse::().ok()).ok_or_else(|| err("invalid integer in header")) - } - - /// Consume the single whitespace byte that separates header and raster. - fn single_ws(&mut self) -> Result<(), CodecError> { - match self.b.get(self.pos) { - Some(c) if c.is_ascii_whitespace() => { - self.pos += 1; - Ok(()) - } - _ => Err(err("missing whitespace after header")), - } - } - - /// PAM ends its header with an LF-terminated line, unlike P4–P6's single separator. - fn pam_endhdr(&mut self) -> Result<(), CodecError> { - while let Some(&c) = self.b.get(self.pos) { - self.pos += 1; - if c == b'\n' { - // Stop at the first LF: whitespace and '#' after it are raster bytes. - return Ok(()); - } - if !c.is_ascii_whitespace() { - return Err(err("unexpected data after PAM ENDHDR")); - } - } - Err(err("unterminated PAM ENDHDR line")) - } - - fn rest(&self) -> &'a [u8] { - &self.b[self.pos.min(self.b.len())..] - } -} - -fn sample_for_maxval(maxval: u32) -> Result { - match maxval { - 1..=255 => Ok(SampleType::U8), - 256..=65535 => Ok(SampleType::U16), - _ => Err(err(format!("invalid maxval {maxval}"))), - } -} - -/// The decoded raster, built straight in the output representation. It is the only buffer sized -/// by the header, so `Limits::check` on the output budgets all of it: no wider scratch buffer -/// (such as one `u32` per sample) sits beside it. -struct Samples { - maxval: u32, - sample: SampleType, - bytes: Vec, -} - -impl Samples { - /// Reserve room for `cap` samples (callers bound `cap` by the file length where the header - /// alone could overstate it). - fn with_capacity(maxval: u32, cap: usize) -> Result { - let sample = sample_for_maxval(maxval)?; - let len = cap.checked_mul(sample.bytes()).ok_or_else(|| CodecError::LimitExceeded(format!("{cap} samples do not fit in memory")))?; - let mut bytes = Vec::new(); - bytes.try_reserve_exact(len).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {len} bytes for the image")))?; - Ok(Samples { maxval, sample, bytes }) - } - - /// Append one raw value in `[0, maxval]`, rescaled to the full range of the output sample. - fn push(&mut self, v: u32) -> Result<(), CodecError> { - let m = self.maxval; - if v > m { - return Err(err("sample exceeds maxval")); - } - match (self.sample, m) { - (SampleType::U8, 255) => self.bytes.push(v as u8), - (SampleType::U8, m) => self.bytes.push(((v * 255 + m / 2) / m) as u8), - (_, 65535) => self.bytes.extend_from_slice(&(v as u16).to_ne_bytes()), - (_, m) => self.bytes.extend_from_slice(&(((v as u64 * 65535 + m as u64 / 2) / m as u64) as u16).to_ne_bytes()), - } - Ok(()) - } - - fn into_image(self, w: u32, h: u32, layout: ChannelLayout) -> Result { - Image::from_raw(w, h, layout, self.sample, self.bytes) - } -} - -/// `w × h × channels` samples. `Limits::none()` lets any header through, so the product is -/// checked here rather than trusted to the limits. -fn sample_count(w: u32, h: u32, channels: usize) -> Result { - (w as usize) - .checked_mul(h as usize) - .and_then(|p| p.checked_mul(channels)) - .ok_or_else(|| CodecError::LimitExceeded(format!("{w}x{h}x{channels} samples do not fit in memory"))) -} - -fn read_binary(data: &[u8], n: usize, maxval: u32) -> Result { - let mut out = Samples::with_capacity(maxval, n)?; - if maxval < 256 { - let Some(raw) = data.get(..n) else { - return Err(err("truncated raster")); - }; - for &v in raw { - out.push(v as u32)?; - } - } else { - let Some(raw) = n.checked_mul(2).and_then(|len| data.get(..len)) else { - return Err(err("truncated raster")); - }; - for c in raw.as_chunks::<2>().0 { - out.push(u16::from_be_bytes(*c) as u32)?; - } - } - Ok(out) -} - -/// `P1`..`P7`, `PF` or `Pf` followed by white space. -pub fn looks_like_pnm(b: &[u8]) -> bool { - matches!(b, [b'P', b'1'..=b'7' | b'F' | b'f', s, ..] if s.is_ascii_whitespace()) -} - pub fn decode(bytes: &[u8], limits: &Limits) -> Result { - if bytes.len() < 2 || bytes[0] != b'P' { - return Err(err("not a Netpbm file")); - } - let kind = bytes[1]; - let mut r = Reader { b: bytes, pos: 2 }; - match kind { - b'1' | b'4' => { - let (w, h) = (r.uint()?, r.uint()?); - limits.check(w, h, ChannelLayout::Gray, SampleType::U8)?; - let n = sample_count(w, h, 1)?; - let mut px; - if kind == b'4' { - r.single_ws()?; - let row = (w as usize).div_ceil(8); - let data = r.rest(); - if row.checked_mul(h as usize).is_none_or(|len| data.len() < len) { - return Err(err("truncated raster")); - } - px = Vec::with_capacity(n); - for y in 0..h as usize { - for x in 0..w as usize { - let bit = (data[y * row + x / 8] >> (7 - x % 8)) & 1; - px.push(if bit == 1 { 0 } else { 255 }); - } - } - } else { - // Each ASCII pixel takes at least a byte, so the file bounds the reservation. - px = Vec::with_capacity(n.min(r.rest().len())); - while px.len() < n { - r.skip_ws_and_comments(); - match r.b.get(r.pos) { - Some(b'0') => px.push(255), - Some(b'1') => px.push(0), - Some(_) => return Err(err("invalid PBM digit")), - None => return Err(err("truncated raster")), - } - r.pos += 1; - } - } - Image::from_u8(w, h, ChannelLayout::Gray, px) - } - b'2' | b'3' | b'5' | b'6' => { - let (w, h, maxval) = (r.uint()?, r.uint()?, r.uint()?); - let layout = if matches!(kind, b'2' | b'5') { ChannelLayout::Gray } else { ChannelLayout::Rgb }; - limits.check(w, h, layout, sample_for_maxval(maxval)?)?; - let n = sample_count(w, h, layout.channels())?; - let samples = if matches!(kind, b'5' | b'6') { - r.single_ws()?; - read_binary(r.rest(), n, maxval)? - } else { - // Each ASCII sample is a digit and a separator (the last may lack one), so the - // file, not the header, bounds the reservation. - let mut v = Samples::with_capacity(maxval, n.min(r.rest().len() / 2 + 1))?; - for _ in 0..n { - v.push(r.uint()?)?; - } - v - }; - samples.into_image(w, h, layout) - } - b'7' => decode_pam(&mut r, limits), - b'F' | b'f' => { - let layout = if kind == b'F' { ChannelLayout::Rgb } else { ChannelLayout::Gray }; - let (w, h) = (r.uint()?, r.uint()?); - let scale: f32 = std::str::from_utf8(r.token()?) - .ok() - .and_then(|s| s.parse().ok()) - .filter(|s: &f32| s.is_finite() && *s != 0.0) - .ok_or_else(|| err("invalid PFM scale"))?; - r.single_ws()?; - limits.check(w, h, layout, SampleType::F32)?; - let nc = layout.channels(); - let row = w as usize * nc; - let data = r.rest(); - if sample_count(w, h, nc)?.checked_mul(4).is_none_or(|len| data.len() < len) { - return Err(err("truncated raster")); - } - let le = scale < 0.0; - // Write native-endian bytes straight into the output buffer: a separate `Vec` - // would double the budgeted allocation. - let row_bytes = row * 4; - let mut out = vec![0u8; row_bytes * h as usize]; - for (y, src) in data.chunks_exact(row_bytes).take(h as usize).enumerate() { - // PFM rows are stored bottom-to-top. - let dst_start = (h as usize - 1 - y) * row_bytes; - let Some(dst) = out.get_mut(dst_start..dst_start + row_bytes) else { - return Err(err("truncated raster")); - }; - for (d, s) in dst.as_chunks_mut::<4>().0.iter_mut().zip(src.as_chunks::<4>().0) { - let v = if le { f32::from_le_bytes(*s) } else { f32::from_be_bytes(*s) }; - *d = v.to_ne_bytes(); - } - } - Image::from_raw(w, h, layout, SampleType::F32, out) - } - _ => Err(err("unknown Netpbm subtype")), - } + bridge::format(Format::Pnm, bytes, limits) } - -fn decode_pam(r: &mut Reader<'_>, limits: &Limits) -> Result { - let (mut w, mut h, mut depth, mut maxval) = (None, None, None, None); - let mut tupltype = String::new(); - loop { - let tok = r.token()?; - match tok { - b"WIDTH" => w = Some(r.uint()?), - b"HEIGHT" => h = Some(r.uint()?), - b"DEPTH" => depth = Some(r.uint()?), - b"MAXVAL" => maxval = Some(r.uint()?), - b"TUPLTYPE" => { - let t = r.token()?; - if !tupltype.is_empty() { - tupltype.push(' '); - } - tupltype.push_str(std::str::from_utf8(t).map_err(|_| err("bad TUPLTYPE"))?); - } - b"ENDHDR" => break, - _ => return Err(err("unknown PAM header field")), - } +pub fn encode(image: &Image) -> Result, CodecError> { + // PFM has only gray and RGB tuples. Preserve this facade's contract that + // unsupported layouts are refused instead of silently dropping alpha. + if image.sample_type() == crate::SampleType::F32 && !matches!(image.layout(), crate::ChannelLayout::Gray | crate::ChannelLayout::Rgb) { + return Err(CodecError::encode("PNM", format!("PFM cannot store {:?}", image.layout()))); } - r.pam_endhdr()?; - let (w, h, depth, maxval) = match (w, h, depth, maxval) { - (Some(a), Some(b), Some(c), Some(d)) => (a, b, c, d), - _ => return Err(err("incomplete PAM header")), - }; - let layout = match (tupltype.as_str(), depth) { - ("BLACKANDWHITE" | "GRAYSCALE", 1) | ("", 1) => ChannelLayout::Gray, - ("BLACKANDWHITE_ALPHA" | "GRAYSCALE_ALPHA", 2) | ("", 2) => ChannelLayout::GrayA, - ("RGB", 3) | ("", 3) => ChannelLayout::Rgb, - ("RGB_ALPHA", 4) | ("", 4) => ChannelLayout::Rgba, - ("CMYK", 4) => ChannelLayout::Cmyk, - ("CMYK_ALPHA", 5) => ChannelLayout::CmykA, - (t, d) => { - return Err(CodecError::unsupported(F, format!("PAM tuple type {t:?} with depth {d}"))); - } - }; - limits.check(w, h, layout, sample_for_maxval(maxval)?)?; - let n = sample_count(w, h, layout.channels())?; - read_binary(r.rest(), n, maxval)?.into_image(w, h, layout) -} - -/// Writes `img` as PGM/PPM (gray, RGB), PAM (alpha, CMYK) or PFM (float gray, RGB). -pub fn encode(img: &Image) -> Result, CodecError> { - let (w, h) = (img.width(), img.height()); - let layout = img.layout(); - let mut out = Vec::new(); - match img.sample_type() { - SampleType::F32 => { - let tag = match layout { - ChannelLayout::Gray => "Pf", - ChannelLayout::Rgb => "PF", - l => return Err(CodecError::encode(F, format!("PFM cannot store {l:?}"))), - }; - out.extend_from_slice(format!("{tag}\n{w} {h}\n-1.0\n").as_bytes()); - let s = img.to_f32_samples().unwrap_or_default(); - let row = w as usize * layout.channels(); - for y in (0..h as usize).rev() { - for v in &s[y * row..(y + 1) * row] { - out.extend_from_slice(&v.to_le_bytes()); - } - } - } - sample @ (SampleType::U8 | SampleType::U16) => { - let maxval = if sample == SampleType::U8 { 255 } else { 65535 }; - match layout { - ChannelLayout::Gray => out.extend_from_slice(format!("P5\n{w} {h}\n{maxval}\n").as_bytes()), - ChannelLayout::Rgb => out.extend_from_slice(format!("P6\n{w} {h}\n{maxval}\n").as_bytes()), - l => { - let t = match l { - ChannelLayout::GrayA => "GRAYSCALE_ALPHA", - ChannelLayout::Rgba => "RGB_ALPHA", - ChannelLayout::Cmyk => "CMYK", - _ => "CMYK_ALPHA", - }; - out.extend_from_slice( - format!("P7\nWIDTH {w}\nHEIGHT {h}\nDEPTH {}\nMAXVAL {maxval}\nTUPLTYPE {t}\nENDHDR\n", l.channels()).as_bytes(), - ); - } - } - if sample == SampleType::U8 { - out.extend_from_slice(img.data()); - } else { - for v in img.to_u16_samples().unwrap_or_default() { - out.extend_from_slice(&v.to_be_bytes()); - } - } - } - } - Ok(out) + bridge::encode(image, Format::Pnm, &Default::default()) } diff --git a/crates/raster-codecs/src/xpm.rs b/crates/raster-codecs/src/xpm.rs index 05d93cd75..2d49a2d63 100644 --- a/crates/raster-codecs/src/xpm.rs +++ b/crates/raster-codecs/src/xpm.rs @@ -1,243 +1,13 @@ -//! X PixMap reader for XPM2 plain-text icons and XPM3 C-source icon arrays. -//! Supports transparency (None), RGB hex at 4/8/16 bits per channel and -//! common X11 colour names. Does not execute C code. +//! XPM compatibility facade; implementation and color-name data live in craft-image-codecs. +use crate::{CodecError, Image, Limits, bridge}; +use craft_image_codecs::Format; -use std::collections::HashMap; - -mod x11_colors; - -use crate::{ChannelLayout, CodecError, Image, Limits, SampleType}; - -const MAX_SOURCE: usize = 256 << 20; -const MAX_COLOURS: usize = 65_536; -const MAX_CPP: usize = 8; - -fn bad(msg: impl std::fmt::Display) -> CodecError { - CodecError::malformed("XPM", msg) -} - -/// Extract C string literals without treating quoted text or // in a string as a comment. -/// XPM uses one literal for the header, each palette entry and each row. -fn c_strings(bytes: &[u8]) -> Result>, CodecError> { - let mut lines = Vec::new(); - let mut i = 0usize; - while i < bytes.len() { - if bytes[i..].starts_with(b"/*") { - let tail = &bytes[i + 2..]; - let Some(end) = tail.windows(2).position(|p| p == b"*/") else { return Err(bad("unterminated C comment")) }; - i += end + 4; - } else if bytes[i..].starts_with(b"//") { - i += bytes[i..].iter().position(|&c| c == b'\n').unwrap_or(bytes.len() - i); - } else if bytes[i] == b'"' { - i += 1; - let mut s = Vec::new(); - let mut closed = false; - while i < bytes.len() { - let c = bytes[i]; - i += 1; - match c { - b'"' => { - closed = true; - break; - } - b'\\' => { - let next = *bytes.get(i).ok_or_else(|| bad("truncated C escape"))?; - i += 1; - s.push(match next { - b'n' => b'\n', - b'r' => b'\r', - b't' => b'\t', - b'0' => 0, - _ => next, // quoted backslash or quote - }); - } - _ => s.push(c), - } - } - if !closed { - return Err(bad("unterminated XPM string")); - } - lines.push(s); - } else { - i += 1; - } - } - Ok(lines) -} - -/// Extract XPM2's plain-text lines without trimming pixel rows: leading and -/// trailing spaces can be palette keys, and physical CRLF is not image data. -fn xpm2_lines(bytes: &[u8]) -> Result>, CodecError> { - let mut input = bytes.split(|&b| b == b'\n'); - let header = input.next().ok_or_else(|| bad("missing XPM2 marker"))?; - if header.strip_suffix(b"\r").unwrap_or(header) != b"! XPM2" { - return Err(bad("not an XPM2 image")); - } - let mut lines = Vec::new(); - for raw in input { - let line = raw.strip_suffix(b"\r").unwrap_or(raw); - // The header may be preceded by comments and blank lines. After the - // header, a leading '!' is allowed as a palette key or pixel value. - if lines.is_empty() && (line.is_empty() || line.starts_with(b"!")) { - continue; - } - lines.push(line.to_vec()); - } - Ok(lines) -} - -fn channel(hex: &str) -> Option { - let v = u32::from_str_radix(hex, 16).ok()?; - let max = (1u32 << (hex.len() * 4)) - 1; - Some(((v * 255 + max / 2) / max) as u8) -} - -fn named(name: &str) -> Option<[u8; 3]> { - let s = name.to_ascii_lowercase().replace([' ', '-', '_'], ""); - // XPM imports X11 names, whose RGB values differ from CSS for names - // such as gray, maroon and purple. Keep these exact rather than guessing. - if let Ok(index) = x11_colors::X11_COLORS.binary_search_by_key(&s.as_str(), |(name, _)| *name) { - return Some(x11_colors::X11_COLORS[index].1); - } - // Retain useful web color aliases not present in the X11 database. - Some(match s.as_str() { - "silver" => [192, 192, 192], - "olive" => [128, 128, 0], - "teal" => [0, 128, 128], - "fuchsia" => [255, 0, 255], - "navyblue" => [0, 0, 128], - _ => return None, - }) -} - -/// Look up an exact normalized X11 color name as RGB channels in `0..=1`. -/// -/// Names must contain 1–64 bytes of lowercase ASCII letters or digits. Unknown names, -/// unnormalized names and XPM-only aliases return `None`; this does not parse hex colors -/// or transparency. Values are the stored X11 bytes divided by 255, without CSS overrides -/// or color-space conversion. XPM decoding retains its own normalization and alias policy. pub fn x11_color_rgb(name: &str) -> Option<[f32; 3]> { - // Bound work on untrusted names before searching the shared table, without allocating. - if name.is_empty() || name.len() > 64 || !name.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit()) { - return None; - } - let index = x11_colors::X11_COLORS.binary_search_by_key(&name, |(entry, _)| *entry).ok()?; - let (_, rgb) = x11_colors::X11_COLORS.get(index)?; - Some(rgb.map(|channel| f32::from(channel) / 255.0)) + craft_image_codecs::xpm_named_color_rgb(name) } - -fn color(name: &str) -> Option<[u8; 4]> { - if name.eq_ignore_ascii_case("none") { - return Some([0, 0, 0, 0]); - } - let rgb = if let Some(hex) = name.strip_prefix('#') { - // UTF-8 byte lengths can match a supported hex width while slicing would panic. - // `from_str_radix` would also take a leading `+`: accept hex digits only. - if !hex.bytes().all(|b| b.is_ascii_hexdigit()) { - return None; - } - let digits = match hex.len() { - 3 => 1, - 6 => 2, - 12 => 4, - _ => return None, - }; - let rgb = (0..3).map(|i| channel(&hex[i * digits..(i + 1) * digits])).collect::>>()?; - [rgb[0], rgb[1], rgb[2]] - } else { - named(name)? - }; - Some([rgb[0], rgb[1], rgb[2], 255]) +pub fn looks_like_xpm(bytes: &[u8]) -> bool { + craft_image_codecs::detect(bytes) == Some(Format::Xpm) } - -/// The XPM3 comment or the XPM2 first line. -pub fn looks_like_xpm(b: &[u8]) -> bool { - b.starts_with(b"/* XPM */") || b.starts_with(b"! XPM2\n") || b.starts_with(b"! XPM2\r\n") -} - pub fn decode(bytes: &[u8], limits: &Limits) -> Result { - if bytes.len() > MAX_SOURCE { - return Err(CodecError::LimitExceeded("XPM source exceeds 256 MiB".into())); - } - let lines = if bytes.starts_with(b"/* XPM */") { - c_strings(bytes)? - } else if bytes.starts_with(b"! XPM2\n") || bytes.starts_with(b"! XPM2\r\n") { - xpm2_lines(bytes)? - } else { - return Err(bad("not an XPM2/XPM3 image")); - }; - decode_lines(&lines, limits) -} - -fn decode_lines(lines: &[Vec], limits: &Limits) -> Result { - let header = lines.first().ok_or_else(|| bad("missing image header"))?; - let parts = std::str::from_utf8(header).map_err(|_| bad("non-UTF-8 XPM header"))?; - let mut parts = parts.split_ascii_whitespace(); - let number = |key: &str, s: Option<&str>| -> Result { - s.ok_or_else(|| bad(format!("missing {key}")))?.parse::().map_err(|_| bad(format!("invalid {key}"))) - }; - let width = number("width", parts.next())?; - let height = number("height", parts.next())?; - let colours = number("palette size", parts.next())?; - let cpp = number("chars per pixel", parts.next())?; - if width == 0 || height == 0 || colours == 0 || colours > MAX_COLOURS || !(1..=MAX_CPP).contains(&cpp) { - return Err(bad("invalid XPM dimensions, palette size or chars per pixel")); - } - let (w, h) = (u32::try_from(width).map_err(|_| bad("width overflow"))?, u32::try_from(height).map_err(|_| bad("height overflow"))?); - limits.check(w, h, ChannelLayout::Rgba, SampleType::U8)?; - let required = 1usize.checked_add(colours).and_then(|n| n.checked_add(height)).ok_or_else(|| bad("row count overflow"))?; - if lines.len() < required { - return Err(bad("truncated palette or image rows")); - } - let mut palette = HashMap::, [u8; 4]>::with_capacity(colours); - for line in &lines[1..1 + colours] { - if line.len() < cpp { - return Err(bad("truncated colour key")); - } - let fields = std::str::from_utf8(&line[cpp..]).map_err(|_| bad("invalid colour name"))?; - // XPM priority is colour, gray, four-level gray, then monochrome, - // regardless of the order of keys in the palette entry. - let terms: Vec<&str> = fields.split_ascii_whitespace().collect(); - let mut picked: Option<(u8, String)> = None; - let mut i = 0; - while i < terms.len() { - let priority = match terms[i] { - "c" => 4, - "g" => 3, - "g4" => 2, - "m" => 1, - _ => 0, // Symbolic names ('s') are not colour values. - }; - let start = i + 1; - i = start; - while i < terms.len() && !matches!(terms[i], "c" | "g" | "g4" | "m" | "s") { - i += 1; - } - if priority > 0 && i > start && picked.as_ref().is_none_or(|(current, _)| priority > *current) { - picked = Some((priority, terms[start..i].join(" "))); - } - } - let value = picked.as_ref().and_then(|(_, name)| color(name)).ok_or_else(|| bad("unsupported or missing XPM colour"))?; - if palette.insert(line[..cpp].to_vec(), value).is_some() { - return Err(bad("duplicate XPM palette key")); - } - } - let row_len = width.checked_mul(cpp).ok_or_else(|| bad("row byte count overflow"))?; - // Validate all source rows before reserving the decoded RGBA image. A malformed - // short row must not be able to make a large header-sized allocation first. - if lines[1 + colours..required].iter().any(|line| line.len() != row_len) { - return Err(bad("XPM row has the wrong width")); - } - let total = width.checked_mul(height).and_then(|n| n.checked_mul(4)).ok_or_else(|| bad("pixel size overflow"))?; - let mut pixels = Vec::new(); - pixels.try_reserve_exact(total).map_err(|_| bad("XPM pixel buffer is too large"))?; - pixels.resize(total, 0); - for (y, line) in lines[1 + colours..required].iter().enumerate() { - for x in 0..width { - let key = &line[x * cpp..(x + 1) * cpp]; - let rgba = palette.get(key).ok_or_else(|| bad("unknown palette key"))?; - pixels[(y * width + x) * 4..(y * width + x + 1) * 4].copy_from_slice(rgba); - } - } - Image::from_u8(w, h, ChannelLayout::Rgba, pixels) + bridge::format(Format::Xpm, bytes, limits) } diff --git a/crates/raster-codecs/src/xpm/x11_colors.rs b/crates/raster-codecs/src/xpm/x11_colors.rs deleted file mode 100644 index 31986999a..000000000 --- a/crates/raster-codecs/src/xpm/x11_colors.rs +++ /dev/null @@ -1,666 +0,0 @@ -//! X11 color-name database (X.Org rgb.txt) normalized to lowercase without -//! spaces, underscores, or hyphens. RGB values are *X11* values, not CSS values. -//! Source: -//! License: MIT (X.Org Foundation), see the upstream app/rgb COPYING file. -//! Aliases differing only in case or spaces have been collapsed. -//! Keep sorted: decoder uses binary search without allocation per candidate. - -pub(super) const X11_COLORS: &[(&str, [u8; 3])] = &[ - ("aliceblue", [240, 248, 255]), - ("antiquewhite", [250, 235, 215]), - ("antiquewhite1", [255, 239, 219]), - ("antiquewhite2", [238, 223, 204]), - ("antiquewhite3", [205, 192, 176]), - ("antiquewhite4", [139, 131, 120]), - ("aquamarine", [127, 255, 212]), - ("aquamarine1", [127, 255, 212]), - ("aquamarine2", [118, 238, 198]), - ("aquamarine3", [102, 205, 170]), - ("aquamarine4", [69, 139, 116]), - ("azure", [240, 255, 255]), - ("azure1", [240, 255, 255]), - ("azure2", [224, 238, 238]), - ("azure3", [193, 205, 205]), - ("azure4", [131, 139, 139]), - ("beige", [245, 245, 220]), - ("bisque", [255, 228, 196]), - ("bisque1", [255, 228, 196]), - ("bisque2", [238, 213, 183]), - ("bisque3", [205, 183, 158]), - ("bisque4", [139, 125, 107]), - ("black", [0, 0, 0]), - ("blanchedalmond", [255, 235, 205]), - ("blue", [0, 0, 255]), - ("blue1", [0, 0, 255]), - ("blue2", [0, 0, 238]), - ("blue3", [0, 0, 205]), - ("blue4", [0, 0, 139]), - ("blueviolet", [138, 43, 226]), - ("brown", [165, 42, 42]), - ("brown1", [255, 64, 64]), - ("brown2", [238, 59, 59]), - ("brown3", [205, 51, 51]), - ("brown4", [139, 35, 35]), - ("burlywood", [222, 184, 135]), - ("burlywood1", [255, 211, 155]), - ("burlywood2", [238, 197, 145]), - ("burlywood3", [205, 170, 125]), - ("burlywood4", [139, 115, 85]), - ("cadetblue", [95, 158, 160]), - ("cadetblue1", [152, 245, 255]), - ("cadetblue2", [142, 229, 238]), - ("cadetblue3", [122, 197, 205]), - ("cadetblue4", [83, 134, 139]), - ("chartreuse", [127, 255, 0]), - ("chartreuse1", [127, 255, 0]), - ("chartreuse2", [118, 238, 0]), - ("chartreuse3", [102, 205, 0]), - ("chartreuse4", [69, 139, 0]), - ("chocolate", [210, 105, 30]), - ("chocolate1", [255, 127, 36]), - ("chocolate2", [238, 118, 33]), - ("chocolate3", [205, 102, 29]), - ("chocolate4", [139, 69, 19]), - ("coral", [255, 127, 80]), - ("coral1", [255, 114, 86]), - ("coral2", [238, 106, 80]), - ("coral3", [205, 91, 69]), - ("coral4", [139, 62, 47]), - ("cornflowerblue", [100, 149, 237]), - ("cornsilk", [255, 248, 220]), - ("cornsilk1", [255, 248, 220]), - ("cornsilk2", [238, 232, 205]), - ("cornsilk3", [205, 200, 177]), - ("cornsilk4", [139, 136, 120]), - ("cyan", [0, 255, 255]), - ("cyan1", [0, 255, 255]), - ("cyan2", [0, 238, 238]), - ("cyan3", [0, 205, 205]), - ("cyan4", [0, 139, 139]), - ("darkblue", [0, 0, 139]), - ("darkcyan", [0, 139, 139]), - ("darkgoldenrod", [184, 134, 11]), - ("darkgoldenrod1", [255, 185, 15]), - ("darkgoldenrod2", [238, 173, 14]), - ("darkgoldenrod3", [205, 149, 12]), - ("darkgoldenrod4", [139, 101, 8]), - ("darkgray", [169, 169, 169]), - ("darkgreen", [0, 100, 0]), - ("darkgrey", [169, 169, 169]), - ("darkkhaki", [189, 183, 107]), - ("darkmagenta", [139, 0, 139]), - ("darkolivegreen", [85, 107, 47]), - ("darkolivegreen1", [202, 255, 112]), - ("darkolivegreen2", [188, 238, 104]), - ("darkolivegreen3", [162, 205, 90]), - ("darkolivegreen4", [110, 139, 61]), - ("darkorange", [255, 140, 0]), - ("darkorange1", [255, 127, 0]), - ("darkorange2", [238, 118, 0]), - ("darkorange3", [205, 102, 0]), - ("darkorange4", [139, 69, 0]), - ("darkorchid", [153, 50, 204]), - ("darkorchid1", [191, 62, 255]), - ("darkorchid2", [178, 58, 238]), - ("darkorchid3", [154, 50, 205]), - ("darkorchid4", [104, 34, 139]), - ("darkred", [139, 0, 0]), - ("darksalmon", [233, 150, 122]), - ("darkseagreen", [143, 188, 143]), - ("darkseagreen1", [193, 255, 193]), - ("darkseagreen2", [180, 238, 180]), - ("darkseagreen3", [155, 205, 155]), - ("darkseagreen4", [105, 139, 105]), - ("darkslateblue", [72, 61, 139]), - ("darkslategray", [47, 79, 79]), - ("darkslategray1", [151, 255, 255]), - ("darkslategray2", [141, 238, 238]), - ("darkslategray3", [121, 205, 205]), - ("darkslategray4", [82, 139, 139]), - ("darkslategrey", [47, 79, 79]), - ("darkturquoise", [0, 206, 209]), - ("darkviolet", [148, 0, 211]), - ("deeppink", [255, 20, 147]), - ("deeppink1", [255, 20, 147]), - ("deeppink2", [238, 18, 137]), - ("deeppink3", [205, 16, 118]), - ("deeppink4", [139, 10, 80]), - ("deepskyblue", [0, 191, 255]), - ("deepskyblue1", [0, 191, 255]), - ("deepskyblue2", [0, 178, 238]), - ("deepskyblue3", [0, 154, 205]), - ("deepskyblue4", [0, 104, 139]), - ("dimgray", [105, 105, 105]), - ("dimgrey", [105, 105, 105]), - ("dodgerblue", [30, 144, 255]), - ("dodgerblue1", [30, 144, 255]), - ("dodgerblue2", [28, 134, 238]), - ("dodgerblue3", [24, 116, 205]), - ("dodgerblue4", [16, 78, 139]), - ("firebrick", [178, 34, 34]), - ("firebrick1", [255, 48, 48]), - ("firebrick2", [238, 44, 44]), - ("firebrick3", [205, 38, 38]), - ("firebrick4", [139, 26, 26]), - ("floralwhite", [255, 250, 240]), - ("forestgreen", [34, 139, 34]), - ("gainsboro", [220, 220, 220]), - ("ghostwhite", [248, 248, 255]), - ("gold", [255, 215, 0]), - ("gold1", [255, 215, 0]), - ("gold2", [238, 201, 0]), - ("gold3", [205, 173, 0]), - ("gold4", [139, 117, 0]), - ("goldenrod", [218, 165, 32]), - ("goldenrod1", [255, 193, 37]), - ("goldenrod2", [238, 180, 34]), - ("goldenrod3", [205, 155, 29]), - ("goldenrod4", [139, 105, 20]), - ("gray", [190, 190, 190]), - ("gray0", [0, 0, 0]), - ("gray1", [3, 3, 3]), - ("gray10", [26, 26, 26]), - ("gray100", [255, 255, 255]), - ("gray11", [28, 28, 28]), - ("gray12", [31, 31, 31]), - ("gray13", [33, 33, 33]), - ("gray14", [36, 36, 36]), - ("gray15", [38, 38, 38]), - ("gray16", [41, 41, 41]), - ("gray17", [43, 43, 43]), - ("gray18", [46, 46, 46]), - ("gray19", [48, 48, 48]), - ("gray2", [5, 5, 5]), - ("gray20", [51, 51, 51]), - ("gray21", [54, 54, 54]), - ("gray22", [56, 56, 56]), - ("gray23", [59, 59, 59]), - ("gray24", [61, 61, 61]), - ("gray25", [64, 64, 64]), - ("gray26", [66, 66, 66]), - ("gray27", [69, 69, 69]), - ("gray28", [71, 71, 71]), - ("gray29", [74, 74, 74]), - ("gray3", [8, 8, 8]), - ("gray30", [77, 77, 77]), - ("gray31", [79, 79, 79]), - ("gray32", [82, 82, 82]), - ("gray33", [84, 84, 84]), - ("gray34", [87, 87, 87]), - ("gray35", [89, 89, 89]), - ("gray36", [92, 92, 92]), - ("gray37", [94, 94, 94]), - ("gray38", [97, 97, 97]), - ("gray39", [99, 99, 99]), - ("gray4", [10, 10, 10]), - ("gray40", [102, 102, 102]), - ("gray41", [105, 105, 105]), - ("gray42", [107, 107, 107]), - ("gray43", [110, 110, 110]), - ("gray44", [112, 112, 112]), - ("gray45", [115, 115, 115]), - ("gray46", [117, 117, 117]), - ("gray47", [120, 120, 120]), - ("gray48", [122, 122, 122]), - ("gray49", [125, 125, 125]), - ("gray5", [13, 13, 13]), - ("gray50", [127, 127, 127]), - ("gray51", [130, 130, 130]), - ("gray52", [133, 133, 133]), - ("gray53", [135, 135, 135]), - ("gray54", [138, 138, 138]), - ("gray55", [140, 140, 140]), - ("gray56", [143, 143, 143]), - ("gray57", [145, 145, 145]), - ("gray58", [148, 148, 148]), - ("gray59", [150, 150, 150]), - ("gray6", [15, 15, 15]), - ("gray60", [153, 153, 153]), - ("gray61", [156, 156, 156]), - ("gray62", [158, 158, 158]), - ("gray63", [161, 161, 161]), - ("gray64", [163, 163, 163]), - ("gray65", [166, 166, 166]), - ("gray66", [168, 168, 168]), - ("gray67", [171, 171, 171]), - ("gray68", [173, 173, 173]), - ("gray69", [176, 176, 176]), - ("gray7", [18, 18, 18]), - ("gray70", [179, 179, 179]), - ("gray71", [181, 181, 181]), - ("gray72", [184, 184, 184]), - ("gray73", [186, 186, 186]), - ("gray74", [189, 189, 189]), - ("gray75", [191, 191, 191]), - ("gray76", [194, 194, 194]), - ("gray77", [196, 196, 196]), - ("gray78", [199, 199, 199]), - ("gray79", [201, 201, 201]), - ("gray8", [20, 20, 20]), - ("gray80", [204, 204, 204]), - ("gray81", [207, 207, 207]), - ("gray82", [209, 209, 209]), - ("gray83", [212, 212, 212]), - ("gray84", [214, 214, 214]), - ("gray85", [217, 217, 217]), - ("gray86", [219, 219, 219]), - ("gray87", [222, 222, 222]), - ("gray88", [224, 224, 224]), - ("gray89", [227, 227, 227]), - ("gray9", [23, 23, 23]), - ("gray90", [229, 229, 229]), - ("gray91", [232, 232, 232]), - ("gray92", [235, 235, 235]), - ("gray93", [237, 237, 237]), - ("gray94", [240, 240, 240]), - ("gray95", [242, 242, 242]), - ("gray96", [245, 245, 245]), - ("gray97", [247, 247, 247]), - ("gray98", [250, 250, 250]), - ("gray99", [252, 252, 252]), - ("green", [0, 255, 0]), - ("green1", [0, 255, 0]), - ("green2", [0, 238, 0]), - ("green3", [0, 205, 0]), - ("green4", [0, 139, 0]), - ("greenyellow", [173, 255, 47]), - ("grey", [190, 190, 190]), - ("grey0", [0, 0, 0]), - ("grey1", [3, 3, 3]), - ("grey10", [26, 26, 26]), - ("grey100", [255, 255, 255]), - ("grey11", [28, 28, 28]), - ("grey12", [31, 31, 31]), - ("grey13", [33, 33, 33]), - ("grey14", [36, 36, 36]), - ("grey15", [38, 38, 38]), - ("grey16", [41, 41, 41]), - ("grey17", [43, 43, 43]), - ("grey18", [46, 46, 46]), - ("grey19", [48, 48, 48]), - ("grey2", [5, 5, 5]), - ("grey20", [51, 51, 51]), - ("grey21", [54, 54, 54]), - ("grey22", [56, 56, 56]), - ("grey23", [59, 59, 59]), - ("grey24", [61, 61, 61]), - ("grey25", [64, 64, 64]), - ("grey26", [66, 66, 66]), - ("grey27", [69, 69, 69]), - ("grey28", [71, 71, 71]), - ("grey29", [74, 74, 74]), - ("grey3", [8, 8, 8]), - ("grey30", [77, 77, 77]), - ("grey31", [79, 79, 79]), - ("grey32", [82, 82, 82]), - ("grey33", [84, 84, 84]), - ("grey34", [87, 87, 87]), - ("grey35", [89, 89, 89]), - ("grey36", [92, 92, 92]), - ("grey37", [94, 94, 94]), - ("grey38", [97, 97, 97]), - ("grey39", [99, 99, 99]), - ("grey4", [10, 10, 10]), - ("grey40", [102, 102, 102]), - ("grey41", [105, 105, 105]), - ("grey42", [107, 107, 107]), - ("grey43", [110, 110, 110]), - ("grey44", [112, 112, 112]), - ("grey45", [115, 115, 115]), - ("grey46", [117, 117, 117]), - ("grey47", [120, 120, 120]), - ("grey48", [122, 122, 122]), - ("grey49", [125, 125, 125]), - ("grey5", [13, 13, 13]), - ("grey50", [127, 127, 127]), - ("grey51", [130, 130, 130]), - ("grey52", [133, 133, 133]), - ("grey53", [135, 135, 135]), - ("grey54", [138, 138, 138]), - ("grey55", [140, 140, 140]), - ("grey56", [143, 143, 143]), - ("grey57", [145, 145, 145]), - ("grey58", [148, 148, 148]), - ("grey59", [150, 150, 150]), - ("grey6", [15, 15, 15]), - ("grey60", [153, 153, 153]), - ("grey61", [156, 156, 156]), - ("grey62", [158, 158, 158]), - ("grey63", [161, 161, 161]), - ("grey64", [163, 163, 163]), - ("grey65", [166, 166, 166]), - ("grey66", [168, 168, 168]), - ("grey67", [171, 171, 171]), - ("grey68", [173, 173, 173]), - ("grey69", [176, 176, 176]), - ("grey7", [18, 18, 18]), - ("grey70", [179, 179, 179]), - ("grey71", [181, 181, 181]), - ("grey72", [184, 184, 184]), - ("grey73", [186, 186, 186]), - ("grey74", [189, 189, 189]), - ("grey75", [191, 191, 191]), - ("grey76", [194, 194, 194]), - ("grey77", [196, 196, 196]), - ("grey78", [199, 199, 199]), - ("grey79", [201, 201, 201]), - ("grey8", [20, 20, 20]), - ("grey80", [204, 204, 204]), - ("grey81", [207, 207, 207]), - ("grey82", [209, 209, 209]), - ("grey83", [212, 212, 212]), - ("grey84", [214, 214, 214]), - ("grey85", [217, 217, 217]), - ("grey86", [219, 219, 219]), - ("grey87", [222, 222, 222]), - ("grey88", [224, 224, 224]), - ("grey89", [227, 227, 227]), - ("grey9", [23, 23, 23]), - ("grey90", [229, 229, 229]), - ("grey91", [232, 232, 232]), - ("grey92", [235, 235, 235]), - ("grey93", [237, 237, 237]), - ("grey94", [240, 240, 240]), - ("grey95", [242, 242, 242]), - ("grey96", [245, 245, 245]), - ("grey97", [247, 247, 247]), - ("grey98", [250, 250, 250]), - ("grey99", [252, 252, 252]), - ("honeydew", [240, 255, 240]), - ("honeydew1", [240, 255, 240]), - ("honeydew2", [224, 238, 224]), - ("honeydew3", [193, 205, 193]), - ("honeydew4", [131, 139, 131]), - ("hotpink", [255, 105, 180]), - ("hotpink1", [255, 110, 180]), - ("hotpink2", [238, 106, 167]), - ("hotpink3", [205, 96, 144]), - ("hotpink4", [139, 58, 98]), - ("indianred", [205, 92, 92]), - ("indianred1", [255, 106, 106]), - ("indianred2", [238, 99, 99]), - ("indianred3", [205, 85, 85]), - ("indianred4", [139, 58, 58]), - ("ivory", [255, 255, 240]), - ("ivory1", [255, 255, 240]), - ("ivory2", [238, 238, 224]), - ("ivory3", [205, 205, 193]), - ("ivory4", [139, 139, 131]), - ("khaki", [240, 230, 140]), - ("khaki1", [255, 246, 143]), - ("khaki2", [238, 230, 133]), - ("khaki3", [205, 198, 115]), - ("khaki4", [139, 134, 78]), - ("lavender", [230, 230, 250]), - ("lavenderblush", [255, 240, 245]), - ("lavenderblush1", [255, 240, 245]), - ("lavenderblush2", [238, 224, 229]), - ("lavenderblush3", [205, 193, 197]), - ("lavenderblush4", [139, 131, 134]), - ("lawngreen", [124, 252, 0]), - ("lemonchiffon", [255, 250, 205]), - ("lemonchiffon1", [255, 250, 205]), - ("lemonchiffon2", [238, 233, 191]), - ("lemonchiffon3", [205, 201, 165]), - ("lemonchiffon4", [139, 137, 112]), - ("lightblue", [173, 216, 230]), - ("lightblue1", [191, 239, 255]), - ("lightblue2", [178, 223, 238]), - ("lightblue3", [154, 192, 205]), - ("lightblue4", [104, 131, 139]), - ("lightcoral", [240, 128, 128]), - ("lightcyan", [224, 255, 255]), - ("lightcyan1", [224, 255, 255]), - ("lightcyan2", [209, 238, 238]), - ("lightcyan3", [180, 205, 205]), - ("lightcyan4", [122, 139, 139]), - ("lightgoldenrod", [238, 221, 130]), - ("lightgoldenrod1", [255, 236, 139]), - ("lightgoldenrod2", [238, 220, 130]), - ("lightgoldenrod3", [205, 190, 112]), - ("lightgoldenrod4", [139, 129, 76]), - ("lightgoldenrodyellow", [250, 250, 210]), - ("lightgray", [211, 211, 211]), - ("lightgreen", [144, 238, 144]), - ("lightgrey", [211, 211, 211]), - ("lightpink", [255, 182, 193]), - ("lightpink1", [255, 174, 185]), - ("lightpink2", [238, 162, 173]), - ("lightpink3", [205, 140, 149]), - ("lightpink4", [139, 95, 101]), - ("lightsalmon", [255, 160, 122]), - ("lightsalmon1", [255, 160, 122]), - ("lightsalmon2", [238, 149, 114]), - ("lightsalmon3", [205, 129, 98]), - ("lightsalmon4", [139, 87, 66]), - ("lightseagreen", [32, 178, 170]), - ("lightskyblue", [135, 206, 250]), - ("lightskyblue1", [176, 226, 255]), - ("lightskyblue2", [164, 211, 238]), - ("lightskyblue3", [141, 182, 205]), - ("lightskyblue4", [96, 123, 139]), - ("lightslateblue", [132, 112, 255]), - ("lightslategray", [119, 136, 153]), - ("lightslategrey", [119, 136, 153]), - ("lightsteelblue", [176, 196, 222]), - ("lightsteelblue1", [202, 225, 255]), - ("lightsteelblue2", [188, 210, 238]), - ("lightsteelblue3", [162, 181, 205]), - ("lightsteelblue4", [110, 123, 139]), - ("lightyellow", [255, 255, 224]), - ("lightyellow1", [255, 255, 224]), - ("lightyellow2", [238, 238, 209]), - ("lightyellow3", [205, 205, 180]), - ("lightyellow4", [139, 139, 122]), - ("limegreen", [50, 205, 50]), - ("linen", [250, 240, 230]), - ("magenta", [255, 0, 255]), - ("magenta1", [255, 0, 255]), - ("magenta2", [238, 0, 238]), - ("magenta3", [205, 0, 205]), - ("magenta4", [139, 0, 139]), - ("maroon", [176, 48, 96]), - ("maroon1", [255, 52, 179]), - ("maroon2", [238, 48, 167]), - ("maroon3", [205, 41, 144]), - ("maroon4", [139, 28, 98]), - ("mediumaquamarine", [102, 205, 170]), - ("mediumblue", [0, 0, 205]), - ("mediumorchid", [186, 85, 211]), - ("mediumorchid1", [224, 102, 255]), - ("mediumorchid2", [209, 95, 238]), - ("mediumorchid3", [180, 82, 205]), - ("mediumorchid4", [122, 55, 139]), - ("mediumpurple", [147, 112, 219]), - ("mediumpurple1", [171, 130, 255]), - ("mediumpurple2", [159, 121, 238]), - ("mediumpurple3", [137, 104, 205]), - ("mediumpurple4", [93, 71, 139]), - ("mediumseagreen", [60, 179, 113]), - ("mediumslateblue", [123, 104, 238]), - ("mediumspringgreen", [0, 250, 154]), - ("mediumturquoise", [72, 209, 204]), - ("mediumvioletred", [199, 21, 133]), - ("midnightblue", [25, 25, 112]), - ("mintcream", [245, 255, 250]), - ("mistyrose", [255, 228, 225]), - ("mistyrose1", [255, 228, 225]), - ("mistyrose2", [238, 213, 210]), - ("mistyrose3", [205, 183, 181]), - ("mistyrose4", [139, 125, 123]), - ("moccasin", [255, 228, 181]), - ("navajowhite", [255, 222, 173]), - ("navajowhite1", [255, 222, 173]), - ("navajowhite2", [238, 207, 161]), - ("navajowhite3", [205, 179, 139]), - ("navajowhite4", [139, 121, 94]), - ("navy", [0, 0, 128]), - ("navyblue", [0, 0, 128]), - ("oldlace", [253, 245, 230]), - ("olivedrab", [107, 142, 35]), - ("olivedrab1", [192, 255, 62]), - ("olivedrab2", [179, 238, 58]), - ("olivedrab3", [154, 205, 50]), - ("olivedrab4", [105, 139, 34]), - ("orange", [255, 165, 0]), - ("orange1", [255, 165, 0]), - ("orange2", [238, 154, 0]), - ("orange3", [205, 133, 0]), - ("orange4", [139, 90, 0]), - ("orangered", [255, 69, 0]), - ("orangered1", [255, 69, 0]), - ("orangered2", [238, 64, 0]), - ("orangered3", [205, 55, 0]), - ("orangered4", [139, 37, 0]), - ("orchid", [218, 112, 214]), - ("orchid1", [255, 131, 250]), - ("orchid2", [238, 122, 233]), - ("orchid3", [205, 105, 201]), - ("orchid4", [139, 71, 137]), - ("palegoldenrod", [238, 232, 170]), - ("palegreen", [152, 251, 152]), - ("palegreen1", [154, 255, 154]), - ("palegreen2", [144, 238, 144]), - ("palegreen3", [124, 205, 124]), - ("palegreen4", [84, 139, 84]), - ("paleturquoise", [175, 238, 238]), - ("paleturquoise1", [187, 255, 255]), - ("paleturquoise2", [174, 238, 238]), - ("paleturquoise3", [150, 205, 205]), - ("paleturquoise4", [102, 139, 139]), - ("palevioletred", [219, 112, 147]), - ("palevioletred1", [255, 130, 171]), - ("palevioletred2", [238, 121, 159]), - ("palevioletred3", [205, 104, 137]), - ("palevioletred4", [139, 71, 93]), - ("papayawhip", [255, 239, 213]), - ("peachpuff", [255, 218, 185]), - ("peachpuff1", [255, 218, 185]), - ("peachpuff2", [238, 203, 173]), - ("peachpuff3", [205, 175, 149]), - ("peachpuff4", [139, 119, 101]), - ("peru", [205, 133, 63]), - ("pink", [255, 192, 203]), - ("pink1", [255, 181, 197]), - ("pink2", [238, 169, 184]), - ("pink3", [205, 145, 158]), - ("pink4", [139, 99, 108]), - ("plum", [221, 160, 221]), - ("plum1", [255, 187, 255]), - ("plum2", [238, 174, 238]), - ("plum3", [205, 150, 205]), - ("plum4", [139, 102, 139]), - ("powderblue", [176, 224, 230]), - ("purple", [160, 32, 240]), - ("purple1", [155, 48, 255]), - ("purple2", [145, 44, 238]), - ("purple3", [125, 38, 205]), - ("purple4", [85, 26, 139]), - ("red", [255, 0, 0]), - ("red1", [255, 0, 0]), - ("red2", [238, 0, 0]), - ("red3", [205, 0, 0]), - ("red4", [139, 0, 0]), - ("rosybrown", [188, 143, 143]), - ("rosybrown1", [255, 193, 193]), - ("rosybrown2", [238, 180, 180]), - ("rosybrown3", [205, 155, 155]), - ("rosybrown4", [139, 105, 105]), - ("royalblue", [65, 105, 225]), - ("royalblue1", [72, 118, 255]), - ("royalblue2", [67, 110, 238]), - ("royalblue3", [58, 95, 205]), - ("royalblue4", [39, 64, 139]), - ("saddlebrown", [139, 69, 19]), - ("salmon", [250, 128, 114]), - ("salmon1", [255, 140, 105]), - ("salmon2", [238, 130, 98]), - ("salmon3", [205, 112, 84]), - ("salmon4", [139, 76, 57]), - ("sandybrown", [244, 164, 96]), - ("seagreen", [46, 139, 87]), - ("seagreen1", [84, 255, 159]), - ("seagreen2", [78, 238, 148]), - ("seagreen3", [67, 205, 128]), - ("seagreen4", [46, 139, 87]), - ("seashell", [255, 245, 238]), - ("seashell1", [255, 245, 238]), - ("seashell2", [238, 229, 222]), - ("seashell3", [205, 197, 191]), - ("seashell4", [139, 134, 130]), - ("sienna", [160, 82, 45]), - ("sienna1", [255, 130, 71]), - ("sienna2", [238, 121, 66]), - ("sienna3", [205, 104, 57]), - ("sienna4", [139, 71, 38]), - ("skyblue", [135, 206, 235]), - ("skyblue1", [135, 206, 255]), - ("skyblue2", [126, 192, 238]), - ("skyblue3", [108, 166, 205]), - ("skyblue4", [74, 112, 139]), - ("slateblue", [106, 90, 205]), - ("slateblue1", [131, 111, 255]), - ("slateblue2", [122, 103, 238]), - ("slateblue3", [105, 89, 205]), - ("slateblue4", [71, 60, 139]), - ("slategray", [112, 128, 144]), - ("slategray1", [198, 226, 255]), - ("slategray2", [185, 211, 238]), - ("slategray3", [159, 182, 205]), - ("slategray4", [108, 123, 139]), - ("slategrey", [112, 128, 144]), - ("snow", [255, 250, 250]), - ("snow1", [255, 250, 250]), - ("snow2", [238, 233, 233]), - ("snow3", [205, 201, 201]), - ("snow4", [139, 137, 137]), - ("springgreen", [0, 255, 127]), - ("springgreen1", [0, 255, 127]), - ("springgreen2", [0, 238, 118]), - ("springgreen3", [0, 205, 102]), - ("springgreen4", [0, 139, 69]), - ("steelblue", [70, 130, 180]), - ("steelblue1", [99, 184, 255]), - ("steelblue2", [92, 172, 238]), - ("steelblue3", [79, 148, 205]), - ("steelblue4", [54, 100, 139]), - ("tan", [210, 180, 140]), - ("tan1", [255, 165, 79]), - ("tan2", [238, 154, 73]), - ("tan3", [205, 133, 63]), - ("tan4", [139, 90, 43]), - ("thistle", [216, 191, 216]), - ("thistle1", [255, 225, 255]), - ("thistle2", [238, 210, 238]), - ("thistle3", [205, 181, 205]), - ("thistle4", [139, 123, 139]), - ("tomato", [255, 99, 71]), - ("tomato1", [255, 99, 71]), - ("tomato2", [238, 92, 66]), - ("tomato3", [205, 79, 57]), - ("tomato4", [139, 54, 38]), - ("turquoise", [64, 224, 208]), - ("turquoise1", [0, 245, 255]), - ("turquoise2", [0, 229, 238]), - ("turquoise3", [0, 197, 205]), - ("turquoise4", [0, 134, 139]), - ("violet", [238, 130, 238]), - ("violetred", [208, 32, 144]), - ("violetred1", [255, 62, 150]), - ("violetred2", [238, 58, 140]), - ("violetred3", [205, 50, 120]), - ("violetred4", [139, 34, 82]), - ("wheat", [245, 222, 179]), - ("wheat1", [255, 231, 186]), - ("wheat2", [238, 216, 174]), - ("wheat3", [205, 186, 150]), - ("wheat4", [139, 126, 102]), - ("white", [255, 255, 255]), - ("whitesmoke", [245, 245, 245]), - ("yellow", [255, 255, 0]), - ("yellow1", [255, 255, 0]), - ("yellow2", [238, 238, 0]), - ("yellow3", [205, 205, 0]), - ("yellow4", [139, 139, 0]), - ("yellowgreen", [154, 205, 50]), -]; diff --git a/crates/raster-codecs/tests/shared_contract.rs b/crates/raster-codecs/tests/shared_contract.rs new file mode 100644 index 000000000..390242a65 --- /dev/null +++ b/crates/raster-codecs/tests/shared_contract.rs @@ -0,0 +1,46 @@ +//! The compatibility facade must retain its policy while sharing the codec implementation. +use craft_raster_codecs::{ChannelLayout, CodecError, Image, Limits, SampleType, dds, fits, iff, pnm, xpm}; + +#[test] +fn facade_refuses_conversions_the_generic_encoder_can_plan() { + for layout in [ChannelLayout::GrayA, ChannelLayout::Rgba, ChannelLayout::Cmyk, ChannelLayout::CmykA] { + let image = Image::from_f32(1, 1, layout, &vec![0.5; layout.channels()]).unwrap(); + assert!(matches!(pnm::encode(&image), Err(CodecError::Encode { .. }))); + assert!(matches!(fits::encode(&image), Err(CodecError::Encode { .. }))); + } + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Cmyk, ChannelLayout::CmykA] { + let image = Image::from_u8(1, 1, layout, vec![127; layout.channels()]).unwrap(); + for compression in [dds::DdsCompression::Bc1, dds::DdsCompression::Bc2, dds::DdsCompression::Bc3] { + assert!(matches!(dds::encode(&image, dds::DdsOptions { compression, mipmaps: false }), Err(CodecError::Encode { .. }))); + } + } + for layout in [ChannelLayout::Rgb, ChannelLayout::Rgba] { + let image = Image::from_u16(1, 1, layout, &vec![32768; layout.channels()]).unwrap(); + assert!(matches!(iff::encode(&image), Err(CodecError::Encode { .. }))); + assert!(matches!(dds::encode(&image, dds::DdsOptions::default()), Err(CodecError::Encode { .. }))); + } +} + +#[test] +fn fits_facade_keeps_metadata_samples_and_explicit_limits() { + let mut image = Image::from_u16(2, 1, ChannelLayout::Gray, &[0, 65535]).unwrap(); + image.text.push(("OBJECT".into(), "shared codec".into())); + let bytes = fits::encode(&image).unwrap(); + let decoded = fits::decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(decoded.sample_type(), SampleType::U16); + assert_eq!(decoded.data(), image.data()); + assert!(decoded.text.contains(&("OBJECT".into(), "shared codec".into()))); + for limits in + [Limits { max_width: 1, ..Limits::default() }, Limits { max_pixels: 1, ..Limits::default() }, Limits { max_alloc: 3, ..Limits::default() }] + { + assert!(matches!(fits::decode(&bytes, &limits), Err(CodecError::LimitExceeded(_)))); + } +} + +#[test] +fn x11_table_lookup_does_not_adopt_xpm_aliases_or_normalization() { + assert_eq!(xpm::x11_color_rgb("red"), Some([1.0, 0.0, 0.0])); + for name in ["Red", "light blue", "light-blue", "light_blue", "none", "transparent", ""] { + assert_eq!(xpm::x11_color_rgb(name), None, "{name}"); + } +}