Search in sources :

Example 6 with ImageWriterSpi

use of javax.imageio.spi.ImageWriterSpi in project jdk8u_jdk by JetBrains.

the class BMPImageWriter method canEncodeImage.

protected boolean canEncodeImage(int compression, ImageTypeSpecifier imgType) {
    ImageWriterSpi spi = this.getOriginatingProvider();
    if (!spi.canEncodeImage(imgType)) {
        return false;
    }
    int biType = imgType.getBufferedImageType();
    int bpp = imgType.getColorModel().getPixelSize();
    if (compressionType == BI_RLE4 && bpp != 4) {
        // only 4bpp images can be encoded as BI_RLE4
        return false;
    }
    if (compressionType == BI_RLE8 && bpp != 8) {
        // only 8bpp images can be encoded as BI_RLE8
        return false;
    }
    if (bpp == 16) {
        /*
             * Technically we expect that we may be able to
             * encode only some of SinglePixelPackedSampleModel
             * images here.
             *
             * In addition we should take into account following:
             *
             * 1. BI_RGB case, according to the MSDN description:
             *
             *     The bitmap has a maximum of 2^16 colors. If the
             *     biCompression member of the BITMAPINFOHEADER is BI_RGB,
             *     the bmiColors member of BITMAPINFO is NULL. Each WORD
             *     in the bitmap array represents a single pixel. The
             *     relative intensities of red, green, and blue are
             *     represented with five bits for each color component.
             *
             * 2. BI_BITFIELDS case, according ot the MSDN description:
             *
             *     Windows 95/98/Me: When the biCompression member is
             *     BI_BITFIELDS, the system supports only the following
             *     16bpp color masks: A 5-5-5 16-bit image, where the blue
             *     mask is 0x001F, the green mask is 0x03E0, and the red mask
             *     is 0x7C00; and a 5-6-5 16-bit image, where the blue mask
             *     is 0x001F, the green mask is 0x07E0, and the red mask is
             *     0xF800.
             */
        boolean canUseRGB = false;
        boolean canUseBITFIELDS = false;
        SampleModel sm = imgType.getSampleModel();
        if (sm instanceof SinglePixelPackedSampleModel) {
            int[] sizes = ((SinglePixelPackedSampleModel) sm).getSampleSize();
            canUseRGB = true;
            canUseBITFIELDS = true;
            for (int i = 0; i < sizes.length; i++) {
                canUseRGB &= (sizes[i] == 5);
                canUseBITFIELDS &= ((sizes[i] == 5) || (i == 1 && sizes[i] == 6));
            }
        }
        return (((compressionType == BI_RGB) && canUseRGB) || ((compressionType == BI_BITFIELDS) && canUseBITFIELDS));
    }
    return true;
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi)

Example 7 with ImageWriterSpi

use of javax.imageio.spi.ImageWriterSpi in project jdk8u_jdk by JetBrains.

the class ImageIO method getImageReader.

/**
     * Returns an <code>ImageReader</code>corresponding to the given
     * <code>ImageWriter</code>, if there is one, or <code>null</code>
     * if the plug-in for this <code>ImageWriter</code> does not
     * specify a corresponding <code>ImageReader</code>, or if the
     * given <code>ImageWriter</code> is not registered.  This method
     * is provided principally for symmetry with
     * <code>getImageWriter(ImageReader)</code>.  Note that this
     * method returns the "preferred" reader, which is the first in
     * the list returned by
     * javax.imageio.spi.ImageWriterSpi.<code>getImageReaderSpiNames()</code>.
     *
     * @param writer an instance of a registered <code>ImageWriter</code>.
     *
     * @return an <code>ImageReader</code>, or null.
     *
     * @exception IllegalArgumentException if <code>writer</code> is
     * <code>null</code>.
     *
     * @see #getImageWriter(ImageReader)
     * @see javax.imageio.spi.ImageWriterSpi#getImageReaderSpiNames()
     */
