Search in sources :

Example 1 with IOInterface

use of nitf.IOInterface in project imageio-ext by geosolutions-it.

the class NITFImageWriter method writeNITF.

/**
 * Do the real write operation (writing images, texts, ...)
 *
 * @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;
}
Also used : IOHandle(nitf.IOHandle) StreamIOWriteHandler(nitf.StreamIOWriteHandler) DataBufferByte(java.awt.image.DataBufferByte) SegmentSource(nitf.SegmentSource) BandSource(nitf.BandSource) TextWrapper(it.geosolutions.imageio.plugins.nitronitf.wrapper.TextWrapper) ImageWrapper(it.geosolutions.imageio.plugins.nitronitf.wrapper.ImageWrapper) WriteHandler(nitf.WriteHandler) StreamIOWriteHandler(nitf.StreamIOWriteHandler) IOInterface(nitf.IOInterface) ImageSource(nitf.ImageSource) MemorySource(nitf.MemorySource) RenderedImage(java.awt.image.RenderedImage) SegmentWriter(nitf.SegmentWriter) JP2KKakaduImageWriter(it.geosolutions.imageio.plugins.jp2k.JP2KKakaduImageWriter) SegmentWriter(nitf.SegmentWriter) Writer(nitf.Writer) ImageWriter(javax.imageio.ImageWriter) WriteCompression(it.geosolutions.imageio.plugins.nitronitf.NITFUtilities.WriteCompression)

Aggregations

JP2KKakaduImageWriter (it.geosolutions.imageio.plugins.jp2k.JP2KKakaduImageWriter)1 WriteCompression (it.geosolutions.imageio.plugins.nitronitf.NITFUtilities.WriteCompression)1 ImageWrapper (it.geosolutions.imageio.plugins.nitronitf.wrapper.ImageWrapper)1 TextWrapper (it.geosolutions.imageio.plugins.nitronitf.wrapper.TextWrapper)1 DataBufferByte (java.awt.image.DataBufferByte)1 RenderedImage (java.awt.image.RenderedImage)1 ImageWriter (javax.imageio.ImageWriter)1 BandSource (nitf.BandSource)1 IOHandle (nitf.IOHandle)1 IOInterface (nitf.IOInterface)1 ImageSource (nitf.ImageSource)1 MemorySource (nitf.MemorySource)1 SegmentSource (nitf.SegmentSource)1 SegmentWriter (nitf.SegmentWriter)1 StreamIOWriteHandler (nitf.StreamIOWriteHandler)1 WriteHandler (nitf.WriteHandler)1 Writer (nitf.Writer)1