Java Code Examples for javax.imageio.spi.ImageWriterSpi#canEncodeImage()

The following examples show how to use javax.imageio.spi.ImageWriterSpi#canEncodeImage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageUtil.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/** Checks that the provided <code>ImageWriter</code> can encode
 * the provided <code>ImageTypeSpecifier</code> or not.  If not, an
 * <code>IIOException</code> will be thrown.
 * @param writer The provided <code>ImageWriter</code>.
 * @param type The image to be tested.
 * @throws IIOException If the writer cannot encoded the provided image.
 */
public static final void canEncodeImage(ImageWriter writer,
                                        ImageTypeSpecifier type)
    throws IIOException {
    ImageWriterSpi spi = writer.getOriginatingProvider();

    if(type != null && spi != null && !spi.canEncodeImage(type))  {
        throw new IIOException(I18N.getString("ImageUtil2")+" "+
                               writer.getClass().getName());
    }
}
 
Example 2
Source File: ImageUtil.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/** Checks that the provided <code>ImageWriter</code> can encode
 * the provided <code>ImageTypeSpecifier</code> or not.  If not, an
 * <code>IIOException</code> will be thrown.
 * @param writer The provided <code>ImageWriter</code>.
 * @param type The image to be tested.
 * @throws IIOException If the writer cannot encoded the provided image.
 */
public static final void canEncodeImage(ImageWriter writer,
                                        ImageTypeSpecifier type)
    throws IIOException {
    ImageWriterSpi spi = writer.getOriginatingProvider();

    if(type != null && spi != null && !spi.canEncodeImage(type))  {
        throw new IIOException(I18N.getString("ImageUtil2")+" "+
                               writer.getClass().getName());
    }
}
 
Example 3
Source File: ImageUtil.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/** Checks that the provided {@code ImageWriter} can encode
 * the provided {@code ImageTypeSpecifier} or not.  If not, an
 * {@code IIOException} will be thrown.
 * @param writer The provided {@code ImageWriter}.
 * @param type The image to be tested.
 * @throws IIOException If the writer cannot encoded the provided image.
 */
public static final void canEncodeImage(ImageWriter writer,
                                        ImageTypeSpecifier type)
    throws IIOException {
    ImageWriterSpi spi = writer.getOriginatingProvider();

    if(type != null && spi != null && !spi.canEncodeImage(type))  {
        throw new IIOException(I18N.getString("ImageUtil2")+" "+
                               writer.getClass().getName());
    }
}
 
Example 4
Source File: ImageUtil.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/** Checks that the provided <code>ImageWriter</code> can encode
 * the provided <code>ImageTypeSpecifier</code> or not.  If not, an
 * <code>IIOException</code> will be thrown.
 * @param writer The provided <code>ImageWriter</code>.
 * @param type The image to be tested.
 * @throws IIOException If the writer cannot encoded the provided image.
 */
public static final void canEncodeImage(ImageWriter writer,
                                        ImageTypeSpecifier type)
    throws IIOException {
    ImageWriterSpi spi = writer.getOriginatingProvider();

    if(type != null && spi != null && !spi.canEncodeImage(type))  {
        throw new IIOException(I18N.getString("ImageUtil2")+" "+
                               writer.getClass().getName());
    }
}
 
Example 5
Source File: ImageUtil.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/** Checks that the provided <code>ImageWriter</code> can encode
 * the provided <code>ImageTypeSpecifier</code> or not.  If not, an
 * <code>IIOException</code> will be thrown.
 * @param writer The provided <code>ImageWriter</code>.
 * @param type The image to be tested.
 * @throws IIOException If the writer cannot encoded the provided image.
 */
public static final void canEncodeImage(ImageWriter writer,
                                        ImageTypeSpecifier type)
    throws IIOException {
    ImageWriterSpi spi = writer.getOriginatingProvider();

    if(type != null && spi != null && !spi.canEncodeImage(type))  {
        throw new IIOException(I18N.getString("ImageUtil2")+" "+
                               writer.getClass().getName());
    }
}
 