public static ImageReader getImageReader(ImageWriter writer) {
    if (writer == null) {
        throw new IllegalArgumentException("writer == null!");
    }
    ImageWriterSpi writerSpi = writer.getOriginatingProvider();
    if (writerSpi == null) {
        Iterator writerSpiIter;
        // Ensure category is present
        try {
            writerSpiIter = theRegistry.getServiceProviders(ImageWriterSpi.class, false);
        } catch (IllegalArgumentException e) {
            return null;
        }
        while (writerSpiIter.hasNext()) {
            ImageWriterSpi temp = (ImageWriterSpi) writerSpiIter.next();
            if (temp.isOwnWriter(writer)) {
                writerSpi = temp;
                break;
            }
        }
        if (writerSpi == null) {
            return null;
        }
    }
    String[] readerNames = writerSpi.getImageReaderSpiNames();
    if (readerNames == null) {
        return null;
    }
    Class readerSpiClass = null;
    try {
        readerSpiClass = Class.forName(readerNames[0], true, ClassLoader.getSystemClassLoader());
    } catch (ClassNotFoundException e) {
        return null;
    }
    ImageReaderSpi readerSpi = (ImageReaderSpi) theRegistry.getServiceProviderByClass(readerSpiClass);
    if (readerSpi == null) {
        return null;
    }
    try {
        return readerSpi.createReaderInstance();
    } catch (IOException e) {
        // Deregister the spi in this case, but only as a readerSpi
        theRegistry.deregisterServiceProvider(readerSpi, ImageReaderSpi.class);
        return null;
    }
}
Also used : ImageReaderSpi(javax.imageio.spi.ImageReaderSpi) Iterator(java.util.Iterator) IOException(java.io.IOException) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi)

Example 8 with ImageWriterSpi

use of javax.imageio.spi.ImageWriterSpi 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;
    }
}
Also used : ImageReaderSpi(javax.imageio.spi.ImageReaderSpi) Iterator(java.util.Iterator) IOException(java.io.IOException) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi)

Example 9 with ImageWriterSpi

use of javax.imageio.spi.ImageWriterSpi 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);
}
Also used : ImageReaderSpi(javax.imageio.spi.ImageReaderSpi) Iterator(java.util.Iterator) ServiceRegistry(javax.imageio.spi.ServiceRegistry) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi) ImageTranscoderSpi(javax.imageio.spi.ImageTranscoderSpi)

Example 10 with ImageWriterSpi

use of javax.imageio.spi.ImageWriterSpi in project jdk8u_jdk by JetBrains.

the class XDataTransferer method getPlatformMappingsForFlavor.

/*
     * The XDnD protocol prescribes that the Atoms used as targets for data
     * transfer should have string names that represent the corresponding MIME
     * types.
     * To meet this requirement we return a list of formats that represent
     * MIME types to which the data in this flavor can be translated by the Data
     * Transfer subsystem.
     */
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
    LinkedHashSet<String> natives = new LinkedHashSet<>(1);
    if (df == null) {
        return natives;
    }
    String charset = df.getParameter("charset");
    String baseType = df.getPrimaryType() + "/" + df.getSubType();
    String mimeType = baseType;
    if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
        mimeType += ";charset=" + charset;
    }
    // doesn't require translation.
    if (df.getRepresentationClass() != null && (df.isRepresentationClassInputStream() || df.isRepresentationClassByteBuffer() || byte[].class.equals(df.getRepresentationClass()))) {
        natives.add(mimeType);
    }
    if (DataFlavor.imageFlavor.equals(df)) {
        String[] mimeTypes = ImageIO.getWriterMIMETypes();
        if (mimeTypes != null) {
            for (int i = 0; i < mimeTypes.length; i++) {
                Iterator writers = ImageIO.getImageWritersByMIMEType(mimeTypes[i]);
                while (writers.hasNext()) {
                    ImageWriter imageWriter = (ImageWriter) writers.next();
                    ImageWriterSpi writerSpi = imageWriter.getOriginatingProvider();
                    if (writerSpi != null && writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
                        natives.add(mimeTypes[i]);
                        break;
                    }
                }
            }
        }
    } else if (DataTransferer.isFlavorCharsetTextType(df)) {
        // "text/plain" MIME type.
        if (DataFlavor.stringFlavor.equals(df)) {
            baseType = "text/plain";
        }
        for (String encoding : DataTransferer.standardEncodings()) {
            if (!encoding.equals(charset)) {
                natives.add(baseType + ";charset=" + encoding);
            }
        }
        // Add a MIME format without specified charset.
        if (!natives.contains(baseType)) {
            natives.add(baseType);
        }
    }
    return natives;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi)

Aggregations

ImageWriterSpi (javax.imageio.spi.ImageWriterSpi)15 Iterator (java.util.Iterator)5 ImageWriter (javax.imageio.ImageWriter)5 ImageReaderSpi (javax.imageio.spi.ImageReaderSpi)5 IIORegistry (javax.imageio.spi.IIORegistry)4 IOException (java.io.IOException)3 BufferedImage (java.awt.image.BufferedImage)2 File (java.io.File)2 PNGImageReaderSpi (com.sun.imageio.plugins.png.PNGImageReaderSpi)1 PNGImageWriterSpi (com.sun.imageio.plugins.png.PNGImageWriterSpi)1 BandedSampleModel (java.awt.image.BandedSampleModel)1 ComponentSampleModel (java.awt.image.ComponentSampleModel)1 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)1 SampleModel (java.awt.image.SampleModel)1 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1