about summary refs log tree commit diff
path: root/src/main.rs
blob: 5a57e4da7e16df9042bfd8f6b491adf3cedaadc8 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::fs::File;

use lri_rs::Message;

// This code is going to be rough. Just trying to parse this using the technique
// I know: just play with the raw data
fn main() {
	let fname = std::env::args().nth(1).unwrap();
	let data = std::fs::read(fname).unwrap();

	println!("Read {:.2}MB", data.len() as f32 / (1024.0 * 1024.0));

	let header = LightHeader::new(&data[0..32]);
	let data = &data[32..];

	let LightHeader {
		magic_number,
		combined_length,
		header_length,
		message_length,
		kind,
		reserved,
	} = header;

	//AHHH it does not seem the combined legth or header length are correct? it seems like nonsense?
	//drat. we'll know when I try and parse the message I think I extracted. 1510 bytes seems too
	//small almost.
	//the thing that makes me suspicious, and think it's right, is that the reserved are all 0x00
	//and then the next byte is data, so.
	println!("\nMagic: {magic_number}\nCombined Length: {combined_length}\nHeader Length: {header_length}\nMessage Length: {message_length}\nKind: {kind}\nReserved: {reserved:?}\nNext 8 Bytes: {:?}", &data[0..8]);

	let message = &data[..message_length as usize];
	//let data = &data[message_length as usize..];

	//let asdf = lri_rs::LightHeader::parse_from_bytes(&data).unwrap();
	//println!("{:?}", asdf.get_image_time_stamp());

	println!("\nTook Message of {message_length} bytes");
	println!("{} bytes left", data.len());

	let rt = (data.len() as f32 / 2.0).sqrt().floor() as usize;
	println!("{} png", rt * rt);

	println!("{:?}", &data[0..32]);

	let width = rt / 8;
	let height = rt * 4;
	let pixels = width * height;

	let file = File::create("ahh.png").unwrap();
	let mut enc = png::Encoder::new(file, width as u32, height as u32);
	enc.set_color(png::ColorType::Grayscale);
	enc.set_depth(png::BitDepth::Sixteen);
	let mut writer = enc.write_header().unwrap();
	writer
		.write_image_data(&data[rt * 3..(rt * 3) + pixels * 2])
		.unwrap();
}

struct LightHeader {
	magic_number: String,
	combined_length: u64,
	header_length: u64,
	message_length: u32,
	// type
	kind: u8,
	reserved: [u8; 7],
}

impl LightHeader {
	pub fn new(data: &[u8]) -> Self {
		let magic_number = String::from_utf8(data[0..4].to_vec()).unwrap();
		let combined_length = u64::from_le_bytes(data[4..12].try_into().unwrap());
		println!("Combined Length: {:?}", &data[4..12]);

		let header_length = u64::from_le_bytes(data[12..20].try_into().unwrap());
		println!("Header Length: {:?}", &data[12..20]);

		let message_length = u32::from_le_bytes(data[20..24].try_into().unwrap());
		println!("Message Length: {:?}", &data[20..24]);

		let kind = data[24];
		let reserved = data[25..32].try_into().unwrap();

		LightHeader {
			magic_number,
			combined_length,
			header_length,
			message_length,
			kind,
			reserved,
		}
	}
}