add rest of compression algorithms

This commit is contained in:
Vinzenz Schroeter 2024-05-11 23:16:41 +02:00
parent 664ce42e6c
commit 0a3f400e92
13 changed files with 342 additions and 49 deletions

110
Cargo.lock generated
View file

@ -14,6 +14,38 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]]
name = "bzip2"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bdb116a6ef3f6c3698828873ad02c3014b3c85cadb88496095628e3ef1e347f8"
dependencies = [
"bzip2-sys",
"libc",
]
[[package]]
name = "bzip2-sys"
version = "0.1.11+1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "736a955f3fa7875102d57c82b8cac37ec45224a07fd32d58f9f7a186b6cd4cdc"
dependencies = [
"cc",
"libc",
"pkg-config",
]
[[package]]
name = "cc"
version = "1.0.97"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "099a5357d84c4c61eb35fc8eafa9a79a902c2f76911e5747ced4e032edd8d9b4"
dependencies = [
"jobserver",
"libc",
"once_cell",
]
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -39,12 +71,47 @@ dependencies = [
"miniz_oxide", "miniz_oxide",
] ]
[[package]]
name = "jobserver"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2b099aaa34a9751c5bf0878add70444e1ed2dd73f347be99003d4577277de6e"
dependencies = [
"libc",
]
[[package]]
name = "libc"
version = "0.2.154"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.21" version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
[[package]]
name = "lz4"
version = "1.24.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e9e2dd86df36ce760a60f6ff6ad526f7ba1f14ba0356f8254fb6905e6494df1"
dependencies = [
"libc",
"lz4-sys",
]
[[package]]
name = "lz4-sys"
version = "1.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57d27b317e207b10f69f5e75494119e391a96f48861ae870d1da6edac98ca900"
dependencies = [
"cc",
"libc",
]
[[package]] [[package]]
name = "miniz_oxide" name = "miniz_oxide"
version = "0.7.2" version = "0.7.2"
@ -138,6 +205,18 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "once_cell"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "pkg-config"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.82" version = "1.0.82"
@ -160,11 +239,14 @@ dependencies = [
name = "servicepoint2" name = "servicepoint2"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bzip2",
"flate2", "flate2",
"log", "log",
"lz4",
"num", "num",
"num-derive", "num-derive",
"num-traits", "num-traits",
"zstd",
] ]
[[package]] [[package]]
@ -183,3 +265,31 @@ name = "unicode-ident"
version = "1.0.12" version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "zstd"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d789b1514203a1120ad2429eae43a7bd32b90976a7bb8a05f7ec02fa88cc23a"
dependencies = [
"zstd-safe",
]
[[package]]
name = "zstd-safe"
version = "7.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd99b45c6bc03a018c8b8a86025678c87e55526064e38f9df301989dce7ec0a"
dependencies = [
"zstd-sys",
]
[[package]]
name = "zstd-sys"
version = "2.0.10+zstd.1.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c253a4914af5bafc8fa8c86ee400827e83cf6ec01195ec1f1ed8441bf00d65aa"
dependencies = [
"cc",
"pkg-config",
]

View file

@ -8,4 +8,15 @@ num = "0.4"
num-derive = "0.4" num-derive = "0.4"
num-traits = "0.2" num-traits = "0.2"
log = "0.4" log = "0.4"
flate2 = "1.0" flate2 = { version = "1.0", optional = true }
bzip2 = { version = "0.4", optional = true }
lz4 = { version = "1.24", optional = true }
zstd = { version = "0.13", optional = true }
[features]
default = ["compression-gz", "compression-bz", "compression-lz", "compression-zs"]
compression-gz = ["dep:flate2"]
compression-bz = ["dep:bzip2"]
compression-lz = ["dep:lz4"]
compression-zs = ["dep:zstd"]

104
examples/Cargo.lock generated
View file

