Skip to content

Commit 97af3fc

Browse files
Add simpleComparison and load tests (#220)
Co-authored-by: Roman Beekeeper <[email protected]>
1 parent d22bfe6 commit 97af3fc

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

src/main/java/com/github/romankh3/image/comparison/ImageComparison.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,25 @@ public ImageComparisonResult compareImages() {
215215
.setRectangles(rectangles);
216216
}
217217

218+
/**
219+
* Compare two images and exclude drawing rectangles with the differences.
220+
*
221+
* @return the result comparisonState
222+
*/
223+
public ImageComparisonResult simpleComparison(){
224+
// check that the images have the same size
225+
if (isImageSizesNotEqual(expected, actual)) {
226+
BufferedImage actualResized = ImageComparisonUtil.resize(actual, expected.getWidth(), expected.getHeight());
227+
return ImageComparisonResult.defaultSizeMisMatchResult(expected, actual, getDifferencePercent(actualResized, expected));
228+
}
229+
230+
if (isFirstDifferences()){
231+
return ImageComparisonResult.defaultMisMatchResult(expected, actual, 0);
232+
}else {
233+
return ImageComparisonResult.defaultMatchResult(expected, actual);
234+
}
235+
}
236+
218237
/**
219238
* Check images for equals their widths and heights.
220239
*
@@ -301,6 +320,24 @@ private List<Rectangle> populateRectangles() {
301320
return mergeRectangles(mergeRectangles(rectangles));
302321
}
303322

323+
/**
324+
* say if there exists two pixels equal or not.
325+
*
326+
* @return true, if first find two different pixels.
327+
*/
328+
private boolean isFirstDifferences() {
329+
for (int y = 0; y < expected.getHeight(); y++) {
330+
for (int x = 0; x < expected.getWidth(); x++) {
331+
if (!excludedAreas.contains(new Point(x, y))) {
332+
if (isDifferentPixels(expected.getRGB(x, y), actual.getRGB(x, y))) {
333+
return true;
334+
}
335+
}
336+
}
337+
}
338+
return false;
339+
}
340+
304341
/**
305342
* Say if provided {@param countOfDifferentPixels} is allowed for {@link ImageComparisonState#MATCH} state.
306343
*
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.github.romankh3.image.comparison;
2+
3+
import com.github.romankh3.image.comparison.model.ImageComparisonResult;
4+
import com.github.romankh3.image.comparison.model.ImageComparisonState;
5+
import java.awt.image.BufferedImage;
6+
7+
import org.junit.jupiter.api.DisplayName;
8+
import org.junit.jupiter.api.Test;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
11+
12+
@DisplayName("Load tests for {@link simpleComparison} method")
13+
public class LoadTest {
14+
@Test
15+
@DisplayName("should result MisMatch for two images")
16+
public void simpleComparisonTest(){
17+
//load images to be compared:
18+
BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png");
19+
BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png");
20+
21+
//Create ImageComparison object and compare the images.
22+
ImageComparisonResult imageComparisonResult = new ImageComparison(expectedImage, actualImage).simpleComparison();
23+
24+
//Check the result
25+
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult.getImageComparisonState());
26+
}
27+
28+
29+
@Test
30+
@DisplayName("compare load differences between {@link simpleComparison} method and {@link compareImage} method")
31+
public void loadTest1(){
32+
//load images to be compared:
33+
BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png");
34+
BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png");
35+
36+
long time1 = System.currentTimeMillis();
37+
//Create ImageComparison object and compare the images.
38+
ImageComparisonResult imageComparisonResult1 = new ImageComparison(expectedImage, actualImage).compareImages();
39+
long etime1 = System.currentTimeMillis();
40+
//Time for compareImage method
41+
long compareImageTime = etime1 - time1;
42+
43+
//Check the result
44+
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult1.getImageComparisonState());
45+
46+
long time2 = System.currentTimeMillis();
47+
//Create ImageComparison2 object and compare the images.
48+
ImageComparisonResult imageComparisonResult2 = new ImageComparison(expectedImage, actualImage).simpleComparison();
49+
long etime2 = System.currentTimeMillis();
50+
//Time for simpleComparison method
51+
long simpleComparisonTime = etime2 - time2;
52+
53+
//Check the result
54+
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult2.getImageComparisonState());
55+
//check the load differences between simpleComparison and compareImage pattern
56+
assertTrue(simpleComparisonTime < compareImageTime);
57+
}
58+
59+
@Test
60+
@DisplayName("compare load differences between {@link simpleComparison} method and {@link compareImage} method")
61+
public void loadTest2(){
62+
//load images to be compared:
63+
BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected#201.png");
64+
BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual#201.png");
65+
66+
long time1 = System.currentTimeMillis();
67+
//Create ImageComparison object and compare the images.
68+
ImageComparisonResult imageComparisonResult1 = new ImageComparison(expectedImage, actualImage).compareImages();
69+
long etime1 = System.currentTimeMillis();
70+
//Time for compareImage method
71+
long compareImageTime = etime1 - time1;
72+
73+
//Check the result
74+
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult1.getImageComparisonState());
75+
76+
long time2 = System.currentTimeMillis();
77+
//Create ImageComparison2 object and compare the images.
78+
ImageComparisonResult imageComparisonResult2 = new ImageComparison(expectedImage, actualImage).simpleComparison();
79+
long etime2 = System.currentTimeMillis();
80+
//Time for simpleComparison method
81+
long simpleComparisonTime = etime2 - time2;
82+
83+
//Check the result
84+
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult2.getImageComparisonState());
85+
//check the load differences between simpleComparison and compareImage pattern
86+
assertTrue(simpleComparisonTime < compareImageTime);
87+
}
88+
89+
@Test
90+
@DisplayName("compare load differences between {@link simpleComparison} method and {@link compareImage} method")
91+
public void loadTest3(){
92+
//load images to be compared:
93+
BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected#98.png");
94+
BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual#98.png");
95+
96+
long time1 = System.currentTimeMillis();
97+
//Create ImageComparison object and compare the images.
98+
ImageComparisonResult imageComparisonResult1 = new ImageComparison(expectedImage, actualImage).compareImages();
99+
long etime1 = System.currentTimeMillis();
100+
//Time for compareImage method
101+
long compareImageTime = etime1 - time1;
102+
103+
//Check the result
104+
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult1.getImageComparisonState());
105+
106+
long time2 = System.currentTimeMillis();
107+
//Create ImageComparison2 object and compare the images.
108+
ImageComparisonResult imageComparisonResult2 = new ImageComparison(expectedImage, actualImage).simpleComparison();
109+
long etime2 = System.currentTimeMillis();
110+
//Time for simpleComparison method
111+
long simpleComparisonTime = etime2 - time2;
112+
113+
//Check the result
114+
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult2.getImageComparisonState());
115+
//check the load differences between simpleComparison and compareImage pattern
116+
assertTrue(simpleComparisonTime < compareImageTime);
117+
}
118+
119+
}

0 commit comments

Comments
 (0)