[gdal-dev] Reading/Interpreting multi band Tiffs

Tom Kazimiers 2voodoo at gmx.de
Tue Sep 23 13:10:00 EDT 2008

Frank Warmerdam schrieb:
> Tom Kazimiers wrote:
>> Hi there,
>> up to now I only faced Tiff images with palette information. It was
>> relatively easy to read and display everything - I use RasterIO for the
>> one available band and the color table.
>> Now I want to load and display multi band Tiff images - namely the bands
>> red, green and blue. For what I have understood a pixels RGB value is
>> then the value from the red band at the pixels position as red value,
>> the green value from the green band and so on. So do I understand it
>> right that I have to make three RasterIO calls to get the color of one
>> pixel - one call for each band? Exists there maybe a more convenient way
>> like making one call and getting back the color?
> Tom,
> There is also a RasterIO() method on the Dataset which allows
> fetching all the bands in one request.  In C++ this is:
> http://www.gdal.org/classGDALDataset.html#e077c53268d2272eebed10b891a05743
> In C:
> http://www.gdal.org/gdal_8h.html#764cb93a266ada902b13c2bdad3f7037
> In Python you would use:
>     def ReadRaster(*args, **kwargs):
>         """
>         ReadRaster(self, int xoff, int yoff, int xsize, int ysize, int
> buf_len,
>             int buf_xsize=0, int buf_ysize=0, GDALDataType buf_type=0,
>             int band_list=0) -> CPLErr
>         """
>         return _gdal.Dataset_ReadRaster(*args, **kwargs)
> eg.
> rgb_buffer = dataset.ReadRaster(0,0,100,100,gdal.GDT_Byte,[1,2,3])
> to read a 3 band buffer for the region 1,2,3 in "Byte" format.
> Note that the C/C++ APIs read in band interleave format by default.
> So the buffer would be filled with all the red, followed by tall the
> green followed by all the blue.  If you want pixel interleaving
> you will need to carefully select appropriate nPixelSpace, nLineSpace
> and nBandSpace parameters.
> Best regards,

thank you very much - this was what I was looking for. Unfortunately I
need to write Data in the form Band3, Band2, Band1, Band3, Band2, Band1,
... (BGR)
Like you have said, the reading is sequential and it was not very hard
to get it working with a result of the form Band1, Band2, Ban3, Band1,
Band2, Band3, ... (RGB)
Is there a way to change the order of the bands so that I can use this
function (alternatively I could write a fast switch function, sure - but
it would be nicer to have it all in one function)? With three seperate
calls to the per-raster-band-RasterIO I can controle everything pretty
good, but it seems that the per-dataset-RasterIO is faster (maybe
illusion :P).


More information about the gdal-dev mailing list