From dd99a0d834b8ea40ecd765958e3b821abe880755 Mon Sep 17 00:00:00 2001 From: G2-Games Date: Wed, 13 Nov 2024 10:53:15 -0600 Subject: [PATCH] Removed `image` as a dependency from `cz` --- cz/Cargo.toml | 10 ++-------- cz/src/dynamic.rs | 29 ----------------------------- pak_explorer/Cargo.toml | 3 ++- pak_explorer/src/main.rs | 9 ++++++++- utils/Cargo.toml | 2 +- utils/src/bin/czutil.rs | 30 ++++++++++++++++++++++++++---- 6 files changed, 39 insertions(+), 44 deletions(-) diff --git a/cz/Cargo.toml b/cz/Cargo.toml index 4b68c79..5e985b8 100644 --- a/cz/Cargo.toml +++ b/cz/Cargo.toml @@ -9,17 +9,11 @@ Prototype Ltd. license = "MIT" authors.workspace = true -[features] -png = ["dep:image"] - [dependencies] -byteorder-lite = "0.1.0" -thiserror = "1.0" +byteorder-lite = "0.1" +thiserror = "2.0" imagequant = "4.3" rgb = "0.8" -# Only active on PNG feature -image = { version = "0.25", optional = true } - [lints] workspace = true diff --git a/cz/src/dynamic.rs b/cz/src/dynamic.rs index ca9f759..5d2dd66 100644 --- a/cz/src/dynamic.rs +++ b/cz/src/dynamic.rs @@ -192,35 +192,6 @@ impl DynamicCz { Ok(()) } - /// Save the CZ# image as a lossless PNG file. - /// - /// Internally, the [`DynamicCz`] struct operates on 32-bit RGBA values, - /// which is the highest encountered in CZ# files, therefore saving them - /// as a PNG of the same or better quality is lossless. - #[cfg(feature = "png")] - pub fn save_as_png>( - &self, - path: &P, - ) -> Result<(), image::error::EncodingError> { - let size = (self.header_common.width() as u32 * self.header_common.height() as u32) * 4; - - let mut buf = vec![0; size as usize]; - buf[..self.bitmap.len()].copy_from_slice(&self.bitmap); - - let image = image::RgbaImage::from_raw( - self.header_common.width() as u32, - self.header_common.height() as u32, - buf.clone(), - ) - .unwrap(); - - image - .save_with_format(path, image::ImageFormat::Png) - .unwrap(); - - Ok(()) - } - /// Create a CZ# image from RGBA bytes. The bytes *must* be RGBA, as that /// is the only format that is used internally. pub fn from_raw(version: CzVersion, width: u16, height: u16, bitmap: Vec) -> Self { diff --git a/pak_explorer/Cargo.toml b/pak_explorer/Cargo.toml index e14ed88..8c4a591 100644 --- a/pak_explorer/Cargo.toml +++ b/pak_explorer/Cargo.toml @@ -11,9 +11,10 @@ publish = false [dependencies] colog = "1.3.0" -cz = { path = "../cz/", features = ["png"] } +cz = { path = "../cz/" } eframe = { version = "0.28.1", default-features = false, features = ["wayland", "x11", "accesskit", "default_fonts", "wgpu"] } egui_extras = "0.28.1" +image = { version = "0.25.5", default-features = false, features = ["png"] } log = "0.4.22" luca_pak = { path = "../luca_pak/" } rfd = "0.14.1" diff --git a/pak_explorer/src/main.rs b/pak_explorer/src/main.rs index 806631a..946f690 100644 --- a/pak_explorer/src/main.rs +++ b/pak_explorer/src/main.rs @@ -162,7 +162,14 @@ impl eframe::App for PakExplorer { entry.as_bytes(), )) .unwrap(); - cz.save_as_png(&path).unwrap(); + image::save_buffer_with_format( + path, + cz.as_raw(), + cz.header().width() as u32, + cz.header().height() as u32, + image::ColorType::Rgba8, + image::ImageFormat::Png + ).unwrap(); } } diff --git a/utils/Cargo.toml b/utils/Cargo.toml index 136bb7d..45781f4 100644 --- a/utils/Cargo.toml +++ b/utils/Cargo.toml @@ -13,7 +13,7 @@ name = "czutil" name = "pakutil" [dependencies] -cz = { path = "../cz/", features = ["png"] } +cz = { path = "../cz/" } luca_pak = { path = "../luca_pak/" } image = { version = "0.25", default-features = false, features = ["png"] } diff --git a/utils/src/bin/czutil.rs b/utils/src/bin/czutil.rs index 11b4b9e..0c27d77 100644 --- a/utils/src/bin/czutil.rs +++ b/utils/src/bin/czutil.rs @@ -1,4 +1,5 @@ -use clap::{error::ErrorKind, Command, Error, Parser, Subcommand}; +use clap::{error::ErrorKind, Error, Parser, Subcommand}; +use image::ColorType; use std::{ fs, path::{Path, PathBuf}, @@ -145,16 +146,37 @@ fn main() { } }; - cz.save_as_png(&final_path).unwrap(); + image::save_buffer_with_format( + final_path, + cz.as_raw(), + cz.header().width() as u32, + cz.header().height() as u32, + ColorType::Rgba8, + image::ImageFormat::Png + ).unwrap(); } } else { let cz = cz::open(input).unwrap(); if let Some(output) = output { - cz.save_as_png(output).unwrap(); + image::save_buffer_with_format( + output, + cz.as_raw(), + cz.header().width() as u32, + cz.header().height() as u32, + ColorType::Rgba8, + image::ImageFormat::Png + ).unwrap(); } else { let file_stem = PathBuf::from(input.file_name().unwrap()); - cz.save_as_png(&file_stem.with_extension("png")).unwrap(); + image::save_buffer_with_format( + file_stem.with_extension("png"), + cz.as_raw(), + cz.header().width() as u32, + cz.header().height() as u32, + ColorType::Rgba8, + image::ImageFormat::Png + ).unwrap(); } } }