mindustry logic execution, map- and schematic- parsing and rendering
update fimg
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | build.rs | 23 | ||||
| -rw-r--r-- | src/block/logic.rs | 4 | ||||
| -rw-r--r-- | src/lib.rs | 2 | ||||
| -rw-r--r-- | src/utils/image/holder.rs | 8 | ||||
| -rw-r--r-- | src/utils/image/mod.rs | 31 |
6 files changed, 33 insertions, 37 deletions
@@ -26,7 +26,7 @@ blurslice = { version = "0.1" } enum_dispatch = "0.3" fast_image_resize = "2.7" phf = { version = "0.11", features = ["macros"] } -fimg = { version = "0.2", default-features = false } +fimg = { version = "0.3.3", default-features = false } [features] bin = ["fimg/save"] @@ -38,17 +38,17 @@ fn main() { let mut n = 22usize; wr!(full => "pub mod full {{"); - wr!(full => "pub static EMPTY4: Image<&[u8], 4> = Image::new(unsafe {{ std::num::NonZeroU32::new_unchecked(32) }}, unsafe {{ std::num::NonZeroU32::new_unchecked(32) }}, &[0; 32 * 32 * 4]);"); - wr!(full => "pub static EMPTY: Image<&[u8], 3> = Image::new(unsafe {{ std::num::NonZeroU32::new_unchecked(32) }}, unsafe {{ std::num::NonZeroU32::new_unchecked(32) }}, &[0; 32 * 32 * 3]);"); + wr!(full => "pub static EMPTY4: Image<&[u8], 4> = Image::make::<32, 32>();"); + wr!(full => "pub static EMPTY: Image<&[u8], 3> = Image::make::<32, 32>();"); wr!(quar => "pub mod quar {{"); // forced to do this because try_into isnt const - wr!(quar => "pub static EMPTY4: Image<&[u8], 4> = Image::new(unsafe {{ std::num::NonZeroU32::new_unchecked(8) }}, unsafe {{ std::num::NonZeroU32::new_unchecked(8) }}, &[0; 8 * 8 * 4]);"); - wr!(quar => "pub static EMPTY: Image<&[u8], 3> = Image::new(unsafe {{ std::num::NonZeroU32::new_unchecked(8) }}, unsafe {{ std::num::NonZeroU32::new_unchecked(8) }}, &[0; 8 * 8 * 3]);"); + wr!(quar => "pub static EMPTY4: Image<&[u8], 4> = Image::make::<8, 8>();"); + wr!(quar => "pub static EMPTY: Image<&[u8], 3> = Image::make::<8, 8>();"); wr!(eigh => "pub mod eigh {{"); - wr!(eigh => "pub static EMPTY4: Image<&[u8], 4> = Image::new(unsafe {{ std::num::NonZeroU32::new_unchecked(4) }}, unsafe {{ std::num::NonZeroU32::new_unchecked(4) }}, &[0; 4 * 4 * 4]);"); - wr!(eigh => "pub static EMPTY: Image<&[u8], 3> = Image::new(unsafe {{ std::num::NonZeroU32::new_unchecked(4) }}, unsafe {{ std::num::NonZeroU32::new_unchecked(4) }}, &[0; 4 * 4 * 3]);"); + wr!(eigh => "pub static EMPTY4: Image<&[u8], 4> = Image::make::<4, 4>();"); + wr!(eigh => "pub static EMPTY: Image<&[u8], 3> = Image::make::<4, 4>();"); for mut file in [&full, &quar, &eigh] { wr!(file => "use crate::utils::Image;"); @@ -78,6 +78,7 @@ fn main() { macro_rules! writ { ($ext:ident / $scale:literal) => { let mut buf = File::create(o.join(n.to_string() + "-" + stringify!($ext))).unwrap(); + let out_path = format!("{}/{n}-{}", o.display(), stringify!($ext)); // boulders let (mx, my) = if env && p.width() + p.height() == 48+48 { (32, 32) @@ -103,16 +104,10 @@ fn main() { let y = new.height(); if rgb { buf.write_all(&new.into_rgb8().into_raw()).unwrap(); - wr!($ext => - r#"pub(crate) static {path}: Image<&[u8], 3> = Image::new(unsafe {{ std::num::NonZeroU32::new_unchecked( {x} ) }}, unsafe {{ std::num::NonZeroU32::new_unchecked( {y} ) }}, include_bytes!(concat!(env!("OUT_DIR"), "/{n}-{}")));"#, - stringify!($ext) - ); + wr!($ext => r#"pub(crate) static {path}: Image<&[u8], 3> = unsafe {{ Image::new(std::num::NonZeroU32::new_unchecked({x}), std::num::NonZeroU32::new_unchecked({y}), include_bytes!("{out_path}")) }};"#); } else { buf.write_all(&new.into_rgba8().into_raw()).unwrap(); - wr!($ext => - r#"pub(crate) static {path}: Image<&[u8], 4> = Image::new(unsafe {{ std::num::NonZeroU32::new_unchecked( {x} ) }}, unsafe {{ std::num::NonZeroU32::new_unchecked( {y} ) }}, include_bytes!(concat!(env!("OUT_DIR"), "/{n}-{}")));"#, - stringify!($ext) - ); + wr!($ext => r#"pub(crate) static {path}: Image<&[u8], 4> = unsafe {{ Image::new(std::num::NonZeroU32::new_unchecked({x}), std::num::NonZeroU32::new_unchecked({y}), include_bytes!("{out_path}")) }};"#); } }; } diff --git a/src/block/logic.rs b/src/block/logic.rs index 6662001..94f26f2 100644 --- a/src/block/logic.rs +++ b/src/block/logic.rs @@ -134,7 +134,7 @@ impl BlockLogic for CanvasBlock { Scale::Eigth => 1, }; let mut img = Image::alloc(p.width(), p.height()); - for ([r, g, b, a], &y) in img.chunked_mut().zip(p.buffer.iter()) { + for ([r, g, b, a], &y) in img.chunked_mut().zip(p.buffer().iter()) { (*r, *g, *b) = PALETTE[y as usize]; *a = 255; } @@ -149,7 +149,7 @@ impl BlockLogic for CanvasBlock { } let mut def = Image::alloc(s * self.size as u32, s * self.size as u32); - for [r, g, b, a] in def.buffer.array_chunks_mut::<4>() { + for [r, g, b, a] in def.chunked_mut() { (*r, *g, *b) = PALETTE[0]; *a = 255; } @@ -1,5 +1,5 @@ //! crate for dealing with mindustry -#![feature(const_trait_impl, let_chains, effects, array_chunks)] +#![feature(const_trait_impl, let_chains, effects)] #![warn( clippy::multiple_unsafe_ops_per_block, clippy::missing_const_for_fn, diff --git a/src/utils/image/holder.rs b/src/utils/image/holder.rs index e905f48..facd9ef 100644 --- a/src/utils/image/holder.rs +++ b/src/utils/image/holder.rs @@ -10,7 +10,7 @@ impl<const CHANNELS: usize> ImageHolder<CHANNELS> { pub fn own(self) -> Image<Vec<u8>, CHANNELS> { match self { Self::Own(x) => x, - Self::Borrow(x) => Image::new(x.width, x.height, x.buffer.to_vec()), + Self::Borrow(x) => x.to_owned(), } } } @@ -29,9 +29,9 @@ impl<const CHANNELS: usize> ImageHolder<CHANNELS> { #[inline] pub fn borrow_mut(&mut self) -> Image<&mut [u8], CHANNELS> { match self { - Self::Own(x) => Image::new(x.width, x.height, &mut x.buffer), - Self::Borrow(_) => { - *self = Self::from(std::mem::replace(self, Self::from(Image::default())).own()); + Self::Own(x) => x.as_mut(), + Self::Borrow(x) => { + *self = Self::from(x.to_owned()); self.borrow_mut() } } diff --git a/src/utils/image/mod.rs b/src/utils/image/mod.rs index 9d853b6..5bcbb3f 100644 --- a/src/utils/image/mod.rs +++ b/src/utils/image/mod.rs @@ -31,7 +31,7 @@ impl ImageUtils for Image<&mut [u8], 4> { fn tint(&mut self, (r, g, b): (u8, u8, u8)) -> &mut Self { let [tr, tg, tb] = [r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0]; - for [r, g, b, _] in self.buffer.array_chunks_mut::<4>() { + for [r, g, b, _] in self.chunked_mut() { *r = (*r as f32 * tr) as u8; *g = (*g as f32 * tg) as u8; *b = (*b as f32 * tb) as u8; @@ -41,21 +41,24 @@ impl ImageUtils for Image<&mut [u8], 4> { // this function is very cold but im removing image so might as well use fir fn scale(self, to: u32) -> Image<Vec<u8>, 4> { - let from = - fr::Image::from_slice_u8(self.width, self.height, self.buffer, fr::PixelType::U8x4) - .unwrap(); - let to = to.try_into().unwrap(); - let mut dst = fr::Image::new(to, to, fr::PixelType::U8x4); + let from = fr::Image::from_slice_u8( + self.width().try_into().unwrap(), + self.height().try_into().unwrap(), + self.take_buffer(), + fr::PixelType::U8x4, + ) + .unwrap(); + let toz = to.try_into().unwrap(); + let mut dst = fr::Image::new(toz, toz, fr::PixelType::U8x4); fr::Resizer::new(fr::ResizeAlg::Nearest) .resize(&from.view(), &mut dst.view_mut()) .unwrap(); - Image::new(to, to, dst.into_vec()) + Image::build(to, to).buf(dst.into_vec()) } fn shadow(&mut self) -> &mut Self { - let mut shadow: Image<Vec<u8>, 4> = - Image::new(self.width, self.height, self.buffer.to_vec()); - for [r, g, b, a] in shadow.buffer.array_chunks_mut() { + let mut shadow: Image<Vec<u8>, 4> = self.to_owned(); + for [r, g, b, a] in shadow.chunked_mut() { if *a < 128 { *r /= 10; *g /= 10; @@ -63,16 +66,14 @@ impl ImageUtils for Image<&mut [u8], 4> { } } blurslice::gaussian_blur_bytes::<4>( - &mut shadow.buffer, + unsafe { shadow.buffer_mut() }, self.width() as usize, self.height() as usize, 9.0, ) .unwrap(); - for ([r, g, b, a], &[from_r, from_g, from_b, from_a]) in self - .buffer - .array_chunks_mut() - .zip(shadow.buffer.array_chunks()) + for ([r, g, b, a], &[from_r, from_g, from_b, from_a]) in + self.chunked_mut().zip(shadow.chunked()) { if *a == 0 { (*r, *g, *b, *a) = (from_r, from_g, from_b, from_a); |