Example 6
Source File: ValidateXImage.java    From sambox with Apache License 2.0 5 votes vote down vote up
public static void validate(PDImageXObject ximage, int bpc, int width, int height,
        String format, String colorSpaceName) throws IOException
{
    // check the dictionary
    assertNotNull(ximage);
    COSStream cosStream = ximage.getCOSObject();
    assertNotNull(cosStream);
    assertEquals(COSName.XOBJECT, cosStream.getItem(COSName.TYPE));
    assertEquals(COSName.IMAGE, cosStream.getItem(COSName.SUBTYPE));
    assertTrue(ximage.getCOSObject().getFilteredLength() > 0);
    assertEquals(bpc, ximage.getBitsPerComponent());
    assertEquals(width, ximage.getWidth());
    assertEquals(height, ximage.getHeight());
    assertEquals(format, getSuffix(ximage));
    assertEquals(colorSpaceName, ximage.getColorSpace().getName());

    // check the image
    assertNotNull(ximage.getImage());
    assertEquals(ximage.getWidth(), ximage.getImage().getWidth());
    assertEquals(ximage.getHeight(), ximage.getImage().getHeight());

    boolean canEncode = true;
    boolean writeOk;
    // jdk11+ no longer encodes ARGB jpg
    // https://bugs.openjdk.java.net/browse/JDK-8211748
    if ("jpg".equals(format) && ximage.getImage().getType() == BufferedImage.TYPE_INT_ARGB)
    {
        ImageWriter writer = ImageIO.getImageWritersBySuffix(format).next();
        ImageWriterSpi originatingProvider = writer.getOriginatingProvider();
        canEncode = originatingProvider.canEncodeImage(ximage.getImage());
    }
    if (canEncode)
    {
        writeOk = ImageIO.write(ximage.getImage(), format, new ByteArrayOutputStream());
        assertTrue(writeOk);
    }
    writeOk = ImageIO.write(ximage.getOpaqueImage(), format, new ByteArrayOutputStream());
    assertTrue(writeOk);
}
 
Example 7
Source File: XDataTransferer.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
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;
    }

    // Add a mapping to the MIME native whenever the representation class
    // 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)) {
        // stringFlavor is semantically equivalent to the standard
        // "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;
}
 
Example 8
Source File: ImageIO.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
public boolean filter(Object elt) {
    ImageWriterSpi spi = (ImageWriterSpi)elt;
    return Arrays.asList(spi.getFormatNames()).contains(formatName) &&
        spi.canEncodeImage(type);
}
 
Example 9
Source File: BMPImageWriter.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 10
Source File: ImageIO.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public boolean filter(Object elt) {
    ImageWriterSpi spi = (ImageWriterSpi)elt;
    return Arrays.asList(spi.getFormatNames()).contains(formatName) &&
        spi.canEncodeImage(type);
}
 
Example 11
Source File: ImageIO.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public boolean filter(Object elt) {
    ImageWriterSpi spi = (ImageWriterSpi)elt;
    return Arrays.asList(spi.getFormatNames()).contains(formatName) &&
        spi.canEncodeImage(type);
}
 
Example 12
Source File: ImageIO.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public boolean filter(Object elt) {
    ImageWriterSpi spi = (ImageWriterSpi)elt;
    return Arrays.asList(spi.getFormatNames()).contains(formatName) &&
        spi.canEncodeImage(type);
}
 
Example 13
Source File: ImageIO.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public boolean filter(Object elt) {
    ImageWriterSpi spi = (ImageWriterSpi)elt;
    return Arrays.asList(spi.getFormatNames()).contains(formatName) &&
        spi.canEncodeImage(type);
}
 
Example 14
Source File: ImageIO.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
public boolean filter(Object elt) {
    ImageWriterSpi spi = (ImageWriterSpi)elt;
    return Arrays.asList(spi.getFormatNames()).contains(formatName) &&
        spi.canEncodeImage(type);
}
 
Example 15
Source File: XDataTransferer.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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;
    }

    // Add a mapping to the MIME native whenever the representation class
    // 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)) {
        // stringFlavor is semantically equivalent to the standard
        // "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;
}
 
Example 16
Source File: BMPImageWriter.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
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;
}
 
Example 17
Source File: BMPImageWriter.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 18
Source File: XDataTransferer.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public List getPlatformMappingsForFlavor(DataFlavor df) {
    List natives = new ArrayList(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;
    }

    // Add a mapping to the MIME native whenever the representation class
    // doesn't require translation.
    if (df.getRepresentationClass() != null &&
        (df.isRepresentationClassInputStream() ||
         df.isRepresentationClassByteBuffer() ||
         byteArrayClass.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)) {
        // stringFlavor is semantically equivalent to the standard
        // "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;
}
 
Example 19
Source File: BMPImageWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 20
Source File: BMPImageWriter.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
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;
}