2015-05-05 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / libjava / classpath / javax / imageio / ImageIO.java
blob9abb1c818908aa53c6418400917cd7dff39b9117
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
319 implements Iterator<ImageReader>
321 Iterator<ImageReaderSpi> it;
322 Object readerExtension;
324 public ImageReaderIterator(Iterator<ImageReaderSpi> it,
325 Object readerExtension)
327 this.it = it;
328 this.readerExtension = readerExtension;
331 public ImageReaderIterator(Iterator<ImageReaderSpi> it)
333 this.it = it;
336 public boolean hasNext()
338 return it.hasNext();
341 public ImageReader next()
345 ImageReaderSpi spi = it.next();
346 return (readerExtension == null
347 ? spi.createReaderInstance()
348 : spi.createReaderInstance(readerExtension));
350 catch (IOException e)
352 return null;
356 public void remove()
358 throw new UnsupportedOperationException();
362 private static final class ImageWriterIterator
363 implements Iterator<ImageWriter>
365 Iterator<ImageWriterSpi> it;
366 Object writerExtension;
368 public ImageWriterIterator(Iterator<ImageWriterSpi> it,
369 Object writerExtension)
371 this.it = it;
372 this.writerExtension = writerExtension;
375 public ImageWriterIterator(Iterator<ImageWriterSpi> it)
377 this.it = it;
380 public boolean hasNext()
382 return it.hasNext();
385 public ImageWriter next()
389 ImageWriterSpi spi = it.next();
390 return (writerExtension == null
391 ? spi.createWriterInstance()
392 : spi.createWriterInstance(writerExtension));
394 catch (IOException e)
396 return null;
400 public void remove()
402 throw new UnsupportedOperationException();
406 private static File cacheDirectory;
407 private static boolean useCache = true;
409 private static Iterator<ImageReader> getReadersByFilter(Class<ImageReaderSpi> type,
410 ServiceRegistry.Filter filter,
411 Object readerExtension)
415 Iterator<ImageReaderSpi> it
416 = getRegistry().getServiceProviders(type, filter, true);
417 return new ImageReaderIterator(it, readerExtension);
419 catch (IllegalArgumentException e)
421 return Collections.EMPTY_SET.iterator();
425 private static Iterator<ImageWriter> getWritersByFilter(Class<ImageWriterSpi> type,
426 ServiceRegistry.Filter filter,
427 Object writerExtension)
431 Iterator<ImageWriterSpi> it
432 = getRegistry().getServiceProviders(type, filter, true);
433 return new ImageWriterIterator(it, writerExtension);
435 catch (IllegalArgumentException e)
437 return Collections.EMPTY_SET.iterator();
442 * Retrieve the current cache directory.
444 * @return the current cache directory or null if none is set.
446 public static File getCacheDirectory()
448 return cacheDirectory;
452 * Retrieve an iterator over all registered readers for the given
453 * format.
455 * @param formatName an infomal format name (e.g. "jpeg" or "bmp")
457 * @return an iterator over a collection of image readers
459 * @exception IllegalArgumentException if formatName is null
461 public static Iterator<ImageReader> getImageReadersByFormatName(String formatName)
463 if (formatName == null)
464 throw new IllegalArgumentException("formatName may not be null");
466 return getReadersByFilter(ImageReaderSpi.class,
467 new ReaderFormatFilter(formatName),
468 formatName);
472 * Retrieve an iterator over all registered readers for the given
473 * MIME type.
475 * @param MIMEType a MIME specification for an image type
476 * (e.g. "image/jpeg" or "image/x-bmp")
478 * @return an iterator over a collection of image readers
480 * @exception IllegalArgumentException if MIMEType is null
482 public static Iterator<ImageReader> getImageReadersByMIMEType(String MIMEType)
484 if (MIMEType == null)
485 throw new IllegalArgumentException("MIMEType may not be null");
487 return getReadersByFilter(ImageReaderSpi.class,
488 new ReaderMIMETypeFilter(MIMEType),
489 MIMEType);
493 * Retrieve an iterator over all registered readers for the given
494 * file suffix.
496 * @param fileSuffix an image file suffix (e.g. "jpg" or "bmp")
498 * @return an iterator over a collection of image readers
500 * @exception IllegalArgumentException if fileSuffix is null
502 public static Iterator<ImageReader> getImageReadersBySuffix(String fileSuffix)
504 if (fileSuffix == null)
505 throw new IllegalArgumentException("formatName may not be null");
507 return getReadersByFilter(ImageReaderSpi.class,
508 new ReaderSuffixFilter(fileSuffix),
509 fileSuffix);
513 * Retrieve an iterator over all registered writers for the given
514 * format.
516 * @param formatName an infomal format name (e.g. "jpeg" or "bmp")
518 * @return an iterator over a collection of image writers
520 * @exception IllegalArgumentException if formatName is null
522 public static Iterator<ImageWriter> getImageWritersByFormatName(String formatName)
524 if (formatName == null)
525 throw new IllegalArgumentException("formatName may not be null");
527 return getWritersByFilter(ImageWriterSpi.class,
528 new WriterFormatFilter(formatName),
529 formatName);
533 * Retrieve an iterator over all registered writers for the given
534 * MIME type.
536 * @param MIMEType a MIME specification for an image type
537 * (e.g. "image/jpeg" or "image/x-bmp")
539 * @return an iterator over a collection of image writers
541 * @exception IllegalArgumentException if MIMEType is null
543 public static Iterator<ImageWriter> getImageWritersByMIMEType(String MIMEType)
545 if (MIMEType == null)
546 throw new IllegalArgumentException("MIMEType may not be null");
548 return getWritersByFilter(ImageWriterSpi.class,
549 new WriterMIMETypeFilter(MIMEType),
550 MIMEType);
554 * Retrieve an iterator over all registered writers for the given
555 * file suffix.
557 * @param fileSuffix an image file suffix (e.g. "jpg" or "bmp")
559 * @return an iterator over a collection of image writers
561 * @exception IllegalArgumentException if fileSuffix is null
563 public static Iterator<ImageWriter> getImageWritersBySuffix(String fileSuffix)
565 if (fileSuffix == null)
566 throw new IllegalArgumentException("fileSuffix may not be null");
568 return getWritersByFilter(ImageWriterSpi.class,
569 new WriterSuffixFilter(fileSuffix),
570 fileSuffix);
574 * Retrieve all the informal format names supported by the
575 * collection of registered image readers.
577 * @return an array of format names
579 public static String[] getReaderFormatNames()
583 Iterator it =
584 getRegistry().getServiceProviders(ImageReaderSpi.class, true);
585 ArrayList result = new ArrayList();
587 while (it.hasNext())
589 ImageReaderSpi spi = (ImageReaderSpi) it.next();
590 String[] names = spi.getFormatNames();
592 for (int i = names.length - 1; i >= 0; --i)
593 result.add(names[i]);
596 return (String[]) result.toArray(new String[result.size()]);
598 catch (IllegalArgumentException e)
600 return new String[0];
605 * Retrieve all the MIME types supported by the collection of
606 * registered image readers.
608 * @return an array of MIME types
610 public static String[] getReaderMIMETypes()
614 Iterator it =
615 getRegistry().getServiceProviders(ImageReaderSpi.class, true);
616 ArrayList result = new ArrayList();
618 while (it.hasNext())
620 ImageReaderSpi spi = (ImageReaderSpi) it.next();
621 String[] names = spi.getMIMETypes();
623 for (int i = names.length - 1; i >= 0; --i)
624 result.add(names[i]);
627 return (String[]) result.toArray(new String[result.size()]);
629 catch (IllegalArgumentException e)
631 return new String[0];
635 private static IIORegistry getRegistry()
637 return IIORegistry.getDefaultInstance();
641 * Check whether or not an on-disk cache is used for image input and
642 * output streams.
644 * @return true if an on-disk cache is available, false otherwise
646 public static boolean getUseCache()
648 return useCache;
652 * Retrieve all the informal format names supported by the
653 * collection of registered image writers.
655 * @return an array of format names
657 public static String[] getWriterFormatNames()
661 Iterator it =
662 getRegistry().getServiceProviders(ImageWriterSpi.class, true);
663 ArrayList result = new ArrayList();
665 while (it.hasNext())
667 ImageWriterSpi spi = (ImageWriterSpi) it.next();
668 String[] names = spi.getFormatNames();
670 for (int i = names.length - 1; i >= 0; --i)
671 result.add(names[i]);
674 return (String[]) result.toArray(new String[result.size()]);
676 catch (IllegalArgumentException e)
678 return new String[0];
683 * Retrieve all the MIME types supported by the collection of
684 * registered image writers.
686 * @return an array of MIME types
688 public static String[] getWriterMIMETypes()
692 Iterator it =
693 getRegistry().getServiceProviders(ImageWriterSpi.class, true);
694 ArrayList result = new ArrayList();
696 while (it.hasNext())
698 ImageWriterSpi spi = (ImageWriterSpi) it.next();
699 String[] names = spi.getMIMETypes();
701 for (int i = names.length - 1; i >= 0; --i)
702 result.add(names[i]);
705 return (String[]) result.toArray(new String[result.size()]);
707 catch (IllegalArgumentException e)
709 return new String[0];
714 * Rescans the application classpath for ImageIO service providers
715 * and registers them.
717 public static void scanForPlugins()
719 IIORegistry.getDefaultInstance().registerApplicationClasspathSpis();
723 * Set the directory to be used for caching image data. A null
724 * argument means to use the default system temporary directory.
725 * This cache directory is only used if getUseCache returns true.
727 * @param cacheDirectory the directory where image data should be
728 * cached
730 * @exception IllegalArgumentException if cacheDirectory is not a
731 * directory
733 public static void setCacheDirectory(File cacheDirectory)
735 // FIXME: add SecurityManager call
736 if (cacheDirectory != null)
738 if (!cacheDirectory.isDirectory())
739 throw new IllegalArgumentException("cacheDirectory must be a directory");
741 cacheDirectory.canWrite();
744 ImageIO.cacheDirectory = cacheDirectory;
748 * Control whether or not an on-disk cache is used. This cache is
749 * used to store input or output data from an image data stream when
750 * data in the stream needs to be re-processed.
752 * If useCache is false the cache will be stored in memory. Doing
753 * so eliminates file creation and deletion overhead. The default
754 * is to use an on-disk cache.
756 * @param useCache true to use an on-disk cache, false otherwise
758 public static void setUseCache(boolean useCache)
760 ImageIO.useCache = useCache;
764 * Write an image to a file using a registered writer that supports
765 * the given format, overwriting the file if it already exists.
767 * @param im the image data to write
768 * @param formatName an informal description of the output format
769 * @param output the file to which the image will be written
771 * @return false if no registered writer supports the given format,
772 * true otherwise
774 * @exception IllegalArgumentException if any argument is null
775 * @exception IOException if a writing error occurs
777 public static boolean write(RenderedImage im,
778 String formatName,
779 File output)
780 throws IOException
782 if (im == null || formatName == null || output == null)
783 throw new IllegalArgumentException ("null argument");
785 return write(im, formatName, new FileOutputStream(output));
789 * Write an image to an output stream using a registered writer that
790 * supports the given format.
792 * @param im the image data to write
793 * @param formatName an informal description of the output format
794 * @param output the output stream to which the image will be
795 * written
797 * @return false if no registered writer supports the given format,
798 * true otherwise
800 * @exception IllegalArgumentException if any argument is null
801 * @exception IOException if a writing error occurs
803 public static boolean write(RenderedImage im,
804 String formatName,
805 OutputStream output)
806 throws IOException
808 if (im == null || formatName == null || output == null)
809 throw new IllegalArgumentException ("null argument");
811 return write(im, formatName, new MemoryCacheImageOutputStream(output));
815 * Write an image to an ImageOutputStream using a registered writer
816 * that supports the given format. Image data is written starting
817 * at the ImageOutputStream's current stream pointer, overwriting
818 * any existing data.
820 * @param im the image data to write
821 * @param formatName an informal description of the output format
822 * @param output the image output stream to which the image will be
823 * written
825 * @return false if no registered writer supports the given format,
826 * true otherwise
828 * @exception IllegalArgumentException if any argument is null
829 * @exception IOException if a writing error occurs
831 public static boolean write(RenderedImage im,
832 String formatName,
833 ImageOutputStream output)
834 throws IOException
836 if (im == null || formatName == null || output == null)
837 throw new IllegalArgumentException ("null argument");
839 Iterator writers = getImageWritersByFormatName(formatName);
840 IIOImage img = new IIOImage(im, null, null);
841 while (writers.hasNext())
843 ImageWriter w = (ImageWriter) writers.next();
846 w.setOutput(output);
848 catch (IllegalArgumentException e)
850 continue;
853 w.write(null, img, null);
854 w.dispose();
855 output.close();
856 return true;
858 return false;
862 * Create a buffered image from an image input stream. An image
863 * reader that supports the given image data is automatically
864 * selected from the collection of registered readers. If no
865 * registered reader can handle the input format, null is returned.
867 * @param stream the image input stream from which to read image
868 * data
870 * @return a new buffered image created from the given image data,
871 * or null
873 * @exception IllegalArgumentException if stream is null
874 * @exception IOException if a reading error occurs
876 public static BufferedImage read(ImageInputStream stream)
877 throws IOException
879 if (stream == null)
880 throw new IllegalArgumentException("null argument");
882 Iterator providers = getRegistry().getServiceProviders(ImageReaderSpi.class, true);
883 while (providers.hasNext())
885 ImageReaderSpi spi = (ImageReaderSpi) providers.next();
886 if (spi.canDecodeInput(stream))
888 ImageReader reader = spi.createReaderInstance();
889 reader.setInput(stream);
890 return reader.read(0, null);
893 return null;
897 * Create a buffered image from a URL. An image reader that
898 * supports the given image data is automatically selected from the
899 * collection of registered readers. If no registered reader can
900 * handle the input format, null is returned.
902 * The image data will be cached in the current cache directory if
903 * caching is enabled.
905 * This method does not locate readers that read data directly from
906 * a URL. To locate such readers manually, use IIORegistry and
907 * ImageReaderSpi.
909 * @param input the URL from which to retrieve the image file
911 * @return a new buffered image created from the given image URL, or
912 * null
914 * @exception IllegalArgumentException if input is null
915 * @exception IOException if a reading error occurs
917 public static BufferedImage read(URL input)
918 throws IOException
920 if (input == null)
921 throw new IllegalArgumentException("null argument");
923 return read(input.openStream());
927 * Create a buffered image from an input stream. An image reader
928 * that supports the given image data is automatically selected from
929 * the collection of registered readers. If no registered reader
930 * can handle the input format, null is returned.
932 * The image data will be cached in the current cache directory if
933 * caching is enabled.
935 * This method does not locate readers that read data directly from
936 * an input stream. To locate such readers manually, use
937 * IIORegistry and ImageReaderSpi.
939 * @param input the input stream from which to read the image data
941 * @return a new buffered image created from the given input stream,
942 * or null
944 * @exception IllegalArgumentException if input is null
945 * @exception IOException if a reading error occurs
947 public static BufferedImage read(InputStream input)
948 throws IOException
950 if (input == null)
951 throw new IllegalArgumentException("null argument");
953 return read(new MemoryCacheImageInputStream(input));
957 * Create a buffered image from a file. An image reader that
958 * supports the given image data is automatically selected from the
959 * collection of registered readers. If no registered reader can
960 * handle the input format, null is returned.
962 * The image data will be cached in the current cache directory if
963 * caching is enabled.
965 * This method does not locate readers that read data directly from
966 * a file. To locate such readers manually, use IIORegistry and
967 * ImageReaderSpi.
969 * @param input the file from which to read image data
971 * @return a new buffered image created from the given image file,
972 * or null
974 * @exception IllegalArgumentException if input is null
975 * @exception IOException if a reading error occurs
977 public static BufferedImage read(File input)
978 throws IOException
980 if (input == null)
981 throw new IllegalArgumentException("null argument");
983 return read(new FileInputStream(input));
987 * Create an image input stream from the given object. The
988 * collection of ImageInputStreamSpis registered with the
989 * IIORegistry is searched for an image input stream that can take
990 * input from the given object. null is returned if no such SPI is
991 * registered.
993 * The image data will be cached in the current cache directory if
994 * caching is enabled.
996 * @param input an object from which to read image data
998 * @return an ImageInputStream that can read data from input, or
999 * null
1001 * @exception IllegalArgumentException if input is null
1002 * @exception IOException if caching is required but not enabled
1004 public static ImageInputStream createImageInputStream (Object input)
1005 throws IOException
1007 if (input == null)
1008 throw new IllegalArgumentException ("null argument");
1010 Iterator spis = getRegistry().getServiceProviders
1011 (ImageInputStreamSpi.class, true);
1013 ImageInputStreamSpi foundSpi = null;
1015 while(spis.hasNext())
1017 ImageInputStreamSpi spi = (ImageInputStreamSpi) spis.next();
1019 if (input.getClass().equals(spi.getInputClass()))
1021 foundSpi = spi;
1022 break;
1026 return foundSpi == null ? null :
1027 foundSpi.createInputStreamInstance (input,
1028 getUseCache(),
1029 getCacheDirectory());
1033 * Create an image output stream from the given object. The
1034 * collection of ImageOutputStreamSpis registered with the
1035 * IIORegistry is searched for an image output stream that can send
1036 * output to the given object. null is returned if no such SPI is
1037 * registered.
1039 * The image data will be cached in the current cache directory if
1040 * caching is enabled.
1042 * @param output an object to which to write image data
1044 * @return an ImageOutputStream that can send data to output, or
1045 * null
1047 * @exception IllegalArgumentException if output is null
1048 * @exception IOException if caching is required but not enabled
1050 public static ImageOutputStream createImageOutputStream (Object output)
1051 throws IOException
1053 if (output == null)
1054 throw new IllegalArgumentException ("null argument");
1056 Iterator spis = getRegistry().getServiceProviders
1057 (ImageOutputStreamSpi.class, true);
1059 ImageOutputStreamSpi foundSpi = null;
1061 while(spis.hasNext())
1063 ImageOutputStreamSpi spi = (ImageOutputStreamSpi) spis.next();
1065 if (output.getClass().equals(spi.getOutputClass()))
1067 foundSpi = spi;
1068 break;
1072 return foundSpi == null ? null :
1073 foundSpi.createOutputStreamInstance (output,
1074 getUseCache(),
1075 getCacheDirectory());
1079 * Retrieve an image reader corresponding to an image writer, or
1080 * null if writer is not registered or if no corresponding reader is
1081 * registered.
1083 * @param writer a registered image writer
1085 * @return an image reader corresponding to writer, or null
1087 * @exception IllegalArgumentException if writer is null
1089 public static ImageReader getImageReader (ImageWriter writer)
1091 if (writer == null)
1092 throw new IllegalArgumentException ("null argument");
1094 ImageWriterSpi spi = writer.getOriginatingProvider();
1096 String[] readerSpiNames = spi.getImageReaderSpiNames();
1098 ImageReader r = null;
1100 if (readerSpiNames != null)
1104 Class readerClass = Class.forName (readerSpiNames[0]);
1105 r = (ImageReader) readerClass.newInstance ();
1107 catch (Exception e)
1109 return null;
1112 return r;
1116 * Retrieve an iterator over the collection of registered image
1117 * readers that support reading data from the given object.
1119 * @param input the object for which to retrieve image readers
1121 * @return an iterator over a collection of image readers
1123 public static Iterator<ImageReader> getImageReaders (Object input)
1125 if (input == null)
1126 throw new IllegalArgumentException ("null argument");
1128 Iterator<ImageReaderSpi> spiIterator
1129 = getRegistry().getServiceProviders (ImageReaderSpi.class,
1130 new ReaderObjectFilter(input),
1131 true);
1132 return new ImageReaderIterator(spiIterator);
1136 * Retrieve an iterator over the collection of registered image
1137 * writers that support writing images of the given type and in the
1138 * given format.
1140 * @param type the output image's colour and sample models
1141 * @param formatName the output image format
1143 * @return an iterator over a collection of image writers
1145 public static Iterator<ImageWriter> getImageWriters (ImageTypeSpecifier type,
1146 String formatName)
1148 if (type == null || formatName == null)
1149 throw new IllegalArgumentException ("null argument");
1151 final Iterator<ImageWriterSpi> spiIterator
1152 = getRegistry().getServiceProviders (ImageWriterSpi.class,
1153 new WriterObjectFilter(type,
1154 formatName),
1155 true);
1156 return new ImageWriterIterator(spiIterator);
1160 * Retrieve an image writer corresponding to an image reader, or
1161 * null if reader is not registered or if no corresponding writer is
1162 * registered. This method is useful for preserving metadata
1163 * without needing to understand its format, since the returned
1164 * writer will be able to write, unchanged, the metadata passed to
1165 * it by the reader.
1167 * @param reader a registered image reader
1169 * @return an image writer corresponding to reader, or null
1171 * @exception IllegalArgumentException if reader is null
1173 public static ImageWriter getImageWriter (ImageReader reader)
1175 if (reader == null)
1176 throw new IllegalArgumentException ("null argument");
1178 ImageReaderSpi spi = reader.getOriginatingProvider();
1180 String[] writerSpiNames = spi.getImageWriterSpiNames();
1182 ImageWriter w = null;
1184 if (writerSpiNames != null)
1188 Class writerClass = Class.forName (writerSpiNames[0]);
1189 w = (ImageWriter) writerClass.newInstance ();
1191 catch (Exception e)
1193 return null;
1196 return w;
1200 * Retrieve an iterator over a collection of image transcoders that
1201 * support transcoding from the given image reader's metadata format
1202 * to the given writer's metadata format.
1204 * @param reader an image reader
1205 * @param writer an image writer
1207 * @return an iterator over a collection of image transcoders
1209 * @exception IllegalArgumentException if either reader or writer is
1210 * null
1212 public static Iterator<ImageTranscoder> getImageTranscoders (ImageReader reader,
1213 ImageWriter writer)
1215 if (reader == null || writer == null)
1216 throw new IllegalArgumentException ("null argument");
1218 final Iterator<ImageTranscoderSpi> spiIterator
1219 = getRegistry().getServiceProviders (ImageTranscoderSpi.class,
1220 new TranscoderFilter (reader,
1221 writer),
1222 true);
1223 return new Iterator<ImageTranscoder>()
1225 public boolean hasNext()
1227 return spiIterator.hasNext();
1230 public ImageTranscoder next()
1232 return spiIterator.next().createTranscoderInstance();
1235 public void remove()
1237 throw new UnsupportedOperationException();