forked from ishanashah/Image-Manipulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JIP.java
422 lines (378 loc) · 14.8 KB
/
JIP.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
package assignment;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
import java.lang.reflect.Constructor;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
/**
* This class provides a GUI interface and handles all sorts of low-level details.
*/
public class JIP extends JFrame implements ActionListener {
public static void main(String[] args) {
(new JIP()).setVisible(true);
}
public JIP() {
effectMap = new HashMap<>();
setupGUI();
}
protected void setupGUI() {
// setup Frame elements
setTitle("Java Image Processor");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
menuBar.add(menu);
menuItemOpenImage = new JMenuItem("Open image...");
menuItemOpenImage.setMnemonic(KeyEvent.VK_O);
menuItemOpenImage.addActionListener(this);
menu.add(menuItemOpenImage);
menuItemSaveImage = new JMenuItem("Save image...");
menuItemSaveImage.setMnemonic(KeyEvent.VK_S);
menuItemSaveImage.addActionListener(this);
menu.add(menuItemSaveImage);
menuItemExit = new JMenuItem("Exit");
menuItemExit.setMnemonic(KeyEvent.VK_X);
menuItemExit.addActionListener(this);
menu.add(menuItemExit);
effectsMenu = new JMenu("Effects");
effectsMenu.setMnemonic(KeyEvent.VK_E);
menuBar.add(effectsMenu);
// add known ImageEffects to effects menu
for(ImageEffect ie : getFilters().values()) {
addEffect(ie);
}
// add component to display image
imagePanel = new JImagePanel();
getContentPane().add(imagePanel);
pack();
// setup open file dialog
String[] formats = ImageIO.getReaderFormatNames();
int unique = 0, remaining = formats.length;
openOuter:
for (; unique < remaining; unique++) {
formats[unique] = formats[unique].toLowerCase();
for (int j = 0; j < unique; j++) {
if (formats[j].equals(formats[unique])) {
formats[unique--] = formats[--remaining];
continue openOuter;
}
}
}
Arrays.sort(formats, 0, unique);
final String[] fReadableFormats = formats;
final int fRUnique = unique;
StringBuffer desc = new StringBuffer("Image files [");
if (fRUnique == 0) {
desc.append("N/A");
} else {
desc.append(fReadableFormats[0].toUpperCase());
for (int i = 1; i < fRUnique; i++) {
desc.append(", " + fReadableFormats[i].toUpperCase());
}
}
desc.append(']');
final String fDesc = desc.toString();
openFileChooser = new JFileChooser(".");
openFileChooser.setDialogTitle("Open Image");
openFileChooser.addChoosableFileFilter(new FileFilter() {
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
String filenameExtension = getExtension(file).toLowerCase();
for (int i = 0; i < fRUnique; i++) {
if (filenameExtension.equals(fReadableFormats[i])) {
return true;
}
}
return false;
}
public String getDescription() {
return fDesc;
}
});
// setup save file dialog
formats = ImageIO.getWriterFormatNames();
unique = 0;
remaining = formats.length;
saveOuter:
for (; unique < remaining; unique++) {
formats[unique] = formats[unique].toLowerCase();
for (int j = 0; j < unique; j++) {
if (formats[j].equals(formats[unique])) {
formats[unique--] = formats[--remaining];
continue saveOuter;
}
}
}
Arrays.sort(formats, 0, unique);
writableFormats = new String[unique];
for (int i = 0; i < unique; i++) {
writableFormats[i] = formats[i];
}
final String[] fWritableFormats = writableFormats;
final int fWUnique = unique;
saveFileChooser = new JFileChooser(".");
saveFileChooser.setDialogTitle("Save Image");
saveFileChooser.setAcceptAllFileFilterUsed(false);
for (int i = 0; i < fWUnique; i++) {
final int j = i;
saveFileChooser.addChoosableFileFilter(new FileFilter() {
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
String filenameExtension = getExtension(file).toLowerCase();
if (filenameExtension.equals(fWritableFormats[j])) {
return true;
}
return false;
}
public String getDescription() {
return fWritableFormats[j].toUpperCase();
}
});
}
}
public void addEffect(ImageEffect ie) {
JMenuItem menuItem = new JMenuItem(ie.getDescription());
menuItem.addActionListener(this);
int pos = 0;
try {
while (pos < effectsMenu.getMenuComponentCount() && ie.getDescription().compareTo(((JMenuItem)effectsMenu.getMenuComponent(pos)).getText()) >= 0) {
pos++;
}
} catch (ClassCastException e) {
}
effectsMenu.add(menuItem, pos);
effectMap.put(menuItem, ie);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == menuItemOpenImage) {
promptOpenImage();
repaint();
} else if (e.getSource() == menuItemSaveImage) {
promptSaveImage();
} else if (e.getSource() == menuItemExit) {
System.exit(0);
} else {
ImageEffect effect = (ImageEffect) effectMap.get(e.getSource());
ArrayList<ImageEffectParam> params = effect.getParameters();
if (params != null) {
try {
for (ImageEffectParam param : params) {
Object input = null;
do {
input = JOptionPane.showInputDialog(null,
param.getDescription(), param.getName(),
JOptionPane.INFORMATION_MESSAGE,
null, null, param.getDefaultValue());
} while(!parseAndVerifyInput(input.toString(),
param));
}
} catch(NullPointerException ex) {
// This is caused by a window being closed
return;
}
}
BufferedImage img = (BufferedImage)imagePanel.getBackgroundImage();
if (effect != null && img != null) {
BufferedImage newImg = effect.apply(img, params);
imagePanel.setBackgroundImage(newImg);
if (newImg != null && (newImg.getWidth() != img.getWidth() ||
newImg.getHeight() != img.getHeight())) {
pack();
} else {
repaint();
}
}
}
}
public void promptOpenImage() {
int action = openFileChooser.showOpenDialog(this);
if (action != JFileChooser.APPROVE_OPTION) {
return;
}
BufferedImage img = createBufferedImage(openFileChooser.getSelectedFile());
if (img == null) {
JOptionPane.showMessageDialog(this, "Error opening " + openFileChooser.getSelectedFile(), "Error", JOptionPane.ERROR_MESSAGE);
} else {
imagePanel.setBackgroundImage(img);
pack();
}
}
public void promptSaveImage() {
int action = saveFileChooser.showSaveDialog(this);
if (action != JFileChooser.APPROVE_OPTION || imagePanel.getBackgroundImage() == null) {
return;
}
File file = saveFileChooser.getSelectedFile();
String format = getExtension(file).toLowerCase();
int i = 0;
for (; i < writableFormats.length; i++) {
if (format.equals(writableFormats[i])) {
break;
}
}
if (i == writableFormats.length) {
format = saveFileChooser.getFileFilter().getDescription().toLowerCase();
file = new File(saveFileChooser.getCurrentDirectory(), file.getName() + "." + format);
}
try {
if (!ImageIO.write((BufferedImage)imagePanel.getBackgroundImage(), format, file)) {
JOptionPane.showMessageDialog(this, "Error saving " + file, "Error", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error saving " + file, "Error", JOptionPane.ERROR_MESSAGE);
}
}
public BufferedImage createBufferedImage(File file) {
BufferedImage img;
try {
img = ImageIO.read(file);
} catch (Exception e) {
return null;
}
if (img.getType() == BufferedImage.TYPE_INT_RGB) {
return img;
}
BufferedImage nImg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
nImg.createGraphics().drawImage(img, 0, 0, null);
return nImg;
}
public static boolean parseAndVerifyInput(String input,
ImageEffectParam param) {
int value = -1;
try {
value = Integer.parseInt(input);
} catch (NumberFormatException ex) {
showErrorMessage("Invalid integer format.");
return false;
}
if (value > param.getMaxValue() ||
value < param.getMinValue()) {
showErrorMessage("Integer value out of allowed range." +
" Min: " + param.getMinValue() +
" Max: " + param.getMaxValue());
return false;
}
param.setValue(value);
return true;
}
private static void showErrorMessage(String description) {
JOptionPane.showMessageDialog(null, description, "ERROR",
JOptionPane.ERROR_MESSAGE);
}
/**
* Kind of messy method, but it populates the HashMap effects with all the ImageEffects
* it can find in the class paths given.
*/
public static HashMap<String, ImageEffect> getFilters() {
String s = ImageEffect.class.getResource(ImageEffect.class.getSimpleName() + ".class").getFile();
s = s.substring(0, s.lastIndexOf(ImageEffect.class.getSimpleName()) - 1);
String[] searchPaths = s.split(File.pathSeparator);
HashMap<String, ImageEffect> effects = new HashMap<>();
for(int i = 0; i < searchPaths.length; i++) {
searchPaths[i] = searchPaths[i].replace("%20", " ");
File path = new File(searchPaths[i]);
if (!path.isDirectory()) {
continue;
}
File[] classFiles = path.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
if(name.endsWith(".class")) {
return true;
}
return false;
}
});
for(int j = 0; j < classFiles.length; j++) {
String className = getBase(classFiles[j]);
try {
Class<?> c = Class.forName(ImageEffect.class.getPackage().getName() + "." + className);
if(ImageEffect.class.isAssignableFrom(c)) {
Constructor construct = c.getDeclaredConstructor();
construct.setAccessible(true);
effects.put(className, (ImageEffect) construct.newInstance());
}
} catch(Exception e) {
}
}
}
return effects;
}
private static String getExtension(File file) {
if (file == null || file.isDirectory()) {
return "";
}
int index = file.getName().lastIndexOf('.');
if (index == -1) {
return "";
}
return file.getName().substring(index + 1);
}
private static String getBase(File file) {
if (file == null || file.isDirectory()) {
return "";
}
int index = file.getName().lastIndexOf('.');
if (index == -1) {
return file.getName();
}
return file.getName().substring(0, index);
}
private JMenu effectsMenu;
private JMenuItem menuItemOpenImage;
private JMenuItem menuItemSaveImage;
private JMenuItem menuItemExit;
private JImagePanel imagePanel;
private JFileChooser openFileChooser;
private String[] writableFormats;
private JFileChooser saveFileChooser;
private Map<JMenuItem, ImageEffect> effectMap;
}
/**
* A JPanel with an image for a background.
*/
class JImagePanel extends JPanel {
public JImagePanel() {
super();
}
public JImagePanel(boolean isDoubleBuffered) {
super(isDoubleBuffered);
}
public JImagePanel(LayoutManager layout) {
super(layout);
}
public JImagePanel(LayoutManager layout, boolean isDoubleBuffered) {
super(layout, isDoubleBuffered);
}
public Image getBackgroundImage() {
return backgroundImage;
}
public void setBackgroundImage(Image image) {
backgroundImage = image;
invalidate();
}
public void paintComponent(Graphics g) {
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
} else {
super.paintComponent(g);
}
}
public Dimension getPreferredSize() {
Dimension dim = super.getPreferredSize();
if (backgroundImage != null) {
dim.setSize(Math.max(dim.getWidth(), backgroundImage.getWidth(this)), Math.max(dim.getHeight(), backgroundImage.getHeight(this)));
}
return dim;
}
private Image backgroundImage;
}