Skip to content

Commit

Permalink
Merge 5bc97eb into 5ed652e
Browse files Browse the repository at this point in the history
  • Loading branch information
romankh3 committed Nov 15, 2019
2 parents 5ed652e + 5bc97eb commit 5f701f7
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 103 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.github.romankh3'
version '3.3.1'
version '3.4.0-SNAPSHOT'
description 'A library and utility to compare different images.'
sourceCompatibility = 1.8

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.romankh3</groupId>
<artifactId>image-comparison</artifactId>
<version>3.3.1</version>
<version>3.4.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Image Comparison</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,34 @@

import com.github.romankh3.image.comparison.ArgsParser.Arguments;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.function.Consumer;

/**
* Class for static method related to command line.
*/
class CommandLineUsage {

public ImageComparison create(String... args) throws IOException {
public ImageComparison create(String... args) {
Optional<Arguments> arguments = new ArgsParser().parseArgs(args);
return arguments.isPresent() ? create(arguments.get()) : createDefault();
}

public ImageComparison create(ArgsParser.Arguments args) throws IOException {
public ImageComparison create(ArgsParser.Arguments args) {
return new ImageComparison(
ImageComparisonUtil.readImageFromFile(args.getExpected()),
ImageComparisonUtil.readImageFromFile(args.getActual()),
args.getDestinationImage().orElse(null));
}

private ImageComparison createDefault() throws IOException {
private ImageComparison createDefault() {
return new ImageComparison(
ImageComparisonUtil.readImageFromResources("expected.png"),
ImageComparisonUtil.readImageFromResources("actual.png"),
null);
}

public static void handleResult(ImageComparison instance, IOConsumer<File> saveToFile, Runnable showUI)
throws IOException {
public static void handleResult(ImageComparison instance, Consumer<File> saveToFile, Runnable showUI) {
if (instance.getDestination().isPresent()) {
saveToFile.accept(instance.getDestination().get());
} else {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import com.github.romankh3.image.comparison.model.ExcludedAreas;
import com.github.romankh3.image.comparison.model.Point;
import com.github.romankh3.image.comparison.model.Rectangle;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand All @@ -37,7 +35,7 @@ public class ImageComparison {
/**
* Actual image for comparing
*/
private final BufferedImage actual;
private final BufferedImage actual;

/**
* Width of the line that is drawn the rectangle
Expand Down Expand Up @@ -119,10 +117,10 @@ public class ImageComparison {
*
* @param expected expected image to be compared
* @param actual actual image to be compared
* @throws IOException due to saving result image.
*/
public ImageComparison(String expected, String actual) throws IOException {
this(ImageComparisonUtil.readImageFromResources(expected), ImageComparisonUtil.readImageFromResources(actual),
public ImageComparison(String expected, String actual) {
this(ImageComparisonUtil.readImageFromResources(expected),
ImageComparisonUtil.readImageFromResources(actual),
null);
}

Expand Down Expand Up @@ -153,15 +151,14 @@ public ImageComparison(BufferedImage expected, BufferedImage actual) {
* Draw rectangles which cover the regions of the difference pixels.
*
* @return the result of the drawing.
* @throws IOException due to saving result image.
*/
public ComparisonResult compareImages() throws IOException {
public ComparisonResult compareImages() {

// check images for valid
if (isImageSizesNotEqual(expected, actual)) {
BufferedImage actualResized=ImageComparisonUtil.resize(actual, expected.getWidth(), expected.getHeight());
differencePercent=ImageComparisonUtil.getDifferencePercent(actualResized,expected);
return ComparisonResult.defaultSizeMisMatchResult(expected, actual,differencePercent);
BufferedImage actualResized = ImageComparisonUtil.resize(actual, expected.getWidth(), expected.getHeight());
differencePercent = ImageComparisonUtil.getDifferencePercent(actualResized, expected);
return ComparisonResult.defaultSizeMisMatchResult(expected, actual, differencePercent);
}

List<Rectangle> rectangles = populateRectangles();
Expand All @@ -178,6 +175,7 @@ public ComparisonResult compareImages() throws IOException {

return ComparisonResult.defaultMisMatchResult(expected, actual).setResult(resultImage);
}

/**
* Check images for equals their widths and heights.
*
Expand Down Expand Up @@ -210,7 +208,6 @@ private void populateTheMatrixOfTheDifferences() {
*
* @param expectedRgb the RGB value of the Pixel of the Expected image.
* @param actualRgb the RGB value of the Pixel of the Actual image.
*
* @return {@code true} if they' are difference, {@code false} otherwise.
*/
private boolean isDifferentPixels(int expectedRgb, int actualRgb) {
Expand Down Expand Up @@ -320,7 +317,7 @@ private List<Rectangle> mergeRectangles(List<Rectangle> rectangles) {
* @param rectangles the collection of the {@link Rectangle} objects.
* @return result {@link BufferedImage} with drawn rectangles.
*/
private BufferedImage drawRectangles(List<Rectangle> rectangles) throws IOException {
private BufferedImage drawRectangles(List<Rectangle> rectangles) {
BufferedImage resultImage = ImageComparisonUtil.deepCopy(actual);
Graphics2D graphics = resultImage.createGraphics();
graphics.setColor(Color.RED);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.romankh3.image.comparison;

/**
* {@link RuntimeException} exception, created due to reason to avoid rethrowing checked exceptions.
*/
public class ImageComparisonException extends RuntimeException {

public ImageComparisonException(String message) {
super(message);
}

public ImageComparisonException(String message, Throwable throwable) {
super(message, throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.awt.image.Kernel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
Expand Down Expand Up @@ -63,14 +62,18 @@ static BufferedImage deepCopy(BufferedImage image) {
*
* @param path the path where contains image.
* @return the {@link BufferedImage} object of this specific image.
* @throws IOException due to read the image from resources.
* @throws ImageComparisonException due to read the image from resources.
*/
public static BufferedImage readImageFromResources(String path) throws IOException {
public static BufferedImage readImageFromResources(String path) throws ImageComparisonException {
InputStream inputStream = ImageComparisonUtil.class.getClassLoader().getResourceAsStream(path);
if (inputStream != null) {
return ImageIO.read(inputStream);
try {
return ImageIO.read(inputStream);
} catch (IOException e) {
throw new ImageComparisonException("Can not read image from the file, path=" + path, e);
}
} else {
throw new IOException("Image " + path + " not found");
throw new ImageComparisonException("Image " + path + " not found");
}
}

Expand All @@ -79,27 +82,35 @@ public static BufferedImage readImageFromResources(String path) throws IOExcepti
*
* @param path the path where contains image.
* @return the {@link BufferedImage} object of this specific image.
* @throws IOException due to read the image from FS.
* @throws ImageComparisonException due to read the image from FS.
*/
public static BufferedImage readImageFromFile(File path) throws IOException {
return ImageIO.read(path);
public static BufferedImage readImageFromFile(File path) throws ImageComparisonException {
try {
return ImageIO.read(path);
} catch (IOException e) {
throw new ImageComparisonException("Can not read file from path=" + path.getAbsolutePath(), e);
}
}

/**
* Save image to the provided path.
*
* @param pathFile the path to the saving image.
* @param image the {@link BufferedImage} object of this specific image.
* @throws IOException due to save image.
* @throws ImageComparisonException due to save image.
*/
public static void saveImage(File pathFile, BufferedImage image) throws IOException {
public static void saveImage(File pathFile, BufferedImage image) throws ImageComparisonException {
File dir = pathFile.getParentFile();
// make dir if it's not using from Gradle.
boolean dirExists = dir == null || dir.isDirectory() || dir.mkdirs();
if (!dirExists) {
throw new FileNotFoundException("Unable to create directory " + dir);
throw new ImageComparisonException("Unable to create directory " + dir);
}
try {
ImageIO.write(image, "png", pathFile);
} catch (IOException e) {
throw new ImageComparisonException("Can not save image to path=" + pathFile.getAbsolutePath(), e);
}
ImageIO.write(image, "png", pathFile);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/github/romankh3/image/comparison/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import static com.github.romankh3.image.comparison.CommandLineUsage.handleResult;

import java.awt.image.BufferedImage;
import java.io.IOException;

/**
* Main class for running image-comparison from commandline.
*/
public class Main {

public static void main(String[] args) throws IOException {
public static void main(String[] args) {
CommandLineUsage commandLineUsage = new CommandLineUsage();
ImageComparison imgCmp = commandLineUsage.create(args);
BufferedImage result = imgCmp.compareImages().getResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.github.romankh3.image.comparison.ImageComparisonUtil;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
* Data transfer objects which contains all the needed data for result of the comparison.
Expand Down Expand Up @@ -40,7 +39,6 @@ public class ComparisonResult {
* @param expected expected {@link BufferedImage} object.
* @param actual actual {@link BufferedImage} object.
* @param differencePercent the percent of the differences between images.
*
* @return instance of the {@link ComparisonResult} object.
*/
public static ComparisonResult defaultSizeMisMatchResult(BufferedImage expected, BufferedImage actual,
Expand Down Expand Up @@ -88,9 +86,8 @@ public static ComparisonResult defaultMatchResult(BufferedImage expected, Buffer
*
* @param file the provided {@link File} object.
* @return this {@link ComparisonResult} object.
* @throws IOException due to save image.
*/
public ComparisonResult writeResultTo(File file) throws IOException {
public ComparisonResult writeResultTo(File file) {
ImageComparisonUtil.saveImage(file, result);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@ public void assertImagesEqual(BufferedImage expected, BufferedImage actual) {
}
}

protected Throwable getException(Runnable action) {
try {
action.run();
return null;
} catch (Throwable ex) {
return ex;
}
}

}

0 comments on commit 5f701f7

Please sign in to comment.