-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
66 lines (58 loc) · 1.69 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
pub mod coder;
use coder::qmcoder;
extern crate clap;
use clap::{App, Arg , ArgGroup};
use std::fs;
use std::io::Read;
use crate::fs::File;
fn get_file_as_byte_vec(filename: &String)->Vec<u8>{
let error_msg = "Error reading file: "
.to_string() + filename;
let mut f = File::open(&filename).expect(&error_msg);
let metadata = fs::metadata(&filename)
.expect("unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
f.read(&mut buffer).expect("buffer overflow");
buffer
}
/* fn is_binary_image(byte_stream: &Vec<u8>)->bool {
let mut is_binary = true;
for byte in byte_stream {
if byte > &1 {
is_binary = false;
break;
}
}
is_binary
} */
fn to_bit_plane(byte_stream: &Vec<u8>)->Vec<bool>{
let mut bit_planes: Vec<bool> = Vec::new();
for c in 0..8 {
for byte in byte_stream {
let bit = byte & 2_u8.pow(c);
match bit {
0 => {bit_planes.push(false);}
_ => {bit_planes.push(true);}
};
}
}
bit_planes
}
fn main() {
let matches = App::new("QM coder")
.arg(
Arg::with_name("compress")
.short("c")
.long("compress")
.value_name("FILE")
.takes_value(true),
)
.get_matches();
if let Some(file) = matches.value_of("compress"){
let byte_stream: Vec<u8> = get_file_as_byte_vec(&file.to_string());
// we do not separate gray/binary image
let bit_plane: Vec<bool> = to_bit_plane(&byte_stream);
let x = qmcoder::Encoder::new();
x.read_QT_table();
}
}