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 package wjhk.jupload2.filedata.helper;
27
28 import java.awt.image.BufferedImage;
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.Iterator;
32
33 import javax.imageio.IIOImage;
34 import javax.imageio.ImageIO;
35 import javax.imageio.ImageReader;
36 import javax.imageio.ImageWriteParam;
37 import javax.imageio.ImageWriter;
38 import javax.imageio.metadata.IIOMetadata;
39 import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
40 import javax.imageio.stream.FileImageInputStream;
41 import javax.imageio.stream.FileImageOutputStream;
42
43 import wjhk.jupload2.exception.JUploadIOException;
44 import wjhk.jupload2.filedata.DefaultFileData;
45 import wjhk.jupload2.filedata.PictureFileData;
46 import wjhk.jupload2.policies.PictureUploadPolicy;
47
48
49
50
51
52
53
54 public class ImageReaderWriterHelper {
55
56
57
58
59 FileImageInputStream fileImageInputStream = null;
60
61
62
63
64 FileImageOutputStream fileImageOutputStream;
65
66
67
68
69 PictureFileData pictureFileData;
70
71
72
73
74 ImageReader imageReader = null;
75
76
77
78
79 ImageWriter imageWriter = null;
80
81
82
83
84 ImageWriteParam imageWriterParam = null;
85
86
87
88
89
90 String targetPictureFormat;
91
92
93
94
95 PictureUploadPolicy uploadPolicy;
96
97
98
99
100
101
102
103
104
105
106
107 public ImageReaderWriterHelper(PictureUploadPolicy uploadPolicy, PictureFileData pictureFileData) {
108 this.uploadPolicy = uploadPolicy;
109 this.pictureFileData = pictureFileData;
110
111 this.targetPictureFormat = uploadPolicy.getImageFileConversionInfo().getTargetFormat(
112 pictureFileData.getFileExtension());
113 }
114
115
116
117
118
119
120
121
122
123
124 public String getTargetPictureFormat() {
125 return this.targetPictureFormat;
126 }
127
128
129
130
131
132
133
134 public void setOutput(File file) throws JUploadIOException {
135
136 initImageWriter();
137
138 try {
139 this.fileImageOutputStream = new FileImageOutputStream(file);
140 } catch (IOException e) {
141 throw new JUploadIOException("ImageReaderWriterHelper.setOutput()", e);
142 }
143 this.imageWriter.setOutput(this.fileImageOutputStream);
144 }
145
146
147
148
149
150
151 public void dispose() throws JUploadIOException {
152
153 if (this.imageWriter != null) {
154
155 this.imageWriter.dispose();
156 if (this.fileImageOutputStream != null) {
157 try {
158 this.fileImageOutputStream.close();
159 } catch (IOException e) {
160 throw new JUploadIOException("ImageReaderWriterHelper.dispose() [fileImageOutputStream]", e);
161 }
162 }
163 this.imageWriter = null;
164 this.fileImageOutputStream = null;
165 }
166
167
168 if (this.imageReader != null) {
169
170 this.imageReader.dispose();
171 try {
172 this.fileImageInputStream.close();
173 } catch (IOException e) {
174 throw new JUploadIOException("ImageReaderWriterHelper.dispose() [fileImageInputStream]", e);
175 }
176 this.imageReader = null;
177 this.fileImageInputStream = null;
178 }
179 }
180
181
182
183
184
185
186
187
188 public int getNumImages(boolean allowSearch) throws JUploadIOException {
189 initImageReader();
190 try {
191 return this.imageReader.getNumImages(allowSearch);
192 } catch (IOException e) {
193 throw new JUploadIOException("ImageReaderWriterHelper.getNumImages() [fileImageInputStream]", e);
194 }
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 public BufferedImage readImage(int imageIndex) throws JUploadIOException, IndexOutOfBoundsException {
216 initImageReader();
217 try {
218 this.uploadPolicy.displayDebug("ImageReaderWriterHelper: reading picture number " + imageIndex
219 + " of file " + this.pictureFileData.getFileName(), 30);
220 return this.imageReader.read(imageIndex);
221 } catch (IndexOutOfBoundsException e) {
222
223
224
225 throw e;
226 } catch (IOException e) {
227 throw new JUploadIOException("ImageReaderWriterHelper.readImage(" + imageIndex + ")", e);
228 }
229 }
230
231
232
233
234
235
236
237
238
239
240 public IIOImage readAll(int imageIndex) throws JUploadIOException, IndexOutOfBoundsException {
241 initImageReader();
242 try {
243 this.uploadPolicy.displayDebug("ImageReaderWriterHelper: reading picture number " + imageIndex
244 + " of file " + this.pictureFileData.getFileName(), 30);
245 return this.imageReader.readAll(imageIndex, this.imageReader.getDefaultReadParam());
246 } catch (IndexOutOfBoundsException e) {
247
248
249
250 throw e;
251 } catch (IOException e) {
252 throw new JUploadIOException("ImageReaderWriterHelper.readAll(" + imageIndex + ")", e);
253 }
254 }
255
256
257
258
259
260
261
262
263 public IIOMetadata getImageMetadata(int imageIndex) throws JUploadIOException {
264
265 initImageReader();
266
267
268 try {
269 this.uploadPolicy.displayDebug("ImageReaderWriterHelper: reading metadata for picture number " + imageIndex
270 + " of file " + this.pictureFileData.getFileName(), 30);
271 return this.imageReader.getImageMetadata(imageIndex);
272 } catch (IOException e) {
273 throw new JUploadIOException("ImageReaderWriterHelper.getImageMetadata()", e);
274 }
275 }
276
277
278
279
280
281
282
283
284
285 public void writeInsert(int numIndex, IIOImage iioImage, ImageWriteParam iwp) throws JUploadIOException {
286 initImageWriter();
287 try {
288 this.imageWriter.writeInsert(numIndex, iioImage, iwp);
289 } catch (IOException e) {
290 throw new JUploadIOException("ImageReaderWriterHelper.writeInsert()", e);
291 }
292 }
293
294
295
296
297
298
299
300 public void write(IIOImage iioImage) throws JUploadIOException {
301 initImageWriter();
302 try {
303 this.imageWriter.write(null, iioImage, this.imageWriterParam);
304 } catch (IOException e) {
305 throw new JUploadIOException("ImageReaderWriterHelper.write()", e);
306 }
307 }
308
309
310
311
312
313
314
315
316
317
318 private void initImageWriter() throws JUploadIOException {
319 if (this.imageWriter == null) {
320
321
322
323 Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(this.targetPictureFormat);
324 if (!iter.hasNext()) {
325
326
327
328
329 if (this.targetPictureFormat.equals("gif") && System.getProperty("java.version").startsWith("1.5")) {
330 throw new JUploadIOException(
331 "gif pictures are not supported in Java 1.5. Please switch to JRE 1.6.");
332 }
333
334 throw new JUploadIOException("No writer for the '" + this.targetPictureFormat + "' picture format.");
335 } else {
336 this.imageWriter = iter.next();
337 this.imageWriterParam = this.imageWriter.getDefaultWriteParam();
338
339
340 if (this.targetPictureFormat.equalsIgnoreCase("jpg")
341 || this.targetPictureFormat.equalsIgnoreCase("jpeg")) {
342 this.imageWriterParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
343
344
345 this.imageWriterParam.setCompressionQuality(this.uploadPolicy.getPictureCompressionQuality());
346
347 ((JPEGImageWriteParam) this.imageWriterParam).setOptimizeHuffmanTables(true);
348 }
349
350
351 try {
352 this.uploadPolicy.displayDebug(
353 "ImageWriter1 (used), CompressionQuality=" + this.imageWriterParam.getCompressionQuality(),
354 50);
355 } catch (Exception e2) {
356
357
358
359
360
361 this.uploadPolicy.displayWarn(e2.getClass().getName() + " in ImageReaderWriterHelper.java");
362 }
363 }
364 }
365 }
366
367
368
369
370
371
372 private void initImageReader() throws JUploadIOException {
373
374 try {
375 this.fileImageInputStream = new FileImageInputStream(this.pictureFileData.getWorkingSourceFile());
376 } catch (IOException e) {
377 throw new JUploadIOException("ImageReaderWriterHelper.initImageReader()", e);
378 }
379
380
381
382 if (this.imageReader == null) {
383 String ext = DefaultFileData.getExtension(this.pictureFileData.getFileName());
384 Iterator<ImageReader> iterator = ImageIO.getImageReadersBySuffix(ext);
385 if (iterator.hasNext()) {
386 this.imageReader = iterator.next();
387 this.imageReader.setInput(this.fileImageInputStream);
388 this.uploadPolicy.displayDebug("Foud one reader for " + ext + " extension", 50);
389 }
390
391
392 if (this.imageReader == null) {
393 this.uploadPolicy.displayErr("Found no reader for " + ext + " extension");
394 } else if (this.uploadPolicy.getDebugLevel() >= 50) {
395
396 try {
397 this.uploadPolicy.displayDebug("Nb images in " + this.pictureFileData.getFileName() + ": "
398 + this.imageReader.getNumImages(true), 50);
399 } catch (IOException e) {
400
401 }
402 }
403 }
404 }
405 }