From b61caab1b199f0a092efdbeb7f95b386298fb2e2 Mon Sep 17 00:00:00 2001 From: Bon-Bon <1157264534@qq.com> Date: Tue, 24 May 2022 14:55:04 +0800 Subject: [PATCH 1/5] Add functions for issue#190 --- .../image/comparison/ImageComparisonUtil.java | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 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..87542f9 100644 --- a/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java +++ b/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java @@ -142,11 +142,12 @@ 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(isDiffPixel(img1.getRGB(x, y), img2.getRGB(x, y))) { + diff += 1; + } } } - long maxDiff = 3L * 255 * width * height; - + long maxDiff = (long) width * height; return (float) (100.0 * diff / maxDiff); } @@ -166,4 +167,25 @@ public static int pixelDiff(int rgb1, int rgb2) { int b2 = rgb2 & 0xff; return Math.abs(r1 - r2) + Math.abs(g1 - g2) + Math.abs(b1 - b2); } + + + /** + * Compare two pixels + * + * @param rgb1 the first rgb + * @param rgb2 the second rgn + * @return true If they are the same RGB pixel. + */ + public static boolean isDiffPixel(int rgb1, int rgb2) { + int r1 = (rgb1 >> 16) & 0xff; + int g1 = (rgb1 >> 8) & 0xff; + int b1 = rgb1 & 0xff; + int r2 = (rgb2 >> 16) & 0xff; + int g2 = (rgb2 >> 8) & 0xff; + int b2 = rgb2 & 0xff; + if(Math.abs(r1 - r2) + Math.abs(g1 - g2) + Math.abs(b1 - b2) == 0) { + return false; + } + return true; + } } From 44596b02eb9916d2fe39e49197d2e3a1e5debb6a Mon Sep 17 00:00:00 2001 From: Bon-Bon <1157264534@qq.com> Date: Tue, 24 May 2022 14:56:07 +0800 Subject: [PATCH 2/5] add tests for issue#190 --- .../comparison/ImageComparisonUnitTest.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) 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..0cc65e8 100644 --- a/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java +++ b/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java @@ -24,6 +24,42 @@ @DisplayName("Unit-level testing for {@link ImageComparison} object.") public class ImageComparisonUnitTest { + @DisplayName("For issue#190, test the Percentage of Different Pixels of different images") + @Test + public void testDifferencePercent1() { + //given + BufferedImage expectedResultImage = readImageFromResources("result.png"); + File file = new File("build/test-images/result.png"); + float actualPercent = (float) (100.0 * 695 / (985 * 701)); + //when + ImageComparison imageComparison = new ImageComparison("expected.png", "actual.png"); + ImageComparisonResult imageComparisonResult = imageComparison.compareImages().writeResultTo(file); + + //then + assertNotNull(imageComparison.getActual()); + assertNotNull(imageComparison.getExpected()); + assertEquals(MISMATCH, imageComparisonResult.getImageComparisonState()); + assertEquals(actualPercent, imageComparisonResult.getDifferencePercent()); + } + + @DisplayName("For issue#190, test the Percentage of Different Pixels of the same images") + @Test + public void testDifferencePercent2() { + //given + BufferedImage expectedResultImage = readImageFromResources("result.png"); + File file = new File("build/test-images/result.png"); + float actualPercent = 0.0f; + //when + ImageComparison imageComparison = new ImageComparison("actual.png", "actual.png"); + ImageComparisonResult imageComparisonResult = imageComparison.compareImages().writeResultTo(file); + + //then + assertNotNull(imageComparison.getActual()); + assertNotNull(imageComparison.getExpected()); + assertEquals(MATCH, imageComparisonResult.getImageComparisonState()); + assertEquals(actualPercent, imageComparisonResult.getDifferencePercent()); + } + @DisplayName("The most important test. Shown, that the changes in algorithm, " + "don't break the main behaviour and result as expected") @Test From cb5a99e61dfb923f083eb9b7074601bf2a14dcd7 Mon Sep 17 00:00:00 2001 From: Bon-Bon <1157264534@qq.com> Date: Sun, 29 May 2022 19:41:11 +0800 Subject: [PATCH 3/5] Update pom.xml --- pom.xml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pom.xml b/pom.xml index 77de9ef..8e15629 100644 --- a/pom.xml +++ b/pom.xml @@ -68,4 +68,34 @@ + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.1.2 + + + + checkstyle + + + + + + + org.apache.maven.plugins + maven-pmd-plugin + 3.16.0 + + + /rulesets/java/quickstart.xml + + true + + + + \ No newline at end of file From a10e0548e4796d7e00ec5cec75e10564b8937943 Mon Sep 17 00:00:00 2001 From: Bon-Bon <1157264534@qq.com> Date: Sun, 29 May 2022 19:51:54 +0800 Subject: [PATCH 4/5] Update ImageComparisonUtil.java --- .../image/comparison/ImageComparisonUtil.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 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 87542f9..8aa819d 100644 --- a/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java +++ b/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java @@ -16,11 +16,18 @@ import javax.imageio.ImageIO; import javax.swing.ImageIcon; + + + /** * Tools for the {@link ImageComparison} object. */ public final class ImageComparisonUtil { + public static int OFFSET = 0xff; + public static int EIGHT = 8; + public static int TENSIX = 8; + /** * Make a copy of the {@link BufferedImage} object. * @@ -168,24 +175,20 @@ public static int pixelDiff(int rgb1, int rgb2) { return Math.abs(r1 - r2) + Math.abs(g1 - g2) + Math.abs(b1 - b2); } - /** - * Compare two pixels + * Compare two pixels. * * @param rgb1 the first rgb - * @param rgb2 the second rgn + * @param rgb2 the second rgb * @return true If they are the same RGB pixel. */ - public static boolean isDiffPixel(int rgb1, int rgb2) { - int r1 = (rgb1 >> 16) & 0xff; - int g1 = (rgb1 >> 8) & 0xff; - int b1 = rgb1 & 0xff; - int r2 = (rgb2 >> 16) & 0xff; - int g2 = (rgb2 >> 8) & 0xff; - int b2 = rgb2 & 0xff; - if(Math.abs(r1 - r2) + Math.abs(g1 - g2) + Math.abs(b1 - b2) == 0) { - return false; - } - return true; + public static boolean isDiffPixel(final int rgb1, final int rgb2) { + int r1 = (rgb1 >> TENSIX) & OFFSET; + int g1 = (rgb1 >> EIGHT) & OFFSET; + int b1 = rgb1 & OFFSET; + int r2 = (rgb2 >> TENSIX) & OFFSET; + int g2 = (rgb2 >> EIGHT) & OFFSET; + int b2 = rgb2 & OFFSET; + return Math.abs(r1 - r2) + Math.abs(g1 - g2) + Math.abs(b1 - b2) != 0; } } From 89e24bc02913c7d2792cf11f111434b146acff42 Mon Sep 17 00:00:00 2001 From: scp-WFZ <1157264534@qq.com> Date: Sun, 29 May 2022 21:27:19 +0800 Subject: [PATCH 5/5] Add issue comment //CS304 Issue link: https://github.com/romankh3/image-comparison/issues/190 --- .../github/romankh3/image/comparison/ImageComparisonUtil.java | 2 ++ .../romankh3/image/comparison/ImageComparisonUnitTest.java | 2 ++ 2 files changed, 4 insertions(+) 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 8aa819d..3579be7 100644 --- a/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java +++ b/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java @@ -154,6 +154,7 @@ public static float getDifferencePercent(BufferedImage img1, BufferedImage img2) } } } + //CS304 Issue link: https://github.com/romankh3/image-comparison/issues/190 long maxDiff = (long) width * height; return (float) (100.0 * diff / maxDiff); } @@ -182,6 +183,7 @@ public static int pixelDiff(int rgb1, int rgb2) { * @param rgb2 the second rgb * @return true If they are the same RGB pixel. */ + //CS304 Issue link: https://github.com/romankh3/image-comparison/issues/190 public static boolean isDiffPixel(final int rgb1, final int rgb2) { int r1 = (rgb1 >> TENSIX) & OFFSET; int g1 = (rgb1 >> EIGHT) & OFFSET; 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 0cc65e8..fd32faa 100644 --- a/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java +++ b/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java @@ -24,6 +24,7 @@ @DisplayName("Unit-level testing for {@link ImageComparison} object.") public class ImageComparisonUnitTest { + //CS304 (manually written) Issue link: https://github.com/romankh3/image-comparison/issues/190 @DisplayName("For issue#190, test the Percentage of Different Pixels of different images") @Test public void testDifferencePercent1() { @@ -42,6 +43,7 @@ public void testDifferencePercent1() { assertEquals(actualPercent, imageComparisonResult.getDifferencePercent()); } + //CS304 (manually written) Issue link: https://github.com/romankh3/image-comparison/issues/190 @DisplayName("For issue#190, test the Percentage of Different Pixels of the same images") @Test public void testDifferencePercent2() {