mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 20:25:02 +00:00
Preserve raster facade encode contracts
This commit is contained in:
@@ -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]);
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
@@ -52,6 +52,17 @@ pub fn encode(image: &Image, options: DdsOptions) -> Result<Vec<u8>, 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,
|
||||
|
||||
@@ -12,5 +12,8 @@ pub fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
bridge::format(Format::Fits, bytes, limits)
|
||||
}
|
||||
pub fn encode(image: &Image) -> Result<Vec<u8>, 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())
|
||||
}
|
||||
|
||||
@@ -9,5 +9,8 @@ pub fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
bridge::format(Format::Iff, bytes, limits)
|
||||
}
|
||||
pub fn encode(image: &Image) -> Result<Vec<u8>, 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())
|
||||
}
|
||||
|
||||
@@ -9,5 +9,10 @@ pub fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
bridge::format(Format::Pnm, bytes, limits)
|
||||
}
|
||||
pub fn encode(image: &Image) -> Result<Vec<u8>, 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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user