init: initial commit

This commit is contained in:
Louis Dalibard 2024-07-04 17:41:19 +02:00
parent 152b482ac5
commit 181cdbfc2d
4 changed files with 52 additions and 0 deletions

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "qotk"
version = "0.1.0"
edition = "2021"
[dependencies]
image = "0.25.1"
zstd = {version = "0.13.1", features = ["zstdmt"] }

19
src/decode.rs Normal file
View File

@ -0,0 +1,19 @@
use std::{fs::File, io::Read};
use image::RgbaImage;
use zstd::stream::read::Decoder;
pub fn decode(in_path_str: &str, out_path_str: &str) {
let in_file = File::open(in_path_str).unwrap();
let mut decoder = Decoder::new(in_file).unwrap();
let mut width_bytes = [0u8; 4];
let mut height_bytes = [0u8; 4];
decoder.read_exact(&mut width_bytes).unwrap();
decoder.read_exact(&mut height_bytes).unwrap();
let mut img = RgbaImage::new(
u32::from_be_bytes(width_bytes),
u32::from_be_bytes(height_bytes),
);
decoder.read_exact(&mut img).unwrap();
decoder.finish();
img.save(out_path_str).unwrap();
}

18
src/encode.rs Normal file
View File

@ -0,0 +1,18 @@
use image::{io::Reader as ImageReader, GenericImageView};
use std::fs::File;
use std::io::prelude::*;
use zstd::stream::write::Encoder;
pub fn encode(in_path_str: &str, out_path_str: &str, level: i32, n_workers: u32) {
let img = ImageReader::open(in_path_str).unwrap().decode().unwrap();
let (width, height) = &img.dimensions();
let width_bytes = width.to_be_bytes();
let height_bytes = height.to_be_bytes();
let out_file = File::create(out_path_str).unwrap();
let mut encoder = Encoder::new(out_file, level).unwrap(); /* 1-22 */
encoder.multithread(n_workers).unwrap();
encoder.write_all(&width_bytes).unwrap();
encoder.write_all(&height_bytes).unwrap();
encoder.write_all(img.into_rgba8().as_raw()).unwrap();
encoder.finish().unwrap();
}

7
src/main.rs Normal file
View File

@ -0,0 +1,7 @@
mod decode;
mod encode;
fn main() {
encode::encode("in.png", "out.qotk", 30, 28); /* 1-22 */
decode::decode("out.qotk", "out.png");
}