use of it.geosolutions.imageio.plugins.nitronitf.wrapper.ShapeFileWrapper in project imageio-ext by geosolutions-it.
the class NITFImageWriter method write.
@Override
public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException {
// Headers and segments initialization
NITFImageWriteParam nitfParam = null;
HeaderWrapper header = null;
Map<String, Map<String, String>> extensionsMap = null;
ShapeFileWrapper shape = null;
List<TextWrapper> texts = null;
WriteCompression compression = null;
List<ImageWrapper> inputImages = null;
if (param != null && param instanceof NITFImageWriteParam) {
nitfParam = (NITFImageWriteParam) param;
NITFProperties nitfMetadata = nitfParam.getNitfProperties();
if (nitfMetadata != null) {
header = nitfMetadata.getHeader();
shape = nitfMetadata.getShape();
texts = nitfMetadata.getTextsWrapper();
inputImages = nitfMetadata.getImagesWrapper();
}
compression = nitfParam.getWriteCompression();
}
ImageWrapper imageW = inputImages.get(0);
RenderedImage ri = imageW.getImage();
final boolean isJP2 = (compression != null && compression != WriteCompression.UNCOMPRESSED);
FileImageInputStreamExt jp2Stream = null;
File tempFile = null;
try {
Record record = new Record(Version.NITF_21);
if (isJP2) {
// Proceeding with jp2 compression
if (JP2_TEMP_FOLDER != null) {
tempFile = File.createTempFile("jp2compressed", ".jpc", new File(JP2_TEMP_FOLDER));
}
String parentPath = outputFile.getParent();
String name = FilenameUtils.getBaseName(outputFile.getCanonicalPath());
tempFile = new File(parentPath + File.separatorChar + name + ".j2c");
prepareJP2Image(ri, tempFile, compression);
jp2Stream = new FileImageInputStreamExtImpl(tempFile);
}
// populating the file header
initFileHeader(record, header);
// adding an image segment to the record
addImageSegment(record, inputImages, jp2Stream, compression);
if (texts != null && !texts.isEmpty()) {
// adding a text segment if present
addTextSegment(record, texts);
}
if (!writeNITF(record, inputImages, shape, jp2Stream, texts)) {
throw new IOException("Unable to successfully write");
}
} catch (Throwable t) {
IOException ioe = new IOException();
ioe.initCause(t);
throw ioe;
} finally {
// Releasing resources
if (jp2Stream != null) {
try {
jp2Stream.close();
} catch (Throwable thr) {
// Eat exception
}
}
if (tempFile != null) {
try {
tempFile.delete();
} catch (Throwable thr) {
}
}
}
// record.destruct();
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Successfully wrote NITF: " + outputFile);
}
}
Aggregations