Skip to content

Commit

Permalink
#11: start
Browse files Browse the repository at this point in the history
  • Loading branch information
romankh3 committed Apr 3, 2019
1 parent 8e1ee12 commit d4bfc36
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 68 deletions.
49 changes: 29 additions & 20 deletions src/main/java/ua/comparison/image/ImageComparison.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ua.comparison.image;

import static java.awt.Color.RED;
import static java.awt.Color.white;
import static ua.comparison.image.CommandLineUtil.create;
import static ua.comparison.image.CommandLineUtil.handleResult;
import static ua.comparison.image.ImageComparisonTools.checkCorrectImageSize;
Expand All @@ -21,8 +20,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import ua.comparison.image.model.Rectangle;

public class ImageComparison {
Expand All @@ -31,7 +28,7 @@ public class ImageComparison {
* The threshold which means the max distance between non-equal pixels.
* Could be changed according size and requirements to the image.
*/
public static int threshold = 5;
public static int threshold = 3;

/**
* The number which marks how many rectangles. Beginning from 2.
Expand Down Expand Up @@ -108,24 +105,33 @@ public BufferedImage compareImages() throws IOException {

groupRegions();

List<Rectangle> rectangles = populateRectangles();

drawRectangles(rectangles, graphics);

//save the image:
saveImage(this.getDestination().orElse(Files.createTempFile(NAME_PREFIX, NAME_SUFFIX).toFile()), outImg);
return outImg;
}

private List<Rectangle> populateRectangles() {
List<Rectangle> rectangles = new ArrayList<>();
while (counter <= regionCount) {
Rectangle rectangle = createRectangle(matrix, counter);
if(!rectangle.equals(Rectangle.createDefault())) {
if (!rectangle.equals(Rectangle.createDefault())) {
rectangles.add(createRectangle(matrix, counter));
}
counter++;
}

return rectangles;
}

private void drawRectangles(List<Rectangle> rectangles, Graphics2D graphics) {
rectangles.forEach(rectangle -> graphics.drawRect(rectangle.getMinY(),
rectangle.getMinX(),
rectangle.getWidth(),
rectangle.getHeight()));

//save the image:
saveImage(this.getDestination().orElse(Files.createTempFile(NAME_PREFIX, NAME_SUFFIX).toFile()), outImg);
return outImg;
rectangle.getMinX(),
rectangle.getWidth(),
rectangle.getHeight()));
}

/**
Expand All @@ -135,7 +141,7 @@ private void groupRegions() {
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
if (matrix[row][col] == 1) {
joinToRegion(row, col);
joinToRegion(row, col, threshold);
regionCount++;
}
}
Expand All @@ -150,20 +156,23 @@ private void groupRegions() {
* @param row the value of the row.
* @param col the value of the column.
*/
private void joinToRegion(int row, int col) {
private void joinToRegion(int row, int col, int localThreshold) {
if (row < 0 || row >= matrix.length || col < 0 || col >= matrix[row].length || matrix[row][col] != 1) {
return;
}

matrix[row][col] = regionCount;

for (int i = 0; i < threshold; i++) {
joinToRegion(row + 1 + i, col);
joinToRegion(row, col + 1 + i);
for (int i = 0; i < localThreshold; i++) {
// joinToRegion(row - 1 - i, col, 1);
joinToRegion(row + 1 + i, col, threshold);
// joinToRegion(row, col - 1 - i, 1);
joinToRegion(row, col + 1 + i, threshold);

joinToRegion(row + 1 + i, col - 1 - i);
joinToRegion(row - 1 - i, col + 1 + i);
joinToRegion(row + 1 + i, col + 1 + i);
// joinToRegion(row - 1 - i, col - 1 - i, 1);
joinToRegion(row + 1 + i, col - 1 - i, threshold);
joinToRegion(row - 1 - i, col + 1 + i, threshold);
joinToRegion(row + 1 + i, col + 1 + i, threshold);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ public class ImageComparisonToolsUnitTest {

@Test
public void testFrameMethod() throws IOException, URISyntaxException {
BufferedImage image = readImageFromResources( "result1.png" );
Frame resultFrame = createGUI( image );
assertEquals( image.getHeight(), resultFrame.getHeight() );
assertEquals( image.getWidth(), resultFrame.getWidth() );
BufferedImage image = readImageFromResources("result1.png");
Frame resultFrame = createGUI(image);
assertEquals(image.getHeight(), resultFrame.getHeight());
assertEquals(image.getWidth(), resultFrame.getWidth());
}

@Test( expected = IllegalArgumentException.class )
@Test(expected = IllegalArgumentException.class)
public void testCheckCorrectImageSize() {
BufferedImage image1 = new BufferedImage(10, 10, 10);
BufferedImage image2 = new BufferedImage(12, 12, 10);

ImageComparisonTools.checkCorrectImageSize( image1, image2 );
ImageComparisonTools.checkCorrectImageSize(image1, image2);
}

@Test
public void testSaveImage() throws IOException, URISyntaxException {
BufferedImage image = readImageFromResources( "result1.png" );
BufferedImage image = readImageFromResources("result1.png");
String path = "build/test/correct/save/image.png";
ImageComparisonTools.saveImage( new File( path ), image );
Assert.assertTrue( new File( path ).exists() );
ImageComparisonTools.saveImage(new File(path), image);
Assert.assertTrue(new File(path).exists());
}
}
58 changes: 20 additions & 38 deletions src/test/java/ua/comparison/image/ImageComparisonUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static ua.comparison.image.ImageComparisonTools.isDifferent;
import static ua.comparison.image.ImageComparisonTools.readImageFromResources;

import java.awt.image.BufferedImage;
Expand All @@ -14,7 +13,6 @@
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.Ignore;
import org.junit.Test;

Expand All @@ -31,26 +29,10 @@ public class ImageComparisonUnitTest {
@Test
public void testCorrectWorkingImage1Image2() throws IOException, URISyntaxException {
//given
BufferedImage expectedResultImage = readImageFromResources( "result1.png" );
BufferedImage expectedResultImage = readImageFromResources("result1.png");

//when
BufferedImage drawnDifferences = new ImageComparison( "image1.png", "image2.png" ).compareImages();

//then
assertImagesEqual(expectedResultImage, drawnDifferences);
}

/**
* Ignored dou to resolving problem with overlapping.
*/
@Ignore
@Test
public void testCorrectWorkingImage1Image() throws IOException, URISyntaxException {
//given
BufferedImage expectedResultImage = readImageFromResources( "result2.png" );

//when
BufferedImage drawnDifferences = new ImageComparison( "image1.png", "image3.png" ).compareImages();
BufferedImage drawnDifferences = new ImageComparison("image1.png", "image2.png").compareImages();

//then
assertImagesEqual(expectedResultImage, drawnDifferences);
Expand Down Expand Up @@ -79,49 +61,49 @@ public void testIssue21() throws IOException, URISyntaxException {
*/
@Test
public void testIssue11() throws IOException, URISyntaxException {
// TODO: 3/13/2019 Implemented logic
BufferedImage bufferedImage = new ImageComparison("image1.png", "image3.png").compareImages();
assertNotNull(bufferedImage);
}

@Test
public void testCreateDefault() throws IOException, URISyntaxException {
ImageComparison comparison = CommandLineUtil.createDefault();
assertImagesEqual(readImageFromResources("image1.png" ), comparison.getImage1());
assertImagesEqual(readImageFromResources("image2.png" ), comparison.getImage2());
assertImagesEqual(readImageFromResources("image1.png"), comparison.getImage1());
assertImagesEqual(readImageFromResources("image2.png"), comparison.getImage2());
assertFalse(comparison.getDestination().isPresent());
}

@Test
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() );
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 = CommandLineUtil.create(image1.getAbsolutePath(), image2.getAbsolutePath());

assertImagesEqual(readImageFromResources("image1.png" ), comparison.getImage1());
assertImagesEqual(readImageFromResources("image2.png" ), comparison.getImage2());
assertImagesEqual(readImageFromResources("image1.png"), comparison.getImage1());
assertImagesEqual(readImageFromResources("image2.png"), comparison.getImage2());
assertFalse(comparison.getDestination().isPresent());
}

@Test
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() );
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 = CommandLineUtil.create(new ArgsParser.Arguments(image1, image2, null));

assertImagesEqual(readImageFromResources("image1.png" ), comparison.getImage1());
assertImagesEqual(readImageFromResources("image2.png" ), comparison.getImage2());
assertImagesEqual(readImageFromResources("image1.png"), comparison.getImage1());
assertImagesEqual(readImageFromResources("image2.png"), comparison.getImage2());
assertFalse(comparison.getDestination().isPresent());
}

@Test
public void testCreateWithTwoImagesAndDestinationFileAsArgs() 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() );
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 = CommandLineUtil.create(new ArgsParser.Arguments(image1, image2, destination));

assertImagesEqual(readImageFromResources("image1.png" ), comparison.getImage1());
assertImagesEqual(readImageFromResources("image2.png" ), comparison.getImage2());
assertImagesEqual(readImageFromResources("image1.png"), comparison.getImage1());
assertImagesEqual(readImageFromResources("image2.png"), comparison.getImage2());
assertTrue(comparison.getDestination().isPresent());
assertEquals(destination, comparison.getDestination().get());
}
Expand All @@ -140,8 +122,8 @@ public void resultIsHandledCorrectlyWhenItShouldShowUI() throws IOException, URI

@Test
public void resultIsHandledCorrectlyWhenItShouldSaveToFile() 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() );
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 = CommandLineUtil.create(new ArgsParser.Arguments(image1, image2, destination));

Expand All @@ -159,7 +141,7 @@ private static void assertImagesEqual(BufferedImage imgA, BufferedImage imgB) {
fail("Images have different dimensions");
}

int width = imgA.getWidth();
int width = imgA.getWidth();
int height = imgA.getHeight();

for (int y = 0; y < height; y++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@
public class RectangleUnitTest {

@Test
public void testGetterSetter() {
public void testDefaultRectangle() {
Rectangle rectangle = Rectangle.createDefault();

assertEquals( rectangle.getMinX(), Integer.MAX_VALUE );
assertEquals( rectangle.getMinY(), Integer.MAX_VALUE );
assertEquals( rectangle.getMaxX(), Integer.MIN_VALUE );
assertEquals( rectangle.getMaxY(), Integer.MIN_VALUE );
}

@Test
public void testGetterSetter() {
Rectangle rectangle = Rectangle.createDefault();

rectangle.setMinX( 10 );
rectangle.setMinY( 20 );
Expand Down

0 comments on commit d4bfc36

Please sign in to comment.