Skip to content

Commit

Permalink
Fix on bug #134 - Fixing getting size for diff rectangle (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
grigaman authored and romankh3 committed Nov 4, 2019
1 parent d9dd6f8 commit edb82bb
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 10 deletions.
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.3.0'
version '3.3.1-SNAPSHOT'
description 'A library and utility to compare different images.'
sourceCompatibility = 1.8

Expand Down
31 changes: 30 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.romankh3</groupId>
<artifactId>image-comparison</artifactId>
<version>3.3.0</version>
<version>3.3.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Image Comparison</name>
Expand All @@ -32,4 +32,33 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,12 @@ private BufferedImage drawRectangles(List<Rectangle> rectangles) throws IOExcept
* @param rectangles the collection of the {@link Rectangle}.
*/
private void draw(Graphics2D graphics, List<Rectangle> rectangles) {
// getWidth/getHeight return real width/height,
// so need to draw rectangle on one px smaller because minpoint + width/height is point on excluded pixel
rectangles.forEach(rectangle -> graphics.drawRect(rectangle.getMinPoint().getX(),
rectangle.getMinPoint().getY(),
rectangle.getWidth(),
rectangle.getHeight()));
rectangle.getWidth() - 1,
rectangle.getHeight() - 1));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public Integer size() {
* @return rectangle width.
*/
public int getWidth() {
return maxPoint.getX() - minPoint.getX();
return maxPoint.getX() - minPoint.getX() + 1; // min and max point are included, so real width is +1px
}

/**
Expand All @@ -143,7 +143,7 @@ public int getWidth() {
* @return rectangle height.
*/
public int getHeight() {
return maxPoint.getY() - minPoint.getY();
return maxPoint.getY() - minPoint.getY() + 1; // min and max point are included, so real width is +1px
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void testDestinationGetting() throws IOException {
public void testMinimalRectangleSize() throws IOException {
//given
ImageComparison imageComparison = new ImageComparison("expected.png", "actual.png");
imageComparison.setMinimalRectangleSize(10);
imageComparison.setMinimalRectangleSize(4 * 4 + 1);
BufferedImage expectedImage = readImageFromResources("minimalRectangleSizeResult.png");

//when
Expand Down Expand Up @@ -283,6 +283,23 @@ public void testIssue113() throws IOException {
assertEquals(0.0, imageComparison.getPixelToleranceLevel(), 0.0);
}

/**
* Test issue #134 If image is different in a line in 1 px, ComparisonState is always MATCH.
*/
@Test
public void testIssue134() throws IOException {
//given
ImageComparison imageComparison = new ImageComparison("expected#134.png", "actual#134.png");
BufferedImage expectedImage = readImageFromResources("result#134.png");

//when
ComparisonResult comparisonResult = imageComparison.compareImages();

//then
assertEquals(MISMATCH, comparisonResult.getComparisonState());
assertImagesEqual(expectedImage, comparisonResult.getResult());
}

@Test
public void testMatchSize() throws IOException {
//when
Expand Down Expand Up @@ -322,12 +339,15 @@ public void testResearchJpegImages() throws IOException {
BufferedImage expectedResult = readImageFromResources("result.jpg");

//when
ComparisonResult comparisonResult = new ImageComparison(expected, actual).compareImages();
ComparisonResult comparisonResult = new ImageComparison(expected, actual)
.setMinimalRectangleSize(4)
.compareImages();

//then
assertEquals(MISMATCH, comparisonResult.getComparisonState());
assertImagesEqual(expectedResult, comparisonResult.getResult());
}

@Test
public void testCompareMisSizedImages() throws IOException {
//given
Expand All @@ -341,7 +361,5 @@ public void testCompareMisSizedImages() throws IOException {
assertEquals(SIZE_MISMATCH, comparisonResult.getComparisonState());
boolean differenceLessThan2 = comparisonResult.getDifferencePercent()<2;
assertTrue(differenceLessThan2);

}

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

0 comments on commit edb82bb

Please sign in to comment.