Update image

This commit is contained in:
Youser Nayme 2024-06-23 11:07:15 -04:00
parent 48dfac6f8b
commit a1930b81d0
No known key found for this signature in database
GPG Key ID: 08E3619383679CAE
6 changed files with 75 additions and 47 deletions

35
Cargo.lock generated
View File

@ -1078,12 +1078,6 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "color_quant"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]] [[package]]
name = "colorchoice" name = "colorchoice"
version = "1.0.1" version = "1.0.1"
@ -3184,16 +3178,16 @@ dependencies = [
[[package]] [[package]]
name = "image" name = "image"
version = "0.24.9" version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5690139d2f55868e080017335e4b94cb7414274c74f1669c84fb5feba2c9f69d" checksum = "fd54d660e773627692c524beaad361aca785a4f9f5730ce91f42aabe5bce3d11"
dependencies = [ dependencies = [
"bytemuck", "bytemuck",
"byteorder", "byteorder",
"color_quant",
"jpeg-decoder",
"num-traits", "num-traits",
"png", "png",
"zune-core",
"zune-jpeg",
] ]
[[package]] [[package]]
@ -3463,12 +3457,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "jpeg-decoder"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5d4a7da358eff58addd2877a45865158f0d78c911d43a5784ceb7bbf52833b0"
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.69" version = "0.3.69"
@ -9169,3 +9157,18 @@ dependencies = [
"cc", "cc",
"pkg-config", "pkg-config",
] ]
[[package]]
name = "zune-core"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f423a2c17029964870cfaabb1f13dfab7d092a62a29a89264f4d36990ca414a"
[[package]]
name = "zune-jpeg"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec866b44a2a1fd6133d363f073ca1b179f438f99e7e5bfb1e33f7181facfe448"
dependencies = [
"zune-core",
]

View File

@ -157,7 +157,7 @@ serde_json = { version = "1.0.50" }
ron = { version = "0.8", default-features = false } ron = { version = "0.8", default-features = false }
specs = { version = "0.20", features = ["nightly"] } specs = { version = "0.20", features = ["nightly"] }
image = { version = "0.24", default-features = false, features = ["png"] } image = { version = "0.25", default-features = false, features = ["png"] }
rayon = { version = "1.5" } rayon = { version = "1.5" }
clap = { version = "4.2", features = ["derive"] } clap = { version = "4.2", features = ["derive"] }

View File

