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
319: implements Iterator<ImageReader>
320: {
321: Iterator<ImageReaderSpi> it;
322: Object readerExtension;
323:
324: public ImageReaderIterator(Iterator<ImageReaderSpi> it,
325: Object readerExtension)
326: {
327: this.it = it;
328: this.readerExtension = readerExtension;
329: }
330:
331: public ImageReaderIterator(Iterator<ImageReaderSpi> it)
332: {
333: this.it = it;
334: }
335:
336: public boolean hasNext()
337: {
338: return it.hasNext();
339: }
340:
341: public ImageReader next()
342: {
343: try
344: {
345: ImageReaderSpi spi = it.next();
346: return (readerExtension == null
347: ? spi.createReaderInstance()
348: : spi.createReaderInstance(readerExtension));
349: }
350: catch (IOException e)
351: {
352: return null;
353: }
354: }
355:
356: public void remove()
357: {
358: throw new UnsupportedOperationException();
359: }
360: }
361:
362: private static final class ImageWriterIterator
363: implements Iterator<ImageWriter>
364: {
365: Iterator<ImageWriterSpi> it;
366: Object writerExtension;
367:
368: public ImageWriterIterator(Iterator<ImageWriterSpi> it,
369: Object writerExtension)
370: {
371: this.it = it;
372: this.writerExtension = writerExtension;
373: }
374:
375: public ImageWriterIterator(Iterator<ImageWriterSpi> it)
376: {
377: this.it = it;
378: }
379:
380: public boolean hasNext()
381: {
382: return it.hasNext();
383: }
384:
385: public ImageWriter next()
386: {
387: try
388: {
389: ImageWriterSpi spi = it.next();
390: return (writerExtension == null
391: ? spi.createWriterInstance()
392: : spi.createWriterInstance(writerExtension));
393: }
394: catch (IOException e)
395: {
396: return null;
397: }
398: }
399:
400: public void remove()
401: {
402: throw new UnsupportedOperationException();
403: }
404: }
405:
406: private static File cacheDirectory;
407: private static boolean useCache = true;
408:
409: private static Iterator<ImageReader> getReadersByFilter(Class<ImageReaderSpi> type,
410: ServiceRegistry.Filter filter,
411: Object readerExtension)
412: {
413: try
414: {
415: Iterator<ImageReaderSpi> it
416: = getRegistry().getServiceProviders(type, filter, true);
417: return new ImageReaderIterator(it, readerExtension);
418: }
419: catch (IllegalArgumentException e)
420: {
421: return Collections.EMPTY_SET.iterator();
422: }
423: }
424:
425: private static Iterator<ImageWriter> getWritersByFilter(Class<ImageWriterSpi> type,
426: ServiceRegistry.Filter filter,
427: Object writerExtension)
428: {
429: try
430: {
431: Iterator<ImageWriterSpi> it
432: = getRegistry().getServiceProviders(type, filter, true);
433: return new ImageWriterIterator(it, writerExtension);
434: }
435: catch (IllegalArgumentException e)
436: {
437: return Collections.EMPTY_SET.iterator();
438: }
439: }
440:
441:
446: public static File getCacheDirectory()
447: {
448: return cacheDirectory;
449: }
450:
451:
461: public static Iterator<ImageReader> getImageReadersByFormatName(String formatName)
462: {
463: if (formatName == null)
464: throw new IllegalArgumentException("formatName may not be null");
465:
466: return getReadersByFilter(ImageReaderSpi.class,
467: new ReaderFormatFilter(formatName),
468: formatName);
469: }
470:
471:
482: public static Iterator<ImageReader> getImageReadersByMIMEType(String MIMEType)
483: {
484: if (MIMEType == null)
485: throw new IllegalArgumentException("MIMEType may not be null");
486:
487: return getReadersByFilter(ImageReaderSpi.class,
488: new ReaderMIMETypeFilter(MIMEType),
489: MIMEType);
490: }
491:
492:
502: public static Iterator<ImageReader> getImageReadersBySuffix(String fileSuffix)
503: {
504: if (fileSuffix == null)
505: throw new IllegalArgumentException("formatName may not be null");
506:
507: return getReadersByFilter(ImageReaderSpi.class,
508: new ReaderSuffixFilter(fileSuffix),
509: fileSuffix);
510: }
511:
512:
522: public static Iterator<ImageWriter> getImageWritersByFormatName(String formatName)
523: {
524: if (formatName == null)
525: throw new IllegalArgumentException("formatName may not be null");
526:
527: return getWritersByFilter(ImageWriterSpi.class,
528: new WriterFormatFilter(formatName),
529: formatName);
530: }
531:
532:
543: public static Iterator<ImageWriter> getImageWritersByMIMEType(String MIMEType)
544: {
545: if (MIMEType == null)
546: throw new IllegalArgumentException("MIMEType may not be null");
547:
548: return getWritersByFilter(ImageWriterSpi.class,
549: new WriterMIMETypeFilter(MIMEType),
550: MIMEType);
551: }
552:
553:
563: public static Iterator<ImageWriter> getImageWritersBySuffix(String fileSuffix)
564: {
565: if (fileSuffix == null)
566: throw new IllegalArgumentException("fileSuffix may not be null");
567:
568: return getWritersByFilter(ImageWriterSpi.class,
569: new WriterSuffixFilter(fileSuffix),
570: fileSuffix);
571: }
572:
573:
579: public static String[] getReaderFormatNames()
580: {
581: try
582: {
583: Iterator it =
584: getRegistry().getServiceProviders(ImageReaderSpi.class, true);
585: ArrayList result = new ArrayList();
586:
587: while (it.hasNext())
588: {
589: ImageReaderSpi spi = (ImageReaderSpi) it.next();
590: String[] names = spi.getFormatNames();
591:
592: for (int i = names.length - 1; i >= 0; --i)
593: result.add(names[i]);
594: }
595:
596: return (String[]) result.toArray(new String[result.size()]);
597: }
598: catch (IllegalArgumentException e)
599: {
600: return new String[0];
601: }
602: }
603:
604:
610: public static String[] getReaderMIMETypes()
611: {
612: try
613: {
614: Iterator it =
615: getRegistry().getServiceProviders(ImageReaderSpi.class, true);
616: ArrayList result = new ArrayList();
617:
618: while (it.hasNext())
619: {
620: ImageReaderSpi spi = (ImageReaderSpi) it.next();
621: String[] names = spi.getMIMETypes();
622:
623: for (int i = names.length - 1; i >= 0; --i)
624: result.add(names[i]);
625: }
626:
627: return (String[]) result.toArray(new String[result.size()]);
628: }
629: catch (IllegalArgumentException e)
630: {
631: return new String[0];
632: }
633: }
634:
635: private static IIORegistry getRegistry()
636: {
637: return IIORegistry.getDefaultInstance();
638: }
639:
640:
646: public static boolean getUseCache()
647: {
648: return useCache;
649: }
650:
651:
657: public static String[] getWriterFormatNames()
658: {
659: try
660: {
661: Iterator it =
662: getRegistry().getServiceProviders(ImageWriterSpi.class, true);
663: ArrayList result = new ArrayList();
664:
665: while (it.hasNext())
666: {
667: ImageWriterSpi spi = (ImageWriterSpi) it.next();
668: String[] names = spi.getFormatNames();
669:
670: for (int i = names.length - 1; i >= 0; --i)
671: result.add(names[i]);
672: }
673:
674: return (String[]) result.toArray(new String[result.size()]);
675: }
676: catch (IllegalArgumentException e)
677: {
678: return new String[0];
679: }
680: }
681:
682:
688: public static String[] getWriterMIMETypes()
689: {
690: try
691: {
692: Iterator it =
693: getRegistry().getServiceProviders(ImageWriterSpi.class, true);
694: ArrayList result = new ArrayList();
695:
696: while (it.hasNext())
697: {
698: ImageWriterSpi spi = (ImageWriterSpi) it.next();
699: String[] names = spi.getMIMETypes();
700:
701: for (int i = names.length - 1; i >= 0; --i)
702: result.add(names[i]);
703: }
704:
705: return (String[]) result.toArray(new String[result.size()]);
706: }
707: catch (IllegalArgumentException e)
708: {
709: return new String[0];
710: }
711: }
712:
713:
717: public static void scanForPlugins()
718: {
719: IIORegistry.getDefaultInstance().registerApplicationClasspathSpis();
720: }
721:
722:
733: public static void setCacheDirectory(File cacheDirectory)
734: {
735:
736: if (cacheDirectory != null)
737: {
738: if (!cacheDirectory.isDirectory())
739: throw new IllegalArgumentException("cacheDirectory must be a directory");
740:
741: cacheDirectory.canWrite();
742: }
743:
744: ImageIO.cacheDirectory = cacheDirectory;
745: }
746:
747:
758: public static void setUseCache(boolean useCache)
759: {
760: ImageIO.useCache = useCache;
761: }
762:
763:
777: public static boolean write(RenderedImage im,
778: String formatName,
779: File output)
780: throws IOException
781: {
782: if (im == null || formatName == null || output == null)
783: throw new IllegalArgumentException ("null argument");
784:
785: return write(im, formatName, new FileOutputStream(output));
786: }
787:
788:
803: public static boolean write(RenderedImage im,
804: String formatName,
805: OutputStream output)
806: throws IOException
807: {
808: if (im == null || formatName == null || output == null)
809: throw new IllegalArgumentException ("null argument");
810:
811: return write(im, formatName, new MemoryCacheImageOutputStream(output));
812: }
813:
814:
831: public static boolean write(RenderedImage im,
832: String formatName,
833: ImageOutputStream output)
834: throws IOException
835: {
836: if (im == null || formatName == null || output == null)
837: throw new IllegalArgumentException ("null argument");
838:
839: Iterator writers = getImageWritersByFormatName(formatName);
840: IIOImage img = new IIOImage(im, null, null);
841: while (writers.hasNext())
842: {
843: ImageWriter w = (ImageWriter) writers.next();
844: try
845: {
846: w.setOutput(output);
847: }
848: catch (IllegalArgumentException e)
849: {
850: continue;
851: }
852:
853: w.write(null, img, null);
854: w.dispose();
855: output.close();
856: return true;
857: }
858: return false;
859: }
860:
861:
876: public static BufferedImage read(ImageInputStream stream)
877: throws IOException
878: {
879: if (stream == null)
880: throw new IllegalArgumentException("null argument");
881:
882: Iterator providers = getRegistry().getServiceProviders(ImageReaderSpi.class, true);
883: while (providers.hasNext())
884: {
885: ImageReaderSpi spi = (ImageReaderSpi) providers.next();
886: if (spi.canDecodeInput(stream))
887: {
888: ImageReader reader = spi.createReaderInstance();
889: reader.setInput(stream);
890: return reader.read(0, null);
891: }
892: }
893: return null;
894: }
895:
896:
917: public static BufferedImage read(URL input)
918: throws IOException
919: {
920: if (input == null)
921: throw new IllegalArgumentException("null argument");
922:
923: return read(input.openStream());
924: }
925:
926:
947: public static BufferedImage read(InputStream input)
948: throws IOException
949: {
950: if (input == null)
951: throw new IllegalArgumentException("null argument");
952:
953: return read(new MemoryCacheImageInputStream(input));
954: }
955:
956:
977: public static BufferedImage read(File input)
978: throws IOException
979: {
980: if (input == null)
981: throw new IllegalArgumentException("null argument");
982:
983: return read(new FileInputStream(input));
984: }
985:
986:
1004: public static ImageInputStream createImageInputStream (Object input)
1005: throws IOException
1006: {
1007: if (input == null)
1008: throw new IllegalArgumentException ("null argument");
1009:
1010: Iterator spis = getRegistry().getServiceProviders
1011: (ImageInputStreamSpi.class, true);
1012:
1013: ImageInputStreamSpi foundSpi = null;
1014:
1015: while(spis.hasNext())
1016: {
1017: ImageInputStreamSpi spi = (ImageInputStreamSpi) spis.next();
1018:
1019: if (input.getClass().equals(spi.getInputClass()))
1020: {
1021: foundSpi = spi;
1022: break;
1023: }
1024: }
1025:
1026: return foundSpi == null ? null :
1027: foundSpi.createInputStreamInstance (input,
1028: getUseCache(),
1029: getCacheDirectory());
1030: }
1031:
1032:
1050: public static ImageOutputStream createImageOutputStream (Object output)
1051: throws IOException
1052: {
1053: if (output == null)
1054: throw new IllegalArgumentException ("null argument");
1055:
1056: Iterator spis = getRegistry().getServiceProviders
1057: (ImageOutputStreamSpi.class, true);
1058:
1059: ImageOutputStreamSpi foundSpi = null;
1060:
1061: while(spis.hasNext())
1062: {
1063: ImageOutputStreamSpi spi = (ImageOutputStreamSpi) spis.next();
1064:
1065: if (output.getClass().equals(spi.getOutputClass()))
1066: {
1067: foundSpi = spi;
1068: break;
1069: }
1070: }
1071:
1072: return foundSpi == null ? null :
1073: foundSpi.createOutputStreamInstance (output,
1074: getUseCache(),
1075: getCacheDirectory());
1076: }
1077:
1078:
1089: public static ImageReader getImageReader (ImageWriter writer)
1090: {
1091: if (writer == null)
1092: throw new IllegalArgumentException ("null argument");
1093:
1094: ImageWriterSpi spi = writer.getOriginatingProvider();
1095:
1096: String[] readerSpiNames = spi.getImageReaderSpiNames();
1097:
1098: ImageReader r = null;
1099:
1100: if (readerSpiNames != null)
1101: {
1102: try
1103: {
1104: Class readerClass = Class.forName (readerSpiNames[0]);
1105: r = (ImageReader) readerClass.newInstance ();
1106: }
1107: catch (Exception e)
1108: {
1109: return null;
1110: }
1111: }
1112: return r;
1113: }
1114:
1115:
1123: public static Iterator<ImageReader> getImageReaders (Object input)
1124: {
1125: if (input == null)
1126: throw new IllegalArgumentException ("null argument");
1127:
1128: Iterator<ImageReaderSpi> spiIterator
1129: = getRegistry().getServiceProviders (ImageReaderSpi.class,
1130: new ReaderObjectFilter(input),
1131: true);
1132: return new ImageReaderIterator(spiIterator);
1133: }
1134:
1135:
1145: public static Iterator<ImageWriter> getImageWriters (ImageTypeSpecifier type,
1146: String formatName)
1147: {
1148: if (type == null || formatName == null)
1149: throw new IllegalArgumentException ("null argument");
1150:
1151: final Iterator<ImageWriterSpi> spiIterator
1152: = getRegistry().getServiceProviders (ImageWriterSpi.class,
1153: new WriterObjectFilter(type,
1154: formatName),
1155: true);
1156: return new ImageWriterIterator(spiIterator);
1157: }
1158:
1159:
1173: public static ImageWriter getImageWriter (ImageReader reader)
1174: {
1175: if (reader == null)
1176: throw new IllegalArgumentException ("null argument");
1177:
1178: ImageReaderSpi spi = reader.getOriginatingProvider();
1179:
1180: String[] writerSpiNames = spi.getImageWriterSpiNames();
1181:
1182: ImageWriter w = null;
1183:
1184: if (writerSpiNames != null)
1185: {
1186: try
1187: {
1188: Class writerClass = Class.forName (writerSpiNames[0]);
1189: w = (ImageWriter) writerClass.newInstance ();
1190: }
1191: catch (Exception e)
1192: {
1193: return null;
1194: }
1195: }
1196: return w;
1197: }
1198:
1199:
1212: public static Iterator<ImageTranscoder> getImageTranscoders (ImageReader reader,
1213: ImageWriter writer)
1214: {
1215: if (reader == null || writer == null)
1216: throw new IllegalArgumentException ("null argument");
1217:
1218: final Iterator<ImageTranscoderSpi> spiIterator
1219: = getRegistry().getServiceProviders (ImageTranscoderSpi.class,
1220: new TranscoderFilter (reader,
1221: writer),
1222: true);
1223: return new Iterator<ImageTranscoder>()
1224: {
1225: public boolean hasNext()
1226: {
1227: return spiIterator.hasNext();
1228: }
1229:
1230: public ImageTranscoder next()
1231: {
1232: return spiIterator.next().createTranscoderInstance();
1233: }
1234:
1235: public void remove()
1236: {
1237: throw new UnsupportedOperationException();
1238: }
1239: };
1240: }
1241: }