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

Implement the comparison of images ignore translation for #217 #222

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 @@ -702,4 +702,64 @@ public ImageComparison setExcludedRectangleColor(Color excludedRectangleColor) {
this.excludedRectangleColor = excludedRectangleColor;
return this;
}


/**
* compare images while ignore their position in background
* @param dest is the result destination
* @return the result of the drawing.
*/
public ImageComparisonResult compareImagesIgnoreBackground(File dest) {
int exceptedBgd = getBackgroundRGB(expected);
int actualBgd = getBackgroundRGB(actual);
BufferedImage expectedInterest = getInterestArea(expected, exceptedBgd);
BufferedImage actualInterest = getInterestArea(actual, actualBgd);
ImageComparison imageComparison = new ImageComparison(expectedInterest, actualInterest, dest);
return imageComparison.compareImages();
}

private int getBackgroundRGB(BufferedImage image) {
// count RGB
Map<Integer, Integer> countMap = new HashMap<>();
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
if (!excludedAreas.contains(new Point(x, y))) {
int key = image.getRGB(x, y);
if (countMap.containsKey(key)) {
countMap.put(key, countMap.get(key) + 1);
} else {
countMap.put(key, 1);
}
}
}
}
// get background
int backGround = Integer.MIN_VALUE;
int maxCount = Integer.MIN_VALUE;
for (Map.Entry<Integer, Integer> entry : countMap.entrySet()) {
if (entry.getValue() > maxCount) {
maxCount = entry.getValue();
backGround = entry.getKey();
}
}
return backGround;
}

private BufferedImage getInterestArea(BufferedImage image, int background) {
Point leftTop = new Point(image.getWidth(), image.getHeight());
Point rightBottom = new Point(0, 0);
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
if (!excludedAreas.contains(new Point(x, y))) {
if (image.getRGB(x, y) != background){
leftTop = new Point(Math.min(x, leftTop.x), Math.min(y, leftTop.y));
rightBottom = new Point(Math.max(x, rightBottom.x), Math.max(y, rightBottom.y));
}
}
}
}
return image.getSubimage(leftTop.x, leftTop.y,
rightBottom.x - leftTop.x, rightBottom.y - leftTop.y);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,63 @@ public void shouldProperlyCompareMisSizedImages() {
assertTrue(differenceLessThan2);
}

@DisplayName("Should properly compare while ignore white background for #217")
@Test
public void shouldProperlyHandelIssue217() {
//given
BufferedImage expected = readImageFromResources("expected#217a.png");
BufferedImage actual = readImageFromResources("actual#217a.png");
BufferedImage expectedResult = readImageFromResources("result#217a.png");

//when
ImageComparisonResult imageComparisonResult =
new ImageComparison(expected, actual)
.setPixelToleranceLevel(0.0)
.compareImagesIgnoreBackground(null);

//then
assertEquals(MISMATCH, imageComparisonResult.getImageComparisonState());
assertImagesEqual(expectedResult, imageComparisonResult.getResult());
}


@DisplayName("Should properly compare while ignore white background for #217")
@Test
public void shouldProperlyCompareIgnoreBackground() {
//given
BufferedImage expected = readImageFromResources("expected#217b.png");
BufferedImage actual = readImageFromResources("actual#217b.png");

//when
ImageComparisonResult imageComparisonResult =
new ImageComparison(expected, actual)
.setPixelToleranceLevel(0.0)
.compareImagesIgnoreBackground(null);

//then
assertEquals(MATCH, imageComparisonResult.getImageComparisonState());
}

@DisplayName("Should properly compare difference while ignore background")
@Test
public void shouldProperlyCompareDifferenceAndIgnoreBackground() {
//given
BufferedImage expected = readImageFromResources("expected#217c.png");
BufferedImage actual = readImageFromResources("actual#217c.png");
BufferedImage expectedResult = readImageFromResources("result#217c.png");

//when
ImageComparisonResult imageComparisonResult =
new ImageComparison(expected, actual)
.setPixelToleranceLevel(0.0)
.compareImagesIgnoreBackground(null);

//then
assertEquals(MISMATCH, imageComparisonResult.getImageComparisonState());
assertImagesEqual(expectedResult, 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
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void shouldProperlyWorkInBug136() {
@Test
public void shouldProperlyHandleBug180FromRoot() {
//given
String imagePath = "build/resources/test/result.png";
String imagePath = "build/test-images/result.png";

//when
BufferedImage result = ImageComparisonUtil.readImageFromResources(imagePath);
Expand Down
Binary file added src/test/resources/actual#217a.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/actual#217b.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/actual#217c.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/expected#217a.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/expected#217b.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/expected#217c.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#217a.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#217c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.