- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHelper.java
More file actions
Latest commit
174 lines (155 loc) · 5.29 KB
/
Copy pathHelper.java
File metadata and controls
174 lines (155 loc) · 5.29 KB
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
packagemain;
importjavax.imageio.ImageIO;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
/**
* Provide simple tools to read, write and show pictures.
*/
publicfinalclassHelper {
/**
* Draws a rectangle over a RGB image
* @param r : an integer, the vertical coordinate (col) of the upper left corner.
* @param c : an integer, the horizontal coordinate (row) of the upper left corner.
* @param w : an integer, the width of the rectangle.
* @param h : an integer, the height of the rectangle.
* @param dst : a 2D integer array, the RGB image on which to draw the rectangle.
* @param color: an integer representing the RBG value of the line color of the rectangle
* @param strokeWidth: width of pencil stroke
*/
publicstaticvoiddrawBox(intr, intc, intw, inth, int[][] dst, intstrokeWidth, intcolor) {
if (strokeWidth < 1) strokeWidth = 1;
for (introw = r; row < r + h && row < dst.length; ++row) {
for (intcol = c; col < c + w && col < dst[0].length; ++col) {
if (row < r + strokeWidth || row >= r + h - strokeWidth ||
col < c + strokeWidth || col >= c + w - strokeWidth) {
dst[row][col] = color;
}
}
}
}
/**
* Draws a red rectangle over a RGB image
* @param r : an integer, the vertical coordinate (col) of the upper left corner.
* @param c : an integer, the horizontal coordinate (row) of the upper left corner.
* @param w : an integer, the width of the rectangle.
* @param h : an integer, the height of the rectangle.
* @param dst : a 2D integer array, the RGB image on which to draw the rectangle.
*/
publicstaticvoiddrawBox(intr, intc, intw, inth, int[][] dst) {
drawBox(r, c, w, h, dst, w/15, 255 << 16);
}
// Convert specified BufferedImage into an array
privatestaticint[][] fromBufferedImage(BufferedImageimage) {
intwidth = image.getWidth();
intheight = image.getHeight();
int[][] array = newint[height][width];
for (introw = 0; row < height; ++row) {
for (intcol = 0; col < width; ++col) {
array[row][col] = image.getRGB(col, row) & 0xffffffff;
}
}
returnarray;
}
// Convert specified array into a BufferedImage
privatestaticBufferedImagetoBufferedImage(int[][] array) {
intwidth = array[0].length;
intheight = array.length;
BufferedImageimage = newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (introw = 0; row < height; ++row) {
for (intcol = 0; col < width; ++col) {
image.setRGB(col, row, array[row][col] | 0xff000000);
}
}
returnimage;
}
/**
* Reads specified image from disk.
* @param path : a String, the Input file path
* @return HxW integer array of packed RGB colors, or <code>null</code> on failure
* @see #write
*/
publicstaticint[][] read(Stringpath) {
try {
BufferedImageimage = ImageIO.read(newFile(path));
returnfromBufferedImage(image);
} catch (IOExceptione) {
System.out.println(e);
System.out.println("Path: " + path);
System.exit(1);
returnnull;
}
}
/**
* Writes specified image to disk.
* @param path : a String, the Output file path
* @param array HxW array of packed RGB colors
* @return {@code true} if write operation was successful, {@code false} otherwise
* @see #read
*/
publicstaticbooleanwrite(Stringpath, int[][] array) {
// Convert array to Java image
BufferedImageimage = toBufferedImage(array);
// Get desired file format
intindex = path.lastIndexOf('.');
if (index < 0)
returnfalse;
Stringextension = path.substring(index + 1);
// Export image
try {
returnImageIO.write(image, extension, newFile(path));
} catch (IOExceptione) {
returnfalse;
}
}
/**
* Shows specified image in a window.
* @param array : a HxW integer array of packed RGB colors
* @param title : a String, the title to be displayed
*/
publicstaticvoidshow(int[][] array, Stringtitle) {
// Convert array to Java image
finalBufferedImageimage = toBufferedImage(array);
// Create a panel to render this image
@SuppressWarnings("serial")
JPanelpanel = newJPanel() {
@Override
protectedvoidpaintComponent(Graphicsg) {
super.paintComponent(g);
g.drawImage(image, 0, 0, Math.max(getWidth(), 100), Math.max(getHeight(), 100), null, null);
}
};
// Create a frame to hold this panel
finalJFrameframe = newJFrame(title);
frame.add(panel);
frame.getContentPane().setPreferredSize(newDimension(Math.max(image.getWidth(), 300), Math.max(image.getHeight(), 300)));
frame.pack();
// Register closing event
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(newWindowAdapter() {
@Override
publicvoidwindowClosing(WindowEvente) {
frame.setVisible(false);
synchronized (frame) {
frame.notifyAll();
}
}
});
// Show this frame
frame.setVisible(true);
// Wait for close operation
try {
synchronized (frame) {
while (frame.isVisible())
frame.wait();
}
} catch (InterruptedExceptione) {
// Empty on purpose
}
frame.dispose();
}
}