Skip to content

Commit

Permalink
#11: first step.
Browse files Browse the repository at this point in the history
  • Loading branch information
romankh3 committed Mar 13, 2019
1 parent b54a110 commit c6ae80a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 74 deletions.
45 changes: 45 additions & 0 deletions src/main/java/ua/comparison/image/CommandLineUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ua.comparison.image;

import static ua.comparison.image.ImageComparisonTools.readImageFromFile;
import static ua.comparison.image.ImageComparisonTools.readImageFromResources;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Optional;
import ua.comparison.image.ArgsParser.Arguments;

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

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

static ImageComparison createDefault() throws IOException, URISyntaxException {
return new ImageComparison(
readImageFromResources("image1.png"),
readImageFromResources("image2.png"),
null);
}

static ImageComparison create(ArgsParser.Arguments args) throws IOException {
return new ImageComparison(
readImageFromFile(args.getImage1()),
readImageFromFile(args.getImage2()),
args.getDestinationImage().orElse(null));
}

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

}
99 changes: 42 additions & 57 deletions src/main/java/ua/comparison/image/ImageComparison.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ua.comparison.image;

import static java.awt.Color.RED;
import static ua.comparison.image.CommandLineUtil.create;
import static ua.comparison.image.CommandLineUtil.handleResult;
import static ua.comparison.image.ImageComparisonTools.checkCorrectImageSize;
import static ua.comparison.image.ImageComparisonTools.createGUI;
import static ua.comparison.image.ImageComparisonTools.createRectangle;
import static ua.comparison.image.ImageComparisonTools.deepCopy;
import static ua.comparison.image.ImageComparisonTools.populateTheMatrixOfTheDifferences;
import static ua.comparison.image.ImageComparisonTools.readImageFromFile;
import static ua.comparison.image.ImageComparisonTools.readImageFromResources;
import static ua.comparison.image.ImageComparisonTools.saveImage;

Expand All @@ -16,6 +17,8 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import ua.comparison.image.model.Rectangle;

