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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Rustem V. Rafikov
* @version $Revision: 1.3 $
*/
package javax.imageio;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.spi.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Arrays;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.net.URL;
/**
* The ImageIO class provides static methods to perform reading and writing
* operations using registered ImageReader and ImageWriter objects.
*
* @since Android 1.0
*/
public final class ImageIO {
/**
* The constant registry.
*/
private static final IIORegistry registry = IIORegistry.getDefaultInstance();
/**
* Instantiates a new ImageIO.
*/
private ImageIO() {
}
/**
* Scans for plug-ins in the class path, loads spi classes, and registers
* them with the IIORegistry.
*/
public static void scanForPlugins() {
throw new UnsupportedOperationException("Not supported yet");
}
/**
* Sets flag which indicates whether a cache file is used when creating
* ImageInputStreams and ImageOutputStreams or not.
*
* @param useCache
* the use cache flag.
*/
public static void setUseCache(boolean useCache) {
throw new UnsupportedOperationException("Not supported yet");
}
/**
* Gets the flag which indicates whether a cache file is used when creating
* ImageInputStreams and ImageOutputStreams or not. This method returns the
* current value which is set by setUseCache method.
*
* @return the use cache flag.
*/
public static boolean getUseCache() {
// TODO implement
return false;
}
/**
* Sets the cache directory.
*
* @param cacheDirectory
* the File which specifies a cache directory.
*/
public static void setCacheDirectory(File cacheDirectory) {
throw new UnsupportedOperationException("Not supported yet");
}
/**
* Gets the directory where cache files are created, returned the file which
* is set by setCacheDirectory method, or null.
*
* @return the File object which is set by setCacheDirectory method, or
* null.
*/
public static File getCacheDirectory() {
// TODO implement
// -- null indicates system-dep default temporary directory
return null;
}
/**
* Creates an ImageInputStream from the specified Object. The specified
* Object should obtain the input source such as File, or InputStream.
*
* @param input
* the input Object such as File, or InputStream.
* @return the ImageInputStream object, or null.
* @throws IOException
* if an I/O exception has occurred.
*/
public static ImageInputStream createImageInputStream(Object input) throws IOException {
if (input == null) {
throw new IllegalArgumentException("input source cannot be NULL");
}
Iterator<ImageInputStreamSpi> it = registry.getServiceProviders(ImageInputStreamSpi.class,
true);
while (it.hasNext()) {
ImageInputStreamSpi spi = it.next();
if (spi.getInputClass().isInstance(input)) {
return spi.createInputStreamInstance(input);
}
}
return null;
}
/**
* Creates an ImageOutputStream using the specified Object. The specified
* Object should obtain the output source such as File, or OutputStream.
*
* @param output
* the output Object such as File, or OutputStream.
* @return the ImageOutputStream object, or null.
* @throws IOException
* if an I/O exception has occurred.
*/
public static ImageOutputStream createImageOutputStream(Object output) throws IOException {
if (output == null) {
throw new IllegalArgumentException("output destination cannot be NULL");
}
Iterator<ImageOutputStreamSpi> it = registry.getServiceProviders(
ImageOutputStreamSpi.class, true);
while (it.hasNext()) {
ImageOutputStreamSpi spi = it.next();
if (spi.getOutputClass().isInstance(output)) {
// todo - use getUseCache and getCacheDir here
return spi.createOutputStreamInstance(output);
}
}
return null;
}
/**
* Gets the array of format names as String which can be decoded by
* registered ImageReader objects.
*
* @return the array of format names.
*/
public static String[] getReaderFormatNames() {
throw new UnsupportedOperationException("Not supported yet");
}
/**
* Gets the array of MIME types as String which can be decoded by registered
* ImageReader objects.
*
* @return the array of MIME types.
*/
public static String[] getReaderMIMETypes() {
throw new UnsupportedOperationException("Not supported yet");
}
/**
* Gets the Iterator of registered ImageReader which are able to decode an
* input data specified by input Object.
*
* @param input
* the input Object with encoded data such as ImageInputStream
* object.
* @return the Iterator of registered ImageReader.
*/
public static Iterator<ImageReader> getImageReaders(Object input) {
if (input == null) {
throw new NullPointerException("input cannot be NULL");
}
Iterator<ImageReaderSpi> it = registry.getServiceProviders(ImageReaderSpi.class,
new CanReadFilter(input), true);
return new SpiIteratorToReadersIteratorWrapper(it);
}
/**
* Gets the Iterator of registered ImageReader which are able to decode the
* specified format.
*
* @param formatName
* the format name such as "jpeg", or "gif".
* @return the Iterator of registered ImageReader.
*/
public static Iterator<ImageReader> getImageReadersByFormatName(String formatName) {
if (formatName == null) {
throw new NullPointerException("format name cannot be NULL");
}
Iterator<ImageReaderSpi> it = registry.getServiceProviders(ImageReaderSpi.class,
new FormatFilter(formatName), true);
return new SpiIteratorToReadersIteratorWrapper(it);
}
/**
* Gets the Iterator which lists the registered ImageReader objects that are
* able to decode files with the specified suffix.
*
* @param fileSuffix
* the file suffix such as "jpg".
* @return the Iterator of registered ImageReaders.
*/
public static Iterator<ImageReader> getImageReadersBySuffix(String fileSuffix) {
if (fileSuffix == null) {
throw new NullPointerException("suffix cannot be NULL");
}
Iterator<ImageReaderSpi> it = registry.getServiceProviders(ImageReaderSpi.class,
new SuffixFilter(fileSuffix), true);
return new SpiIteratorToReadersIteratorWrapper(it);
}
/**
* Gets the Iterator of registered ImageReader objects that are able to
* decode files with the specified MIME type.
*
* @param MIMEType
* the MIME type such as "image/jpeg".
* @return the Iterator of registered ImageReaders.
*/
public static Iterator<ImageReader> getImageReadersByMIMEType(String MIMEType) {
throw new UnsupportedOperationException("Not supported yet");
}
/**
* Gets an array of Strings giving the names of the formats supported by
* registered ImageWriter objects.
*
* @return the array of format names.
*/
public static String[] getWriterFormatNames() {
throw new UnsupportedOperationException("Not supported yet");
}
/**
* Gets an array of Strings giving the MIME types of the formats supported
* by registered ImageWriter objects.
*
* @return the array of MIME types.
*/
public static String[] getWriterMIMETypes() {
throw new UnsupportedOperationException("Not supported yet");
}
/**
* Gets the Iterator which lists the registered ImageReader objects that are
* able to encode the specified image format.
*
* @param formatName
* the image format name such as "jpeg".
* @return the Iterator of registered ImageWriter.
*/
public static Iterator<ImageWriter> getImageWritersByFormatName(String formatName) {
if (formatName == null) {
throw new NullPointerException("format name cannot be NULL");
}
Iterator<ImageWriterSpi> it = registry.getServiceProviders(ImageWriterSpi.class,
new FormatFilter(formatName), true);
return new SpiIteratorToWritersIteratorWrapper(it);
}
/**
* Gets the Iterator which lists the registered ImageReader objects that are
* able to encode the specified suffix.
*
* @param fileSuffix
* the file suffix such as "jpg".
* @return the Iterator of registered ImageWriter.
*/
public static Iterator<ImageWriter> getImageWritersBySuffix(String fileSuffix) {
if (fileSuffix == null) {
throw new NullPointerException("suffix cannot be NULL");
}
Iterator<ImageWriterSpi> it = registry.getServiceProviders(ImageWriterSpi.class,
new SuffixFilter(fileSuffix), true);
return new SpiIteratorToWritersIteratorWrapper(it);
}
/**
* Gets the Iterator which lists the registered ImageReader objects that are
* able to encode the specified MIME type.
*
* @param MIMEType
* the MIME type such as "image/jpeg".
* @return the Iterator of registered ImageWriter.
*/
public static Iterator<ImageWriter> getImageWritersByMIMEType(String MIMEType) {
throw new UnsupportedOperationException("Not supported yet");
}
/**
* Gets an ImageWriter object which corresponds to the specified
* ImageReader, or returns null if the specified ImageReader is not
* registered.
*
* @param reader
* the specified ImageReader.
* @return the ImageWriter, or null.
*/
public static ImageWriter getImageWriter(ImageReader reader) {
throw new UnsupportedOperationException("Not supported yet");
}
/**
* Gets an ImageReader object which corresponds to the specified
* ImageWriter, or returns null if the specified ImageWriter is not
* registered.
*
* @param writer
* the registered ImageWriter object.
* @return the ImageReader.
*/
public static ImageReader getImageReader(ImageWriter writer) {
throw new UnsupportedOperationException("Not supported yet");
}
/**
* Gets the Iterator of ImageWriter objects which are able to encode images
* with the specified ImageTypeSpecifier and format.
*
* @param type
* the ImageTypeSpecifier, which defines layout.
* @param formatName
* the format name.
* @return the Iterator of ImageWriter objects.
*/
public static Iterator<ImageWriter> getImageWriters(ImageTypeSpecifier type, String formatName) {
if (type == null) {
throw new NullPointerException("type cannot be NULL");
}
if (formatName == null) {
throw new NullPointerException("format name cannot be NULL");
}
Iterator<ImageWriterSpi> it = registry.getServiceProviders(ImageWriterSpi.class,
new FormatAndEncodeFilter(type, formatName), true);
return new SpiIteratorToWritersIteratorWrapper(it);
}
/**
* Gets the Iterator of registered ImageTranscoders which are able to
* transcode the metadata of the specified ImageReader object to a suitable
* object for encoding by the specified ImageWriter.
*
* @param reader
* the specified ImageReader.
* @param writer
* the specified ImageWriter.
* @return the Iterator of registered ImageTranscoders.
*/
public static Iterator<ImageTranscoder> getImageTranscoders(ImageReader reader,
ImageWriter writer) {
throw new UnsupportedOperationException("Not supported yet");
}
/**
* Reads image data from the specified File and decodes it using the
* appropriate registered ImageReader object. The File is wrapped in an
* ImageInputStream.
*
* @param input
* the File to be read.
* @return the BufferedImage decoded from the specified File, or null.
* @throws IOException
* if an I/O exception has occurred.
*/
public static BufferedImage read(File input) throws IOException {
if (input == null) {
throw new IllegalArgumentException("input == null!");
}
ImageInputStream stream = createImageInputStream(input);
return read(stream);
}
/**
* Reads image data from the specified InputStream and decodes it using an
* appropriate registered an ImageReader object.
*
* @param input
* the InputStream.
* @return the BufferedImage decoded from the specified InputStream, or
* null.
* @throws IOException
* if an I/O exception has occurred.
*/
public static BufferedImage read(InputStream input) throws IOException {
if (input == null) {
throw new IllegalArgumentException("input == null!");
}
ImageInputStream stream = createImageInputStream(input);
return read(stream);
}
/**
* Reads image data from the specified URL and decodes it using the
* appropriate registered ImageReader object.
*
* @param input
* the URL to be read.
* @return the BufferedImage decoded from the specified URL, or null.
* @throws IOException
* if an I/O exception has occurred.
*/
public static BufferedImage read(URL input) throws IOException {
if (input == null) {
throw new IllegalArgumentException("input == null!");
}
InputStream stream = input.openStream();
BufferedImage res = read(stream);
stream.close();
return res;
}
/**
* Reads image data from the specified ImageInputStream and decodes it using
* appropriate registered an ImageReader object.
*
* @param stream
* the ImageInputStream.
* @return the BufferedImage decoded from the specified ImageInputStream, or
* null.
* @throws IOException
* if an I/O exception has occurred.
*/
public static BufferedImage read(ImageInputStream stream) throws IOException {
if (stream == null) {
throw new IllegalArgumentException("stream == null!");
}
Iterator<ImageReader> imageReaders = getImageReaders(stream);
if (!imageReaders.hasNext()) {
return null;
}
ImageReader reader = imageReaders.next();
reader.setInput(stream, false, true);
BufferedImage res = reader.read(0);
reader.dispose();
try {
stream.close();
} catch (IOException e) {
// Stream could be already closed, proceed silently in this case
}
return res;
}
/**
* Writes the specified image in the specified format (using an appropriate
* ImageWriter) to the specified ImageOutputStream.
*
* @param im
* the RenderedImage.
* @param formatName
* the format name.
* @param output
* the ImageOutputStream where Image to be written.
* @return true, if Image is written successfully, false otherwise.
* @throws IOException
* if an I/O exception has occurred.
*/
public static boolean write(RenderedImage im, String formatName, ImageOutputStream output)
throws IOException {
if (im == null) {
throw new IllegalArgumentException("image cannot be NULL");
}
if (formatName == null) {
throw new IllegalArgumentException("format name cannot be NULL");
}
if (output == null) {
throw new IllegalArgumentException("output cannot be NULL");
}
Iterator<ImageWriter> it = getImageWriters(ImageTypeSpecifier.createFromRenderedImage(im),
formatName);
if (it.hasNext()) {
ImageWriter writer = it.next();
writer.setOutput(output);
writer.write(im);
output.flush();
writer.dispose();
return true;
}
return false;
}
/**
* Writes the specified image in the specified format (using an appropriate
* ImageWriter) to the specified File.
*
* @param im
* the RenderedImage.
* @param formatName
* the format name.
* @param output
* the output File where Image to be written.
* @return true, if Image is written successfully, false otherwise.
* @throws IOException
* if an I/O exception has occurred.
*/
public static boolean write(RenderedImage im, String formatName, File output)
throws IOException {
if (output == null) {
throw new IllegalArgumentException("output cannot be NULL");
}
if (output.exists()) {
output.delete();
}
ImageOutputStream ios = createImageOutputStream(output);
boolean rt = write(im, formatName, ios);
ios.close();
return rt;
}
/**
* Writes the specified image in the specified format (using an appropriate
* ImageWriter) to the specified OutputStream.
*
* @param im
* the RenderedImage.
* @param formatName
* the format name.
* @param output
* the OutputStream where Image is to be written.
* @return true, if Image is written successfully, false otherwise.
* @throws IOException
* if an I/O exception has occurred.
*/
public static boolean write(RenderedImage im, String formatName, OutputStream output)
throws IOException {
if (output == null) {
throw new IllegalArgumentException("output cannot be NULL");
}
ImageOutputStream ios = createImageOutputStream(output);
boolean rt = write(im, formatName, ios);
ios.close();
return rt;
}
/**
* Filter to match spi by format name.
*/
static class FormatFilter implements ServiceRegistry.Filter {
/**
* The name.
*/
private String name;
/**
* Instantiates a new format filter.
*
* @param name
* the name.
*/
public FormatFilter(String name) {
this.name = name;
}
public boolean filter(Object provider) {
ImageReaderWriterSpi spi = (ImageReaderWriterSpi)provider;
return Arrays.asList(spi.getFormatNames()).contains(name);
}
}
/**
* Filter to match spi by format name and encoding possibility.
*/
static class FormatAndEncodeFilter extends FormatFilter {
/**
* The type.
*/
private ImageTypeSpecifier type;
/**
* Instantiates a new format and encode filter.
*
* @param type
* the type.
* @param name
* the name.
*/
public FormatAndEncodeFilter(ImageTypeSpecifier type, String name) {
super(name);
this.type = type;
}
@Override
public boolean filter(Object provider) {
ImageWriterSpi spi = (ImageWriterSpi)provider;
return super.filter(provider) && spi.canEncodeImage(type);
}
}
/**
* Filter to match spi by suffix.
*/
static class SuffixFilter implements ServiceRegistry.Filter {
/**
* The suf.
*/
private String suf;
/**
* Instantiates a new suffix filter.
*
* @param suf
* the suf.
*/
public SuffixFilter(String suf) {
this.suf = suf;
}
public boolean filter(Object provider) {
ImageReaderWriterSpi spi = (ImageReaderWriterSpi)provider;
return Arrays.asList(spi.getFileSuffixes()).contains(suf);
}
}
/**
* Filter to match spi by decoding possibility.
*/
static class CanReadFilter implements ServiceRegistry.Filter {
/**
* The input.
*/
private Object input;
/**
* Instantiates a new can read filter.
*
* @param input
* the input.
*/
public CanReadFilter(Object input) {
this.input = input;
}
public boolean filter(Object provider) {
ImageReaderSpi spi = (ImageReaderSpi)provider;
try {
return spi.canDecodeInput(input);
} catch (IOException e) {
return false;
}
}
}
/**
* Wraps Spi's iterator to ImageWriter iterator.
*/
static class SpiIteratorToWritersIteratorWrapper implements Iterator<ImageWriter> {
/**
* The backend.
*/
private Iterator<ImageWriterSpi> backend;
/**
* Instantiates a new spi iterator to writers iterator wrapper.
*
* @param backend
* the backend.
*/
public SpiIteratorToWritersIteratorWrapper(Iterator<ImageWriterSpi> backend) {
this.backend = backend;
}
/**
* Next.
*
* @return the image writer.
*/
public ImageWriter next() {
try {
return backend.next().createWriterInstance();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* Checks for next.
*
* @return true, if successful.
*/
public boolean hasNext() {
return backend.hasNext();
}
/**
* Removes the.
*/
public void remove() {
throw new UnsupportedOperationException(
"Use deregisterServiceprovider instead of Iterator.remove()");
}
}
/**
* Wraps spi's iterator to ImageReader iterator.
*/
static class SpiIteratorToReadersIteratorWrapper implements Iterator<ImageReader> {
/**
* The backend.
*/
private Iterator<ImageReaderSpi> backend;
/**
* Instantiates a new spi iterator to readers iterator wrapper.
*
* @param backend
* the backend.
*/
public SpiIteratorToReadersIteratorWrapper(Iterator<ImageReaderSpi> backend) {
this.backend = backend;
}
/**
* Next.
*
* @return the image reader.
*/
public ImageReader next() {
try {
return backend.next().createReaderInstance();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* Checks for next.
*
* @return true, if successful.
*/
public boolean hasNext() {
return backend.hasNext();
}
/**
* Removes the.
*/
public void remove() {
throw new UnsupportedOperationException(
"Use deregisterServiceprovider instead of Iterator.remove()");
}
}
}
|