mirror of
https://github.com/Dangoware/sqp.git
synced 2025-04-19 07:12:55 -05:00
Moved picture struct into new file
This commit is contained in:
parent
b6a86f7c42
commit
896b9f2228
3 changed files with 75 additions and 69 deletions
|
@ -5,4 +5,4 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
byteorder = "1.5.0"
|
||||
image = "0.25.2"
|
||||
image = { version = "0.25.2", default-features = false, features = ["png"] }
|
||||
|
|
74
src/main.rs
74
src/main.rs
|
@ -2,17 +2,16 @@ mod compression;
|
|||
mod header;
|
||||
mod operations;
|
||||
mod binio;
|
||||
mod picture;
|
||||
|
||||
use std::{fs::File, io::{Read, Write}};
|
||||
|
||||
use byteorder::{ReadBytesExt, WriteBytesExt, LE};
|
||||
use compression::{compress, decompress, ChunkInfo, CompressionInfo};
|
||||
use std::fs::File;
|
||||
use header::Header;
|
||||
use picture::DangoPicture;
|
||||
|
||||
use image::RgbaImage;
|
||||
use operations::{diff_line, line_diff};
|
||||
|
||||
fn main() {
|
||||
let image_data = image::open("dripping.png").unwrap().to_rgba8();
|
||||
let image_data = image::open("test.png").unwrap().to_rgba8();
|
||||
let encoded_dpf = DangoPicture {
|
||||
header: Header {
|
||||
width: image_data.width(),
|
||||
|
@ -34,66 +33,5 @@ fn main() {
|
|||
decoded_dpf.header.height,
|
||||
decoded_dpf.bitmap
|
||||
).unwrap();
|
||||
out_image.save("test2.png").unwrap();
|
||||
}
|
||||
|
||||
struct DangoPicture {
|
||||
header: Header,
|
||||
bitmap: Vec<u8>,
|
||||
}
|
||||
|
||||
impl DangoPicture {
|
||||
fn encode<O: Write + WriteBytesExt>(&self, mut output: O) {
|
||||
let header = Header {
|
||||
width: self.header.width,
|
||||
height: self.header.height,
|
||||
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Write out the header
|
||||
output.write_all(&header.to_bytes()).unwrap();
|
||||
|
||||
let modified_data = diff_line(header.width, header.height, &self.bitmap);
|
||||
|
||||
// Compress the image data
|
||||
let (compressed_data, compression_info) = compress(&modified_data);
|
||||
|
||||
// Write out compression info
|
||||
compression_info.write_into(&mut output).unwrap();
|
||||
|
||||
// Write out compressed data
|
||||
output.write_all(&compressed_data).unwrap();
|
||||
}
|
||||
|
||||
fn decode<I: Read + ReadBytesExt>(mut input: I) -> DangoPicture {
|
||||
let mut magic = [0u8; 8];
|
||||
input.read_exact(&mut magic).unwrap();
|
||||
|
||||
let header = Header {
|
||||
magic,
|
||||
width: input.read_u32::<LE>().unwrap(),
|
||||
height: input.read_u32::<LE>().unwrap(),
|
||||
};
|
||||
|
||||
let mut compression_info = CompressionInfo {
|
||||
chunk_count: input.read_u32::<LE>().unwrap() as usize,
|
||||
chunks: Vec::new(),
|
||||
};
|
||||
|
||||
for _ in 0..compression_info.chunk_count {
|
||||
compression_info.chunks.push(ChunkInfo {
|
||||
size_compressed: input.read_u32::<LE>().unwrap() as usize,
|
||||
size_raw: input.read_u32::<LE>().unwrap() as usize,
|
||||
});
|
||||
}
|
||||
|
||||
let preprocessed_bitmap = decompress(&mut input, &compression_info);
|
||||
let bitmap = line_diff(header.width, header.height, &preprocessed_bitmap);
|
||||
|
||||
DangoPicture {
|
||||
header,
|
||||
bitmap
|
||||
}
|
||||
}
|
||||
out_image.save("test.png").unwrap();
|
||||
}
|
||||
|
|
68
src/picture.rs
Normal file
68
src/picture.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
use std::io::{Read, Write};
|
||||
|
||||
use byteorder::{ReadBytesExt, WriteBytesExt, LE};
|
||||
|
||||
use crate::{compression::{compress, decompress, ChunkInfo, CompressionInfo}, header::Header, operations::{diff_line, line_diff}};
|
||||
|
||||
pub struct DangoPicture {
|
||||
pub header: Header,
|
||||
pub bitmap: Vec<u8>,
|
||||
}
|
||||
|
||||
impl DangoPicture {
|
||||
/// Encode the image into anything that implements [Write]
|
||||
pub fn encode<O: Write + WriteBytesExt>(&self, mut output: O) {
|
||||
let header = Header {
|
||||
width: self.header.width,
|
||||
height: self.header.height,
|
||||
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Write out the header
|
||||
output.write_all(&header.to_bytes()).unwrap();
|
||||
|
||||
let modified_data = diff_line(header.width, header.height, &self.bitmap);
|
||||
|
||||
// Compress the image data
|
||||
let (compressed_data, compression_info) = compress(&modified_data);
|
||||
|
||||
// Write out compression info
|
||||
compression_info.write_into(&mut output).unwrap();
|
||||
|
||||
// Write out compressed data
|
||||
output.write_all(&compressed_data).unwrap();
|
||||
}
|
||||
|
||||
/// Decode the image from anything that implements [Read]
|
||||
pub fn decode<I: Read + ReadBytesExt>(mut input: I) -> DangoPicture {
|
||||
let mut magic = [0u8; 8];
|
||||
input.read_exact(&mut magic).unwrap();
|
||||
|
||||
let header = Header {
|
||||
magic,
|
||||
width: input.read_u32::<LE>().unwrap(),
|
||||
height: input.read_u32::<LE>().unwrap(),
|
||||
};
|
||||
|
||||
let mut compression_info = CompressionInfo {
|
||||
chunk_count: input.read_u32::<LE>().unwrap() as usize,
|
||||
chunks: Vec::new(),
|
||||
};
|
||||
|
||||
for _ in 0..compression_info.chunk_count {
|
||||
compression_info.chunks.push(ChunkInfo {
|
||||
size_compressed: input.read_u32::<LE>().unwrap() as usize,
|
||||
size_raw: input.read_u32::<LE>().unwrap() as usize,
|
||||
});
|
||||
}
|
||||
|
||||
let preprocessed_bitmap = decompress(&mut input, &compression_info);
|
||||
let bitmap = line_diff(header.width, header.height, &preprocessed_bitmap);
|
||||
|
||||
DangoPicture {
|
||||
header,
|
||||
bitmap
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue