craft-texture: compare images by value whether or not the buffer is shared

Image equality no longer short-cuts on a shared buffer. An image holding
NaN compared equal to its clone but unequal to the same image built
again; it is now unequal to both, as f32 comparison gives.
This commit is contained in:
dotCooCoo committed 2026-11-13 19:55:39 -08:00
1 parent 316db97052
commit 47baffee9d
1 file changed
+15 -1
+15 -1
View File
@@ -134,9 +134,11 @@ pub struct Image {
data: Arc<Vec<f32>>,
}
/// Images are equal when their size, kind and floats are equal. Floats compare as `f32` does, so
/// an image holding NaN is unequal to every image, its clones included.
impl PartialEq for Image {
fn eq(&self, o: &Image) -> bool {
self.w == o.w && self.h == o.h && self.kind == o.kind && (Arc::ptr_eq(&self.data, &o.data) || self.data == o.data)
self.w == o.w && self.h == o.h && self.kind == o.kind && self.data == o.data
}
}
@@ -739,6 +741,18 @@ mod tests {
}
}
#[test]
fn equality_does_not_depend_on_buffer_sharing() {
let img = Image::filled(2, 1, Kind::Gray, [f32::NAN; 4]);
let rebuilt = Image::filled(2, 1, Kind::Gray, [f32::NAN; 4]);
// NaN pixels compare unequal as floats do, whether or not the buffer is shared.
assert_ne!(img, rebuilt);
assert_ne!(img, img.clone());
let finite = Image::filled(2, 1, Kind::Gray, [0.5; 4]);
assert_eq!(finite, finite.clone());
assert_eq!(finite, Image::filled(2, 1, Kind::Gray, [0.5; 4]));
}
#[test]
fn box_shrink_averages_large_hdr_values() {
let img = Image::filled(2, 2, Kind::Color, [f32::MAX, -f32::MAX, 1.0, 1.0]);