Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image simiarity test #3008

Merged
merged 8 commits into from
Aug 15, 2023
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
/.env

/CI.asc

**/actual*.png
**/diff*.png
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ tests: build-builder
mkdir --parent core/build/reports/
mkdir --parent core/build/resources/
mkdir --parent core/build/scripts/
mkdir --parent examples/build/resources/test/
docker run --rm --user=$(shell id -u):$(shell id -g) \
--volume=$(PWD)/core/src/:/src/core/src/:ro \
--volume=$(PWD)/core/build/reports/:/src/core/build/reports/ \
--volume=$(PWD)/core/build/resources/:/src/core/build/resources/ \
--volume=$(PWD)/core/build/scripts/:/src/core/build/scripts/ \
--volume=$(PWD)/core/src/test/:/src/core/src/test/:ro \
--volume=$(PWD)/core/src/test/:/src/core/src/test/ \
mapfish_print_builder \
gradle --parallel --exclude-task=:core:spotbugsMain --exclude-task=:core:checkstyleMain --exclude-task=:core:violations \
--exclude-task=:core:spotbugsTest --exclude-task=:core:checkstyleTest \
Expand Down
45 changes: 33 additions & 12 deletions core/src/main/java/org/mapfish/print/test/util/ImageSimilarity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public final class ImageSimilarity {
private static final Logger LOGGER = LoggerFactory.getLogger(ImageSimilarity.class);

private static final boolean GENERATE_IN_SOURCE = true;
public static final boolean REGENERATE_EXPECTED_IMAGES = false;

private final BufferedImage expectedImage;
private final BufferedImage maskImage;
Expand All @@ -48,13 +48,14 @@ public final class ImageSimilarity {
/** The constructor, which creates the GUI and start the image processing task. */
public ImageSimilarity(final File expectedFile) throws IOException {
this.expectedImage = expectedFile.exists() ? ImageIO.read(expectedFile) : null;
if (GENERATE_IN_SOURCE) {
if (REGENERATE_EXPECTED_IMAGES) {
this.expectedPath =
new File(
expectedFile
.toString()
.replace("/out/", "/src/")
.replace("/build/classes/test/", "/src/test/resources/"));
.replace("/build/classes/test/", "/src/test/resources/")
.replace("/src/core/build/resources/test/", "/src/core/src/test/resources/"));
} else {
this.expectedPath = expectedFile;
}
Expand Down Expand Up @@ -95,6 +96,7 @@ public ImageSimilarity(final File expectedFile) throws IOException {
*
* @param image image to write
* @param file path and file name (extension will be ignored and changed to tiff.
* @throws IOException if the image could not be written.
*/
private static void writeUncompressedImage(BufferedImage image, String file) throws IOException {
try {
Expand Down Expand Up @@ -215,7 +217,7 @@ private File getRelatedFile(final String name) {
*
* @return a number between 0 and 10000 or Double.MAX_VALUE on images format error.
*/
private double calcDistance(final BufferedImage actual) {
double calcDistance(final BufferedImage actual) {
// There are several ways to calculate distances between two vectors,
// we will calculate the sum of the distances between the RGB values of
// pixels in the same positions.
Expand Down Expand Up @@ -257,10 +259,7 @@ private double calcDistance(final BufferedImage actual) {
double squareDist = 0.0;
for (int i = 0; i < this.expectedImage.getSampleModel().getNumBands(); i++) {
double colorDist = (expectedPixel[i] - actualPixel[i]) * (maskPixel[0] / 255.0);
if (colorDist
> 7.0) { // allow a small color change (JPEG compression, anti-aliasing, ...)
squareDist += colorDist * colorDist;
}
squareDist += colorDist * colorDist;
}
double pxDiff =
Math.sqrt(squareDist) / Math.sqrt(this.expectedImage.getSampleModel().getNumBands());
Expand All @@ -281,6 +280,7 @@ private double calcDistance(final BufferedImage actual) {
* distance.
*
* @param actual the image to compare to "this" image.
* @throws IOException if the image could not be written.
*/
public void assertSimilarity(final File actual) throws IOException {
assertSimilarity(actual, 1);
Expand All @@ -291,6 +291,7 @@ public void assertSimilarity(final File actual) throws IOException {
* distance.
*
* @param maxDistance the maximum distance between the two images.
* @throws IOException if the image could not be written.
*/
public void assertSimilarity(final byte[] graphicData, final double maxDistance)
throws IOException {
Expand All @@ -305,6 +306,7 @@ public void assertSimilarity(final byte[] graphicData, final double maxDistance)
* @param width the graphic width (required for svg files)
* @param height the graphic height (required for svg files)
* @param maxDistance the maximum distance between the two images.
* @throws IOException if the image could not be written.
*/
public void assertSimilarity(
final List<URI> graphicFiles, final int width, final int height, final double maxDistance)
Expand All @@ -317,6 +319,7 @@ public void assertSimilarity(
* distance.
*
* @param maxDistance the maximum distance between the two images.
* @throws IOException if the image could not be written.
*/
public void assertSimilarity(
final URI svgFile, final int width, final int height, final double maxDistance)
Expand All @@ -329,6 +332,7 @@ public void assertSimilarity(
* distance.
*
* @param maxDistance the maximum distance between the two images.
* @throws IOException if the image could not be written.
*/
public void assertSimilarity(
final JasperPrint jasperPrint, final Integer page, final double maxDistance)
Expand All @@ -342,25 +346,42 @@ public void assertSimilarity(
*
* @param actualFile the file to compare to "this" image.
* @param maxDistance the maximum distance between the two images.
* @throws IOException if the image could not be written.
*/
public void assertSimilarity(final File actualFile, final double maxDistance) throws IOException {
assertSimilarity(ImageIO.read(actualFile), maxDistance);
}

/**
* Check that the actual image and the image calculated by this object are within a relay small
* distance.
*
* @param actualImage the image to compare to "this" image.
* @throws IOException if the image could not be written.
*/
public void assertSimilarity(final BufferedImage actualImage) throws IOException {
assertSimilarity(actualImage, 1);
}

/**
* Check that the actual image and the image calculated by this object are within the given
* distance.
*
* @param actualImage the image to compare to "this" image.
* @param maxDistance the maximum distance between the two images.
* @throws IOException if the image could not be written.
*/
public void assertSimilarity(final BufferedImage actualImage, final double maxDistance)
throws IOException {
if (!this.expectedPath.exists()) {
if (REGENERATE_EXPECTED_IMAGES || !this.expectedPath.exists()) {
ImageIO.write(actualImage, "png", expectedPath);
throw new AssertionError(
"The expected file was missing and has been generated: "
+ expectedPath.getAbsolutePath());
if (REGENERATE_EXPECTED_IMAGES) {
return;
sebr72 marked this conversation as resolved.
Show resolved Hide resolved
} else {
throw new AssertionError(
"The expected file was missing and has been generated: "
+ expectedPath.getAbsolutePath());
}
}
final double distance = calcDistance(actualImage);
if (distance > maxDistance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public abstract class AbstractMapfishSpringTest {
public static File getFile(Class<?> testClass, String fileName) {
final URL resource = testClass.getResource(fileName);
if (resource == null) {
throw new AssertionError("Unable to find test resource: " + fileName);
throw new AssertionError(
"Unable to find test resource: " + fileName + ", on: " + testClass.getName());
}

return new File(resource.getFile());
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/org/mapfish/print/cli/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public void testNewAPI() throws Exception {
};
Main.runMain(args);

new ImageSimilarity(getFile("expectedV3Image.png")).assertSimilarity(this.outputFile, 10);
new ImageSimilarity(getFile("expectedV3Image.png")).assertSimilarity(this.outputFile, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testPrint() throws Exception {

// note that we are using a sample size of 50, because the image is quite big.
// otherwise small differences are not detected!
new ImageSimilarity(getFile(BASE_DIR + "expectedReport.png")).assertSimilarity(print, 0, 5);
new ImageSimilarity(getFile(BASE_DIR + "expectedReport.png")).assertSimilarity(print, 0, 0);
}

@Test(expected = Test.None.class /* no exception expected */)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ public void testRenderTable() throws Exception {

assertEquals(1, print.getPages().size());

new ImageSimilarity(getFile(BASE_DIR + "expected-page.png")).assertSimilarity(print, 0, 15);
new ImageSimilarity(getFile(BASE_DIR + "expected-page.png")).assertSimilarity(print, 0, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void testDefaultDynamicTableProperties() throws Exception {

// note that we are using a sample size of 50, because the image is quite big.
// otherwise small differences are not detected!
new ImageSimilarity(getFile(baseDir + "expectedImage.png")).assertSimilarity(print, 0, 10);
new ImageSimilarity(getFile(baseDir + "expectedImage.png")).assertSimilarity(print, 0, 0);
}

@Test
Expand Down Expand Up @@ -100,7 +100,7 @@ public void testBasicTablePrint() throws Exception {

// note that we are using a sample size of 50, because the image is quite big.
// otherwise small differences are not detected!
new ImageSimilarity(getFile(baseDir + "expectedImage.png")).assertSimilarity(print, 0, 10);
new ImageSimilarity(getFile(baseDir + "expectedImage.png")).assertSimilarity(print, 0, 0);
}

@Test
Expand All @@ -117,7 +117,7 @@ public void testDynamicTablePrint() throws Exception {

// note that we are using a sample size of 50, because the image is quite big.
// otherwise small differences are not detected!
new ImageSimilarity(getFile(baseDir + "expectedImage.png")).assertSimilarity(print, 0, 10);
new ImageSimilarity(getFile(baseDir + "expectedImage.png")).assertSimilarity(print, 0, 0);
}

@Test
Expand All @@ -138,7 +138,7 @@ public void testColumnImageConverter() throws Exception {

// note that we are using a sample size of 50, because the image is quite big.
// otherwise small differences are not detected!
new ImageSimilarity(getFile(baseDir + "expectedImage.png")).assertSimilarity(print, 0, 5);
new ImageSimilarity(getFile(baseDir + "expectedImage.png")).assertSimilarity(print, 0, 0);
}

@Test
Expand All @@ -160,7 +160,7 @@ public void testTableConverters() throws Exception {

// note that we are using a sample size of 50, because the image is quite big.
// otherwise small differences are not detected!
new ImageSimilarity(getFile(baseDir + "expectedImage.png")).assertSimilarity(print, 0, 10);
new ImageSimilarity(getFile(baseDir + "expectedImage.png")).assertSimilarity(print, 0, 0);
}

@Test
Expand All @@ -177,6 +177,6 @@ public void testTableConvertersDynamic() throws Exception {

// note that we are using a sample size of 50, because the image is quite big.
// otherwise small differences are not detected!
new ImageSimilarity(getFile(baseDir + "expectedImage.png")).assertSimilarity(print, 0, 10);
new ImageSimilarity(getFile(baseDir + "expectedImage.png")).assertSimilarity(print, 0, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public void testExecute() throws Exception {
this.forkJoinPool.submit(template.getProcessorGraph().createTask(values));
taskFuture.get();

assertImage(values, 1, "layerGraphics", "expectedSimpleImage.png", 630, 294, 40);
assertImage(values, 1, "overviewMapLayerGraphics", "expectedOverviewImage.png", 320, 200, 10);
assertImage(values, 1, "layerGraphics", "expectedSimpleImage.png", 630, 294, 0);
assertImage(values, 1, "overviewMapLayerGraphics", "expectedOverviewImage.png", 320, 200, 0);
}

private void assertImage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public void testExecute() throws Exception {
this.forkJoinPool.submit(template.getProcessorGraph().createTask(values));
taskFuture.get();

assertImage(values, 1, "layerGraphics", "expectedSimpleImage.png", 630, 294, 100);
assertImage(values, 1, "overviewMapLayerGraphics", "expectedOverviewImage.png", 300, 200, 25);
assertImage(values, 1, "layerGraphics", "expectedSimpleImage.png", 630, 294, 0);
assertImage(values, 1, "overviewMapLayerGraphics", "expectedOverviewImage.png", 300, 200, 0);
}

private void assertImage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,47 +57,47 @@ public void testExecute() throws Exception {

final AbstractJasperReportOutputFormat format =
(AbstractJasperReportOutputFormat) this.outputFormat.get("pngOutputFormat");
testPrint(config, requestData, "default-aoi", format, 40);
testPrint(config, requestData, "default-aoi", format, 0);

getAreaOfInterest(requestData).put("display", "CLIP");
testPrint(config, requestData, "clip-full-aoi", format, 30);
testPrint(config, requestData, "clip-full-aoi", format, 0);
getAreaOfInterest(requestData).remove("display");

requestData = loadJsonRequestData();

getPagingAttributes(requestData).put("aoiDisplay", "clip");
testPrint(config, requestData, "clip-page-aoi", format, 40);
testPrint(config, requestData, "clip-page-aoi", format, 0);

getPagingAttributes(requestData).put("aoiDisplay", "render");
testPrint(config, requestData, "default-aoi", format, 40);
testPrint(config, requestData, "default-aoi", format, 0);

getPagingAttributes(requestData).put("aoiDisplay", "none");
testPrint(config, requestData, "none-aoi", format, 40);
testPrint(config, requestData, "none-aoi", format, 0);

getAreaOfInterest(requestData).put("display", "CLIP");
getPagingAttributes(requestData).put("aoiDisplay", "RENDER");
testPrint(config, requestData, "full-clip-sub-render", format, 40);
testPrint(config, requestData, "full-clip-sub-render", format, 0);

getAreaOfInterest(requestData).put("display", "CLIP");
getPagingAttributes(requestData).put("aoiDisplay", "NONE");
testPrint(config, requestData, "full-clip-sub-none", format, 40);
testPrint(config, requestData, "full-clip-sub-none", format, 0);

getAreaOfInterest(requestData).put("display", "CLIP");
getPagingAttributes(requestData).put("aoiDisplay", "NONE");
testPrint(config, requestData, "full-clip-sub-none", format, 40);
testPrint(config, requestData, "full-clip-sub-none", format, 0);

getAreaOfInterest(requestData).put("display", "NONE");
getPagingAttributes(requestData).put("aoiDisplay", "NONE");
testPrint(config, requestData, "all-none", format, 40);
testPrint(config, requestData, "all-none", format, 0);

getAreaOfInterest(requestData).put("display", "NONE");
getPagingAttributes(requestData).put("aoiDisplay", "NONE");
getMapAttributes(requestData).put("dpi", 254);
testPrint(config, requestData, "higher-dpi", format, 40);
testPrint(config, requestData, "higher-dpi", format, 3);

config = configurationFactory.getConfig(getFile(BASE_DIR + "config-scalebar.yaml"));
requestData = loadJsonRequestData();
testPrint(config, requestData, "scalebar", format, 45);
testPrint(config, requestData, "scalebar", format, 0);
}

private JSONObject getAreaOfInterest(PJsonObject requestData) throws JSONException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ public void testExecute() throws Exception {
assertEquals(2, layerGraphics.size());

new ImageSimilarity(getFile(BASE_DIR + "/expectedSimpleImage-no-bounds.png"))
.assertSimilarity(layerGraphics, 630, 294, 100);
.assertSimilarity(layerGraphics, 630, 294, 0);
}
}
Loading