[Gdal-dev] Writing bit interleaved data with GDALRasterBand::RasterIO

Frank Warmerdam warmerdam at pobox.com
Wed Jul 7 12:01:59 EDT 2004


Chapman, Martin wrote:
> All,
>  
> If I have raster data stored as bit interleaved (r,g,b,r,g,b,r,g,b...), 
> can I use the GDALRasterBand::RasterIO function to write this data to 
> disc?  I read that the nPixelSpace and nLineSpace parameters allow 
> reading into or writing from unusually organized buffers.  If this is 
> possible, could someone please provide me a brief example of how to use 
> these parameters with this type of data structure?

Martin,

Do you literally mean bit interleaved?  Or pixel interleaved?

I'll assume you mean pixel interleaved.   To write from a pixel interleaved
1 line buffer of 8bit data you might use:

poBandRed->RasterIO( GF_Write, 0, line, xsize, 1,
                      pabyRGB + 0, xsize, 1, GDT_Byte,
                      3, xsize * 3 );

poBandGreen->RasterIO( GF_Write, 0, line, xsize, 1,
                      pabyRGB + 1, xsize, 1, GDT_Byte,
                      3, xsize * 3 );
poBandBlue->RasterIO( GF_Write, 0, line, xsize, 1,
                      pabyRGB + 2, xsize, 1, GDT_Byte,
                      3, xsize * 3 );

Note that the buffer pointer you pass in needs to point to the beginning
of the data to be written (hence the +1 and +2 offsets on pabyRGB for green
and blue).  The nPixelSpace is the number of bytes from the beginning of one
pixel to the beginning of the next.  3 in this case since there are 3 bytes
of data for for each pixel written.  The linespacing is the offset from the
beginning of one line to the next, so xsize * 3 in this case.

I hope this helps. If your data is really "bit" interleaved you will need
to repack stuff before writing.

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    | Geospatial Programmer for Rent




More information about the Gdal-dev mailing list