use of javax.imageio.spi.ImageWriterSpi in project imageio-ext by geosolutions-it.
the class TIFFImageWriterSpi method onRegistration.
public void onRegistration(ServiceRegistry registry, Class category) {
super.onRegistration(registry, category);
if (registered) {
return;
}
registered = true;
final Iterator<ImageReaderWriterSpi> writers = ImageIOUtilities.getJDKImageReaderWriterSPI(registry, "TIFF", true).iterator();
while (writers.hasNext()) {
final ImageWriterSpi spi = (ImageWriterSpi) writers.next();
if (spi == this)
continue;
registry.deregisterServiceProvider(spi);
registry.setOrdering(category, this, spi);
}
}
use of javax.imageio.spi.ImageWriterSpi in project imageio-ext by geosolutions-it.
the class TIFFBaseJPEGCompressor method initJPEGWriter.
/**
* Initializes the JPEGWriter and JPEGParam instance variables.
* This method must be called before encode() is invoked.
*
* @param supportsStreamMetadata Whether the JPEG writer must
* support JPEG native stream metadata, i.e., be capable of writing
* abbreviated streams.
* @param supportsImageMetadata Whether the JPEG writer must
* support JPEG native image metadata.
*/
protected void initJPEGWriter(boolean supportsStreamMetadata, boolean supportsImageMetadata) {
// Reset the writer to null if it does not match preferences.
if (this.JPEGWriter != null && (supportsStreamMetadata || supportsImageMetadata)) {
ImageWriterSpi spi = this.JPEGWriter.getOriginatingProvider();
if (supportsStreamMetadata) {
String smName = spi.getNativeStreamMetadataFormatName();
if (smName == null || !smName.equals(STREAM_METADATA_NAME)) {
this.JPEGWriter = null;
}
}
if (this.JPEGWriter != null && supportsImageMetadata) {
String imName = spi.getNativeImageMetadataFormatName();
if (imName == null || !imName.equals(IMAGE_METADATA_NAME)) {
this.JPEGWriter = null;
}
}
}
// Set the writer.
if (this.JPEGWriter == null) {
Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
while (iter.hasNext()) {
// Get a writer.
ImageWriter writer = (ImageWriter) iter.next();
// Verify its metadata support level.
if (supportsStreamMetadata || supportsImageMetadata) {
ImageWriterSpi spi = writer.getOriginatingProvider();
if (supportsStreamMetadata) {
String smName = spi.getNativeStreamMetadataFormatName();
if (smName == null || !smName.equals(STREAM_METADATA_NAME)) {
// Try the next one.
continue;
}
}
if (supportsImageMetadata) {
String imName = spi.getNativeImageMetadataFormatName();
if (imName == null || !imName.equals(IMAGE_METADATA_NAME)) {
// Try the next one.
continue;
}
}
}
// Set the writer.
this.JPEGWriter = writer;
break;
}
if (this.JPEGWriter == null) {
// XXX The exception thrown should really be an IIOException.
throw new IllegalStateException("No appropriate JPEG writers found!");
}
}
this.usingCodecLib = JPEGWriter.getClass().getName().startsWith("com.sun.media");
if (DEBUG)
System.out.println("usingCodecLib = " + usingCodecLib);
// Initialize the ImageWriteParam.
if (this.JPEGParam == null) {
if (param != null && param instanceof JPEGImageWriteParam) {
JPEGParam = (JPEGImageWriteParam) param;
} else {
JPEGParam = new JPEGImageWriteParam(writer != null ? writer.getLocale() : null);
if (param.getCompressionMode() == ImageWriteParam.MODE_EXPLICIT) {
JPEGParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
JPEGParam.setCompressionQuality(param.getCompressionQuality());
}
}
}
}
use of javax.imageio.spi.ImageWriterSpi in project imageio-ext by geosolutions-it.
the class ImageIOUtilities method replaceProvider.
/**
* Replace the original provider with name originalProviderName with the provider with name
* customProviderName for the class providerClass and for the provided format .
* @param providerClass the {@link Class} for the providers.
* @param customProviderName the name of the provider we want to use as new preferred provider.
* @param originalProviderName the name of the provider we want to deregister.
* @param format the format for this provi
* @return <code>true</code> if we find both of the providers and the replacement succeed, <code>false</code> otherwise.
*/
public static boolean replaceProvider(final Class<? extends ImageReaderWriterSpi> providerClass, final String customProviderName, final String originalProviderName, final String format) {
// now we need to set the order
final IIORegistry registry = IIORegistry.getDefaultInstance();
ImageReaderWriterSpi standard = null, custom = null;
for (final Iterator<? extends ImageReaderWriterSpi> it = registry.getServiceProviders(providerClass, false); it.hasNext(); ) {
final ImageReaderWriterSpi provider = it.next();
final String providerClassName = provider.getClass().getName();
final String[] formats = provider.getFormatNames();
for (int i = 0; i < formats.length; i++) {
if (formats[i].equalsIgnoreCase(format)) {
if (providerClassName.equalsIgnoreCase(originalProviderName)) {
standard = provider;
} else if (providerClassName.equalsIgnoreCase(customProviderName)) {
custom = provider;
}
if (standard != null && custom != null) {
if (ImageReaderSpi.class.isAssignableFrom(standard.getClass()))
return registry.setOrdering(ImageReaderSpi.class, (ImageReaderSpi) custom, (ImageReaderSpi) standard);
else
return registry.setOrdering(ImageWriterSpi.class, (ImageWriterSpi) custom, (ImageWriterSpi) standard);
}
}
}
}
// we did not find them
return false;
}
Aggregations