|
5 | 5 |
|
6 | 6 | package com.assertthat.selenium_shutterbug.utils.image;
|
7 | 7 |
|
8 |
| -import com.assertthat.selenium_shutterbug.utils.file.FileUtil; |
| 8 | +import com.assertthat.selenium_shutterbug.utils.image.model.ImageData; |
9 | 9 | import com.assertthat.selenium_shutterbug.utils.web.Coordinates;
|
10 | 10 |
|
11 | 11 | import java.awt.*;
|
|
17 | 17 | import java.awt.image.ConvolveOp;
|
18 | 18 | import java.awt.image.Kernel;
|
19 | 19 | import java.awt.image.PixelGrabber;
|
20 |
| -import java.io.File; |
21 | 20 |
|
22 | 21 | /**
|
23 | 22 | * Created by Glib_Briia on 17/06/2016.
|
@@ -107,86 +106,30 @@ public static BufferedImage convertToGrayAndWhite(BufferedImage sourceImage) {
|
107 | 106 | }
|
108 | 107 |
|
109 | 108 | 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()); |
116 | 113 | }
|
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); |
136 | 115 | }
|
137 | 116 |
|
138 | 117 | /**
|
139 | 118 | * 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 |
142 | 122 | * @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 |
144 | 124 | * @return If the test passes
|
145 | 125 | */
|
146 | 126 | 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()); |
181 | 131 | }
|
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); |
190 | 133 | }
|
191 | 134 |
|
192 | 135 | public static BufferedImage scale(BufferedImage source, double ratio) {
|
|
0 commit comments