@ -9,7 +9,7 @@ use num_traits::cast::FromPrimitive;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
fmt::Debug, fmt::Debug,
io::{Read, Write}, io::{Cursor, Read, Write},
marker::PhantomData, marker::PhantomData,
}; };
use tracing::warn; use tracing::warn;
@ -142,7 +142,7 @@ pub trait VoxelImageDecoding: VoxelImageEncoding {
fn get_block(ws: &Self::Workspace, x: u32, y: u32, is_border: bool) -> Block; fn get_block(ws: &Self::Workspace, x: u32, y: u32, is_border: bool) -> Block;
} }
pub fn image_from_bytes<'a, I: ImageDecoder<'a>, P: 'static + Pixel<Subpixel = u8>>( pub fn image_from_bytes<I: ImageDecoder, P: 'static + Pixel<Subpixel = u8>>(
decoder: I, decoder: I,
) -> Option<ImageBuffer<P, Vec<u8>>> { ) -> Option<ImageBuffer<P, Vec<u8>>> {
let (w, h) = decoder.dimensions(); let (w, h) = decoder.dimensions();
@ -249,8 +249,13 @@ impl<const N: u32> VoxelImageEncoding for QuadPngEncoding<N> {
CompressionType::Fast, CompressionType::Fast,
FilterType::Up, FilterType::Up,
); );
png.write_image(x.as_raw(), x.width(), x.height(), image::ColorType::L8) png.write_image(
.ok()?; x.as_raw(),
x.width(),
x.height(),
image::ExtendedColorType::L8,
)
.ok()?;
indices[i] = buf.len(); indices[i] = buf.len();
Some(()) Some(())
}; };
@ -268,7 +273,7 @@ impl<const N: u32> VoxelImageEncoding for QuadPngEncoding<N> {
ws.3.as_raw(), ws.3.as_raw(),
ws.3.width(), ws.3.width(),
ws.3.height(), ws.3.height(),
image::ColorType::Rgb8, image::ExtendedColorType::Rgb8,
) )
.ok()?; .ok()?;
} }
@ -339,10 +344,10 @@ impl<const N: u32> VoxelImageDecoding for QuadPngEncoding<N> {
indices[1]..indices[2], indices[1]..indices[2],
indices[2]..quad.len(), indices[2]..quad.len(),
]; ];
let a = image_from_bytes(PngDecoder::new(&quad[ranges[0].clone()]).ok()?)?; let a = image_from_bytes(PngDecoder::new(Cursor::new(&quad[ranges[0].clone()])).ok()?)?;
let b = image_from_bytes(PngDecoder::new(&quad[ranges[1].clone()]).ok()?)?; let b = image_from_bytes(PngDecoder::new(Cursor::new(&quad[ranges[1].clone()])).ok()?)?;
let c = image_from_bytes(PngDecoder::new(&quad[ranges[2].clone()]).ok()?)?; let c = image_from_bytes(PngDecoder::new(Cursor::new(&quad[ranges[2].clone()])).ok()?)?;
let d = image_from_bytes(PngDecoder::new(&quad[ranges[3].clone()]).ok()?)?; let d = image_from_bytes(PngDecoder::new(Cursor::new(&quad[ranges[3].clone()])).ok()?)?;
Some((a, b, c, d, sprite_data, HashMap::new())) Some((a, b, c, d, sprite_data, HashMap::new()))
} }
@ -533,8 +538,13 @@ impl<const AVERAGE_PALETTE: bool> VoxelImageEncoding for TriPngEncoding<AVERAGE_
CompressionType::Fast, CompressionType::Fast,
FilterType::Up, FilterType::Up,
); );
png.write_image(x.as_raw(), x.width(), x.height(), image::ColorType::L8) png.write_image(
.ok()?; x.as_raw(),
x.width(),
x.height(),
image::ExtendedColorType::L8,
)
.ok()?;
indices[i] = buf.len(); indices[i] = buf.len();
Some(()) Some(())
}; };
@ -581,9 +591,9 @@ impl<const AVERAGE_PALETTE: bool> VoxelImageDecoding for TriPngEncoding<AVERAGE_
indices[0]..indices[1], indices[0]..indices[1],
indices[1]..indices[2], indices[1]..indices[2],
]; ];
let a = image_from_bytes(PngDecoder::new(&quad[ranges[0].clone()]).ok()?)?; let a = image_from_bytes(PngDecoder::new(Cursor::new(&quad[ranges[0].clone()])).ok()?)?;
let b = image_from_bytes(PngDecoder::new(&quad[ranges[1].clone()]).ok()?)?; let b = image_from_bytes(PngDecoder::new(Cursor::new(&quad[ranges[1].clone()])).ok()?)?;
let c = image_from_bytes(PngDecoder::new(&quad[ranges[2].clone()]).ok()?)?; let c = image_from_bytes(PngDecoder::new(Cursor::new(&quad[ranges[2].clone()])).ok()?)?;
let mut d: HashMap<_, HashMap<_, _>> = HashMap::new(); let mut d: HashMap<_, HashMap<_, _>> = HashMap::new();
if AVERAGE_PALETTE { if AVERAGE_PALETTE {
for i in 0..=255 { for i in 0..=255 {

View File

@ -17,7 +17,7 @@ use common::{
uniform_idx_as_vec2, uniform_idx_as_vec2,
}, },
}; };
use image::{codecs::png::PngEncoder, ColorType, DynamicImage, GenericImage, ImageEncoder}; use image::{codecs::png::PngEncoder, DynamicImage, GenericImage, ImageEncoder};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use rayon::ThreadPool; use rayon::ThreadPool;
@ -222,7 +222,7 @@ fn generate_one(
image.as_bytes(), image.as_bytes(),
map_size_lg.chunks().x as u32, map_size_lg.chunks().x as u32,
map_size_lg.chunks().y as u32, map_size_lg.chunks().y as u32,
ColorType::Rgba8, image::ExtendedColorType::Rgba8,
) { ) {
error!(?error, "Could not write image data"); error!(?error, "Could not write image data");
} }

View File

