about summary refs log tree commit diff
path: root/gifcheck/src/main.rs
blob: 8eec4c15b4a91752eb25999db45f5bf01e22f55f (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
use std::{ops::Deref, path::PathBuf};

use gifed::{reader::Decoder, Gif};

mod fix;

fn main() {
	let file = std::env::args().nth(1).unwrap();
	let arg = std::env::args().nth(2).map(|cmd| cmd.to_lowercase());

	let gif = Decoder::file(&file).unwrap().read_all().unwrap();

	let plt_report = same_palette(&gif);
	match plt_report {
		PaletteReport {
			has_local: true,
			local_redundant: true,
			local_matching_indicies,
		} => {
			if local_matching_indicies {
				println!("!!! LOCPLT_NORE. This could've been a global palette");
			} else {
				println!("!!  LOCPLT. This gif can be reindexed and have a global palette");
			}
		}
		PaletteReport {
			has_local: true,
			local_redundant: false,
			..
		} => {
			println!("    gif has local palettes and they differ");
		}
		PaletteReport {
			has_local: false, ..
		} => {
			println!("    gif only has a global palette");
		}
	}

	if arg.as_deref() == Some("fix") {
		if let Some(fix_gif) = fix::palette_errors(&gif, plt_report) {
			if !fix::images_match_exactly(&gif, &fix_gif) {
				panic!("fixed images did not exactly match, this is a hard error")
			}

			println!("--- fixing, writing!");
			let mut path = PathBuf::from(file);
			path.set_file_name(format!(
				"{}_fix",
				path.file_stem().unwrap().to_string_lossy()
			));

			fix_gif.save(path).unwrap();
		}
	}
}

pub struct PaletteReport {
	// Does the gif even contain local color tables?
	has_local: bool,
	// ... do those color tables always contain the same colors?
	local_redundant: bool,
	// ... and do those colors all have matching inidices, making it possible
	// to simply set the global palette and remove the locals?
	local_matching_indicies: bool,
}

fn same_palette(gif: &Gif) -> PaletteReport {
	let mut palette = gif.global_color_table.as_ref();
	let mut report = PaletteReport {
		has_local: false,
		local_redundant: true,
		local_matching_indicies: true,
	};

	for img in gif.images() {
		if let Some(local_palette) = img.compressed.palette() {
			report.has_local = true;

			match palette {
				None => palette = Some(local_palette),
				Some(known_palette) => {
					if !local_palette.eq(known_palette) {
						// Are the palletes equal, even?
						report.local_redundant = false;
						report.local_matching_indicies = false;
						return report;
					} else if report.local_matching_indicies {
						// it's matching, but are the indicies the same?
						for known_color in known_palette.deref() {
							for local_color in local_palette.deref() {
								if known_color != local_color {
									report.local_matching_indicies = false;
								}
							}
						}
					}
				}
			}
		}
	}

	report
}