Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Merge origin/main into the DICOM reader; fix the review findings

Vikram Bhaskaran7b8e21662e34
diff --cc apps/photocraft-web/src/web.rs
index c4b89dd4e,8d7e8e082..ea3a8b564
--- a/apps/photocraft-web/src/web.rs
+++ b/apps/photocraft-web/src/web.rs
@@@ -14,13 -14,12 +14,12 @@@ use wasm_bindgen::JsCast as _
type Inbox = Arc<Mutex<Vec<(String, Vec<u8>)>>>;
- /// Everything File › Open reads: PhotoCraft, Photoshop, OpenRaster and Affinity documents, flat images, and
+ /// Everything File › Open reads: PhotoCraft, Photoshop, OpenRaster, Krita and Affinity documents, flat images, and
/// Photoshop brushes (.abr), gradients (.grd) and swatches (.aco, .ase), which go to the preset libraries.
const OPEN_EXTS: &[&str] = &[
- "pcraft", "pdn", "ora", "psd", "psb", "psdt", "png", "jpg", "jpeg", "tif", "tiff", "webp", "gif", "bmp", "tga", "ico", "qoi", "exr", "hdr", "pbm", "pgm",
- "ppm", "pam", "pfm", "pcx", "dcm", "dicom", "dc3", "fits", "fit", "fts", "heic", "heif", "hif", "dng", "cr2", "cr3", "nef", "nrw", "arw", "pef", "orf",
- "rw2", "raf", "abr", "grd",
- "svg", "svgz", "aco", "ase", "af", "afdesign", "afphoto", "afpub",
+ "pcraft", "pdn", "ora", "kra", "krz", "psd", "psb", "psdt", "png", "jpg", "jpeg", "tif", "tiff", "webp", "gif", "bmp", "tga", "ico", "qoi", "exr", "hdr",
- "pbm", "pgm", "ppm", "pam", "pfm", "dds", "fits", "fit", "fts", "dpx", "heic", "heif", "hif", "dng", "cr2", "cr3", "nef", "nrw", "arw", "pef", "orf",
++ "pbm", "pgm", "ppm", "pam", "pfm", "dds", "fits", "fit", "fts", "dpx", "dcm", "dicom", "dc3", "heic", "heif", "hif", "dng", "cr2", "cr3", "nef", "nrw", "arw", "pef", "orf",
+ "rw2", "raf", "abr", "grd", "svg", "svgz", "aco", "ase", "af", "afdesign", "afphoto", "afpub",
];
const SVG_EXTS: &[&str] = &["svg", "svgz"];
const CANVAS_ID: &str = "photocraft_canvas";
diff --cc apps/photocraft/src/services.rs
index 107b6324f,e67f13ae4..5bcf45443
--- a/apps/photocraft/src/services.rs
+++ b/apps/photocraft/src/services.rs
@@@ -20,10 -20,9 +20,9 @@@ mod recovery_tests
/// Photoshop brushes (.abr), gradients (.grd), swatches (.aco, .ase) and keyboard shortcut sets
/// (.kys), which go to the preset libraries and Edit › Keyboard Shortcuts.
const OPEN_EXTS: &[&str] = &[
- "pcraft", "pdn", "ora", "kra", "psd", "psb", "psdt", "png", "jpg", "jpeg", "tif", "tiff", "webp", "gif", "bmp", "tga", "ico", "qoi", "exr", "hdr", "pbm",
- "pgm", "ppm", "pam", "pfm", "pcx", "dcm", "dicom", "dc3", "fits", "fit", "fts", "heic", "heif", "hif", "dng", "cr2", "cr3", "nef", "nrw", "arw", "pef",
- "orf", "rw2", "raf",
- "abr", "grd", "svg", "svgz", "aco", "ase", "kys", "af", "afdesign", "afphoto", "afpub",
+ "pcraft", "pdn", "ora", "kra", "krz", "psd", "psb", "psdt", "png", "jpg", "jpeg", "tif", "tiff", "webp", "gif", "bmp", "tga", "ico", "qoi", "exr", "hdr",
- "pbm", "pgm", "ppm", "pam", "pfm", "pcx", "dds", "xpm", "fits", "fit", "fts", "dpx", "heic", "heif", "hif", "dng", "cr2", "cr3", "nef", "nrw", "arw",
++ "pbm", "pgm", "ppm", "pam", "pfm", "pcx", "dds", "xpm", "fits", "fit", "fts", "dpx", "dcm", "dicom", "dc3", "heic", "heif", "hif", "dng", "cr2", "cr3", "nef", "nrw", "arw",
+ "pef", "orf", "rw2", "raf", "abr", "grd", "svg", "svgz", "aco", "ase", "kys", "af", "afdesign", "afphoto", "afpub",
];
const SVG_EXTS: &[&str] = &["svg", "svgz"];
diff --cc crates/codecs/README.md
index 42edd522d,2409c46e0..51f51c3f0
--- a/crates/codecs/README.md
+++ b/crates/codecs/README.md
@@@ -45,7 -46,7 +46,8 @@@ let out = encode(&img, Format::Tiff, &E
| OpenEXR | yes | yes | F16, F32 | Y, YA, RGB, RGBA | yes | no | no | no | no | no | no | `exr` |
| Radiance HDR | yes | yes | F32 | RGB | no | no | no | no | no | no | yes (RGBE) | `image` |
| FITS | yes | yes | U8, U16, F32 | Gray, RGB | no | no | no | no | no | yes (header cards) | no | built in |
+ | DPX | yes | yes | U8, U16 (10/12-bit code values read and written exactly through U16) | Gray, RGB, RGBA | yes | no | no | no | no | DPX header fields only (creator, project, copyright, creation time, transfer, colorimetric, frame position, frame rate, time code) | no | built in |
+| DICOM | yes | **no** | U8, U16 | Gray, RGB | no | no | no | no | no | no | n/a | built in |
| AVIF | **no** | only with feature `avif` | U8 | RGB, RGBA | yes | no | no | no | no | no | yes | `image` + `ravif` |
| HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` |
@@@ -75,18 -82,23 +83,32 @@@ Palette BMP pixels decode as RGB in `Im
kept as text (`HIERARCH` for long names). Tile-compressed (fpack) files are refused with
`CodecError::Unsupported`; Bayer mosaics open undebayered.
- * **Camera raw files** (DNG, CR2, NEF, ARW… which are TIFF-structured) are recognised and refused
- with `CodecError::Unsupported`: they are sensor data, not flat images. `photocraft-raw` decodes
- and develops them, and `photocraft-io` routes them there.
+ * **DPX.** SMPTE 268M frames in either byte order (`SDPX`, `XPDS`): the first image element,
+ descriptor 6 (luma), 50 (RGB) or 51 (RGBA), unsigned and unencoded, at 8, 10, 12 or 16 bits.
+ 10-bit data may be packed (0) or filled three to a word (1, method A; 2, method B), 12-bit data
+ packed (0) or one to a 16-bit word (1, method A). 10- and 12-bit code values open as U16
+ `round(c × 65535 / (2^b − 1))`, so a frame written back at its depth keeps them exactly. No
+ colour transform is applied: `DecodeWarning::DpxEncoding` names the transfer characteristic and
+ colorimetric specification, so a log frame is reported as log. More elements than the first
+ give `DecodeWarning::MoreElements`; the image orientation is applied on import. Writing is
+ big-endian, one unencoded element, at `EncodeOptions::dpx_bits` (8/10/12/16; by default 8 for
+ U8 and 16 otherwise), packing 1 for 10 and 12 bits; gray with alpha is written as luma.
+ Other descriptors, signed or run-length-encoded data and other bit depths return
+ `CodecError::Unsupported` naming the field. Cineon (`.cin`) is not supported.
+* **DICOM (read-only).** Single-frame medical images over the uncompressed transfer syntaxes
+ (implicit VR little-endian, explicit VR little/big-endian): MONOCHROME1/2 gray (with the
- 1 = white inversion) and RGB/YBR_FULL (8-bit), 8- and 16-bit allocated samples, interleaved or
- planar, scaled from `BitsStored`/`HighBit` and mapped up when `PixelRepresentation` is signed.
- Compressed transfer syntaxes, sequences, overlays, multi-frame pixel data and other photometric
++ 1 = white inversion) and RGB/YBR_FULL, 8- and 16-bit allocated samples, interleaved or planar,
++ scaled from `BitsStored`/`HighBit` and mapped up when `PixelRepresentation` is signed.
++ Undefined-length sequences outside the pixel data are skipped, as clinical files contain them.
++ Compressed transfer syntaxes, sequences over the pixel data and other photometric
+ interpretations return `Unsupported`/`Malformed`. There is no DICOM writer
+ (`ASYMMETRIC_EXCEPTIONS`).
+
+ * **Camera raw files** (DNG, CR2, NEF, ARW… which are TIFF-structured) are recognised and refused
+ with `CodecError::Unsupported`: they are sensor data, not flat images. `photocraft-raw` decodes
+ and develops them, and `photocraft-io` routes them there.
+
* **HEIF/HEIC (read-only, feature `heif`).** Decoding lives in the optional `photocraft-heif` crate
behind the non-default `heif` feature (official PhotoCraft builds enable it; distributors choose).
Without it, HEIF is still detected and decoding returns `CodecError::Unsupported` ("HEIC/HEIF
diff --cc crates/codecs/src/codecs/dicom.rs
index e86142b2a,000000000..ef4717758
mode 100644,000000..100644
--- a/crates/codecs/src/codecs/dicom.rs
+++ b/crates/codecs/src/codecs/dicom.rs
@@@ -1,364 -1,0 +1,505 @@@
+//! DICOM (medical imaging) reader, uncompressed transfer syntaxes only.
+//!
- //! Supports what a viewer needs from a single-frame image: MONOCHROME1/2 (gray) and RGB,
- //! 8- and 16-bit samples, interleaved or planar, both byte orders and explicit/implicit VR.
- //! Compressed transfer syntaxes, sequences, overlays and multi-frame pixel data are refused
- //! with a clear error. Read-only in this crate.
++//! 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.
+
+use std::collections::HashMap;
+
+use crate::error::CodecError;
+use crate::format::Format;
+use crate::image::{ChannelLayout, Image, SampleType};
+use crate::options::Limits;
+
+const F: Format = Format::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)
+}
+
+/// A DICOM file begins with a 128-byte preamble and the `DICM` magic.
+pub(crate) fn is_dicom(b: &[u8]) -> bool {
+ b.get(128..132) == Some(b"DICM")
+}
+
+#[derive(Clone, Copy)]
+struct Endian {
+ big: bool,
+}
+
- /// The transfer syntax's byte order and whether value representations are explicit.
++/// 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 data set.
++/// 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<u16> {
+ 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<u32> {
+ 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(())
++ }
+}
+
- /// A parsed element: its VR and the byte range of its value.
- struct Elem {
- vr: [u8; 2],
- start: usize,
- len: usize,
++/// 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))
+}
+
- /// Walk the elements from `start`, calling `f` with each `(group, element, value)`. Undefined
- /// lengths (sequences) are refused, keeping the decoder single-frame and bounded.
++/// 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<usize, CodecError> {
+ let mut c = Cur { b, pos: start, big: syn.endian.big };
+ loop {
- let Some(group) = c.u16() else { return Ok(c.pos - start) };
- let elem = c.u16().ok_or_else(|| bad("truncated element tag"))?;
- let (vr, len) = if syn.explicit {
- let vr = c.take(2).ok_or_else(|| bad("truncated VR"))?;
- let vr2 = [vr[0], vr[1]];
- let len = if long_vr(&vr2) {
- 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"))?)
- };
- (vr2, len)
- } else {
- ([0, 0], c.u32().ok_or_else(|| bad("truncated length"))?)
- };
- if len == 0xFFFF_FFFF {
- return Err(CodecError::unsupported(F, "undefined-length (sequence) elements"));
++ 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("element value runs past the end of the file"));
++ 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<u16> {
- let c = b.get(e.start..e.start + 2)?;
++ 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). The file meta group is always
- /// explicit VR little-endian; the data set follows its `(0002,0010)` TransferSyntaxUID.
++/// 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<String> = None;
+ let mut data_start = b.len();
+ let mut c = Cur { b, pos: PREAMBLE, big: false };
- loop {
++ while c.pos + 8 <= b.len() {
+ let save = c.pos;
- let Some(group) = c.u16() else { break };
- let _ = c.u16().ok_or_else(|| bad("truncated element tag"))?;
++ let (group, elem, _, len) = read_header(&mut c, &meta)?;
+ if group != 0x0002 {
+ // A non-meta group begins the data set.
+ data_start = save;
+ break;
+ }
- let vr = c.take(2).ok_or_else(|| bad("truncated VR"))?;
- let vr2 = [vr[0], vr[1]];
- let len = if long_vr(&vr2) {
- c.take(2).ok_or_else(|| bad("truncated VR"))?;
- c.u32().ok_or_else(|| bad("truncated length"))?
- } else {
- u32::from(c.u16().ok_or_else(|| bad("truncated length"))?)
- };
- if len == 0xFFFF_FFFF {
++ 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("meta element runs past the end of the file"));
++ return Err(bad("a meta element runs past the end of the file"));
+ }
- if (group, 0x0010) == (0x0002, 0x0010) {
++ 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 _ = elements(b, PREAMBLE, &meta, |_, _, _| Ok(()))?; // validate the meta group parses
+ 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(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
+ 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 = get_u16((0x0028, 0x0010)).ok_or_else(|| bad("missing Rows (0028,0010)"))?;
- let cols = get_u16((0x0028, 0x0011)).ok_or_else(|| bad("missing Columns (0028,0011)"))?;
- let samples = get_u16((0x0028, 0x0002)).unwrap_or(1);
++ 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));
+ 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 + e.len))
++ .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 (0028,0004/7FE0,0010)"))?;
++ 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" || photometric == "YBR_FULL_422";
++ let ybr = photometric == "YBR_FULL";
+ if !(mono || rgb || ybr) {
+ return Err(CodecError::unsupported(F, format!("photometric interpretation {photometric}")));
+ }
- if mono && samples != 1 {
++ // 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 || samples != 3) {
- return Err(CodecError::unsupported(F, format!("{bits}-bit {photometric} ({samples} samples)")));
++ 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(u32::from(cols), u32::from(rows), layout, out_sample)?;
- let n = usize::from(cols) * usize::from(rows) * usize::from(samples);
- let bytes_per = if bits == 16 { 2 } else { 1 };
- let need = n.checked_mul(bytes_per).ok_or_else(|| bad("pixel size overflow"))?;
- let data = b.get(pixel.start..pixel.start + need).ok_or_else(|| bad("pixel data is truncated"))?;
++ 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(usize::from(cols) * usize::from(rows) * out_channels * bytes_per)
- .map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?;
- // Sample `i` of `samples` channels at pixel `p`, honouring the planar configuration.
- let sample_at = |p: usize, ch: usize, s: usize| -> u32 {
- let idx = if planar == 1 { ch * usize::from(cols) * usize::from(rows) + p } else { s + p * usize::from(samples) + ch };
++ 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<u32> {
++ 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 off = idx * 2;
- let c = [data[off], data[off + 1]];
- u32::from(if big { u16::from_be_bytes(c) } else { u16::from_le_bytes(c) })
++ 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 {
- u32::from(data[idx])
++ 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<u8>, x: f64| {
+ if bits == 8 {
+ out.push((x * 255.0).round() as u8);
+ } else {
+ out.extend_from_slice(&q(x).to_ne_bytes());
+ }
+ };
- let px = usize::from(cols) * usize::from(rows);
- for p in 0..px {
++ 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(sample_at(p, 0, 0));
++ 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(sample_at(p, ch, ch)));
++ push(&mut out, norm(s(ch)?));
+ }
+ } else {
+ // YBR_FULL (8-bit): BT.601 full-range to RGB.
- let y = sample_at(p, 0, 0) as f64;
- let cb = sample_at(p, 1, 1) as f64 - 128.0;
- let cr = sample_at(p, 2, 2) as f64 - 128.0;
++ 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(u32::from(cols), u32::from(rows), layout, out_sample, out)
++ Image::from_raw(cols, rows, layout, out_sample, out)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
- /// A little-endian explicit-VR DICOM with meta group and one frame.
- fn synth(rows: u16, cols: u16, samples: u16, bits: u16, photometric: &str, pixels: &[u8]) -> Vec<u8> {
- fn us(b: &mut Vec<u8>, g: u16, e: u16, v: u16) {
- b.extend_from_slice(&g.to_le_bytes());
- b.extend_from_slice(&e.to_le_bytes());
- b.extend_from_slice(b"US");
- b.extend_from_slice(&2u16.to_le_bytes());
- b.extend_from_slice(&v.to_le_bytes());
- }
- fn cs(b: &mut Vec<u8>, g: u16, e: u16, v: &str) {
++ /// Build the file meta group (explicit VR little-endian), including the given extra
++ /// `(element, ascii)` pairs before the TransferSyntaxUID.
++ fn meta(b: &mut Vec<u8>, ts: &str, extra: &[(u16, &str)]) {
++ let ui = |e: u16, v: &str, b: &mut Vec<u8>| {
+ 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(&0x0002u16.to_le_bytes());
+ b.extend_from_slice(&e.to_le_bytes());
- b.extend_from_slice(b"CS");
++ 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<u8>, 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<u8>, 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<u8> {
+ let mut b = vec![0u8; 128];
+ b.extend_from_slice(b"DICM");
- // File meta group: TransferSyntaxUID explicit VR LE => explicit VR little endian.
- let ts = b"1.2.840.10008.1.2.1\0";
- b.extend_from_slice(&0x0002u16.to_le_bytes());
- b.extend_from_slice(&0x0010u16.to_le_bytes());
- b.extend_from_slice(b"UI");
- b.extend_from_slice(&(ts.len() as u16).to_le_bytes());
- b.extend_from_slice(ts);
- // Data set.
- us(&mut b, 0x0028, 0x0010, rows);
- us(&mut b, 0x0028, 0x0011, cols);
- us(&mut b, 0x0028, 0x0002, samples);
- cs(&mut b, 0x0028, 0x0004, photometric);
- us(&mut b, 0x0028, 0x0100, bits);
- us(&mut b, 0x0028, 0x0101, bits);
- us(&mut b, 0x0028, 0x0102, bits - 1);
- us(&mut b, 0x0028, 0x0103, 0);
- // Pixel data (OB, long form).
++ // 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());
- b.extend_from_slice(b"OB");
- b.extend_from_slice(&0u16.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(2, 2, 1, 8, "MONOCHROME2", &[0, 51, 102, 255]);
++ 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<u8> = [0u16, 32768, 65535].iter().flat_map(|v| v.to_le_bytes()).collect();
- let b = synth(3, 1, 1, 16, "MONOCHROME2", &px);
++ 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(2, 1, 1, 8, "MONOCHROME1", &[0, 255]);
++ 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(2, 2, 1, 8, "MONOCHROME2", &[0, 1, 2, 3]);
++ 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 rejects_compressed_transfer_syntax() {
- let mut b = synth(1, 1, 1, 8, "MONOCHROME2", &[0]);
++ 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 { .. })));
+ }
+}
diff --cc crates/codecs/src/codecs/mod.rs
index 8b9e024d0,b0b3ca46e..752b967a1
--- a/crates/codecs/src/codecs/mod.rs
+++ b/crates/codecs/src/codecs/mod.rs
@@@ -1,5 -1,6 +1,7 @@@
+ pub(crate) mod dds;
pub(crate) mod deep_exr;
+pub(crate) mod dicom;
+ pub(crate) mod dpx;
pub(crate) mod exr;
pub(crate) mod exr_cryptomatte;
pub(crate) mod fits;
diff --cc crates/codecs/src/format.rs
index 9aad8a35d,dca235f80..de6ecafb2
--- a/crates/codecs/src/format.rs
+++ b/crates/codecs/src/format.rs
@@@ -27,8 -31,8 +31,10 @@@ pub enum Format
Heif,
/// FITS (Flexible Image Transport System), the astronomy format: gray or RGB image data.
Fits,
+ /// DPX (SMPTE 268M Digital Picture Exchange): luma, RGB or RGBA film and video frames.
+ Dpx,
+ /// DICOM single-frame images (uncompressed): read-only.
+ Dicom,
}
/// What a format can hold **and** what this crate reads/writes for it.
@@@ -60,6 -64,7 +66,11 @@@ pub struct FormatCaps
/// Formats that are enabled but intentionally not symmetric, with the reason.
/// The test-suite asserts that every other enabled format is read+write.
pub const ASYMMETRIC_EXCEPTIONS: &[(Format, &str)] = &[
+ (Format::Xpm, "XPM is a legacy Unix icon interchange format. Reading existing XPM icons is supported; writing modern image assets should use PNG instead."),
++ (
++ Format::Dicom,
++ "DICOM single-frame images are imported for medical review; this crate has no DICOM writer, so save an imported frame as TIFF or PNG instead.",
++ ),
(
Format::Pcx,
"PCX is supported for opening legacy paint and clipart assets. New files should use PNG; writing obsolete indexed and planar variants is intentionally not provided.",
@@@ -90,7 -95,7 +101,7 @@@ const ALL_LAYOUTS: &[ChannelLayout] = &
impl Format {
/// Every format known to the crate (enabled or not).
- pub const ALL: [Format; 17] = [
- pub const ALL: [Format; 19] = [
++ pub const ALL: [Format; 20] = [
Format::Png,
Format::Jpeg,
Format::Tiff,
@@@ -107,7 -114,7 +120,8 @@@
Format::Avif,
Format::Heif,
Format::Fits,
+ Format::Dpx,
+ Format::Dicom,
];
pub fn caps(self) -> FormatCaps {
@@@ -138,7 -147,7 +154,8 @@@
Format::Avif => "AVIF",
Format::Heif => "HEIF",
Format::Fits => "FITS",
+ Format::Dpx => "DPX",
+ Format::Dicom => "DICOM",
}
}
@@@ -162,7 -173,7 +181,8 @@@
Format::Avif => &["avif"],
Format::Heif => &["heic", "heif", "hif"],
Format::Fits => &["fits", "fit", "fts"],
+ Format::Dpx => &["dpx"],
+ Format::Dicom => &["dcm", "dicom", "dc3"],
}
}
@@@ -184,7 -197,7 +206,8 @@@
Format::Avif => "image/avif",
Format::Heif => "image/heif",
Format::Fits => "image/fits",
+ Format::Dpx => "image/x-dpx",
+ Format::Dicom => "application/dicom",
}
}
@@@ -236,7 -251,8 +261,9 @@@ pub fn caps(format: Format) -> FormatCa
FormatCaps { read: cfg!(feature = "heif"), write: false, depths: &[S::U8, S::U16], icc: true, exif: true, xmp: true, lossy: true, ..base }
}
Format::Fits => FormatCaps { depths: &[S::U8, S::U16, S::F32], layouts: &[L::Gray, L::Rgb], alpha: false, text: true, ..base },
+ // `text`: the DPX header fields only (see `fidelity_warnings_with`).
+ Format::Dpx => FormatCaps { depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb, L::Rgba], text: true, ..base },
+ Format::Dicom => FormatCaps { read: true, write: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base },
}
}
@@@ -285,12 -304,15 +315,18 @@@ pub fn detect(bytes: &[u8]) -> Option<F
if b.len() >= 3 && b[0] == b'P' && matches!(b[1], b'1'..=b'7' | b'F' | b'f') && b[2].is_ascii_whitespace() {
return Some(Format::Pnm);
}
- if b.starts_with(b"SIMPLE =") {
- return Some(Format::Fits);
+ if b.starts_with(b"SDPX") || b.starts_with(b"XPDS") {
+ return Some(Format::Dpx);
}
+ if crate::codecs::dicom::is_dicom(b) {
+ return Some(Format::Dicom);
+ }
+ if b.starts_with(b"SIMPLE =") {
+ return Some(Format::Fits);
+ }
+ if b.starts_with(b"/* XPM */") || b.starts_with(b"! XPM2\n") || b.starts_with(b"! XPM2\r\n") {
+ return Some(Format::Xpm);
+ }
if crate::codecs::pcx::looks_like_pcx(b) {
return Some(Format::Pcx);
}
diff --cc crates/codecs/src/lib.rs
index 2424dcde7,ec663ace7..bef7623d1
--- a/crates/codecs/src/lib.rs
+++ b/crates/codecs/src/lib.rs
@@@ -43,7 -43,7 +43,7 @@@ pub use crate::resolution::{exif_resolu
pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding};
pub use half::f16;
- use crate::codecs::{dicom, exr, fits, heif, jpeg, pcx, png, pnm, tiff, via_image, webp};
-use crate::codecs::{dds, dpx, exr, fits, heif, jpeg, pcx, png, pnm, tiff, via_image, webp, xpm};
++use crate::codecs::{dds, dicom, dpx, exr, fits, heif, jpeg, pcx, png, pnm, tiff, via_image, webp, xpm};
/// Detect the format and decode with default [`Limits`].
pub fn decode(bytes: &[u8]) -> Result<Image, CodecError> {
@@@ -75,8 -75,10 +75,11 @@@ pub fn decode_as_with(format: Format, b
Format::WebP => webp::decode(bytes, l),
Format::Pnm => pnm::decode(bytes, l),
Format::Pcx => pcx::decode(bytes, l),
- Format::Dicom => dicom::decode(bytes, l),
+ Format::Xpm => xpm::decode(bytes, l),
+ Format::Dds => dds::decode(bytes, l),
Format::Fits => fits::decode(bytes, l),
+ Format::Dpx => dpx::decode(bytes, l),
++ Format::Dicom => dicom::decode(bytes, l),
Format::OpenExr => exr::decode(bytes, l),
Format::Heif => heif::decode(bytes, l, opts.keep_orientation),
Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::decode(format, bytes, l),
@@@ -224,8 -227,10 +228,11 @@@ pub fn encode(image: &Image, format: Fo
Format::WebP => webp::encode(image, plan, opts),
Format::Pnm => pnm::encode(image, plan, opts),
Format::Pcx => Err(CodecError::unsupported(format, "PCX is import-only; save as PNG instead")),
- Format::Dicom => Err(CodecError::unsupported(format, "DICOM is import-only; save as TIFF or PNG instead")),
+ Format::Xpm => Err(CodecError::unsupported(format, "XPM is import-only; save as PNG instead")),
+ Format::Dds => Err(CodecError::unsupported(format, "DDS is import-only; save as PNG instead")),
Format::Fits => fits::encode(image, plan, opts),
+ Format::Dpx => dpx::encode(image, plan, opts),
++ Format::Dicom => Err(CodecError::unsupported(format, "DICOM is import-only; save as TIFF or PNG instead")),
Format::OpenExr => exr::encode(image, plan, opts),
Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")),
Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::encode(format, image, plan, opts),
diff --cc docs/file-format-parity.md
index 2c8beadbe,4cb21759e..74a159a25
--- a/docs/file-format-parity.md
+++ b/docs/file-format-parity.md
@@@ -43,8 -43,8 +43,8 @@@ XL are missing (gap G10). Remaining: 30
| Generic EPS / AI | R (rasterize) | no | no | |
| SVG | R (rasterize) | yes, as editable shape layers or a vector smart object | no | Better than Photoshop for editing; gradients approximated (FILE-215-9) |
| JPEG 2000 | R/W | no (#1047) | no | |
- | Cineon / DPX | R (DPX), R/W (CIN) | no (#1049) | no | |
+ | Cineon / DPX | R (DPX), R/W (CIN) | DPX: luma, RGB, RGBA at 8/10/12/16 bits, both byte orders (#1049); Cineon no | DPX: 8/10/12/16 bits (`dpxBits`, `--dpx-bits`); Cineon no | Writes DPX, which Photoshop only reads; header fields (creator, project, copyright, transfer, frame rate, time code…) round-trip; no Cineon, no RLE |
-| DICOM | R/W | no | no | |
+| DICOM | R/W | yes (#1048) | no | Read-only, uncompressed syntaxes: MONOCHROME1/2, RGB, YBR_FULL |
| PCX | R/W | no | no | |
| Pixar | R/W | no | no | |
| Amiga IFF | R/W | no | no | |