diff --git a/crates/image-codecs/src/codecs/xpm.rs b/crates/image-codecs/src/codecs/xpm.rs index 2dbd867c9..c74b5f16b 100644 --- a/crates/image-codecs/src/codecs/xpm.rs +++ b/crates/image-codecs/src/codecs/xpm.rs @@ -114,6 +114,16 @@ pub(crate) fn named(name: &str) -> Option<[u8; 3]> { }) } +/// Exact, normalized X11 lookup for consumers that need table membership rather +/// than XPM's additional aliases and normalization rules. +pub(crate) fn exact_x11_name(name: &str) -> Option<[u8; 3]> { + if name.is_empty() || name.len() > 64 || !name.bytes().all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit()) { + return None; + } + let index = x11_colors::X11_COLORS.binary_search_by_key(&name, |(entry, _)| *entry).ok()?; + Some(x11_colors::X11_COLORS.get(index)?.1) +} + fn color(name: &str) -> Option<[u8; 4]> { if name.eq_ignore_ascii_case("none") { return Some([0, 0, 0, 0]); diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 8085ca44e..e6a54756c 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -218,10 +218,7 @@ pub fn pcx_dimensions(bytes: &[u8]) -> Option<(u32, u32)> { /// The X11 color-name lookup used by XPM files, expressed as normalized sRGB. pub fn xpm_named_color_rgb(name: &str) -> Option<[f32; 3]> { - if name.is_empty() || name.len() > 64 || !name.bytes().all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit()) { - return None; - } - let rgb = codecs::xpm::named(name)?; + let rgb = codecs::xpm::exact_x11_name(name)?; Some(rgb.map(|channel| f32::from(channel) / 255.0)) } diff --git a/crates/raster-codecs/src/dds.rs b/crates/raster-codecs/src/dds.rs index 789028fb4..eb703a074 100644 --- a/crates/raster-codecs/src/dds.rs +++ b/crates/raster-codecs/src/dds.rs @@ -52,6 +52,17 @@ pub fn encode(image: &Image, options: DdsOptions) -> Result, CodecError> if image.sample_type() != SampleType::U8 { return Err(CodecError::encode("DDS", "DDS is written at 8 bits per channel")); } + // BC storage encodes RGBA blocks. The generic image API can plan a + // conversion from other layouts, but this compatibility facade accepts + // only layouts its former writer stored directly. + let allowed = if options.compression.is_block() { + image.layout() == crate::ChannelLayout::Rgba + } else { + matches!(image.layout(), crate::ChannelLayout::Gray | crate::ChannelLayout::GrayA | crate::ChannelLayout::Rgb | crate::ChannelLayout::Rgba) + }; + if !allowed { + return Err(CodecError::encode("DDS", format!("{:?} pixels can't be written as `{}`", image.layout(), options.compression.name()))); + } let compression = match options.compression { DdsCompression::None => craft_image_codecs::DdsCompression::None, DdsCompression::Bc1 => craft_image_codecs::DdsCompression::Bc1, diff --git a/crates/raster-codecs/src/fits.rs b/crates/raster-codecs/src/fits.rs index 4a662ab6e..5b82541c9 100644 --- a/crates/raster-codecs/src/fits.rs +++ b/crates/raster-codecs/src/fits.rs @@ -12,5 +12,8 @@ pub fn decode(bytes: &[u8], limits: &Limits) -> Result { bridge::format(Format::Fits, bytes, limits) } pub fn encode(image: &Image) -> Result, CodecError> { + if !matches!(image.layout(), crate::ChannelLayout::Gray | crate::ChannelLayout::Rgb) { + return Err(CodecError::encode("FITS", format!("FITS cannot store {:?}", image.layout()))); + } bridge::encode(image, Format::Fits, &Default::default()) } diff --git a/crates/raster-codecs/src/iff.rs b/crates/raster-codecs/src/iff.rs index 282617e21..b2d44b89a 100644 --- a/crates/raster-codecs/src/iff.rs +++ b/crates/raster-codecs/src/iff.rs @@ -9,5 +9,8 @@ pub fn decode(bytes: &[u8], limits: &Limits) -> Result { bridge::format(Format::Iff, bytes, limits) } pub fn encode(image: &Image) -> Result, CodecError> { + if image.layout() != crate::ChannelLayout::Rgb || image.sample_type() != crate::SampleType::U8 { + return Err(CodecError::encode("IFF", "ILBM writer stores 8-bit RGB pixels")); + } bridge::encode(image, Format::Iff, &Default::default()) } diff --git a/crates/raster-codecs/src/pnm.rs b/crates/raster-codecs/src/pnm.rs index 834e6508a..338938700 100644 --- a/crates/raster-codecs/src/pnm.rs +++ b/crates/raster-codecs/src/pnm.rs @@ -9,5 +9,10 @@ pub fn decode(bytes: &[u8], limits: &Limits) -> Result { bridge::format(Format::Pnm, bytes, limits) } pub fn encode(image: &Image) -> Result, CodecError> { + // PFM has only gray and RGB tuples. Preserve this facade's contract that + // unsupported layouts are refused instead of silently dropping alpha. + if image.sample_type() == crate::SampleType::F32 && !matches!(image.layout(), crate::ChannelLayout::Gray | crate::ChannelLayout::Rgb) { + return Err(CodecError::encode("PNM", format!("PFM cannot store {:?}", image.layout()))); + } bridge::encode(image, Format::Pnm, &Default::default()) }