-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageData.java
95 lines (85 loc) · 2.75 KB
/
ImageData.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*************************************************************************
* Copyright 2012 Benedict J. Brown
*
* Compilation: javac ImageData.java
* Execution: java ImageData imagename
*
* Library for reading an image into a 2D array of ints, where each
* int represents a pixel in Java's ARGB format.
*
* % java ImageData mandrill.jpg
*
* Remarks
* -------
* - img[y][x] is row y, column x, where (0, 0) is upper left
*
* - img stores each pixel in an int as 0RGB (i.e. alpha is forced to 0)
* in order to guarantee that pixel values are always positive
*
* - relies on the Picture class from the introcs standard library
*
* - note that Picture's pixel() accesses pixels as (col,row) or (x,y),
* which is the transpose of what load provides
*
*************************************************************************/
public class ImageData {
/* Load an image from filename, and return it
* as a 2D array of integers in row-major order
*
* and empty, 0x0 array is returned on errors
*/
public static int[][] load(String filename) {
try {
Picture p = new Picture(filename);
int[][] img = new int[p.height()][p.width()];
for (int row = 0; row < p.height(); row++) {
for (int col = 0; col < p.width(); col++) {
img[row][col] = p.get(col, row).getRGB() & 0xFFFFFF;
}
}
return img;
} catch (RuntimeException e) {
return new int[0][0];
}
}
/* Create a Picture object from image data
* in a 2D array of ints. This is primarily
* a helper method for show() and save(),
* but is declared public because it is
* useful in its own right.
*/
public static Picture getPicture(int[][] img) {
if (img == null) return null;
if (img.length == 0) return null;
if (img[0].length == 0) return null;
Picture p = new Picture(img[0].length, img.length);
for (int row = 0; row < img.length; row++) {
for (int col = 0; col < img[0].length; col++) {
p.set(col, row, new java.awt.Color(img[row][col]));
}
}
return p;
}
/* Display img in a window
*/
public static void show(int[][] img) {
Picture p = getPicture(img);
if (p != null) p.show();
}
/* Save img to filename. The format is determined
* by the file's extension.
*/
public static void save(int[][] img, String filename) {
Picture p = getPicture(img);
if (p != null) p.save(filename);
}
/* Load an image, print its dimensions, and display
* it for testing purposes and as a simple image viewer.
*/
public static void main(String[] args) {
int[][] img = load(args[0]);
System.out.printf("Image %s: width = %d, height = %d\n",
args[0], img[0].length, img.length);
show(img);
}
}