Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

package com.assertthat.selenium_shutterbug.utils.image;

import com.assertthat.selenium_shutterbug.utils.file.FileUtil;
import com.assertthat.selenium_shutterbug.utils.web.Coordinates;

import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.*;
import java.io.File;

/**
* Created by Glib_Briia on 17/06/2016.
Expand All @@ -18,6 +20,7 @@ public class ImageProcessor {

private static final int ARCH_SIZE = 10;
private static float[] matrix = new float[49];
private static double pixelError = Double.MAX_VALUE;

static {
for (int i = 0; i < 49; i++)
Expand Down Expand Up @@ -129,6 +132,68 @@ public static boolean imagesAreEquals(BufferedImage image1, BufferedImage image2
return p == 0 || p <= deviation;
}

/**
* Extends the functionality of imagesAreEqualsWithDiff, but creates a third BufferedImage and applies pixel manipulation to it.
* @param image1 The first image to compare
* @param image2 The second image to compare
* @param pathFileName The output path filename for the third image, if null then is ignored
* @param deviation The upper limit of the pixel deviation for the test
* @return If the test passes
*/
public static boolean imagesAreEqualsWithDiff(BufferedImage image1, BufferedImage image2, String pathFileName, double deviation) {
BufferedImage output = new BufferedImage(image1.getWidth(), image1.getHeight(), BufferedImage.TYPE_INT_RGB);

int width1 = image1.getWidth(null);
int width2 = image2.getWidth(null);
int height1 = image1.getHeight(null);
int height2 = image2.getHeight(null);
if ((width1 != width2) || (height1 != height2)) {
throw new UnableToCompareImagesException("Images dimensions mismatch: image1 - " + width1 + "x" + height1 + "; image2 - " + width2 + "x" + height2);
}
long diff = 0;
long recordedDiff = 0; // Records the difference so it can be compared, saves having to do three if statements
for (int y = 0; y < height1; y++) {
for (int x = 0; x < width1; x++) {
recordedDiff = diff;

// Grab RGB values of both images, then bit shift and bitwise AND to break them down into R, G and B
int rgb1 = image1.getRGB(x, y);
int rgb2 = image2.getRGB(x, y);
int r1 = (rgb1 >> 16) & 0xff;
int g1 = (rgb1 >> 8) & 0xff;
int b1 = (rgb1) & 0xff;
int r2 = (rgb2 >> 16) & 0xff;
int g2 = (rgb2 >> 8) & 0xff;
int b2 = (rgb2) & 0xff;
diff += Math.abs(r1 - r2);
diff += Math.abs(g1 - g2);
diff += Math.abs(b1 - b2);

// If difference > recorded difference, change pixel to red. If zero, set to image 1's original pixel
if(diff > recordedDiff)
output.setRGB(x,y,new Color(255,0,0).getRGB() & rgb1); // Dark red = original position, Light red is moved to
else
output.setRGB(x,y,rgb1);
}
}
int colourSpaceBytes = 3; // RGB is 24 bit, or 3 bytes
double totalPixels = width1 * height1 * colourSpaceBytes;
pixelError = diff / totalPixels / 255.0;

// Write the image as png, with the filename based on the path provided
if(pixelError > 0)
FileUtil.writeImage(output,"png",new File(pathFileName));
return pixelError == 0 || pixelError <= deviation;
}

/**
* Gives back the pixel error set by imagesAreEqualsWithDiff. 'getPixelError' should be called after 'imagesAreEqualsWithDiff.'
* @return pixelError The pixel error
*/
public static double getPixelError(){
return pixelError;
}

public static BufferedImage scale(BufferedImage source, double ratio) {
int w = (int) (source.getWidth() * ratio);
int h = (int) (source.getHeight() * ratio);
Expand Down