-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageUtils.java
187 lines (170 loc) · 6.08 KB
/
ImageUtils.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* ImageUtils.java */
/* DO NOT CHANGE THIS FILE. */
/* YOUR SUBMISSION MUST WORK CORRECTLY WITH _OUR_ COPY OF THIS FILE. */
/* You may wish to make temporary changes or insert println() statements */
/* while testing your code. When you're finished testing and debugging, */
/* though, make sure your code works with the original version of this file. */
/**
* The ImageUtils class reads and writes TIFF file, converting to and from
* pixel arrays in PixImage format or run-length encodings in
* RunLengthEncoding format. Methods are also included for displaying images
* in PixImage format.
*
* @author Joel Galenson
**/
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.media.jai.JAI;
import javax.media.jai.RenderedImageAdapter;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
* ImageUtils contains utilities for reading, writing, and displaying images.
*
* It uses JAI to read and write TIFF files, as the standard libraries cannot
* read them.
*
* All image data is in RGB format (see BufferedImage.getRGB).
*/
public class ImageUtils {
/**
* buffer2PixImage() converts a BufferedImage to a PixImage.
* @param bImage the image to convert.
* @return a PixImage with the same pixels as the BufferedImage.
*/
private static PixImage buffer2PixImage(BufferedImage bImage) {
PixImage pImage = new PixImage(bImage.getWidth(), bImage.getHeight());
for (int x = 0; x < bImage.getWidth(); x++) {
for (int y = 0; y < bImage.getHeight(); y++) {
Color color = new Color(bImage.getRGB(x, y));
pImage.setPixel(x, y, (short) color.getRed(), (short) color.getGreen(),
(short) color.getBlue());
}
}
return pImage;
}
/**
* pixImage2buffer() converts a PixImage to a BufferedImage.
* @param pImage the image to convert.
* @return a BufferedImage with the same pixels as the PixImage.
*/
static BufferedImage pixImage2buffer(PixImage pImage) {
BufferedImage bImage = new BufferedImage(pImage.getWidth(),
pImage.getHeight(),
BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < bImage.getWidth(); x++) {
for (int y = 0; y < bImage.getHeight(); y++) {
bImage.setRGB(x, y, new Color(pImage.getRed(x, y),
pImage.getGreen(x, y),
pImage.getBlue(x, y)).getRGB());
}
}
return bImage;
}
/**
* readTIFF() reads an image from a file and formats it as a BufferedImage.
* @param filename the name of the file to read.
* @return a BufferedImage of the file
*/
private static BufferedImage readTIFF(String filename) {
return (new RenderedImageAdapter(JAI.create("fileload", filename)))
.getAsBufferedImage();
}
/**
* readTIFFPix() reads an image from a file and formats it as a PixImage.
* @param filename the name of the file to read.
* @return a PixImage of the file
*/
public static PixImage readTIFFPix(String filename) {
return buffer2PixImage(readTIFF(filename));
}
/**
* readTIFFRLE() reads an image from a file and formats it as a run-length
* encoding.
* @param filename the name of the file to read.
* @return a RunLengthEncoding of the file.
*/
public static RunLengthEncoding readTIFFRLE(String filename) {
return new RunLengthEncoding(readTIFFPix(filename));
}
/**
* writeTIFF() writes a BufferedImage to a specified file in TIFF format.
* @param rle the input BufferedImage.
* @param filename the output filename.
*/
private static void writeTIFF(BufferedImage image, String filename) {
JAI.create("filestore", image, filename, "tiff");
}
/**
* writeTIFF() writes a PixImage to a specified file in TIFF format.
* @param image the input PixImage.
* @param filename the output filename.
*/
public static void writeTIFF(PixImage image, String filename) {
writeTIFF(pixImage2buffer(image), filename);
}
/**
* writeTIFF() writes a run-length encoding to a specified file in TIFF
* format.
* @param rle the input run-length encoded image.
* @param filename the output filename.
*/
public static void writeTIFF(RunLengthEncoding rle, String filename) {
writeTIFF(rle.toPixImage(), filename);
}
/**
* displayFrame displays a JFrame and pauses until the window is closed.
* @param frame a JFrame to display.
*/
private static void displayFrame(final JFrame frame) {
try {
synchronized (ImageUtils.class) {
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent event) {
synchronized (ImageUtils.class) {
ImageUtils.class.notify();
frame.dispose();
}
}
});
frame.pack();
frame.setVisible(true);
ImageUtils.class.wait();
}
} catch (InterruptedException e) {
System.out.println("Interrupted Exception in displayFrame().");
e.printStackTrace();
}
}
/**
* displayTIFFs displays a sequence of PixImages and pauses until the window
* is closed.
* @param images an array of PixImages to display.
*/
public static void displayTIFFs(PixImage[] images) {
JFrame frame = new JFrame();
Box box = Box.createHorizontalBox();
for (int i = 0; i < images.length; i++) {
box.add(new JLabel(new ImageIcon(pixImage2buffer(images[i]))));
if (i < images.length - 1) {
box.add(Box.createHorizontalStrut(10));
}
}
frame.add(box);
displayFrame(frame);
}
/**
* displayTIFF displays a PixIamge and pauses until the window is closed.
* @param image the PixImage to display.
*/
public static void displayTIFF(PixImage image) {
displayTIFFs(new PixImage[] { image });
}
}