From b7244fb0a8f732f0c41d6482ec250e9b1fc542e5 Mon Sep 17 00:00:00 2001 From: NMazur Date: Wed, 2 Nov 2022 14:46:54 +0400 Subject: [PATCH 1/2] getDifferencePercent fix (#233) --- .../romankh3/image/comparison/ImageComparisonUtil.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java b/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java index 0231095..347ee8b 100644 --- a/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java +++ b/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java @@ -142,10 +142,11 @@ public static float getDifferencePercent(BufferedImage img1, BufferedImage img2) long diff = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { - diff += pixelDiff(img1.getRGB(x, y), img2.getRGB(x, y)); + if(pixelDiff(img1.getRGB(x, y), img2.getRGB(x, y)) > 0) + diff++; } } - long maxDiff = 3L * 255 * width * height; + long maxDiff = (long) width * height; return (float) (100.0 * diff / maxDiff); } From d7a8abca3b180d1cab80154fad546776bb6898d2 Mon Sep 17 00:00:00 2001 From: NMazur Date: Wed, 2 Nov 2022 15:33:55 +0400 Subject: [PATCH 2/2] Updated tests + added a new one --- .../image/comparison/ImageComparisonUtil.java | 2 +- .../comparison/ImageComparisonUnitTest.java | 20 ++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java b/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java index 347ee8b..2f89e5a 100644 --- a/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java +++ b/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java @@ -142,7 +142,7 @@ public static float getDifferencePercent(BufferedImage img1, BufferedImage img2) long diff = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { - if(pixelDiff(img1.getRGB(x, y), img2.getRGB(x, y)) > 0) + if(img1.getRGB(x, y) != img2.getRGB(x, y)) diff++; } } diff --git a/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java b/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java index 73b9c49..36c9acb 100644 --- a/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java +++ b/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java @@ -436,7 +436,7 @@ public void shouldProperlyHandleIssue196() { //then assertEquals(MISMATCH, imageComparisonResult.getImageComparisonState()); - assertEquals(0.4274517595767975, imageComparisonResult.getDifferencePercent()); + assertEquals(0.6673570275306702, imageComparisonResult.getDifferencePercent()); assertImagesEqual(expectedImage, imageComparisonResult.getResult()); } @@ -519,8 +519,22 @@ public void shouldProperlyCompareMisSizedImages() { //then assertEquals(SIZE_MISMATCH, imageComparisonResult.getImageComparisonState()); assertEquals(0, imageComparisonResult.getRectangles().size()); - boolean differenceLessThan2 = imageComparisonResult.getDifferencePercent() < 2; - assertTrue(differenceLessThan2); + assertTrue(imageComparisonResult.getDifferencePercent() <= 99.39796); + } + + @DisplayName("Should properly get and set percentage of differences #233") + @Test + public void shouldProperlySetPercentageOfDifferences() { + //given + ImageComparison imageComparison = new ImageComparison("expected#196.png", "actual#196.png"); + + //when + float difPercent = imageComparison.compareImages().getDifferencePercent(); + ImageComparisonResult imageComparisonResult = imageComparison.setAllowingPercentOfDifferentPixels(difPercent) + .compareImages(); + + //then + assertEquals(MATCH, imageComparisonResult.getImageComparisonState()); } private void assertImagesEqual(BufferedImage expected, BufferedImage actual) {