add reverse compression option

This commit is contained in:
2022-09-21 21:37:30 +02:00
parent f5fc9bd005
commit 8f33ae0b1e

View File

@@ -14,6 +14,7 @@ fn main() -> Result<()> {
let parity_contexts = args let parity_contexts = args
.opt_value_from_str(["-p", "--parity"])? .opt_value_from_str(["-p", "--parity"])?
.unwrap_or(1usize); .unwrap_or(1usize);
let reverse = args.contains(["-r", "--reverse"]);
if parity_contexts != 1 && parity_contexts != 2 && parity_contexts != 4 { if parity_contexts != 1 && parity_contexts != 2 && parity_contexts != 4 {
eprintln!("--parity has to be 1, 2 or 4"); eprintln!("--parity has to be 1, 2 or 4");
@@ -25,10 +26,13 @@ fn main() -> Result<()> {
let mut data = vec![]; let mut data = vec![];
File::open(infile)?.read_to_end(&mut data)?; File::open(infile)?.read_to_end(&mut data)?;
if reverse {
data.reverse();
}
let mut pb = pbr::ProgressBar::new(data.len() as u64); let mut pb = pbr::ProgressBar::new(data.len() as u64);
pb.set_units(pbr::Units::Bytes); pb.set_units(pbr::Units::Bytes);
let packed_data = upkr::pack( let mut packed_data = upkr::pack(
&data, &data,
level, level,
use_bitstream, use_bitstream,
@@ -39,6 +43,10 @@ fn main() -> Result<()> {
); );
pb.finish(); pb.finish();
if reverse {
packed_data.reverse();
}
println!( println!(
"Compressed {} bytes to {} bytes ({}%)", "Compressed {} bytes to {} bytes ({}%)",
data.len(), data.len(),
@@ -52,6 +60,7 @@ fn main() -> Result<()> {
let parity_contexts = args let parity_contexts = args
.opt_value_from_str(["-p", "--parity"])? .opt_value_from_str(["-p", "--parity"])?
.unwrap_or(1usize); .unwrap_or(1usize);
let reverse = args.contains(["-r", "--reverse"]);
if parity_contexts != 1 && parity_contexts != 2 && parity_contexts != 4 { if parity_contexts != 1 && parity_contexts != 2 && parity_contexts != 4 {
eprintln!("--parity has to be 1, 2 or 4"); eprintln!("--parity has to be 1, 2 or 4");
@@ -63,8 +72,14 @@ fn main() -> Result<()> {
let mut data = vec![]; let mut data = vec![];
File::open(infile)?.read_to_end(&mut data)?; File::open(infile)?.read_to_end(&mut data)?;
let packed_data = upkr::unpack(&data, use_bitstream, parity_contexts); if reverse {
File::create(outfile)?.write_all(&packed_data)?; data.reverse();
}
let mut unpacked_data = upkr::unpack(&data, use_bitstream, parity_contexts);
if reverse {
unpacked_data.reverse();
}
File::create(outfile)?.write_all(&unpacked_data)?;
} }
Some(other) => { Some(other) => {
bail!("Unknown subcommand '{}'", other); bail!("Unknown subcommand '{}'", other);
@@ -82,5 +97,6 @@ fn print_help() {
eprintln!(" -b, --bitstream bitstream mode"); eprintln!(" -b, --bitstream bitstream mode");
eprintln!(" -l, --level N compression level 0-9"); eprintln!(" -l, --level N compression level 0-9");
eprintln!(" -p, --parity N use N (2/4) parity contexts"); eprintln!(" -p, --parity N use N (2/4) parity contexts");
eprintln!(" -r, --reverse reverse input & output");
process::exit(1); process::exit(1);
} }