11package aquality .selenium .core .visualization ;
22
3+ import aquality .selenium .core .configurations .IVisualConfiguration ;
4+ import com .google .inject .Inject ;
5+
36import java .awt .*;
47import java .awt .image .BufferedImage ;
58
69public class ImageComparator implements IImageComparator {
7- private static final int DEFAULT_THRESHOLD = 3 ;
810 private static final int THRESHOLD_DIVISOR = 255 ;
9- private final int comparisonHeight = 16 ;
10- private final int comparisonWidth = 16 ;
11+ private final IVisualConfiguration visualConfiguration ;
12+
13+ @ Inject
14+ public ImageComparator (IVisualConfiguration visualConfiguration ) {
15+ this .visualConfiguration = visualConfiguration ;
16+ }
17+
18+ private int getComparisonHeight () {
19+ return visualConfiguration .getComparisonHeight ();
20+ }
21+
22+ private int getComparisonWidth () {
23+ return visualConfiguration .getComparisonWidth ();
24+ }
1125
1226 public float percentageDifference (Image thisOne , Image theOtherOne , float threshold ) {
1327 if (threshold < 0 || threshold > 1 ) {
1428 throw new IllegalArgumentException (String .format ("Threshold should be between 0 and 1, but was [%s]" , threshold ));
1529 }
1630
17- int intThreshold = Float . valueOf ( threshold * THRESHOLD_DIVISOR ). intValue ( );
31+ int intThreshold = ( int ) ( threshold * THRESHOLD_DIVISOR );
1832 return percentageDifference (thisOne , theOtherOne , intThreshold );
1933 }
2034
2135 public float percentageDifference (Image thisOne , Image theOtherOne ) {
22- return percentageDifference (thisOne , theOtherOne , DEFAULT_THRESHOLD );
36+ return percentageDifference (thisOne , theOtherOne , visualConfiguration . getDefaultThreshold () );
2337 }
2438
2539 protected float percentageDifference (Image thisOne , Image theOtherOne , int threshold ) {
@@ -35,16 +49,16 @@ protected float percentageDifference(Image thisOne, Image theOtherOne, int thres
3549 }
3650 }
3751
38- return diffPixels / (float ) (comparisonWidth * comparisonHeight );
52+ return diffPixels / (float ) (getComparisonWidth () * getComparisonHeight () );
3953 }
4054
4155 protected int [][] getDifferences (Image thisOne , Image theOtherOne ) {
4256 int [][] firstGray = getResizedGrayScaleValues (thisOne );
4357 int [][] secondGray = getResizedGrayScaleValues (theOtherOne );
4458
45- int [][] differences = new int [comparisonWidth ][ comparisonHeight ];
46- for (int y = 0 ; y < comparisonHeight ; y ++) {
47- for (int x = 0 ; x < comparisonWidth ; x ++) {
59+ int [][] differences = new int [getComparisonWidth ()][ getComparisonHeight () ];
60+ for (int y = 0 ; y < getComparisonHeight () ; y ++) {
61+ for (int x = 0 ; x < getComparisonWidth () ; x ++) {
4862 differences [x ][y ] = (byte ) Math .abs (firstGray [x ][y ] - secondGray [x ][y ]);
4963 }
5064 }
@@ -53,13 +67,11 @@ protected int[][] getDifferences(Image thisOne, Image theOtherOne) {
5367 }
5468
5569 protected int [][] getResizedGrayScaleValues (Image image ) {
56- BufferedImage resizedImage = new BufferedImage (comparisonWidth , comparisonHeight , BufferedImage .TYPE_BYTE_GRAY );
57- Graphics2D graphics2D = resizedImage .createGraphics ();
58- graphics2D .drawImage (image , 0 , 0 , comparisonWidth , comparisonHeight , null );
59- graphics2D .dispose ();
60- int [][] grayScale = new int [comparisonWidth ][comparisonHeight ];
61- for (int y = 0 ; y < comparisonHeight ; y ++) {
62- for (int x = 0 ; x < comparisonWidth ; x ++) {
70+ BufferedImage resizedImage = ImageFunctions .resize (image , getComparisonWidth (), getComparisonHeight ());
71+ BufferedImage grayImage = ImageFunctions .grayscale (resizedImage );
72+ int [][] grayScale = new int [grayImage .getWidth ()][grayImage .getHeight ()];
73+ for (int y = 0 ; y < grayImage .getHeight (); y ++) {
74+ for (int x = 0 ; x < grayImage .getWidth (); x ++) {
6375 int pixel = resizedImage .getRGB (x , y );
6476 int red = (pixel >> 16 ) & 0xff ;
6577 grayScale [x ][y ] = Math .abs (red );
0 commit comments