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)
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., 59 Temple Place, Suite 330, Boston, MA
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
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
;
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
;
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
.ImageReaderSpi
;
56 import javax
.imageio
.spi
.ImageWriterSpi
;
57 import javax
.imageio
.spi
.ServiceRegistry
;
58 import javax
.imageio
.stream
.ImageInputStream
;
59 import javax
.imageio
.stream
.ImageOutputStream
;
60 import javax
.imageio
.stream
.MemoryCacheImageInputStream
;
61 import javax
.imageio
.stream
.MemoryCacheImageOutputStream
;
63 public final class ImageIO
66 * This class isn't intended to be instantiated.
70 private static final class ReaderFormatFilter
implements ServiceRegistry
.Filter
72 private String formatName
;
74 public ReaderFormatFilter(String formatName
)
76 this.formatName
= formatName
;
79 public boolean filter (Object provider
)
81 if (provider
instanceof ImageReaderSpi
)
83 ImageWriterSpi spi
= (ImageWriterSpi
) provider
;
84 String
[] formatNames
= spi
.getFormatNames();
86 for (int i
= formatNames
.length
- 1; i
>= 0; --i
)
87 if (formatName
.equals(formatNames
[i
]))
95 private static final class ReaderMIMETypeFilter
implements ServiceRegistry
.Filter
97 private String MIMEType
;
99 public ReaderMIMETypeFilter(String MIMEType
)
101 this.MIMEType
= MIMEType
;
104 public boolean filter(Object provider
)
106 if (provider
instanceof ImageReaderSpi
)
108 ImageReaderSpi spi
= (ImageReaderSpi
) provider
;
109 String
[] mimetypes
= spi
.getMIMETypes();
111 for (int i
= mimetypes
.length
- 1; i
>= 0; --i
)
112 if (MIMEType
.equals(mimetypes
[i
]))
120 private static final class ReaderSuffixFilter
implements ServiceRegistry
.Filter
122 private String fileSuffix
;
124 public ReaderSuffixFilter(String fileSuffix
)
126 this.fileSuffix
= fileSuffix
;
129 public boolean filter(Object provider
)
131 if (provider
instanceof ImageReaderSpi
)
133 ImageReaderSpi spi
= (ImageReaderSpi
) provider
;
134 String
[] suffixes
= spi
.getFileSuffixes();
136 for (int i
= suffixes
.length
- 1; i
>= 0; --i
)
137 if (fileSuffix
.equals(suffixes
[i
]))
145 private static final class WriterFormatFilter
implements ServiceRegistry
.Filter
147 private String formatName
;
149 public WriterFormatFilter(String formatName
)
151 this.formatName
= formatName
;
154 public boolean filter(Object provider
)
156 if (provider
instanceof ImageWriterSpi
)
158 ImageWriterSpi spi
= (ImageWriterSpi
) provider
;
159 String
[] formatNames
= spi
.getFormatNames();
161 for (int i
= formatNames
.length
- 1; i
>= 0; --i
)
162 if (formatName
.equals(formatNames
[i
]))
170 private static final class WriterMIMETypeFilter
implements ServiceRegistry
.Filter
172 private String MIMEType
;
174 public WriterMIMETypeFilter(String MIMEType
)
176 this.MIMEType
= MIMEType
;
179 public boolean filter(Object provider
)
181 if (provider
instanceof ImageWriterSpi
)
183 ImageWriterSpi spi
= (ImageWriterSpi
) provider
;
184 String
[] mimetypes
= spi
.getMIMETypes();
186 for (int i
= mimetypes
.length
- 1; i
>= 0; --i
)
187 if (MIMEType
.equals(mimetypes
[i
]))
195 private static final class WriterSuffixFilter
implements ServiceRegistry
.Filter
197 private String fileSuffix
;
199 public WriterSuffixFilter(String fileSuffix
)
201 this.fileSuffix
= fileSuffix
;
204 public boolean filter(Object provider
)
206 if (provider
instanceof ImageWriterSpi
)
208 ImageWriterSpi spi
= (ImageWriterSpi
) provider
;
209 String
[] suffixes
= spi
.getFileSuffixes();
211 for (int i
= suffixes
.length
- 1; i
>= 0; --i
)
212 if (fileSuffix
.equals(suffixes
[i
]))
220 private static final class ImageReaderIterator
implements Iterator
223 Object readerExtension
;
225 public ImageReaderIterator(Iterator it
, Object readerExtension
)
228 this.readerExtension
= readerExtension
;
231 public boolean hasNext()
240 return ((ImageReaderSpi
) it
.next()).createReaderInstance(readerExtension
);
242 catch (IOException e
)
250 throw new UnsupportedOperationException();
254 private static final class ImageWriterIterator
implements Iterator
257 Object writerExtension
;
259 public ImageWriterIterator(Iterator it
, Object writerExtension
)
262 this.writerExtension
= writerExtension
;
265 public boolean hasNext()
274 return ((ImageWriterSpi
) it
.next()).createWriterInstance(writerExtension
);
276 catch (IOException e
)
284 throw new UnsupportedOperationException();
288 private static File cacheDirectory
;
289 private static boolean useCache
= true;
291 private static Iterator
getReadersByFilter(Class type
,
292 ServiceRegistry
.Filter filter
,
293 Object readerExtension
)
297 Iterator it
= getRegistry().getServiceProviders(type
, filter
, true);
298 return new ImageReaderIterator(it
, readerExtension
);
300 catch (IllegalArgumentException e
)
302 return Collections
.EMPTY_SET
.iterator();
306 private static Iterator
getWritersByFilter(Class type
,
307 ServiceRegistry
.Filter filter
,
308 Object writerExtension
)
312 Iterator it
= getRegistry().getServiceProviders(type
, filter
, true);
313 return new ImageWriterIterator(it
, writerExtension
);
315 catch (IllegalArgumentException e
)
317 return Collections
.EMPTY_SET
.iterator();
321 public static File
getCacheDirectory()
323 return cacheDirectory
;
326 public static Iterator
getImageReadersByFormatName(String formatName
)
328 if (formatName
== null)
329 throw new IllegalArgumentException("formatName may not be null");
331 return getReadersByFilter(ImageReaderSpi
.class,
332 new ReaderFormatFilter(formatName
),
336 public static Iterator
getImageReadersByMIMEType(String MIMEType
)
338 if (MIMEType
== null)
339 throw new IllegalArgumentException("MIMEType may not be null");
341 return getReadersByFilter(ImageReaderSpi
.class,
342 new ReaderMIMETypeFilter(MIMEType
),
346 public static Iterator
getImageReadersBySuffix(String fileSuffix
)
348 if (fileSuffix
== null)
349 throw new IllegalArgumentException("formatName may not be null");
351 return getReadersByFilter(ImageReaderSpi
.class,
352 new ReaderSuffixFilter(fileSuffix
),
356 public static Iterator
getImageWritersByFormatName(String formatName
)
358 if (formatName
== null)
359 throw new IllegalArgumentException("formatName may not be null");
361 return getWritersByFilter(ImageWriterSpi
.class,
362 new WriterFormatFilter(formatName
),
366 public static Iterator
getImageWritersByMIMEType(String MIMEType
)
368 if (MIMEType
== null)
369 throw new IllegalArgumentException("MIMEType may not be null");
371 return getWritersByFilter(ImageWriterSpi
.class,
372 new WriterMIMETypeFilter(MIMEType
),
376 public static Iterator
getImageWritersBySuffix(String fileSuffix
)
378 if (fileSuffix
== null)
379 throw new IllegalArgumentException("fileSuffix may not be null");
381 return getWritersByFilter(ImageWriterSpi
.class,
382 new WriterSuffixFilter(fileSuffix
),
386 public static String
[] getReaderFormatNames()
391 getRegistry().getServiceProviders(ImageReaderSpi
.class, true);
392 ArrayList result
= new ArrayList();
396 ImageReaderSpi spi
= (ImageReaderSpi
) it
.next();
397 String
[] names
= spi
.getFormatNames();
399 for (int i
= names
.length
- 1; i
>= 0; --i
)
400 result
.add(names
[i
]);
403 return (String
[]) result
.toArray(new String
[result
.size()]);
405 catch (IllegalArgumentException e
)
407 return new String
[0];
411 public static String
[] getReaderMIMETypes()
416 getRegistry().getServiceProviders(ImageReaderSpi
.class, true);
417 ArrayList result
= new ArrayList();
421 ImageReaderSpi spi
= (ImageReaderSpi
) it
.next();
422 String
[] names
= spi
.getMIMETypes();
424 for (int i
= names
.length
- 1; i
>= 0; --i
)
425 result
.add(names
[i
]);
428 return (String
[]) result
.toArray(new String
[result
.size()]);
430 catch (IllegalArgumentException e
)
432 return new String
[0];
436 private static IIORegistry
getRegistry()
438 return IIORegistry
.getDefaultInstance();
441 public static boolean getUseCache()
446 public static String
[] getWriterFormatNames()
451 getRegistry().getServiceProviders(ImageWriterSpi
.class, true);
452 ArrayList result
= new ArrayList();
456 ImageWriterSpi spi
= (ImageWriterSpi
) it
.next();
457 String
[] names
= spi
.getFormatNames();
459 for (int i
= names
.length
- 1; i
>= 0; --i
)
460 result
.add(names
[i
]);
463 return (String
[]) result
.toArray(new String
[result
.size()]);
465 catch (IllegalArgumentException e
)
467 return new String
[0];
471 public static String
[] getWriterMIMETypes()
476 getRegistry().getServiceProviders(ImageWriterSpi
.class, true);
477 ArrayList result
= new ArrayList();
481 ImageWriterSpi spi
= (ImageWriterSpi
) it
.next();
482 String
[] names
= spi
.getMIMETypes();
484 for (int i
= names
.length
- 1; i
>= 0; --i
)
485 result
.add(names
[i
]);
488 return (String
[]) result
.toArray(new String
[result
.size()]);
490 catch (IllegalArgumentException e
)
492 return new String
[0];
497 * Rescans the application classpath for ImageIO service providers
498 * and registers them.
500 public static void scanForPlugins()
502 IIORegistry
.getDefaultInstance().registerApplicationClasspathSpis();
505 public static void setCacheDirectory(File cacheDirectory
)
507 if (cacheDirectory
!= null)
509 if (!cacheDirectory
.isDirectory())
510 throw new IllegalArgumentException("cacheDirectory must be a directory");
512 cacheDirectory
.canWrite();
515 ImageIO
.cacheDirectory
= cacheDirectory
;
518 public static void setUseCache(boolean useCache
)
520 ImageIO
.useCache
= useCache
;
524 * "Standard" simplified entry points.
527 public static boolean write(RenderedImage im
,
532 return write(im
, formatName
, new FileOutputStream(output
));
535 public static boolean write(RenderedImage im
,
540 return write(im
, formatName
, new MemoryCacheImageOutputStream(output
));
544 public static boolean write(RenderedImage im
,
546 ImageOutputStream output
)
549 Iterator writers
= getImageWritersByFormatName(formatName
);
550 IIOImage img
= new IIOImage(im
, null, null);
551 while (writers
.hasNext())
553 ImageWriter w
= (ImageWriter
) writers
.next();
558 catch (IllegalArgumentException e
)
563 w
.write(null, img
, null);
570 public static BufferedImage
read(ImageInputStream stream
)
573 Iterator providers
= getRegistry().getServiceProviders(ImageReaderSpi
.class, true);
574 while (providers
.hasNext())
576 ImageReaderSpi spi
= (ImageReaderSpi
) providers
.next();
577 if (spi
.canDecodeInput(stream
))
579 ImageReader reader
= spi
.createReaderInstance();
580 reader
.setInput(stream
);
581 return reader
.read(0, null);
587 public static BufferedImage
read(URL input
)
590 return read(input
.openStream());
593 public static BufferedImage
read(InputStream input
)
596 return read(new MemoryCacheImageInputStream(input
));
599 public static BufferedImage
read(File input
)
602 return read(new FileInputStream(input
));