@ -82,6 +82,38 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]]
name = "bzip2"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bdb116a6ef3f6c3698828873ad02c3014b3c85cadb88496095628e3ef1e347f8"
dependencies = [
"bzip2-sys",
"libc",
]
[[package]]
name = "bzip2-sys"
version = "0.1.11+1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "736a955f3fa7875102d57c82b8cac37ec45224a07fd32d58f9f7a186b6cd4cdc"
dependencies = [
"cc",
"libc",
"pkg-config",
]
[[package]]
name = "cc"
version = "1.0.97"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "099a5357d84c4c61eb35fc8eafa9a79a902c2f76911e5747ced4e032edd8d9b4"
dependencies = [
"jobserver",
"libc",
"once_cell",
]
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -216,6 +248,15 @@ version = "1.70.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800"
[[package]]
name = "jobserver"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2b099aaa34a9751c5bf0878add70444e1ed2dd73f347be99003d4577277de6e"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.154" version = "0.2.154"
@ -228,6 +269,26 @@ version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
[[package]]
name = "lz4"
version = "1.24.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e9e2dd86df36ce760a60f6ff6ad526f7ba1f14ba0356f8254fb6905e6494df1"
dependencies = [
"libc",
"lz4-sys",
]
[[package]]
name = "lz4-sys"
version = "1.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57d27b317e207b10f69f5e75494119e391a96f48861ae870d1da6edac98ca900"
dependencies = [
"cc",
"libc",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.7.2" version = "2.7.2"
@ -337,6 +398,18 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "once_cell"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "pkg-config"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
[[package]] [[package]]
name = "ppv-lite86" name = "ppv-lite86"
version = "0.2.17" version = "0.2.17"
@ -435,11 +508,14 @@ checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56"
name = "servicepoint2" name = "servicepoint2"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bzip2",
"flate2", "flate2",
"log", "log",
"lz4",
"num", "num",
"num-derive", "num-derive",
"num-traits", "num-traits",
"zstd",
] ]
[[package]] [[package]]
@ -559,3 +635,31 @@ dependencies = [
"log", "log",
"servicepoint2", "servicepoint2",
] ]
[[package]]
name = "zstd"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d789b1514203a1120ad2429eae43a7bd32b90976a7bb8a05f7ec02fa88cc23a"
dependencies = [
"zstd-safe",
]
[[package]]
name = "zstd-safe"
version = "7.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd99b45c6bc03a018c8b8a86025678c87e55526064e38f9df301989dce7ec0a"
dependencies = [
"zstd-sys",
]
[[package]]
name = "zstd-sys"
version = "2.0.10+zstd.1.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c253a4914af5bafc8fa8c86ee400827e83cf6ec01195ec1f1ed8441bf00d65aa"
dependencies = [
"cc",
"pkg-config",
]

View file

@ -6,6 +6,6 @@ publish = false
[dependencies] [dependencies]
clap = { version = "4.5.4", features = ["derive"] } clap = { version = "4.5.4", features = ["derive"] }
servicepoint2 = { path = "../.." } servicepoint2 = { path = "../.." , default-features = false }
log = "0.4.21" log = "0.4.21"
env_logger = "0.11.3" env_logger = "0.11.3"

View file

@ -5,7 +5,7 @@ edition = "2021"
publish = false publish = false
[dependencies] [dependencies]
servicepoint2 = { path = "../.." } servicepoint2 = { path = "../.." , default-features = false }
rand = "0.8.5" rand = "0.8.5"
clap = { version = "4.5.4", features = ["derive"] } clap = { version = "4.5.4", features = ["derive"] }
log = "0.4.21" log = "0.4.21"

View file

@ -5,7 +5,7 @@ edition = "2021"
publish = false publish = false
[dependencies] [dependencies]
servicepoint2 = { path = "../.." } servicepoint2 = { path = "../.." , default-features = false }
clap = { version = "4.5.4", features = ["derive"] } clap = { version = "4.5.4", features = ["derive"] }
env_logger = "0.11.3" env_logger = "0.11.3"
log = "0.4.21" log = "0.4.21"

View file

@ -5,7 +5,7 @@ edition = "2021"
publish = false publish = false
[dependencies] [dependencies]
servicepoint2 = { path = "../.." } servicepoint2 = { path = "../.." , default-features = false }
clap = { version = "4.5.4", features = ["derive"] } clap = { version = "4.5.4", features = ["derive"] }
rand = "0.8.5" rand = "0.8.5"
env_logger = "0.11.3" env_logger = "0.11.3"

View file

@ -1,4 +1,3 @@
use std::cmp::Ord;
use std::time::Duration; use std::time::Duration;
use clap::Parser; use clap::Parser;
use rand::Rng; use rand::Rng;

View file

@ -15,9 +15,7 @@ struct Cli {
} }
fn main() { fn main() {
env_logger::builder() env_logger::init();
.filter_level(log::LevelFilter::Debug)
.init();
let cli = Cli::parse(); let cli = Cli::parse();
let connection = Connection::open(cli.destination).unwrap(); let connection = Connection::open(cli.destination).unwrap();

View file

@ -1,9 +1,7 @@
use std::io::Read; use crate::{BitVec, ByteGrid, Header, Packet, PixelGrid, TILE_SIZE};
use flate2::bufread::GzEncoder;
use flate2::Compression;
use flate2::read::GzDecoder;
use crate::{BitVec, ByteGrid, Header, Packet, Payload, PixelGrid, TILE_SIZE};
use crate::command_codes::{CommandCode, CompressionCode}; use crate::command_codes::{CommandCode, CompressionCode};
use crate::compression::{into_compressed, into_decompressed};
/// An origin marks the top left position of the /// An origin marks the top left position of the
/// data sent to the display. /// data sent to the display.
@ -239,38 +237,3 @@ fn packet_into_linear_bitmap(packet: Packet) -> Result<(BitVec, CompressionCode)
}; };
Ok((BitVec::load(&payload), sub)) Ok((BitVec::load(&payload), sub))
} }
fn into_decompressed(kind: CompressionCode, payload: Payload) -> Option<Payload> {
match kind {
CompressionCode::None => Some(payload),
CompressionCode::Gz => {
let mut decoder = GzDecoder::new(&*payload);
let mut decompressed = vec!();
match decoder.read_to_end(&mut decompressed) {
Err(_) => None,
Ok(_) => Some(decompressed)
}
}
CompressionCode::Bz => todo!(),
CompressionCode::Lz => todo!(),
CompressionCode::Zs => todo!(),
}
}
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!(),
}
}

