mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge remote-tracking branch 'origin/main' into switch-craft-kra-ora-xcf
# Conflicts: # Cargo.toml
This commit is contained in:
commit
66e204d45c
25 files changed
+434
-5565
No files matched your search
@@ -10,7 +10,6 @@ next to them.
|
||||
|
||||
| Path | Title | Author | Source | License |
|
||||
|---|---|---|---|---|
|
||||
| `crates/codecs/src/codecs/xpm/x11_colors.rs` | X11 `rgb.txt` color-name lookup table (normalized X.Org RGB values) | X.Org Foundation and X11 contributors | <https://cgit.freedesktop.org/xorg/app/rgb/tree/rgb.txt> | MIT (X.Org app/rgb) |
|
||||
| Embedded at build time from storytold/craft-i18n `locales/<code>/common.tsv` and `locales/<code>/photocraft.tsv` (revision in [`craft-i18n.rev`](craft-i18n.rev)) | UI translations (all languages) | craft-i18n and PhotoCraft contributors; per-language credits and provenance in [craft-i18n's ATTRIBUTION.md](https://github.com/storytold/craft-i18n/blob/main/ATTRIBUTION.md) | <https://github.com/storytold/craft-i18n> | MIT OR Apache-2.0; Copyright (c) 2026 ArtCraft Team and the craft-i18n contributors; notice in [NOTICE](NOTICE) |
|
||||
| `epaint_default_fonts::EGUI_ICONS` (build input; deflated into `OUT_DIR/egui_fonts/egui-icons.ttf.deflate`, registered by `crates/ui-egui/src/text/egui_fonts.rs`) | egui-icons, five-glyph subset of emoji-icon-font | John Slegers | [egui fork at 6d518fc](https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts), [original](https://github.com/jslegers/emoji-icon-font); egui subset, unmodified here | MIT; Copyright (c) 2014 John Slegers; full upstream notice in [NOTICE](NOTICE) |
|
||||
| `assets/fonts/Inter-Regular.ttf`, `Inter-Medium.ttf`, `Inter-SemiBold.ttf` | Inter 4.001 (UI font) | The Inter Project Authors (Rasmus Andersson) | <https://github.com/rsms/inter> | SIL OFL 1.1, [`assets/fonts/OFL-Inter.txt`](assets/fonts/OFL-Inter.txt) |
|
||||
|
||||
Generated
+43
@@ -1431,11 +1431,32 @@ name = "craft-docpath"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
|
||||
[[package]]
|
||||
name = "craft-dpx"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-exr"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
|
||||
dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-fonts-build"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
|
||||
[[package]]
|
||||
name = "craft-gif"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
|
||||
|
||||
[[package]]
|
||||
name = "craft-heif"
|
||||
version = "0.1.0"
|
||||
@@ -1541,6 +1562,23 @@ dependencies = [
|
||||
"winsafe",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-raster-codecs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
|
||||
dependencies = [
|
||||
"rayon",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-tga"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-ui"
|
||||
version = "0.1.0"
|
||||
@@ -5588,8 +5626,13 @@ dependencies = [
|
||||
name = "photocraft-codecs"
|
||||
version = "0.23.1"
|
||||
dependencies = [
|
||||
"craft-dpx",
|
||||
"craft-exr",
|
||||
"craft-gif",
|
||||
"craft-heif",
|
||||
"craft-jpeg",
|
||||
"craft-raster-codecs",
|
||||
"craft-tga",
|
||||
"exr",
|
||||
"flate2",
|
||||
"half",
|
||||
|
||||
+10
@@ -46,6 +46,16 @@ craft-ora = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b
|
||||
craft-xcf = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
|
||||
# Paint.NET PDN3 and its MS-NRBF serialization (craft-libs#968).
|
||||
craft-pdn = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
|
||||
# DDS, DICOM, FITS, IFF/ILBM, PCX, PNM and XPM: the shared decoders and encoders (craft-libs#880).
|
||||
# DPX and Cineon frames (craft-libs#887); the mapping onto images stays in crates/codecs.
|
||||
craft-dpx = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
|
||||
# The Targa writer (craft-libs#892).
|
||||
craft-tga = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
|
||||
# GIF blocks without decoding: the first frame's colour table, the frame walk (craft-libs#939).
|
||||
craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
|
||||
# OpenEXR channel naming and Cryptomatte IDs, manifests and streams (craft-libs#929).
|
||||
craft-exr = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
|
||||
craft-raster-codecs = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
|
||||
craft-print = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
|
||||
# HEIC/HEIF: the shared decoder (replaces crates/heif; craft-libs#784).
|
||||
craft-heif = { git = "https://github.com/storytold/craft-libs", rev = "081078bb1b44aeedfa5bef3d4f13d978dc77510a" }
|
||||
|
||||
@@ -33,6 +33,11 @@ zune-core = "0.5"
|
||||
jpeg-encoder = "0.6"
|
||||
# 12-bit and arithmetic-coded JPEG, which zune-jpeg does not decode.
|
||||
craft-jpeg.workspace = true
|
||||
craft-raster-codecs.workspace = true
|
||||
craft-dpx.workspace = true
|
||||
craft-tga.workspace = true
|
||||
craft-gif.workspace = true
|
||||
craft-exr.workspace = true
|
||||
# Pure-Rust modular JPEG XL lossless encoder.
|
||||
zune-jpegxl = { version = "=0.5.2", default-features = false, features = ["std"] }
|
||||
craft-heif = { workspace = true, optional = true }
|
||||
|
||||
@@ -1,20 +1,13 @@
|
||||
pub(crate) mod dds;
|
||||
pub(crate) mod dds_bc;
|
||||
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;
|
||||
pub(crate) mod gif_frames;
|
||||
pub(crate) mod gif_palette;
|
||||
pub(crate) mod heif;
|
||||
pub(crate) mod iff;
|
||||
pub(crate) mod jpeg;
|
||||
pub(crate) mod jxl;
|
||||
pub(crate) mod pcx;
|
||||
pub(crate) mod png;
|
||||
pub(crate) mod pnm;
|
||||
mod raster;
|
||||
pub(crate) mod tga;
|
||||
pub(crate) mod tiff;
|
||||
pub(crate) mod tiff_exif;
|
||||
@@ -22,4 +15,4 @@ pub(crate) mod tiff_ifd;
|
||||
pub(crate) mod via_image;
|
||||
pub(crate) mod vp8;
|
||||
pub(crate) mod webp;
|
||||
pub(crate) mod xpm;
|
||||
pub(crate) use raster::{dds, dicom, fits, iff, pcx, pnm, xpm};
|
||||
@@ -1,556 +0,0 @@
|
||||
//! 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.
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use super::dds_bc::{self, Block, Mask};
|
||||
use crate::SampleType;
|
||||
use crate::error::CodecError;
|
||||
use crate::fidelity::Plan;
|
||||
use crate::format::Format;
|
||||
use crate::image::{ChannelLayout, Image};
|
||||
use crate::options::{DdsCompression, EncodeOptions, Limits};
|
||||
|
||||
/// `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(Format::Dds, reason)
|
||||
}
|
||||
fn unsupported(reason: impl Into<String>) -> CodecError {
|
||||
CodecError::unsupported(Format::Dds, reason)
|
||||
}
|
||||
fn le32(b: &[u8], at: usize) -> Result<u32, CodecError> {
|
||||
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))
|
||||
}
|
||||
|
||||
/// Header-declared extra subresources cannot survive a single-image rewrite, even without presence flags.
|
||||
pub(crate) fn has_additional_images(bytes: &[u8]) -> Result<bool, CodecError> {
|
||||
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)
|
||||
}
|
||||
|
||||
/// 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<Option<Field>, 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<Field>; 4],
|
||||
},
|
||||
Bc1,
|
||||
Bc2,
|
||||
Bc3,
|
||||
}
|
||||
|
||||
impl Encoding {
|
||||
fn masks(bits: u32, masks: [u32; 4], gray: bool) -> Result<Encoding, CodecError> {
|
||||
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<u32>) -> 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<Encoding, CodecError> {
|
||||
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<Encoding, CodecError> {
|
||||
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(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
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(Format::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`, as [`crate::fidelity::plan`] chose it.
|
||||
fn new(compression: DdsCompression, layout: ChannelLayout) -> Result<Store, CodecError> {
|
||||
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<usize> {
|
||||
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<usize> {
|
||||
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))
|
||||
}
|
||||
|
||||
pub(crate) fn encode(image: &Image, plan: Plan, opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
|
||||
let img = image.converted(plan.layout, SampleType::U8);
|
||||
let pixels = img.as_u8().ok_or_else(|| encode_err("the image was not converted to 8 bits per channel"))?;
|
||||
let store = Store::new(opts.dds.compression, img.layout())?;
|
||||
let (width, height) = img.dimensions();
|
||||
let levels = if opts.dds.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::options::alloc_zeroed::<u8>(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());
|
||||
}
|
||||
};
|
||||
// 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);
|
||||
}
|
||||
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<Vec<u8>, 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::options::alloc_zeroed::<u8>(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)
|
||||
}
|
||||
@@ -1,368 +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::<f32>() / 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::<f32>().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::<f32>();
|
||||
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<Item = u8>| 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::<u32>();
|
||||
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]);
|
||||
}
|
||||
}
|
||||
@@ -1,517 +0,0 @@
|
||||
//! 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.
|
||||
|
||||
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: 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<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(())
|
||||
}
|
||||
}
|
||||
|
||||
/// 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<usize, CodecError> {
|
||||
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<u16> {
|
||||
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<String> = 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(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 = 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<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 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<u8>, 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<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(&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<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");
|
||||
// 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<u8> = [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 { .. })));
|
||||
}
|
||||
}
|
||||
+132
-770
@@ -1,15 +1,8 @@
|
||||
//! DPX (SMPTE 268M Digital Picture Exchange), implemented here from the published layout: the
|
||||
//! 1664-byte generic header (file and image information, image source), the 384-byte industry
|
||||
//! header (film and television), then the image data of each element.
|
||||
//! DPX (SMPTE 268M Digital Picture Exchange) and Kodak Cineon (`.cin`): the format code is
|
||||
//! craft-libs' `craft-dpx`. This maps its frames and header fields onto this crate's image:
|
||||
//!
|
||||
//! Reading:
|
||||
//!
|
||||
//! * Both byte orders: `SDPX` (big-endian) and `XPDS` (little-endian).
|
||||
//! * The first image element, descriptor 6 (luma), 50 (RGB) or 51 (RGBA), unsigned, without
|
||||
//! run-length encoding, at 8, 10, 12 or 16 bits. 10-bit samples are packed into 32-bit words
|
||||
//! (packing 0), or three to a word with the padding bits at the bottom (1, method A) or the
|
||||
//! top (2, method B); 12-bit samples are packed (0) or one to a 16-bit word in its top bits
|
||||
//! (1, method A). Lines start on a 32-bit word boundary.
|
||||
//! * The first image element, luma, RGB or RGBA (ABGR comes back as RGBA). Y'CbCr elements are
|
||||
//! not converted here and stay unsupported.
|
||||
//! * 8- and 16-bit code values become U8 and U16 samples unchanged; 10- and 12-bit ones become U16
|
||||
//! as `round(c × 65535 / (2^b − 1))`, so they come back exactly when written at their depth.
|
||||
//! * No colour transform: the transfer characteristic and colorimetric specification are reported
|
||||
@@ -18,11 +11,11 @@
|
||||
//! * Header fields a pipeline relies on travel as text metadata under the [`KEYS`] names, and a
|
||||
//! DPX export writes them back.
|
||||
//!
|
||||
//! Writing: one element, big-endian, unencoded, descriptor 6, 50 or 51; 8 and 16 bits with
|
||||
//! packing 0, 10 and 12 bits with packing 1 (method A). The bit depth follows the image
|
||||
//! (8 → 8, 16 → 16) unless [`EncodeOptions::dpx_bits`] picks one.
|
||||
//!
|
||||
//! Kodak Cineon (`.cin`), the format DPX grew from, is read and written here too (see its section).
|
||||
//! Writing: one element, big-endian, unencoded, luma, RGB or RGBA; 8 and 16 bits with packing 0,
|
||||
//! 10 and 12 bits with packing 1 (method A). The bit depth follows the image (8 → 8, 16 → 16)
|
||||
//! unless [`EncodeOptions::dpx_bits`] picks one. Cineon is written at 10 bits.
|
||||
|
||||
use craft_dpx::{Frame, Info, Kind, Layout};
|
||||
|
||||
use crate::Format;
|
||||
use crate::error::CodecError;
|
||||
@@ -31,29 +24,8 @@ use crate::image::{ChannelLayout, DecodeWarning, Image, SampleType};
|
||||
use crate::options::{DpxBits, EncodeOptions, Limits};
|
||||
|
||||
const F: Format = Format::Dpx;
|
||||
/// Generic plus industry header: where our image data starts.
|
||||
const HEADER_LEN: usize = 2048;
|
||||
const GENERIC_LEN: u32 = 1664;
|
||||
const INDUSTRY_LEN: u32 = 384;
|
||||
/// "Undefined" for 32-bit fields (also the bit pattern of an undefined float).
|
||||
const UNDEF32: u32 = u32::MAX;
|
||||
const UNDEF8: u8 = u8::MAX;
|
||||
/// Offset of the first image element and the size of one.
|
||||
const ELEMENT0: usize = 780;
|
||||
const ELEMENT_LEN: usize = 72;
|
||||
const MAX_ELEMENTS: u16 = 8;
|
||||
|
||||
// Header field offsets (generic file header, image information, film and TV headers).
|
||||
const CREATION_TIME: (usize, usize) = (136, 24);
|
||||
const CREATOR: (usize, usize) = (160, 100);
|
||||
const PROJECT: (usize, usize) = (260, 200);
|
||||
const COPYRIGHT: (usize, usize) = (460, 200);
|
||||
const ORIENTATION: usize = 768;
|
||||
const FRAME_POSITION: usize = 1712;
|
||||
const FRAME_RATE: usize = 1724;
|
||||
const TIME_CODE: usize = 1920;
|
||||
|
||||
/// Text metadata keys of the header fields kept through a DPX → document → DPX round trip.
|
||||
/// Header fields carried as text metadata.
|
||||
pub(crate) const KEY_CREATOR: &str = "DPX:Creator";
|
||||
pub(crate) const KEY_PROJECT: &str = "DPX:Project";
|
||||
pub(crate) const KEY_COPYRIGHT: &str = "DPX:Copyright";
|
||||
@@ -66,473 +38,126 @@ pub(crate) const KEY_TIME_CODE: &str = "DPX:TimeCode";
|
||||
pub(crate) const KEYS: [&str; 9] =
|
||||
[KEY_CREATOR, KEY_PROJECT, KEY_COPYRIGHT, KEY_CREATION_TIME, KEY_TRANSFER, KEY_COLORIMETRIC, KEY_FRAME_POSITION, KEY_FRAME_RATE, KEY_TIME_CODE];
|
||||
|
||||
fn err(msg: impl std::fmt::Display) -> CodecError {
|
||||
CodecError::malformed(F, msg)
|
||||
}
|
||||
|
||||
fn unsupported(msg: impl Into<String>) -> CodecError {
|
||||
CodecError::unsupported(F, msg)
|
||||
}
|
||||
|
||||
/// The file's byte order.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum Order {
|
||||
Big,
|
||||
Little,
|
||||
}
|
||||
|
||||
impl Order {
|
||||
fn bytes<const N: usize>(b: &[u8], at: usize) -> Option<[u8; N]> {
|
||||
b.get(at..at.checked_add(N)?)?.try_into().ok()
|
||||
}
|
||||
|
||||
fn u16(self, b: &[u8], at: usize) -> Option<u16> {
|
||||
let s = Self::bytes::<2>(b, at)?;
|
||||
Some(if self == Order::Big { u16::from_be_bytes(s) } else { u16::from_le_bytes(s) })
|
||||
}
|
||||
|
||||
fn u32(self, b: &[u8], at: usize) -> Option<u32> {
|
||||
let s = Self::bytes::<4>(b, at)?;
|
||||
Some(if self == Order::Big { u32::from_be_bytes(s) } else { u32::from_le_bytes(s) })
|
||||
}
|
||||
|
||||
/// A 32-bit field that may be undefined (all ones).
|
||||
fn defined_u32(self, b: &[u8], at: usize) -> Option<u32> {
|
||||
self.u32(b, at).filter(|&v| v != UNDEF32)
|
||||
}
|
||||
|
||||
fn f32(self, b: &[u8], at: usize) -> Option<f32> {
|
||||
self.defined_u32(b, at).map(f32::from_bits).filter(|v| v.is_finite())
|
||||
fn err(format: Format, e: craft_dpx::Error) -> CodecError {
|
||||
match e {
|
||||
craft_dpx::Error::NotDpx => CodecError::malformed(format, "missing the DPX or Cineon magic number"),
|
||||
craft_dpx::Error::Malformed(m) => CodecError::malformed(format, m),
|
||||
craft_dpx::Error::Unsupported(m) => CodecError::unsupported(format, m),
|
||||
craft_dpx::Error::LimitExceeded(m) => CodecError::LimitExceeded(m.into()),
|
||||
craft_dpx::Error::Encode(m) => CodecError::encode(format, m),
|
||||
}
|
||||
}
|
||||
|
||||
/// The byte order from the magic number, `None` when it is not DPX.
|
||||
fn order(b: &[u8]) -> Option<Order> {
|
||||
match b.get(..4)? {
|
||||
b"SDPX" => Some(Order::Big),
|
||||
b"XPDS" => Some(Order::Little),
|
||||
_ => None,
|
||||
fn dpx_limits(l: &Limits) -> craft_dpx::Limits {
|
||||
let mut out = craft_dpx::Limits::default();
|
||||
out.max_pixels = l.max_pixels;
|
||||
out
|
||||
}
|
||||
|
||||
/// The header, for a file of `kind`; anything else is malformed.
|
||||
fn info(bytes: &[u8], kind: Kind) -> Result<Info, CodecError> {
|
||||
let format = if kind == Kind::Dpx { F } else { Format::Cineon };
|
||||
if craft_dpx::sniff(bytes) != Some(kind) {
|
||||
return Err(CodecError::malformed(
|
||||
format,
|
||||
if kind == Kind::Dpx { "missing SDPX/XPDS magic number" } else { "missing the Cineon magic number (802A5FD7)" },
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// An ASCII header string: up to the first NUL, printable characters only, trimmed. `None` when
|
||||
/// empty or undefined (all ones).
|
||||
fn string(b: &[u8], (at, len): (usize, usize)) -> Option<String> {
|
||||
let raw = b.get(at..at.checked_add(len)?)?;
|
||||
let end = raw.iter().position(|&c| c == 0).unwrap_or(raw.len());
|
||||
let s: String = raw.get(..end)?.iter().filter(|c| (32..=126).contains(*c)).map(|&c| c as char).collect();
|
||||
Some(s.trim().to_string()).filter(|s| !s.is_empty())
|
||||
}
|
||||
|
||||
/// The descriptive fields of image element 0 that decoding needs.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct Element {
|
||||
signed: bool,
|
||||
descriptor: u8,
|
||||
transfer: u8,
|
||||
colorimetric: u8,
|
||||
bits: u8,
|
||||
packing: u16,
|
||||
encoding: u16,
|
||||
data_offset: usize,
|
||||
eol_padding: usize,
|
||||
}
|
||||
|
||||
struct Header {
|
||||
order: Order,
|
||||
width: u32,
|
||||
height: u32,
|
||||
elements: u16,
|
||||
element: Element,
|
||||
// The size is checked by the caller, against its own limits.
|
||||
let mut unlimited = craft_dpx::Limits::default();
|
||||
unlimited.max_pixels = u64::MAX;
|
||||
craft_dpx::probe(bytes, &unlimited).map_err(|e| err(format, e))
|
||||
}
|
||||
|
||||
/// The decoder opens only element zero; protect the other declared elements from flat saves.
|
||||
pub(crate) fn has_additional_images(bytes: &[u8]) -> Result<bool, CodecError> {
|
||||
Ok(parse(bytes)?.elements > 1)
|
||||
Ok(info(bytes, Kind::Dpx)?.elements > 1)
|
||||
}
|
||||
|
||||
fn parse(b: &[u8]) -> Result<Header, CodecError> {
|
||||
let o = order(b).ok_or_else(|| err("missing SDPX/XPDS magic number"))?;
|
||||
let missing = |what: &str| err(format!("the header ends before {what}"));
|
||||
let image_offset = o.u32(b, 4).ok_or_else(|| missing("the image data offset"))?;
|
||||
o.u16(b, ORIENTATION).ok_or_else(|| missing("the orientation"))?;
|
||||
let elements = o.u16(b, ORIENTATION + 2).ok_or_else(|| missing("the element count"))?;
|
||||
if !(1..=MAX_ELEMENTS).contains(&elements) {
|
||||
return Err(err(format!("invalid number of image elements {elements}")));
|
||||
}
|
||||
let width = o.u32(b, 772).ok_or_else(|| missing("the line length"))?;
|
||||
let height = o.u32(b, 776).ok_or_else(|| missing("the line count"))?;
|
||||
let e = ELEMENT0;
|
||||
let field = |at: usize| o.u32(b, e + at).ok_or_else(|| missing("image element 1"));
|
||||
let byte = |at: usize| b.get(e + at).copied().ok_or_else(|| missing("image element 1"));
|
||||
let half = |at: usize| o.u16(b, e + at).ok_or_else(|| missing("image element 1"));
|
||||
let data_offset = match field(28)? {
|
||||
0 | UNDEF32 => image_offset,
|
||||
v => v,
|
||||
};
|
||||
let eol_padding = match field(32)? {
|
||||
UNDEF32 => 0,
|
||||
v => v,
|
||||
};
|
||||
let element = Element {
|
||||
signed: field(0)? == 1,
|
||||
descriptor: byte(20)?,
|
||||
transfer: byte(21)?,
|
||||
colorimetric: byte(22)?,
|
||||
bits: byte(23)?,
|
||||
packing: half(24)?,
|
||||
encoding: half(26)?,
|
||||
data_offset: usize::try_from(data_offset).map_err(|_| err("image data offset out of range"))?,
|
||||
eol_padding: usize::try_from(eol_padding).map_err(|_| err("line padding out of range"))?,
|
||||
};
|
||||
Ok(Header { order: o, width, height, elements, element })
|
||||
}
|
||||
|
||||
/// The EXIF orientation (1–8) that turns the stored lines upright: the DPX orientation codes 0–7
|
||||
/// in SMPTE 268M order (left-to-right top-to-bottom, mirrored, flipped, rotated, transposed…).
|
||||
/// Anything else, undefined included, is taken as the default (1).
|
||||
/// The EXIF orientation (1–8) that turns the stored lines upright; 1 for an unreadable header.
|
||||
pub(crate) fn orientation(b: &[u8]) -> u16 {
|
||||
let Some(o) = order(b).and_then(|o| o.u16(b, ORIENTATION)) else { return 1 };
|
||||
match o {
|
||||
1 => 2,
|
||||
2 => 4,
|
||||
3 => 3,
|
||||
4 => 5,
|
||||
5 => 6,
|
||||
6 => 8,
|
||||
7 => 7,
|
||||
_ => 1,
|
||||
}
|
||||
info(b, Kind::Dpx).map_or(1, |i| i.exif_orientation())
|
||||
}
|
||||
|
||||
/// Bytes in one stored line of `samples` samples, padded to a 32-bit word.
|
||||
fn line_bytes(samples: usize, bits: u8, packing: u16) -> Option<usize> {
|
||||
let words = match (bits, packing) {
|
||||
// Three samples to a word, or one to a 16-bit half word.
|
||||
(10, 1 | 2) => samples.div_ceil(3),
|
||||
(12, 1 | 2) => samples.div_ceil(2),
|
||||
_ => samples.checked_mul(usize::from(bits))?.div_ceil(32),
|
||||
/// The Cineon magic number (`802A5FD7` hex).
|
||||
pub(crate) fn is_cineon(b: &[u8]) -> bool {
|
||||
craft_dpx::sniff(b) == Some(Kind::Cineon)
|
||||
}
|
||||
|
||||
/// The EXIF orientation (1–8) for a Cineon header's orientation code.
|
||||
pub(crate) fn cineon_orientation(b: &[u8]) -> u16 {
|
||||
info(b, Kind::Cineon).map_or(1, |i| i.exif_orientation())
|
||||
}
|
||||
|
||||
/// A decoded frame as an image: luma, RGB or RGBA, 8 bits as U8, deeper codes widened to U16.
|
||||
fn image(format: Format, frame: Frame) -> Result<(Info, Image), CodecError> {
|
||||
let i = &frame.info;
|
||||
let layout = match i.layout {
|
||||
Layout::Luma => ChannelLayout::Gray,
|
||||
Layout::Rgb => ChannelLayout::Rgb,
|
||||
Layout::Rgba => ChannelLayout::Rgba,
|
||||
l => return Err(CodecError::unsupported(format, format!("{l:?} (Y'CbCr) elements are not supported (only luma, RGB and RGBA)"))),
|
||||
};
|
||||
words.checked_mul(4)
|
||||
let img = match i.bits {
|
||||
8 => Image::from_u8(i.width, i.height, layout, frame.codes.iter().map(|&c| c as u8).collect())?,
|
||||
_ => Image::from_u16(i.width, i.height, layout, &frame.samples_u16())?,
|
||||
};
|
||||
Ok((frame.info, img))
|
||||
}
|
||||
|
||||
/// Code values of one stored line.
|
||||
fn unpack_line(line: &[u8], o: Order, bits: u8, packing: u16, out: &mut [u16]) -> Result<(), CodecError> {
|
||||
let short = || err("truncated image data");
|
||||
match (bits, packing) {
|
||||
(8, _) => {
|
||||
let src = line.get(..out.len()).ok_or_else(short)?;
|
||||
for (v, &c) in out.iter_mut().zip(src) {
|
||||
*v = u16::from(c);
|
||||
}
|
||||
}
|
||||
(16, _) => {
|
||||
for (i, v) in out.iter_mut().enumerate() {
|
||||
*v = o.u16(line, i * 2).ok_or_else(short)?;
|
||||
}
|
||||
}
|
||||
(10, 1 | 2) => {
|
||||
let base = if packing == 1 { 22 } else { 20 };
|
||||
for (i, v) in out.iter_mut().enumerate() {
|
||||
let word = o.u32(line, i / 3 * 4).ok_or_else(short)?;
|
||||
*v = ((word >> (base - 10 * (i % 3) as u32)) & 0x3FF) as u16;
|
||||
}
|
||||
}
|
||||
(12, 1 | 2) => {
|
||||
let shift = if packing == 1 { 4 } else { 0 };
|
||||
for (i, v) in out.iter_mut().enumerate() {
|
||||
*v = (o.u16(line, i * 2).ok_or_else(short)? >> shift) & 0xFFF;
|
||||
}
|
||||
}
|
||||
// Packing 0: one bit stream through the line's 32-bit words, high bits first.
|
||||
_ => {
|
||||
let mask = (1u64 << bits) - 1;
|
||||
let (mut acc, mut have, mut word) = (0u64, 0u32, 0usize);
|
||||
for v in out.iter_mut() {
|
||||
while have < u32::from(bits) {
|
||||
acc = (acc << 32) | u64::from(o.u32(line, word * 4).ok_or_else(short)?);
|
||||
word += 1;
|
||||
have += 32;
|
||||
}
|
||||
have -= u32::from(bits);
|
||||
*v = ((acc >> have) & mask) as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A 10- or 12-bit code value as a 16-bit sample: `round(c × 65535 / max)`.
|
||||
fn widen(c: u16, max: u64) -> u16 {
|
||||
((u64::from(c) * 2 * 65535 + max) / (2 * max)).min(65535) as u16
|
||||
}
|
||||
|
||||
/// The inverse of [`widen`]: `round(v × max / 65535)`.
|
||||
fn narrow(v: u16, max: u64) -> u16 {
|
||||
((u64::from(v) * 2 * max + 65535) / (2 * 65535)) as u16
|
||||
/// Decodes a frame of `kind` within `limits`, checked as this crate checks every decoder.
|
||||
fn decode_kind(bytes: &[u8], limits: &Limits, kind: Kind) -> Result<(Info, Image), CodecError> {
|
||||
let format = if kind == Kind::Dpx { F } else { Format::Cineon };
|
||||
let i = info(bytes, kind)?;
|
||||
let layout = match i.layout {
|
||||
Layout::Luma => ChannelLayout::Gray,
|
||||
Layout::Rgb => ChannelLayout::Rgb,
|
||||
_ => ChannelLayout::Rgba,
|
||||
};
|
||||
limits.check(i.width, i.height, layout, if i.bits == 8 { SampleType::U8 } else { SampleType::U16 })?;
|
||||
image(format, craft_dpx::decode(bytes, &dpx_limits(limits)).map_err(|e| err(format, e))?)
|
||||
}
|
||||
|
||||
pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
let h = parse(bytes)?;
|
||||
let e = h.element;
|
||||
let layout = match e.descriptor {
|
||||
6 => ChannelLayout::Gray,
|
||||
50 => ChannelLayout::Rgb,
|
||||
51 => ChannelLayout::Rgba,
|
||||
d => return Err(unsupported(format!("descriptor {d} is not supported (only 6 luma, 50 RGB and 51 RGBA)"))),
|
||||
};
|
||||
if e.signed {
|
||||
return Err(unsupported("data sign 1 (signed samples) is not supported"));
|
||||
let (info, mut img) = decode_kind(bytes, limits, Kind::Dpx)?;
|
||||
let i = &info;
|
||||
if i.elements > 1 {
|
||||
img.warnings.push(DecodeWarning::MoreElements { total: u32::from(i.elements) });
|
||||
}
|
||||
if e.encoding != 0 {
|
||||
return Err(unsupported(format!("encoding {} (run-length encoded data) is not supported", e.encoding)));
|
||||
if i.transfer.is_some() || i.colorimetric.is_some() {
|
||||
img.warnings.push(DecodeWarning::DpxEncoding { transfer: i.transfer.unwrap_or(u8::MAX), colorimetric: i.colorimetric.unwrap_or(u8::MAX) });
|
||||
}
|
||||
let packing_ok = match e.bits {
|
||||
8 | 16 => e.packing == 0,
|
||||
10 => e.packing <= 2,
|
||||
12 => e.packing <= 1,
|
||||
b => return Err(unsupported(format!("bit size {b} is not supported (only 8, 10, 12 and 16)"))),
|
||||
};
|
||||
if !packing_ok {
|
||||
return Err(unsupported(format!("packing {} is not supported at {} bits", e.packing, e.bits)));
|
||||
}
|
||||
let sample = if e.bits == 8 { SampleType::U8 } else { SampleType::U16 };
|
||||
limits.check(h.width, h.height, layout, sample)?;
|
||||
|
||||
let nc = layout.channels();
|
||||
let per_line = (h.width as usize).checked_mul(nc).ok_or_else(|| CodecError::LimitExceeded("line too long".into()))?;
|
||||
let height = h.height as usize;
|
||||
let data = bytes.get(e.data_offset..).ok_or_else(|| err("the image data offset is past the end of the file"))?;
|
||||
let too_large = || CodecError::LimitExceeded("image too large".into());
|
||||
let mut line_len = line_bytes(per_line, e.bits, e.packing).ok_or_else(too_large)?;
|
||||
let mut stride = line_len.checked_add(e.eol_padding).ok_or_else(too_large)?;
|
||||
// Lines run `stride` apart; the last one needs no padding after it.
|
||||
let span = |stride: usize, line_len: usize| stride.checked_mul(height - 1)?.checked_add(line_len);
|
||||
// Some writers pack 8- and 16-bit lines without padding them to a word; take that layout
|
||||
// when the file is too short for padded lines but holds the unpadded ones.
|
||||
let tight = per_line * usize::from(e.bits / 8);
|
||||
if matches!(e.bits, 8 | 16) && e.eol_padding == 0 && line_len != tight && span(stride, line_len).is_none_or(|need| data.len() < need) {
|
||||
(line_len, stride) = (tight, tight);
|
||||
}
|
||||
if span(stride, line_len).ok_or_else(too_large)? > data.len() {
|
||||
return Err(err("truncated image data"));
|
||||
}
|
||||
|
||||
let mut codes = vec![0u16; per_line.checked_mul(height).ok_or_else(too_large)?];
|
||||
for (y, row) in codes.chunks_exact_mut(per_line.max(1)).enumerate() {
|
||||
let start = y * stride;
|
||||
let line = data.get(start..).ok_or_else(|| err("truncated image data"))?;
|
||||
unpack_line(line, h.order, e.bits, e.packing, row)?;
|
||||
}
|
||||
|
||||
let mut img = match e.bits {
|
||||
8 => Image::from_u8(h.width, h.height, layout, codes.iter().map(|&c| c as u8).collect())?,
|
||||
16 => Image::from_u16(h.width, h.height, layout, &codes)?,
|
||||
b => {
|
||||
let max = (1u64 << b) - 1;
|
||||
let wide: Vec<u16> = codes.iter().map(|&c| widen(c, max)).collect();
|
||||
Image::from_u16(h.width, h.height, layout, &wide)?
|
||||
}
|
||||
};
|
||||
if h.elements > 1 {
|
||||
img.warnings.push(DecodeWarning::MoreElements { total: u32::from(h.elements) });
|
||||
}
|
||||
if e.transfer != UNDEF8 || e.colorimetric != UNDEF8 {
|
||||
img.warnings.push(DecodeWarning::DpxEncoding { transfer: e.transfer, colorimetric: e.colorimetric });
|
||||
}
|
||||
img.meta.text = header_text(bytes, h.order, e);
|
||||
img.meta.text = header_text(i);
|
||||
Ok(img)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Cineon (Kodak .cin)
|
||||
//
|
||||
// Cineon is the 10-bit film-scan format DPX grew from (same Kodak header family, big-endian).
|
||||
// The generic header is 1024 bytes: the file information fields, then at offset 192 the image
|
||||
// orientation (1 byte), the channel count (1 byte) and up to eight 28-byte channel descriptors
|
||||
// (the first carries the bits per pixel and the line/image dimensions). Image data is three
|
||||
// 10-bit samples packed into each 32-bit word, lines on a word boundary — the same layout as
|
||||
// DPX's 10-bit packing method A, so the unpacking and packing are shared.
|
||||
|
||||
/// The Cineon magic number (`802A5FD7` hex), big-endian.
|
||||
pub(crate) fn is_cineon(b: &[u8]) -> bool {
|
||||
b.get(..4) == Some(&[0x80, 0x2A, 0x5F, 0xD7])
|
||||
}
|
||||
|
||||
/// The EXIF orientation (1–8) for a Cineon header's orientation code (the same 0–7 order as DPX).
|
||||
pub(crate) fn cineon_orientation(b: &[u8]) -> u16 {
|
||||
let Some(code) = b.get(192).copied() else { return 1 };
|
||||
match code {
|
||||
1 => 2,
|
||||
2 => 4,
|
||||
3 => 3,
|
||||
4 => 5,
|
||||
5 => 6,
|
||||
6 => 8,
|
||||
7 => 7,
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
|
||||
/// Decode a Cineon (`.cin`) frame: 10-bit logarithmic film scans, big-endian, one (gray) or
|
||||
/// three (RGB) channels. The 10-bit code values become U16 as `round(c × 65535 / 1023)`; the
|
||||
/// file's black/white reference points are not applied (a log conversion is a display choice).
|
||||
/// Decode a Cineon (`.cin`) frame: 10-bit logarithmic film scans, one (gray) or three (RGB)
|
||||
/// channels. The file's black/white reference points are not applied (a log conversion is a
|
||||
/// display choice).
|
||||
pub(crate) fn decode_cineon(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
if !is_cineon(bytes) {
|
||||
return Err(err("missing the Cineon magic number (802A5FD7)"));
|
||||
}
|
||||
let o = Order::Big;
|
||||
let missing = |what: &str| err(format!("the header ends before {what}"));
|
||||
let image_offset = usize::try_from(o.u32(bytes, 4).ok_or_else(|| missing("the image data offset"))?).map_err(|_| err("image data offset out of range"))?;
|
||||
let channels = *bytes.get(193).ok_or_else(|| missing("the channel count"))?;
|
||||
let bits = *bytes.get(198).ok_or_else(|| missing("the bits per pixel"))?;
|
||||
if bits != 10 {
|
||||
return Err(unsupported(format!("{bits}-bit samples (Cineon frames are 10-bit)")));
|
||||
}
|
||||
let layout = match channels {
|
||||
1 => ChannelLayout::Gray,
|
||||
3 => ChannelLayout::Rgb,
|
||||
n => return Err(unsupported(format!("{n} channels (Cineon frames have 1 or 3)"))),
|
||||
};
|
||||
let width = o.u32(bytes, 200).ok_or_else(|| missing("the pixels per line"))?;
|
||||
let height = o.u32(bytes, 204).ok_or_else(|| missing("the lines per image"))?;
|
||||
limits.check(width, height, layout, SampleType::U16)?;
|
||||
|
||||
let (w, h) = (width as usize, height as usize);
|
||||
let per_line = w.checked_mul(layout.channels()).ok_or_else(|| CodecError::LimitExceeded("line too long".into()))?;
|
||||
// Three 10-bit samples per 32-bit word, lines on a word boundary (DPX packing 1).
|
||||
let line_len = line_bytes(per_line, 10, 1).ok_or_else(|| CodecError::LimitExceeded("line too long".into()))?;
|
||||
let data = bytes.get(image_offset..).ok_or_else(|| err("the image data offset is past the end of the file"))?;
|
||||
let need = line_len.checked_mul(h).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?;
|
||||
if data.len() < need {
|
||||
return Err(err("truncated image data"));
|
||||
}
|
||||
let mut codes = vec![0u16; per_line.checked_mul(h).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?];
|
||||
for (y, row) in codes.chunks_exact_mut(per_line).enumerate() {
|
||||
let start = y * line_len;
|
||||
let line = data.get(start..start + line_len).ok_or_else(|| err("truncated image data"))?;
|
||||
unpack_line(line, o, 10, 1, row)?;
|
||||
}
|
||||
let wide: Vec<u16> = codes.iter().map(|&c| widen(c, 1023)).collect();
|
||||
Image::from_u16(width, height, layout, &wide)
|
||||
}
|
||||
|
||||
/// Cineon's generic plus film header: where our image data starts.
|
||||
const CINEON_HEADER_LEN: usize = 2048;
|
||||
const CINEON_GENERIC_LEN: u32 = 1024;
|
||||
/// Offset of the first of the eight 28-byte channel descriptors.
|
||||
const CINEON_CHANNEL0: usize = 196;
|
||||
|
||||
/// Encode a Cineon 4.5 frame: the generic header, an undefined film header, then the samples as
|
||||
/// 10-bit code values `round(v × 1023 / 65535)`, three to a big-endian word, left justified
|
||||
/// (packing 5), lines on a word boundary. The plan has already made the image U16 gray or RGB.
|
||||
pub(crate) fn encode_cineon(src: &Image, plan: Plan) -> Result<Vec<u8>, CodecError> {
|
||||
let enc = |msg: String| CodecError::encode(Format::Cineon, msg);
|
||||
let img = src.converted(plan.layout, plan.sample);
|
||||
let (w, h) = img.dimensions();
|
||||
// Channel designators: 0 for black and white, 1–3 for red, green and blue.
|
||||
let designators: &[u8] = match img.layout() {
|
||||
ChannelLayout::Gray => &[0],
|
||||
ChannelLayout::Rgb => &[1, 2, 3],
|
||||
l => return Err(enc(format!("Cineon cannot store {l:?}"))),
|
||||
};
|
||||
let samples = img.to_u16_samples().ok_or_else(|| enc(format!("cannot write {:?} samples at 10 bits", img.sample_type())))?;
|
||||
let codes: Vec<u16> = samples.into_iter().map(|v| narrow(v, 1023)).collect();
|
||||
let per_line = (w as usize).checked_mul(designators.len()).ok_or_else(|| enc("image too large".into()))?;
|
||||
if codes.len() != per_line.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 data_len = line_bytes(per_line, 10, 1).and_then(|l| l.checked_mul(h as usize)).ok_or_else(|| enc("image too large".into()))?;
|
||||
let total =
|
||||
CINEON_HEADER_LEN.checked_add(data_len).and_then(|t| u32::try_from(t).ok()).ok_or_else(|| enc("image too large for a Cineon file (4 GiB)".into()))?;
|
||||
|
||||
let mut wr = Writer { h: vec![0; CINEON_HEADER_LEN] };
|
||||
// File information.
|
||||
wr.u32(0, 0x802A_5FD7);
|
||||
wr.u32(4, CINEON_HEADER_LEN as u32);
|
||||
wr.u32(8, CINEON_GENERIC_LEN);
|
||||
wr.u32(12, CINEON_HEADER_LEN as u32 - CINEON_GENERIC_LEN);
|
||||
wr.u32(16, 0);
|
||||
wr.u32(20, total);
|
||||
wr.string((24, 8), "V4.5");
|
||||
// Image information: default orientation, then each channel's descriptor; the unused ones,
|
||||
// the white point and the primaries are undefined.
|
||||
wr.u8(192, 0);
|
||||
wr.u8(193, designators.len() as u8);
|
||||
wr.undefined(CINEON_CHANNEL0, 420);
|
||||
for (i, &designator) in designators.iter().enumerate() {
|
||||
let c = CINEON_CHANNEL0 + 28 * i;
|
||||
wr.u8(c, 0);
|
||||
wr.u8(c + 1, designator);
|
||||
wr.u8(c + 2, 10);
|
||||
wr.u8(c + 3, 0);
|
||||
wr.u32(c + 4, w);
|
||||
wr.u32(c + 8, h);
|
||||
// Code values 0–1023 stand for printing densities 0–2.048, the Cineon convention. All
|
||||
// four are 32-bit floats here (DPX stores the data values as integers).
|
||||
wr.u32(c + 12, 0f32.to_bits());
|
||||
wr.u32(c + 16, 0f32.to_bits());
|
||||
wr.u32(c + 20, 1023f32.to_bits());
|
||||
wr.u32(c + 24, 2.048f32.to_bits());
|
||||
}
|
||||
// Pixel-interleaved, unsigned, positive, no line or channel padding.
|
||||
wr.u8(681, 5);
|
||||
// Image origination: pixel pitches and gamma unknown.
|
||||
wr.undefined(972, 984);
|
||||
// Film header: manufacturer, type, perforation offset, prefix, count, frame position and rate
|
||||
// unknown; its strings stay empty.
|
||||
wr.undefined(1024, 1036);
|
||||
wr.undefined(1068, 1076);
|
||||
|
||||
let mut out = wr.h;
|
||||
out.try_reserve_exact(data_len).map_err(|_| enc("out of memory".into()))?;
|
||||
for row in codes.chunks_exact(per_line.max(1)) {
|
||||
pack_line(&mut out, row, 10);
|
||||
}
|
||||
Ok(out)
|
||||
decode_kind(bytes, limits, Kind::Cineon).map(|(_, img)| img)
|
||||
}
|
||||
|
||||
/// The kept header fields as text metadata, in [`KEYS`] order; undefined ones are left out.
|
||||
fn header_text(b: &[u8], o: Order, e: Element) -> Vec<(String, String)> {
|
||||
fn header_text(i: &Info) -> Vec<(String, String)> {
|
||||
let mut text = Vec::new();
|
||||
let mut put = |k: &str, v: Option<String>| {
|
||||
if let Some(v) = v {
|
||||
text.push((k.to_string(), v));
|
||||
}
|
||||
};
|
||||
put(KEY_CREATOR, string(b, CREATOR));
|
||||
put(KEY_PROJECT, string(b, PROJECT));
|
||||
put(KEY_COPYRIGHT, string(b, COPYRIGHT));
|
||||
put(KEY_CREATION_TIME, string(b, CREATION_TIME));
|
||||
put(KEY_TRANSFER, (e.transfer != UNDEF8).then(|| e.transfer.to_string()));
|
||||
put(KEY_COLORIMETRIC, (e.colorimetric != UNDEF8).then(|| e.colorimetric.to_string()));
|
||||
// The industry header is there when the generic header says so and the file holds it.
|
||||
let industry = o.u32(b, 28).is_some_and(|l| l != 0 && l != UNDEF32) && b.len() >= HEADER_LEN;
|
||||
if industry {
|
||||
put(KEY_FRAME_POSITION, o.defined_u32(b, FRAME_POSITION).map(|v| v.to_string()));
|
||||
put(KEY_FRAME_RATE, o.f32(b, FRAME_RATE).map(|v| v.to_string()));
|
||||
put(KEY_TIME_CODE, o.defined_u32(b, TIME_CODE).map(format_time_code));
|
||||
}
|
||||
put(KEY_CREATOR, i.creator.clone());
|
||||
put(KEY_PROJECT, i.project.clone());
|
||||
put(KEY_COPYRIGHT, i.copyright.clone());
|
||||
put(KEY_CREATION_TIME, i.creation_time.clone());
|
||||
put(KEY_TRANSFER, i.transfer.map(|v| v.to_string()));
|
||||
put(KEY_COLORIMETRIC, i.colorimetric.map(|v| v.to_string()));
|
||||
put(KEY_FRAME_POSITION, i.frame_position.map(|v| v.to_string()));
|
||||
put(KEY_FRAME_RATE, i.frame_rate.map(|v| v.to_string()));
|
||||
put(KEY_TIME_CODE, i.time_code.map(craft_dpx::format_time_code));
|
||||
text
|
||||
}
|
||||
|
||||
/// A SMPTE time code (BCD `HHMMSSFF`) as `HH:MM:SS:FF`, or `0x…` when it is not valid BCD.
|
||||
fn format_time_code(v: u32) -> String {
|
||||
let d: [u32; 8] = std::array::from_fn(|i| (v >> ((7 - i) * 4)) & 0xF);
|
||||
if d.iter().all(|&d| d <= 9) { format!("{}{}:{}{}:{}{}:{}{}", d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7]) } else { format!("{v:#010x}") }
|
||||
}
|
||||
|
||||
/// The inverse of [`format_time_code`]; `None` for anything else.
|
||||
fn parse_time_code(s: &str) -> Option<u32> {
|
||||
if let Some(hex) = s.strip_prefix("0x") {
|
||||
return u32::from_str_radix(hex, 16).ok();
|
||||
}
|
||||
let parts: Vec<&str> = s.split(':').collect();
|
||||
if parts.len() != 4 || parts.iter().any(|p| p.len() != 2 || !p.bytes().all(|c| c.is_ascii_digit())) {
|
||||
return None;
|
||||
}
|
||||
parts.iter().flat_map(|p| p.bytes()).try_fold(0u32, |acc, c| Some((acc << 4) | u32::from(c - b'0')))
|
||||
}
|
||||
|
||||
/// Bits written for `sample` when the options pick none: 8 for U8, 16 otherwise.
|
||||
pub(crate) fn bits_for(opt: Option<DpxBits>, sample: SampleType) -> u8 {
|
||||
match opt {
|
||||
@@ -558,8 +183,7 @@ pub(crate) fn keeps_u16_samples(img: &Image, bits: u8) -> bool {
|
||||
if img.sample_type() != SampleType::U16 || !(1..16).contains(&bits) {
|
||||
return false;
|
||||
}
|
||||
let max = (1u64 << bits) - 1;
|
||||
img.data().as_chunks::<2>().0.iter().map(|c| u16::from_ne_bytes(*c)).all(|v| widen(narrow(v, max), max) == v)
|
||||
img.data().as_chunks::<2>().0.iter().map(|c| u16::from_ne_bytes(*c)).all(|v| craft_dpx::widen(craft_dpx::narrow(v, bits), bits) == v)
|
||||
}
|
||||
|
||||
/// The plan's sample type for the chosen bits: U8 for 8 bits, U16 for 10, 12 and 16.
|
||||
@@ -572,315 +196,53 @@ pub(crate) fn plan_sample(opt: Option<DpxBits>, src: SampleType) -> SampleType {
|
||||
}
|
||||
}
|
||||
|
||||
struct Writer {
|
||||
h: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
fn u8(&mut self, at: usize, v: u8) {
|
||||
if let Some(b) = self.h.get_mut(at) {
|
||||
*b = v;
|
||||
}
|
||||
}
|
||||
|
||||
fn u16(&mut self, at: usize, v: u16) {
|
||||
if let Some(s) = self.h.get_mut(at..at + 2) {
|
||||
s.copy_from_slice(&v.to_be_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
fn u32(&mut self, at: usize, v: u32) {
|
||||
if let Some(s) = self.h.get_mut(at..at + 4) {
|
||||
s.copy_from_slice(&v.to_be_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
/// Marks a run of numeric fields undefined (all ones).
|
||||
fn undefined(&mut self, from: usize, to: usize) {
|
||||
if let Some(s) = self.h.get_mut(from..to) {
|
||||
s.fill(0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
/// An ASCII string field, cut to leave room for a terminating NUL.
|
||||
fn string(&mut self, (at, len): (usize, usize), v: &str) {
|
||||
let ascii: Vec<u8> = v.chars().map(|c| if (' '..='~').contains(&c) { c as u8 } else { b'?' }).take(len.saturating_sub(1)).collect();
|
||||
if let Some(s) = self.h.get_mut(at..at + ascii.len()) {
|
||||
s.copy_from_slice(&ascii);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn text<'a>(img: &'a Image, key: &str) -> Option<&'a str> {
|
||||
img.meta.text.iter().find(|(k, _)| k == key).map(|(_, v)| v.trim()).filter(|v| !v.is_empty())
|
||||
}
|
||||
|
||||
/// Appends one line's code values, padded to a 32-bit word.
|
||||
fn pack_line(out: &mut Vec<u8>, codes: &[u16], bits: u8) {
|
||||
let start = out.len();
|
||||
match bits {
|
||||
8 => out.extend(codes.iter().map(|&c| c as u8)),
|
||||
10 => {
|
||||
for chunk in codes.chunks(3) {
|
||||
let word = chunk.iter().enumerate().fold(0u32, |w, (i, &c)| w | (u32::from(c & 0x3FF) << (22 - 10 * i as u32)));
|
||||
out.extend_from_slice(&word.to_be_bytes());
|
||||
}
|
||||
}
|
||||
12 => out.extend(codes.iter().flat_map(|&c| ((c & 0xFFF) << 4).to_be_bytes())),
|
||||
_ => out.extend(codes.iter().flat_map(|&c| c.to_be_bytes())),
|
||||
}
|
||||
let written = out.len() - start;
|
||||
out.resize(start + written.div_ceil(4) * 4, 0);
|
||||
/// `img`'s samples as `bits`-bit code values, with the layout they are written in.
|
||||
fn codes(format: Format, img: &Image, bits: u8) -> Result<(Layout, Vec<u16>), CodecError> {
|
||||
let layout = match img.layout() {
|
||||
ChannelLayout::Gray => Layout::Luma,
|
||||
ChannelLayout::Rgb => Layout::Rgb,
|
||||
ChannelLayout::Rgba if format == F => Layout::Rgba,
|
||||
l => return Err(CodecError::encode(format, format!("{format:?} cannot store {l:?}"))),
|
||||
};
|
||||
let codes: Vec<u16> = match (img.sample_type(), bits) {
|
||||
(SampleType::U8, 8) => img.data().iter().map(|&v| u16::from(v)).collect(),
|
||||
(SampleType::U16, 16) => img.to_u16_samples().unwrap_or_default(),
|
||||
(SampleType::U16, 10 | 12) => img.to_u16_samples().unwrap_or_default().into_iter().map(|v| craft_dpx::narrow(v, bits)).collect(),
|
||||
(s, b) => return Err(CodecError::encode(format, format!("cannot write {s:?} samples at {b} bits"))),
|
||||
};
|
||||
Ok((layout, codes))
|
||||
}
|
||||
|
||||
/// Encode a Cineon 4.5 frame at 10 bits. The plan has already made the image U16 gray or RGB.
|
||||
pub(crate) fn encode_cineon(src: &Image, plan: Plan) -> Result<Vec<u8>, CodecError> {
|
||||
let img = src.converted(plan.layout, plan.sample);
|
||||
let (layout, codes) = codes(Format::Cineon, &img, 10)?;
|
||||
let (width, height) = img.dimensions();
|
||||
craft_dpx::encode_cineon(&craft_dpx::Picture { width, height, layout, bits: 10, codes: &codes }).map_err(|e| err(Format::Cineon, e))
|
||||
}
|
||||
|
||||
pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
|
||||
let img = src.converted(plan.layout, plan.sample);
|
||||
let (w, h) = img.dimensions();
|
||||
let (descriptor, layout) = match img.layout() {
|
||||
ChannelLayout::Gray => (6u8, ChannelLayout::Gray),
|
||||
ChannelLayout::Rgb => (50, ChannelLayout::Rgb),
|
||||
ChannelLayout::Rgba => (51, ChannelLayout::Rgba),
|
||||
l => return Err(CodecError::encode(F, format!("DPX cannot store {l:?}"))),
|
||||
};
|
||||
let bits = bits_for(opts.dpx_bits, img.sample_type());
|
||||
let codes: Vec<u16> = match (img.sample_type(), bits) {
|
||||
(SampleType::U8, 8) => img.data().iter().map(|&v| u16::from(v)).collect(),
|
||||
(SampleType::U16, 16) => img.to_u16_samples().unwrap_or_default(),
|
||||
(SampleType::U16, 10 | 12) => {
|
||||
let max = (1u64 << bits) - 1;
|
||||
img.to_u16_samples().unwrap_or_default().into_iter().map(|v| narrow(v, max)).collect()
|
||||
}
|
||||
(s, b) => return Err(CodecError::encode(F, format!("cannot write {s:?} samples at {b} bits"))),
|
||||
};
|
||||
let per_line = w as usize * layout.channels();
|
||||
if codes.len() != per_line * h as usize {
|
||||
return Err(CodecError::encode(F, "the image has the wrong number of samples"));
|
||||
}
|
||||
let data_len = line_bytes(per_line, bits, if matches!(bits, 10 | 12) { 1 } else { 0 })
|
||||
.and_then(|l| l.checked_mul(h as usize))
|
||||
.ok_or_else(|| CodecError::encode(F, "image too large"))?;
|
||||
let total = u32::try_from(HEADER_LEN + data_len).map_err(|_| CodecError::encode(F, "image too large for a DPX file (4 GiB)"))?;
|
||||
|
||||
let mut wr = Writer { h: vec![0; HEADER_LEN] };
|
||||
// Generic file header.
|
||||
wr.h[..4].copy_from_slice(b"SDPX");
|
||||
wr.u32(4, HEADER_LEN as u32);
|
||||
wr.string((8, 8), "V2.0");
|
||||
wr.u32(16, total);
|
||||
wr.u32(20, 1);
|
||||
wr.u32(24, GENERIC_LEN);
|
||||
wr.u32(28, INDUSTRY_LEN);
|
||||
wr.u32(32, 0);
|
||||
let (layout, codes) = codes(F, &img, bits)?;
|
||||
let keep = |key| if opts.embed_metadata { text(&img, key) } else { None };
|
||||
wr.string(CREATION_TIME, keep(KEY_CREATION_TIME).unwrap_or(""));
|
||||
wr.string(CREATOR, keep(KEY_CREATOR).unwrap_or("PhotoCraft"));
|
||||
wr.string(PROJECT, keep(KEY_PROJECT).unwrap_or(""));
|
||||
wr.string(COPYRIGHT, keep(KEY_COPYRIGHT).unwrap_or(""));
|
||||
wr.u32(660, UNDEF32);
|
||||
// Image information: default orientation, one element.
|
||||
wr.u16(ORIENTATION, 0);
|
||||
wr.u16(ORIENTATION + 2, 1);
|
||||
wr.u32(772, w);
|
||||
wr.u32(776, h);
|
||||
let e = ELEMENT0;
|
||||
wr.u32(e, 0);
|
||||
wr.u32(e + 4, 0);
|
||||
wr.u32(e + 8, UNDEF32);
|
||||
wr.u32(e + 12, ((1u32 << bits) - 1).min(65535));
|
||||
wr.u32(e + 16, UNDEF32);
|
||||
wr.u8(e + 20, descriptor);
|
||||
// No claim about the encoding unless the document came with one.
|
||||
let code = |key| keep(key).and_then(|v| v.parse::<u8>().ok()).unwrap_or(UNDEF8);
|
||||
wr.u8(e + 21, code(KEY_TRANSFER));
|
||||
wr.u8(e + 22, code(KEY_COLORIMETRIC));
|
||||
wr.u8(e + 23, bits);
|
||||
wr.u16(e + 24, if matches!(bits, 10 | 12) { 1 } else { 0 });
|
||||
wr.u16(e + 26, 0);
|
||||
wr.u32(e + 28, HEADER_LEN as u32);
|
||||
wr.u32(e + 32, 0);
|
||||
wr.u32(e + 36, 0);
|
||||
// The seven unused elements, the image source header's numbers and the industry header's
|
||||
// numbers are undefined; their strings stay empty.
|
||||
wr.undefined(e + ELEMENT_LEN, e + ELEMENT_LEN * usize::from(MAX_ELEMENTS));
|
||||
wr.undefined(1408, 1432);
|
||||
wr.undefined(1620, 1644);
|
||||
wr.undefined(FRAME_POSITION, 1732);
|
||||
wr.undefined(TIME_CODE, 1972);
|
||||
if let Some(v) = keep(KEY_FRAME_POSITION).and_then(|v| v.parse::<u32>().ok()) {
|
||||
wr.u32(FRAME_POSITION, v);
|
||||
}
|
||||
if let Some(v) = keep(KEY_FRAME_RATE).and_then(|v| v.parse::<f32>().ok()).filter(|v| v.is_finite()) {
|
||||
wr.u32(FRAME_RATE, v.to_bits());
|
||||
}
|
||||
if let Some(v) = keep(KEY_TIME_CODE).and_then(parse_time_code) {
|
||||
wr.u32(TIME_CODE, v);
|
||||
}
|
||||
|
||||
let mut out = wr.h;
|
||||
out.try_reserve_exact(data_len).map_err(|_| CodecError::encode(F, "out of memory"))?;
|
||||
for row in codes.chunks_exact(per_line.max(1)) {
|
||||
pack_line(&mut out, row, bits);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A minimal header for `w`×`h` with element 0 set; image data at 2048.
|
||||
fn header(o: Order, w: u32, h: u32, descriptor: u8, bits: u8, packing: u16) -> Vec<u8> {
|
||||
let mut b = vec![0u8; HEADER_LEN];
|
||||
let put32 = |b: &mut Vec<u8>, at: usize, v: u32| b[at..at + 4].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() });
|
||||
let put16 = |b: &mut Vec<u8>, at: usize, v: u16| b[at..at + 2].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() });
|
||||
b[..4].copy_from_slice(if o == Order::Big { b"SDPX" } else { b"XPDS" });
|
||||
put32(&mut b, 4, HEADER_LEN as u32);
|
||||
put32(&mut b, 28, INDUSTRY_LEN);
|
||||
put16(&mut b, ORIENTATION, 0);
|
||||
put16(&mut b, ORIENTATION + 2, 1);
|
||||
put32(&mut b, 772, w);
|
||||
put32(&mut b, 776, h);
|
||||
b[ELEMENT0 + 20] = descriptor;
|
||||
b[ELEMENT0 + 21] = UNDEF8;
|
||||
b[ELEMENT0 + 22] = UNDEF8;
|
||||
b[ELEMENT0 + 23] = bits;
|
||||
put16(&mut b, ELEMENT0 + 24, packing);
|
||||
put32(&mut b, ELEMENT0 + 28, HEADER_LEN as u32);
|
||||
b[FRAME_POSITION..1732].fill(0xFF);
|
||||
b[TIME_CODE..1972].fill(0xFF);
|
||||
b
|
||||
}
|
||||
|
||||
fn words(o: Order, ws: &[u32]) -> Vec<u8> {
|
||||
ws.iter().flat_map(|w| if o == Order::Big { w.to_be_bytes() } else { w.to_le_bytes() }).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ten_bit_method_a_and_b_unpack_three_to_a_word() {
|
||||
for o in [Order::Big, Order::Little] {
|
||||
for (packing, base) in [(1u16, 22u32), (2, 20)] {
|
||||
let mut b = header(o, 1, 1, 50, 10, packing);
|
||||
b.extend(words(o, &[(1023 << base) | (512 << (base - 10)) | (1 << (base - 20))]));
|
||||
let img = decode(&b, &Limits::default()).unwrap();
|
||||
assert_eq!(img.to_u16_samples().unwrap(), vec![65535, widen(512, 1023), widen(1, 1023)], "{o:?} packing {packing}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ten_bit_packing_0_is_one_bit_stream_per_line() {
|
||||
// Four luma samples: 40 bits over two words, the line padded to the second word.
|
||||
let samples = [1u64, 2, 1022, 1023];
|
||||
let stream = samples.iter().fold(0u64, |acc, &s| (acc << 10) | s) << 24;
|
||||
for o in [Order::Big, Order::Little] {
|
||||
let mut b = header(o, 4, 2, 6, 10, 0);
|
||||
let line = words(o, &[(stream >> 32) as u32, stream as u32]);
|
||||
b.extend(&line);
|
||||
b.extend(&line);
|
||||
let img = decode(&b, &Limits::default()).unwrap();
|
||||
let want: Vec<u16> = samples.iter().map(|&s| widen(s as u16, 1023)).collect();
|
||||
assert_eq!(img.to_u16_samples().unwrap(), [want.clone(), want].concat(), "{o:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn twelve_bit_method_a_keeps_the_top_bits() {
|
||||
let mut b = header(Order::Big, 2, 1, 6, 12, 1);
|
||||
b.extend_from_slice(&[0xFF, 0xF0, 0x00, 0x10]);
|
||||
let img = decode(&b, &Limits::default()).unwrap();
|
||||
assert_eq!(img.to_u16_samples().unwrap(), vec![65535, widen(1, 4095)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn widen_and_narrow_round_trip_every_code() {
|
||||
for bits in [10u8, 12] {
|
||||
let max = (1u64 << bits) - 1;
|
||||
for c in 0..=max as u16 {
|
||||
assert_eq!(narrow(widen(c, max), max), c, "{bits}-bit {c}");
|
||||
}
|
||||
}
|
||||
assert_eq!(widen(512, 1023), ((512.0f64 * 65535.0 / 1023.0).round()) as u16);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn orientation_codes_map_to_exif() {
|
||||
let mut b = header(Order::Big, 1, 1, 6, 8, 0);
|
||||
for (dpx, exif) in [(0u16, 1u16), (1, 2), (2, 4), (3, 3), (4, 5), (5, 6), (6, 8), (7, 7), (0xFFFF, 1)] {
|
||||
b[ORIENTATION..ORIENTATION + 2].copy_from_slice(&dpx.to_be_bytes());
|
||||
assert_eq!(orientation(&b), exif, "DPX {dpx}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn time_codes_round_trip() {
|
||||
assert_eq!(format_time_code(0x0102_0304), "01:02:03:04");
|
||||
assert_eq!(parse_time_code("01:02:03:04"), Some(0x0102_0304));
|
||||
assert_eq!(parse_time_code(&format_time_code(0xABCD_0000)), Some(0xABCD_0000));
|
||||
assert_eq!(parse_time_code("1:2:3:4"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eight_bit_lines_without_word_padding_still_open() {
|
||||
// 1 × 2 RGB: 3 bytes a line, 6 in all; padded lines would need 8.
|
||||
let mut b = header(Order::Big, 1, 2, 50, 8, 0);
|
||||
b.extend_from_slice(&[1, 2, 3, 4, 5, 6]);
|
||||
let img = decode(&b, &Limits::default()).unwrap();
|
||||
assert_eq!(img.data(), &[1, 2, 3, 4, 5, 6]);
|
||||
}
|
||||
|
||||
/// A minimal Cineon: a 1024-byte generic header, then `w × h` 10-bit samples packed three
|
||||
/// to a 32-bit word (RGB in word order, or gray three pixels per word).
|
||||
fn cineon(w: u32, h: u32, channels: u8, px: &[[u16; 3]]) -> Vec<u8> {
|
||||
let mut b = vec![0u8; 1024];
|
||||
b[0..4].copy_from_slice(&[0x80, 0x2A, 0x5F, 0xD7]); // magic (802A5FD7)
|
||||
b[4..8].copy_from_slice(&1024u32.to_be_bytes()); // image data offset
|
||||
b[192] = 0; // orientation: left-to-right, top-to-bottom
|
||||
b[193] = channels;
|
||||
b[198] = 10; // bits per pixel (channel 1)
|
||||
b[200..204].copy_from_slice(&w.to_be_bytes()); // pixels per line
|
||||
b[204..208].copy_from_slice(&h.to_be_bytes()); // lines per image
|
||||
let word = |a: u16, c: u16, e: u16| ((u32::from(a) << 22) | (u32::from(c) << 12) | (u32::from(e) << 2)).to_be_bytes();
|
||||
if channels == 3 {
|
||||
for p in px {
|
||||
b.extend_from_slice(&word(p[0], p[1], p[2]));
|
||||
}
|
||||
} else {
|
||||
let g = |i: usize| px.get(i).map_or(0, |p| p[0]);
|
||||
for i in (0..px.len()).step_by(3) {
|
||||
b.extend_from_slice(&word(g(i), g(i + 1), g(i + 2)));
|
||||
}
|
||||
}
|
||||
b
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cineon_reads_ten_bit_rgb() {
|
||||
let px = [[0, 512, 1023], [1023, 0, 256]];
|
||||
let b = cineon(2, 1, 3, &px);
|
||||
assert!(is_cineon(&b));
|
||||
assert_eq!(order(&b), None, "Cineon is not DPX");
|
||||
let img = decode_cineon(&b, &Limits::default()).unwrap();
|
||||
assert_eq!((img.width(), img.height(), img.layout()), (2, 1, ChannelLayout::Rgb));
|
||||
assert_eq!(
|
||||
img.to_u16_samples().unwrap(),
|
||||
vec![widen(0, 1023), widen(512, 1023), widen(1023, 1023), widen(1023, 1023), widen(0, 1023), widen(256, 1023)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cineon_reads_grayscale_and_rejects_other_depths() {
|
||||
let px = [[10, 0, 0], [20, 0, 0], [30, 0, 0], [40, 0, 0]];
|
||||
let b = cineon(4, 1, 1, &px);
|
||||
let img = decode_cineon(&b, &Limits::default()).unwrap();
|
||||
assert_eq!(img.layout(), ChannelLayout::Gray);
|
||||
assert_eq!(img.to_u16_samples().unwrap(), vec![widen(10, 1023), widen(20, 1023), widen(30, 1023), widen(40, 1023)]);
|
||||
// A depth other than 10 is unsupported, and truncation errors rather than panicking.
|
||||
let mut bad = cineon(1, 1, 3, &[[0, 0, 0]]);
|
||||
bad[198] = 8;
|
||||
assert!(matches!(decode_cineon(&bad, &Limits::default()), Err(CodecError::Unsupported { .. })));
|
||||
let full = cineon(4, 4, 3, &[[0; 3]; 16]);
|
||||
for cut in [0, 4, 200, 1024, full.len() - 1] {
|
||||
assert!(decode_cineon(&full[..cut.min(full.len())], &Limits::default()).is_err(), "cut {cut}");
|
||||
}
|
||||
}
|
||||
let code = |key| keep(key).and_then(|v| v.parse::<u8>().ok());
|
||||
let meta = craft_dpx::Metadata {
|
||||
creator: Some(keep(KEY_CREATOR).unwrap_or("PhotoCraft")),
|
||||
project: keep(KEY_PROJECT),
|
||||
copyright: keep(KEY_COPYRIGHT),
|
||||
creation_time: keep(KEY_CREATION_TIME),
|
||||
transfer: code(KEY_TRANSFER),
|
||||
colorimetric: code(KEY_COLORIMETRIC),
|
||||
frame_position: keep(KEY_FRAME_POSITION).and_then(|v| v.parse::<u32>().ok()),
|
||||
frame_rate: keep(KEY_FRAME_RATE).and_then(|v| v.parse::<f32>().ok()),
|
||||
time_code: keep(KEY_TIME_CODE).and_then(craft_dpx::parse_time_code),
|
||||
};
|
||||
let (width, height) = img.dimensions();
|
||||
craft_dpx::encode(&craft_dpx::Picture { width, height, layout, bits, codes: &codes }, &meta).map_err(|e| err(F, e))
|
||||
}
|
||||
@@ -20,84 +20,23 @@ use crate::options::Limits;
|
||||
|
||||
const F: Format = Format::OpenExr;
|
||||
|
||||
/// A Cryptomatte stream channel (`crypto_asset00.red`) splits into its stream name
|
||||
/// (`crypto_asset00`), the stream index, whether it is a coverage channel (green/alpha)
|
||||
/// instead of an ID (red/blue), and its rank within the set (0 for red/green, 1 for
|
||||
/// blue/alpha).
|
||||
pub(crate) struct StreamChannel {
|
||||
pub stream: String,
|
||||
pub index: u32,
|
||||
pub coverage: bool,
|
||||
pub rank: usize,
|
||||
}
|
||||
|
||||
/// `<name>NN` where NN is one or more digits (the reference readers match `\d+`), with a
|
||||
/// `.red`/`.r` or `.blue`/`.b` (ID) or `.green`/`.g` or `.alpha`/`.a` (coverage) suffix,
|
||||
/// in any case (Blender writes `.R`/`.G`/`.B`/`.A`).
|
||||
pub(crate) fn split_stream(channel: &str) -> Option<StreamChannel> {
|
||||
let (stream, suffix) = channel.rsplit_once('.')?;
|
||||
let (coverage, rank) = match suffix.to_ascii_lowercase().as_str() {
|
||||
"red" | "r" => (false, 0),
|
||||
"green" | "g" => (true, 0),
|
||||
"blue" | "b" => (false, 1),
|
||||
"alpha" | "a" => (true, 1),
|
||||
_ => return None,
|
||||
};
|
||||
let digits = stream.chars().rev().take_while(|c| c.is_ascii_digit()).count();
|
||||
if digits == 0 || digits == stream.len() {
|
||||
return None;
|
||||
}
|
||||
let split = stream.len() - digits;
|
||||
let index = stream[split..].parse().ok()?;
|
||||
Some(StreamChannel { stream: stream.to_string(), index, coverage, rank })
|
||||
}
|
||||
|
||||
/// MurmurHash3, x86-32 bit variant, seed 0 (public-domain algorithm, Austin Appleby).
|
||||
fn murmur3_32(data: &[u8]) -> u32 {
|
||||
const C1: u32 = 0xcc9e_2d51;
|
||||
const C2: u32 = 0x1b87_3593;
|
||||
let mut h = 0u32;
|
||||
let (chunks, rest) = data.as_chunks::<4>();
|
||||
for c in chunks {
|
||||
let mut k = u32::from_le_bytes(*c);
|
||||
k = k.wrapping_mul(C1).rotate_left(15).wrapping_mul(C2);
|
||||
h ^= k;
|
||||
h = h.rotate_left(13).wrapping_mul(5).wrapping_add(0xe654_6b64);
|
||||
}
|
||||
if !rest.is_empty() {
|
||||
let mut k = [0u8; 4];
|
||||
k[..rest.len()].copy_from_slice(rest);
|
||||
let mut k = u32::from_le_bytes(k);
|
||||
k = k.wrapping_mul(C1).rotate_left(15).wrapping_mul(C2);
|
||||
h ^= k;
|
||||
}
|
||||
h ^= data.len() as u32;
|
||||
h ^= h >> 16;
|
||||
h = h.wrapping_mul(0x85eb_ca6b);
|
||||
h ^= h >> 13;
|
||||
h = h.wrapping_mul(0xc2b2_ae35);
|
||||
h ^ (h >> 16)
|
||||
}
|
||||
pub(crate) use craft_exr::cryptomatte::split_stream;
|
||||
|
||||
/// The Cryptomatte ID of a name: the MurmurHash3 bits with the exponent fixed away from
|
||||
/// 0 and 255, as an f32 value.
|
||||
pub fn cryptomatte_id(name: &str) -> f32 {
|
||||
let bits = murmur3_32(name.as_bytes());
|
||||
let exp = (bits >> 23) & 0xff;
|
||||
let bits = if exp == 0 || exp == 255 { bits ^ (1 << 23) } else { bits };
|
||||
f32::from_bits(bits)
|
||||
craft_exr::cryptomatte::id(name)
|
||||
}
|
||||
|
||||
/// The 7-hex-digit metadata key of a layer name (the ID bits without their last digit).
|
||||
pub fn cryptomatte_key(name: &str) -> String {
|
||||
format!("{:08x}", cryptomatte_id(name).to_bits())[..7].to_string()
|
||||
craft_exr::cryptomatte::key(name)
|
||||
}
|
||||
|
||||
/// The preview colour of an ID (the reference formula): green and blue channels shifted
|
||||
/// out of the ID's bit pattern, red unused.
|
||||
pub fn cryptomatte_preview_color(id: f32) -> [f32; 3] {
|
||||
let bits = id.to_bits();
|
||||
[0.0, f32::from_bits(bits << 8), f32::from_bits(bits << 16)]
|
||||
craft_exr::cryptomatte::preview_color(id)
|
||||
}
|
||||
|
||||
/// The `cryptomatte/<key>/<field>` text attribute of a header, if present.
|
||||
@@ -109,19 +48,10 @@ fn attribute(other: &std::collections::HashMap<exr::meta::attribute::Text, Attri
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a manifest JSON (`{"object name": "<8-hex ID bits>"}`) into (ID value, name)
|
||||
/// pairs. Entries that are not strings of up to eight hex digits are skipped, like the
|
||||
/// reference readers tolerate a partially broken manifest; a broken document yields none.
|
||||
/// A manifest JSON (`{"object name": "<8-hex ID bits>"}`) as (ID value, name) pairs, by name.
|
||||
/// Entries that are not hex strings are skipped; a broken document yields none.
|
||||
fn parse_manifest(json: &str) -> Vec<(f32, String)> {
|
||||
let Ok(value) = serde_json::from_str::<serde_json::Value>(json) else { return Vec::new() };
|
||||
let Some(map) = value.as_object() else { return Vec::new() };
|
||||
let mut out = Vec::with_capacity(map.len().min(1 << 20));
|
||||
for (name, hex) in map {
|
||||
let Some(hex) = hex.as_str() else { continue };
|
||||
let Some(bits) = u32::from_str_radix(hex.trim_start_matches("0x"), 16).ok() else { continue };
|
||||
out.push((f32::from_bits(bits), name.clone()));
|
||||
}
|
||||
out
|
||||
craft_exr::cryptomatte::parse_manifest(json).into_iter().map(|(name, bits)| (f32::from_bits(bits), name)).collect()
|
||||
}
|
||||
|
||||
/// How a layer name compares with a stream's channel prefix: the channel prefix and the `name`
|
||||
@@ -211,19 +141,8 @@ pub(crate) fn layers(bytes: &[u8], limits: &Limits) -> Result<Vec<CryptomatteLay
|
||||
|
||||
/// Every `cryptomatte/<key>/` key of a header, sorted, without duplicates.
|
||||
fn cryptomatte_keys(other: &std::collections::HashMap<exr::meta::attribute::Text, exr::meta::attribute::AttributeValue>) -> Vec<String> {
|
||||
let mut keys = Vec::new();
|
||||
// Track seen keys in a set: the attribute names are file-controlled, so membership with
|
||||
// keys.iter().any() is O(n^2) in the number of distinct `cryptomatte/<key>/<field>`
|
||||
// groups. The set only answers "seen before"; the result is sorted below.
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for name in other.keys() {
|
||||
let full = name.to_string();
|
||||
let Some(rest) = full.strip_prefix("cryptomatte/") else { continue };
|
||||
let Some((key, _field)) = rest.split_once('/') else { continue };
|
||||
if !key.is_empty() && seen.insert(key.to_string()) {
|
||||
keys.push(key.to_string());
|
||||
}
|
||||
}
|
||||
let names: Vec<String> = other.keys().map(|name| name.to_string()).collect();
|
||||
let mut keys = craft_exr::cryptomatte::keys(names.iter().map(String::as_str));
|
||||
keys.sort(); // HashMap order varies per call; layer names must stay stable
|
||||
keys
|
||||
}
|
||||
|
||||
@@ -1,857 +0,0 @@
|
||||
//! 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 [`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.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::Format;
|
||||
use crate::error::CodecError;
|
||||
use crate::fidelity::Plan;
|
||||
use crate::image::{ChannelLayout, DecodeWarning, Image, SampleType};
|
||||
use crate::options::{EncodeOptions, Limits};
|
||||
|
||||
const F: Format = Format::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)
|
||||
}
|
||||
|
||||
#[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<Value>,
|
||||
/// 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<Card>,
|
||||
/// 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<String, usize>,
|
||||
/// 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<i64> {
|
||||
match self.get(key)? {
|
||||
Value::Raw(r) => r.parse::<i64>().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<f64> {
|
||||
match self.get(key)? {
|
||||
// Fortran-style `D` exponents are allowed in FITS.
|
||||
Value::Raw(r) => r.replace(['D', 'd'], "E").parse::<f64>().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<Header, CodecError> {
|
||||
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<Vec<u64>, 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<u64, CodecError> {
|
||||
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"))
|
||||
}
|
||||
|
||||
/// 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(crate) fn has_additional_images(bytes: &[u8]) -> Result<bool, CodecError> {
|
||||
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"))
|
||||
}
|
||||
|
||||
/// 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<Header>), 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(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
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<f64> {
|
||||
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 => {
|
||||
// Admission above assumed 16-bit samples; the float fallback is twice that.
|
||||
limits.check(w, hgt, layout, SampleType::F32)?;
|
||||
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.meta.text = text;
|
||||
Ok(img)
|
||||
}
|
||||
|
||||
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::<f64>().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('\'');
|
||||
}
|
||||
}
|
||||
// Strings are at least eight characters inside the quotes.
|
||||
while s.len() < 9 {
|
||||
s.push(' ');
|
||||
}
|
||||
s.push('\'');
|
||||
s
|
||||
}
|
||||
|
||||
struct Writer {
|
||||
out: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
fn card(&mut self, text: &str) {
|
||||
let mut c: Vec<u8> = 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<char> = 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::<String>()));
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
|
||||
let img = src.converted(plan.layout, plan.sample);
|
||||
let (w, h) = img.dimensions();
|
||||
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,
|
||||
s => return Err(CodecError::encode(F, format!("unsupported sample {s:?}"))),
|
||||
};
|
||||
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");
|
||||
if opts.embed_metadata {
|
||||
for (k, v) in &img.meta.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<u8> {
|
||||
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.meta.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.meta.text = vec![("OBJECT".into(), value.clone()), ("CONTINUE".into(), "orphan".into())];
|
||||
let b = crate::encode(&img, F, &EncodeOptions::default()).unwrap();
|
||||
assert!(!b.chunks(CARD).any(|c| c.starts_with(b"CONTINUE=")));
|
||||
let decoded = decode(&b, &Limits::default()).unwrap();
|
||||
assert_eq!(decoded.meta.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.meta.text.contains(&("OBJECT".into(), "M 31".into())));
|
||||
assert!(img.meta.text.contains(&("EXPTIME".into(), "300.0".into())));
|
||||
assert!(!img.meta.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 opts = EncodeOptions::default();
|
||||
let bytes = encode(&img, crate::fidelity::plan(&img, F, &opts), &opts).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.meta.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 Batch Save and Close 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!(!crate::has_unretained_source_data(b).unwrap());
|
||||
assert_eq!(decode(b, &Limits::default()).unwrap().data(), &[42]);
|
||||
}
|
||||
// Only empty HDUs: nothing to open, nothing to lose.
|
||||
assert!(!crate::has_unretained_source_data(&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!(crate::has_unretained_source_data(&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", "photocraft 1.0");
|
||||
w.text("HISTORY", "stacked");
|
||||
let cards: Vec<Card> = 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("photocraft 1.0".into())));
|
||||
assert_eq!((cards[2].key.as_str(), cards[2].text.as_str()), ("HISTORY", "stacked"));
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
//! The colour table of a GIF, read from its blocks without decoding pixels (GIF89a
|
||||
//! specification: Logical Screen Descriptor, Global and Local Color Tables, Graphic Control
|
||||
//! Extension).
|
||||
//!
|
||||
//! A GIF is always palette-based, so it opens in Indexed Color with the table of the frame that
|
||||
//! [`crate::decode`] keeps, the first: its Local Color Table when it has one, else the Global
|
||||
//! one, and the transparent entry that frame's Graphic Control Extension names.
|
||||
|
||||
/// Colours of the table the first frame uses, and its transparent entry. `None` for anything
|
||||
/// that isn't a well-formed GIF up to its first image, or a first frame without a table.
|
||||
pub(crate) fn palette(b: &[u8]) -> Option<(Vec<[u8; 3]>, Option<u8>)> {
|
||||
if !(b.starts_with(b"GIF87a") || b.starts_with(b"GIF89a")) {
|
||||
return None;
|
||||
}
|
||||
// Logical Screen Descriptor: width, height, packed fields, background, aspect.
|
||||
let packed = *b.get(10)?;
|
||||
let mut at = 13;
|
||||
let table = |at: usize, packed: u8| -> Option<Vec<[u8; 3]>> {
|
||||
let n = 2usize << (packed & 0x07);
|
||||
let bytes = b.get(at..at.checked_add(n.checked_mul(3)?)?)?;
|
||||
Some(bytes.as_chunks::<3>().0.to_vec())
|
||||
};
|
||||
let global = if packed & 0x80 != 0 {
|
||||
let t = table(at, packed)?;
|
||||
at += t.len() * 3;
|
||||
Some(t)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let mut transparent = None;
|
||||
loop {
|
||||
match *b.get(at)? {
|
||||
// Extension: label, then data sub-blocks up to a zero-length one.
|
||||
0x21 => {
|
||||
let label = *b.get(at + 1)?;
|
||||
at += 2;
|
||||
// Graphic Control Extension: block size 4, packed (bit 0: transparent colour
|
||||
// given), delay (2 bytes), transparent colour index.
|
||||
if label == 0xF9 && *b.get(at)? == 4 {
|
||||
let flags = *b.get(at + 1)?;
|
||||
transparent = (flags & 0x01 != 0).then_some(*b.get(at + 4)?);
|
||||
}
|
||||
loop {
|
||||
let size = usize::from(*b.get(at)?);
|
||||
at = at.checked_add(1 + size)?;
|
||||
if size == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Image Descriptor: position and size (8 bytes), packed fields, then the Local Color
|
||||
// Table if bit 7 is set. This is the first frame: its table is the one used.
|
||||
0x2C => {
|
||||
let packed = *b.get(at + 9)?;
|
||||
let colors = if packed & 0x80 != 0 { table(at + 10, packed)? } else { global? };
|
||||
let transparent = transparent.filter(|i| usize::from(*i) < colors.len());
|
||||
return Some((colors, transparent));
|
||||
}
|
||||
// Trailer, or anything else: no image.
|
||||
_ => return None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::palette;
|
||||
|
||||
/// A GIF up to its first image: `global` table entries, optional GCE transparency, optional
|
||||
/// local table.
|
||||
fn gif(global: &[[u8; 3]], transparent: Option<u8>, local: Option<&[[u8; 3]]>) -> Vec<u8> {
|
||||
let size_bits = |n: usize| (n.max(2).next_power_of_two().trailing_zeros() - 1) as u8;
|
||||
let mut b = b"GIF89a".to_vec();
|
||||
b.extend([4, 0, 4, 0]);
|
||||
b.push(if global.is_empty() { 0 } else { 0x80 | size_bits(global.len()) });
|
||||
b.extend([0, 0]);
|
||||
let pad = |b: &mut Vec<u8>, t: &[[u8; 3]]| {
|
||||
let n = 2usize << size_bits(t.len());
|
||||
for k in 0..n {
|
||||
b.extend(t.get(k).copied().unwrap_or([0, 0, 0]));
|
||||
}
|
||||
};
|
||||
if !global.is_empty() {
|
||||
pad(&mut b, global);
|
||||
}
|
||||
// A comment extension first, to skip.
|
||||
b.extend([0x21, 0xFE, 3, b'h', b'i', b'!', 0]);
|
||||
if let Some(i) = transparent {
|
||||
b.extend([0x21, 0xF9, 4, 0x01, 0, 0, i, 0]);
|
||||
}
|
||||
b.extend([0x2C, 0, 0, 0, 0, 4, 0, 4, 0]);
|
||||
match local {
|
||||
Some(t) => {
|
||||
b.push(0x80 | size_bits(t.len()));
|
||||
pad(&mut b, t);
|
||||
}
|
||||
None => b.push(0),
|
||||
}
|
||||
b.extend([2, 2, 0x4C, 0x01, 0, 0x3B]);
|
||||
b
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_first_frames_table_and_transparent_entry() {
|
||||
let g = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 255]];
|
||||
assert_eq!(palette(&gif(&g, None, None)), Some((g.to_vec(), None)));
|
||||
assert_eq!(palette(&gif(&g, Some(2), None)), Some((g.to_vec(), Some(2))));
|
||||
// A local table wins over the global one.
|
||||
let l = [[1, 2, 3], [4, 5, 6]];
|
||||
assert_eq!(palette(&gif(&g, Some(1), Some(&l))), Some((l.to_vec(), Some(1))));
|
||||
// A transparent index past the table is no transparent entry.
|
||||
assert_eq!(palette(&gif(&l, Some(9), None)), Some((l.to_vec(), None)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_table_or_not_a_gif_is_none() {
|
||||
assert_eq!(palette(&gif(&[], None, None)), None, "no table at all");
|
||||
assert_eq!(palette(b"\x89PNG\r\n\x1a\n"), None);
|
||||
assert_eq!(palette(b""), None);
|
||||
let whole = gif(&[[9, 9, 9], [8, 8, 8]], Some(0), None);
|
||||
for cut in 0..whole.len() - 6 {
|
||||
// Cut short anywhere before the image descriptor's fields: never a panic.
|
||||
let _ = palette(&whole[..cut]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,544 +0,0 @@
|
||||
//! 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.
|
||||
|
||||
use crate::error::CodecError;
|
||||
use crate::fidelity::Plan;
|
||||
use crate::format::Format;
|
||||
use crate::image::{ChannelLayout, Image, SampleType};
|
||||
use crate::options::Limits;
|
||||
|
||||
const F: Format = Format::Iff;
|
||||
|
||||
fn bad(msg: impl std::fmt::Display) -> CodecError {
|
||||
CodecError::malformed(F, msg)
|
||||
}
|
||||
|
||||
fn unsupported(msg: impl Into<String>) -> CodecError {
|
||||
CodecError::unsupported(F, msg)
|
||||
}
|
||||
|
||||
fn be32(b: &[u8], at: usize) -> Option<u32> {
|
||||
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<u16> {
|
||||
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(crate) 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<Item = Chunk<'_>> {
|
||||
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<Header, CodecError> {
|
||||
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"));
|
||||
}
|
||||
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<usize, CodecError> {
|
||||
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(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
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::options::alloc_zeroed::<u8>(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<u8>) {
|
||||
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<u8>, 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(crate) fn encode(src: &Image, plan: Plan) -> Result<Vec<u8>, CodecError> {
|
||||
let enc = |msg: String| CodecError::encode(F, msg);
|
||||
let img = src.converted(plan.layout, plan.sample);
|
||||
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.dimensions();
|
||||
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<u8> {
|
||||
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]);
|
||||
}
|
||||
|
||||
fn plan(layout: ChannelLayout) -> Plan {
|
||||
Plan { layout, sample: SampleType::U8 }
|
||||
}
|
||||
|
||||
#[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<u8> = (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, plan(ChannelLayout::Rgb)).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, plan(ChannelLayout::Rgba)).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<u8>> = 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, plan(ChannelLayout::Gray)).is_err());
|
||||
let wide = Image::from_u8(65536, 1, ChannelLayout::Rgb, vec![0; 65536 * 3]).unwrap();
|
||||
assert!(encode(&wide, plan(ChannelLayout::Rgb)).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 { .. })));
|
||||
}
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
//! 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.
|
||||
|
||||
use crate::SampleType;
|
||||
use crate::error::CodecError;
|
||||
use crate::format::Format;
|
||||
use crate::image::{ChannelLayout, Image};
|
||||
use crate::options::Limits;
|
||||
|
||||
const HEADER: usize = 128;
|
||||
|
||||
fn bad(reason: impl std::fmt::Display) -> CodecError {
|
||||
CodecError::malformed(Format::Pcx, reason)
|
||||
}
|
||||
fn le16(b: &[u8], i: usize) -> u16 {
|
||||
u16::from_le_bytes([b[i], b[i + 1]])
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
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)) && (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(())
|
||||
}
|
||||
|
||||
pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
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 (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"))?;
|
||||
// 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 rgb = if bits == 8 && planes == 3 {
|
||||
[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[(y * width + x) * 3..(y * width + x + 1) * 3].copy_from_slice(&rgb);
|
||||
}
|
||||
}
|
||||
let mut image = Image::from_u8(w, h, ChannelLayout::Rgb, 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)));
|
||||
}
|
||||
Ok(image)
|
||||
}
|
||||
@@ -1,355 +0,0 @@
|
||||
//! 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.
|
||||
|
||||
use crate::Format;
|
||||
use crate::error::CodecError;
|
||||
use crate::fidelity::Plan;
|
||||
use crate::image::{ChannelLayout, Image, SampleType};
|
||||
use crate::options::{EncodeOptions, Limits};
|
||||
|
||||
const F: Format = Format::Pnm;
|
||||
|
||||
fn err(msg: impl std::fmt::Display) -> CodecError {
|
||||
CodecError::malformed(F, msg)
|
||||
}
|
||||
|
||||
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<u32, CodecError> {
|
||||
let t = self.token()?;
|
||||
std::str::from_utf8(t).ok().and_then(|s| s.parse::<u32>().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<SampleType, CodecError> {
|
||||
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 (#2938).
|
||||
struct Samples {
|
||||
maxval: u32,
|
||||
sample: SampleType,
|
||||
bytes: Vec<u8>,
|
||||
}
|
||||
|
||||
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<Self, CodecError> {
|
||||
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, CodecError> {
|
||||
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<usize, CodecError> {
|
||||
(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<Samples, CodecError> {
|
||||
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)
|
||||
}
|
||||
|
||||
pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
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 (#1110).
|
||||
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 (#1110).
|
||||
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<f32>`
|
||||
// would double the budgeted allocation (#2938).
|
||||
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")),
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_pam(r: &mut Reader<'_>, limits: &Limits) -> Result<Image, CodecError> {
|
||||
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")),
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
pub(crate) fn encode(src: &Image, plan: Plan, _opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
|
||||
let img = src.converted(plan.layout, plan.sample);
|
||||
let (w, h) = img.dimensions();
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
s => return Err(CodecError::encode(F, format!("unsupported sample {s:?}"))),
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
//! DDS, DICOM, FITS, IFF/ILBM, PCX, PNM and XPM: the format code is craft-libs'
|
||||
//! `craft-raster-codecs`. This maps its image, limits and errors onto this crate's, and the
|
||||
//! encoders take this crate's [`Plan`] (the layout and depth conversion stays here).
|
||||
|
||||
use craft_raster_codecs as rc;
|
||||
|
||||
use crate::error::CodecError;
|
||||
use crate::fidelity::Plan;
|
||||
use crate::format::Format;
|
||||
use crate::image::{ChannelLayout, DecodeWarning, Image, SampleType};
|
||||
use crate::options::Limits;
|
||||
|
||||
fn limits(l: &Limits) -> rc::Limits {
|
||||
rc::Limits { max_width: l.max_width, max_height: l.max_height, max_pixels: l.max_pixels, max_alloc: l.max_alloc }
|
||||
}
|
||||
|
||||
fn err(format: Format, e: rc::CodecError) -> CodecError {
|
||||
match e {
|
||||
rc::CodecError::Unsupported { reason, .. } => CodecError::Unsupported { format, reason },
|
||||
rc::CodecError::Malformed { message, .. } => CodecError::Malformed { format, message },
|
||||
rc::CodecError::LimitExceeded(m) => CodecError::LimitExceeded(m),
|
||||
rc::CodecError::Encode { message, .. } => CodecError::Encode { format, message },
|
||||
rc::CodecError::InvalidImage(m) => CodecError::InvalidImage(m),
|
||||
}
|
||||
}
|
||||
|
||||
/// A decoded image as this crate's.
|
||||
fn from_rc(format: Format, img: rc::Image) -> Result<Image, CodecError> {
|
||||
let layout = match img.layout() {
|
||||
rc::ChannelLayout::Gray => ChannelLayout::Gray,
|
||||
rc::ChannelLayout::GrayA => ChannelLayout::GrayA,
|
||||
rc::ChannelLayout::Rgb => ChannelLayout::Rgb,
|
||||
rc::ChannelLayout::Rgba => ChannelLayout::Rgba,
|
||||
rc::ChannelLayout::Cmyk => ChannelLayout::Cmyk,
|
||||
rc::ChannelLayout::CmykA => ChannelLayout::CmykA,
|
||||
};
|
||||
let sample = match img.sample_type() {
|
||||
rc::SampleType::U8 => SampleType::U8,
|
||||
rc::SampleType::U16 => SampleType::U16,
|
||||
rc::SampleType::F32 => SampleType::F32,
|
||||
};
|
||||
let (w, h) = img.dimensions();
|
||||
let (dpi, text) = (img.dpi, img.text.clone());
|
||||
let warnings: Vec<DecodeWarning> = img.warnings.iter().map(|rc::DecodeWarning::MorePages { total }| DecodeWarning::MorePages { total: *total }).collect();
|
||||
let mut out = Image::from_raw(w, h, layout, sample, img.into_data()).map_err(|e| CodecError::malformed(format, e))?;
|
||||
out.meta.dpi = dpi;
|
||||
out.meta.text = text;
|
||||
out.warnings = warnings;
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// `image` converted as `plan` says, as the shared crate's image (with its resolution and text).
|
||||
fn to_rc(format: Format, image: &Image, plan: Plan) -> Result<rc::Image, CodecError> {
|
||||
let img = image.converted(plan.layout, plan.sample);
|
||||
let layout = match img.layout() {
|
||||
ChannelLayout::Gray => rc::ChannelLayout::Gray,
|
||||
ChannelLayout::GrayA => rc::ChannelLayout::GrayA,
|
||||
ChannelLayout::Rgb => rc::ChannelLayout::Rgb,
|
||||
ChannelLayout::Rgba => rc::ChannelLayout::Rgba,
|
||||
ChannelLayout::Cmyk => rc::ChannelLayout::Cmyk,
|
||||
ChannelLayout::CmykA => rc::ChannelLayout::CmykA,
|
||||
l => return Err(CodecError::encode(format, format!("cannot store {l:?}"))),
|
||||
};
|
||||
let sample = match img.sample_type() {
|
||||
SampleType::U8 => rc::SampleType::U8,
|
||||
SampleType::U16 => rc::SampleType::U16,
|
||||
SampleType::F32 => rc::SampleType::F32,
|
||||
s => return Err(CodecError::encode(format, format!("unsupported sample {s:?}"))),
|
||||
};
|
||||
let (w, h) = img.dimensions();
|
||||
let mut out = rc::Image::from_raw(w, h, layout, sample, img.data().to_vec()).map_err(|e| err(format, e))?;
|
||||
out.dpi = img.meta.dpi;
|
||||
out.text = img.meta.text.clone();
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
pub(crate) mod dds {
|
||||
use super::{CodecError, Format, Image, Limits, Plan, SampleType, err, from_rc, limits, rc, to_rc};
|
||||
use crate::options::EncodeOptions;
|
||||
const F: Format = Format::Dds;
|
||||
|
||||
pub(crate) fn has_additional_images(bytes: &[u8]) -> Result<bool, CodecError> {
|
||||
rc::dds::has_additional_images(bytes).map_err(|e| err(F, e))
|
||||
}
|
||||
pub(crate) fn decode(b: &[u8], l: &Limits) -> Result<Image, CodecError> {
|
||||
from_rc(F, rc::dds::decode(b, &limits(l)).map_err(|e| err(F, e))?)
|
||||
}
|
||||
pub(crate) fn encode(image: &Image, plan: Plan, opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
|
||||
let img = to_rc(F, image, Plan { sample: SampleType::U8, ..plan })?;
|
||||
rc::dds::encode(&img, opts.dds).map_err(|e| err(F, e))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod dicom {
|
||||
use super::{CodecError, Format, Image, Limits, err, from_rc, limits, rc};
|
||||
const F: Format = Format::Dicom;
|
||||
|
||||
pub(crate) fn is_dicom(b: &[u8]) -> bool {
|
||||
rc::dicom::is_dicom(b)
|
||||
}
|
||||
pub(crate) fn decode(b: &[u8], l: &Limits) -> Result<Image, CodecError> {
|
||||
from_rc(F, rc::dicom::decode(b, &limits(l)).map_err(|e| err(F, e))?)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod fits {
|
||||
use super::{CodecError, Format, Image, Limits, Plan, err, from_rc, limits, rc, to_rc};
|
||||
use crate::options::EncodeOptions;
|
||||
const F: Format = Format::Fits;
|
||||
|
||||
pub(crate) fn has_additional_images(bytes: &[u8]) -> Result<bool, CodecError> {
|
||||
rc::fits::has_additional_images(bytes).map_err(|e| err(F, e))
|
||||
}
|
||||
pub(crate) fn decode(b: &[u8], l: &Limits) -> Result<Image, CodecError> {
|
||||
from_rc(F, rc::fits::decode(b, &limits(l)).map_err(|e| err(F, e))?)
|
||||
}
|
||||
pub(crate) fn encode(image: &Image, plan: Plan, _opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
|
||||
rc::fits::encode(&to_rc(F, image, plan)?).map_err(|e| err(F, e))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod iff {
|
||||
use super::{CodecError, Format, Image, Limits, Plan, err, from_rc, limits, rc, to_rc};
|
||||
const F: Format = Format::Iff;
|
||||
|
||||
pub(crate) fn looks_like_iff(b: &[u8]) -> bool {
|
||||
rc::iff::looks_like_iff(b)
|
||||
}
|
||||
pub(crate) fn decode(b: &[u8], l: &Limits) -> Result<Image, CodecError> {
|
||||
from_rc(F, rc::iff::decode(b, &limits(l)).map_err(|e| err(F, e))?)
|
||||
}
|
||||
pub(crate) fn encode(image: &Image, plan: Plan) -> Result<Vec<u8>, CodecError> {
|
||||
rc::iff::encode(&to_rc(F, image, plan)?).map_err(|e| err(F, e))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod pcx {
|
||||
use super::{CodecError, Format, Image, Limits, err, from_rc, limits, rc};
|
||||
const F: Format = Format::Pcx;
|
||||
|
||||
pub(crate) fn looks_like_pcx(b: &[u8]) -> bool {
|
||||
rc::pcx::looks_like_pcx(b)
|
||||
}
|
||||
pub(crate) fn decode(b: &[u8], l: &Limits) -> Result<Image, CodecError> {
|
||||
from_rc(F, rc::pcx::decode(b, &limits(l)).map_err(|e| err(F, e))?)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod pnm {
|
||||
use super::{CodecError, Format, Image, Limits, Plan, err, from_rc, limits, rc, to_rc};
|
||||
use crate::options::EncodeOptions;
|
||||
const F: Format = Format::Pnm;
|
||||
|
||||
pub(crate) fn decode(b: &[u8], l: &Limits) -> Result<Image, CodecError> {
|
||||
from_rc(F, rc::pnm::decode(b, &limits(l)).map_err(|e| err(F, e))?)
|
||||
}
|
||||
pub(crate) fn encode(image: &Image, plan: Plan, _opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
|
||||
rc::pnm::encode(&to_rc(F, image, plan)?).map_err(|e| err(F, e))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod xpm {
|
||||
use super::{CodecError, Format, Image, Limits, err, from_rc, limits, rc};
|
||||
const F: Format = Format::Xpm;
|
||||
|
||||
pub(crate) fn decode(b: &[u8], l: &Limits) -> Result<Image, CodecError> {
|
||||
from_rc(F, rc::xpm::decode(b, &limits(l)).map_err(|e| err(F, e))?)
|
||||
}
|
||||
}
|
||||
+13
-162
@@ -1,175 +1,26 @@
|
||||
//! Targa writer. The `image` crate still reads Targa ([`super::via_image`]); writing is ours
|
||||
//! because its run-length packets ran on from the end of one row into the next. TGA 2.0 says a
|
||||
//! packet must not cross a scanline, and readers that decode a row at a time (Pillow, texture
|
||||
//! converters, game engines) misread such files (#6382).
|
||||
//!
|
||||
//! Like Photoshop's Targa: RLE (types 10 and 11), rows stored bottom-up (the bottom-left origin
|
||||
//! every reader assumes, also those that ignore the origin bits), alpha bits in the descriptor,
|
||||
//! and the TGA 2.0 footer.
|
||||
//! Targa writer: craft-libs' `craft-tga` (run-length packets that end with their scanline, rows
|
||||
//! bottom-up, alpha bits in the descriptor, the TGA 2.0 footer, #6382). The `image` crate still
|
||||
//! reads Targa ([`super::via_image`]).
|
||||
|
||||
use crate::Format;
|
||||
use crate::error::CodecError;
|
||||
use crate::image::{ChannelLayout, Image, SampleType};
|
||||
|
||||
/// Most pixels one packet holds (a 7-bit count, plus one).
|
||||
const MAX_PACKET: usize = 128;
|
||||
|
||||
/// The TGA 2.0 footer: no extension or developer area, then the signature.
|
||||
const FOOTER: &[u8; 26] = b"\0\0\0\0\0\0\0\0TRUEVISION-XFILE.\0";
|
||||
|
||||
/// Encodes an 8-bit Gray, Gray+alpha, RGB or RGBA image as a run-length encoded Targa.
|
||||
pub(crate) fn encode(img: &Image) -> Result<Vec<u8>, CodecError> {
|
||||
let (w, h) = img.dimensions();
|
||||
if img.sample_type() != SampleType::U8 {
|
||||
return Err(CodecError::encode(Format::Tga, format!("{:?} samples (Targa stores 8 bits)", img.sample_type())));
|
||||
}
|
||||
let (bpp, image_type, alpha_bits) = match img.layout() {
|
||||
ChannelLayout::Gray => (1, 11, 0),
|
||||
ChannelLayout::GrayA => (2, 11, 8),
|
||||
ChannelLayout::Rgb => (3, 10, 0),
|
||||
ChannelLayout::Rgba => (4, 10, 8),
|
||||
let layout = match img.layout() {
|
||||
ChannelLayout::Gray => craft_tga::Layout::Gray,
|
||||
ChannelLayout::GrayA => craft_tga::Layout::GrayAlpha,
|
||||
ChannelLayout::Rgb => craft_tga::Layout::Rgb,
|
||||
ChannelLayout::Rgba => craft_tga::Layout::Rgba,
|
||||
l => return Err(CodecError::encode(Format::Tga, format!("no Targa layout for {l:?}"))),
|
||||
};
|
||||
let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(h)) else {
|
||||
return Err(CodecError::encode(Format::Tga, format!("dimensions exceed 65535x65535: {w}x{h}")));
|
||||
};
|
||||
let row = w as usize * bpp;
|
||||
let data = img.data();
|
||||
if data.len() != row * h as usize {
|
||||
return Err(CodecError::InvalidImage("buffer size mismatch".into()));
|
||||
}
|
||||
let mut out = Vec::with_capacity(18 + data.len() + data.len() / MAX_PACKET + FOOTER.len());
|
||||
out.extend_from_slice(&[0, 0, image_type, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
out.extend_from_slice(&w16.to_le_bytes());
|
||||
out.extend_from_slice(&h16.to_le_bytes());
|
||||
// Bits per pixel; the descriptor holds the alpha bits, origin bits 0 (bottom-left).
|
||||
out.extend_from_slice(&[(bpp * 8) as u8, alpha_bits]);
|
||||
let mut px = vec![0u8; row];
|
||||
for y in (0..h as usize).rev() {
|
||||
px.copy_from_slice(&data[y * row..(y + 1) * row]);
|
||||
// Targa stores colour as BGR.
|
||||
if bpp >= 3 {
|
||||
for p in px.chunks_exact_mut(bpp) {
|
||||
p.swap(0, 2);
|
||||
}
|
||||
}
|
||||
rle_row(&px, bpp, &mut out);
|
||||
}
|
||||
out.extend_from_slice(FOOTER);
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// One scanline as run-length packets that end with it: a run of two or more equal pixels is a
|
||||
/// run packet, anything else goes out in raw packets.
|
||||
fn rle_row(row: &[u8], bpp: usize, out: &mut Vec<u8>) {
|
||||
let px: Vec<&[u8]> = row.chunks_exact(bpp).collect();
|
||||
let n = px.len();
|
||||
let mut i = 0;
|
||||
while i < n {
|
||||
let mut run = 1;
|
||||
while i + run < n && run < MAX_PACKET && px[i + run] == px[i] {
|
||||
run += 1;
|
||||
}
|
||||
if run >= 2 {
|
||||
out.push(0x80 | (run - 1) as u8);
|
||||
out.extend_from_slice(px[i]);
|
||||
i += run;
|
||||
continue;
|
||||
}
|
||||
// Raw until two equal pixels start a run, the packet is full or the row ends. The first
|
||||
// pixel always goes in: it differs from the one after it, or it is the row's last.
|
||||
let start = i;
|
||||
while i < n && i - start < MAX_PACKET && !(i > start && i + 1 < n && px[i] == px[i + 1]) {
|
||||
i += 1;
|
||||
}
|
||||
out.push((i - start - 1) as u8);
|
||||
for p in &px[start..i] {
|
||||
out.extend_from_slice(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn run_packets_at_the_128_pixel_boundary() {
|
||||
for bpp in 1..=4 {
|
||||
for (pixels, headers) in [(1, vec![0]), (2, vec![0x81]), (127, vec![0xfe]), (128, vec![0xff]), (129, vec![0xff, 0])] {
|
||||
let pixel: Vec<u8> = (1..=bpp as u8).collect();
|
||||
let mut encoded = Vec::new();
|
||||
rle_row(&pixel.repeat(pixels), bpp, &mut encoded);
|
||||
let expected: Vec<u8> = headers.into_iter().flat_map(|header| std::iter::once(header).chain(pixel.iter().copied())).collect();
|
||||
assert_eq!(encoded, expected, "{pixels} pixels, {bpp} bytes per pixel");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_packets_at_the_128_pixel_boundary() {
|
||||
for bpp in 1..=4 {
|
||||
for pixels in [127usize, 128, 129] {
|
||||
let row: Vec<u8> = (0..pixels).flat_map(|x| vec![x as u8; bpp]).collect();
|
||||
let mut encoded = Vec::new();
|
||||
rle_row(&row, bpp, &mut encoded);
|
||||
let mut expected = vec![(pixels.min(128) - 1) as u8];
|
||||
expected.extend_from_slice(&row[..pixels.min(128) * bpp]);
|
||||
if pixels > 128 {
|
||||
expected.push(0);
|
||||
expected.extend_from_slice(&row[128 * bpp..]);
|
||||
}
|
||||
assert_eq!(encoded, expected, "{pixels} pixels, {bpp} bytes per pixel");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_packets_stop_before_a_run_of_complete_pixels() {
|
||||
// Equal first channels alone do not make an RGBA run; alpha participates too.
|
||||
let a = [10, 20, 30, 40];
|
||||
let b = [10, 20, 30, 41];
|
||||
let c = [50, 60, 70, 80];
|
||||
let row = [a, b, c, c, a].concat();
|
||||
let mut encoded = Vec::new();
|
||||
rle_row(&row, 4, &mut encoded);
|
||||
assert_eq!(encoded, [vec![1], a.to_vec(), b.to_vec(), vec![0x81], c.to_vec(), vec![0], a.to_vec()].concat());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_sample_types_and_layouts_targa_cannot_store() {
|
||||
for sample in [SampleType::U16, SampleType::F16, SampleType::F32] {
|
||||
let img = Image::new(1, 1, ChannelLayout::Rgb, sample).unwrap();
|
||||
assert!(matches!(encode(&img), Err(CodecError::Encode { format: Format::Tga, .. })), "{sample:?}");
|
||||
}
|
||||
for layout in [ChannelLayout::Cmyk, ChannelLayout::CmykA, ChannelLayout::Lab, ChannelLayout::LabA] {
|
||||
let img = Image::new(1, 1, layout, SampleType::U8).unwrap();
|
||||
assert!(matches!(encode(&img), Err(CodecError::Encode { format: Format::Tga, .. })), "{layout:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dimensions_fit_the_unsigned_16_bit_header() {
|
||||
for (width, height) in [(65535, 1), (1, 65535)] {
|
||||
let img = Image::new(width, height, ChannelLayout::Gray, SampleType::U8).unwrap();
|
||||
let file = encode(&img).unwrap();
|
||||
assert_eq!(&file[12..14], &(width as u16).to_le_bytes());
|
||||
assert_eq!(&file[14..16], &(height as u16).to_le_bytes());
|
||||
}
|
||||
for (width, height) in [(65536, 1), (1, 65536)] {
|
||||
let img = Image::new(width, height, ChannelLayout::Gray, SampleType::U8).unwrap();
|
||||
assert!(matches!(encode(&img), Err(CodecError::Encode { format: Format::Tga, .. })), "{width}x{height}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_buffers_are_rejected_before_targa_encoding() {
|
||||
// Image keeps its buffer private, so malformed buffers must fail at construction.
|
||||
for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] {
|
||||
let len = 2 * 2 * layout.channels();
|
||||
for bytes in [len - 1, len + 1] {
|
||||
assert!(matches!(Image::from_u8(2, 2, layout, vec![0; bytes]), Err(CodecError::InvalidImage(_))), "{layout:?}: {bytes} bytes");
|
||||
}
|
||||
}
|
||||
}
|
||||
let (w, h) = img.dimensions();
|
||||
craft_tga::encode(w, h, layout, img.data()).map_err(|e| match e {
|
||||
craft_tga::Error::Encode(m) if m == "buffer size mismatch" => CodecError::InvalidImage(m),
|
||||
e => CodecError::encode(Format::Tga, e),
|
||||
})
|
||||
}
|
||||
@@ -231,75 +231,29 @@ fn bmp_is_rle(b: &[u8]) -> bool {
|
||||
u32_at(14).is_some_and(|n| n >= 40) && matches!(u32_at(30), Some(1 | 2))
|
||||
}
|
||||
|
||||
/// The blocks of a GIF (craft-gif), every frame recorded. The frame list is bounded by the file:
|
||||
/// each image descriptor takes at least 11 of its bytes.
|
||||
fn gif_scan(b: &[u8]) -> Option<craft_gif::Info> {
|
||||
craft_gif::scan(b, usize::MAX)
|
||||
}
|
||||
|
||||
/// The width and height of a GIF's first image descriptor, if the blocks reach one.
|
||||
fn gif_first_frame(b: &[u8]) -> Option<(u32, u32)> {
|
||||
let mut at = None;
|
||||
gif_walk(b, |i| {
|
||||
at = Some(i);
|
||||
false
|
||||
});
|
||||
let u16_at = |at: usize| b.get(at..at + 2).and_then(|b| b.try_into().ok()).map(u16::from_le_bytes);
|
||||
// Descriptor: separator, left, top, width, height (little-endian u16s), packed fields.
|
||||
let i = at?;
|
||||
Some((u32::from(u16_at(i + 5)?), u32::from(u16_at(i + 7)?)))
|
||||
craft_gif::scan(b, 1)?.frames.first().map(|f| (u32::from(f.width), u32::from(f.height)))
|
||||
}
|
||||
|
||||
/// Counts a GIF's frames (image descriptors) by walking its blocks, without decompressing any.
|
||||
/// The total is unknown when the blocks end before the trailer or break off.
|
||||
pub(crate) fn gif_more_frames(b: &[u8]) -> Option<DecodeWarning> {
|
||||
let (frames, total) = gif_walk(b, |_| true);
|
||||
(frames > 1).then_some(DecodeWarning::MoreFrames { total })
|
||||
let info = gif_scan(b)?;
|
||||
let total = info.complete.then(|| u32::try_from(info.frames.len()).unwrap_or(u32::MAX));
|
||||
(info.frames.len() > 1).then_some(DecodeWarning::MoreFrames { total })
|
||||
}
|
||||
|
||||
/// Whether a GIF holds more than one frame, or None when the walk stops before the trailer
|
||||
/// after at most one frame (so frames past the break could not be counted).
|
||||
pub(crate) fn gif_has_more_frames(b: &[u8]) -> Option<bool> {
|
||||
let (frames, total) = gif_walk(b, |_| true);
|
||||
if frames > 1 { Some(true) } else { total.map(|_| false) }
|
||||
}
|
||||
|
||||
/// Walks a GIF's blocks without decompressing any, handing `frame` the offset of each image
|
||||
/// descriptor until it returns false. Gives the descriptors seen, and the frame count if the
|
||||
/// blocks reached the trailer.
|
||||
fn gif_walk(b: &[u8], mut frame: impl FnMut(usize) -> bool) -> (u32, Option<u32>) {
|
||||
// Size of the colour table a packed-fields byte announces.
|
||||
let table = |flags: u8| if flags & 0x80 != 0 { 3usize << ((flags & 7) + 1) } else { 0 };
|
||||
// Skips the data sub-blocks starting at `i` (each length-prefixed, ended by a zero length).
|
||||
let skip = |mut i: usize| -> Option<usize> {
|
||||
loop {
|
||||
let n = usize::from(*b.get(i)?);
|
||||
i = i.checked_add(1 + n)?;
|
||||
if n == 0 {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
};
|
||||
// Header (6 bytes), logical screen descriptor (7) and the global colour table.
|
||||
let Some(&flags) = b.get(10) else {
|
||||
return (0, None);
|
||||
};
|
||||
let mut i = 13 + table(flags);
|
||||
let mut frames = 0u32;
|
||||
let total = loop {
|
||||
let next = match b.get(i) {
|
||||
Some(0x3B) => break Some(frames),
|
||||
Some(0x21) => skip(i + 2),
|
||||
Some(0x2C) => {
|
||||
frames = frames.saturating_add(1);
|
||||
if !frame(i) {
|
||||
break None;
|
||||
}
|
||||
// Descriptor (10 bytes, packed fields last), local colour table, LZW code size.
|
||||
b.get(i + 9).and_then(|&flags| skip(i + 11 + table(flags)))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
match next {
|
||||
Some(n) => i = n,
|
||||
None => break None,
|
||||
}
|
||||
};
|
||||
(frames, total)
|
||||
gif_scan(b)?.has_more_frames()
|
||||
}
|
||||
|
||||
pub(crate) fn from_dynamic(f: Format, d: DynamicImage) -> Result<Image, CodecError> {
|
||||
|
||||
@@ -1,226 +0,0 @@
|
||||
//! 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.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
mod x11_colors;
|
||||
|
||||
use crate::SampleType;
|
||||
use crate::error::CodecError;
|
||||
use crate::format::Format;
|
||||
use crate::image::{ChannelLayout, Image};
|
||||
use crate::options::Limits;
|
||||
|
||||
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(Format::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<Vec<Vec<u8>>, 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<Vec<Vec<u8>>, 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<u8> {
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
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::<Option<Vec<_>>>()?;
|
||||
[rgb[0], rgb[1], rgb[2]]
|
||||
} else {
|
||||
named(name)?
|
||||
};
|
||||
Some([rgb[0], rgb[1], rgb[2], 255])
|
||||
}
|
||||
|
||||
pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
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<u8>], limits: &Limits) -> Result<Image, CodecError> {
|
||||
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<usize, CodecError> {
|
||||
s.ok_or_else(|| bad(format!("missing {key}")))?.parse::<usize>().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::<Vec<u8>, [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)
|
||||
}
|
||||
@@ -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: https://cgit.freedesktop.org/xorg/app/rgb/tree/rgb.txt
|
||||
//! 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]),
|
||||
];
|
||||
+12
-57
@@ -373,69 +373,24 @@ impl ExrPartInfo {
|
||||
}
|
||||
}
|
||||
|
||||
/// One selection policy for header ranking/probing and flat decoding: never synthesize
|
||||
/// RGB(A) or YA from separate layers. OpenEXR layers use the full prefix before the last
|
||||
/// dot, including nested layers; a leading or trailing dot alone does not name a layer.
|
||||
/// One selection policy for header ranking/probing and flat decoding (craft-exr's): never
|
||||
/// synthesize RGB(A) or YA from separate layers.
|
||||
pub(crate) fn exr_color_channels<'a>(names: impl IntoIterator<Item = &'a str>) -> Option<(ChannelLayout, Vec<usize>)> {
|
||||
#[derive(Default)]
|
||||
struct Group {
|
||||
r: Option<usize>,
|
||||
g: Option<usize>,
|
||||
b: Option<usize>,
|
||||
a: Option<usize>,
|
||||
y: Option<usize>,
|
||||
}
|
||||
|
||||
// Sorted prefixes make rank ties deterministic independently of channel order, with
|
||||
// the empty (unprefixed) group first. Unsupported channels cannot complete a group.
|
||||
let mut groups = std::collections::BTreeMap::<&str, Group>::new();
|
||||
let mut count = 0usize;
|
||||
for (index, name) in names.into_iter().enumerate() {
|
||||
count = count.saturating_add(1);
|
||||
let (prefix, base) = name.rsplit_once('.').filter(|(prefix, base)| !prefix.is_empty() && !base.is_empty()).unwrap_or(("", name));
|
||||
if !matches!(base, "R" | "G" | "B" | "A" | "Y") {
|
||||
continue;
|
||||
}
|
||||
let group = groups.entry(prefix).or_default();
|
||||
let component = match base {
|
||||
"R" => &mut group.r,
|
||||
"G" => &mut group.g,
|
||||
"B" => &mut group.b,
|
||||
"A" => &mut group.a,
|
||||
"Y" => &mut group.y,
|
||||
_ => continue,
|
||||
use craft_exr::channels::ColorLayout;
|
||||
craft_exr::channels::color_channels(names).map(|(layout, indices)| {
|
||||
let layout = match layout {
|
||||
ColorLayout::Y => ChannelLayout::Gray,
|
||||
ColorLayout::YA => ChannelLayout::GrayA,
|
||||
ColorLayout::Rgb => ChannelLayout::Rgb,
|
||||
ColorLayout::Rgba => ChannelLayout::Rgba,
|
||||
};
|
||||
component.get_or_insert(index);
|
||||
}
|
||||
|
||||
let mut best = None;
|
||||
let mut best_rank = 0;
|
||||
for group in groups.values() {
|
||||
let candidate = if let (Some(r), Some(g), Some(b)) = (group.r, group.g, group.b) {
|
||||
match group.a {
|
||||
Some(a) => (ChannelLayout::Rgba, vec![r, g, b, a]),
|
||||
None => (ChannelLayout::Rgb, vec![r, g, b]),
|
||||
}
|
||||
} else if let Some(y) = group.y {
|
||||
match group.a {
|
||||
Some(a) => (ChannelLayout::GrayA, vec![y, a]),
|
||||
None => (ChannelLayout::Gray, vec![y]),
|
||||
}
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
let rank = candidate.0.channels();
|
||||
if rank > best_rank {
|
||||
best = Some(candidate);
|
||||
best_rank = rank;
|
||||
}
|
||||
}
|
||||
best.or_else(|| (count == 1).then(|| (ChannelLayout::Gray, vec![0])))
|
||||
(layout, indices)
|
||||
})
|
||||
}
|
||||
|
||||
/// Strip an optional `layer.` prefix from a channel name (`diffuse.R` → `R`).
|
||||
pub(crate) fn base_channel_name(name: &str) -> &str {
|
||||
name.rsplit('.').next().unwrap_or(name)
|
||||
craft_exr::channels::base_channel_name(name)
|
||||
}
|
||||
|
||||
/// One Cryptomatte layer of an EXR file (Cryptomatte specification 1.2): per-pixel object or
|
||||
|
||||
@@ -210,7 +210,7 @@ pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> {
|
||||
/// 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<u8>)> {
|
||||
crate::codecs::gif_palette::palette(bytes)
|
||||
craft_gif::palette(bytes)
|
||||
}
|
||||
|
||||
/// Returns the embedded colour table for an indexed BMP, if it has one.
|
||||
|
||||
@@ -235,60 +235,8 @@ impl DpxBits {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 fn name(self) -> &'static str {
|
||||
match self {
|
||||
DdsCompression::None => "none",
|
||||
DdsCompression::Bc1 => "bc1",
|
||||
DdsCompression::Bc2 => "bc2",
|
||||
DdsCompression::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<Self> {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
/// A block compression (lossy), not uncompressed pixels.
|
||||
pub fn is_block(self) -> bool {
|
||||
self != DdsCompression::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,
|
||||
}
|
||||
/// How a DDS texture is written: from craft-raster-codecs, which writes it.
|
||||
pub use craft_raster_codecs::dds::{DdsCompression, DdsOptions};
|
||||
|
||||
/// The longer side, in pixels, at which a TIFF image pyramid stops: the last level written is
|
||||
/// the first one no longer than this ([`EncodeOptions::tiff_pyramid`]).
|
||||
|
||||
@@ -82,19 +82,27 @@ impl Fixture {
|
||||
(8, _) => out.extend(codes.iter().map(|&c| c as u8)),
|
||||
(16, _) => out.extend(codes.iter().flat_map(|&c| self.u16(c))),
|
||||
(10, 1 | 2) => {
|
||||
let base = if self.packing == 1 { 22 } else { 20 };
|
||||
// Method A pads the bottom two bits, method B the top two. The first sample sits in
|
||||
// the high bits, except in a luma element, which fills each word from its low end
|
||||
// (as FFmpeg and ImageMagick read and write it).
|
||||
let pad = if self.packing == 1 { 2 } else { 0 };
|
||||
let luma = self.descriptor == 6;
|
||||
for chunk in codes.chunks(3) {
|
||||
let w = chunk.iter().enumerate().fold(0u32, |w, (i, &c)| w | (u32::from(c) << (base - 10 * i as u32)));
|
||||
let w = chunk.iter().enumerate().fold(0u32, |w, (i, &c)| {
|
||||
let slot = if luma { i as u32 } else { 2 - i as u32 };
|
||||
w | (u32::from(c) << (pad + 10 * slot))
|
||||
});
|
||||
out.extend(self.u32(w));
|
||||
}
|
||||
}
|
||||
(12, 1) => out.extend(codes.iter().flat_map(|&c| self.u16(c << 4))),
|
||||
_ => {
|
||||
// One bit stream, high bits first, cut into 32-bit words.
|
||||
let mut bits: Vec<bool> = codes.iter().flat_map(|&c| (0..self.bits).rev().map(move |b| (c >> b) & 1 == 1)).collect();
|
||||
// Packing 0: the samples follow each other through the line's 32-bit words, the
|
||||
// first in the low bits (as FFmpeg and ImageMagick read it).
|
||||
let mut bits: Vec<bool> = codes.iter().flat_map(|&c| (0..self.bits).map(move |b| (c >> b) & 1 == 1)).collect();
|
||||
bits.resize(bits.len().div_ceil(32) * 32, false);
|
||||
for word in bits.chunks(32) {
|
||||
out.extend(self.u32(word.iter().fold(0u32, |w, &b| (w << 1) | u32::from(b))));
|
||||
out.extend(self.u32(word.iter().rev().fold(0u32, |w, &b| (w << 1) | u32::from(b))));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -177,6 +185,8 @@ fn written_codes(b: &[u8]) -> (u8, u16, Vec<u16>) {
|
||||
for i in 0..per_line {
|
||||
codes.push(match bits {
|
||||
8 => u16::from(line[i]),
|
||||
// A luma element fills each word from its low end.
|
||||
10 if nc == 1 => ((be32_at(line, i / 3 * 4) >> (2 + 10 * (i % 3) as u32)) & 0x3FF) as u16,
|
||||
10 => ((be32_at(line, i / 3 * 4) >> (22 - 10 * (i % 3) as u32)) & 0x3FF) as u16,
|
||||
12 => u16::from_be_bytes([line[i * 2], line[i * 2 + 1]]) >> 4,
|
||||
_ => u16::from_be_bytes([line[i * 2], line[i * 2 + 1]]),
|
||||
@@ -405,9 +415,10 @@ fn unsupported_and_damaged_files_error_instead_of_panicking() {
|
||||
let mut float = Fixture::new(50, 16, 0, 2, 2).bytes();
|
||||
float[ELEMENT0 + 23] = 32;
|
||||
unsupported(float, "32-bit float");
|
||||
// 12-bit method B (the value in the low bits of each 16-bit word) is read since craft-dpx.
|
||||
let mut method_b = Fixture::new(50, 12, 1, 2, 2).bytes();
|
||||
method_b[ELEMENT0 + 24..ELEMENT0 + 26].copy_from_slice(&2u16.to_be_bytes());
|
||||
unsupported(method_b, "12-bit method B");
|
||||
assert!(decode(&method_b).is_ok(), "12-bit method B");
|
||||
|
||||
let good = Fixture::new(51, 10, 1, 9, 9).bytes();
|
||||
let mut no_elements = good.clone();
|
||||
@@ -438,9 +449,13 @@ fn cineon_bytes(channels: u8, width: u32, height: u32, codes: &[u16]) -> Vec<u8>
|
||||
b[0..4].copy_from_slice(&0x802A_5FD7u32.to_be_bytes());
|
||||
b[4..8].copy_from_slice(&2048u32.to_be_bytes());
|
||||
b[193] = channels;
|
||||
b[198] = 10;
|
||||
b[200..204].copy_from_slice(&width.to_be_bytes());
|
||||
b[204..208].copy_from_slice(&height.to_be_bytes());
|
||||
// One descriptor per channel, all alike.
|
||||
for c in 0..usize::from(channels) {
|
||||
let at = 196 + 28 * c;
|
||||
b[at + 2] = 10;
|
||||
b[at + 4..at + 8].copy_from_slice(&width.to_be_bytes());
|
||||
b[at + 8..at + 12].copy_from_slice(&height.to_be_bytes());
|
||||
}
|
||||
for line in codes.chunks(width as usize * usize::from(channels)) {
|
||||
for chunk in line.chunks(3) {
|
||||
let w = chunk.iter().enumerate().fold(0u32, |w, (i, &c)| w | (u32::from(c) << (22 - 10 * i as u32)));
|
||||
|
||||
+1
-1
@@ -437,7 +437,7 @@ what an agent can't finish alone.
|
||||
`.cin`), sharing the DPX header/packing (`crates/codecs/src/codecs/dpx.rs`); a Cineon frame saved
|
||||
back keeps its code values.
|
||||
- **Progress (2026-10-23):** Amiga IFF/ILBM (`FORM ... ILBM`/`PBM `) 1–8-plane paletted and
|
||||
24-plane RGB bitmaps open, stored or ByteRun1-compressed (`crates/codecs/src/codecs/iff.rs`);
|
||||
24-plane RGB bitmaps open, stored or ByteRun1-compressed (craft-libs' `craft-raster-codecs`, `iff`);
|
||||
Save As `.iff` writes a 24-plane ILBM with a 1-bit mask plane for alpha.
|
||||
- **Estimate:** 120–220 h.
|
||||
|
||||
|
||||
Reference in new issue
Block a user