about summary refs log tree commit diff
path: root/gifprobe
diff options
context:
space:
mode:
authorGenny <gen@nyble.dev>2022-11-25 01:28:19 -0600
committerGenny <gen@nyble.dev>2022-11-25 01:28:19 -0600
commit3068091f9bf05a48e2f20806ca22368188b56a7a (patch)
treeb8fc116d6ee28e70324545fa79f15cbfb22fd2d1 /gifprobe
parentac8578823b9ef467dc58a6afd5f7f4adfdb4c8bd (diff)
downloadgifed-3068091f9bf05a48e2f20806ca22368188b56a7a.tar.gz
gifed-3068091f9bf05a48e2f20806ca22368188b56a7a.zip
Read now gives offsets
Diffstat (limited to 'gifprobe')
-rw-r--r--gifprobe/src/main.rs75
1 files changed, 60 insertions, 15 deletions
diff --git a/gifprobe/src/main.rs b/gifprobe/src/main.rs
index 241c421..2f5c025 100644
--- a/gifprobe/src/main.rs
+++ b/gifprobe/src/main.rs
@@ -1,9 +1,11 @@
+use std::ops::Range;
+
 use gifed::{
 	block::{
 		Block::{self},
-		IndexedImage,
+		CompressedImage,
 	},
-	reader::GifReader,
+	reader::Decoder,
 };
 use owo_colors::OwoColorize;
 
@@ -15,36 +17,49 @@ fn main() {
 		return;
 	};
 
-	let gif = GifReader::file(&file).unwrap();
+	let decoder = Decoder::file(&file).unwrap();
+	let mut reader = decoder.read().unwrap();
 
-	println!("Version {}", gif.header.yellow());
+	println!("Version {}", reader.version.yellow());
 	println!(
 		"Logical Screen Descriptor\n\tDimensions {}x{}",
-		gif.screen_descriptor.width.yellow(),
-		gif.screen_descriptor.height.yellow()
+		reader.screen_descriptor.width.yellow(),
+		reader.screen_descriptor.height.yellow()
 	);
 
-	if gif.screen_descriptor.has_color_table() {
+	if reader.screen_descriptor.has_color_table() {
 		println!(
 			"\tGlobal Color Table Present {}\n\tGlobal Color Table Size {}",
 			"Yes".green(),
-			gif.screen_descriptor.color_table_len().green()
+			reader.screen_descriptor.color_table_len().green()
 		);
 	} else {
 		println!(
 			"\tGlobal Color Table Present {}\n\tGlobal Color Table Size {}",
 			"No".red(),
-			gif.screen_descriptor.color_table_len().red()
+			reader.screen_descriptor.color_table_len().red()
 		);
 	}
 
 	let mut img_count = 0;
 	let mut hundreths: usize = 0;
 
-	for block in gif.blocks {
+	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::IndexedImage(img) => {
-				describe_image(&img);
+			Block::CompressedImage(img) => {
+				describe_image(&img, offset);
 				img_count += 1;
 			}
 			Block::GraphicControlExtension(gce) => {
@@ -57,8 +72,11 @@ fn main() {
 					format!("Reserved: {:b}", gce.packed().disposal_method())
 				};
 
+				print!("Graphic Control Extension");
+				print_offset(offset);
+
 				println!(
-					"Graphic Control Extension\n\tDelay Time {}\n\tDispose {}",
+					"\tDelay Time {}\n\tDispose {}",
 					format!("{}s", gce.delay() as f32 / 100.0).yellow(),
 					dispose_string.yellow()
 				)
@@ -120,9 +138,12 @@ fn main() {
 	);
 }
 
-fn describe_image(bli: &IndexedImage) {
+fn describe_image(bli: &CompressedImage, offset: Range<usize>) {
+	print!("Image");
+	print_offset(offset);
+
 	println!(
-		"Image\n\tOffset {}x{}\n\tDimensions {}x{}",
+		"\tOffset {}x{}\n\tDimensions {}x{}",
 		bli.image_descriptor.left.yellow(),
 		bli.image_descriptor.top.yellow(),
 		bli.image_descriptor.width.yellow(),
@@ -143,3 +164,27 @@ fn describe_image(bli: &IndexedImage) {
 		);
 	}
 }
+
+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());
+	}
+}