Advertisement
Guest User

Writing a JPEG200 file with ImageIO-Ext Kakadu support

a guest
Oct 25th, 2010
1,165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1.  * Simple class to test read and write capabilities provided by the Kakadu plugin.
  2.  *
  3.  * Make sure to add additional checks (like null checks, dynamic library availability,...) for real
  4.  * usage.
  5.  */
  6.  
  7.  
  8.  /**
  9.   * Test write operation. Compress the following RenderedImage to the provided output file. Also adds
  10.   * a GeoJP2 georeferencing box as provided by the geoJp2 byte array parameter.
  11.   *
  12.   *
  13.   * @param inputImage the input {@link RenderedImage} to be compressed and written as jp2.
  14.   * @param fileOut the output {@link File} where to write the image
  15.   * @param geoJp2 the geoJp2 content to be added as georeference.
  16.   * @throws KduException
  17.   * @throws FileNotFoundException
  18.   * @throws IOException
  19.   */
  20.     public void testWrite(final RenderedImage inputImage, final File fileOut, final byte[] geoJp2) throws KduException,
  21.             IOException {
  22.    
  23.         FileImageOutputStreamExtImpl outStream = null;
  24.         ImageWriter writer = null;
  25.         try {
  26.             //Setting up the output
  27.             outStream = new FileImageOutputStreamExtImpl(fileOut);
  28.             writer = new JP2KKakaduImageWriterSpi().createWriterInstance();
  29.             if (writer != null){
  30.                 writer.setOutput(outStream);
  31.                 final JP2KKakaduImageWriteParam param = new JP2KKakaduImageWriteParam();
  32.                
  33.                 // //
  34.                 //
  35.                 // Setting up write parameters
  36.                 // quality = 0.125 ===============> Lossy 8:1 compression
  37.                 // cLevels = 5 ===================> 5 DWT levels
  38.                 // qualityLayers = 12 ============> 12 Quality layers
  39.                 // tilingMode = EXPLICIT =========> force the creation of inner tiling in the output
  40.                 // tileWidth = tileHeight = 512 ==> Inner tiles are 512x512
  41.                 // orgGen_plt = true =============> setting packet length information in the header of tile-parts
  42.                 // cOrder = RPCL =================> setting progression order as RPCL
  43.                 // cPrecincts = "{512,512},{256,256},{128,128},{64,64},{32,32},{16,16}"
  44.                 // //
  45.                 param.setQuality(0.125);
  46.                 param.setCLevels(5);
  47.                 param.setQualityLayers(12);
  48.                 param.setTilingMode(ImageWriteParam.MODE_EXPLICIT);
  49.                 param.setTiling(512, 512, 0, 0);
  50.                 param.setOrgGen_plt(true);
  51.                 param.setcOrder(ProgressionOrder.RPCL);
  52.                 param.setcPrecincts("{512,512},{256,256},{128,128},{64,64},{32,32},{16,16}");
  53.                
  54.                 // Setting georeferencing through a geoJp2 byte array which represents
  55.                 // the content of a degenerated 1x1 tif image with inner georeferencing.
  56.                 //
  57.                 param.setGeoJp2(geoJp2);
  58.                 writer.write(null, new IIOImage(inputImage, null, null), param);
  59.             }
  60.         } finally {
  61.             try {
  62.                 if (outStream != null){
  63.                     outStream.close();
  64.                 }
  65.             } catch (Throwable t){
  66.                 //Ignore
  67.             }
  68.             try {
  69.                 if (writer != null){
  70.                     writer.dispose();
  71.                 }
  72.             } catch (Throwable t){
  73.                 //Ignore
  74.             }
  75.         }
  76.            
  77.     }
  78.    
  79.     /**
  80.      * Test reading a Jp2 image from the provided inputFile.
  81.      * @param inputFile the {@link File} containing the input jp2 image.
  82.      * @throws IOException
  83.      */
  84.     public void testRead(final File inputFile) throws IOException {
  85.         ImageReader reader = null;
  86.         try {
  87.             reader = new JP2KKakaduImageReaderSpi().createReaderInstance();
  88.             reader.setInput(inputFile);
  89.             final ImageReadParam param = new JP2KKakaduImageReadParam();
  90.            
  91.             // access the 5th decomposition level (using a 32 subsampling factor
  92.             // on both x,y components) of a top-left area of the image with
  93.             // width and height = 2048x2048
  94.             param.setSourceSubsampling(32, 32, 0, 0);
  95.             param.setSourceRegion(new Rectangle(0,0,2048,2048));
  96.             final RenderedImage image = reader.read(0, param);
  97.            
  98.         } finally {
  99.             try {
  100.                 if (reader != null){
  101.                     reader.dispose();
  102.                 }
  103.             } catch (Throwable t){
  104.                 //Ignore
  105.             }
  106.         }
  107.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement