Skip to content

Commit 7c37c47

Browse files
authored
Merge pull request #72 from firefly-zero/img-usage
inspect: Count color usage for images
2 parents fdee880 + e5fbebe commit 7c37c47

1 file changed

Lines changed: 31 additions & 8 deletions

File tree

src/commands/inspect.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,15 @@ struct ImageStats {
240240
bpp: u8,
241241
width: u16,
242242
height: u16,
243-
swaps: Vec<Option<u8>>,
243+
swaps: Vec<Swap>,
244244
pixels: usize,
245245
}
246246

247+
struct Swap {
248+
color: Option<u8>,
249+
uses: u32,
250+
}
251+
247252
fn inspect_images(rom_path: &Path) -> anyhow::Result<Vec<ImageStats>> {
248253
let dir = fs::read_dir(rom_path)?;
249254
let mut stats = Vec::new();
@@ -274,7 +279,7 @@ fn inspect_image(path: &Path) -> Option<ImageStats> {
274279
_ => 8,
275280
};
276281
let max_colors = match bpp {
277-
1 => 1,
282+
1 => 2,
278283
2 => 4,
279284
_ => 16,
280285
};
@@ -290,8 +295,19 @@ fn inspect_image(path: &Path) -> Option<ImageStats> {
290295
let pixels = image_bytes.len() * ppb;
291296
#[expect(clippy::cast_possible_truncation)]
292297
let height = pixels as u16 / width;
298+
293299
let swaps = parse_swaps(transp, swaps);
294-
let swaps = swaps[..max_colors].to_vec();
300+
let mut swaps: Vec<_> = swaps
301+
.map(|color| Swap { color, uses: 0 })
302+
.into_iter()
303+
.collect();
304+
for byte in image_bytes {
305+
let c1 = usize::from(byte & 0xF);
306+
let c2 = usize::from((byte >> 4) & 0xF);
307+
swaps[c1].uses += 1;
308+
swaps[c2].uses += 1;
309+
}
310+
swaps.truncate(max_colors);
295311

296312
let name = path.file_name()?;
297313
let name: String = name.to_str()?.to_string();
@@ -454,12 +470,19 @@ fn print_image_stats(stats: ImageStats) {
454470
println!(" {}: {}", "pixels".cyan(), stats.pixels);
455471
println!(" {}", "colors".cyan());
456472
for (i, swap) in stats.swaps.into_iter().enumerate() {
457-
if let Some(swap) = swap {
458-
let name = get_color_name(swap);
459-
let swap = swap + 1;
460-
println!(" {i:>2} -> {swap:>2} {name}");
473+
let usage: String = if swap.uses == 0 {
474+
"unused".blue().to_string()
475+
} else if swap.uses < 10 {
476+
format!("{:>6}", swap.uses).yellow().to_string()
477+
} else {
478+
format!("{:>6}", swap.uses)
479+
};
480+
if let Some(color) = swap.color {
481+
let name = get_color_name(color);
482+
let color = color + 1;
483+
println!(" {i:>2} -> {color:>2} {name} {usage}");
461484
} else {
462-
println!(" {i:>2} -> 0 transparent");
485+
println!(" {i:>2} -> 0 transparent {usage}");
463486
}
464487
}
465488
}

0 commit comments

Comments
 (0)