View file

@ -33,9 +33,13 @@ impl CommandCode {
#[derive(Debug, FromPrimitive, ToPrimitive, Clone, Copy)] #[derive(Debug, FromPrimitive, ToPrimitive, Clone, Copy)]
pub enum CompressionCode { pub enum CompressionCode {
None = 0x0, None = 0x0,
#[cfg(feature = "compression-gz")]
Gz = 0x677a, Gz = 0x677a,
#[cfg(feature = "compression-bz")]
Bz = 0x627a, Bz = 0x627a,
#[cfg(feature = "compression-lz")]
Lz = 0x6c7a, Lz = 0x6c7a,
#[cfg(feature = "compression-zs")]
Zs = 0x7a73, Zs = 0x7a73,
} }

103
src/compression.rs Normal file
View file

@ -0,0 +1,103 @@
use crate::{CompressionCode, Payload};
use std::io::{Read, Write};
#[cfg(feature = "compression-bz")]
use bzip2::read::{BzEncoder, BzDecoder};
#[cfg(feature = "compression-gz")]
use flate2::read::{GzEncoder, GzDecoder};
#[cfg(feature = "compression-lz")]
use lz4::{EncoderBuilder as Lz4EncoderBuilder, Decoder as Lz4Decoder};
#[cfg(feature = "compression-zs")]
use zstd::{Encoder as ZstdEncoder, Decoder as ZstdDecoder};
pub(crate) fn into_decompressed(kind: CompressionCode, payload: Payload) -> Option<Payload> {
match kind {
CompressionCode::None => Some(payload),
#[cfg(feature = "compression-gz")]
CompressionCode::Gz => {
let mut decoder = GzDecoder::new(&*payload);
let mut decompressed = vec!();
match decoder.read_to_end(&mut decompressed) {
Err(_) => None,
Ok(_) => Some(decompressed)
}
}
#[cfg(feature = "compression-bz")]
CompressionCode::Bz => {
let mut decoder = BzDecoder::new(&*payload);
let mut decompressed = vec!();
match decoder.read_to_end(&mut decompressed) {
Err(_) => None,
Ok(_) => Some(decompressed)
}
}
#[cfg(feature = "compression-lz")]
CompressionCode::Lz => {
let mut decoder = match Lz4Decoder::new(&*payload) {
Err(_) => return None,
Ok(value) => value
};
let mut decompressed = vec!();
match decoder.read_to_end(&mut decompressed) {
Err(_) => None,
Ok(_) => Some(decompressed)
}
}
#[cfg(feature = "compression-zs")]
CompressionCode::Zs => {
let mut decoder = match ZstdDecoder::new(&*payload) {
Err(_) => return None,
Ok(value) => value
};
let mut decompressed = vec!();
match decoder.read_to_end(&mut decompressed) {
Err(_) => None,
Ok(_) => Some(decompressed)
}
}
}
}
pub(crate) fn into_compressed(kind: CompressionCode, payload: Payload) -> Payload {
match kind {
CompressionCode::None => payload,
#[cfg(feature = "compression-gz")]
CompressionCode::Gz => {
let mut encoder = GzEncoder::new(&*payload, flate2::Compression::fast());
let mut compressed = vec!();
match encoder.read_to_end(&mut compressed) {
Err(err) => panic!("could not compress payload: {}", err),
Ok(_) => compressed,
}
}
#[cfg(feature = "compression-bz")]
CompressionCode::Bz => {
let mut encoder = BzEncoder::new(&*payload, bzip2::Compression::fast());
let mut compressed = vec!();
match encoder.read_to_end(&mut compressed) {
Err(err) => panic!("could not compress payload: {}", err),
Ok(_) => compressed,
}
}
#[cfg(feature = "compression-lz")]
CompressionCode::Lz => {
let mut encoder = Lz4EncoderBuilder::new()
.build(vec!())
.expect("could not create encoder");
encoder.write(&*payload)
.expect("could not write payload");
let (payload, _) = encoder.finish();
payload
}
#[cfg(feature = "compression-zs")]
CompressionCode::Zs => {
let mut encoder = ZstdEncoder::new(vec!(), zstd::DEFAULT_COMPRESSION_LEVEL)
.expect("could not create encoder");
encoder.write(&*payload)
.expect("could not compress payload");
encoder.finish()
.expect("could not finish encoding")
}
}
}

View file

@ -5,6 +5,7 @@ mod packet;
mod command; mod command;
mod command_codes; mod command_codes;
mod byte_grid; mod byte_grid;
mod compression;
pub use crate::connection::Connection; pub use crate::connection::Connection;
pub use crate::pixel_grid::PixelGrid; pub use crate::pixel_grid::PixelGrid;