mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-18 18:10:14 +01:00
move compression into own function
This commit is contained in:
parent
5c61d02749
commit
664ce42e6c
|
@ -2,7 +2,7 @@ use std::io::Read;
|
||||||
use flate2::bufread::GzEncoder;
|
use flate2::bufread::GzEncoder;
|
||||||
use flate2::Compression;
|
use flate2::Compression;
|
||||||
use flate2::read::GzDecoder;
|
use flate2::read::GzDecoder;
|
||||||
use crate::{BitVec, ByteGrid, Header, Packet, PixelGrid, TILE_SIZE};
|
use crate::{BitVec, ByteGrid, Header, Packet, Payload, PixelGrid, TILE_SIZE};
|
||||||
use crate::command_codes::{CommandCode, CompressionCode};
|
use crate::command_codes::{CommandCode, CompressionCode};
|
||||||
|
|
||||||
/// An origin marks the top left position of the
|
/// An origin marks the top left position of the
|
||||||
|
@ -97,7 +97,7 @@ pub enum TryFromPacketError {
|
||||||
UnexpectedPayloadSize(usize, usize),
|
UnexpectedPayloadSize(usize, usize),
|
||||||
ExtraneousHeaderValues,
|
ExtraneousHeaderValues,
|
||||||
InvalidCompressionCode(u16),
|
InvalidCompressionCode(u16),
|
||||||
DecompressionFailed(std::io::Error),
|
DecompressionFailed,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<Packet> for Command {
|
impl TryFrom<Packet> for Command {
|
||||||
|
@ -186,21 +186,7 @@ impl TryFrom<Packet> for Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bitmap_linear_into_packet(command: CommandCode, offset: Offset, compression: CompressionCode, payload: Vec<u8>) -> Packet {
|
fn bitmap_linear_into_packet(command: CommandCode, offset: Offset, compression: CompressionCode, payload: Vec<u8>) -> Packet {
|
||||||
let payload = match compression {
|
let payload = into_compressed(compression, payload);
|
||||||
CompressionCode::None => payload,
|
|
||||||
CompressionCode::Gz => {
|
|
||||||
let mut encoder = GzEncoder::new(&*payload, Compression::best());
|
|
||||||
let mut compressed = vec!();
|
|
||||||
match encoder.read_to_end(&mut compressed) {
|
|
||||||
Err(err) => panic!("could not compress payload: {}", err),
|
|
||||||
Ok(_) => compressed,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
CompressionCode::Bz => todo!(),
|
|
||||||
CompressionCode::Lz => todo!(),
|
|
||||||
CompressionCode::Zs => todo!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let compression = CompressionCode::to_primitive(&compression);
|
let compression = CompressionCode::to_primitive(&compression);
|
||||||
Packet(Header(command.to_primitive(), offset, payload.len() as u16, compression, 0), payload)
|
Packet(Header(command.to_primitive(), offset, payload.len() as u16, compression, 0), payload)
|
||||||
}
|
}
|
||||||
|
@ -247,21 +233,44 @@ fn packet_into_linear_bitmap(packet: Packet) -> Result<(BitVec, CompressionCode)
|
||||||
None => return Err(TryFromPacketError::InvalidCompressionCode(sub)),
|
None => return Err(TryFromPacketError::InvalidCompressionCode(sub)),
|
||||||
Some(value) => value
|
Some(value) => value
|
||||||
};
|
};
|
||||||
let payload = match sub {
|
let payload = match into_decompressed(sub, payload) {
|
||||||
CompressionCode::None => payload,
|
None => return Err(TryFromPacketError::DecompressionFailed),
|
||||||
|
Some(value) => value
|
||||||
|
};
|
||||||
|
Ok((BitVec::load(&payload), sub))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_decompressed(kind: CompressionCode, payload: Payload) -> Option<Payload> {
|
||||||
|
match kind {
|
||||||
|
CompressionCode::None => Some(payload),
|
||||||
CompressionCode::Gz => {
|
CompressionCode::Gz => {
|
||||||
let mut decoder = GzDecoder::new(&*payload);
|
let mut decoder = GzDecoder::new(&*payload);
|
||||||
let mut decompressed = vec!();
|
let mut decompressed = vec!();
|
||||||
match decoder.read_to_end(&mut decompressed) {
|
match decoder.read_to_end(&mut decompressed) {
|
||||||
Err(err) => return Err(TryFromPacketError::DecompressionFailed(err)),
|
Err(_) => None,
|
||||||
Ok(_) => {}
|
Ok(_) => Some(decompressed)
|
||||||
}
|
}
|
||||||
decompressed
|
|
||||||
}
|
}
|
||||||
CompressionCode::Bz => todo!(),
|
CompressionCode::Bz => todo!(),
|
||||||
CompressionCode::Lz => todo!(),
|
CompressionCode::Lz => todo!(),
|
||||||
CompressionCode::Zs => todo!(),
|
CompressionCode::Zs => todo!(),
|
||||||
};
|
}
|
||||||
|
|
||||||
Ok((BitVec::load(&payload), sub))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn into_compressed(kind: CompressionCode, payload: Payload) -> Payload {
|
||||||
|
match kind {
|
||||||
|
CompressionCode::None => payload,
|
||||||
|
CompressionCode::Gz => {
|
||||||
|
let mut encoder = GzEncoder::new(&*payload, Compression::best());
|
||||||
|
let mut compressed = vec!();
|
||||||
|
match encoder.read_to_end(&mut compressed) {
|
||||||
|
Err(err) => panic!("could not compress payload: {}", err),
|
||||||
|
Ok(_) => compressed,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CompressionCode::Bz => todo!(),
|
||||||
|
CompressionCode::Lz => todo!(),
|
||||||
|
CompressionCode::Zs => todo!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ use num::{FromPrimitive, ToPrimitive};
|
||||||
use num_derive::{FromPrimitive, ToPrimitive};
|
use num_derive::{FromPrimitive, ToPrimitive};
|
||||||
|
|
||||||
#[repr(u16)]
|
#[repr(u16)]
|
||||||
#[derive(Debug, FromPrimitive, ToPrimitive)]
|
#[derive(Debug, FromPrimitive, ToPrimitive, Copy, Clone)]
|
||||||
pub enum CommandCode {
|
pub enum CommandCode {
|
||||||
Clear = 0x0002,
|
Clear = 0x0002,
|
||||||
Cp437Data = 0x0003,
|
Cp437Data = 0x0003,
|
||||||
|
@ -30,7 +30,7 @@ impl CommandCode {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(u16)]
|
#[repr(u16)]
|
||||||
#[derive(Debug, FromPrimitive, ToPrimitive)]
|
#[derive(Debug, FromPrimitive, ToPrimitive, Clone, Copy)]
|
||||||
pub enum CompressionCode {
|
pub enum CompressionCode {
|
||||||
None = 0x0,
|
None = 0x0,
|
||||||
Gz = 0x677a,
|
Gz = 0x677a,
|
||||||
|
|
Loading…
Reference in a new issue