52 |
import java.util.Iterator; |
import java.util.Iterator; |
53 |
|
|
54 |
import javax.imageio.spi.IIORegistry; |
import javax.imageio.spi.IIORegistry; |
55 |
|
import javax.imageio.spi.ImageInputStreamSpi; |
56 |
|
import javax.imageio.spi.ImageOutputStreamSpi; |
57 |
import javax.imageio.spi.ImageReaderSpi; |
import javax.imageio.spi.ImageReaderSpi; |
58 |
|
import javax.imageio.spi.ImageTranscoderSpi; |
59 |
import javax.imageio.spi.ImageWriterSpi; |
import javax.imageio.spi.ImageWriterSpi; |
60 |
import javax.imageio.spi.ServiceRegistry; |
import javax.imageio.spi.ServiceRegistry; |
61 |
import javax.imageio.stream.ImageInputStream; |
import javax.imageio.stream.ImageInputStream; |
63 |
import javax.imageio.stream.MemoryCacheImageInputStream; |
import javax.imageio.stream.MemoryCacheImageInputStream; |
64 |
import javax.imageio.stream.MemoryCacheImageOutputStream; |
import javax.imageio.stream.MemoryCacheImageOutputStream; |
65 |
|
|
66 |
|
/** |
67 |
|
* An uninstantiable class that provides static methods for locating |
68 |
|
* and using image readers and writers. |
69 |
|
*/ |
70 |
public final class ImageIO |
public final class ImageIO |
71 |
{ |
{ |
72 |
/** |
/** |
73 |
* This class isn't intended to be instantiated. |
* Construct an ImageIO. Private since ImageIO is not instantiable. |
74 |
*/ |
*/ |
75 |
private ImageIO() {} |
private ImageIO() |
76 |
|
{ |
77 |
|
} |
78 |
|
|
79 |
private static final class ReaderFormatFilter implements ServiceRegistry.Filter |
private static final class ReaderFormatFilter implements ServiceRegistry.Filter |
80 |
{ |
{ |
126 |
} |
} |
127 |
} |
} |
128 |
|
|
129 |
|
private static final class ReaderObjectFilter implements ServiceRegistry.Filter |
130 |
|
{ |
131 |
|
private Object object; |
132 |
|
|
133 |
|
public ReaderObjectFilter(Object object) |
134 |
|
{ |
135 |
|
this.object = object; |
136 |
|
} |
137 |
|
|
138 |
|
public boolean filter(Object provider) |
139 |
|
{ |
140 |
|
if (provider instanceof ImageReaderSpi) |
141 |
|
{ |
142 |
|
ImageReaderSpi spi = (ImageReaderSpi) provider; |
143 |
|
|
144 |
|
try |
145 |
|
{ |
146 |
|
if (spi.canDecodeInput(object)) |
147 |
|
return true; |
148 |
|
} |
149 |
|
catch (IOException e) |
150 |
|
{ |
151 |
|
// Return false in this case |
152 |
|
} |
153 |
|
} |
154 |
|
return false; |
155 |
|
} |
156 |
|
} |
157 |
|
|
158 |
private static final class ReaderSuffixFilter implements ServiceRegistry.Filter |
private static final class ReaderSuffixFilter implements ServiceRegistry.Filter |
159 |
{ |
{ |
160 |
private String fileSuffix; |
private String fileSuffix; |
255 |
} |
} |
256 |
} |
} |
257 |
|
|
258 |
|
private static final class WriterObjectFilter implements ServiceRegistry.Filter |
259 |
|
{ |
260 |
|
private ImageTypeSpecifier type; |
261 |
|
private String formatName; |
262 |
|
|
263 |
|
public WriterObjectFilter(ImageTypeSpecifier type, |
264 |
|
String formatName) |
265 |
|
{ |
266 |
|
this.type = type; |
267 |
|
this.formatName = formatName; |
268 |
|
} |
269 |
|
|
270 |
|
public boolean filter(Object provider) |
271 |
|
{ |
272 |
|
if (provider instanceof ImageWriterSpi) |
273 |
|
{ |
274 |
|
ImageWriterSpi spi = (ImageWriterSpi) provider; |
275 |
|
|
276 |
|
if (spi.canEncodeImage(type)) |
277 |
|
{ |
278 |
|
String[] formatNames = spi.getFormatNames(); |
279 |
|
for (int i = formatNames.length - 1; i >= 0; --i) |
280 |
|
if (formatName.equals(formatNames[i])) |
281 |
|
return true; |
282 |
|
} |
283 |
|
} |
284 |
|
|
285 |
|
return false; |
286 |
|
} |
287 |
|
} |
288 |
|
|
289 |
|
private static final class TranscoderFilter implements ServiceRegistry.Filter |
290 |
|
{ |
291 |
|
private ImageReader reader; |
292 |
|
private ImageWriter writer; |
293 |
|
|
294 |
|
public TranscoderFilter(ImageReader reader, |
295 |
|
ImageWriter writer) |
296 |
|
{ |
297 |
|
this.reader = reader; |
298 |
|
this.writer = writer; |
299 |
|
} |
300 |
|
|
301 |
|
public boolean filter(Object provider) |
302 |
|
{ |
303 |
|
if (provider instanceof ImageTranscoderSpi) |
304 |
|
{ |
305 |
|
ImageTranscoderSpi spi = (ImageTranscoderSpi) provider; |
306 |
|
|
307 |
|
if (spi.getReaderServiceProviderName().equals |
308 |
|
(reader.getOriginatingProvider().getClass().getName()) |
309 |
|
&& spi.getWriterServiceProviderName().equals |
310 |
|
(writer.getOriginatingProvider().getClass().getName())) |
311 |
|
return true; |
312 |
|
} |
313 |
|
|
314 |
|
return false; |
315 |
|
} |
316 |
|
} |
317 |
|
|
318 |
private static final class ImageReaderIterator implements Iterator |
private static final class ImageReaderIterator implements Iterator |
319 |
{ |
{ |
320 |
Iterator it; |
Iterator it; |
416 |
} |
} |
417 |
} |
} |
418 |
|
|
419 |
|
/** |
420 |
|
* Retrieve the current cache directory. |
421 |
|
* |
422 |
|
* @return the current cache directory or null if none is set. |
423 |
|
*/ |
424 |
public static File getCacheDirectory() |
public static File getCacheDirectory() |
425 |
{ |
{ |
426 |
return cacheDirectory; |
return cacheDirectory; |
427 |
} |
} |
428 |
|
|
429 |
|
/** |
430 |
|
* Retrieve an iterator over all registered readers for the given |
431 |
|
* format. |
432 |
|
* |
433 |
|
* @param formatName an infomal format name (e.g. "jpeg" or "bmp") |
434 |
|
* |
435 |
|
* @return an iterator over a collection of image readers |
436 |
|
* |
437 |
|
* @exception IllegalArgumentException if formatName is null |
438 |
|
*/ |
439 |
public static Iterator getImageReadersByFormatName(String formatName) |
public static Iterator getImageReadersByFormatName(String formatName) |
440 |
{ |
{ |
441 |
if (formatName == null) |
if (formatName == null) |
446 |
formatName); |
formatName); |
447 |
} |
} |
448 |
|
|
449 |
|
/** |
450 |
|
* Retrieve an iterator over all registered readers for the given |
451 |
|
* MIME type. |
452 |
|
* |
453 |
|
* @param MIMEType a MIME specification for an image type |
454 |
|
* (e.g. "image/jpeg" or "image/x-bmp") |
455 |
|
* |
456 |
|
* @return an iterator over a collection of image readers |
457 |
|
* |
458 |
|
* @exception IllegalArgumentException if MIMEType is null |
459 |
|
*/ |
460 |
public static Iterator getImageReadersByMIMEType(String MIMEType) |
public static Iterator getImageReadersByMIMEType(String MIMEType) |
461 |
{ |
{ |
462 |
if (MIMEType == null) |
if (MIMEType == null) |
467 |
MIMEType); |
MIMEType); |
468 |
} |
} |
469 |
|
|
470 |
|
/** |
471 |
|
* Retrieve an iterator over all registered readers for the given |
472 |
|
* file suffix. |
473 |
|
* |
474 |
|
* @param fileSuffix an image file suffix (e.g. "jpg" or "bmp") |
475 |
|
* |
476 |
|
* @return an iterator over a collection of image readers |
477 |
|
* |
478 |
|
* @exception IllegalArgumentException if fileSuffix is null |
479 |
|
*/ |
480 |
public static Iterator getImageReadersBySuffix(String fileSuffix) |
public static Iterator getImageReadersBySuffix(String fileSuffix) |
481 |
{ |
{ |
482 |
if (fileSuffix == null) |
if (fileSuffix == null) |
487 |
fileSuffix); |
fileSuffix); |
488 |
} |
} |
489 |
|
|
490 |
|
/** |
491 |
|
* Retrieve an iterator over all registered writers for the given |
492 |
|
* format. |
493 |
|
* |
494 |
|
* @param formatName an infomal format name (e.g. "jpeg" or "bmp") |
495 |
|
* |
496 |
|
* @return an iterator over a collection of image writers |
497 |
|
* |
498 |
|
* @exception IllegalArgumentException if formatName is null |
499 |
|
*/ |
500 |
public static Iterator getImageWritersByFormatName(String formatName) |
public static Iterator getImageWritersByFormatName(String formatName) |
501 |
{ |
{ |
502 |
if (formatName == null) |
if (formatName == null) |
507 |
formatName); |
formatName); |
508 |
} |
} |
509 |
|
|
510 |
|
/** |
511 |
|
* Retrieve an iterator over all registered writers for the given |
512 |
|
* MIME type. |
513 |
|
* |
514 |
|
* @param MIMEType a MIME specification for an image type |
515 |
|
* (e.g. "image/jpeg" or "image/x-bmp") |
516 |
|
* |
517 |
|
* @return an iterator over a collection of image writers |
518 |
|
* |
519 |
|
* @exception IllegalArgumentException if MIMEType is null |
520 |
|
*/ |
521 |
public static Iterator getImageWritersByMIMEType(String MIMEType) |
public static Iterator getImageWritersByMIMEType(String MIMEType) |
522 |
{ |
{ |
523 |
if (MIMEType == null) |
if (MIMEType == null) |
528 |
MIMEType); |
MIMEType); |
529 |
} |
} |
530 |
|
|
531 |
|
/** |
532 |
|
* Retrieve an iterator over all registered writers for the given |
533 |
|
* file suffix. |
534 |
|
* |
535 |
|
* @param fileSuffix an image file suffix (e.g. "jpg" or "bmp") |
536 |
|
* |
537 |
|
* @return an iterator over a collection of image writers |
538 |
|
* |
539 |
|
* @exception IllegalArgumentException if fileSuffix is null |
540 |
|
*/ |
541 |
public static Iterator getImageWritersBySuffix(String fileSuffix) |
public static Iterator getImageWritersBySuffix(String fileSuffix) |
542 |
{ |
{ |
543 |
if (fileSuffix == null) |
if (fileSuffix == null) |
548 |
fileSuffix); |
fileSuffix); |
549 |
} |
} |
550 |
|
|
551 |
|
/** |
552 |
|
* Retrieve all the informal format names supported by the |
553 |
|
* collection of registered image readers. |
554 |
|
* |
555 |
|
* @return an array of format names |
556 |
|
*/ |
557 |
public static String[] getReaderFormatNames() |
public static String[] getReaderFormatNames() |
558 |
{ |
{ |
559 |
try |
try |
579 |
} |
} |
580 |
} |
} |
581 |
|
|
582 |
|
/** |
583 |
|
* Retrieve all the MIME types supported by the collection of |
584 |
|
* registered image readers. |
585 |
|
* |
586 |
|
* @return an array of MIME types |
587 |
|
*/ |
588 |
public static String[] getReaderMIMETypes() |
public static String[] getReaderMIMETypes() |
589 |
{ |
{ |
590 |
try |
try |
615 |
return IIORegistry.getDefaultInstance(); |
return IIORegistry.getDefaultInstance(); |
616 |
} |
} |
617 |
|
|
618 |
|
/** |
619 |
|
* Check whether or not an on-disk cache is used for image input and |
620 |
|
* output streams. |
621 |
|
* |
622 |
|
* @return true if an on-disk cache is available, false otherwise |
623 |
|
*/ |
624 |
public static boolean getUseCache() |
public static boolean getUseCache() |
625 |
{ |
{ |
626 |
return useCache; |
return useCache; |
627 |
} |
} |
628 |
|
|
629 |
|
/** |
630 |
|
* Retrieve all the informal format names supported by the |
631 |
|
* collection of registered image writers. |
632 |
|
* |
633 |
|
* @return an array of format names |
634 |
|
*/ |
635 |
public static String[] getWriterFormatNames() |
public static String[] getWriterFormatNames() |
636 |
{ |
{ |
637 |
try |
try |
657 |
} |
} |
658 |
} |
} |
659 |
|
|
660 |
|
/** |
661 |
|
* Retrieve all the MIME types supported by the collection of |
662 |
|
* registered image writers. |
663 |
|
* |
664 |
|
* @return an array of MIME types |
665 |
|
*/ |
666 |
public static String[] getWriterMIMETypes() |
public static String[] getWriterMIMETypes() |
667 |
{ |
{ |
668 |
try |
try |
697 |
IIORegistry.getDefaultInstance().registerApplicationClasspathSpis(); |
IIORegistry.getDefaultInstance().registerApplicationClasspathSpis(); |
698 |
} |
} |
699 |
|
|
700 |
|
/** |
701 |
|
* Set the directory to be used for caching image data. A null |
702 |
|
* argument means to use the default system temporary directory. |
703 |
|
* This cache directory is only used if getUseCache returns true. |
704 |
|
* |
705 |
|
* @param cacheDirectory the directory where image data should be |
706 |
|
* cached |
707 |
|
* |
708 |
|
* @exception IllegalArgumentException if cacheDirectory is not a |
709 |
|
* directory |
710 |
|
*/ |
711 |
public static void setCacheDirectory(File cacheDirectory) |
public static void setCacheDirectory(File cacheDirectory) |
712 |
{ |
{ |
713 |
|
// FIXME: add SecurityManager call |
714 |
if (cacheDirectory != null) |
if (cacheDirectory != null) |
715 |
{ |
{ |
716 |
if (!cacheDirectory.isDirectory()) |
if (!cacheDirectory.isDirectory()) |
722 |
ImageIO.cacheDirectory = cacheDirectory; |
ImageIO.cacheDirectory = cacheDirectory; |
723 |
} |
} |
724 |
|
|
725 |
|
/** |
726 |
|
* Control whether or not an on-disk cache is used. This cache is |
727 |
|
* used to store input or output data from an image data stream when |
728 |
|
* data in the stream needs to be re-processed. |
729 |
|
* |
730 |
|
* If useCache is false the cache will be stored in memory. Doing |
731 |
|
* so eliminates file creation and deletion overhead. The default |
732 |
|
* is to use an on-disk cache. |
733 |
|
* |
734 |
|
* @param useCache true to use an on-disk cache, false otherwise |
735 |
|
*/ |
736 |
public static void setUseCache(boolean useCache) |
public static void setUseCache(boolean useCache) |
737 |
{ |
{ |
738 |
ImageIO.useCache = useCache; |
ImageIO.useCache = useCache; |
739 |
} |
} |
740 |
|
|
741 |
/* |
/** |
742 |
* "Standard" simplified entry points. |
* Write an image to a file using a registered writer that supports |
743 |
|
* the given format, overwriting the file if it already exists. |
744 |
|
* |
745 |
|
* @param im the image data to write |
746 |
|
* @param formatName an informal description of the output format |
747 |
|
* @param output the file to which the image will be written |
748 |
|
* |
749 |
|
* @return false if no registered writer supports the given format, |
750 |
|
* true otherwise |
751 |
|
* |
752 |
|
* @exception IllegalArgumentException if any argument is null |
753 |
|
* @exception IOException if a writing error occurs |
754 |
*/ |
*/ |
|
|
|
755 |
public static boolean write(RenderedImage im, |
public static boolean write(RenderedImage im, |
756 |
String formatName, |
String formatName, |
757 |
File output) |
File output) |
758 |
throws IOException |
throws IOException |
759 |
{ |
{ |
760 |
|
if (im == null || formatName == null || output == null) |
761 |
|
throw new IllegalArgumentException ("null argument"); |
762 |
|
|
763 |
return write(im, formatName, new FileOutputStream(output)); |
return write(im, formatName, new FileOutputStream(output)); |
764 |
} |
} |
765 |
|
|
766 |
|
/** |
767 |
|
* Write an image to an output stream using a registered writer that |
768 |
|
* supports the given format. |
769 |
|
* |
770 |
|
* @param im the image data to write |
771 |
|
* @param formatName an informal description of the output format |
772 |
|
* @param output the output stream to which the image will be |
773 |
|
* written |
774 |
|
* |
775 |
|
* @return false if no registered writer supports the given format, |
776 |
|
* true otherwise |
777 |
|
* |
778 |
|
* @exception IllegalArgumentException if any argument is null |
779 |
|
* @exception IOException if a writing error occurs |
780 |
|
*/ |
781 |
public static boolean write(RenderedImage im, |
public static boolean write(RenderedImage im, |
782 |
String formatName, |
String formatName, |
783 |
OutputStream output) |
OutputStream output) |
784 |
throws IOException |
throws IOException |
785 |
{ |
{ |
786 |
|
if (im == null || formatName == null || output == null) |
787 |
|
throw new IllegalArgumentException ("null argument"); |
788 |
|
|
789 |
return write(im, formatName, new MemoryCacheImageOutputStream(output)); |
return write(im, formatName, new MemoryCacheImageOutputStream(output)); |
790 |
} |
} |
791 |
|
|
792 |
|
/** |
793 |
|
* Write an image to an ImageOutputStream using a registered writer |
794 |
|
* that supports the given format. Image data is written starting |
795 |
|
* at the ImageOutputStream's current stream pointer, overwriting |
796 |
|
* any existing data. |
797 |
|
* |
798 |
|
* @param im the image data to write |
799 |
|
* @param formatName an informal description of the output format |
800 |
|
* @param output the image output stream to which the image will be |
801 |
|
* written |
802 |
|
* |
803 |
|
* @return false if no registered writer supports the given format, |
804 |
|
* true otherwise |
805 |
|
* |
806 |
|
* @exception IllegalArgumentException if any argument is null |
807 |
|
* @exception IOException if a writing error occurs |
808 |
|
*/ |
809 |
public static boolean write(RenderedImage im, |
public static boolean write(RenderedImage im, |
810 |
String formatName, |
String formatName, |
811 |
ImageOutputStream output) |
ImageOutputStream output) |
812 |
throws IOException |
throws IOException |
813 |
{ |
{ |
814 |
|
if (im == null || formatName == null || output == null) |
815 |
|
throw new IllegalArgumentException ("null argument"); |
816 |
|
|
817 |
Iterator writers = getImageWritersByFormatName(formatName); |
Iterator writers = getImageWritersByFormatName(formatName); |
818 |
IIOImage img = new IIOImage(im, null, null); |
IIOImage img = new IIOImage(im, null, null); |
819 |
while (writers.hasNext()) |
while (writers.hasNext()) |
835 |
return false; |
return false; |
836 |
} |
} |
837 |
|
|
838 |
|
/** |
839 |
|
* Create a buffered image from an image input stream. An image |
840 |
|
* reader that supports the given image data is automatically |
841 |
|
* selected from the collection of registered readers. If no |
842 |
|
* registered reader can handle the input format, null is returned. |
843 |
|
* |
844 |
|
* @param stream the image input stream from which to read image |
845 |
|
* data |
846 |
|
* |
847 |
|
* @return a new buffered image created from the given image data, |
848 |
|
* or null |
849 |
|
* |
850 |
|
* @exception IllegalArgumentException if stream is null |
851 |
|
* @exception IOException if a reading error occurs |
852 |
|
*/ |
853 |
public static BufferedImage read(ImageInputStream stream) |
public static BufferedImage read(ImageInputStream stream) |
854 |
throws IOException |
throws IOException |
855 |
{ |
{ |
856 |
|
if (stream == null) |
857 |
|
throw new IllegalArgumentException("null argument"); |
858 |
|
|
859 |
Iterator providers = getRegistry().getServiceProviders(ImageReaderSpi.class, true); |
Iterator providers = getRegistry().getServiceProviders(ImageReaderSpi.class, true); |
860 |
while (providers.hasNext()) |
while (providers.hasNext()) |
861 |
{ |
{ |
869 |
} |
} |
870 |
return null; |
return null; |
871 |
} |
} |
872 |
|
|
873 |
|
/** |
874 |
|
* Create a buffered image from a URL. An image reader that |
875 |
|
* supports the given image data is automatically selected from the |
876 |
|
* collection of registered readers. If no registered reader can |
877 |
|
* handle the input format, null is returned. |
878 |
|
* |
879 |
|
* The image data will be cached in the current cache directory if |
880 |
|
* caching is enabled. |
881 |
|
* |
882 |
|
* This method does not locate readers that read data directly from |
883 |
|
* a URL. To locate such readers manually, use IIORegistry and |
884 |
|
* ImageReaderSpi. |
885 |
|
* |
886 |
|
* @param input the URL from which to retrieve the image file |
887 |
|
* |
888 |
|
* @return a new buffered image created from the given image URL, or |
889 |
|
* null |
890 |
|
* |
891 |
|
* @exception IllegalArgumentException if input is null |
892 |
|
* @exception IOException if a reading error occurs |
893 |
|
*/ |
894 |
public static BufferedImage read(URL input) |
public static BufferedImage read(URL input) |
895 |
throws IOException |
throws IOException |
896 |
{ |
{ |
897 |
|
if (input == null) |
898 |
|
throw new IllegalArgumentException("null argument"); |
899 |
|
|
900 |
return read(input.openStream()); |
return read(input.openStream()); |
901 |
} |
} |
902 |
|
|
903 |
|
/** |
904 |
|
* Create a buffered image from an input stream. An image reader |
905 |
|
* that supports the given image data is automatically selected from |
906 |
|
* the collection of registered readers. If no registered reader |
907 |
|
* can handle the input format, null is returned. |
908 |
|
* |
909 |
|
* The image data will be cached in the current cache directory if |
910 |
|
* caching is enabled. |
911 |
|
* |
912 |
|
* This method does not locate readers that read data directly from |
913 |
|
* an input stream. To locate such readers manually, use |
914 |
|
* IIORegistry and ImageReaderSpi. |
915 |
|
* |
916 |
|
* @param input the input stream from which to read the image data |
917 |
|
* |
918 |
|
* @return a new buffered image created from the given input stream, |
919 |
|
* or null |
920 |
|
* |
921 |
|
* @exception IllegalArgumentException if input is null |
922 |
|
* @exception IOException if a reading error occurs |
923 |
|
*/ |
924 |
public static BufferedImage read(InputStream input) |
public static BufferedImage read(InputStream input) |
925 |
throws IOException |
throws IOException |
926 |
{ |
{ |
927 |
|
if (input == null) |
928 |
|
throw new IllegalArgumentException("null argument"); |
929 |
|
|
930 |
return read(new MemoryCacheImageInputStream(input)); |
return read(new MemoryCacheImageInputStream(input)); |
931 |
} |
} |
932 |
|
|
933 |
|
/** |
934 |
|
* Create a buffered image from a file. An image reader that |
935 |
|
* supports the given image data is automatically selected from the |
936 |
|
* collection of registered readers. If no registered reader can |
937 |
|
* handle the input format, null is returned. |
938 |
|
* |
939 |
|
* The image data will be cached in the current cache directory if |
940 |
|
* caching is enabled. |
941 |
|
* |
942 |
|
* This method does not locate readers that read data directly from |
943 |
|
* a file. To locate such readers manually, use IIORegistry and |
944 |
|
* ImageReaderSpi. |
945 |
|
* |
946 |
|
* @param input the file from which to read image data |
947 |
|
* |
948 |
|
* @return a new buffered image created from the given image file, |
949 |
|
* or null |
950 |
|
* |
951 |
|
* @exception IllegalArgumentException if input is null |
952 |
|
* @exception IOException if a reading error occurs |
953 |
|
*/ |
954 |
public static BufferedImage read(File input) |
public static BufferedImage read(File input) |
955 |
throws IOException |
throws IOException |
956 |
{ |
{ |
957 |
|
if (input == null) |
958 |
|
throw new IllegalArgumentException("null argument"); |
959 |
|
|
960 |
return read(new FileInputStream(input)); |
return read(new FileInputStream(input)); |
961 |
} |
} |
962 |
|
|
963 |
|
/** |
964 |
|
* Create an image input stream from the given object. The |
965 |
|
* collection of ImageInputStreamSpis registered with the |
966 |
|
* IIORegistry is searched for an image input stream that can take |
967 |
|
* input from the given object. null is returned if no such SPI is |
968 |
|
* registered. |
969 |
|
* |
970 |
|
* The image data will be cached in the current cache directory if |
971 |
|
* caching is enabled. |
972 |
|
* |
973 |
|
* @param input an object from which to read image data |
974 |
|
* |
975 |
|
* @return an ImageInputStream that can read data from input, or |
976 |
|
* null |
977 |
|
* |
978 |
|
* @exception IllegalArgumentException if input is null |
979 |
|
* @exception IOException if caching is required but not enabled |
980 |
|
*/ |
981 |
|
public static ImageInputStream createImageInputStream (Object input) |
982 |
|
throws IOException |
983 |
|
{ |
984 |
|
if (input == null) |
985 |
|
throw new IllegalArgumentException ("null argument"); |
986 |
|
|
987 |
|
Iterator spis = getRegistry().getServiceProviders |
988 |
|
(ImageInputStreamSpi.class, true); |
989 |
|
|
990 |
|
ImageInputStreamSpi foundSpi = null; |
991 |
|
|
992 |
|
while(spis.hasNext()) |
993 |
|
{ |
994 |
|
ImageInputStreamSpi spi = (ImageInputStreamSpi) spis.next(); |
995 |
|
|
996 |
|
if (input.getClass().equals(spi.getInputClass())) |
997 |
|
{ |
998 |
|
foundSpi = spi; |
999 |
|
break; |
1000 |
|
} |
1001 |
|
} |
1002 |
|
|
1003 |
|
return foundSpi == null ? null : |
1004 |
|
foundSpi.createInputStreamInstance (input, |
1005 |
|
getUseCache(), |
1006 |
|
getCacheDirectory()); |
1007 |
|
} |
1008 |
|
|
1009 |
|
/** |
1010 |
|
* Create an image output stream from the given object. The |
1011 |
|
* collection of ImageOutputStreamSpis registered with the |
1012 |
|
* IIORegistry is searched for an image output stream that can send |
1013 |
|
* output to the given object. null is returned if no such SPI is |
1014 |
|
* registered. |
1015 |
|
* |
1016 |
|
* The image data will be cached in the current cache directory if |
1017 |
|
* caching is enabled. |
1018 |
|
* |
1019 |
|
* @param input an object to which to write image data |
1020 |
|
* |
1021 |
|
* @return an ImageOutputStream that can send data to output, or |
1022 |
|
* null |
1023 |
|
* |
1024 |
|
* @exception IllegalArgumentException if output is null |
1025 |
|
* @exception IOException if caching is required but not enabled |
1026 |
|
*/ |
1027 |
|
public static ImageOutputStream createImageOutputStream (Object output) |
1028 |
|
throws IOException |
1029 |
|
{ |
1030 |
|
if (output == null) |
1031 |
|
throw new IllegalArgumentException ("null argument"); |
1032 |
|
|
1033 |
|
Iterator spis = getRegistry().getServiceProviders |
1034 |
|
(ImageOutputStreamSpi.class, true); |
1035 |
|
|
1036 |
|
ImageOutputStreamSpi foundSpi = null; |
1037 |
|
|
1038 |
|
while(spis.hasNext()) |
1039 |
|
{ |
1040 |
|
ImageOutputStreamSpi spi = (ImageOutputStreamSpi) spis.next(); |
1041 |
|
|
1042 |
|
if (output.getClass().equals(spi.getOutputClass())) |
1043 |
|
{ |
1044 |
|
foundSpi = spi; |
1045 |
|
break; |
1046 |
|
} |
1047 |
|
} |
1048 |
|
|
1049 |
|
return foundSpi == null ? null : |
1050 |
|
foundSpi.createOutputStreamInstance (output, |
1051 |
|
getUseCache(), |
1052 |
|
getCacheDirectory()); |
1053 |
|
} |
1054 |
|
|
1055 |
|
/** |
1056 |
|
* Retrieve an image reader corresponding to an image writer, or |
1057 |
|
* null if writer is not registered or if no corresponding reader is |
1058 |
|
* registered. |
1059 |
|
* |
1060 |
|
* @param writer a registered image writer |
1061 |
|
* |
1062 |
|
* @return an image reader corresponding to writer, or null |
1063 |
|
* |
1064 |
|
* @exception IllegalArgumentException if writer is null |
1065 |
|
*/ |
1066 |
|
public static ImageReader getImageReader (ImageWriter writer) |
1067 |
|
{ |
1068 |
|
if (writer == null) |
1069 |
|
throw new IllegalArgumentException ("null argument"); |
1070 |
|
|
1071 |
|
ImageWriterSpi spi = (ImageWriterSpi) getRegistry() |
1072 |
|
.getServiceProviderByClass(writer.getClass()); |
1073 |
|
|
1074 |
|
String[] readerSpiNames = spi.getImageReaderSpiNames(); |
1075 |
|
|
1076 |
|
ImageReader r = null; |
1077 |
|
|
1078 |
|
if (readerSpiNames != null) |
1079 |
|
{ |
1080 |
|
try |
1081 |
|
{ |
1082 |
|
Class readerClass = Class.forName (readerSpiNames[0]); |
1083 |
|
r = (ImageReader) readerClass.newInstance (); |
1084 |
|
} |
1085 |
|
catch (Exception e) |
1086 |
|
{ |
1087 |
|
return null; |
1088 |
|
} |
1089 |
|
} |
1090 |
|
return r; |
1091 |
|
} |
1092 |
|
|
1093 |
|
/** |
1094 |
|
* Retrieve an iterator over the collection of registered image |
1095 |
|
* readers that support reading data from the given object. |
1096 |
|
* |
1097 |
|
* @param input the object for which to retrieve image readers |
1098 |
|
* |
1099 |
|
* @return an iterator over a collection of image readers |
1100 |
|
*/ |
1101 |
|
public static Iterator getImageReaders (Object input) |
1102 |
|
{ |
1103 |
|
if (input == null) |
1104 |
|
throw new IllegalArgumentException ("null argument"); |
1105 |
|
|
1106 |
|
return getRegistry().getServiceProviders (ImageReaderSpi.class, |
1107 |
|
new ReaderObjectFilter(input), |
1108 |
|
true); |
1109 |
|
} |
1110 |
|
|
1111 |
|
/** |
1112 |
|
* Retrieve an iterator over the collection of registered image |
1113 |
|
* writers that support writing images of the given type and in the |
1114 |
|
* given format. |
1115 |
|
* |
1116 |
|
* @param type the output image's colour and sample models |
1117 |
|
* @param formatName the output image format |
1118 |
|
* |
1119 |
|
* @return an iterator over a collection of image writers |
1120 |
|
*/ |
1121 |
|
public static Iterator getImageWriters (ImageTypeSpecifier type, |
1122 |
|
String formatName) |
1123 |
|
{ |
1124 |
|
if (type == null || formatName == null) |
1125 |
|
throw new IllegalArgumentException ("null argument"); |
1126 |
|
|
1127 |
|
return getRegistry().getServiceProviders (ImageWriterSpi.class, |
1128 |
|
new WriterObjectFilter(type, |
1129 |
|
formatName), |
1130 |
|
true); |
1131 |
|
} |
1132 |
|
|
1133 |
|
/** |
1134 |
|
* Retrieve an image writer corresponding to an image reader, or |
1135 |
|
* null if reader is not registered or if no corresponding writer is |
1136 |
|
* registered. This method is useful for preserving metadata |
1137 |
|
* without needing to understand its format, since the returned |
1138 |
|
* writer will be able to write, unchanged, the metadata passed to |
1139 |
|
* it by the reader. |
1140 |
|
* |
1141 |
|
* @param reader a registered image reader |
1142 |
|
* |
1143 |
|
* @return an image writer corresponding to reader, or null |
1144 |
|
* |
1145 |
|
* @exception IllegalArgumentException if reader is null |
1146 |
|
*/ |
1147 |
|
public static ImageWriter getImageWriter (ImageReader reader) |
1148 |
|
{ |
1149 |
|
if (reader == null) |
1150 |
|
throw new IllegalArgumentException ("null argument"); |
1151 |
|
|
1152 |
|
ImageReaderSpi spi = (ImageReaderSpi) getRegistry() |
1153 |
|
.getServiceProviderByClass(reader.getClass()); |
1154 |
|
|
1155 |
|
String[] writerSpiNames = spi.getImageWriterSpiNames(); |
1156 |
|
|
1157 |
|
ImageWriter w = null; |
1158 |
|
|
1159 |
|
if (writerSpiNames != null) |
1160 |
|
{ |
1161 |
|
try |
1162 |
|
{ |
1163 |
|
Class writerClass = Class.forName (writerSpiNames[0]); |
1164 |
|
w = (ImageWriter) writerClass.newInstance (); |
1165 |
|
} |
1166 |
|
catch (Exception e) |
1167 |
|
{ |
1168 |
|
return null; |
1169 |
|
} |
1170 |
|
} |
1171 |
|
return w; |
1172 |
|
} |
1173 |
|
|
1174 |
|
/** |
1175 |
|
* Retrieve an iterator over a collection of image transcoders that |
1176 |
|
* support transcoding from the given image reader's metadata format |
1177 |
|
* to the given writer's metadata format. |
1178 |
|
* |
1179 |
|
* @param reader an image reader |
1180 |
|
* @param writer an image writer |
1181 |
|
* |
1182 |
|
* @return an iterator over a collection of image transcoders |
1183 |
|
* |
1184 |
|
* @exception IllegalArgumentException if either reader or writer is |
1185 |
|
* null |
1186 |
|
*/ |
1187 |
|
public static Iterator getImageTranscoders (ImageReader reader, |
1188 |
|
ImageWriter writer) |
1189 |
|
{ |
1190 |
|
if (reader == null || writer == null) |
1191 |
|
throw new IllegalArgumentException ("null argument"); |
1192 |
|
|
1193 |
|
return getRegistry().getServiceProviders (ImageTranscoderSpi.class, |
1194 |
|
new TranscoderFilter (reader, |
1195 |
|
writer), |
1196 |
|
true); |
1197 |
|
} |
1198 |
} |
} |