chore: cut over runtime raster image codecs to craft-image-codecs (#668)

* chore: cut over runtime raster image codecs to craft-image-codecs

* fix(formats): move inline mesh_import test into tests.rs

* fix(engine): simplify nonminimal_bool in user_brushes.rs

* fix(ui-egui): simplify nonminimal_bool in link_import.rs

* fix(formats): use as_chunks::<4>() in mesh_import for clippy

* fix: preserve mesh texture orientation and resize precision
This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-18 22:15:08 -05:00
1 parent 68b579f0c8
commit 51c3f6441d
12 files changed
+264 -23

No files matched your search

Generated
+145
View File
@@ -132,6 +132,21 @@ dependencies = [
"zerocopy",
]
[[package]]
name = "alloc-no-stdlib"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2fb6cfd47bf496ff64095c20eaba0c201404ee38714d4142fcfa1dc334fcc7a"
[[package]]
name = "alloc-stdlib"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b5c1865780388bfa186411ab5f247819487fc4864c6e9c3106611fa347586e1"
dependencies = [
"alloc-no-stdlib",
]
[[package]]
name = "allocator-api2"
version = "0.2.21"
@@ -494,6 +509,16 @@ dependencies = [
"piper",
]
[[package]]
name = "brotli-decompressor"
version = "6.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc031cd87c6529082b0b65c604e86af0ed7744841b17ccfe60b1e31fa606179e"
dependencies = [
"alloc-no-stdlib",
"alloc-stdlib",
]
[[package]]
name = "bumpalo"
version = "3.20.3"
@@ -677,6 +702,12 @@ dependencies = [
"bytemuck",
]
[[package]]
name = "color_quant"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "combine"
version = "4.6.8"
@@ -770,6 +801,14 @@ dependencies = [
"rayon",
]
[[package]]
name = "craft-dds"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=b130ac1fa44f923c3024884ab8ceb5ccdb88f15f#b130ac1fa44f923c3024884ab8ceb5ccdb88f15f"
dependencies = [
"rayon",
]
[[package]]
name = "craft-dialogs"
version = "0.1.0"
@@ -811,6 +850,41 @@ dependencies = [
"writeable",
]
[[package]]
name = "craft-image-codecs"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=b130ac1fa44f923c3024884ab8ceb5ccdb88f15f#b130ac1fa44f923c3024884ab8ceb5ccdb88f15f"
dependencies = [
"craft-dds",
"craft-jpeg",
"craft-metadata",
"craft-tiff",
"crc32fast",
"exr",
"flate2",
"half",
"hayro-jpeg2000",
"image",
"image-webp",
"jpeg-encoder",
"png",
"rayon",
"serde_json",
"thiserror 2.0.21",
"tiff",
"zune-core",
"zune-jpeg",
"zune-jpegxl",
]
[[package]]
name = "craft-jpeg"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=b130ac1fa44f923c3024884ab8ceb5ccdb88f15f#b130ac1fa44f923c3024884ab8ceb5ccdb88f15f"
dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-keymap"
version = "0.1.0"
@@ -864,6 +938,18 @@ dependencies = [
"serde_json",
]
[[package]]
name = "craft-metadata"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=b130ac1fa44f923c3024884ab8ceb5ccdb88f15f#b130ac1fa44f923c3024884ab8ceb5ccdb88f15f"
dependencies = [
"brotli-decompressor",
"chrono",
"craft-tiff",
"quick-xml 0.41.0",
"serde",
]
[[package]]
name = "craft-quit"
version = "0.1.0"
@@ -886,6 +972,15 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-tiff"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=b130ac1fa44f923c3024884ab8ceb5ccdb88f15f#b130ac1fa44f923c3024884ab8ceb5ccdb88f15f"
dependencies = [
"serde",
"thiserror 2.0.21",
]
[[package]]
name = "craft-ui"
version = "0.1.0"
@@ -1499,6 +1594,7 @@ dependencies = [
"miniz_oxide 0.8.9",
"num-complex",
"pulp",
"rayon-core",
"smallvec",
"zune-inflate",
]
@@ -1789,6 +1885,16 @@ dependencies = [
"r-efi 6.0.0",
]
[[package]]
name = "gif"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee8cfcc411d9adbbaba82fb72661cc1bcca13e8bba98b364e62b2dba8f960159"
dependencies = [
"color_quant",
"weezl",
]
[[package]]
name = "gl_generator"
version = "0.14.0"
@@ -2025,6 +2131,15 @@ dependencies = [
"foldhash 0.2.0",
]
[[package]]
name = "hayro-jpeg2000"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c75ab947623ef4ccaa7acf0579edf7cbb5a73838e3839a7be73335e522f433a1"
dependencies = [
"fearless_simd",
]
[[package]]
name = "hermit-abi"
version = "0.5.3"
@@ -2268,11 +2383,14 @@ checksum = "85ab80394333c02fe689eaf900ab500fbd0c2213da414687ebf995a65d5a6104"
dependencies = [
"bytemuck",
"byteorder-lite",
"color_quant",
"exr",
"gif",
"image-webp",
"moxcms",
"num-traits",
"png",
"qoi",
"tiff",
"zune-core",
"zune-jpeg",
@@ -2417,6 +2535,12 @@ dependencies = [
"libc",
]
[[package]]
name = "jpeg-encoder"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b454d911ac55068f53495488d8ccd0646eaa540c033a28ee15b07838afafb01f"
[[package]]
name = "js-sys"
version = "0.3.106"
@@ -3563,6 +3687,15 @@ version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea"
[[package]]
name = "qoi"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001"
dependencies = [
"bytemuck",
]
[[package]]
name = "quick-error"
version = "2.0.1"
@@ -4430,6 +4563,7 @@ dependencies = [
name = "texturecraft"
version = "0.6.1"
dependencies = [
"craft-image-codecs",
"craft-menu",
"craft-update",
"eframe",
@@ -4544,6 +4678,7 @@ dependencies = [
"base64 0.22.1",
"bytemuck",
"craft-cms",
"craft-image-codecs",
"craft-materialx",
"craft-mcmat",
"craft-mesh",
@@ -4628,6 +4763,7 @@ dependencies = [
"base64 0.22.1",
"craft-dialogs",
"craft-i18n",
"craft-image-codecs",
"craft-keymap",
"craft-quit",
"craft-ui",
@@ -6327,6 +6463,15 @@ dependencies = [
"zune-core",
]
[[package]]
name = "zune-jpegxl"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cba11ecd07cc23351500e840ae03841b7e4997923ec8e4832ee3ae199ea0b6b4"
dependencies = [
"zune-core",
]
[[package]]
name = "zvariant"
version = "5.15.0"
+1
View File
@@ -42,6 +42,7 @@ skrifa = { version = "0.44", default-features = false, features = ["std"] }
epaint_default_fonts = "0.37"
log = "0.4"
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "tga", "tiff", "exr", "hdr", "bmp", "webp"] }
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "b130ac1fa44f923c3024884ab8ceb5ccdb88f15f" }
half = "2"
exr = { version = "1.73", default-features = false }
eframe = { version = "0.37", default-features = false }
+1
View File
@@ -18,6 +18,7 @@ texturecraft-automation = { workspace = true }
eframe = { workspace = true, features = ["default_fonts", "wgpu", "accesskit", "wayland", "x11", "persistence"] }
egui = { workspace = true }
image = { workspace = true }
craft-image-codecs = { workspace = true }
serde_json = { workspace = true }
log = { workspace = true }
# The message box for errors that end the app (a Windows release build has no console).
+9 -3
View File
@@ -119,9 +119,8 @@ fn initial_session(args: &Args) -> Session {
}
fn icon() -> Option<egui::IconData> {
let img = image::load_from_memory(ICON_PNG).ok()?.into_rgba8();
let (w, h) = img.dimensions();
Some(egui::IconData { rgba: img.into_raw(), width: w, height: h })
let img = craft_image_codecs::decode(ICON_PNG).ok()?;
Some(egui::IconData { rgba: img.to_rgba8(), width: img.width(), height: img.height() })
}
/// Windows shows a window's big icon (`ICON_BIG`) in the taskbar and Alt+Tab; eframe sets only the
@@ -325,6 +324,13 @@ mod tests {
assert!(!checks_for_updates_at_start(Some(9000)));
}
#[test]
fn bundled_app_icon_decodes_to_rgba() {
let icon = icon().unwrap();
assert!(icon.width > 0 && icon.height > 0);
assert_eq!(icon.rgba.len(), (icon.width * icon.height * 4) as usize);
}
#[test]
fn rejects_missing_or_invalid_gpu_args() {
assert!(parse_args_from(["--gpu".to_string()]).is_err());
@@ -78,7 +78,7 @@ impl UserBrushes {
match std::fs::symlink_metadata(&path) {
Ok(_)
if old_file != Some(path.as_path())
&& !old_file.is_some_and(|old| old.canonicalize().ok().zip(path.canonicalize().ok()).is_some_and(|(old, new)| old == new)) =>
&& old_file.is_none_or(|old| old.canonicalize().ok().zip(path.canonicalize().ok()).is_none_or(|(old, new)| old != new)) =>
{
return Err(format!("{} already exists", path.display()));
}
+1
View File
@@ -25,6 +25,7 @@ texturecraft-compose = { workspace = true }
image = { workspace = true }
png = "0.18"
bytemuck = { workspace = true, features = ["extern_crate_alloc"] }
craft-image-codecs = { workspace = true }
gltf = { workspace = true }
flate2 = { workspace = true }
lz4_flex = { workspace = true }
+38 -12
View File
@@ -357,23 +357,49 @@ pub fn import_high_poly_bytes(file_name: &str, bytes: &[u8]) -> Result<HighPolyM
/// Decode a base color texture to RGBA8, at most [`BASE_COLOR_SIDE`] per side, charging its
/// texels to `budget`.
fn decode_base_color(bytes: &[u8], budget: &mut u64) -> Result<(u32, u32, Vec<[u8; 4]>)> {
let bad = |e: image::ImageError| FormatError::Malformed { kind: "image", msg: e.to_string() };
let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().map_err(|e| bad(e.into()))?;
let mut limits = image::Limits::default();
limits.max_image_width = Some(crate::MAX_IMAGE_SIZE);
limits.max_image_height = Some(crate::MAX_IMAGE_SIZE);
reader.limits(limits);
let img = crate::guard("image", || reader.decode().map_err(bad))?;
let img =
if img.width().max(img.height()) > BASE_COLOR_SIDE { img.resize(BASE_COLOR_SIDE, BASE_COLOR_SIDE, image::imageops::FilterType::Triangle) } else { img };
let rgba = img.into_rgba8();
let (w, h) = rgba.dimensions();
let bad = |e: craft_image_codecs::CodecError| FormatError::Malformed { kind: "image", msg: e.to_string() };
let mut opts = craft_image_codecs::DecodeOptions::default();
opts.limits.max_width = crate::MAX_IMAGE_SIZE;
opts.limits.max_height = crate::MAX_IMAGE_SIZE;
// Mesh UVs refer to stored pixels, not an EXIF-adjusted photograph.
opts.keep_orientation = true;
// Preserve the prior ImageReader allocation limit; this is not a total RSS cap.
opts.limits.max_alloc = 512 << 20;
let img = crate::guard("image", || craft_image_codecs::decode_with(bytes, &opts).map_err(bad))?;
let (w, h, rgba) = if img.width().max(img.height()) > BASE_COLOR_SIDE {
use craft_image_codecs::{ChannelLayout, SampleType};
let invalid = || FormatError::Malformed { kind: "image", msg: "invalid RGBA buffer".into() };
// Keep native precision through filtering. Converting HDR to u8 first clips highlights.
// Bound expanded RGBA storage before conversion as well as the decoder's native output.
let mut dyn_img = match img.sample_type() {
SampleType::F16 | SampleType::F32 => {
let rgba = img.to_rgba_f32(&opts.limits).map_err(bad)?;
image::DynamicImage::ImageRgba32F(image::Rgba32FImage::from_raw(img.width(), img.height(), rgba).ok_or_else(invalid)?)
}
SampleType::U16 => {
opts.limits.check(img.width(), img.height(), ChannelLayout::Rgba, SampleType::U16).map_err(bad)?;
image::DynamicImage::ImageRgba16(image::ImageBuffer::from_raw(img.width(), img.height(), img.to_rgba16()).ok_or_else(invalid)?)
}
SampleType::U8 => {
opts.limits.check(img.width(), img.height(), ChannelLayout::Rgba, SampleType::U8).map_err(bad)?;
image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(img.width(), img.height(), img.to_rgba8()).ok_or_else(invalid)?)
}
};
dyn_img = dyn_img.resize(BASE_COLOR_SIDE, BASE_COLOR_SIDE, image::imageops::FilterType::Triangle);
let rgba = dyn_img.into_rgba8();
let (w, h) = rgba.dimensions();
(w, h, rgba.pixels().map(|p| p.0).collect::<Vec<[u8; 4]>>())
} else {
let rgba = img.to_rgba8();
let pixels = rgba.as_chunks::<4>().0.to_vec();
(img.width(), img.height(), pixels)
};
let texels = u64::from(w) * u64::from(h);
if texels > *budget {
return Err(FormatError::TooLarge(format!("the base color textures exceed {} megatexels", BASE_COLOR_TEXELS >> 20)));
}
*budget -= texels;
Ok((w, h, rgba.pixels().map(|p| p.0).collect()))
Ok((w, h, rgba))
}
fn read(file_name: &str, bytes: &[u8]) -> Result<RawMesh> {
+45
View File
@@ -669,3 +669,48 @@ fn group_index_preserves_order_and_reuses_nonadjacent_material_parts() {
}
assert_eq!(raw.groups[1000], ("other".into(), "0".into(), vec![6, 7, 8]));
}
#[test]
fn decode_base_color_resizes_oversized_and_rejects_corrupt_bytes() {
let mut budget = BASE_COLOR_TEXELS;
assert!(decode_base_color(b"not an image", &mut budget).is_err());
let side = BASE_COLOR_SIDE + 2;
let img = craft_image_codecs::Image::from_u8(side, 2, craft_image_codecs::ChannelLayout::Rgba, vec![128; (side * 2 * 4) as usize]).unwrap();
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &Default::default()).unwrap();
let (w, h, pixels) = decode_base_color(&png, &mut budget).unwrap();
assert!(w <= BASE_COLOR_SIDE && h <= BASE_COLOR_SIDE);
assert_eq!(pixels.len(), (w * h) as usize);
}
#[test]
fn base_color_keeps_stored_jpeg_orientation_for_mesh_uvs() {
let pixels = image::RgbImage::from_fn(4, 2, |x, _| if x < 2 { image::Rgb([255, 0, 0]) } else { image::Rgb([0, 0, 255]) });
let mut jpeg = Vec::new();
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpeg, 100).encode_image(&pixels).unwrap();
// Synthetic TIFF Orientation=6 inside EXIF APP1, not an external image fixture.
let exif = b"Exif\0\0II\x2a\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0\x06\0\0\0\0\0\0\0";
let mut tagged = jpeg[..2].to_vec();
tagged.extend_from_slice(&[0xff, 0xe1]);
tagged.extend_from_slice(&u16::try_from(exif.len() + 2).unwrap().to_be_bytes());
tagged.extend_from_slice(exif);
tagged.extend_from_slice(&jpeg[2..]);
let expected = image::load_from_memory(&tagged).unwrap().into_rgba8();
let mut budget = BASE_COLOR_TEXELS;
let (w, h, actual) = decode_base_color(&tagged, &mut budget).unwrap();
assert_eq!((w, h), (4, 2));
assert_eq!(actual, expected.pixels().map(|p| p.0).collect::<Vec<_>>());
}
#[test]
fn base_color_filters_hdr_before_clamping_to_eight_bits() {
let h = BASE_COLOR_SIDE * 2;
let samples: Vec<_> = (0..h).map(|y| image::Rgb([if y % 2 == 0 { 4.0 } else { 0.0 }; 3])).collect();
let mut hdr = Vec::new();
image::codecs::hdr::HdrEncoder::new(&mut hdr).encode(&samples, 1, h as usize).unwrap();
let expected = image::load_from_memory(&hdr).unwrap().resize(BASE_COLOR_SIDE, BASE_COLOR_SIDE, image::imageops::FilterType::Triangle).into_rgba8();
let mut budget = BASE_COLOR_TEXELS;
let (w, h, actual) = decode_base_color(&hdr, &mut budget).unwrap();
assert_eq!((w, h), expected.dimensions());
assert_eq!(actual, expected.pixels().map(|p| p.0).collect::<Vec<_>>());
}
+2
View File
@@ -25,6 +25,7 @@ rayon = { workspace = true }
log = { workspace = true }
base64 = "0.22"
image = { workspace = true }
craft-image-codecs = { workspace = true }
zip = { workspace = true }
# The OS UI language for the first run (MIT OR Apache-2.0; the browser asks `navigator.language`).
@@ -60,3 +61,4 @@ egui_kittest = { version = "0.37", features = ["wgpu", "eframe"] }
# Fallible setup of the exact GPU device used by rendering regression tests.
pollster = "0.4"
image = { workspace = true }
craft-image-codecs = { workspace = true }
+17 -3
View File
@@ -68,9 +68,8 @@ pub fn poll(app: &mut TextureCraftApp, ctx: &egui::Context) {
}
pub(crate) fn encode_png(w: u32, h: u32, rgba: Vec<u8>) -> Option<(u32, u32, Vec<u8>)> {
let img = image::RgbaImage::from_raw(w, h, rgba)?;
let mut png = Vec::new();
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).ok()?;
let img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgba, rgba).ok()?;
let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &Default::default()).ok()?;
Some((w, h, png))
}
@@ -251,3 +250,18 @@ pub fn screenshot(app: &TextureCraftApp, view: u64) -> Option<(u32, u32, Vec<u8>
let (w, h, rgba) = gpu.renderer.read_view(&rs.device, &rs.queue, view)?;
encode_png(w, h, rgba)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encodes_rgba_buffer_to_png() {
let (w, h, png) = encode_png(2, 1, vec![255, 0, 0, 255, 0, 255, 0, 255]).unwrap();
assert_eq!((w, h), (2, 1));
let decoded = craft_image_codecs::decode(&png).unwrap();
assert_eq!((decoded.width(), decoded.height()), (2, 1));
assert_eq!(decoded.to_rgba8(), vec![255, 0, 0, 255, 0, 255, 0, 255]);
assert!(encode_png(2, 1, vec![0; 3]).is_none());
}
}
+1 -1
View File
@@ -90,7 +90,7 @@ impl TextureCraftApp {
}
if let Place::Above(target, _) | Place::Below(target) = job.place
&& !self.session.doc.project.locate(target).is_some_and(|(set, _, _)| set == job.set)
&& self.session.doc.project.locate(target).is_none_or(|(set, _, _)| set != job.set)
{
self.toast("Material import canceled because the target layer changed", true);
continue;
+3 -3
View File
@@ -143,10 +143,10 @@ fn paint_icon(ui: &mut Ui) {
Some(t) => t,
None => {
// Decoded once per size; a PNG that doesn't decode leaves the space empty.
let t = image::load_from_memory(png).ok().map(|img| {
let img = img.into_rgba8();
let t = craft_image_codecs::decode(png).ok().map(|img| {
let size = [img.width() as usize, img.height() as usize];
ui.ctx().load_texture("app_icon", egui::ColorImage::from_rgba_unmultiplied(size, img.as_raw()), egui::TextureOptions::LINEAR)
let rgba = img.to_rgba8();
ui.ctx().load_texture("app_icon", egui::ColorImage::from_rgba_unmultiplied(size, &rgba), egui::TextureOptions::LINEAR)
});
ui.data_mut(|d| d.insert_temp(id, t.clone()));
t