aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/Helper.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/Helper.java')
-rw-r--r--src/main/java/Helper.java140
1 files changed, 140 insertions, 0 deletions
diff --git a/src/main/java/Helper.java b/src/main/java/Helper.java
new file mode 100644
index 0000000..b7a139a
--- /dev/null
+++ b/src/main/java/Helper.java
@@ -0,0 +1,140 @@
1import javax.imageio.ImageIO;
2import javax.swing.*;
3import java.awt.*;
4import java.awt.event.WindowAdapter;
5import java.awt.event.WindowEvent;
6import java.awt.image.BufferedImage;
7import java.io.File;
8import java.io.IOException;
9
10/**
11 * Provide simple tools to read, write and show pictures.
12 */
13public final class Helper {
14
15 // Convert specified BufferedImage into an array
16 private static int[][] fromBufferedImage(BufferedImage image) {
17 int width = image.getWidth();
18 int height = image.getHeight();
19 int[][] array = new int[height][width];
20 for (int row = 0; row < height; ++row) {
21 for (int col = 0; col < width; ++col) {
22 array[row][col] = image.getRGB(col, row);
23 }
24 }
25 return array;
26 }
27
28 // Convert specified array int a BufferedImage
29 private static BufferedImage toBufferedImage(int[][] array) {
30 int width = array[0].length;
31 int height = array.length;
32 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
33 for (int row = 0; row < height; ++row) {
34 for (int col = 0; col < width; ++col) {
35 image.setRGB(col, row, array[row][col]);
36 }
37 }
38 return image;
39 }
40
41 /**
42 * Reads specified image from disk.
43 *
44 * @param path Input file path
45 * @return HxW array of packed RGB colors, or <code>null</code> on failure
46 * @see #write
47 */
48 public static int[][] read(String path) {
49 try {
50 BufferedImage image = ImageIO.read(new File(path));
51 return fromBufferedImage(image);
52 } catch (IOException e) {
53 return null;
54 }
55 }
56
57 /**
58 * Writes specified image to disk.
59 *
60 * @param path Output file path
61 * @param array HxW array of packed RGB colors
62 * @return {@code true} if write operation was successful, {@code false} otherwise
63 * @see #read
64 */
65 public static boolean write(String path, int[][] array) {
66
67 // Convert array to Java image
68 BufferedImage image = toBufferedImage(array);
69
70 // Get desired file format
71 int index = path.lastIndexOf('.');
72 if (index < 0)
73 return false;
74 String extension = path.substring(index + 1);
75
76 // Export image
77 try {
78 return ImageIO.write(image, extension, new File(path));
79 } catch (IOException e) {
80 return false;
81 }
82
83 }
84
85 /**
86 * Shows specified image in a window.
87 *
88 * @param array HxW array of packed RGB colors
89 * @param title title to be displayed
90 */
91 public static void show(int[][] array, String title) {
92
93 // Convert array to Java image
94 final BufferedImage image = toBufferedImage(array);
95
96 // Create a panel to render this image
97 @SuppressWarnings("serial")
98 JPanel panel = new JPanel() {
99 @Override
100 protected void paintComponent(Graphics g) {
101 super.paintComponent(g);
102 g.drawImage(image, 0, 0, getWidth(), getHeight(), null, null);
103 }
104 };
105
106 // Create a frame to hold this panel
107 final JFrame frame = new JFrame(title);
108 frame.add(panel);
109 frame.pack();
110 frame.setSize(image.getWidth(), image.getHeight());
111
112 // Register closing event
113 frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
114 frame.addWindowListener(new WindowAdapter() {
115 @Override
116 public void windowClosing(WindowEvent e) {
117 frame.setVisible(false);
118 synchronized (frame) {
119 frame.notifyAll();
120 }
121 }
122 });
123
124 // Show this frame
125 frame.setVisible(true);
126
127 // Wait for close operation
128 try {
129 synchronized (frame) {
130 while (frame.isVisible())
131 frame.wait();
132 }
133 } catch (InterruptedException e) {
134 // Empty on purpose
135 }
136 frame.dispose();
137
138 }
139
140}