@ -20,7 +20,7 @@ use rayon::ThreadPoolBuilder;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
collections::BTreeMap, collections::BTreeMap,
io::{Read, Write}, io::{Cursor, Read, Write},
mem, mem,
sync::Arc, sync::Arc,
time::Instant, time::Instant,
@ -205,7 +205,7 @@ impl VoxelImageEncoding for PngEncoding {
ws.0.as_raw(), ws.0.as_raw(),
ws.0.width(), ws.0.width(),
ws.0.height(), ws.0.height(),
image::ColorType::Rgba8, image::ExtendedColorType::Rgba8,
) )
.ok()?; .ok()?;
Some((buf, ws.1.clone())) Some((buf, ws.1.clone()))
@ -308,8 +308,13 @@ impl VoxelImageEncoding for MixedEncoding {
CompressionType::Fast, CompressionType::Fast,
FilterType::Up, FilterType::Up,
); );
png.write_image(x.as_raw(), x.width(), x.height(), image::ColorType::L8) png.write_image(
.ok()?; x.as_raw(),
x.width(),
x.height(),
image::ExtendedColorType::L8,
)
.ok()?;
indices[i] = buf.len(); indices[i] = buf.len();
Some(()) Some(())
}; };
@ -337,14 +342,14 @@ impl VoxelImageDecoding for MixedEncoding {
indices[2]..indices[3], indices[2]..indices[3],
indices[3]..quad.len(), indices[3]..quad.len(),
]; ];
let a = image_from_bytes(PngDecoder::new(&quad[ranges[0].clone()]).ok()?)?; let a = image_from_bytes(PngDecoder::new(Cursor::new(&quad[ranges[0].clone()])).ok()?)?;
let b = image_from_bytes(PngDecoder::new(&quad[ranges[1].clone()]).ok()?)?; let b = image_from_bytes(PngDecoder::new(Cursor::new(&quad[ranges[1].clone()])).ok()?)?;
let c = image_from_bytes(PngDecoder::new(&quad[ranges[2].clone()]).ok()?)?; let c = image_from_bytes(PngDecoder::new(Cursor::new(&quad[ranges[2].clone()])).ok()?)?;
let sprite_data = let sprite_data =
bincode::deserialize::<CompressedData<Vec<[u8; 3]>>>(&quad[ranges[4].clone()]) bincode::deserialize::<CompressedData<Vec<[u8; 3]>>>(&quad[ranges[4].clone()])
.ok()? .ok()?
.decompress()?; .decompress()?;
let d = image_from_bytes(JpegDecoder::new(&quad[ranges[3].clone()]).ok()?)?; let d = image_from_bytes(JpegDecoder::new(Cursor::new(&quad[ranges[3].clone()])).ok()?)?;
Some((a, b, c, d, sprite_data)) Some((a, b, c, d, sprite_data))
} }
@ -417,7 +422,7 @@ impl VoxelImageEncoding for MixedEncodingSparseSprites {
ws.0.as_raw(), ws.0.as_raw(),
ws.0.width(), ws.0.width(),
ws.0.height(), ws.0.height(),
image::ColorType::L8, image::ExtendedColorType::L8,
) )
.ok()?; .ok()?;
let index = buf.len(); let index = buf.len();
@ -474,8 +479,13 @@ impl VoxelImageEncoding for MixedEncodingDenseSprites {
CompressionType::Fast, CompressionType::Fast,
FilterType::Up, FilterType::Up,
); );
png.write_image(x.as_raw(), x.width(), x.height(), image::ColorType::L8) png.write_image(
.ok()?; x.as_raw(),
x.width(),
x.height(),
image::ExtendedColorType::L8,
)
.ok()?;
indices[i] = buf.len(); indices[i] = buf.len();
Some(()) Some(())
}; };
@ -650,8 +660,13 @@ impl<'a, NN: NearestNeighbor, const N: u32> VoxelImageEncoding for PaletteEncodi
CompressionType::Fast, CompressionType::Fast,
FilterType::Up, FilterType::Up,
); );
png.write_image(x.as_raw(), x.width(), x.height(), image::ColorType::L8) png.write_image(
.ok()?; x.as_raw(),
x.width(),
x.height(),
image::ExtendedColorType::L8,
)
.ok()?;
indices[i] = buf.len(); indices[i] = buf.len();
Some(()) Some(())
}; };

View File

@ -87,7 +87,7 @@ fn image_from_function<F: FnMut(u32, u32) -> [u8; 3]>(
heightmap.as_raw(), heightmap.as_raw(),
heightmap.width(), heightmap.width(),
heightmap.height(), heightmap.height(),
image::ColorType::Rgb8, image::ExtendedColorType::Rgb8,
) )
.unwrap(); .unwrap();
let mut f = File::create(name).unwrap(); let mut f = File::create(name).unwrap();