about summary refs log tree commit diff
path: root/gifprobe/src/main.rs
blob: 0a4c7ebb21cbfc89a2394d2e719728743464c31c (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use std::ops::Range;

use gifed::{
	block::{
		Block::{self},
		CompressedImage,
	},
	reader::Decoder,
};
use owo_colors::OwoColorize;

fn main() {
	let file = if let Some(file) = std::env::args().skip(1).next() {
		file
	} else {
		println!("usage: gifprobe file.gif");
		return;
	};

	let expand = match std::env::args().nth(2).as_deref() {
		Some("expand") => true,
		None => false,
		Some(str) => {
			eprintln!("{str} is not recognised. Did you mean 'expand'?");
			return;
		}
	};

	let decoder = Decoder::file(&file).unwrap();
	let mut reader = decoder.read().unwrap();

	println!("Version {}", reader.version.yellow());
	println!(
		"Logical Screen Descriptor\n\tDimensions {}x{}",
		reader.screen_descriptor.width.yellow(),
		reader.screen_descriptor.height.yellow()
	);

	if reader.screen_descriptor.has_color_table() {
		println!(
			"\tGlobal Color Table Present {}\n\tGlobal Color Table Size {}",
			"Yes".green(),
			reader.screen_descriptor.color_table_len().green()
		);
	} else {
		println!(
			"\tGlobal Color Table Present {}\n\tGlobal Color Table Size {}",
			"No".red(),
			reader.screen_descriptor.color_table_len().red()
		);
	}

	let mut img_count = 0;
	let mut hundreths: usize = 0;

	loop {
		let block = match reader.block() {
			Ok(Some(block)) => block,
			Ok(None) => break,
			Err(e) => {
				eprintln!("error reading file: {e}");
				std::process::exit(-1);
			}
		};

		let offset = block.offset;
		let block = block.block;

		match block {
			Block::CompressedImage(img) => {
				describe_image(&img, offset);
				img_count += 1;
			}
			Block::GraphicControlExtension(gce) => {
				hundreths += gce.delay() as usize;

				let dispose_string = if let Some(dispose) = gce.disposal_method() {
					dispose.to_string()
				} else {
					format!("Reserved: {:b}", gce.packed().disposal_method())
				};

				print!("Graphic Control Extension");
				print_offset(offset);

				println!(
					"\tDelay Time {}\n\tDispose {}",
					format!("{}s", gce.delay() as f32 / 100.0).yellow(),
					dispose_string.yellow()
				)
			}
			Block::LoopingExtension(_) => todo!(),
			Block::CommentExtension(cmt) => {
				print!("Comment Extension");
				print_offset(offset);

				println!("\tLength {}", cmt.len().yellow());

				match String::from_utf8(cmt.clone()) {
					Ok(cmt) => println!("\tString \"{}\"", cmt.yellow()),
					Err(_) => {
						if !expand {
							println!("\tString {}", "Content is not utf8".red())
						} else {
							let lossy = String::from_utf8_lossy(&cmt);
							if lossy.len() > 0 {
								println!("\tString (lossy) \"{}\"", lossy.yellow())
							} else {
								println!(
									"\tString (lossy) \"{}\"",
									"Could not parse as UTF8 Lossy".red()
								);
							}
						}
					}
				}
			}
			Block::ApplicationExtension(app) => {
				let auth = app.authentication_code();
				let app_ident = String::from_utf8_lossy(app.identifier());

				print!("Application Extension");
				print_offset(offset);

				println!(
					"\tIdentifier {}\n\tAuthentication {:02X} {:02X} {:02X}",
					app_ident.yellow(),
					auth[0].yellow(),
					auth[1].yellow(),
					auth[2].yellow()
				);

				if app_ident == "NETSCAPE" {
					let data = app.data();
					let looping = u16::from_le_bytes([data[0], data[1]]);

					if looping == 0 {
						println!("\tLoop {}", "forever".yellow())
					} else {
						println!("\tLoop {}", looping.yellow());
					}
				} else {
					let data = app.data();

					match String::from_utf8(data.to_vec()) {
						Ok(s) => {
							println!(
								"\tData {}",
								format!("Valid UTF-8, {} bytes", s.len()).yellow()
							);

							if expand {
								println!("\tString \"{}\"", s.yellow());
							}
						}
						Err(_e) => println!(
							"\tData {}",
							format!("Invalid UTF-8, {} bytes", data.len()).yellow()
						),
					}
				}
			}
		}
	}

	println!(
		"{} is {}.{}s long and has {} frames",
		file,
		hundreths / 100,
		hundreths % 100,
		img_count
	);
}

fn describe_image(bli: &CompressedImage, offset: Range<usize>) {
	print!("Image");
	print_offset(offset);

	println!(
		"\tOffset {}x{}\n\tDimensions {}x{}",
		bli.image_descriptor.left.yellow(),
		bli.image_descriptor.top.yellow(),
		bli.image_descriptor.width.yellow(),
		bli.image_descriptor.height.yellow(),
	);

	if bli.image_descriptor.has_color_table() {
		println!(
			"\tLocal Color Table Present {}\n\tLocal Color Table Size {}",
			"Yes".green(),
			bli.image_descriptor.color_table_size().green()
		);
	} else {
		println!(
			"\tLocal Color Table Present {}\n\tLocal Color Table Size {}",
			"No".red(),
			bli.image_descriptor.color_table_size().red()
		);
	}
}

fn print_offset(offset: Range<usize>) {
	print!(" [");
	print_usize(offset.start);
	print!(" … ");
	print_usize(offset.end);
	println!("] ");
}

fn print_usize(offset: usize) {
	let bytes = offset.to_le_bytes();
	let mut seen_nonzero = false;
	for byte in bytes {
		if byte == 0 {
			if seen_nonzero {
				break;
			}
		} else {
			seen_nonzero = true;
		}

		print!("{:02X}", byte.cyan());
	}
}