[Gdal-dev] How to access 2-D arrays returned by GDALRasterIO?

Frank Warmerdam warmerdam at pobox.com
Mon Jul 10 15:48:55 EDT 2006


Bruce Raup wrote:
> Hi all,
> 
> I'm building a simple application using GDAL for image I/O.  I want to
> extract different subsets from an image band, and for each subset, do
> math with each of the elements within.  Ideally I would like to do
> this in the natural syntax subset[i][j], rather than subset[j*ncol+i].
> 
> I am using GDALRasterIO to retrieve the subsets, putting the result in
> dynamically allocated memory, obtained using CPLMalloc.  I'm using
> CPLMalloc because it's in the GDAL examples, but I really don't know
> what its advantages are, as I haven't found docs on it.  So my
> questions are:
> 
> 1.  Where is the documentation on CPLMalloc and related functions?

Bruce,

The docs can be found at:

   http://iweb.gdal.org/cpl__conv_8h.html#b2d43d9ea4c2c7ad7d5c9b218a9dee5a

Generally speaking CPLMalloc() is just a small cover over malloc() that
issues a fatal error if an out of memory condition occurs.  You can also
use VSIMalloc() which just does a normal malloc() and returns NULL on
failure.   Using CPLMalloc() and VSIMalloc() also has the advantage that
you know the allocation occurs in the GDAL heap, rather than the
application or some other heap (on win32).

Generally speaking you can find CPL function documentation by "Files"
tab of the GDAL web site, and selecting the include file the functions
are declared in.  Not all CPL functions are properly documented though.

> 2.  Do CPLMalloc or GDAL provide for ease of access into image data
> using 2-D indexing?

CPLMalloc() does not really help with this.

> 3.  Or should I be using the list-of-pointers method for 2-D indexing
> as described in, say, Numerical Recipes?

That is one functional approach.

To be honest, I always access 2D arrays in C/C++ using the [i+j*xsize]
syntax, so I'm not sure what "real 2D" alternatives exist.

Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | President OSGF, http://osgeo.org




More information about the Gdal-dev mailing list