Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / imageio / ImageIO.java
blobb2304a783041d66fa11cb350a13889912754004b
1 /* ImageIO.java --
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.imageio;
41 import java.awt.image.BufferedImage;
42 import java.awt.image.RenderedImage;
43 import java.io.File;
44 import java.io.FileInputStream;
45 import java.io.FileOutputStream;
46 import java.io.IOException;
47 import java.io.InputStream;
48 import java.io.OutputStream;
49 import java.net.URL;
50 import java.util.ArrayList;
51 import java.util.Collections;
52 import java.util.Iterator;
54 import javax.imageio.spi.IIORegistry;
55 import javax.imageio.spi.ImageInputStreamSpi;
56 import javax.imageio.spi.ImageOutputStreamSpi;
57 import javax.imageio.spi.ImageReaderSpi;
58 import javax.imageio.spi.ImageTranscoderSpi;
59 import javax.imageio.spi.ImageWriterSpi;
60 import javax.imageio.spi.ServiceRegistry;
61 import javax.imageio.stream.ImageInputStream;
62 import javax.imageio.stream.ImageOutputStream;
63 import javax.imageio.stream.MemoryCacheImageInputStream;
64 import javax.imageio.stream.MemoryCacheImageOutputStream;
66 /**
67 * An uninstantiable class that provides static methods for locating
68 * and using image readers and writers.
70 public final class ImageIO
72 /**
73 * Construct an ImageIO. Private since ImageIO is not instantiable.
75 private ImageIO()
79 private static final class ReaderFormatFilter implements ServiceRegistry.Filter
81 private String formatName;
83 public ReaderFormatFilter(String formatName)
85 this.formatName = formatName;
88 public boolean filter (Object provider)
90 if (provider instanceof ImageReaderSpi)
92 ImageReaderSpi spi = (ImageReaderSpi) provider;
93 String[] formatNames = spi.getFormatNames();
95 for (int i = formatNames.length - 1; i >= 0; --i)
96 if (formatName.equals(formatNames[i]))
97 return true;
100 return false;
104 private static final class ReaderMIMETypeFilter implements ServiceRegistry.Filter
106 private String MIMEType;
108 public ReaderMIMETypeFilter(String MIMEType)
110 this.MIMEType = MIMEType;
113 public boolean filter(Object provider)
115 if (provider instanceof ImageReaderSpi)
117 ImageReaderSpi spi = (ImageReaderSpi) provider;
118 String[] mimetypes = spi.getMIMETypes();
120 for (int i = mimetypes.length - 1; i >= 0; --i)
121 if (MIMEType.equals(mimetypes[i]))
122 return true;
125 return false;
129 private static final class ReaderObjectFilter implements ServiceRegistry.Filter
131 private Object object;
133 public ReaderObjectFilter(Object object)
135 this.object = object;
138 public boolean filter(Object provider)
140 if (provider instanceof ImageReaderSpi)
142 ImageReaderSpi spi = (ImageReaderSpi) provider;
146 if (spi.canDecodeInput(object))
147 return true;
149 catch (IOException e)
151 // Return false in this case
154 return false;
158 private static final class ReaderSuffixFilter implements ServiceRegistry.Filter
160 private String fileSuffix;
162 public ReaderSuffixFilter(String fileSuffix)
164 this.fileSuffix = fileSuffix;
167 public boolean filter(Object provider)
169 if (provider instanceof ImageReaderSpi)
171 ImageReaderSpi spi = (ImageReaderSpi) provider;
172 String[] suffixes = spi.getFileSuffixes();
174 for (int i = suffixes.length - 1; i >= 0; --i)
175 if (fileSuffix.equals(suffixes[i]))
176 return true;
179 return false;
183 private static final class WriterFormatFilter implements ServiceRegistry.Filter
185 private String formatName;
187 public WriterFormatFilter(String formatName)
189 this.formatName = formatName;
192 public boolean filter(Object provider)
194 if (provider instanceof ImageWriterSpi)
196 ImageWriterSpi spi = (ImageWriterSpi) provider;
197 String[] formatNames = spi.getFormatNames();
199 for (int i = formatNames.length - 1; i >= 0; --i)
200 if (formatName.equals(formatNames[i]))
201 return true;
204 return false;
208 private static final class WriterMIMETypeFilter implements ServiceRegistry.Filter
210 private String MIMEType;
212 public WriterMIMETypeFilter(String MIMEType)
214 this.MIMEType = MIMEType;
217 public boolean filter(Object provider)
219 if (provider instanceof ImageWriterSpi)
221 ImageWriterSpi spi = (ImageWriterSpi) provider;
222 String[] mimetypes = spi.getMIMETypes();
224 for (int i = mimetypes.length - 1; i >= 0; --i)
225 if (MIMEType.equals(mimetypes[i]))
226 return true;
229 return false;
233 private static final class WriterSuffixFilter implements ServiceRegistry.Filter
235 private String fileSuffix;
237 public WriterSuffixFilter(String fileSuffix)
239 this.fileSuffix = fileSuffix;
242 public boolean filter(Object provider)
244 if (provider instanceof ImageWriterSpi)
246 ImageWriterSpi spi = (ImageWriterSpi) provider;
247 String[] suffixes = spi.getFileSuffixes();
249 for (int i = suffixes.length - 1; i >= 0; --i)
250 if (fileSuffix.equals(suffixes[i]))
251 return true;
254 return false;
258 private static final class WriterObjectFilter implements ServiceRegistry.Filter
260 private ImageTypeSpecifier type;
261 private String formatName;
263 public WriterObjectFilter(ImageTypeSpecifier type,
264 String formatName)
266 this.type = type;
267 this.formatName = formatName;
270 public boolean filter(Object provider)
272 if (provider instanceof ImageWriterSpi)
274 ImageWriterSpi spi = (ImageWriterSpi) provider;
276 if (spi.canEncodeImage(type))
278 String[] formatNames = spi.getFormatNames();
279 for (int i = formatNames.length - 1; i >= 0; --i)
280 if (formatName.equals(formatNames[i]))
281 return true;
285 return false;
289 private static final class TranscoderFilter implements ServiceRegistry.Filter
291 private ImageReader reader;
292 private ImageWriter writer;
294 public TranscoderFilter(ImageReader reader,
295 ImageWriter writer)
297 this.reader = reader;
298 this.writer = writer;
301 public boolean filter(Object provider)
303 if (provider instanceof ImageTranscoderSpi)
305 ImageTranscoderSpi spi = (ImageTranscoderSpi) provider;
307 if (spi.getReaderServiceProviderName().equals
308 (reader.getOriginatingProvider().getClass().getName())
309 && spi.getWriterServiceProviderName().equals
310 (writer.getOriginatingProvider().getClass().getName()))
311 return true;
314 return false;
318 private static final class ImageReaderIterator implements Iterator
320 Iterator it;
321 Object readerExtension;
323 public ImageReaderIterator(Iterator it, Object readerExtension)
325 this.it = it;
326 this.readerExtension = readerExtension;
329 public boolean hasNext()
331 return it.hasNext();
334 public Object next()
338 return ((ImageReaderSpi) it.next()).createReaderInstance(readerExtension);
340 catch (IOException e)
342 return null;
346 public void remove()
348 throw new UnsupportedOperationException();
352 private static final class ImageWriterIterator implements Iterator
354 Iterator it;
355 Object writerExtension;
357 public ImageWriterIterator(Iterator it, Object writerExtension)
359 this.it = it;
360 this.writerExtension = writerExtension;
363 public boolean hasNext()
365 return it.hasNext();
368 public Object next()
372 return ((ImageWriterSpi) it.next()).createWriterInstance(writerExtension);
374 catch (IOException e)
376 return null;
380 public void remove()
382 throw new UnsupportedOperationException();
386 private static File cacheDirectory;
387 private static boolean useCache = true;
389 private static Iterator getReadersByFilter(Class type,
390 ServiceRegistry.Filter filter,
391 Object readerExtension)
395 Iterator it = getRegistry().getServiceProviders(type, filter, true);
396 return new ImageReaderIterator(it, readerExtension);
398 catch (IllegalArgumentException e)
400 return Collections.EMPTY_SET.iterator();
404 private static Iterator getWritersByFilter(Class type,
405 ServiceRegistry.Filter filter,
406 Object writerExtension)
410 Iterator it = getRegistry().getServiceProviders(type, filter, true);
411 return new ImageWriterIterator(it, writerExtension);
413 catch (IllegalArgumentException e)
415 return Collections.EMPTY_SET.iterator();
420 * Retrieve the current cache directory.
422 * @return the current cache directory or null if none is set.
424 public static File getCacheDirectory()
426 return cacheDirectory;
430 * Retrieve an iterator over all registered readers for the given
431 * format.
433 * @param formatName an infomal format name (e.g. "jpeg" or "bmp")
435 * @return an iterator over a collection of image readers
437 * @exception IllegalArgumentException if formatName is null
439 public static Iterator getImageReadersByFormatName(String formatName)
441 if (formatName == null)
442 throw new IllegalArgumentException("formatName may not be null");
444 return getReadersByFilter(ImageReaderSpi.class,
445 new ReaderFormatFilter(formatName),
446 formatName);
450 * Retrieve an iterator over all registered readers for the given
451 * MIME type.
453 * @param MIMEType a MIME specification for an image type
454 * (e.g. "image/jpeg" or "image/x-bmp")
456 * @return an iterator over a collection of image readers
458 * @exception IllegalArgumentException if MIMEType is null
460 public static Iterator getImageReadersByMIMEType(String MIMEType)
462 if (MIMEType == null)
463 throw new IllegalArgumentException("MIMEType may not be null");
465 return getReadersByFilter(ImageReaderSpi.class,
466 new ReaderMIMETypeFilter(MIMEType),
467 MIMEType);
471 * Retrieve an iterator over all registered readers for the given
472 * file suffix.
474 * @param fileSuffix an image file suffix (e.g. "jpg" or "bmp")
476 * @return an iterator over a collection of image readers
478 * @exception IllegalArgumentException if fileSuffix is null
480 public static Iterator getImageReadersBySuffix(String fileSuffix)
482 if (fileSuffix == null)
483 throw new IllegalArgumentException("formatName may not be null");
485 return getReadersByFilter(ImageReaderSpi.class,
486 new ReaderSuffixFilter(fileSuffix),
487 fileSuffix);
491 * Retrieve an iterator over all registered writers for the given
492 * format.
494 * @param formatName an infomal format name (e.g. "jpeg" or "bmp")
496 * @return an iterator over a collection of image writers
498 * @exception IllegalArgumentException if formatName is null
500 public static Iterator getImageWritersByFormatName(String formatName)
502 if (formatName == null)
503 throw new IllegalArgumentException("formatName may not be null");
505 return getWritersByFilter(ImageWriterSpi.class,
506 new WriterFormatFilter(formatName),
507 formatName);
511 * Retrieve an iterator over all registered writers for the given
512 * MIME type.
514 * @param MIMEType a MIME specification for an image type
515 * (e.g. "image/jpeg" or "image/x-bmp")
517 * @return an iterator over a collection of image writers
519 * @exception IllegalArgumentException if MIMEType is null
521 public static Iterator getImageWritersByMIMEType(String MIMEType)
523 if (MIMEType == null)
524 throw new IllegalArgumentException("MIMEType may not be null");
526 return getWritersByFilter(ImageWriterSpi.class,
527 new WriterMIMETypeFilter(MIMEType),
528 MIMEType);
532 * Retrieve an iterator over all registered writers for the given
533 * file suffix.
535 * @param fileSuffix an image file suffix (e.g. "jpg" or "bmp")
537 * @return an iterator over a collection of image writers
539 * @exception IllegalArgumentException if fileSuffix is null
541 public static Iterator getImageWritersBySuffix(String fileSuffix)
543 if (fileSuffix == null)
544 throw new IllegalArgumentException("fileSuffix may not be null");
546 return getWritersByFilter(ImageWriterSpi.class,
547 new WriterSuffixFilter(fileSuffix),
548 fileSuffix);
552 * Retrieve all the informal format names supported by the
553 * collection of registered image readers.
555 * @return an array of format names
557 public static String[] getReaderFormatNames()
561 Iterator it =
562 getRegistry().getServiceProviders(ImageReaderSpi.class, true);
563 ArrayList result = new ArrayList();
565 while (it.hasNext())
567 ImageReaderSpi spi = (ImageReaderSpi) it.next();
568 String[] names = spi.getFormatNames();
570 for (int i = names.length - 1; i >= 0; --i)
571 result.add(names[i]);
574 return (String[]) result.toArray(new String[result.size()]);
576 catch (IllegalArgumentException e)
578 return new String[0];
583 * Retrieve all the MIME types supported by the collection of
584 * registered image readers.
586 * @return an array of MIME types
588 public static String[] getReaderMIMETypes()
592 Iterator it =
593 getRegistry().getServiceProviders(ImageReaderSpi.class, true);
594 ArrayList result = new ArrayList();
596 while (it.hasNext())
598 ImageReaderSpi spi = (ImageReaderSpi) it.next();
599 String[] names = spi.getMIMETypes();
601 for (int i = names.length - 1; i >= 0; --i)
602 result.add(names[i]);
605 return (String[]) result.toArray(new String[result.size()]);
607 catch (IllegalArgumentException e)
609 return new String[0];
613 private static IIORegistry getRegistry()
615 return IIORegistry.getDefaultInstance();
619 * Check whether or not an on-disk cache is used for image input and
620 * output streams.
622 * @return true if an on-disk cache is available, false otherwise
624 public static boolean getUseCache()
626 return useCache;
630 * Retrieve all the informal format names supported by the
631 * collection of registered image writers.
633 * @return an array of format names
635 public static String[] getWriterFormatNames()
639 Iterator it =
640 getRegistry().getServiceProviders(ImageWriterSpi.class, true);
641 ArrayList result = new ArrayList();
643 while (it.hasNext())
645 ImageWriterSpi spi = (ImageWriterSpi) it.next();
646 String[] names = spi.getFormatNames();
648 for (int i = names.length - 1; i >= 0; --i)
649 result.add(names[i]);
652 return (String[]) result.toArray(new String[result.size()]);
654 catch (IllegalArgumentException e)
656 return new String[0];
661 * Retrieve all the MIME types supported by the collection of
662 * registered image writers.
664 * @return an array of MIME types
666 public static String[] getWriterMIMETypes()
670 Iterator it =
671 getRegistry().getServiceProviders(ImageWriterSpi.class, true);
672 ArrayList result = new ArrayList();
674 while (it.hasNext())
676 ImageWriterSpi spi = (ImageWriterSpi) it.next();
677 String[] names = spi.getMIMETypes();
679 for (int i = names.length - 1; i >= 0; --i)
680 result.add(names[i]);
683 return (String[]) result.toArray(new String[result.size()]);
685 catch (IllegalArgumentException e)
687 return new String[0];
692 * Rescans the application classpath for ImageIO service providers
693 * and registers them.
695 public static void scanForPlugins()
697 IIORegistry.getDefaultInstance().registerApplicationClasspathSpis();
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.
705 * @param cacheDirectory the directory where image data should be
706 * cached
708 * @exception IllegalArgumentException if cacheDirectory is not a
709 * directory
711 public static void setCacheDirectory(File cacheDirectory)
713 // FIXME: add SecurityManager call
714 if (cacheDirectory != null)
716 if (!cacheDirectory.isDirectory())
717 throw new IllegalArgumentException("cacheDirectory must be a directory");
719 cacheDirectory.canWrite();
722 ImageIO.cacheDirectory = cacheDirectory;
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.
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.
734 * @param useCache true to use an on-disk cache, false otherwise
736 public static void setUseCache(boolean useCache)
738 ImageIO.useCache = useCache;
742 * Write an image to a file using a registered writer that supports
743 * the given format, overwriting the file if it already exists.
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
749 * @return false if no registered writer supports the given format,
750 * true otherwise
752 * @exception IllegalArgumentException if any argument is null
753 * @exception IOException if a writing error occurs
755 public static boolean write(RenderedImage im,
756 String formatName,
757 File output)
758 throws IOException
760 if (im == null || formatName == null || output == null)
761 throw new IllegalArgumentException ("null argument");
763 return write(im, formatName, new FileOutputStream(output));
767 * Write an image to an output stream using a registered writer that
768 * supports the given format.
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
775 * @return false if no registered writer supports the given format,
776 * true otherwise
778 * @exception IllegalArgumentException if any argument is null
779 * @exception IOException if a writing error occurs
781 public static boolean write(RenderedImage im,
782 String formatName,
783 OutputStream output)
784 throws IOException
786 if (im == null || formatName == null || output == null)
787 throw new IllegalArgumentException ("null argument");
789 return write(im, formatName, new MemoryCacheImageOutputStream(output));
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.
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
803 * @return false if no registered writer supports the given format,
804 * true otherwise
806 * @exception IllegalArgumentException if any argument is null
807 * @exception IOException if a writing error occurs
809 public static boolean write(RenderedImage im,
810 String formatName,
811 ImageOutputStream output)
812 throws IOException
814 if (im == null || formatName == null || output == null)
815 throw new IllegalArgumentException ("null argument");
817 Iterator writers = getImageWritersByFormatName(formatName);
818 IIOImage img = new IIOImage(im, null, null);
819 while (writers.hasNext())
821 ImageWriter w = (ImageWriter) writers.next();
822 try
824 w.setOutput(output);
826 catch (IllegalArgumentException e)
828 continue;
831 w.write(null, img, null);
832 output.close();
833 return true;
835 return false;
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.
844 * @param stream the image input stream from which to read image
845 * data
847 * @return a new buffered image created from the given image data,
848 * or null
850 * @exception IllegalArgumentException if stream is null
851 * @exception IOException if a reading error occurs
853 public static BufferedImage read(ImageInputStream stream)
854 throws IOException
856 if (stream == null)
857 throw new IllegalArgumentException("null argument");
859 Iterator providers = getRegistry().getServiceProviders(ImageReaderSpi.class, true);
860 while (providers.hasNext())
862 ImageReaderSpi spi = (ImageReaderSpi) providers.next();
863 if (spi.canDecodeInput(stream))
865 ImageReader reader = spi.createReaderInstance();
866 reader.setInput(stream);
867 return reader.read(0, null);
870 return null;
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.
879 * The image data will be cached in the current cache directory if
880 * caching is enabled.
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.
886 * @param input the URL from which to retrieve the image file
888 * @return a new buffered image created from the given image URL, or
889 * null
891 * @exception IllegalArgumentException if input is null
892 * @exception IOException if a reading error occurs
894 public static BufferedImage read(URL input)
895 throws IOException
897 if (input == null)
898 throw new IllegalArgumentException("null argument");
900 return read(input.openStream());
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.
909 * The image data will be cached in the current cache directory if
910 * caching is enabled.
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.
916 * @param input the input stream from which to read the image data
918 * @return a new buffered image created from the given input stream,
919 * or null
921 * @exception IllegalArgumentException if input is null
922 * @exception IOException if a reading error occurs
924 public static BufferedImage read(InputStream input)
925 throws IOException
927 if (input == null)
928 throw new IllegalArgumentException("null argument");
930 return read(new MemoryCacheImageInputStream(input));
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.
939 * The image data will be cached in the current cache directory if
940 * caching is enabled.
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.
946 * @param input the file from which to read image data
948 * @return a new buffered image created from the given image file,
949 * or null
951 * @exception IllegalArgumentException if input is null
952 * @exception IOException if a reading error occurs
954 public static BufferedImage read(File input)
955 throws IOException
957 if (input == null)
958 throw new IllegalArgumentException("null argument");
960 return read(new FileInputStream(input));
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.
970 * The image data will be cached in the current cache directory if
971 * caching is enabled.
973 * @param input an object from which to read image data
975 * @return an ImageInputStream that can read data from input, or
976 * null
978 * @exception IllegalArgumentException if input is null
979 * @exception IOException if caching is required but not enabled
981 public static ImageInputStream createImageInputStream (Object input)
982 throws IOException
984 if (input == null)
985 throw new IllegalArgumentException ("null argument");
987 Iterator spis = getRegistry().getServiceProviders
988 (ImageInputStreamSpi.class, true);
990 ImageInputStreamSpi foundSpi = null;
992 while(spis.hasNext())
994 ImageInputStreamSpi spi = (ImageInputStreamSpi) spis.next();
996 if (input.getClass().equals(spi.getInputClass()))
998 foundSpi = spi;
999 break;
1003 return foundSpi == null ? null :
1004 foundSpi.createInputStreamInstance (input,
1005 getUseCache(),
1006 getCacheDirectory());
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.
1016 * The image data will be cached in the current cache directory if
1017 * caching is enabled.
1019 * @param output an object to which to write image data
1021 * @return an ImageOutputStream that can send data to output, or
1022 * null
1024 * @exception IllegalArgumentException if output is null
1025 * @exception IOException if caching is required but not enabled
1027 public static ImageOutputStream createImageOutputStream (Object output)
1028 throws IOException
1030 if (output == null)
1031 throw new IllegalArgumentException ("null argument");
1033 Iterator spis = getRegistry().getServiceProviders
1034 (ImageOutputStreamSpi.class, true);
1036 ImageOutputStreamSpi foundSpi = null;
1038 while(spis.hasNext())
1040 ImageOutputStreamSpi spi = (ImageOutputStreamSpi) spis.next();
1042 if (output.getClass().equals(spi.getOutputClass()))
1044 foundSpi = spi;
1045 break;
1049 return foundSpi == null ? null :
1050 foundSpi.createOutputStreamInstance (output,
1051 getUseCache(),
1052 getCacheDirectory());
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.
1060 * @param writer a registered image writer
1062 * @return an image reader corresponding to writer, or null
1064 * @exception IllegalArgumentException if writer is null
1066 public static ImageReader getImageReader (ImageWriter writer)
1068 if (writer == null)
1069 throw new IllegalArgumentException ("null argument");
1071 ImageWriterSpi spi = (ImageWriterSpi) getRegistry()
1072 .getServiceProviderByClass(writer.getClass());
1074 String[] readerSpiNames = spi.getImageReaderSpiNames();
1076 ImageReader r = null;
1078 if (readerSpiNames != null)
1082 Class readerClass = Class.forName (readerSpiNames[0]);
1083 r = (ImageReader) readerClass.newInstance ();
1085 catch (Exception e)
1087 return null;
1090 return r;
1094 * Retrieve an iterator over the collection of registered image
1095 * readers that support reading data from the given object.
1097 * @param input the object for which to retrieve image readers
1099 * @return an iterator over a collection of image readers
1101 public static Iterator getImageReaders (Object input)
1103 if (input == null)
1104 throw new IllegalArgumentException ("null argument");
1106 return getRegistry().getServiceProviders (ImageReaderSpi.class,
1107 new ReaderObjectFilter(input),
1108 true);
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.
1116 * @param type the output image's colour and sample models
1117 * @param formatName the output image format
1119 * @return an iterator over a collection of image writers
1121 public static Iterator getImageWriters (ImageTypeSpecifier type,
1122 String formatName)
1124 if (type == null || formatName == null)
1125 throw new IllegalArgumentException ("null argument");
1127 return getRegistry().getServiceProviders (ImageWriterSpi.class,
1128 new WriterObjectFilter(type,
1129 formatName),
1130 true);
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.
1141 * @param reader a registered image reader
1143 * @return an image writer corresponding to reader, or null
1145 * @exception IllegalArgumentException if reader is null
1147 public static ImageWriter getImageWriter (ImageReader reader)
1149 if (reader == null)
1150 throw new IllegalArgumentException ("null argument");
1152 ImageReaderSpi spi = (ImageReaderSpi) getRegistry()
1153 .getServiceProviderByClass(reader.getClass());
1155 String[] writerSpiNames = spi.getImageWriterSpiNames();
1157 ImageWriter w = null;
1159 if (writerSpiNames != null)
1163 Class writerClass = Class.forName (writerSpiNames[0]);
1164 w = (ImageWriter) writerClass.newInstance ();
1166 catch (Exception e)
1168 return null;
1171 return w;
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.
1179 * @param reader an image reader
1180 * @param writer an image writer
1182 * @return an iterator over a collection of image transcoders
1184 * @exception IllegalArgumentException if either reader or writer is
1185 * null
1187 public static Iterator getImageTranscoders (ImageReader reader,
1188 ImageWriter writer)
1190 if (reader == null || writer == null)
1191 throw new IllegalArgumentException ("null argument");
1193 return getRegistry().getServiceProviders (ImageTranscoderSpi.class,
1194 new TranscoderFilter (reader,
1195 writer),
1196 true);