Skip to content
Merged
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
33 changes: 10 additions & 23 deletions datatypes/src/operations/image/to_png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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,
);
}

Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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);
}
}
58 changes: 58 additions & 0 deletions datatypes/src/util/image.rs
Original file line number Diff line number Diff line change
@@ -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<u8> = 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<u8> = 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();
}
}
2 changes: 2 additions & 0 deletions datatypes/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};

Expand Down
8 changes: 3 additions & 5 deletions operators/src/util/raster_stream_to_png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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);
}
}
39 changes: 10 additions & 29 deletions services/src/api/handlers/wms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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")]
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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]
Expand Down