implement optional parity contexts

This commit is contained in:
2022-09-20 23:24:19 +02:00
parent cc41feb5cd
commit f5fc9bd005
5 changed files with 84 additions and 29 deletions

View File

@@ -1,5 +1,6 @@
use anyhow::{bail, Result};
use std::io::prelude::*;
use std::process;
use std::{fs::File, path::PathBuf};
fn main() -> Result<()> {
@@ -10,6 +11,14 @@ fn main() -> Result<()> {
Some("pack") => {
let level = args.opt_value_from_str(["-l", "--level"])?.unwrap_or(2u8);
let use_bitstream = args.contains(["-b", "--bitstream"]);
let parity_contexts = args
.opt_value_from_str(["-p", "--parity"])?
.unwrap_or(1usize);
if parity_contexts != 1 && parity_contexts != 2 && parity_contexts != 4 {
eprintln!("--parity has to be 1, 2 or 4");
process::exit(1);
}
let infile = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
let outfile = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
@@ -23,6 +32,7 @@ fn main() -> Result<()> {
&data,
level,
use_bitstream,
parity_contexts,
Some(&mut |pos| {
pb.set(pos as u64);
}),
@@ -39,13 +49,21 @@ fn main() -> Result<()> {
}
Some("unpack") => {
let use_bitstream = args.contains(["-b", "--bitstream"]);
let parity_contexts = args
.opt_value_from_str(["-p", "--parity"])?
.unwrap_or(1usize);
if parity_contexts != 1 && parity_contexts != 2 && parity_contexts != 4 {
eprintln!("--parity has to be 1, 2 or 4");
process::exit(1);
}
let infile = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
let outfile = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
let mut data = vec![];
File::open(infile)?.read_to_end(&mut data)?;
let packed_data = upkr::unpack(&data, use_bitstream);
let packed_data = upkr::unpack(&data, use_bitstream, parity_contexts);
File::create(outfile)?.write_all(&packed_data)?;
}
Some(other) => {
@@ -58,10 +76,11 @@ fn main() -> Result<()> {
fn print_help() {
eprintln!("Usage:");
eprintln!(" upkr pack [-b] [-l level(0-9)] <infile> <outfile>");
eprintln!(" upkr unpack [-b] <infile> <outfile>");
eprintln!(" upkr pack [-b] [-l level(0-9)] [-p N] <infile> <outfile>");
eprintln!(" upkr unpack [-b] [-p N] <infile> <outfile>");
eprintln!();
eprintln!(" -b, --bitstream bitstream mode");
eprintln!(" -l, --level N compression level 0-9");
std::process::exit(1);
eprintln!(" -p, --parity N use N (2/4) parity contexts");
process::exit(1);
}