How to access pixel blocks using a raster cursor


Summary
This article shows how to work with a pixel block and raster cursor to read and write pixel values.

Accessing pixel blocks using a raster cursor

Raster pixel access is normally handled block by block and the RasterCursor class is used to loop through the pixel blocks of a whole raster. You can define the size of a pixel block or use a system optimized pixel block size.
See the following code example:
[Java]
static void usingRasterCursorWithPixelBlock(IRasterDataset2 rasterDs)throws
    Exception{
    //Create a raster. 
    IRaster2 raster2 = new IRaster2Proxy(rasterDs.createFullRaster());
    //Create a raster cursor with a system-optimized pixel block size by passing a null.
    IRasterCursor rasterCursor = raster2.createCursorEx(null);
    //Use the IRasterEdit interface.
    IRasterEdit rasterEdit = new IRasterEditProxy(raster2);
    //Loop through each band and pixel block.
    IRasterBandCollection bands = new IRasterBandCollectionProxy(rasterDs);
    IPixelBlock3 pixelblock3 = null;
    long blockwidth = 0;
    long blockheight = 0;
    Object pixels;
    IPnt tlc = null;
    long bandCount = bands.getCount();
    do{
        pixelblock3 = new IPixelBlock3Proxy(rasterCursor.getPixelBlock());
        blockwidth = pixelblock3.getWidth();
        blockheight = pixelblock3.getHeight();
        pixelblock3.mask(255);
        for (int k = 0; k < bandCount; k++){
            //Get the pixel array.
            pixels = pixelblock3.getPixelData(k);
            for (long i = 0; i < blockwidth - 1; i++){
                for (long j = 0; j < blockheight - 1; j++){
                    //Get the pixel value from the Object pixels.
                }
            }
            //Set the pixel array to the pixel block.
            pixelblock3.setPixelData(k, pixels);
        }
        //Write back to the raster.
        tlc = rasterCursor.getTopLeft();
        rasterEdit.write(tlc, new IPixelBlockProxy(pixelblock3));
    }
    while (rasterCursor.next());
}






Development licensingDeployment licensing
ArcGIS for Desktop BasicArcGIS for Desktop Basic
ArcGIS for Desktop StandardArcGIS for Desktop Standard
ArcGIS for Desktop AdvancedArcGIS for Desktop Advanced
Engine Developer KitEngine