[gdal-dev] Get pixel values from all bands

Alexander Bruy alexander.bruy at gmail.com
Wed Mar 16 16:21:28 EDT 2011


Hi all

I work on raster processing application (developed with C++ and Qt).
For reading rasters we use GDAL and it's C++ API.

For one task we need to get pixel values from all raster bands. For
this I get all GDALRasterBands and store them in list. Then in loop
read image row by row and store row from each band in another list.

Here is simplified code

// iterate over image row by row
for ( int row = 0; row < ySize; ++row )
{
  // read one row from each band and store it in list
  for ( int b = 0; b < bandCount; ++b )
  {
    float *scanline;
    scanline = (float *) CPLMalloc( sizeof( float ) * xSize );
    bands[ i ]->RasterIO( GF_Read, 0, row, xSize, 1, scanline, xSize,
                          1, GDT_Float32, 0, 0 );
    rows[ i ] = scanline;
  }

  // iterate on pixels in row
  for (int col = 0; col < ySize; ++col)
  {
    // get pixel value from bands
    float pixel[bandCount];
    for ( int r = 0; r < bandCount; ++r )
    {
       // get value in (row, col) from band r
       double pixel[r] = rows[ r ][ col ];
    }
    // do something with this pixel
  }
}

Is this correct approach? Or maybe there is another more correct way
to do this?

Also I'm not sure where I should free memory allocated with CPLMalloc().
After reading pixels and before reading next rows or at end when
all rows processed?

I'm not a big C++ programmer, so any help is very appreciated

Thanks
-- 
Alexander Bruy


More information about the gdal-dev mailing list