Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to draw rectangle with different color for issue #226 #227

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer,Integer> 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.
*
Expand Down Expand Up @@ -297,8 +312,17 @@ private List<Rectangle> populateRectangles() {
}
counter++;
}
List<Rectangle> rectangles1 = mergeRectangles(mergeRectangles(rectangles));
setRectangleColor(rectangles1);
return rectangles1;
}

return mergeRectangles(mergeRectangles(rectangles));
private void setRectangleColor(List<Rectangle> 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));
}
}

/**
Expand All @@ -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));
}
}
}
Expand Down Expand Up @@ -469,12 +494,23 @@ private void saveImageForDestination(BufferedImage image) {
* @param rectangles the collection of the {@link Rectangle}.
*/
private void draw(Graphics2D graphics, List<Rectangle> 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)
);
}
}

/**
Expand Down Expand Up @@ -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++;
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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}.
*/
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -118,6 +130,7 @@ public void setDefaultValues() {
public void makeZeroRectangle() {
this.minPoint = new Point();
this.maxPoint = new Point();
this.setDiffCount(0);
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Binary file added src/test/resources/actual#226.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/test/resources/result#226_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/test/resources/result#226_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.