Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/hist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,13 @@ impl Histogram {

let new_size = (self.hashmap.len() / 3).max(self.hashmap.capacity() / 5);
let old_hashmap = mem::replace(&mut self.hashmap, HashMap::with_capacity_and_hasher(new_size, U32Hasher(0)));
self.hashmap.extend(old_hashmap.into_iter().map(move |(k, v)| {
// Re-insert in key order. Several old keys can collapse to one posterized
// key and the last one inserted wins, so the result must not depend on the
// old map's iteration order, which varies with the hash table's bucket
// layout (hashbrown's probe-group width differs between x86_64 and aarch64).
let mut entries: Vec<(u32, (u32, RGBA))> = old_hashmap.into_iter().collect();
entries.sort_unstable_by_key(|&(k, _)| k);
self.hashmap.extend(entries.into_iter().map(move |(k, v)| {
(k & new_posterize_mask, v)
}));
}
Expand Down Expand Up @@ -301,6 +307,11 @@ impl Histogram {
let weight = boost as f32;
TempHistItem { color, weight, cluster_index }
}));
// Everything after this point is order-sensitive (cluster bucketing by
// position, the f64 weight sum, median cut's tie-breaking, k-means' chunks),
// and a HashMap's iteration order depends on the hash table's bucket layout,
// which differs between architectures. Fix the order so the result does not.
temp.sort_unstable_by_key(|t| (t.color.r, t.color.g, t.color.b, t.color.a, t.cluster_index));

let mut clusters = [Cluster { begin: 0, end: 0 }; LIQ_MAXCLUSTER];
let mut next_begin = 0;
Expand Down
10 changes: 4 additions & 6 deletions src/pal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,10 @@ impl f_pixel {
let mut max_r = [0.; 4];
vst1q_f32(max_r.as_mut_ptr(), max);

let mut max_gb = [0.; 4];
vst1q_f32(max_gb.as_mut_ptr(), vpaddq_f32(max, max));

// add rgb, not a

max_r[1] + max_gb[1]
// add rgb, not a. Same association as the other paths, (r + g) + b:
// a pairwise add gives r + (g + b), and float addition is not associative,
// so the distance metric (and with it the palette) differed by architecture.
max_r[1] + max_r[2] + max_r[3]
}
}

Expand Down