[gdal-dev] GDAL Byte Swapping?

Frank Warmerdam warmerdam at pobox.com
Thu Jun 14 12:41:56 PDT 2012


On Thu, Jun 14, 2012 at 12:26 PM, Trist, Brenda R.
<BRENDA.R.TRIST at saic.com> wrote:
> I am new to GDAL.  I recently downloaded GDAL have successfully built
> and linked to the source.  The program I am writing reads in a
> GeoTiff DEM (32bit format) and writes the dem data portion out to a
> ntif file (using NITRO NITF routines from sourceforge).
> I then ran the command od -c on the tif file I am reading and on the
> ntif I just created to compare the DEM data area.  I noticed that
> while the data is there that the bytes appear to have been swapped.
> I am assuming that I don't want it to do that.  I was wondering if
> anyone has encountered this same thing and if there is something that
> I should be doing differently?  I researched the bug list and all
> mentions of byteswapping pretty much have been closed due to being
> fixed.  So I am assuming that I must be doing something wrong at my end.

Brenda,

I'm guessing you have written your own program
that uses GDAL to read the GeoTIFF and Nitro
to write the NITF but not using the GDAL NITF driver.
Is that right?

Assuming the answer is yes, then the only question is
what does GDAL do when reading from a GeoTIFF
into a memory buffer for non-8bit data.  The answer is
that the GeoTIFF driver analyses the byte ordering of
the GeoTIFF which will start with II if it is little endian
(Intel order) or MM if it is big endian (ie. motorola order).

The GeoTIFF driver will then byte swap the image
data if needed to bring it into the native byte order of
the machine on which the program is run.

This is well established machinery so I have no reason
to believe it would be broken.  You might want to check
if NITRO expects it's image pixel input in machine local
order or some other order.

Peeking into the GDAL NITF driver it appears that
16bit data in NITF is normally in big endian format.  So
if NITRO expects you to pass in the image buffer in
big endian order and you are running on Intel you may
need to byte swap things in your program.

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 Software Developer


More information about the gdal-dev mailing list