Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / imageio / ImageIO.java
blob7afb7207a791898086c486568708c4e6bdb5a3f1
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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.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
65 /**
66 * This class isn't intended to be instantiated.
68 private ImageIO() {}
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]))
88 return true;
91 return false;
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]))
113 return true;
116 return false;
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]))
138 return true;
141 return false;
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]))
163 return true;
166 return false;
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]))
188 return true;
191 return false;
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]))
213 return true;
216 return false;
220 private static final class ImageReaderIterator implements Iterator
222 Iterator it;
223 Object readerExtension;
225 public ImageReaderIterator(Iterator it, Object readerExtension)
227 this.it = it;
228 this.readerExtension = readerExtension;
231 public boolean hasNext()
233 return it.hasNext();
236 public Object next()
240 return ((ImageReaderSpi) it.next()).createReaderInstance(readerExtension);
242 catch (IOException e)
244 return null;
248 public void remove()
250 throw new UnsupportedOperationException();
254 private static final class ImageWriterIterator implements Iterator
256 Iterator it;
257 Object writerExtension;
259 public ImageWriterIterator(Iterator it, Object writerExtension)
261 this.it = it;
262 this.writerExtension = writerExtension;
265 public boolean hasNext()
267 return it.hasNext();
270 public Object next()
274 return ((ImageWriterSpi) it.next()).createWriterInstance(writerExtension);
276 catch (IOException e)
278 return null;
282 public void remove()
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),
333 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),
343 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),
353 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),
363 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),
373 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),
383 fileSuffix);
386 public static String[] getReaderFormatNames()
390 Iterator it =
391 getRegistry().getServiceProviders(ImageReaderSpi.class, true);
392 ArrayList result = new ArrayList();
394 while (it.hasNext())
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()
415 Iterator it =
416 getRegistry().getServiceProviders(ImageReaderSpi.class, true);
417 ArrayList result = new ArrayList();
419 while (it.hasNext())
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()
443 return useCache;
446 public static String[] getWriterFormatNames()
450 Iterator it =
451 getRegistry().getServiceProviders(ImageWriterSpi.class, true);
452 ArrayList result = new ArrayList();
454 while (it.hasNext())
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()
475 Iterator it =
476 getRegistry().getServiceProviders(ImageWriterSpi.class, true);
477 ArrayList result = new ArrayList();
479 while (it.hasNext())
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,
528 String formatName,
529 File output)
530 throws IOException
532 return write(im, formatName, new FileOutputStream(output));
535 public static boolean write(RenderedImage im,
536 String formatName,
537 OutputStream output)
538 throws IOException
540 return write(im, formatName, new MemoryCacheImageOutputStream(output));
544 public static boolean write(RenderedImage im,
545 String formatName,
546 ImageOutputStream output)
547 throws IOException
549 Iterator writers = getImageWritersByFormatName(formatName);
550 IIOImage img = new IIOImage(im, null, null);
551 while (writers.hasNext())
553 ImageWriter w = (ImageWriter) writers.next();
554 try
556 w.setOutput(output);
558 catch (IllegalArgumentException e)
560 continue;
563 w.write(null, img, null);
564 output.close();
565 return true;
567 return false;
570 public static BufferedImage read(ImageInputStream stream)
571 throws IOException
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);
584 return null;
587 public static BufferedImage read(URL input)
588 throws IOException
590 return read(input.openStream());
593 public static BufferedImage read(InputStream input)
594 throws IOException
596 return read(new MemoryCacheImageInputStream(input));
599 public static BufferedImage read(File input)
600 throws IOException
602 return read(new FileInputStream(input));