From a34bd81290b5070801033c1b2c013ebafc34cefa Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sun, 17 Sep 2023 03:22:54 +0400 Subject: Первая версия декоратора текстур MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- texture-decoder/Cargo.toml | 8 ++++++++ texture-decoder/src/main.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 texture-decoder/Cargo.toml create mode 100644 texture-decoder/src/main.rs (limited to 'texture-decoder') diff --git a/texture-decoder/Cargo.toml b/texture-decoder/Cargo.toml new file mode 100644 index 0000000..6694196 --- /dev/null +++ b/texture-decoder/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "texture-decoder" +version = "0.1.0" +edition = "2021" + +[dependencies] +byteorder = "1.4.3" +image = "0.24.7" diff --git a/texture-decoder/src/main.rs b/texture-decoder/src/main.rs new file mode 100644 index 0000000..d12d71a --- /dev/null +++ b/texture-decoder/src/main.rs @@ -0,0 +1,41 @@ +use std::io::Read; + +use byteorder::ReadBytesExt; +use image::Rgba; + +fn decode_texture(file_path: &str, output_path: &str) -> Result<(), std::io::Error> { + // Читаем файл + let mut file = std::fs::File::open(file_path)?; + let mut buffer: Vec = Vec::new(); + file.read_to_end(&mut buffer)?; + + // Декодируем метаданные + let mut cursor = std::io::Cursor::new(&buffer[4..]); + let img_width = cursor.read_u32::()?; + let img_height = cursor.read_u32::()?; + + // Пропустить оставшиеся байты метаданных + cursor.set_position(20); + + // Извлекаем данные изображения + let image_data = buffer[cursor.position() as usize..].to_vec(); + let img = + image::ImageBuffer::, _>::from_raw(img_width, img_height, image_data.to_vec()) + .expect("Failed to decode image"); + + // Сохраняем изображение + img.save(output_path).unwrap(); + + Ok(()) +} + +fn main() { + let args: Vec = std::env::args().collect(); + + let input = &args[1]; + let output = &args[2]; + + if let Err(err) = decode_texture(&input, &output) { + eprintln!("Error: {}", err) + } +} -- cgit v1.2.3