Skip to content

Commit

Permalink
#167 added option to fill rectangles (#168) (#169)
Browse files Browse the repository at this point in the history
#167: Added options to draw transparent fill inside rectangles
  • Loading branch information
romankh3 authored Jan 29, 2020
1 parent 370a57b commit 47a2b18
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ Published on Maven Central and jCenter Java Library that compares 2 images with
| `pixelToleranceLevel` | Level of the pixel tolerance. By default it's 0.1 -> 10% difference. The value can be set from 0.0 to 0.99. |
| `excludedAreas` | ExcludedAreas contains a List of Rectangles to be ignored when comparing images. |
| `drawExcludedRectangles` | Flag which says draw excluded rectangles or not. |
| `fillExcludedRectangles` | Flag which says fill excluded rectangles or not. |
| `percentOpacityExcludedRectangles` | The desired opacity of the excluded rectangle fill. |
| `fillDifferenceRectangles` | Flag which says fill difference rectangles or not. |
| `percentOpacityDifferenceRectangles` | The desired opacity of the difference rectangle fill. |

## Release Notes

Expand Down Expand Up @@ -100,6 +104,16 @@ class Example {
imageComparison.setRectangleLineWidth(5);
imageComparison.getRectangleLineWidth();

//DifferenceRectangleFilling - Fill the inside the difference rectangles with a transparent fill. By default it's false and 20.0% opacity.
imageComparison.setDifferenceRectangleFilling(true, 30.0);
imageComparison.isFillDifferenceRectangles();
imageComparison.getPercentOpacityDifferenceRectangles();

//ExcludedRectangleFilling - Fill the inside the excluded rectangles with a transparent fill. By default it's false and 20.0% opacity.
imageComparison.setExcludedRectangleFilling(true, 30.0);
imageComparison.isFillExcludedRectangles();
imageComparison.getPercentOpacityExcludedRectangles();

//Destination. Before comparing also can be added destination file for result image.
imageComparison.setDestination(resultDestination);
imageComparison.getDestination();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@ public class ImageComparison {
* The difference in percent between two images.
*/
private float differencePercent;

/**
* Flag for filling comparison difference rectangles.
*/
private boolean fillDifferenceRectangles = false;

/**
* Sets the opacity percentage of the fill of comparison difference rectangles. 0.0 means completely transparent and 100.0 means completely opaque.
*/
private double percentOpacityDifferenceRectangles = 20.0;

/**
* Flag for filling excluded rectangles.
*/
private boolean fillExcludedRectangles = false;

/**
* Sets the opacity percentage of the fill of excluded rectangles. 0.0 means completely transparent and 100.0 means completely opaque.
*/
private double percentOpacityExcludedRectangles = 20.0;

/**
* Create a new instance of {@link ImageComparison} that can compare the given images.
Expand Down Expand Up @@ -338,6 +358,10 @@ private void drawExcludedRectangles(Graphics2D graphics) {
if (drawExcludedRectangles) {
graphics.setColor(Color.GREEN);
draw(graphics, excludedAreas.getExcluded());

if (fillExcludedRectangles) {
fillRectangles(graphics, excludedAreas.getExcluded(), percentOpacityExcludedRectangles);
}
}
}

Expand All @@ -361,6 +385,10 @@ private void drawRectanglesOfDifferences(List<Rectangle> rectangles, Graphics2D
}

draw(graphics, rectanglesForDraw);

if (fillDifferenceRectangles) {
fillRectangles(graphics, rectanglesForDraw, percentOpacityDifferenceRectangles);
}
}

/**
Expand Down Expand Up @@ -403,6 +431,32 @@ private void draw(Graphics2D graphics, List<Rectangle> rectangles) {
rectangle.getHeight() - 1)
);
}

/**
* Fill rectangles based on collection of the {@link Rectangle} and {@link Graphics2D}.
* getWidth/getHeight return real width/height,
* so need to draw rectangle fill two px smaller to fit inside rectangle borders.
*
* @param graphics the {@link Graphics2D} object for drawing.
* @param rectangles rectangles the collection of the {@link Rectangle}.
* @param percentOpacity the opacity of the fill.
*/
private void fillRectangles(Graphics2D graphics, List<Rectangle> rectangles, double percentOpacity) {

graphics.setColor(new Color(graphics.getColor().getRed(),
graphics.getColor().getGreen(),
graphics.getColor().getBlue(),
(int) (percentOpacity / 100 * 255)
));
rectangles.forEach(rectangle -> graphics.fillRect(
rectangle.getMinPoint().x - 1,
rectangle.getMinPoint().y - 1,
rectangle.getWidth() - 2,
rectangle.getHeight() - 2)
);
}



/**
* Group rectangle regions in matrix.
Expand Down Expand Up @@ -547,4 +601,34 @@ public ImageComparison setExcludedAreas(List<Rectangle> excludedAreas) {
this.excludedAreas = new ExcludedAreas(excludedAreas);
return this;
}

public boolean isFillDifferenceRectangles() {
return this.fillDifferenceRectangles;
}

public double getPercentOpacityDifferenceRectangles() {
return this.percentOpacityDifferenceRectangles;
}

public ImageComparison setDifferenceRectangleFilling(boolean fillRectangles, double percentOpacity) {
this.fillDifferenceRectangles = fillRectangles;
this.percentOpacityDifferenceRectangles = percentOpacity;
return this;
}

public boolean isFillExcludedRectangles() {
return this.fillExcludedRectangles;
}

public double getPercentOpacityExcludedRectangles() {
return this.percentOpacityExcludedRectangles;
}

public ImageComparison setExcludedRectangleFilling(boolean fillRectangles, double percentOpacity) {
this.fillExcludedRectangles = fillRectangles;
this.percentOpacityExcludedRectangles = percentOpacity;
return this;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,68 @@ public void testIssue113() {
assertImagesEqual(expectedImage, imageComparisonResult.getResult());
assertEquals(0.0, imageComparison.getPixelToleranceLevel(), 0.0);
}

/**
* Test issue #167(a). Fill difference rectangles in transparent red.
*/
@Test
public void testIssue167a() {
//given
BufferedImage expected = readImageFromResources("expected#98.png");
BufferedImage actual = readImageFromResources("actual#98.png");

List<Rectangle> excludedAreas = asList(
new Rectangle(410, 514, 900, 565),
new Rectangle(410, 636, 900, 754)
);

BufferedImage expectedImage = readImageFromResources("result#167a.png");

ImageComparison imageComparison = new ImageComparison(expected, actual)
.setExcludedAreas(excludedAreas)
.setRectangleLineWidth(5)
.setPixelToleranceLevel(0.0)
.setDrawExcludedRectangles(true)
.setDifferenceRectangleFilling(true, 30.0);

//when
ImageComparisonResult imageComparisonResult = imageComparison.compareImages();

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

/**
* Test issue #167(b). Fill excluded rectangles in transparent green.
*/
@Test
public void testIssue167b() {
//given
BufferedImage expected = readImageFromResources("expected#98.png");
BufferedImage actual = readImageFromResources("actual#98.png");

List<Rectangle> excludedAreas = asList(
new Rectangle(410, 514, 900, 565),
new Rectangle(410, 636, 900, 754)
);

BufferedImage expectedImage = readImageFromResources("result#167b.png");

ImageComparison imageComparison = new ImageComparison(expected, actual)
.setExcludedAreas(excludedAreas)
.setRectangleLineWidth(5)
.setPixelToleranceLevel(0.0)
.setDrawExcludedRectangles(true)
.setExcludedRectangleFilling(true, 30.0);

//when
ImageComparisonResult imageComparisonResult = imageComparison.compareImages();

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

/**
* Test issue #134 If image is different in a line in 1 px, ComparisonState is always MATCH.
Expand Down Expand Up @@ -333,7 +395,9 @@ public void testGettersAndSetters() {
.setExcludedAreas(asList(Rectangle.createZero(), Rectangle.createDefault()))
.setDrawExcludedRectangles(true)
.setPixelToleranceLevel(0.6)
.setThreshold(400);
.setThreshold(400)
.setDifferenceRectangleFilling(true, 35.1)
.setExcludedRectangleFilling(true, 45.1);

//then
assertEquals(String.valueOf(100), String.valueOf(imageComparison.getMinimalRectangleSize()));
Expand All @@ -342,6 +406,10 @@ public void testGettersAndSetters() {
assertEquals(String.valueOf(400), String.valueOf(imageComparison.getThreshold()));
assertEquals(0.6, imageComparison.getPixelToleranceLevel(), 0.0);
assertTrue(imageComparison.isDrawExcludedRectangles());
assertEquals(35.1, imageComparison.getPercentOpacityDifferenceRectangles(), 0.0);
assertEquals(45.1, imageComparison.getPercentOpacityExcludedRectangles(), 0.0);
assertTrue(imageComparison.isFillDifferenceRectangles());
assertTrue(imageComparison.isFillExcludedRectangles());
}

@Test
Expand Down
Binary file added src/test/resources/result#167a.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#167b.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 47a2b18

Please sign in to comment.