1 package de.desy.acop.video.timageio;
2
3 import java.awt.image.BufferedImage;
4 import java.io.File;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.URL;
8 import java.util.Iterator;
9
10 import javax.imageio.IIOException;
11 import javax.imageio.IIOImage;
12 import javax.imageio.ImageIO;
13 import javax.imageio.ImageReadParam;
14 import javax.imageio.ImageReader;
15 import javax.imageio.ImageTypeSpecifier;
16 import javax.imageio.ImageWriter;
17 import javax.imageio.metadata.IIOMetadata;
18 import javax.imageio.stream.ImageInputStream;
19 import javax.imageio.stream.ImageOutputStream;
20
21
22
23
24
25
26
27
28 public final class TImageIO {
29
30
31
32
33 private TImageIO() {
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 public static TBufferedImage read(File input) throws IOException {
60 if (input == null)
61 throw new NullPointerException("input == null!");
62
63 if (!input.canRead())
64 throw new IIOException("Can't read input file!");
65
66 ImageInputStream stream = ImageIO.createImageInputStream(input);
67 if (stream == null)
68 throw new IIOException("Can't create an ImageInputStream!");
69
70 TBufferedImage tbi = read(stream);
71 if (tbi == null)
72 stream.close();
73
74 return tbi;
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 public static TBufferedImage read(InputStream input) throws IOException {
116 if (input == null)
117 throw new NullPointerException("input == null!");
118
119 ImageInputStream stream = ImageIO.createImageInputStream(input);
120 TBufferedImage tbi = read(stream);
121 if (tbi == null)
122 stream.close();
123
124 return tbi;
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 public static TBufferedImage read(URL input) throws IOException {
160 if (input == null)
161 throw new NullPointerException("input == null!");
162
163 InputStream istream = null;
164 try {
165 istream = input.openStream();
166 } catch (IOException e) {
167 throw new IIOException("Can't get input stream from URL!", e);
168 }
169
170 ImageInputStream stream = ImageIO.createImageInputStream(istream);
171 TBufferedImage tbi;
172 try {
173 tbi = read(stream);
174 if (tbi == null)
175 stream.close();
176
177 } finally {
178 istream.close();
179 }
180 return tbi;
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 public static TBufferedImage read(ImageInputStream stream) throws IOException {
210 if (stream == null)
211 throw new NullPointerException("stream == null!");
212
213 Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);
214 if (!iter.hasNext())
215 return null;
216
217 ImageReader reader = (ImageReader) iter.next();
218 ImageReadParam param = reader.getDefaultReadParam();
219 reader.setInput(stream, true, false);
220 BufferedImage image;
221 IIOMetadata metadata;
222 try {
223 image = reader.read(0, param);
224 metadata = reader.getImageMetadata(0);
225
226 } finally {
227 reader.dispose();
228 stream.close();
229 }
230 return new TBufferedImage(image, new TImageMetadata(metadata));
231 }
232
233 public static boolean write(TBufferedImage tbi, File output) throws IOException {
234 if (tbi == null)
235 throw new NullPointerException("timage == null!");
236
237 if (output == null)
238 throw new NullPointerException("output == null!");
239
240 ImageOutputStream stream = null;
241 try {
242 output.delete();
243 stream = ImageIO.createImageOutputStream(output);
244
245 } catch (IOException e) {
246 throw new IIOException("Can't create output stream!", e);
247 }
248
249 boolean val;
250 try {
251 val = write(tbi, stream);
252 } finally {
253 stream.close();
254 }
255 return val;
256 }
257
258 public static boolean write(TBufferedImage tbi, ImageOutputStream output) throws IOException {
259 if (tbi == null)
260 throw new NullPointerException("tbi == null!");
261
262 if (output == null)
263 throw new NullPointerException("output == null!");
264
265 ImageWriter writer = null;
266 ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(tbi.getBufferedImage());
267 Iterator<ImageWriter> iter = ImageIO.getImageWriters(type, "png");
268 if (iter.hasNext())
269 writer = (ImageWriter) iter.next();
270
271 if (writer == null)
272 return false;
273
274 writer.setOutput(output);
275 try {
276 IIOMetadata metadata = tbi.getMetadata() == null ? null : tbi.getMetadata().toPngMetadata();
277 writer.write(new IIOImage(tbi.getBufferedImage(), null, metadata));
278
279 } finally {
280 writer.dispose();
281 output.flush();
282 }
283 return true;
284 }
285
286 }