Example usage for javax.imageio ImageTypeSpecifier getNumBands

List of usage examples for javax.imageio ImageTypeSpecifier getNumBands

Introduction

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

Prototype

public int getNumBands() 

Source Link

Document

Return the number of bands specified by this object.

Usage

From source file:nitf.imageio.NITFReader.java

@Override
public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
    readHeader();/*from   ww w.  jav a 2  s  .c om*/
    Raster raster = readRaster(imageIndex, param);

    // get the requested number of destination bands (or 0 for all)
    int numDestBands = param != null ? (param.getDestinationBands() != null ? param.getDestinationBands().length
            : param.getSourceBands() != null ? param.getSourceBands().length : 0) : 0;

    // try to find a good match for the specifier
    ImageTypeSpecifier imageType = null, firstType = null;
    Iterator<ImageTypeSpecifier> imageTypes = getImageTypes(imageIndex);
    while (imageTypes.hasNext() && imageType == null) {
        ImageTypeSpecifier currentImageType = imageTypes.next();
        if (firstType == null)
            firstType = currentImageType;

        if (currentImageType.getNumBands() == numDestBands)
            imageType = currentImageType;
    }

    if (imageType == null) {
        if (firstType == null)
            throw new IOException("Unable to determine the ImageTypeSpecifier");
        else
            imageType = firstType;
    }

    try {
        ImageSubheader subheader = record.getImages()[imageIndex].getSubheader();
        String pvType = subheader.getPixelValueType().getStringData().trim();
        int nbpp = subheader.getNumBitsPerPixel().getIntData();
        int nBytes = ((nbpp - 1) / 8) + 1;

        if (nBytes == 1 || nBytes == 2 || (nBytes == 4 && pvType.equals("R"))
                || (nBytes == 8 && pvType.equals("R"))) {
            return ImageIOUtils.rasterToBufferedImage(raster, imageType);
        }
    } catch (NITFException e) {
        throw new IOException(ExceptionUtils.getStackTrace(e));
    }
    throw new NotImplementedException("Image pixel type or bits per pixel not yet supported");
}