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 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..3579be7 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. * @@ -142,11 +149,13 @@ 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; - + //CS304 Issue link: https://github.com/romankh3/image-comparison/issues/190 + long maxDiff = (long) width * height; return (float) (100.0 * diff / maxDiff); } @@ -166,4 +175,22 @@ 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 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; + 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; + } } 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 898481c..9b24ef7 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,44 @@ @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() { + //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()); + } + + //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() { + //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