Expand All @@ -37,9 +40,18 @@ public class ImageComparison {
*/
private int regionCount = counter;

/**
* First image for comparing
*/
private final BufferedImage image1;

/**
* Second image for comparing
*/
private final BufferedImage image2;

private final /* @Nullable */ File destination;

private int[][] matrix;

ImageComparison(String image1, String image2) throws IOException, URISyntaxException {
Expand All @@ -59,52 +71,12 @@ public ImageComparison(BufferedImage image1, BufferedImage image2, File destinat
this.destination = destination;
}

public BufferedImage getImage1() {
return image1;
}

public BufferedImage getImage2() {
return image2;
}

Optional<File> getDestination() {
return Optional.ofNullable(destination);
}

public static void main(String[] args) throws IOException, URISyntaxException {
ImageComparison imgCmp = create(args);
BufferedImage result = imgCmp.compareImages();
handleResult(imgCmp, (file) -> saveImage(file, result), () -> createGUI(result));
}

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

static ImageComparison createDefault() throws IOException, URISyntaxException {
return new ImageComparison(
readImageFromResources("image1.png"),
readImageFromResources("image2.png"),
null);
}

static ImageComparison create(ArgsParser.Arguments args) throws IOException {
return new ImageComparison(
readImageFromFile(args.getImage1()),
readImageFromFile(args.getImage2()),
args.getDestinationImage().orElse(null));
}

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

/**
* Draw rectangles which cover the regions of the difference pixels.
*
Expand All @@ -122,32 +94,32 @@ public BufferedImage compareImages() throws IOException {
graphics.setColor(RED);

groupRegions();
drawRectangles(graphics);

List<Rectangle> rectangles = new ArrayList<>();
while (counter <= regionCount) {
rectangles.add(createRectangle(matrix, counter));
counter++;
}


avoidOverlapping(rectangles).forEach(rectangle -> graphics.drawRect(rectangle.getMinY(),
rectangle.getMinX(),
rectangle.getWidth(),
rectangle.getHeight()));

//save the image:
saveImage(this.getDestination().orElse(Files.createTempFile("image-comparison", ".png").toFile()), outImg);
return outImg;
}

/**
* Draw rectangles with the differences pixels.
*
* @param graphics the Graphics2D object for drawing rectangles.
*/
private void drawRectangles(Graphics2D graphics) {
if (counter > regionCount) {
return;
}

Rectangle rectangle = createRectangle(matrix, counter);
//todo implement logic for overlapping.
private List<Rectangle> avoidOverlapping(List<Rectangle> rectangles) {

graphics.drawRect(rectangle.getMinY(), rectangle.getMinX(), rectangle.getWidth(), rectangle.getHeight());
counter++;
drawRectangles(graphics);
return rectangles;
}

/**
* Group rectangle regions in binary matrix.
* Group rectangle regions in matrix.
*/
private void groupRegions() {
for (int row = 0; row < matrix.length; row++) {
Expand Down Expand Up @@ -175,6 +147,7 @@ private void joinToRegion(int row, int col) {

matrix[row][col] = regionCount;

//todo refactor it to make it faster.
for (int i = 0; i < threshold; i++) {
// goes to all directions.
joinToRegion(row - 1 - i, col);
Expand All @@ -188,4 +161,16 @@ private void joinToRegion(int row, int col) {
joinToRegion(row + 1 + i, col + 1 + i);
}
}

Optional<File> getDestination() {
return Optional.ofNullable(destination);
}

public BufferedImage getImage1() {
return image1;
}

public BufferedImage getImage2() {
return image2;
}
}
21 changes: 11 additions & 10 deletions src/main/java/ua/comparison/image/ImageComparisonTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,29 @@ public static boolean isDifferent(int rgb1, int rgb2) {
* @return the {@link Rectangle} object.
*/
public static Rectangle createRectangle(int[][] matrix, int counter) {
Rectangle rect = new Rectangle();
Rectangle rectangle = new Rectangle();

//todo refactor it to make it faster.
for (int y = 0; y < matrix.length; y++) {
for (int x = 0; x < matrix[0].length; x++) {
if (matrix[y][x] == counter) {
if (x < rect.getMinX()) {
rect.setMinX(x);
if (x < rectangle.getMinX()) {
rectangle.setMinX(x);
}
if (x > rect.getMaxX()) {
rect.setMaxX(x);
if (x > rectangle.getMaxX()) {
rectangle.setMaxX(x);
}

if (y < rect.getMinY()) {
rect.setMinY(y);
if (y < rectangle.getMinY()) {
rectangle.setMinY(y);
}
if (y > rect.getMaxY()) {
rect.setMaxY(y);
if (y > rectangle.getMaxY()) {
rectangle.setMaxY(y);
}
}
}
}
return rect;
return rectangle;
}

/**
Expand Down
23 changes: 16 additions & 7 deletions src/test/java/ua/comparison/image/ImageComparisonUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,18 @@ public void testIssue17() throws IOException, URISyntaxException {
new ImageComparison( "b1#17.png", "b2#17.png" ).compareImages();
}

/**
* Test issue #11.
*/
@Test
public void testIssue11() throws IOException, URISyntaxException {
// TODO: 3/13/2019 Implemented logic
new ImageComparison("image1.png", "image3.png").compareImages();
}

@Test
public void testCreateDefault() throws IOException, URISyntaxException {
ImageComparison comparison = ImageComparison.createDefault();
ImageComparison comparison = CommandLineUtil.createDefault();
assertImagesEqual(readImageFromResources("image1.png" ), comparison.getImage1());
assertImagesEqual(readImageFromResources("image2.png" ), comparison.getImage2());
assertFalse(comparison.getDestination().isPresent());
Expand All @@ -63,7 +72,7 @@ public void testCreateDefault() throws IOException, URISyntaxException {
public void testCreateWithTwoArgs() throws IOException, URISyntaxException {
File image1 = new File( ImageComparison.class.getClassLoader().getResource ( "image1.png" ).toURI().getPath() );
File image2 = new File( ImageComparison.class.getClassLoader().getResource ( "image2.png" ).toURI().getPath() );
ImageComparison comparison = ImageComparison.create(image1.getAbsolutePath(), image2.getAbsolutePath());
ImageComparison comparison = CommandLineUtil.create(image1.getAbsolutePath(), image2.getAbsolutePath());

assertImagesEqual(readImageFromResources("image1.png" ), comparison.getImage1());
assertImagesEqual(readImageFromResources("image2.png" ), comparison.getImage2());
Expand All @@ -74,7 +83,7 @@ public void testCreateWithTwoArgs() throws IOException, URISyntaxException {
public void testCreateWithTwoImagesAsArgs() throws IOException, URISyntaxException {
File image1 = new File( ImageComparison.class.getClassLoader().getResource ( "image1.png" ).toURI().getPath() );
File image2 = new File( ImageComparison.class.getClassLoader().getResource ( "image2.png" ).toURI().getPath() );
ImageComparison comparison = ImageComparison.create(new ArgsParser.Arguments(image1, image2, null));
ImageComparison comparison = CommandLineUtil.create(new ArgsParser.Arguments(image1, image2, null));

assertImagesEqual(readImageFromResources("image1.png" ), comparison.getImage1());
assertImagesEqual(readImageFromResources("image2.png" ), comparison.getImage2());
Expand All @@ -86,7 +95,7 @@ public void testCreateWithTwoImagesAndDestinationFileAsArgs() throws IOException
File image1 = new File( ImageComparison.class.getClassLoader().getResource ( "image1.png" ).toURI().getPath() );
File image2 = new File( ImageComparison.class.getClassLoader().getResource ( "image2.png" ).toURI().getPath() );
File destination = Files.createTempFile("image-comparison-test", ".png").toFile();
ImageComparison comparison = ImageComparison.create(new ArgsParser.Arguments(image1, image2, destination));
ImageComparison comparison = CommandLineUtil.create(new ArgsParser.Arguments(image1, image2, destination));

assertImagesEqual(readImageFromResources("image1.png" ), comparison.getImage1());
assertImagesEqual(readImageFromResources("image2.png" ), comparison.getImage2());
Expand All @@ -100,7 +109,7 @@ public void resultIsHandledCorrectlyWhenItShouldShowUI() throws IOException, URI
AtomicBoolean savedToFile = new AtomicBoolean(false);
AtomicBoolean showUI = new AtomicBoolean(false);

ImageComparison.handleResult(comparison, file -> savedToFile.set(true), () -> showUI.set(true));
CommandLineUtil.handleResult(comparison, file -> savedToFile.set(true), () -> showUI.set(true));

assertFalse(savedToFile.get());
assertTrue(showUI.get());
Expand All @@ -111,12 +120,12 @@ public void resultIsHandledCorrectlyWhenItShouldSaveToFile() throws IOException,
File image1 = new File( ImageComparison.class.getClassLoader().getResource ( "image1.png" ).toURI().getPath() );
File image2 = new File( ImageComparison.class.getClassLoader().getResource ( "image2.png" ).toURI().getPath() );
File destination = Files.createTempFile("image-comparison-test", ".png").toFile();
ImageComparison comparison = ImageComparison.create(new ArgsParser.Arguments(image1, image2, destination));
ImageComparison comparison = CommandLineUtil.create(new ArgsParser.Arguments(image1, image2, destination));

AtomicBoolean savedToFile = new AtomicBoolean(false);
AtomicBoolean showUI = new AtomicBoolean(false);

ImageComparison.handleResult(comparison, file -> savedToFile.set(true), () -> showUI.set(true));
CommandLineUtil.handleResult(comparison, file -> savedToFile.set(true), () -> showUI.set(true));

assertTrue(savedToFile.get());
assertFalse(showUI.get());
Expand Down

0 comments on commit c6ae80a

Please sign in to comment.