Skip to content

Commit efdf409

Browse files
Extract image compare methods to other class (with lombok)
1 parent 7d28555 commit efdf409

File tree

3 files changed

+110
-72
lines changed

3 files changed

+110
-72
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
<artifactId>commons-io</artifactId>
5252
<version>2.6</version>
5353
</dependency>
54+
<dependency>
55+
<groupId>org.projectlombok</groupId>
56+
<artifactId>lombok</artifactId>
57+
<version>1.18.8</version>
58+
</dependency>
5459
</dependencies>
5560

5661
<build>

src/main/java/com/assertthat/selenium_shutterbug/utils/image/ImageProcessor.java

Lines changed: 15 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package com.assertthat.selenium_shutterbug.utils.image;
77

8-
import com.assertthat.selenium_shutterbug.utils.file.FileUtil;
8+
import com.assertthat.selenium_shutterbug.utils.image.model.ImageData;
99
import com.assertthat.selenium_shutterbug.utils.web.Coordinates;
1010

1111
import java.awt.*;
@@ -17,7 +17,6 @@
1717
import java.awt.image.ConvolveOp;
1818
import java.awt.image.Kernel;
1919
import java.awt.image.PixelGrabber;
20-
import java.io.File;
2120

2221
/**
2322
* Created by Glib_Briia on 17/06/2016.
@@ -107,86 +106,30 @@ public static BufferedImage convertToGrayAndWhite(BufferedImage sourceImage) {
107106
}
108107

109108
public static boolean imagesAreEquals(BufferedImage image1, BufferedImage image2, double deviation) {
110-
int width1 = image1.getWidth(null);
111-
int width2 = image2.getWidth(null);
112-
int height1 = image1.getHeight(null);
113-
int height2 = image2.getHeight(null);
114-
if ((width1 != width2) || (height1 != height2)) {
115-
throw new UnableToCompareImagesException("Images dimensions mismatch: image1 - " + width1 + "x" + height1 + "; image2 - " + width2 + "x" + height2);
109+
ImageData image1Data = new ImageData(image1);
110+
ImageData image2Data = new ImageData(image2);
111+
if (image1Data.notEqualsDimensions(image2Data)) {
112+
throw new UnableToCompareImagesException("Images dimensions mismatch: image1 - " + image1Data.getWidth() + "x" + image1Data.getHeight() + "; image2 - " + image2Data.getWidth() + "x" + image2Data.getHeight());
116113
}
117-
long diff = 0;
118-
for (int y = 0; y < height1; y++) {
119-
for (int x = 0; x < width1; x++) {
120-
int rgb1 = image1.getRGB(x, y);
121-
int rgb2 = image2.getRGB(x, y);
122-
int r1 = (rgb1 >> 16) & 0xff;
123-
int g1 = (rgb1 >> 8) & 0xff;
124-
int b1 = (rgb1) & 0xff;
125-
int r2 = (rgb2 >> 16) & 0xff;
126-
int g2 = (rgb2 >> 8) & 0xff;
127-
int b2 = (rgb2) & 0xff;
128-
diff += Math.abs(r1 - r2);
129-
diff += Math.abs(g1 - g2);
130-
diff += Math.abs(b1 - b2);
131-
}
132-
}
133-
double n = width1 * height1 * 3;
134-
double p = diff / n / 255.0;
135-
return p == 0 || p <= deviation;
114+
return image1Data.equalsEachPixels(image2Data, deviation);
136115
}
137116

138117
/**
139118
* Extends the functionality of imagesAreEqualsWithDiff, but creates a third BufferedImage and applies pixel manipulation to it.
140-
* @param image1 The first image to compare
141-
* @param image2 The second image to compare
119+
*
120+
* @param image1 The first image to compare
121+
* @param image2 The second image to compare
142122
* @param pathFileName The output path filename for the third image, if null then is ignored
143-
* @param deviation The upper limit of the pixel deviation for the test
123+
* @param deviation The upper limit of the pixel deviation for the test
144124
* @return If the test passes
145125
*/
146126
public static boolean imagesAreEqualsWithDiff(BufferedImage image1, BufferedImage image2, String pathFileName, double deviation) {
147-
BufferedImage output = new BufferedImage(image1.getWidth(), image1.getHeight(), BufferedImage.TYPE_INT_RGB);
148-
149-
int width1 = image1.getWidth(null);
150-
int width2 = image2.getWidth(null);
151-
int height1 = image1.getHeight(null);
152-
int height2 = image2.getHeight(null);
153-
if ((width1 != width2) || (height1 != height2)) {
154-
throw new UnableToCompareImagesException("Images dimensions mismatch: image1 - " + width1 + "x" + height1 + "; image2 - " + width2 + "x" + height2);
155-
}
156-
long diff = 0;
157-
long recordedDiff; // Records the difference so it can be compared, saves having to do three if statements
158-
for (int y = 0; y < height1; y++) {
159-
for (int x = 0; x < width1; x++) {
160-
recordedDiff = diff;
161-
162-
// Grab RGB values of both images, then bit shift and bitwise AND to break them down into R, G and B
163-
int rgb1 = image1.getRGB(x, y);
164-
int rgb2 = image2.getRGB(x, y);
165-
int r1 = (rgb1 >> 16) & 0xff;
166-
int g1 = (rgb1 >> 8) & 0xff;
167-
int b1 = (rgb1) & 0xff;
168-
int r2 = (rgb2 >> 16) & 0xff;
169-
int g2 = (rgb2 >> 8) & 0xff;
170-
int b2 = (rgb2) & 0xff;
171-
diff += Math.abs(r1 - r2);
172-
diff += Math.abs(g1 - g2);
173-
diff += Math.abs(b1 - b2);
174-
175-
// If difference > recorded difference, change pixel to red. If zero, set to image 1's original pixel
176-
if(diff > recordedDiff)
177-
output.setRGB(x,y,new Color(255,0,0).getRGB() & rgb1); // Dark red = original position, Light red is moved to
178-
else
179-
output.setRGB(x,y,rgb1);
180-
}
127+
ImageData image1Data = new ImageData(image1);
128+
ImageData image2Data = new ImageData(image2);
129+
if (image1Data.notEqualsDimensions(image2Data)) {
130+
throw new UnableToCompareImagesException("Images dimensions mismatch: image1 - " + image1Data.getWidth() + "x" + image1Data.getHeight() + "; image2 - " + image2Data.getWidth() + "x" + image2Data.getHeight());
181131
}
182-
int colourSpaceBytes = 3; // RGB is 24 bit, or 3 bytes
183-
double totalPixels = width1 * height1 * colourSpaceBytes;
184-
pixelError = diff / totalPixels / 255.0;
185-
186-
// Write the image as png, with the filename based on the path provided
187-
if(pixelError > 0)
188-
FileUtil.writeImage(output,"png",new File(pathFileName+".png"));
189-
return pixelError == 0 || pixelError <= deviation;
132+
return image1Data.equalsEachPixelsWithCreateDifferencesImage(image2Data, deviation, pathFileName);
190133
}
191134

192135
public static BufferedImage scale(BufferedImage source, double ratio) {
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.assertthat.selenium_shutterbug.utils.image.model;
2+
3+
import com.assertthat.selenium_shutterbug.utils.file.FileUtil;
4+
import lombok.Getter;
5+
6+
import java.awt.*;
7+
import java.awt.image.BufferedImage;
8+
import java.io.File;
9+
10+
@Getter
11+
public class ImageData {
12+
private final int RED_RGB = new Color(255, 0, 0).getRGB();
13+
private final BufferedImage image;
14+
private final int width;
15+
private final int height;
16+
17+
public ImageData(BufferedImage image) {
18+
this.image = image;
19+
this.width = image.getWidth(null);
20+
this.height = image.getHeight(null);
21+
}
22+
23+
public boolean notEqualsDimensions(ImageData imageData) {
24+
return !equalsDimensions(imageData);
25+
}
26+
27+
private boolean equalsDimensions(ImageData imageData) {
28+
return this.width == imageData.width && this.height == imageData.height;
29+
}
30+
31+
public boolean equalsEachPixelsWithCreateDifferencesImage(ImageData imageData, double deviation, String pathDifferenceImageFileName) {
32+
return equalsEachPixelsWithCreateDifferencesImage(imageData.getImage(), deviation, pathDifferenceImageFileName);
33+
}
34+
35+
private boolean equalsEachPixelsWithCreateDifferencesImage(BufferedImage image, double deviation, String pathDifferenceImageFileName) {
36+
createDifferencesImage(image, pathDifferenceImageFileName);
37+
return equalsEachPixels(image, deviation);
38+
}
39+
40+
private void createDifferencesImage(BufferedImage image, String pathDifferenceImageFileName) {
41+
BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
42+
43+
for (int y = 0; y < height; y++) {
44+
for (int x = 0; x < width; x++) {
45+
int rgb1 = this.getImage().getRGB(x, y);
46+
int rgb2 = image.getRGB(x, y);
47+
48+
// If difference > recorded difference, change pixel to red. If zero, set to image 1's original pixel
49+
if (rgb1 != rgb2)
50+
output.setRGB(x, y, RED_RGB & rgb1); // Dark red = original position, Light red is moved to
51+
else
52+
output.setRGB(x, y, rgb1);
53+
}
54+
}
55+
56+
FileUtil.writeImage(output, "png", new File(pathDifferenceImageFileName + ".png"));
57+
}
58+
59+
public boolean equalsEachPixels(ImageData imageData, double deviation) {
60+
return equalsEachPixels(imageData.getImage(), deviation);
61+
}
62+
63+
private boolean equalsEachPixels(BufferedImage image, double deviation) {
64+
double p = calculatePixelsDifference(image);
65+
66+
return p == 0 || p <= deviation;
67+
}
68+
69+
private double calculatePixelsDifference(BufferedImage image) {
70+
long diff = 0;
71+
for (int y = 0; y < height; y++) {
72+
for (int x = 0; x < width; x++) {
73+
int rgb1 = this.getImage().getRGB(x, y);
74+
int rgb2 = image.getRGB(x, y);
75+
int r1 = (rgb1 >> 16) & 0xff;
76+
int g1 = (rgb1 >> 8) & 0xff;
77+
int b1 = (rgb1) & 0xff;
78+
int r2 = (rgb2 >> 16) & 0xff;
79+
int g2 = (rgb2 >> 8) & 0xff;
80+
int b2 = (rgb2) & 0xff;
81+
diff += Math.abs(r1 - r2);
82+
diff += Math.abs(g1 - g2);
83+
diff += Math.abs(b1 - b2);
84+
}
85+
}
86+
double n = width * height * 3;
87+
88+
return diff / n / 255.0;
89+
}
90+
}

0 commit comments

Comments
 (0)