Example usage for java.awt.image DataBufferByte getSize

List of usage examples for java.awt.image DataBufferByte getSize

Introduction

In this page you can find the example usage for java.awt.image DataBufferByte getSize.

Prototype

public int getSize() 

Source Link

Document

Returns the size (in array elements) of all banks.

Usage

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

/**
 * Do the real write operation (writing images, texts, ...)
 * //from   w w w . j a va 2 s.  co  m
 * @param record
 * @param images
 * @param shp
 * @param fis
 * @param text
 * @return
 * @throws NITFException
 * @throws IOException
 */
private boolean writeNITF(final Record record, final List<ImageWrapper> images, final ShapeFileWrapper shp,
        final FileImageInputStreamExt fis, final List<TextWrapper> texts) throws NITFException, IOException {
    final int numImages = images.size();
    ImageWrapper image = images.get(0);
    RenderedImage ri = image.getImage();
    WriteCompression compression = image.getCompression();
    int nBands = ri.getSampleModel().getNumBands();
    boolean written = false;
    Writer writer = new Writer();
    IOHandle handle = new IOHandle(outputFile.getCanonicalPath(), IOHandle.NITF_ACCESS_WRITEONLY,
            IOHandle.NITF_CREATE);

    byte[] shapeFileData = null;
    final boolean isJP2 = !(compression == WriteCompression.UNCOMPRESSED);
    if (shp != null) {
        shapeFileData = getShapeData(record, shp);
    }

    boolean prepared = false;
    if (isJP2) {
        // //
        //
        // get the JP2 Codestream previously written with Kakadu and transfer its content within
        // the NITF imageSegment
        //
        // //
        WriteHandler codeStream = null;
        IOInterface io;
        final int size = (int) fis.length();
        io = new IOFileInputStream(fis);

        writer.prepare(record, handle);

        if (shapeFileData != null) {
            writeData(shapeFileData, writer);
        }

        codeStream = new StreamIOWriteHandler(io, 0, size);
        writer.setImageWriteHandler(0, codeStream);
        prepared = true;

    }
    if (!isJP2 || numImages > 1) {

        if (!prepared) {
            writer.prepare(record, handle);
        }

        if (numImages == 1) {

            // setup a Writer
            if (shapeFileData != null) {
                writeData(shapeFileData, writer);
            }

            ImageSource imageSource = new ImageSource();
            nitf.ImageWriter imageWriter = writer.getNewImageWriter(0);
            boolean[] successes = new boolean[nBands];
            final boolean isMono = images.get(0).getImage().getSampleModel().getNumBands() == 1;
            if (isMono) {
                DataBufferByte dbb = (DataBufferByte) ri.getData().getDataBuffer();
                BandSource bs = new MemorySource(dbb.getData(), dbb.getSize(), 0, 0, 0);
                successes[0] = imageSource.addBand(bs);
            } else {
                for (int i = 0; i < nBands; i++) {
                    RenderedImage band = BandSelectDescriptor.create(ri, new int[] { i }, null);
                    DataBufferByte dbb = (DataBufferByte) band.getData().getDataBuffer();
                    BandSource bs = new MemorySource(dbb.getData(), dbb.getSize(), 0, 0, 0);
                    successes[i] = imageSource.addBand(bs);
                }
            }

            imageWriter.attachSource(imageSource);
        } else {
            ImageWrapper img = images.get(1);
            ri = img.getImage();
            nBands = ri.getSampleModel().getNumBands();
            ImageSource imageSource = new ImageSource();
            nitf.ImageWriter imageWriter2 = writer.getNewImageWriter(1);
            boolean[] successes = new boolean[nBands];
            DataBufferByte dbb = (DataBufferByte) ri.getData().getDataBuffer();
            BandSource bs = new MemorySource(dbb.getData(), dbb.getSize(), 0, 0, 0);
            successes[0] = imageSource.addBand(bs);
            imageWriter2.attachSource(imageSource);
        }

    }

    // Adding text
    if (texts != null && !texts.isEmpty()) {
        int i = 0;
        for (TextWrapper text : texts) {
            byte[] textContent = text.getTextContent();
            if (textContent != null) {
                SegmentWriter textWriter = writer.getNewTextWriter(i++);
                SegmentSource source = SegmentSource.makeSegmentMemorySource(textContent, textContent.length, 0,
                        0);
                textWriter.attachSource(source);
            }
        }
    }

    written = writer.write();
    if (handle != null) {
        handle.close();
    }

    return written;
}