[gdal-dev] Get pixel values from all bands

Mateusz Loskot mateusz at loskot.net
Wed Mar 16 16:38:28 EDT 2011


Alexander Bruy wrote:
>
> 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?

Seems OK to me, except that I don't understand what is the cache of row
array for. Why not to process pixels straight from scanline.

> 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?

Don't bother with CPLMalloc, let the compiler to clean after you.

typedef std::vector<float> raster_data_t;

raster_data_t scanline(xSize);
bands[ i ]->RasterIO( GF_Read, 0, row, xSize, 1, &scanline[0], xSize,
                      1, GDT_Float32, 0, 0 );

Best regards,
-- 
Mateusz Loskot
http://mateusz.loskot.net



More information about the gdal-dev mailing list