This repository has been archived by the owner on Mar 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPicture.java
462 lines (422 loc) · 16.6 KB
/
Picture.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package a4;
/******************************************************************************
* Compilation: javac Picture.java
* Execution: java Picture imagename
* Dependencies: none
*
* Data type for manipulating individual pixels of an image. The original
* image can be read from a file in JPG, GIF, or PNG format, or the
* user can create a blank image of a given dimension. Includes methods for
* displaying the image in a window on the screen or saving to a file.
*
* % java Picture mandrill.jpg
*
* Remarks
* -------
* - pixel (x, y) is column x and row y, where (0, 0) is upper left
*
******************************************************************************/
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
/**
* This class provides methods for manipulating individual pixels of
* an image using the RGB color format. The alpha component (for transparency)
* is not currently supported.
* The original image can be read from a {@code PNG}, {@code GIF},
* or {@code JPEG} file or the user can create a blank image of a given dimension.
* This class includes methods for displaying the image in a window on
* the screen or saving it to a file.
* <p>
* Pixel (<em>col</em>, <em>row</em>) is column <em>col</em> and row <em>row</em>.
* By default, the origin (0, 0) is the pixel in the top-left corner,
* which is a common convention in image processing.
* The method {@link #setOriginLowerLeft()} change the origin to the lower left.
* <p>
* The {@code get()} and {@code set()} methods use {@link Color} objects to get
* or set the color of the specified pixel.
* The {@code getRGB()} and {@code setRGB()} methods use a 32-bit {@code int}
* to encode the color, thereby avoiding the need to create temporary
* {@code Color} objects. The red (R), green (G), and blue (B) components
* are encoded using the least significant 24 bits.
* Given a 32-bit {@code int} encoding the color, the following code extracts
* the RGB components:
* <blockquote><pre>
* int r = (rgb >> 16) & 0xFF;
* int g = (rgb >> 8) & 0xFF;
* int b = (rgb >> 0) & 0xFF;
* </pre></blockquote>
* Given the RGB components (8-bits each) of a color,
* the following statement packs it into a 32-bit {@code int}:
* <blockquote><pre>
* int rgb = (r << 16) + (g << 8) + (b << 0);
* </pre></blockquote>
* <p>
* A <em>W</em>-by-<en>H</em> picture uses ~ 4 <em>W H</em> bytes of memory,
* since the color of each pixel is encoded as a 32-bit <code>int</code>.
* <p>
* For additional documentation, see
* <a href="https://introcs.cs.princeton.edu/31datatype">Section 3.1</a> of
* <i>Computer Science: An Interdisciplinary Approach</i>
* by Robert Sedgewick and Kevin Wayne.
* See {@link GrayscalePicture} for a version that supports grayscale images.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public final class Picture implements ActionListener {
private BufferedImage image; // the rasterized image
private JFrame frame; // on-screen view
private String filename; // name of file
private boolean isOriginUpperLeft = true; // location of origin
private final int width, height; // width and height
/**
* Creates a {@code width}-by-{@code height} picture, with {@code width} columns
* and {@code height} rows, where each pixel is black.
*
* @param width the width of the picture
* @param height the height of the picture
* @throws IllegalArgumentException if {@code width} is negative
* @throws IllegalArgumentException if {@code height} is negative
*/
public Picture(int width, int height) {
if (width < 0) throw new IllegalArgumentException("width must be non-negative");
if (height < 0) throw new IllegalArgumentException("height must be non-negative");
this.width = width;
this.height = height;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// set to TYPE_INT_ARGB here and in next constructor to support transparency
}
/**
* Creates a new picture that is a deep copy of the argument picture.
*
* @param picture the picture to copy
* @throws IllegalArgumentException if {@code picture} is {@code null}
*/
public Picture(Picture picture) {
if (picture == null) throw new IllegalArgumentException("constructor argument is null");
width = picture.width();
height = picture.height();
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
filename = picture.filename;
isOriginUpperLeft = picture.isOriginUpperLeft;
for (int col = 0; col < width(); col++)
for (int row = 0; row < height(); row++)
image.setRGB(col, row, picture.image.getRGB(col, row));
}
/**
* Creates a picture by reading an image from a file or URL.
*
* @param filename the name of the file (.png, .gif, or .jpg) or URL.
* @throws IllegalArgumentException if cannot read image
* @throws IllegalArgumentException if {@code filename} is {@code null}
*/
public Picture(String filename) {
if (filename == null) throw new IllegalArgumentException("constructor argument is null");
this.filename = filename;
try {
// try to read from file in working directory
File file = new File(filename);
if (file.isFile()) {
image = ImageIO.read(file);
}
// now try to read from file in same directory as this .class file
else {
URL url = getClass().getResource(filename);
if (url == null) {
url = new URL(filename);
}
image = ImageIO.read(url);
}
if (image == null) {
throw new IllegalArgumentException("could not read image file: " + filename);
}
width = image.getWidth(null);
height = image.getHeight(null);
}
catch (IOException ioe) {
throw new IllegalArgumentException("could not open image file: " + filename, ioe);
}
}
/**
* Creates a picture by reading the image from a PNG, GIF, or JPEG file.
*
* @param file the file
* @throws IllegalArgumentException if cannot read image
* @throws IllegalArgumentException if {@code file} is {@code null}
*/
public Picture(File file) {
if (file == null) throw new IllegalArgumentException("constructor argument is null");
try {
image = ImageIO.read(file);
}
catch (IOException ioe) {
throw new IllegalArgumentException("could not open file: " + file, ioe);
}
if (image == null) {
throw new IllegalArgumentException("could not read file: " + file);
}
width = image.getWidth(null);
height = image.getHeight(null);
filename = file.getName();
}
/**
* Returns a {@link JLabel} containing this picture, for embedding in a {@link JPanel},
* {@link JFrame} or other GUI widget.
*
* @return the {@code JLabel}
*/
public JLabel getJLabel() {
if (image == null) return null; // no image available
ImageIcon icon = new ImageIcon(image);
return new JLabel(icon);
}
/**
* Sets the origin to be the upper left pixel. This is the default.
*/
public void setOriginUpperLeft() {
isOriginUpperLeft = true;
}
/**
* Sets the origin to be the lower left pixel.
*/
public void setOriginLowerLeft() {
isOriginUpperLeft = false;
}
/**
* Displays the picture in a window on the screen.
*/
@SuppressWarnings("deprecation")
public void show() {
// create the GUI for viewing the image if needed
if (frame == null) {
frame = new JFrame();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem menuItem1 = new JMenuItem(" Save... ");
menuItem1.addActionListener(this);
// use getMenuShortcutKeyMaskEx() in Java 10 (getMenuShortcutKeyMask() deprecated)
menuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
menu.add(menuItem1);
frame.setJMenuBar(menuBar);
frame.setContentPane(getJLabel());
// f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
if (filename == null) frame.setTitle(width + "-by-" + height);
else frame.setTitle(filename);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
// draw
frame.repaint();
}
/**
* Returns the height of the picture.
*
* @return the height of the picture (in pixels)
*/
public int height() {
return height;
}
/**
* Returns the width of the picture.
*
* @return the width of the picture (in pixels)
*/
public int width() {
return width;
}
private void validateRowIndex(int row) {
if (row < 0 || row >= height())
throw new IllegalArgumentException("row index must be between 0 and " + (height() - 1) + ": " + row);
}
private void validateColumnIndex(int col) {
if (col < 0 || col >= width())
throw new IllegalArgumentException("column index must be between 0 and " + (width() - 1) + ": " + col);
}
/**
* Returns the color of pixel ({@code col}, {@code row}) as a {@link java.awt.Color}.
*
* @param col the column index
* @param row the row index
* @return the color of pixel ({@code col}, {@code row})
* @throws IllegalArgumentException unless both {@code 0 <= col < width} and {@code 0 <= row < height}
*/
public Color get(int col, int row) {
validateColumnIndex(col);
validateRowIndex(row);
int rgb = getRGB(col, row);
return new Color(rgb);
}
/**
* Returns the color of pixel ({@code col}, {@code row}) as an {@code int}.
* Using this method can be more efficient than {@link #get(int, int)} because
* it does not create a {@code Color} object.
*
* @param col the column index
* @param row the row index
* @return the integer representation of the color of pixel ({@code col}, {@code row})
* @throws IllegalArgumentException unless both {@code 0 <= col < width} and {@code 0 <= row < height}
*/
public int getRGB(int col, int row) {
validateColumnIndex(col);
validateRowIndex(row);
if (isOriginUpperLeft) return image.getRGB(col, row);
else return image.getRGB(col, height - row - 1);
}
/**
* Sets the color of pixel ({@code col}, {@code row}) to given color.
*
* @param col the column index
* @param row the row index
* @param color the color
* @throws IllegalArgumentException unless both {@code 0 <= col < width} and {@code 0 <= row < height}
* @throws IllegalArgumentException if {@code color} is {@code null}
*/
public void set(int col, int row, Color color) {
validateColumnIndex(col);
validateRowIndex(row);
if (color == null) throw new IllegalArgumentException("color argument is null");
int rgb = color.getRGB();
setRGB(col, row, rgb);
}
/**
* Sets the color of pixel ({@code col}, {@code row}) to given color.
*
* @param col the column index
* @param row the row index
* @param rgb the integer representation of the color
* @throws IllegalArgumentException unless both {@code 0 <= col < width} and {@code 0 <= row < height}
*/
public void setRGB(int col, int row, int rgb) {
validateColumnIndex(col);
validateRowIndex(row);
if (isOriginUpperLeft) image.setRGB(col, row, rgb);
else image.setRGB(col, height - row - 1, rgb);
}
/**
* Returns true if this picture is equal to the argument picture.
*
* @param other the other picture
* @return {@code true} if this picture is the same dimension as {@code other}
* and if all pixels have the same color; {@code false} otherwise
*/
public boolean equals(Object other) {
if (other == this) return true;
if (other == null) return false;
if (other.getClass() != this.getClass()) return false;
Picture that = (Picture) other;
if (this.width() != that.width()) return false;
if (this.height() != that.height()) return false;
for (int col = 0; col < width(); col++)
for (int row = 0; row < height(); row++)
if (this.getRGB(col, row) != that.getRGB(col, row)) return false;
return true;
}
/**
* Returns a string representation of this picture.
* The result is a <code>width</code>-by-<code>height</code> matrix of pixels,
* where the color of a pixel is represented using 6 hex digits to encode
* the red, green, and blue components.
*
* @return a string representation of this picture
*/
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(width +"-by-" + height + " picture (RGB values given in hex)\n");
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
int rgb = 0;
if (isOriginUpperLeft) rgb = image.getRGB(col, row);
else rgb = image.getRGB(col, height - row - 1);
sb.append(String.format("#%06X ", rgb & 0xFFFFFF));
}
sb.append("\n");
}
return sb.toString().trim();
}
/**
* This operation is not supported because pictures are mutable.
*
* @return does not return a value
* @throws UnsupportedOperationException if called
*/
public int hashCode() {
throw new UnsupportedOperationException("hashCode() is not supported because pictures are mutable");
}
/**
* Saves the picture to a file in either PNG or JPEG format.
* The filetype extension must be either .png or .jpg.
*
* @param name the name of the file
* @throws IllegalArgumentException if {@code name} is {@code null}
*/
public void save(String name) {
if (name == null) throw new IllegalArgumentException("argument to save() is null");
save(new File(name));
filename = name;
}
/**
* Saves the picture to a file in a PNG or JPEG image format.
*
* @param file the file
* @throws IllegalArgumentException if {@code file} is {@code null}
*/
public void save(File file) {
if (file == null) throw new IllegalArgumentException("argument to save() is null");
filename = file.getName();
if (frame != null) frame.setTitle(filename);
String suffix = filename.substring(filename.lastIndexOf('.') + 1);
if ("jpg".equalsIgnoreCase(suffix) || "png".equalsIgnoreCase(suffix)) {
try {
ImageIO.write(image, suffix, file);
}
catch (IOException e) {
e.printStackTrace();
}
}
else {
System.out.println("Error: filename must end in .jpg or .png");
}
}
/**
* Opens a save dialog box when the user selects "Save As" from the menu.
*/
@Override
public void actionPerformed(ActionEvent e) {
FileDialog chooser = new FileDialog(frame,
"Use a .png or .jpg extension", FileDialog.SAVE);
chooser.setVisible(true);
if (chooser.getFile() != null) {
save(chooser.getDirectory() + File.separator + chooser.getFile());
}
}
/**
* Unit tests this {@code Picture} data type.
* Reads a picture specified by the command-line argument,
* and shows it in a window on the screen.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
}
}