Use btrees to make everything more consistent

This commit is contained in:
Gwilym Inzani 2024-09-25 13:13:51 +01:00
parent afa9d47f0c
commit 62020e692f
4 changed files with 19 additions and 27 deletions

View file

@ -1,6 +1,6 @@
use std::{fmt, str::FromStr};
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Colour {
pub r: u8,
pub g: u8,

View file

@ -1,4 +1,4 @@
use std::{collections::HashMap, hash::BuildHasher};
use std::collections::BTreeMap;
use crate::{colour::Colour, image_loader::Image};
@ -42,7 +42,7 @@ pub struct DeduplicatedData {
pub transformation: Transformation,
}
#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
struct Tile {
data: [Colour; 64],
}
@ -99,9 +99,7 @@ pub(crate) fn deduplicate_image(input: &Image, can_flip: bool) -> (Image, Vec<De
let mut deduplication_data = vec![];
let all_tiles = Tile::split_image(input);
let mut existing_tiles = HashMap::new();
let hasher = std::collections::hash_map::RandomState::new();
let mut existing_tiles = BTreeMap::new();
for tile in all_tiles {
let (tile, transformation) = if can_flip {
@ -109,22 +107,13 @@ pub(crate) fn deduplicate_image(input: &Image, can_flip: bool) -> (Image, Vec<De
let hflipped = tile.hflipped();
let vhflipped = vflipped.hflipped();
// find the one with the smallest hash
let tile_hash = hasher.hash_one(&tile);
let vflipped_hash = hasher.hash_one(&vflipped);
let hflipped_hash = hasher.hash_one(&hflipped);
let vhflipped_hash = hasher.hash_one(&vhflipped);
let minimum = (&tile).min(&vflipped).min(&hflipped).min(&vhflipped);
let minimum = tile_hash
.min(vflipped_hash)
.min(hflipped_hash)
.min(vhflipped_hash);
if minimum == tile_hash {
if minimum == &tile {
(tile, Transformation::none())
} else if minimum == vflipped_hash {
} else if minimum == &vflipped {
(vflipped, Transformation::vflipped())
} else if minimum == hflipped_hash {
} else if minimum == &hflipped {
(hflipped, Transformation::hflipped())
} else {
(vhflipped, Transformation::vhflipped())

View file

@ -1,10 +1,13 @@
use crate::colour::Colour;
use std::{collections::HashSet, fmt};
use std::{
collections::{BTreeSet, HashSet},
fmt,
};
const MAX_COLOURS: usize = 256;
const MAX_COLOURS_PER_PALETTE: usize = 16;
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub(crate) struct Palette16 {
colours: Vec<Colour>,
}
@ -156,7 +159,7 @@ impl Palette16Optimiser {
palette.add_colour(transparent_colour);
palette
})
.collect::<HashSet<Palette16>>()
.collect::<BTreeSet<Palette16>>()
.into_iter()
.map(|palette| palette.colours)
.collect::<Vec<_>>();

View file

@ -1,4 +1,4 @@
use std::collections::HashSet;
use std::collections::BTreeSet;
use crate::{
colour::Colour,
@ -7,13 +7,13 @@ use crate::{
};
pub struct Palette256 {
colours: HashSet<Colour>,
colours: BTreeSet<Colour>,
}
impl Palette256 {
pub fn new() -> Self {
Self {
colours: HashSet::new(),
colours: BTreeSet::new(),
}
}
@ -41,8 +41,8 @@ impl Palette256 {
.cloned()
.collect();
let current_colours_set = HashSet::from_iter(optimised_palette_colours.iter().cloned());
let new_colours: HashSet<_> = self
let current_colours_set = BTreeSet::from_iter(optimised_palette_colours.iter().cloned());
let new_colours: BTreeSet<_> = self
.colours
.symmetric_difference(&current_colours_set)
.collect();