Skip to content

Crop Operation

n-lagomarsini edited this page Jun 24, 2014 · 1 revision

This module contains a new implementation of the JAI Crop operation.

WORKFLOW:

  • creation of the test-files for checking all the functionality of this new operation.
  • development of a new CropOpImage class to test with the previous test-file, the related Descriptor and RenderedImageFactory.

The main differences between the two operations are 2:

  • the hints related to the tile scheduler and the tile cache are now maintained.
  • NoData and ROI are handled.

The Crop operation returns only a a part of the source image. This operation can be simply executed by only taking the tiles inside the destination image bounds and skipping the others.

The classes that compose this module are:

  • CropOpImage.java : this class executes the crop operation.
  • CropDescriptor.java : this class describes the functionality of the Crop operation and the input parameter to set.
  • CropRIF.java : this class is the RenderedImageFactory associated with the Crop operation
  • CropSpi.java : this class is a Service provider interface used for manually registering the RenderedImageFactory and descriptor.

The CropOpImage class takes in input the coordinates for cropping the image and creates a new layout for the destination image. Then the crop operation is done by taking each source image tile and returning it only if it lies inside the input bounds, else a null value is returned. If a ROI object is present, it is intersected with the input bounds for finding the active bounds on which the crop operation is accomplished. If a NoData Range is present, then the crop operation is composed by 2 steps: the first step is a mosaic on the initial image for substituting the NoData values with the optional destinationNoData values, and the second step is a crop of the resultant mosaic.

The CropDescriptor defines the functionality and the input parameters of the Crop operation. Also it provides a create() method which crops an input image. This method takes and groups the input parameters inside a parameterBlock object and passes them to the JAI.create() method. With this last operation the RenderedImageFactory, CropRIF, is called and then it returns a new instance of the CropOpImage.

The CropSpi class is an utility class that can be used for manually registering the descriptor and the RenderedImageFactory if JAI fails to automatically register them.

A simple pseudo-code for understanding the crop operation:

// s[x][y] = pixel value of the source.
// tile = selected tile.
// insideROI = boolean indicating that the tile is inside ROI.
// validData = boolean indicating that the value is not a No Data.
// tileInsideBounds = boolean indicating if the tile is inside the crop bounds.
// hasNoData = boolean indicating that the a input No Data Range is used
// destinationNoData = value indicating destination No Data.
// numTiles,srcHeight,srcWidth = source image tiles number, height, width.

for(int t = 0; t<numTiles;t++){
    if(insideROI && tileInsideBounds){
          if(hasNoData){
                 //Mosaic operation
                 for(int y = 0; y<srcHeight;y++){
                     for(int x = 0; x<srcWidth;x++){
                         if(validData){
                            tile[x][y]=s[x][y];
                         }else{
                            tile[x][y]=destinationNoData;
                         }                         
                     }
                 }
          }
          // The tile is returned.
          return tile;
    }
}

For testing the functionalities of the Crop operations these classes are used:

  • CropImageTest.java
  • ComparisonTest.java

The first test-class is used for checking if the new version of the Crop operation behaves correctly. This evaluation is made by doing the two different Crop operation on the same image and then calculating the subtraction of them; if the result image is always 0 then the two operators are equals. The presence of ROI or No Data is tested by ensuring that the image cropped with ROI is smaller than the original cropped image and that the destination No Data values are present. The user can see the result of the images cropped in 3 different cases by setting the JVM parameters JAI.Ext.Interactive to true and JAI.Ext.TestSelector to the associated value:

  • without ROI and No Data,0
  • with ROI and without No Data,1
  • with ROI and No Data,2

The second test is used for evaluating the performances of the two crop operators. This test is made by executing the crop operation with one of the two CropDescriptor and saving the mean, maximum and minimum computation time. At the end of every cycle the JAI TileCache is flushed so that all the image tiles must be recalculated. The average, maximum and minimum computation times are not saved for all the iterations, because the first N iterations are not considered due to the Java HotSpot compilation. The number of the iterations to consider and not can be set by passing respectively these 2 Integer parameters to the JVM: JAI.Ext.BenchmarkCycles and JAI.Ext.NotBenchmarkCycles. For selecting which of the 2 descriptor associated to the operation must be tested, the JAI.Ext.OldDescriptor JVM parameter must be set to true or false(true for the old descriptor and false for the new one); the JVM parameter JAI.Ext.TestSelector can be from 0 to 5 and indicates the image data type. Finally, if the user wants to add the NoData or ROI control, the JAI.Ext.RangeUsed and the JAI.Ext.ROIUsed parameters must be respectively set to true. The computation times are print to the screen at the end of the process.