diff --git a/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java b/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java index 8067c0b..e48e476 100644 --- a/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java +++ b/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java @@ -215,6 +215,25 @@ public ImageComparisonResult compareImages() { .setRectangles(rectangles); } + /** + * Compare two images and exclude drawing rectangles with the differences. + * + * @return the result comparisonState + */ + public ImageComparisonResult simpleComparison(){ + // check that the images have the same size + if (isImageSizesNotEqual(expected, actual)) { + BufferedImage actualResized = ImageComparisonUtil.resize(actual, expected.getWidth(), expected.getHeight()); + return ImageComparisonResult.defaultSizeMisMatchResult(expected, actual, getDifferencePercent(actualResized, expected)); + } + + if (isFirstDifferences()){ + return ImageComparisonResult.defaultMisMatchResult(expected, actual, 0); + }else { + return ImageComparisonResult.defaultMatchResult(expected, actual); + } + } + /** * Check images for equals their widths and heights. * @@ -301,6 +320,24 @@ private List populateRectangles() { return mergeRectangles(mergeRectangles(rectangles)); } + /** + * say if there exists two pixels equal or not. + * + * @return true, if first find two different pixels. + */ + private boolean isFirstDifferences() { + for (int y = 0; y < expected.getHeight(); y++) { + for (int x = 0; x < expected.getWidth(); x++) { + if (!excludedAreas.contains(new Point(x, y))) { + if (isDifferentPixels(expected.getRGB(x, y), actual.getRGB(x, y))) { + return true; + } + } + } + } + return false; + } + /** * Say if provided {@param countOfDifferentPixels} is allowed for {@link ImageComparisonState#MATCH} state. * diff --git a/src/test/java/com/github/romankh3/image/comparison/LoadTest.java b/src/test/java/com/github/romankh3/image/comparison/LoadTest.java new file mode 100644 index 0000000..d9b45a6 --- /dev/null +++ b/src/test/java/com/github/romankh3/image/comparison/LoadTest.java @@ -0,0 +1,119 @@ +package com.github.romankh3.image.comparison; + +import com.github.romankh3.image.comparison.model.ImageComparisonResult; +import com.github.romankh3.image.comparison.model.ImageComparisonState; +import java.awt.image.BufferedImage; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@DisplayName("Load tests for {@link simpleComparison} method") +public class LoadTest { + @Test + @DisplayName("should result MisMatch for two images") + public void simpleComparisonTest(){ + //load images to be compared: + BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png"); + BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png"); + + //Create ImageComparison object and compare the images. + ImageComparisonResult imageComparisonResult = new ImageComparison(expectedImage, actualImage).simpleComparison(); + + //Check the result + assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult.getImageComparisonState()); + } + + + @Test + @DisplayName("compare load differences between {@link simpleComparison} method and {@link compareImage} method") + public void loadTest1(){ + //load images to be compared: + BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png"); + BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png"); + + long time1 = System.currentTimeMillis(); + //Create ImageComparison object and compare the images. + ImageComparisonResult imageComparisonResult1 = new ImageComparison(expectedImage, actualImage).compareImages(); + long etime1 = System.currentTimeMillis(); + //Time for compareImage method + long compareImageTime = etime1 - time1; + + //Check the result + assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult1.getImageComparisonState()); + + long time2 = System.currentTimeMillis(); + //Create ImageComparison2 object and compare the images. + ImageComparisonResult imageComparisonResult2 = new ImageComparison(expectedImage, actualImage).simpleComparison(); + long etime2 = System.currentTimeMillis(); + //Time for simpleComparison method + long simpleComparisonTime = etime2 - time2; + + //Check the result + assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult2.getImageComparisonState()); + //check the load differences between simpleComparison and compareImage pattern + assertTrue(simpleComparisonTime < compareImageTime); + } + + @Test + @DisplayName("compare load differences between {@link simpleComparison} method and {@link compareImage} method") + public void loadTest2(){ + //load images to be compared: + BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected#201.png"); + BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual#201.png"); + + long time1 = System.currentTimeMillis(); + //Create ImageComparison object and compare the images. + ImageComparisonResult imageComparisonResult1 = new ImageComparison(expectedImage, actualImage).compareImages(); + long etime1 = System.currentTimeMillis(); + //Time for compareImage method + long compareImageTime = etime1 - time1; + + //Check the result + assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult1.getImageComparisonState()); + + long time2 = System.currentTimeMillis(); + //Create ImageComparison2 object and compare the images. + ImageComparisonResult imageComparisonResult2 = new ImageComparison(expectedImage, actualImage).simpleComparison(); + long etime2 = System.currentTimeMillis(); + //Time for simpleComparison method + long simpleComparisonTime = etime2 - time2; + + //Check the result + assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult2.getImageComparisonState()); + //check the load differences between simpleComparison and compareImage pattern + assertTrue(simpleComparisonTime < compareImageTime); + } + + @Test + @DisplayName("compare load differences between {@link simpleComparison} method and {@link compareImage} method") + public void loadTest3(){ + //load images to be compared: + BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected#98.png"); + BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual#98.png"); + + long time1 = System.currentTimeMillis(); + //Create ImageComparison object and compare the images. + ImageComparisonResult imageComparisonResult1 = new ImageComparison(expectedImage, actualImage).compareImages(); + long etime1 = System.currentTimeMillis(); + //Time for compareImage method + long compareImageTime = etime1 - time1; + + //Check the result + assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult1.getImageComparisonState()); + + long time2 = System.currentTimeMillis(); + //Create ImageComparison2 object and compare the images. + ImageComparisonResult imageComparisonResult2 = new ImageComparison(expectedImage, actualImage).simpleComparison(); + long etime2 = System.currentTimeMillis(); + //Time for simpleComparison method + long simpleComparisonTime = etime2 - time2; + + //Check the result + assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult2.getImageComparisonState()); + //check the load differences between simpleComparison and compareImage pattern + assertTrue(simpleComparisonTime < compareImageTime); + } + +}