diff --git a/.travis.yml b/.travis.yml index c297b4a..605775c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,4 +9,4 @@ script: ./gradlew clean build jacocoTestReport jdk: oraclejdk8 after_success: -- ./gradlew jacocoTestReport coveralls \ No newline at end of file + - ./gradlew jacocoTestReport coveralls \ No newline at end of file diff --git a/build.gradle b/build.gradle index bb3a2b0..b77965f 100644 --- a/build.gradle +++ b/build.gradle @@ -43,12 +43,12 @@ defaultTasks << 'build' /* Publishing config */ -task javadocJar( type: Jar ) { +task javadocJar(type: Jar) { classifier = 'javadoc' from javadoc } -task sourcesJar( type: Jar ) { +task sourcesJar(type: Jar) { classifier = 'sources' from sourceSets.main.allSource } @@ -58,7 +58,7 @@ artifacts { } // add all the info required by Maven Central to the pom -configure( install.repositories.mavenInstaller ) { +configure(install.repositories.mavenInstaller) { pom.project { inceptionYear '2019' name project.name @@ -91,19 +91,19 @@ configure( install.repositories.mavenInstaller ) { } def getProjectProperty = { String propertyName -> - project.properties[ propertyName ] + project.properties[propertyName] } bintray { user = getProjectProperty "bintrayUserName" key = getProjectProperty "bintrayApiKey" - configurations = [ 'archives' ] + configurations = ['archives'] publish = true pkg { repo = 'maven' name = 'image-comparison' - licenses = [ 'unlicense' ] - labels = [ 'java', 'image', 'image-comparison' ] + licenses = ['unlicense'] + labels = ['java', 'image', 'image-comparison'] publicDownloadNumbers = true //noinspection GroovyAssignabilityCheck diff --git a/src/main/java/ua/comparison/image/ImageComparison.java b/src/main/java/ua/comparison/image/ImageComparison.java index 236424f..7bf040e 100644 --- a/src/main/java/ua/comparison/image/ImageComparison.java +++ b/src/main/java/ua/comparison/image/ImageComparison.java @@ -25,21 +25,18 @@ public class ImageComparison { /** - * The threshold which means the max distance between non-equal pixels. - * Could be changed according size and requirements to the image. + * Prefix of the name of the result image. */ - public static int threshold = 3; - + private static final String NAME_PREFIX = "image-comparison"; /** - * The number which marks how many rectangles. Beginning from 2. + * Suffix of the name of of the result image. */ - private int counter = 2; - + private static final String NAME_SUFFIX = ".png"; /** - * The number of the marking specific rectangle. + * The threshold which means the max distance between non-equal pixels. + * Could be changed according size and requirements to the image. */ - private int regionCount = counter; - + public static int threshold = 5; /** * First image for comparing */ @@ -51,18 +48,15 @@ public class ImageComparison { private final BufferedImage image2; private final /* @Nullable */ File destination; - - private int[][] matrix; - /** - * Prefix of the name of the result image. + * The number which marks how many rectangles. Beginning from 2. */ - private static final String NAME_PREFIX = "image-comparison"; - + private int counter = 2; /** - * Suffix of the name of of the result image. + * The number of the marking specific rectangle. */ - private static final String NAME_SUFFIX = ".png"; + private int regionCount = counter; + private int[][] matrix; ImageComparison(String image1, String image2) throws IOException, URISyntaxException { this(readImageFromResources(image1), readImageFromResources(image2), null); @@ -141,7 +135,7 @@ private void groupRegions() { for (int row = 0; row < matrix.length; row++) { for (int col = 0; col < matrix[row].length; col++) { if (matrix[row][col] == 1) { - joinToRegion(row, col, threshold); + joinToRegion(row, col); regionCount++; } } @@ -156,23 +150,20 @@ private void groupRegions() { * @param row the value of the row. * @param col the value of the column. */ - private void joinToRegion(int row, int col, int localThreshold) { + private void joinToRegion(int row, int col) { if (row < 0 || row >= matrix.length || col < 0 || col >= matrix[row].length || matrix[row][col] != 1) { return; } matrix[row][col] = regionCount; - for (int i = 0; i < localThreshold; i++) { -// joinToRegion(row - 1 - i, col, 1); - joinToRegion(row + 1 + i, col, threshold); -// joinToRegion(row, col - 1 - i, 1); - joinToRegion(row, col + 1 + i, threshold); + for (int i = 0; i < threshold; i++) { + joinToRegion(row + 1 + i, col); + joinToRegion(row, col + 1 + i); -// joinToRegion(row - 1 - i, col - 1 - i, 1); - joinToRegion(row + 1 + i, col - 1 - i, threshold); - joinToRegion(row - 1 - i, col + 1 + i, threshold); - joinToRegion(row + 1 + i, col + 1 + i, threshold); + joinToRegion(row + 1 + i, col - 1 - i); + joinToRegion(row - 1 - i, col + 1 + i); + joinToRegion(row + 1 + i, col + 1 + i); } } diff --git a/src/main/java/ua/comparison/image/model/Rectangle.java b/src/main/java/ua/comparison/image/model/Rectangle.java index 364269f..9595e79 100644 --- a/src/main/java/ua/comparison/image/model/Rectangle.java +++ b/src/main/java/ua/comparison/image/model/Rectangle.java @@ -25,10 +25,10 @@ public static Rectangle createDefault() { } public void merge(Rectangle that) { - if(this.minX <= that.minX && this.minY <= that.minY && this.maxY >= that.maxY && this.maxX >= that.maxX) { + if (this.minX <= that.minX && this.minY <= that.minY && this.maxY >= that.maxY && this.maxX >= that.maxX) { that.setDefaultValues(); } - if(that.minX <= this.minX && that.minY <= this.minY && that.maxY >= this.maxY && that.maxX >= this.maxX) { + if (that.minX <= this.minX && that.minY <= this.minY && that.maxY >= this.maxY && that.maxX >= this.maxX) { this.setDefaultValues(); } } @@ -55,33 +55,38 @@ public void setDefaultValues() { this.minX = Integer.MAX_VALUE; } + public int getMinX() { + return minX; + } public void setMinX(int minX) { this.minX = minX; } + public int getMinY() { + return minY; + } + public void setMinY(int minY) { this.minY = minY; } + public int getMaxX() { + return maxX; + } + public void setMaxX(int maxX) { this.maxX = maxX; } - public void setMaxY(int maxY) { - this.maxY = maxY; + public int getMaxY() { + return maxY; } - public int getMinX() { - return minX; + public void setMaxY(int maxY) { + this.maxY = maxY; } - public int getMinY() { return minY; } - - public int getMaxX() { return maxX; } - - public int getMaxY() { return maxY; } - public int getWidth() { return maxY - minY; } diff --git a/src/test/java/ua/comparison/image/ImageComparisonUnitTest.java b/src/test/java/ua/comparison/image/ImageComparisonUnitTest.java index 222a2b3..078c130 100644 --- a/src/test/java/ua/comparison/image/ImageComparisonUnitTest.java +++ b/src/test/java/ua/comparison/image/ImageComparisonUnitTest.java @@ -13,7 +13,6 @@ import java.net.URISyntaxException; import java.nio.file.Files; import java.util.concurrent.atomic.AtomicBoolean; -import org.junit.Ignore; import org.junit.Test; @@ -22,6 +21,23 @@ */ public class ImageComparisonUnitTest { + private static void assertImagesEqual(BufferedImage imgA, BufferedImage imgB) { + if (imgA.getWidth() != imgB.getWidth() || imgA.getHeight() != imgB.getHeight()) { + fail("Images have different dimensions"); + } + + int width = imgA.getWidth(); + int height = imgA.getHeight(); + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + if (imgA.getRGB(x, y) != imgB.getRGB(x, y)) { + fail("Images are different, found different pixel at: x = " + x + ", y = " + y); + } + } + } + } + /** * The most important test. Shown, that the changes in algorithm, * don't break the main behaviour and result as expected. @@ -136,21 +152,4 @@ public void resultIsHandledCorrectlyWhenItShouldSaveToFile() throws IOException, assertFalse(showUI.get()); } - private static void assertImagesEqual(BufferedImage imgA, BufferedImage imgB) { - if (imgA.getWidth() != imgB.getWidth() || imgA.getHeight() != imgB.getHeight()) { - fail("Images have different dimensions"); - } - - int width = imgA.getWidth(); - int height = imgA.getHeight(); - - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - if (imgA.getRGB(x, y) != imgB.getRGB(x, y)) { - fail("Images are different, found different pixel at: x = " + x + ", y = " + y); - } - } - } - } - } diff --git a/src/test/java/ua/comparison/image/model/RectangleUnitTest.java b/src/test/java/ua/comparison/image/model/RectangleUnitTest.java index f771ae6..639d82d 100644 --- a/src/test/java/ua/comparison/image/model/RectangleUnitTest.java +++ b/src/test/java/ua/comparison/image/model/RectangleUnitTest.java @@ -13,24 +13,24 @@ public class RectangleUnitTest { public void testDefaultRectangle() { Rectangle rectangle = Rectangle.createDefault(); - assertEquals( rectangle.getMinX(), Integer.MAX_VALUE ); - assertEquals( rectangle.getMinY(), Integer.MAX_VALUE ); - assertEquals( rectangle.getMaxX(), Integer.MIN_VALUE ); - assertEquals( rectangle.getMaxY(), Integer.MIN_VALUE ); + assertEquals(rectangle.getMinX(), Integer.MAX_VALUE); + assertEquals(rectangle.getMinY(), Integer.MAX_VALUE); + assertEquals(rectangle.getMaxX(), Integer.MIN_VALUE); + assertEquals(rectangle.getMaxY(), Integer.MIN_VALUE); } @Test public void testGetterSetter() { Rectangle rectangle = Rectangle.createDefault(); - rectangle.setMinX( 10 ); - rectangle.setMinY( 20 ); - rectangle.setMaxX( 30 ); - rectangle.setMaxY( 40 ); + rectangle.setMinX(10); + rectangle.setMinY(20); + rectangle.setMaxX(30); + rectangle.setMaxY(40); - assertEquals( rectangle.getMinX(), 10 ); - assertEquals( rectangle.getMinY(), 20 ); - assertEquals( rectangle.getMaxX(), 30 ); - assertEquals( rectangle.getMaxY(), 40 ); + assertEquals(rectangle.getMinX(), 10); + assertEquals(rectangle.getMinY(), 20); + assertEquals(rectangle.getMaxX(), 30); + assertEquals(rectangle.getMaxY(), 40); } }