use of javax.imageio.spi.ImageReaderSpi in project jdk8u_jdk by JetBrains.
the class ImageIO method getImageWriter.
/**
* Returns an <code>ImageWriter</code>corresponding to the given
* <code>ImageReader</code>, if there is one, or <code>null</code>
* if the plug-in for this <code>ImageReader</code> does not
* specify a corresponding <code>ImageWriter</code>, or if the
* given <code>ImageReader</code> is not registered. This
* mechanism may be used to obtain an <code>ImageWriter</code>
* that will understand the internal structure of non-pixel
* metadata (as encoded by <code>IIOMetadata</code> objects)
* generated by the <code>ImageReader</code>. By obtaining this
* data from the <code>ImageReader</code> and passing it on to the
* <code>ImageWriter</code> obtained with this method, a client
* program can read an image, modify it in some way, and write it
* back out preserving all metadata, without having to understand
* anything about the structure of the metadata, or even about
* the image format. Note that this method returns the
* "preferred" writer, which is the first in the list returned by
* <code>javax.imageio.spi.ImageReaderSpi.getImageWriterSpiNames()</code>.
*
* @param reader an instance of a registered <code>ImageReader</code>.
*
* @return an <code>ImageWriter</code>, or null.
*
* @exception IllegalArgumentException if <code>reader</code> is
* <code>null</code>.
*
* @see #getImageReader(ImageWriter)
* @see javax.imageio.spi.ImageReaderSpi#getImageWriterSpiNames()
*/
public static ImageWriter getImageWriter(ImageReader reader) {
if (reader == null) {
throw new IllegalArgumentException("reader == null!");
}
ImageReaderSpi readerSpi = reader.getOriginatingProvider();
if (readerSpi == null) {
Iterator readerSpiIter;
// Ensure category is present
try {
readerSpiIter = theRegistry.getServiceProviders(ImageReaderSpi.class, false);
} catch (IllegalArgumentException e) {
return null;
}
while (readerSpiIter.hasNext()) {
ImageReaderSpi temp = (ImageReaderSpi) readerSpiIter.next();
if (temp.isOwnReader(reader)) {
readerSpi = temp;
break;
}
}
if (readerSpi == null) {
return null;
}
}
String[] writerNames = readerSpi.getImageWriterSpiNames();
if (writerNames == null) {
return null;
}
Class writerSpiClass = null;
try {
writerSpiClass = Class.forName(writerNames[0], true, ClassLoader.getSystemClassLoader());
} catch (ClassNotFoundException e) {
return null;
}
ImageWriterSpi writerSpi = (ImageWriterSpi) theRegistry.getServiceProviderByClass(writerSpiClass);
if (writerSpi == null) {
return null;
}
try {
return writerSpi.createWriterInstance();
} catch (IOException e) {
// Deregister the spi in this case, but only as a writerSpi
theRegistry.deregisterServiceProvider(writerSpi, ImageWriterSpi.class);
return null;
}
}
use of javax.imageio.spi.ImageReaderSpi in project jdk8u_jdk by JetBrains.
the class ImageIO method getImageTranscoders.
/**
* Returns an <code>Iterator</code> containing all currently
* registered <code>ImageTranscoder</code>s that claim to be
* able to transcode between the metadata of the given
* <code>ImageReader</code> and <code>ImageWriter</code>.
*
* @param reader an <code>ImageReader</code>.
* @param writer an <code>ImageWriter</code>.
*
* @return an <code>Iterator</code> containing
* <code>ImageTranscoder</code>s.
*
* @exception IllegalArgumentException if <code>reader</code> or
* <code>writer</code> is <code>null</code>.
*/
public static Iterator<ImageTranscoder> getImageTranscoders(ImageReader reader, ImageWriter writer) {
if (reader == null) {
throw new IllegalArgumentException("reader == null!");
}
if (writer == null) {
throw new IllegalArgumentException("writer == null!");
}
ImageReaderSpi readerSpi = reader.getOriginatingProvider();
ImageWriterSpi writerSpi = writer.getOriginatingProvider();
ServiceRegistry.Filter filter = new TranscoderFilter(readerSpi, writerSpi);
Iterator iter;
// Ensure category is present
try {
iter = theRegistry.getServiceProviders(ImageTranscoderSpi.class, filter, true);
} catch (IllegalArgumentException e) {
return Collections.emptyIterator();
}
return new ImageTranscoderIterator(iter);
}
use of javax.imageio.spi.ImageReaderSpi in project jdk8u_jdk by JetBrains.
the class InputImageTests method initIIOReadFormats.
private static void initIIOReadFormats() {
List spis = new ArrayList();
List shortNames = new ArrayList();
ImageIO.scanForPlugins();
IIORegistry registry = IIORegistry.getDefaultInstance();
java.util.Iterator readerspis = registry.getServiceProviders(ImageReaderSpi.class, false);
while (readerspis.hasNext()) {
// REMIND: there could be more than one non-core plugin for
// a particular format, as is the case for JPEG2000 in the JAI
// IIO Tools package, so we should support that somehow
ImageReaderSpi spi = (ImageReaderSpi) readerspis.next();
String klass = spi.getClass().getName();
String format = spi.getFormatNames()[0].toLowerCase();
String suffix = spi.getFileSuffixes()[0].toLowerCase();
if (suffix == null || suffix.equals("")) {
suffix = format;
}
String shortName;
if (klass.startsWith("com.sun.imageio.plugins")) {
shortName = "core-" + suffix;
} else {
shortName = "ext-" + suffix;
}
spis.add(spi);
shortNames.add(shortName);
}
imageioReaderSpis = new ImageReaderSpi[spis.size()];
imageioReaderSpis = (ImageReaderSpi[]) spis.toArray(imageioReaderSpis);
imageioReadFormatShortNames = new String[shortNames.size()];
imageioReadFormatShortNames = (String[]) shortNames.toArray(imageioReadFormatShortNames);
}
use of javax.imageio.spi.ImageReaderSpi in project jdk8u_jdk by JetBrains.
the class GetNumImages method main.
public static void main(String[] args) throws Exception {
IIORegistry registry = IIORegistry.getDefaultInstance();
// test ImageReader.getNumImages() for all available ImageReaders,
// with no source set
Iterator readerspis = registry.getServiceProviders(ImageReaderSpi.class, false);
while (readerspis.hasNext()) {
boolean caughtEx = false;
ImageReaderSpi readerspi = (ImageReaderSpi) readerspis.next();
ImageReader reader = readerspi.createReaderInstance();
try {
reader.getNumImages(false);
} catch (IllegalStateException ise) {
// caught exception, everything's okay
caughtEx = true;
}
if (!caughtEx) {
throw new RuntimeException("Test failed: exception was not " + "thrown for null input: " + reader);
}
}
// test ImageReader.getNumImages() for all available ImageReaders,
// with source set, seekForwardOnly and allowSearch both true
readerspis = registry.getServiceProviders(ImageReaderSpi.class, false);
while (readerspis.hasNext()) {
boolean caughtEx = false;
ImageReaderSpi readerspi = (ImageReaderSpi) readerspis.next();
ImageReader reader = readerspi.createReaderInstance();
byte[] barr = new byte[100];
ByteArrayInputStream bais = new ByteArrayInputStream(barr);
ImageInputStream iis = ImageIO.createImageInputStream(bais);
try {
reader.setInput(iis, true);
reader.getNumImages(true);
} catch (IllegalStateException ise) {
// caught exception, everything's okay
caughtEx = true;
}
if (!caughtEx) {
throw new RuntimeException("Test failed: exception was not " + "thrown when allowSearch and " + "seekForwardOnly are both true: " + reader);
}
}
}
use of javax.imageio.spi.ImageReaderSpi in project jdk8u_jdk by JetBrains.
the class NullInputOutput method main.
public static void main(String[] args) throws Exception {
IIORegistry registry = IIORegistry.getDefaultInstance();
// test ImageReader.read() for all available ImageReaders
Iterator readerspis = registry.getServiceProviders(ImageReaderSpi.class, false);
while (readerspis.hasNext()) {
ImageReaderSpi readerspi = (ImageReaderSpi) readerspis.next();
ImageReader reader = readerspi.createReaderInstance();
try {
reader.read(0);
} catch (IllegalStateException ise) {
// caught exception, everything's okay
}
}
// test ImageWriter.write() for all available ImageWriters
BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Iterator writerspis = registry.getServiceProviders(ImageWriterSpi.class, false);
while (writerspis.hasNext()) {
ImageWriterSpi writerspi = (ImageWriterSpi) writerspis.next();
ImageWriter writer = writerspi.createWriterInstance();
try {
writer.write(bi);
} catch (IllegalStateException ise) {
// caught exception, everything's okay
}
}
}
Aggregations