diff --git a/README.md b/README.md index 3f5fa7b..9e8a2a0 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,9 @@ This will compile, run the tests, and create a runnable jar at `${projectDir}/bu ## Release Notes +### 3.0.1 +* Fixed #98: Ignored area was not actually ignored. + ### 3.0.0 * Added ComparisonResult as a returning value for comparing. It contains: * image1 diff --git a/build.gradle b/build.gradle index 282fc5a..6000480 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ plugins { } group 'com.github.romankh3' -version '3.0.0' +version '3.0.1' description 'A library and utility to compare different images.' sourceCompatibility = 1.8 diff --git a/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java b/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java index 838eac9..ef3a0ad 100644 --- a/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java +++ b/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java @@ -151,7 +151,7 @@ public ComparisonResult compareImages() throws IOException { * * @return true if image size are not equal, false otherwise. */ - public boolean isImageSizesNotEqual(BufferedImage image1, BufferedImage image2) { + private boolean isImageSizesNotEqual(BufferedImage image1, BufferedImage image2) { return image1.getHeight() != image2.getHeight() || image1.getWidth() != image2.getWidth(); } @@ -162,7 +162,7 @@ private void populateTheMatrixOfTheDifferences() { matrix = new int[image1.getWidth()][image1.getHeight()]; for (int y = 0; y < image1.getHeight(); y++) { for (int x = 0; x < image1.getWidth(); x++) { - Point point = new Point(y, x); + Point point = new Point(x, y); if (!excludedAreas.contains(point)) { matrix[x][y] = isDifferentPixels(image1.getRGB(x, y), image2.getRGB(x, y)) ? 1 : 0; } @@ -346,7 +346,8 @@ private void joinToRegion(int row, int col) { /** * Returns the list of rectangles that would be drawn as a diff image. - * If you submit two images that are the same barring the parts you want to excludedAreas you get a list of rectangles that can be used as said excludedAreas + * If you submit two images that are the same barring the parts you want to excludedAreas you get a list of + * rectangles that can be used as said excludedAreas * * @return List of {@link Rectangle} */ diff --git a/src/main/java/com/github/romankh3/image/comparison/model/ExcludedAreas.java b/src/main/java/com/github/romankh3/image/comparison/model/ExcludedAreas.java index 7005d71..cdb3f5d 100644 --- a/src/main/java/com/github/romankh3/image/comparison/model/ExcludedAreas.java +++ b/src/main/java/com/github/romankh3/image/comparison/model/ExcludedAreas.java @@ -7,7 +7,8 @@ * The area that will be excluded, masked, in the image. */ public class ExcludedAreas { - private List excluded; + + private final List excluded; public ExcludedAreas() { excluded = new ArrayList<>(); @@ -26,4 +27,8 @@ public boolean contains(Point point) { return false; } + + public List getExcluded() { + return excluded; + } } 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 d58f689..bd29f9d 100644 --- a/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java +++ b/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java @@ -4,12 +4,15 @@ import static com.github.romankh3.image.comparison.model.ComparisonState.MATCH; import static com.github.romankh3.image.comparison.model.ComparisonState.MISMATCH; import static com.github.romankh3.image.comparison.model.ComparisonState.SIZE_MISMATCH; +import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import com.github.romankh3.image.comparison.model.ComparisonResult; import com.github.romankh3.image.comparison.model.Rectangle; import java.awt.image.BufferedImage; +import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.util.ArrayList; @@ -67,6 +70,20 @@ public void testImagesWithTotallyDifferentImages() throws IOException, URISyntax assertImagesEqual(expectedResult, comparisonResult.getResult()); } + @Test + public void testDestinationGetting() throws IOException, URISyntaxException { + //given + BufferedImage image1 = readImageFromResources("image1.png"); + BufferedImage image2 = readImageFromResources("image2.png"); + ImageComparison imageComparison = new ImageComparison(image1, image2); + + //when + imageComparison.setDestination(new File("result1.png")); + + //then + assertTrue(imageComparison.getDestination().isPresent()); + } + @Test public void testMinimalRectangleSize() throws IOException, URISyntaxException { //given @@ -138,8 +155,11 @@ public void testRectangleWithLineWidth10() throws IOException, URISyntaxExceptio //given BufferedImage expectedResultImage = readImageFromResources("resultThickRectangle.png"); + BufferedImage image1 = readImageFromResources("b1#11.png"); + BufferedImage image2 = readImageFromResources("b2#11.png"); + //when - ImageComparison imageComparison = new ImageComparison("b1#11.png", "b2#11.png"); + ImageComparison imageComparison = new ImageComparison(image1, image2, new File("build/test-images/result.png")); imageComparison.setRectangleLineWidth(10); ComparisonResult comparisonResult = imageComparison.compareImages(); @@ -185,7 +205,7 @@ public void testShouldIgnoreExcludedArea() throws IOException, URISyntaxExceptio BufferedImage image1 = readImageFromResources("b1#17.png"); BufferedImage image2 = readImageFromResources("MaskedComparison#58.png"); List excludedAreas = new ArrayList<>(); - excludedAreas.add(new Rectangle(0, 131, 224, 224)); + excludedAreas.add(new Rectangle(131, 0, 224, 224)); ImageComparison imageComparison = new ImageComparison(image1, image2); imageComparison.setExcludedAreas(excludedAreas); @@ -196,6 +216,31 @@ public void testShouldIgnoreExcludedArea() throws IOException, URISyntaxExceptio assertEquals(result.getComparisonState(), MATCH); } + /** + * Test issue #98. + */ + @Test + public void testIssue98() throws IOException, URISyntaxException { + //given + BufferedImage image1 = readImageFromResources("b1#98.png"); + BufferedImage image2 = readImageFromResources("b2#98.png"); + + List excludedAreas = asList( + new Rectangle(80, 388, 900, 514), + new Rectangle(410, 514, 900, 565), + new Rectangle(410, 636, 900, 754) + ); + + ImageComparison imageComparison = new ImageComparison(image1, image2); + imageComparison.setExcludedAreas(excludedAreas); + + //when + ComparisonResult comparisonResult = imageComparison.compareImages(); + + //then + assertEquals(MATCH, comparisonResult.getComparisonState()); + } + @Test public void testMatchSize() throws IOException, URISyntaxException { //when diff --git a/src/test/java/com/github/romankh3/image/comparison/model/ExcludedAreasUnitTest.java b/src/test/java/com/github/romankh3/image/comparison/model/ExcludedAreasUnitTest.java new file mode 100644 index 0000000..d63bb32 --- /dev/null +++ b/src/test/java/com/github/romankh3/image/comparison/model/ExcludedAreasUnitTest.java @@ -0,0 +1,36 @@ +package com.github.romankh3.image.comparison.model; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.Arrays; +import java.util.List; +import org.junit.Test; + +/** + * Unit-level testing for {@link ExcludedAreas} object. + */ +public class ExcludedAreasUnitTest { + + @Test + public void testBeanCreation() { + //when + ExcludedAreas excludedAreas = new ExcludedAreas(); + + //then + assertNotNull(excludedAreas); + } + + @Test + public void testGettingRectangleList() { + //given + List rectangles = Arrays.asList(Rectangle.createDefault(), Rectangle.createZero()); + + //when + ExcludedAreas excludedAreas = new ExcludedAreas(rectangles); + + //then + assertEquals(rectangles, excludedAreas.getExcluded()); + } + +} diff --git a/src/test/resources/b1#98.png b/src/test/resources/b1#98.png new file mode 100644 index 0000000..48c222b Binary files /dev/null and b/src/test/resources/b1#98.png differ diff --git a/src/test/resources/b2#98.png b/src/test/resources/b2#98.png new file mode 100644 index 0000000..a78e389 Binary files /dev/null and b/src/test/resources/b2#98.png differ