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 8067c0b..a41b51a 100644 --- a/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java +++ b/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java @@ -148,6 +148,21 @@ public class ImageComparison { */ private Color excludedRectangleColor = Color.GREEN; + /** + * Count different pixels in a {@link Rectangle}. + */ + private int pixelCount = 0; + + /** + * Map regin id to correct pixelCount. + */ + private Map pixelCountMap = new HashMap<>(); + + /** + * True for open draw with different color + */ + private boolean drawDifferentColorFlag = false; + /** * Create a new instance of {@link ImageComparison} that can compare the given images. * @@ -297,8 +312,17 @@ private List populateRectangles() { } counter++; } + List rectangles1 = mergeRectangles(mergeRectangles(rectangles)); + setRectangleColor(rectangles1); + return rectangles1; + } - return mergeRectangles(mergeRectangles(rectangles)); + private void setRectangleColor(List rectangles){ + for (int i = 0; i < rectangles.size(); i++) { + Rectangle rectangle = rectangles.get(i); + int gb = 255-(int) Math.round((rectangle.getDiffCount() / (double)(rectangle.getHeight() * rectangle.getWidth()) ) * 255); + rectangles.get(i).setColor(new Color(255, gb, gb)); + } } /** @@ -325,6 +349,7 @@ private Rectangle createRectangle() { for (int x = 0; x < matrix[0].length; x++) { if (matrix[y][x] == counter) { updateRectangleCreation(rectangle, x, y); + rectangle.setDiffCount(pixelCountMap.get(counter)); } } } @@ -469,12 +494,23 @@ private void saveImageForDestination(BufferedImage image) { * @param rectangles the collection of the {@link Rectangle}. */ private void draw(Graphics2D graphics, List rectangles) { - rectangles.forEach(rectangle -> graphics.drawRect( - rectangle.getMinPoint().x, - rectangle.getMinPoint().y, - rectangle.getWidth() - 1, - rectangle.getHeight() - 1) - ); + if(isDrawDifferentColorFlag()){ + for (Rectangle rectangle: rectangles ) { + graphics.setColor(rectangle.getColor()); + graphics.drawRect( + rectangle.getMinPoint().x, + rectangle.getMinPoint().y, + rectangle.getWidth() - 1, + rectangle.getHeight() - 1); + } + }else { + rectangles.forEach(rectangle -> graphics.drawRect( + rectangle.getMinPoint().x, + rectangle.getMinPoint().y, + rectangle.getWidth() - 1, + rectangle.getHeight() - 1) + ); + } } /** @@ -509,7 +545,9 @@ private void groupRegions() { for (int y = 0; y < matrix.length; y++) { for (int x = 0; x < matrix[y].length; x++) { if (matrix[y][x] == 1) { + pixelCount = 0; joinToRegion(x, y); + pixelCountMap.put(regionCount,pixelCount); regionCount++; } } @@ -530,6 +568,7 @@ private void joinToRegion(int x, int y) { } matrix[y][x] = regionCount; + pixelCount++; for (int i = 0; i < threshold; i++) { joinToRegion(x + 1 + i, y); @@ -702,4 +741,12 @@ public ImageComparison setExcludedRectangleColor(Color excludedRectangleColor) { this.excludedRectangleColor = excludedRectangleColor; return this; } + + public boolean isDrawDifferentColorFlag() { + return drawDifferentColorFlag; + } + + public void setDrawDifferentColorFlag(boolean drawDifferentColorFlag) { + this.drawDifferentColorFlag = drawDifferentColorFlag; + } } diff --git a/src/main/java/com/github/romankh3/image/comparison/model/Rectangle.java b/src/main/java/com/github/romankh3/image/comparison/model/Rectangle.java index db5d09b..021548b 100644 --- a/src/main/java/com/github/romankh3/image/comparison/model/Rectangle.java +++ b/src/main/java/com/github/romankh3/image/comparison/model/Rectangle.java @@ -3,7 +3,7 @@ import static java.lang.Integer.max; import static java.lang.Integer.min; -import java.awt.Point; +import java.awt.*; import java.util.Objects; /** @@ -21,6 +21,16 @@ public class Rectangle { */ private Point maxPoint; + /** + * Number of different pixel in current {@link Rectangle}. + */ + private int diffCount = 0; + + /** + * Color for current {@link Rectangle}. + */ + private Color color = Color.BLUE; + /** * Create empty instance of the {@link Rectangle}. */ @@ -83,10 +93,12 @@ public static Rectangle createZero() { * @return new merged {@link Rectangle}. */ public Rectangle merge(Rectangle that) { - return new Rectangle(min(this.getMinPoint().x, that.getMinPoint().x), + Rectangle rectangle = new Rectangle(min(this.getMinPoint().x, that.getMinPoint().x), min(this.getMinPoint().y, that.getMinPoint().y), max(this.getMaxPoint().x, that.getMaxPoint().x), max(this.getMaxPoint().y, that.getMaxPoint().y)); + rectangle.setDiffCount(this.getDiffCount()+that.getDiffCount()); + return rectangle; } /** @@ -118,6 +130,7 @@ public void setDefaultValues() { public void makeZeroRectangle() { this.minPoint = new Point(); this.maxPoint = new Point(); + this.setDiffCount(0); } /** @@ -175,6 +188,22 @@ public void setMaxPoint(Point maxPoint) { this.maxPoint = maxPoint; } + public int getDiffCount() { + return diffCount; + } + + public void setDiffCount(int diffCount) { + this.diffCount = diffCount; + } + + public Color getColor() { + return color; + } + + public void setColor(Color color) { + this.color = color; + } + @Override public boolean equals(Object o) { if (this == o) { 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..861decb 100644 --- a/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java +++ b/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java @@ -523,6 +523,48 @@ public void shouldProperlyCompareMisSizedImages() { assertTrue(differenceLessThan2); } + @DisplayName("Should properly work rectangle with different color") + @Test + public void shouldProperlyWorkRectangleDifferentColor() { + //given + BufferedImage expectedResultImage = readImageFromResources("result#226_1.png"); + + BufferedImage expected = readImageFromResources("expected#11.png"); + BufferedImage actual = readImageFromResources("actual#226.png"); + + ImageComparison imageComparison = new ImageComparison(expected, actual); + imageComparison.setRectangleLineWidth(5); + imageComparison.setDrawDifferentColorFlag(true); + + //when + ImageComparisonResult imageComparisonResult = imageComparison.compareImages(); + + //then + assertEquals(MISMATCH, imageComparisonResult.getImageComparisonState()); + assertImagesEqual(expectedResultImage, imageComparisonResult.getResult()); + } + + @DisplayName("Should properly work rectangle with different color function closing") + @Test + public void shouldProperlyWorkRectangleDifferentColorClose() { + //given + BufferedImage expectedResultImage = readImageFromResources("result#226_2.png"); + + BufferedImage expected = readImageFromResources("expected#11.png"); + BufferedImage actual = readImageFromResources("actual#226.png"); + + ImageComparison imageComparison = new ImageComparison(expected, actual); + imageComparison.setRectangleLineWidth(5); + imageComparison.setDrawDifferentColorFlag(false); + + //when + ImageComparisonResult imageComparisonResult = imageComparison.compareImages(); + + //then + assertEquals(MISMATCH, imageComparisonResult.getImageComparisonState()); + assertImagesEqual(expectedResultImage, imageComparisonResult.getResult()); + } + private void assertImagesEqual(BufferedImage expected, BufferedImage actual) { if (expected.getWidth() != actual.getWidth() || expected.getHeight() != actual.getHeight()) { fail("Images have different dimensions"); diff --git a/src/test/resources/actual#226.png b/src/test/resources/actual#226.png new file mode 100644 index 0000000..7279bf9 Binary files /dev/null and b/src/test/resources/actual#226.png differ diff --git a/src/test/resources/result#226_1.png b/src/test/resources/result#226_1.png new file mode 100644 index 0000000..be48245 Binary files /dev/null and b/src/test/resources/result#226_1.png differ diff --git a/src/test/resources/result#226_2.png b/src/test/resources/result#226_2.png new file mode 100644 index 0000000..4b5c5d4 Binary files /dev/null and b/src/test/resources/result#226_2.png differ