Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- * Simple class to test read and write capabilities provided by the Kakadu plugin.
- *
- * Make sure to add additional checks (like null checks, dynamic library availability,...) for real
- * usage.
- */
- /**
- * Test write operation. Compress the following RenderedImage to the provided output file. Also adds
- * a GeoJP2 georeferencing box as provided by the geoJp2 byte array parameter.
- *
- *
- * @param inputImage the input {@link RenderedImage} to be compressed and written as jp2.
- * @param fileOut the output {@link File} where to write the image
- * @param geoJp2 the geoJp2 content to be added as georeference.
- * @throws KduException
- * @throws FileNotFoundException
- * @throws IOException
- */
- public void testWrite(final RenderedImage inputImage, final File fileOut, final byte[] geoJp2) throws KduException,
- IOException {
- FileImageOutputStreamExtImpl outStream = null;
- ImageWriter writer = null;
- try {
- //Setting up the output
- outStream = new FileImageOutputStreamExtImpl(fileOut);
- writer = new JP2KKakaduImageWriterSpi().createWriterInstance();
- if (writer != null){
- writer.setOutput(outStream);
- final JP2KKakaduImageWriteParam param = new JP2KKakaduImageWriteParam();
- // //
- //
- // Setting up write parameters
- // quality = 0.125 ===============> Lossy 8:1 compression
- // cLevels = 5 ===================> 5 DWT levels
- // qualityLayers = 12 ============> 12 Quality layers
- // tilingMode = EXPLICIT =========> force the creation of inner tiling in the output
- // tileWidth = tileHeight = 512 ==> Inner tiles are 512x512
- // orgGen_plt = true =============> setting packet length information in the header of tile-parts
- // cOrder = RPCL =================> setting progression order as RPCL
- // cPrecincts = "{512,512},{256,256},{128,128},{64,64},{32,32},{16,16}"
- // //
- param.setQuality(0.125);
- param.setCLevels(5);
- param.setQualityLayers(12);
- param.setTilingMode(ImageWriteParam.MODE_EXPLICIT);
- param.setTiling(512, 512, 0, 0);
- param.setOrgGen_plt(true);
- param.setcOrder(ProgressionOrder.RPCL);
- param.setcPrecincts("{512,512},{256,256},{128,128},{64,64},{32,32},{16,16}");
- // Setting georeferencing through a geoJp2 byte array which represents
- // the content of a degenerated 1x1 tif image with inner georeferencing.
- //
- param.setGeoJp2(geoJp2);
- writer.write(null, new IIOImage(inputImage, null, null), param);
- }
- } finally {
- try {
- if (outStream != null){
- outStream.close();
- }
- } catch (Throwable t){
- //Ignore
- }
- try {
- if (writer != null){
- writer.dispose();
- }
- } catch (Throwable t){
- //Ignore
- }
- }
- }
- /**
- * Test reading a Jp2 image from the provided inputFile.
- * @param inputFile the {@link File} containing the input jp2 image.
- * @throws IOException
- */
- public void testRead(final File inputFile) throws IOException {
- ImageReader reader = null;
- try {
- reader = new JP2KKakaduImageReaderSpi().createReaderInstance();
- reader.setInput(inputFile);
- final ImageReadParam param = new JP2KKakaduImageReadParam();
- // access the 5th decomposition level (using a 32 subsampling factor
- // on both x,y components) of a top-left area of the image with
- // width and height = 2048x2048
- param.setSourceSubsampling(32, 32, 0, 0);
- param.setSourceRegion(new Rectangle(0,0,2048,2048));
- final RenderedImage image = reader.read(0, param);
- } finally {
- try {
- if (reader != null){
- reader.dispose();
- }
- } catch (Throwable t){
- //Ignore
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement