Example usage for javax.imageio ImageReadParam getSourceBands

List of usage examples for javax.imageio ImageReadParam getSourceBands

Introduction

In this page you can find the example usage for javax.imageio ImageReadParam getSourceBands.

Prototype

public int[] getSourceBands() 

Source Link

Document

Returns the set of source bands to be used.

Usage

From source file:nitf.imageio.NITFReader.java

@Override
public Raster readRaster(int imageIndex, ImageReadParam param) throws IOException {
    checkIndex(imageIndex);/*from w ww  .  j ava  2  s.  c  om*/

    Rectangle sourceRegion = new Rectangle();
    Rectangle destRegion = new Rectangle();
    computeRegions(param, getWidth(imageIndex), getHeight(imageIndex), null, sourceRegion, destRegion);

    // Set everything to default values
    int sourceXSubsampling = param != null ? param.getSourceXSubsampling() : 1;
    int sourceYSubsampling = param != null ? param.getSourceYSubsampling() : 1;
    Point destinationOffset = param != null ? param.getDestinationOffset() : new Point(0, 0);

    ImageSubheader subheader;
    try {
        subheader = record.getImages()[imageIndex].getSubheader();
    } catch (NITFException e) {
        throw new IOException(ExceptionUtils.getStackTrace(e));
    }
    String irep = subheader.getImageRepresentation().getStringData().trim();
    String pvType = subheader.getPixelValueType().getStringData().trim();
    int nbpp = subheader.getNumBitsPerPixel().getIntData();
    int bandCount = subheader.getBandCount();

    // make the band offsets array, for the output
    int[] bandOffsets = null;
    int[] sourceBands = param != null ? param.getSourceBands() : null;
    if (param != null && param.getDestinationBands() != null)
        bandOffsets = param.getDestinationBands();
    else if (param != null && sourceBands != null) {
        bandOffsets = new int[sourceBands.length];
        for (int i = 0; i < bandOffsets.length; i++)
            bandOffsets[i] = sourceBands[i];
    } else {
        // Setup band offsets -- TODO should we really read ALL bands by
        // default?
        bandOffsets = new int[bandCount];
        for (int i = 0; i < bandOffsets.length; i++)
            bandOffsets[i] = i;
    }

    int nBytes = ((nbpp - 1) / 8) + 1;

    int bufType = -1;

    // byte
    if (nBytes == 1) {
        bufType = DataBuffer.TYPE_BYTE;
    }
    // short
    else if (nBytes == 2) {
        bufType = DataBuffer.TYPE_USHORT;
    }
    // float
    else if (nBytes == 4 && pvType.equals("R")) {
        bufType = DataBuffer.TYPE_FLOAT;
    }
    // double
    else if (nBytes == 8 && pvType.equals("R")) {
        bufType = DataBuffer.TYPE_DOUBLE;
    } else {
        throw new NotImplementedException("not yet implemented");
    }

    WritableRaster ras = ImageIOUtils.makeGenericPixelInterleavedWritableRaster(destRegion.width,
            destRegion.height, bandOffsets.length, bufType);
    checkReadParamBandSettings(param, bandCount, ras.getSampleModel().getNumBands());
    readRaster(imageIndex, sourceRegion, destRegion, sourceXSubsampling, sourceYSubsampling, bandOffsets,
            nBytes, destinationOffset, ras);
    return ras;
}

From source file:nitf.imageio.NITFReader.java

@Override
public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
    readHeader();//from  w  w w . jav a  2  s .co  m
    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");
}