-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadImage.java
70 lines (60 loc) · 1.79 KB
/
LoadImage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
/**
* Helper class for loading images from disk
*
* @author Richard
* @version Dec 30, 2016
* @author Project: Brick-Pop-Solver
*
*/
public class LoadImage
{
/**
* Helper method for reading images from disk
*
* @param fileName
* path to image
* @return integer array of RGB values representing an image's pixels
* @throws IOException
*/
public static int[][] getImage( String fileName ) throws IOException
{
BufferedImage image = ImageIO.read( new File( fileName ) );
int[][] result = new int[image.getWidth()][image.getHeight()];
for ( int x = 0; x < image.getWidth(); x++ )
{
for ( int y = 0; y < image.getHeight(); y++ )
{
result[x][y] = image.getRGB( x, y );
}
}
return result;
}
/**
* Uses black magic to pick out the proper pixels to define our starting
* Board
*
* @param fileName
* path to input image - make sure this is properly cropped
* @return an 10x10 array of Colors representing bricks
* @throws IOException
*/
public static Color[][] getBoard( String fileName ) throws IOException
{
int[][] data = getImage( fileName );
Color[][] img = new Color[10][10];
int width = data.length;
int height = data[0].length;
for ( int x = 5; x < 100; x += 10 )
{
for ( int y = 95; y >= 0; y -= 10 )
{
img[9 - ( y - 5 ) / 10][( ( x - 5 ) / 10 )] = new Color(
"" + data[(int)( ( 0. + x ) / 100 * width )][(int)( ( 0. + y ) / 100 * height )] );
}
}
return img;
}
}