// Rely on rate allocator to limit amount of data
File tmpFile = File.createTempFile("jiio-", ".tmp");
tmpFile.deleteOnExit();
// Creates CodestreamWriter
FileCodestreamWriter bwriter =
new FileCodestreamWriter(tmpFile, Integer.MAX_VALUE);
// Creates the rate allocator
float rate = (float)j2kwparam.getEncodingRate();
PostCompRateAllocator ralloc =
PostCompRateAllocator.createInstance(ecoder,
rate,
bwriter,
j2kwparam);
// Instantiates the HeaderEncoder
HeaderEncoder headenc =
new HeaderEncoder(imgsrc, imsigned, dwt, imgtiler,
j2kwparam, rois,ralloc);
ralloc.setHeaderEncoder(headenc);
// Writes header to be able to estimate header overhead
headenc.encodeMainHeader();
//Initializes rate allocator, with proper header
// overhead. This will also encode all the data
try {
ralloc.initialize();
} catch (RuntimeException e) {
if (WRITE_ABORTED.equals(e.getMessage())) {
bwriter.close();
tmpFile.delete();
processWriteAborted();
return;
} else throw e;
}
// Write header (final)
headenc.reset();
headenc.encodeMainHeader();
// Insert header into the codestream
bwriter.commitBitstreamHeader(headenc);
// Now do the rate-allocation and write result
ralloc.runAndWrite();
//Done for data encoding
bwriter.close();
// Calculate file length
int fileLength = bwriter.getLength();
// Tile-parts and packed packet headers
int pktspertp = j2kwparam.getPacketPerTilePart();
int ntiles = imgtiler.getNumTiles();
if (pktspertp>0 || pphTile || pphMain){