Example usage for javax.imageio ImageReadParam setSourceBands

List of usage examples for javax.imageio ImageReadParam setSourceBands

Introduction

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

Prototype

public void setSourceBands(int[] sourceBands) 

Source Link

Document

Sets the indices of the source bands to be used.

Usage

From source file:ShowImage.java

public static void main(String args[]) throws IOException {
    List<File> files = ImageIOUtils.getFiles(args, new String[] { "ntf", "nsf" });

    for (Iterator iter = files.iterator(); iter.hasNext();) {
        try {//from   ww  w  .  j  av a  2  s .c  o m
            File file = (File) iter.next();
            log.debug("Reading: " + file.getAbsolutePath());
            NITFReader imageReader = (NITFReader) ImageIOUtils.getImageReader("nitf", file);

            for (int i = 0; i < imageReader.getRecord().getImages().length; ++i) {
                log.debug(file.getName() + "[" + i + "]");

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                ImageSubheader subheader = imageReader.getRecord().getImages()[i].getSubheader();
                subheader.print(new PrintStream(stream));
                log.debug(stream.toString());

                try {
                    int numBands = subheader.getBandCount();
                    String irep = subheader.getImageRepresentation().getStringData().trim();
                    int bitsPerPixel = subheader.getNumBitsPerPixel().getIntData();
                    int nBytes = (bitsPerPixel - 1) / 8 + 1;

                    if (irep.equals("RGB") && numBands == 3) {
                        BufferedImage image = imageReader.read(i);
                        ImageIOUtils.showImage(image, file.getName() + "[" + i + "]", true);
                    } else {
                        // read each band, separately
                        for (int j = 0; j < numBands; ++j) {
                            if (nBytes == 1 || nBytes == 2 || nBytes == 4 || nBytes == 8) {
                                ImageReadParam readParam = imageReader.getDefaultReadParam();
                                readParam.setSourceBands(new int[] { j });
                                BufferedImage image = imageReader.read(i, readParam);
                                ImageIOUtils.showImage(image, file.getName() + "[" + i + "][" + j + "]", true);

                                ImageIO.write(image, "jpg",
                                        new FileOutputStream("image" + i + "_" + j + ".jpg"));

                                // downsample
                                // readParam.setSourceSubsampling(2, 2, 0,
                                // 0);
                                // BufferedImage smallerImage = imageReader
                                // .read(i, readParam);
                                //
                                // ImageIOUtils.showImage(smallerImage,
                                // "DOWNSAMPLED: " + file.getName());

                            }
                        }
                    }
                } catch (Exception e) {
                    System.out.println(ExceptionUtils.getStackTrace(e));
                    log.error(ExceptionUtils.getStackTrace(e));
                }
            }
        } catch (Exception e) {
            log.debug(ExceptionUtils.getStackTrace(e));
        }
    }
}

From source file:nitf.imagej.NITF_Reader.java

/**
 * Opens the requested NITF, displaying a Text Window with the metadata
 * contents, and an Image window for each image in the file
 * //w w w.  j  a v a2s .  c  o  m
 * @param dir
 * @param name
 */
public void openNITF(File file) {
    try {
        log.debug("Reading: " + file.getAbsolutePath());
        NITFReader imageReader = (NITFReader) ImageIOUtils.getImageReader("nitf", file);

        Record record = imageReader.getRecord();
        // print the record contents to a byte stream
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        record.print(new PrintStream(out));

        TextWindow resultsWindow = new TextWindow(file.getName() + " [metadata]", "",
                IJ.getInstance().getWidth(), 400);
        TextPanel textPanel = resultsWindow.getTextPanel();
        textPanel.append(out.toString());
        textPanel.setFont(new Font("Courier New", Font.PLAIN, 16));
        resultsWindow.setVisible(true);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        for (int i = 0; i < record.getImages().length; ++i) {
            ImageSubheader subheader = record.getImages()[i].getSubheader();
            try {
                int numCols = subheader.getNumCols().getIntData();
                int numRows = subheader.getNumRows().getIntData();
                int numBands = subheader.getBandCount();
                String irep = subheader.getImageRepresentation().getStringData().trim();
                int bitsPerPixel = subheader.getNumBitsPerPixel().getIntData();

                ImageReadParam readParam = imageReader.getDefaultReadParam();

                String downsampleString = "";

                // if the image is larger than 1.5 times the screen
                // width/height,
                // then just downsample right away
                if (numCols > (screenSize.width * 1.5) || numRows > (screenSize.height * 1.5)) {
                    int xTimes = (int) (numCols / (double) screenSize.width);
                    int yTimes = (int) (numRows / (double) screenSize.height);

                    downsampleString = " (downsampled from " + numCols + "x" + numRows;

                    // pick the min, in case it turns out to be a little
                    // strip
                    int pixelSkip = Math.min(xTimes, yTimes);
                    numCols = (int) Math.ceil(numCols / pixelSkip);
                    numRows = (int) Math.ceil(numCols / pixelSkip);

                    downsampleString += "   to   " + numCols + "x" + numRows + ")";
                    readParam.setSourceSubsampling(pixelSkip, pixelSkip, 0, 0);
                }

                if (irep.equals("RGB") && numBands == 3) {
                    BufferedImage image = imageReader.read(i, readParam);
                    ImagePlus imagePlus = new ImagePlus(
                            file.getName() + " [" + i + "] - RGB" + downsampleString, image);
                    imagePlus.show();
                } else {
                    ImageStack imageStack = new ImageStack(numCols, numRows);
                    // read each band, separately
                    for (int j = 0; j < numBands; ++j) {
                        if (bitsPerPixel == 16 || bitsPerPixel == 8 || bitsPerPixel == 32
                                || bitsPerPixel == 64) {
                            readParam.setSourceBands(new int[] { j });
                            BufferedImage image = imageReader.read(i, readParam);
                            imageStack.addSlice(file.getName() + " [" + i + "] Band " + j + downsampleString,
                                    getPixelsFromBufferedImage(image));
                        }
                    }
                    if (imageStack.getSize() > 0) {
                        ImagePlus imagePlus = new ImagePlus(file.getName() + " [" + i + "]" + downsampleString,
                                imageStack);
                        imagePlus.show();
                    }
                }
            } catch (Exception e) {
                log.debug(ExceptionUtils.getStackTrace(e));
            }
        }
    } catch (Exception e) {
        log.debug(ExceptionUtils.getStackTrace(e));
        IJ.error(e.getMessage());
    }
}