Made tweaks to broken compression implementation

This commit is contained in:
G2-Games 2024-05-09 18:19:35 -05:00
parent c438399548
commit 402c8359fd
4 changed files with 16 additions and 20 deletions

View file

@ -10,5 +10,4 @@ A encoder/decoder for CZ# image files used in
[dependencies] [dependencies]
byteorder = "1.5.0" byteorder = "1.5.0"
thiserror = "1.0.59" thiserror = "1.0.59"
png = "0.17.13" image = "0.25.1"
image = { version = "0.25.1", default-features = false }

View file

@ -343,10 +343,9 @@ fn compress_lzw(data: &[u8], size: usize, last: String) -> (usize, Vec<u16>, Str
} }
let mut dictionary_count = (dictionary.len() + 1) as u16; let mut dictionary_count = (dictionary.len() + 1) as u16;
let mut element = String::new(); let mut element = String::new();
if last.len() != 0 { if last.len() != 0 {
element = last.clone() element = last
} }
let mut compressed = Vec::with_capacity(size); let mut compressed = Vec::with_capacity(size);
@ -373,7 +372,7 @@ fn compress_lzw(data: &[u8], size: usize, last: String) -> (usize, Vec<u16>, Str
let last_element = element; let last_element = element;
if compressed.len() == 0 { if compressed.len() == 0 {
if last_element.len() != 0 { if last_element.len() != 0 {
for c in last_element.bytes() { for c in last_element.chars() {
compressed.push(*dictionary.get(&c.to_string()).unwrap()); compressed.push(*dictionary.get(&c.to_string()).unwrap());
} }
} }

View file

@ -1,5 +1,5 @@
use std::{ use std::{
fs::File, io::{BufReader, Cursor, Read, Seek, SeekFrom, Write}, path::Path fs::{self, File}, io::{BufReader, BufWriter, Read, Seek, SeekFrom, Write}, path::Path
}; };
use byteorder::ReadBytesExt; use byteorder::ReadBytesExt;
@ -22,20 +22,14 @@ impl DynamicCz {
Self::decode(&mut img_file) Self::decode(&mut img_file)
} }
pub fn save_as_png<P: ?Sized + AsRef<Path>>(&self, path: &P) -> Result<(), png::EncodingError> { pub fn save_as_png<P: ?Sized + AsRef<Path>>(&self, path: &P) -> Result<(), image::error::EncodingError> {
let file = std::fs::File::create(path).unwrap(); let image = image::RgbaImage::from_raw(
let writer = std::io::BufWriter::new(file); self.header_common.width() as u32,
self.header_common.height() as u32,
self.bitmap.clone()
).unwrap();
let mut encoder = png::Encoder::new( image.save_with_format(path, image::ImageFormat::Png).unwrap();
writer,
self.header().width() as u32,
self.header().height() as u32,
);
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header()?;
writer.write_image_data(self.bitmap())?; // Save
Ok(()) Ok(())
} }

View file

@ -1,7 +1,11 @@
use cz::dynamic::DynamicCz; use cz::dynamic::DynamicCz;
fn main() { fn main() {
let img = DynamicCz::open("../../test_files/x5a3bvy.cz1").unwrap(); let img = DynamicCz::open("font72.cz1").unwrap();
img.save_as_cz("test.cz1").unwrap(); img.save_as_cz("test.cz1").unwrap();
img.save_as_png("test1.png").unwrap();
let img2 = DynamicCz::open("test.cz1").unwrap();
img2.save_as_png("test2.png").unwrap();
} }