test for making function independently usable (warning: this is really slow)
This commit is contained in:
parent
265555d2f7
commit
98794a6dfc
2 changed files with 27 additions and 29 deletions
|
@ -1,10 +1,9 @@
|
||||||
use std::{
|
use std::{
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
fs::File,
|
io::{Read, Write},
|
||||||
io::{BufReader, BufWriter, Read, Write},
|
|
||||||
usize,
|
usize,
|
||||||
};
|
};
|
||||||
use std::path::PathBuf;
|
use std::io::Cursor;
|
||||||
use adler::Adler32;
|
use adler::Adler32;
|
||||||
use crc32fast::Hasher;
|
use crc32fast::Hasher;
|
||||||
use miniz_oxide::inflate::core::{
|
use miniz_oxide::inflate::core::{
|
||||||
|
@ -38,9 +37,8 @@ impl Chunk {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn encoder(inpath: PathBuf, outpath: PathBuf) {
|
pub fn fix(mut bytes: Vec<u8>) -> Vec<u8> {
|
||||||
let file = File::open(inpath).unwrap();
|
let mut bufread = Cursor::new(bytes.clone());
|
||||||
let mut bufread = BufReader::new(file);
|
|
||||||
|
|
||||||
// read the png header
|
// read the png header
|
||||||
let mut header = [0; 8];
|
let mut header = [0; 8];
|
||||||
|
@ -65,11 +63,11 @@ pub fn encoder(inpath: PathBuf, outpath: PathBuf) {
|
||||||
bufread.read_exact(&mut crc).unwrap();
|
bufread.read_exact(&mut crc).unwrap();
|
||||||
|
|
||||||
let mut chunk = Chunk { kind, data, crc };
|
let mut chunk = Chunk { kind, data, crc };
|
||||||
println!("{:?}", chunk);
|
// println!("{:?}", chunk);
|
||||||
|
|
||||||
// recode the compressed image data
|
// recode the compressed image data
|
||||||
if chunk.kind == *b"IDAT" {
|
if chunk.kind == *b"IDAT" {
|
||||||
println!("Decompressing IDAT chunk");
|
// println!("Decompressing IDAT chunk");
|
||||||
let mut decompressor = DecompressorOxide::new();
|
let mut decompressor = DecompressorOxide::new();
|
||||||
decompressor.init();
|
decompressor.init();
|
||||||
let mut buf = vec![0; 1024 * 1024 * 1024]; // this could probably be smaller
|
let mut buf = vec![0; 1024 * 1024 * 1024]; // this could probably be smaller
|
||||||
|
@ -83,10 +81,10 @@ pub fn encoder(inpath: PathBuf, outpath: PathBuf) {
|
||||||
| TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF,
|
| TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF,
|
||||||
);
|
);
|
||||||
|
|
||||||
println!(
|
// println!(
|
||||||
"Decompressed IDAT chunk status {:?}, bytes read {}, bytes outputted {}",
|
// "Decompressed IDAT chunk status {:?}, bytes read {}, bytes outputted {}",
|
||||||
data.0, data.1, data.2
|
// data.0, data.1, data.2
|
||||||
);
|
// );
|
||||||
|
|
||||||
let _ = buf.split_off(data.2);
|
let _ = buf.split_off(data.2);
|
||||||
|
|
||||||
|
@ -98,7 +96,7 @@ pub fn encoder(inpath: PathBuf, outpath: PathBuf) {
|
||||||
// replace the last 4 bytes of the data with the new checksum
|
// replace the last 4 bytes of the data with the new checksum
|
||||||
let data_len = chunk.data.len();
|
let data_len = chunk.data.len();
|
||||||
chunk.data[data_len - 4..].copy_from_slice(&csum);
|
chunk.data[data_len - 4..].copy_from_slice(&csum);
|
||||||
println!("Corrected Adler32 checksum");
|
// println!("Corrected Adler32 checksum");
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut hasher = Hasher::new();
|
let mut hasher = Hasher::new();
|
||||||
|
@ -107,28 +105,29 @@ pub fn encoder(inpath: PathBuf, outpath: PathBuf) {
|
||||||
let checksum = hasher.finalize();
|
let checksum = hasher.finalize();
|
||||||
|
|
||||||
if checksum != u32::from_be_bytes(chunk.crc) {
|
if checksum != u32::from_be_bytes(chunk.crc) {
|
||||||
println!("CRC error in chunk {:?}", chunk.kind_to_string());
|
// println!("CRC error in chunk {:?}", chunk.kind_to_string());
|
||||||
chunk.crc = checksum.to_be_bytes();
|
chunk.crc = checksum.to_be_bytes();
|
||||||
println!("Corrected CRC");
|
// println!("Corrected CRC");
|
||||||
}
|
}
|
||||||
|
|
||||||
chunks.push(chunk);
|
chunks.push(chunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
let ofile = File::create(outpath).unwrap();
|
// new cursor
|
||||||
let mut writer = BufWriter::new(ofile);
|
let mut newcursor = Cursor::new(Vec::new());
|
||||||
|
|
||||||
// write a new header
|
// write a new header
|
||||||
writer
|
newcursor
|
||||||
.write_all(&[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])
|
.write_all(&[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
for chunk in chunks {
|
for chunk in chunks {
|
||||||
writer
|
newcursor
|
||||||
.write_all(&u32::to_be_bytes(chunk.data.len() as u32))
|
.write_all(&u32::to_be_bytes(chunk.data.len() as u32))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
writer.write_all(&chunk.kind).unwrap();
|
newcursor.write_all(&chunk.kind).unwrap();
|
||||||
writer.write_all(&chunk.data).unwrap();
|
newcursor.write_all(&chunk.data).unwrap();
|
||||||
writer.write_all(&chunk.crc).unwrap();
|
newcursor.write_all(&chunk.crc).unwrap();
|
||||||
}
|
}
|
||||||
|
let finalres: Vec<u8> = newcursor.into_inner();
|
||||||
|
finalres
|
||||||
}
|
}
|
||||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -1,11 +1,8 @@
|
||||||
mod idk;
|
mod idk;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use glob::glob;
|
use glob::glob;
|
||||||
use std::io::{Read};
|
use std::{fs::{metadata}, path::{PathBuf}};
|
||||||
use std::{
|
use std::fs::{read, write, File};
|
||||||
fs::{metadata},
|
|
||||||
path::{PathBuf},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(version, about, long_about = None)]
|
#[command(version, about, long_about = None)]
|
||||||
|
@ -80,6 +77,8 @@ fn main() {
|
||||||
|
|
||||||
for thatpath in filelist {
|
for thatpath in filelist {
|
||||||
println!("Filename: {:?}",thatpath);
|
println!("Filename: {:?}",thatpath);
|
||||||
idk::encoder(thatpath.to_owned(), thatpath)
|
let frfr = read(thatpath.to_owned()).expect("wtf the path doesnt exist");
|
||||||
|
let mut fr = idk::fix(frfr);
|
||||||
|
write(thatpath, fr).expect("file could not write btw");
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue