1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53:
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65:
66:
70: public final class ImageIO
71: {
72:
75: private ImageIO()
76: {
77: }
78:
79: private static final class ReaderFormatFilter implements ServiceRegistry.Filter
80: {
81: private String formatName;
82:
83: public ReaderFormatFilter(String formatName)
84: {
85: this.formatName = formatName;
86: }
87:
88: public boolean filter (Object provider)
89: {
90: if (provider instanceof ImageReaderSpi)
91: {
92: ImageReaderSpi spi = (ImageReaderSpi) provider;
93: String[] formatNames = spi.getFormatNames();
94:
95: for (int i = formatNames.length - 1; i >= 0; --i)
96: if (formatName.equals(formatNames[i]))
97: return true;
98: }
99:
100: return false;
101: }
102: }
103:
104: private static final class ReaderMIMETypeFilter implements ServiceRegistry.Filter
105: {
106: private String MIMEType;
107:
108: public ReaderMIMETypeFilter(String MIMEType)
109: {
110: this.MIMEType = MIMEType;
111: }
112:
113: public boolean filter(Object provider)
114: {
115: if (provider instanceof ImageReaderSpi)
116: {
117: ImageReaderSpi spi = (ImageReaderSpi) provider;
118: String[] mimetypes = spi.getMIMETypes();
119:
120: for (int i = mimetypes.length - 1; i >= 0; --i)
121: if (MIMEType.equals(mimetypes[i]))
122: return true;
123: }
124:
125: return false;
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:
152: }
153: }
154: return false;
155: }
156: }
157:
158: private static final class ReaderSuffixFilter implements ServiceRegistry.Filter
159: {
160: private String fileSuffix;
161:
162: public ReaderSuffixFilter(String fileSuffix)
163: {
164: this.fileSuffix = fileSuffix;
165: }
166:
167: public boolean filter(Object provider)
168: {
169: if (provider instanceof ImageReaderSpi)
170: {
171: ImageReaderSpi spi = (ImageReaderSpi) provider;
172: String[] suffixes = spi.getFileSuffixes();
173:
174: for (int i = suffixes.length - 1; i >= 0; --i)
175: if (fileSuffix.equals(suffixes[i]))
176: return true;
177: }
178:
179: return false;
180: }
181: }
182:
183: private static final class WriterFormatFilter implements ServiceRegistry.Filter
184: {
185: private String formatName;
186:
187: public WriterFormatFilter(String formatName)
188: {
189: this.formatName = formatName;
190: }
191:
192: public boolean filter(Object provider)
193: {
194: if (provider instanceof ImageWriterSpi)
195: {
196: ImageWriterSpi spi = (ImageWriterSpi) provider;
197: String[] formatNames = spi.getFormatNames();
198:
199: for (int i = formatNames.length - 1; i >= 0; --i)
200: if (formatName.equals(formatNames[i]))
201: return true;
202: }
203:
204: return false;
205: }
206: }
207:
208: private static final class WriterMIMETypeFilter implements ServiceRegistry.Filter
209: {
210: private String MIMEType;
211:
212: public WriterMIMETypeFilter(String MIMEType)
213: {
214: this.MIMEType = MIMEType;
215: }
216:
217: public boolean filter(Object provider)
218: {
219: if (provider instanceof ImageWriterSpi)
220: {
221: ImageWriterSpi spi = (ImageWriterSpi) provider;
222: String[] mimetypes = spi.getMIMETypes();
223:
224: for (int i = mimetypes.length - 1; i >= 0; --i)
225: if (MIMEType.equals(mimetypes[i]))
226: return true;
227: }
228:
229: return false;
230: }
231: }
232:
233: private static final class WriterSuffixFilter implements ServiceRegistry.Filter
234: {
235: private String fileSuffix;
236:
237: public WriterSuffixFilter(String fileSuffix)
238: {
239: this.fileSuffix = fileSuffix;
240: }
241:
242: public boolean filter(Object provider)
243: {
244: if (provider instanceof ImageWriterSpi)
245: {
246: ImageWriterSpi spi = (ImageWriterSpi) provider;
247: String[] suffixes = spi.getFileSuffixes();
248:
249: for (int i = suffixes.length - 1; i >= 0; --i)
250: if (fileSuffix.equals(suffixes[i]))
251: return true;
252: }
253:
254: return false;
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
319: {
320: Iterator it;
321: Object readerExtension;
322:
323: public ImageReaderIterator(Iterator it, Object readerExtension)
324: {
325: this.it = it;
326: this.readerExtension = readerExtension;
327: }
328:
329: public boolean hasNext()
330: {
331: return it.hasNext();
332: }
333:
334: public Object next()
335: {
336: try
337: {
338: return ((ImageReaderSpi) it.next()).createReaderInstance(readerExtension);
339: }
340: catch (IOException e)
341: {
342: return null;
343: }
344: }
345:
346: public void remove()
347: {
348: throw new UnsupportedOperationException();
349: }
350: }
351:
352: private static final class ImageWriterIterator implements Iterator
353: {
354: Iterator it;
355: Object writerExtension;
356:
357: public ImageWriterIterator(Iterator it, Object writerExtension)
358: {
359: this.it = it;
360: this.writerExtension = writerExtension;
361: }
362:
363: public boolean hasNext()
364: {
365: return it.hasNext();
366: }
367:
368: public Object next()
369: {
370: try
371: {
372: return ((ImageWriterSpi) it.next()).createWriterInstance(writerExtension);
373: }
374: catch (IOException e)
375: {
376: return null;
377: }
378: }
379:
380: public void remove()
381: {
382: throw new UnsupportedOperationException();
383: }
384: }
385:
386: private static File cacheDirectory;
387: private static boolean useCache = true;
388:
389: private static Iterator getReadersByFilter(Class type,
390: ServiceRegistry.Filter filter,
391: Object readerExtension)
392: {
393: try
394: {
395: Iterator it = getRegistry().getServiceProviders(type, filter, true);
396: return new ImageReaderIterator(it, readerExtension);
397: }
398: catch (IllegalArgumentException e)
399: {
400: return Collections.EMPTY_SET.iterator();
401: }
402: }
403:
404: private static Iterator getWritersByFilter(Class type,
405: ServiceRegistry.Filter filter,
406: Object writerExtension)
407: {
408: try
409: {
410: Iterator it = getRegistry().getServiceProviders(type, filter, true);
411: return new ImageWriterIterator(it, writerExtension);
412: }
413: catch (IllegalArgumentException e)
414: {
415: return Collections.EMPTY_SET.iterator();
416: }
417: }
418:
419:
424: public static File getCacheDirectory()
425: {
426: return cacheDirectory;
427: }
428:
429:
439: public static Iterator getImageReadersByFormatName(String formatName)
440: {
441: if (formatName == null)
442: throw new IllegalArgumentException("formatName may not be null");
443:
444: return getReadersByFilter(ImageReaderSpi.class,
445: new ReaderFormatFilter(formatName),
446: formatName);
447: }
448:
449:
460: public static Iterator getImageReadersByMIMEType(String MIMEType)
461: {
462: if (MIMEType == null)
463: throw new IllegalArgumentException("MIMEType may not be null");
464:
465: return getReadersByFilter(ImageReaderSpi.class,
466: new ReaderMIMETypeFilter(MIMEType),
467: MIMEType);
468: }
469:
470:
480: public static Iterator getImageReadersBySuffix(String fileSuffix)
481: {
482: if (fileSuffix == null)
483: throw new IllegalArgumentException("formatName may not be null");
484:
485: return getReadersByFilter(ImageReaderSpi.class,
486: new ReaderSuffixFilter(fileSuffix),
487: fileSuffix);
488: }
489:
490:
500: public static Iterator getImageWritersByFormatName(String formatName)
501: {
502: if (formatName == null)
503: throw new IllegalArgumentException("formatName may not be null");
504:
505: return getWritersByFilter(ImageWriterSpi.class,
506: new WriterFormatFilter(formatName),
507: formatName);
508: }
509:
510:
521: public static Iterator getImageWritersByMIMEType(String MIMEType)
522: {
523: if (MIMEType == null)
524: throw new IllegalArgumentException("MIMEType may not be null");
525:
526: return getWritersByFilter(ImageWriterSpi.class,
527: new WriterMIMETypeFilter(MIMEType),
528: MIMEType);
529: }
530:
531:
541: public static Iterator getImageWritersBySuffix(String fileSuffix)
542: {
543: if (fileSuffix == null)
544: throw new IllegalArgumentException("fileSuffix may not be null");
545:
546: return getWritersByFilter(ImageWriterSpi.class,
547: new WriterSuffixFilter(fileSuffix),
548: fileSuffix);
549: }
550:
551:
557: public static String[] getReaderFormatNames()
558: {
559: try
560: {
561: Iterator it =
562: getRegistry().getServiceProviders(ImageReaderSpi.class, true);
563: ArrayList result = new ArrayList();
564:
565: while (it.hasNext())
566: {
567: ImageReaderSpi spi = (ImageReaderSpi) it.next();
568: String[] names = spi.getFormatNames();
569:
570: for (int i = names.length - 1; i >= 0; --i)
571: result.add(names[i]);
572: }
573:
574: return (String[]) result.toArray(new String[result.size()]);
575: }
576: catch (IllegalArgumentException e)
577: {
578: return new String[0];
579: }
580: }
581:
582:
588: public static String[] getReaderMIMETypes()
589: {
590: try
591: {
592: Iterator it =
593: getRegistry().getServiceProviders(ImageReaderSpi.class, true);
594: ArrayList result = new ArrayList();
595:
596: while (it.hasNext())
597: {
598: ImageReaderSpi spi = (ImageReaderSpi) it.next();
599: String[] names = spi.getMIMETypes();
600:
601: for (int i = names.length - 1; i >= 0; --i)
602: result.add(names[i]);
603: }
604:
605: return (String[]) result.toArray(new String[result.size()]);
606: }
607: catch (IllegalArgumentException e)
608: {
609: return new String[0];
610: }
611: }
612:
613: private static IIORegistry getRegistry()
614: {
615: return IIORegistry.getDefaultInstance();
616: }
617:
618:
624: public static boolean getUseCache()
625: {
626: return useCache;
627: }
628:
629:
635: public static String[] getWriterFormatNames()
636: {
637: try
638: {
639: Iterator it =
640: getRegistry().getServiceProviders(ImageWriterSpi.class, true);
641: ArrayList result = new ArrayList();
642:
643: while (it.hasNext())
644: {
645: ImageWriterSpi spi = (ImageWriterSpi) it.next();
646: String[] names = spi.getFormatNames();
647:
648: for (int i = names.length - 1; i >= 0; --i)
649: result.add(names[i]);
650: }
651:
652: return (String[]) result.toArray(new String[result.size()]);
653: }
654: catch (IllegalArgumentException e)
655: {
656: return new String[0];
657: }
658: }
659:
660:
666: public static String[] getWriterMIMETypes()
667: {
668: try
669: {
670: Iterator it =
671: getRegistry().getServiceProviders(ImageWriterSpi.class, true);
672: ArrayList result = new ArrayList();
673:
674: while (it.hasNext())
675: {
676: ImageWriterSpi spi = (ImageWriterSpi) it.next();
677: String[] names = spi.getMIMETypes();
678:
679: for (int i = names.length - 1; i >= 0; --i)
680: result.add(names[i]);
681: }
682:
683: return (String[]) result.toArray(new String[result.size()]);
684: }
685: catch (IllegalArgumentException e)
686: {
687: return new String[0];
688: }
689: }
690:
691:
695: public static void scanForPlugins()
696: {
697: IIORegistry.getDefaultInstance().registerApplicationClasspathSpis();
698: }
699:
700:
711: public static void setCacheDirectory(File cacheDirectory)
712: {
713:
714: if (cacheDirectory != null)
715: {
716: if (!cacheDirectory.isDirectory())
717: throw new IllegalArgumentException("cacheDirectory must be a directory");
718:
719: cacheDirectory.canWrite();
720: }
721:
722: ImageIO.cacheDirectory = cacheDirectory;
723: }
724:
725:
736: public static void setUseCache(boolean useCache)
737: {
738: ImageIO.useCache = useCache;
739: }
740:
741:
755: public static boolean write(RenderedImage im,
756: String formatName,
757: File output)
758: 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));
764: }
765:
766:
781: public static boolean write(RenderedImage im,
782: String formatName,
783: OutputStream output)
784: 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));
790: }
791:
792:
809: public static boolean write(RenderedImage im,
810: String formatName,
811: ImageOutputStream output)
812: throws IOException
813: {
814: if (im == null || formatName == null || output == null)
815: throw new IllegalArgumentException ("null argument");
816:
817: Iterator writers = getImageWritersByFormatName(formatName);
818: IIOImage img = new IIOImage(im, null, null);
819: while (writers.hasNext())
820: {
821: ImageWriter w = (ImageWriter) writers.next();
822: try
823: {
824: w.setOutput(output);
825: }
826: catch (IllegalArgumentException e)
827: {
828: continue;
829: }
830:
831: w.write(null, img, null);
832: output.close();
833: return true;
834: }
835: return false;
836: }
837:
838:
853: public static BufferedImage read(ImageInputStream stream)
854: throws IOException
855: {
856: if (stream == null)
857: throw new IllegalArgumentException("null argument");
858:
859: Iterator providers = getRegistry().getServiceProviders(ImageReaderSpi.class, true);
860: while (providers.hasNext())
861: {
862: ImageReaderSpi spi = (ImageReaderSpi) providers.next();
863: if (spi.canDecodeInput(stream))
864: {
865: ImageReader reader = spi.createReaderInstance();
866: reader.setInput(stream);
867: return reader.read(0, null);
868: }
869: }
870: return null;
871: }
872:
873:
894: public static BufferedImage read(URL input)
895: throws IOException
896: {
897: if (input == null)
898: throw new IllegalArgumentException("null argument");
899:
900: return read(input.openStream());
901: }
902:
903:
924: public static BufferedImage read(InputStream input)
925: throws IOException
926: {
927: if (input == null)
928: throw new IllegalArgumentException("null argument");
929:
930: return read(new MemoryCacheImageInputStream(input));
931: }
932:
933:
954: public static BufferedImage read(File input)
955: throws IOException
956: {
957: if (input == null)
958: throw new IllegalArgumentException("null argument");
959:
960: return read(new FileInputStream(input));
961: }
962:
963:
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:
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:
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:
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:
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:
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:
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: }