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

#98: fix the bug with excluded areas. #100

Merged
merged 1 commit into from
May 31, 2019
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand All @@ -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;
}
Expand Down Expand Up @@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
* The area that will be excluded, masked, in the image.
*/
public class ExcludedAreas {
private List<Rectangle> excluded;

private final List<Rectangle> excluded;

public ExcludedAreas() {
excluded = new ArrayList<>();
Expand All @@ -26,4 +27,8 @@ public boolean contains(Point point) {

return false;
}

public List<Rectangle> getExcluded() {
return excluded;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -185,7 +205,7 @@ public void testShouldIgnoreExcludedArea() throws IOException, URISyntaxExceptio
BufferedImage image1 = readImageFromResources("b1#17.png");
BufferedImage image2 = readImageFromResources("MaskedComparison#58.png");
List<Rectangle> 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);

Expand All @@ -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<Rectangle> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Rectangle> rectangles = Arrays.asList(Rectangle.createDefault(), Rectangle.createZero());

//when
ExcludedAreas excludedAreas = new ExcludedAreas(rectangles);

//then
assertEquals(rectangles, excludedAreas.getExcluded());
}

}
Binary file added src/test/resources/b1#98.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/b2#98.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.