diff --git a/datatypes/src/operations/image/to_png.rs b/datatypes/src/operations/image/to_png.rs index 6fdb134e0..83b617355 100644 --- a/datatypes/src/operations/image/to_png.rs +++ b/datatypes/src/operations/image/to_png.rs @@ -272,6 +272,8 @@ mod tests { use super::*; use crate::operations::image::RgbaColor; use crate::raster::GridIndexAccessMut; + use crate::test_data; + use crate::util::assert_image_equals; use std::convert::TryInto; #[test] @@ -298,10 +300,7 @@ mod tests { // crate::util::test::save_test_bytes(&image_bytes, "linear_gradient.png"); - assert_eq!( - include_bytes!("../../../../test_data/colorizer/linear_gradient.png") as &[u8], - image_bytes.as_slice() - ); + assert_image_equals(test_data!("colorizer/linear_gradient.png"), &image_bytes); } #[test] @@ -328,9 +327,9 @@ mod tests { // crate::util::test::save_test_bytes(&image_bytes, "logarithmic_gradient.png"); - assert_eq!( - include_bytes!("../../../../test_data/colorizer/logarithmic_gradient.png") as &[u8], - image_bytes.as_slice() + assert_image_equals( + test_data!("colorizer/logarithmic_gradient.png"), + &image_bytes, ); } @@ -359,10 +358,7 @@ mod tests { // crate::util::test::save_test_bytes(&image_bytes, "palette.png"); - assert_eq!( - include_bytes!("../../../../test_data/colorizer/palette.png") as &[u8], - image_bytes.as_slice() - ); + assert_image_equals(test_data!("colorizer/palette.png"), &image_bytes); } #[test] @@ -378,10 +374,7 @@ mod tests { // crate::util::test::save_test_bytes(&image_bytes, "rgba.png"); - assert_eq!( - include_bytes!("../../../../test_data/colorizer/rgba.png") as &[u8], - image_bytes.as_slice() - ); + assert_image_equals(test_data!("colorizer/rgba.png"), &image_bytes); } #[test] @@ -409,10 +402,7 @@ mod tests { // crate::util::test::save_test_bytes(&image_bytes, "no_data_2.png"); - assert_eq!( - include_bytes!("../../../../test_data/colorizer/no_data.png") as &[u8], - image_bytes.as_slice() - ); + assert_image_equals(test_data!("colorizer/no_data.png"), &image_bytes); } #[test] @@ -436,9 +426,6 @@ mod tests { // crate::util::test::save_test_bytes(&image_bytes, "empty.png"); - assert_eq!( - include_bytes!("../../../../test_data/colorizer/empty.png") as &[u8], - image_bytes.as_slice() - ); + assert_image_equals(test_data!("colorizer/empty.png"), &image_bytes); } } diff --git a/datatypes/src/util/image.rs b/datatypes/src/util/image.rs new file mode 100644 index 000000000..881cfd9a5 --- /dev/null +++ b/datatypes/src/util/image.rs @@ -0,0 +1,58 @@ +use std::path::Path; + +/// Compare two images +/// +/// # Panics +/// - if the `expected` image cannot be loaded +/// - if the `found` bytes cannot be loaded as an image +/// - if the images differ +/// +pub fn assert_image_equals(expected: &Path, found: &[u8]) { + let left_buf = std::fs::read(expected).expect("Failed to read `expected` path"); + let left = + image::load_from_memory(&left_buf).expect("Failed to make image from `expected` path"); + let right = image::load_from_memory(found).expect("Failed to make image from `found` bytes"); + + assert_eq!(left, right, "Images differ: {expected:?}"); +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Cursor; + + #[test] + fn it_asserts_images() { + const PIXELS_PER_AXIS: u32 = 8; + + let tmp_file = tempfile::NamedTempFile::new().unwrap(); + + let mut img = image::RgbaImage::new(PIXELS_PER_AXIS, PIXELS_PER_AXIS); + image::save_buffer_with_format( + tmp_file.path(), + &img, + PIXELS_PER_AXIS, + PIXELS_PER_AXIS, + image::ExtendedColorType::Rgba8, + image::ImageFormat::Png, + ) + .unwrap(); + + let mut bytes: Vec = Vec::new(); + img.write_to(&mut Cursor::new(&mut bytes), image::ImageFormat::Png) + .unwrap(); + + assert_image_equals(tmp_file.path(), &bytes); + + img.put_pixel(0, 0, image::Rgba([0, 1, 2, 3])); + + let mut bytes: Vec = Vec::new(); + img.write_to(&mut Cursor::new(&mut bytes), image::ImageFormat::Png) + .unwrap(); + + std::panic::catch_unwind(|| { + assert_image_equals(tmp_file.path(), &bytes); + }) + .unwrap_err(); + } +} diff --git a/datatypes/src/util/mod.rs b/datatypes/src/util/mod.rs index d6a0348ef..737e1359d 100644 --- a/datatypes/src/util/mod.rs +++ b/datatypes/src/util/mod.rs @@ -5,6 +5,7 @@ mod db_types; pub mod gdal; pub mod helpers; pub mod identifiers; +mod image; pub mod ranges; mod result; pub mod test; @@ -14,6 +15,7 @@ pub use self::identifiers::Identifier; pub use any::{AsAny, AsAnyArc}; pub use byte_size::ByteSize; pub use db_types::{HashMapTextTextDbType, NotNanF64, StringPair, TextTextKeyValue}; +pub use image::assert_image_equals; pub use result::Result; use std::path::{Path, PathBuf}; diff --git a/operators/src/util/raster_stream_to_png.rs b/operators/src/util/raster_stream_to_png.rs index af0f9874d..a8a9478b8 100644 --- a/operators/src/util/raster_stream_to_png.rs +++ b/operators/src/util/raster_stream_to_png.rs @@ -354,7 +354,8 @@ mod tests { use geoengine_datatypes::{ primitives::{BandSelection, Coordinate2D, SpatialPartition2D, SpatialResolution}, raster::{RasterDataType, TilingSpecification}, - util::test::TestDefault, + test_data, + util::{assert_image_equals, test::TestDefault}, }; use crate::{ @@ -420,9 +421,6 @@ mod tests { // geoengine_datatypes::util::test::save_test_bytes(&image_bytes, "png_from_stream.png"); - assert_eq!( - include_bytes!("../../../test_data/raster/png/png_from_stream.png") as &[u8], - image_bytes.as_slice() - ); + assert_image_equals(test_data!("raster/png/png_from_stream.png"), &image_bytes); } } diff --git a/services/src/api/handlers/wms.rs b/services/src/api/handlers/wms.rs index 623807a3b..8999d8837 100644 --- a/services/src/api/handlers/wms.rs +++ b/services/src/api/handlers/wms.rs @@ -492,6 +492,8 @@ mod tests { use geoengine_datatypes::operations::image::{Colorizer, RgbaColor}; use geoengine_datatypes::primitives::CacheTtlSeconds; use geoengine_datatypes::raster::{GridShape2D, RasterDataType, TilingSpecification}; + use geoengine_datatypes::test_data; + use geoengine_datatypes::util::assert_image_equals; use geoengine_operators::engine::{ ExecutionContext, RasterQueryProcessor, RasterResultDescriptor, }; @@ -657,12 +659,9 @@ mod tests { .await .unwrap(); - // geoengine_datatypes::util::test::save_test_bytes(&image_bytes, "raster_small.png"); + // geoengine_datatypes::util::test::save_test_bytes(&image_bytes, test_data!("wms/raster_small.png")); - assert_eq!( - include_bytes!("../../../../test_data/wms/raster_small.png") as &[u8], - image_bytes.as_slice() - ); + assert_image_equals(test_data!("wms/raster_small.png"), &image_bytes); } /// override the pixel size since this test was designed for 600 x 600 pixel tiles @@ -714,10 +713,7 @@ mod tests { // geoengine_datatypes::util::test::save_test_bytes(&image_bytes, "get_map.png"); - assert_eq!( - include_bytes!("../../../../test_data/wms/get_map.png") as &[u8], - image_bytes - ); + assert_image_equals(test_data!("wms/get_map.png"), &image_bytes); } #[ge_context::test] @@ -741,10 +737,7 @@ mod tests { // geoengine_datatypes::util::test::save_test_bytes(&image_bytes, "get_map_ndvi.png"); - assert_eq!( - include_bytes!("../../../../test_data/wms/get_map_ndvi.png") as &[u8], - image_bytes - ); + assert_image_equals(test_data!("wms/get_map_ndvi.png"), &image_bytes); } ///Actix uses serde_urlencoded inside web::Query which does not support this @@ -765,10 +758,7 @@ mod tests { // geoengine_datatypes::util::test::save_test_bytes(&image_bytes, "get_map.png"); - assert_eq!( - include_bytes!("../../../../test_data/wms/get_map.png") as &[u8], - image_bytes - ); + assert_image_equals(test_data!("wms/get_map.png"), &image_bytes); } #[ge_context::test(tiling_spec = "get_map_test_helper_tiling_spec")] @@ -856,10 +846,7 @@ mod tests { // geoengine_datatypes::util::test::save_test_bytes(&image_bytes, "get_map_colorizer.png"); - assert_eq!( - include_bytes!("../../../../test_data/wms/get_map_colorizer.png") as &[u8], - image_bytes - ); + assert_image_equals(test_data!("wms/get_map_colorizer.png"), &image_bytes); } #[ge_context::test(tiling_spec = "get_map_test_helper_tiling_spec")] @@ -921,10 +908,7 @@ mod tests { // geoengine_datatypes::util::test::save_test_bytes(&image_bytes, "ne2_rgb_colorizer.png"); - assert_eq!( - include_bytes!("../../../../test_data/wms/ne2_rgb_colorizer.png") as &[u8], - image_bytes - ); + assert_image_equals(test_data!("wms/ne2_rgb_colorizer.png"), &image_bytes); } #[ge_context::test(tiling_spec = "get_map_test_helper_tiling_spec")] @@ -993,10 +977,7 @@ mod tests { // .unwrap(), // ); - assert_eq!( - include_bytes!("../../../../test_data/wms/ne2_rgb_colorizer_gray.png") as &[u8], - image_bytes - ); + assert_image_equals(test_data!("wms/ne2_rgb_colorizer_gray.png"), &image_bytes); } #[ge_context::test]