Skip to content

Commit e4fddfe

Browse files
authored
Merge pull request #8 from romankh3/refactoring
Refactoring.
2 parents 8627661 + d7e877a commit e4fddfe

File tree

4 files changed

+106
-26
lines changed

4 files changed

+106
-26
lines changed

src/main/java/ua/comparison/image/ImageComparison.java

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package ua.comparison.image;
22

3-
import javax.imageio.ImageIO;
43
import java.awt.*;
54
import java.awt.image.BufferedImage;
6-
import java.io.File;
75
import java.io.IOException;
86
import java.net.URISyntaxException;
97

@@ -65,24 +63,9 @@ public static BufferedImage drawTheDifference( String image1Name, String image2N
6563
private static void drawRectangles( int[][] matrix, Graphics2D graphics, int counter, int lastNumberCount ) {
6664
if( counter > lastNumberCount ) return;
6765

68-
int minX = Integer.MAX_VALUE;
69-
int minY = Integer.MAX_VALUE;
70-
int maxX = Integer.MIN_VALUE;
71-
int maxY = Integer.MIN_VALUE;
66+
Rectangle rectangle = createRectangle( matrix, counter );
7267

73-
for ( int y = 0; y < matrix.length; y++ ) {
74-
for ( int x = 0; x < matrix[0].length; x++ ) {
75-
if ( matrix[y][x] == counter ) {
76-
77-
if ( x < minX ) minX = x;
78-
if ( x > maxX ) maxX = x;
79-
80-
if ( y < minY ) minY = y;
81-
if ( y > maxY ) maxY = y;
82-
}
83-
}
84-
}
85-
graphics.drawRect( minY, minX, maxY - minY, maxX - minX );
68+
graphics.drawRect( rectangle.getMinY(), rectangle.getMinX(), rectangle.getWidth(), rectangle.getHeight() );
8669
drawRectangles( matrix, graphics, ++counter, lastNumberCount );
8770
}
8871

@@ -93,15 +76,10 @@ private static void drawRectangles( int[][] matrix, Graphics2D graphics, int cou
9376
* @return populated binary matrix.
9477
*/
9578
public static int[][] populateTheMatrixOfTheDifferences(BufferedImage image1, BufferedImage image2 ) {
96-
9779
int[][] matrix = new int[image1.getWidth()][image1.getHeight()];
9880
for ( int y = 0; y < image1.getHeight(); y++ ) {
9981
for ( int x = 0; x < image1.getWidth(); x++ ) {
100-
if ( isDifferent( x, y, image1, image2 ) ) {
101-
matrix[x][y] = 1;
102-
} else {
103-
matrix[x][y] = 0;
104-
}
82+
matrix[x][y] = isDifferent( x, y, image1, image2 ) ? 1 : 0;
10583
}
10684
}
10785
return matrix;
@@ -110,7 +88,7 @@ public static int[][] populateTheMatrixOfTheDifferences(BufferedImage image1, Bu
11088
/**
11189
* Group rectangle regions in binary matrix.
11290
* @param matrix The binary matrix.
113-
* @return the last number which marks the lat region.
91+
* @return the last number which marks the last region.
11492
*/
11593
private static int groupRegions( int[][] matrix ) {
11694
int regionCount = 2;

src/main/java/ua/comparison/image/ImageComparisonTools.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public static Frame createGUI(BufferedImage image ) {
3232
return frame;
3333
}
3434

35+
/**
36+
* Make a copy of the {@code BufferedImage} object.
37+
* @param image the provided image.
38+
* @return copy of the provided image.
39+
*/
3540
static BufferedImage deepCopy( BufferedImage image ) {
3641
ColorModel cm = image.getColorModel();
3742
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
@@ -75,6 +80,29 @@ public static boolean isDifferent( int x, int y, BufferedImage image1, BufferedI
7580
return result;
7681
}
7782

83+
/**
84+
* Create a {@code Rectangle} object.
85+
* @param matrix the matrix of the Conformity pixels.
86+
* @param counter the number from marks regions.
87+
* @return the {@code Rectangle} object.
88+
*/
89+
public static Rectangle createRectangle( int[][] matrix, int counter ) {
90+
Rectangle rect = new Rectangle();
91+
92+
for ( int y = 0; y < matrix.length; y++ ) {
93+
for ( int x = 0; x < matrix[0].length; x++ ) {
94+
if ( matrix[y][x] == counter ) {
95+
if ( x < rect.getMinX() ) rect.setMinX( x );
96+
if ( x > rect.getMaxX() ) rect.setMaxX( x );
97+
98+
if ( y < rect.getMinY() ) rect.setMinY( y );
99+
if ( y > rect.getMaxY() ) rect.setMaxY( y );
100+
}
101+
}
102+
}
103+
return rect;
104+
}
105+
78106
/**
79107
* Reads image from the provided path.
80108
* @param path the path where contains image.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ua.comparison.image;
2+
3+
/**
4+
* Object contained data for a rectangle.
5+
*/
6+
public class Rectangle {
7+
8+
private int minX = Integer.MAX_VALUE;
9+
private int minY = Integer.MAX_VALUE;
10+
private int maxX = Integer.MIN_VALUE;
11+
private int maxY = Integer.MIN_VALUE;
12+
13+
public void setMinX(int minX) {
14+
this.minX = minX;
15+
}
16+
17+
public void setMinY(int minY) {
18+
this.minY = minY;
19+
}
20+
21+
public void setMaxX(int maxX) {
22+
this.maxX = maxX;
23+
}
24+
25+
public void setMaxY(int maxY) {
26+
this.maxY = maxY;
27+
}
28+
29+
public int getMinX() {
30+
return minX;
31+
}
32+
33+
public int getMinY() { return minY; }
34+
35+
public int getMaxX() { return maxX; }
36+
37+
public int getMaxY() { return maxY; }
38+
39+
public int getWidth() { return maxY - minY; }
40+
41+
public int getHeight() { return maxX - minX; }
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ua.comparison.image;
2+
3+
import org.junit.Test;
4+
import ua.comparison.image.Rectangle;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
/**
9+
* Unit-level testing for {@link Rectangle} object.
10+
*/
11+
public class RectangleUnitTest {
12+
13+
@Test
14+
public void testGetterSetter() {
15+
Rectangle rectangle = new Rectangle();
16+
17+
assertEquals( rectangle.getMinX(), Integer.MAX_VALUE );
18+
assertEquals( rectangle.getMinY(), Integer.MAX_VALUE );
19+
assertEquals( rectangle.getMaxX(), Integer.MIN_VALUE );
20+
assertEquals( rectangle.getMaxY(), Integer.MIN_VALUE );
21+
22+
rectangle.setMinX( 10 );
23+
rectangle.setMinY( 20 );
24+
rectangle.setMaxX( 30 );
25+
rectangle.setMaxY( 40 );
26+
27+
assertEquals( rectangle.getMinX(), 10 );
28+
assertEquals( rectangle.getMinY(), 20 );
29+
assertEquals( rectangle.getMaxX(), 30 );
30+
assertEquals( rectangle.getMaxY(), 40 );
31+
}
32+
}

0 commit comments

Comments
 (0)