Search in sources :

Example 26 with ImageWriterSpi

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);
    }
}
Also used : ImageReaderWriterSpi(javax.imageio.spi.ImageReaderWriterSpi) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi)

Example 27 with ImageWriterSpi

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());
            }
        }
    }
}
Also used : Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi)

Example 28 with ImageWriterSpi

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;
}
Also used : ImageReaderWriterSpi(javax.imageio.spi.ImageReaderWriterSpi) IIORegistry(javax.imageio.spi.IIORegistry) ImageReaderSpi(javax.imageio.spi.ImageReaderSpi) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi)

Aggregations

ImageWriterSpi (javax.imageio.spi.ImageWriterSpi)28 ImageWriter (javax.imageio.ImageWriter)13 File (java.io.File)9 IIOImage (javax.imageio.IIOImage)8 ImageReaderSpi (javax.imageio.spi.ImageReaderSpi)7 Test (org.junit.Test)7 Iterator (java.util.Iterator)6 ImageWriteParam (javax.imageio.ImageWriteParam)6 IIORegistry (javax.imageio.spi.IIORegistry)6 ImageOutputStream (javax.imageio.stream.ImageOutputStream)5 IOException (java.io.IOException)4 BufferedImage (java.awt.image.BufferedImage)3 FileOutputStream (java.io.FileOutputStream)3 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)3 Ignore (org.junit.Ignore)3 ImageOutputStreamAdapter (it.geosolutions.imageio.stream.output.ImageOutputStreamAdapter)2 OutputStream (java.io.OutputStream)2 ImageReader (javax.imageio.ImageReader)2 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)2 ImageReaderWriterSpi (javax.imageio.spi.ImageReaderWriterSpi)2