Example usage for javax.imageio ImageTypeSpecifier createGrayscale

List of usage examples for javax.imageio ImageTypeSpecifier createGrayscale

Introduction

In this page you can find the example usage for javax.imageio ImageTypeSpecifier createGrayscale.

Prototype

public static ImageTypeSpecifier createGrayscale(int bits, int dataType, boolean isSigned) 

Source Link

Document

Returns a specifier for a grayscale image format that will pack pixels of the given bit depth into array elements of the specified data type.

Usage

From source file:ch5ImageReader.java

/**
 * this method provides suggestions for possible image types that will be
 * used to decode the image specified by index imageIndex. By default, the
 * first image type returned by this method will be the image type of the
 * BufferedImage returned by the ImageReader's getDestination method. In
 * this case, we are suggesting using an 8 bit grayscale image with no alpha
 * component./*from w ww.  j  a  va2 s.  com*/
 */
public Iterator getImageTypes(int imageIndex) {
    java.util.List l = new java.util.ArrayList();
    ;
    int bits = 8;

    /*
     * can convert ch5 format into 8 bit grayscale image with no alpha
     */
    l.add(ImageTypeSpecifier.createGrayscale(bits, DataBuffer.TYPE_BYTE, false));
    return l.iterator();
}

From source file:it.geosolutions.imageio.plugins.nitronitf.ImageIOUtils.java

/**
 * Utility method for creating a BufferedImage from a source raster Currently only Float->Byte and Byte->Byte are supported. Will throw an
 * {@link UnsupportedOperationException} if the conversion is not supported.
 * /*  ww  w  .j av  a2s .c o m*/
 * @param raster
 * @param imageType
 * @return
 */
public static BufferedImage rasterToBufferedImage(Raster raster, ImageTypeSpecifier imageType) {
    if (imageType == null) {
        if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE)
            imageType = ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, false);
        else
            throw new IllegalArgumentException("unable to dynamically determine the imageType");
    }
    // create a new buffered image, for display
    BufferedImage bufImage = imageType.createBufferedImage(raster.getWidth(), raster.getHeight());

    if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_USHORT
            && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) {
        // convert short pixels to bytes
        short[] shortData = ((DataBufferUShort) raster.getDataBuffer()).getData();
        byte[] byteData = ((DataBufferByte) bufImage.getWritableTile(0, 0).getDataBuffer()).getData();
        ImageIOUtils.shortToByteBuffer(shortData, byteData, 1, raster.getNumBands());
    } else if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_FLOAT
            && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) {
        // convert float pixels to bytes
        float[] floatData = ((DataBufferFloat) raster.getDataBuffer()).getData();
        byte[] byteData = ((DataBufferByte) bufImage.getWritableTile(0, 0).getDataBuffer()).getData();
        ImageIOUtils.floatToByteBuffer(floatData, byteData, 1, raster.getNumBands());
    } else if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_DOUBLE
            && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) {
        // convert double pixels to bytes
        double[] doubleData = ((DataBufferDouble) raster.getDataBuffer()).getData();
        byte[] byteData = ((DataBufferByte) bufImage.getWritableTile(0, 0).getDataBuffer()).getData();
        ImageIOUtils.doubleToByteBuffer(doubleData, byteData, 1, raster.getNumBands());
    } else if ((raster.getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE
            && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE)
            || (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_USHORT
                    && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_USHORT)
            || (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_SHORT
                    && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_SHORT)) {
        bufImage.setData(raster);
    } else {
        throw new UnsupportedOperationException(
                "Unable to convert raster type to bufferedImage type: " + raster.getDataBuffer().getDataType()
                        + " ==> " + bufImage.getRaster().getDataBuffer().getDataType());
    }
    return bufImage;
}

From source file:nitf.imageio.NITFReader.java

@Override
public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex) throws IOException {
    checkIndex(imageIndex);//from   w ww  . j a  va2  s.  com

    List<ImageTypeSpecifier> l = new ArrayList<ImageTypeSpecifier>();

    try {
        ImageSubheader subheader = record.getImages()[imageIndex].getSubheader();
        String irep = subheader.getImageRepresentation().getStringData().trim();
        String pvType = subheader.getPixelValueType().getStringData().trim();
        int bandCount = subheader.getBandCount();
        int nbpp = subheader.getNumBitsPerPixel().getIntData();

        // if (NITFUtils.isCompressed(record, imageIndex))
        // {
        // throw new NotImplementedException(
        // "Only uncompressed imagery is currently supported");
        // }
        int nBytes = ((nbpp - 1) / 8) + 1;
        if (nBytes == 1 || nBytes == 2 || (nBytes == 4 && pvType.equals("R"))
                || (nBytes == 8 && pvType.equals("R"))) {
            if (nBytes == 1 && bandCount == 3 && irep.equals("RGB")) {
                ColorSpace rgb = ColorSpace.getInstance(ColorSpace.CS_sRGB);
                int[] bandOffsets = new int[3];
                for (int i = 0; i < bandOffsets.length; ++i)
                    bandOffsets[i] = i;
                l.add(ImageTypeSpecifier.createInterleaved(rgb, bandOffsets, DataBuffer.TYPE_BYTE, false,
                        false));
            }
            l.add(ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, false));
        } else {
            throw new NotImplementedException(
                    "Support for pixels of size " + nbpp + " bytes has not been implemented yet");
        }
    } catch (NITFException e) {
        log.error(ExceptionUtils.getStackTrace(e));
    }
    return l.iterator();
}