[Geotiff] Short RGB Geotiff Write up with sample C source code

Rainer Wiesenfarth Rainer.Wiesenfarth at inpho.de
Wed Jan 5 11:52:37 EST 2005


pw wrote:
> (...)
> I have posted a document that contains example code for:
> 
> a.) Writing RGB data to Tif files
> (...)

Hi Peter,

sorry to have to say this, but the RGB data handling code is buggy. The 
RGB colour generation works only on Intel architecture as it depends on 
little endian byte order. Furthermore, the buffer fill also only works 
for Intel (where the used three bytes "come first" in the uint32) and it 
will fail if the buffer is filled from end to beginning.

There is at least one additional problem with MakeRGBColour: you do not 
do any range checks, so a red value of 256 results in a almost dark (but 
slightly green) pixel (on Intel platforms only).

Unfortunately, there is no really good solution for this that offers 
MakeRGBColour() and an option to fill a buffer using it. That is mainly 
due to the fact that an uint32 has four bytes, while a RGB triple only 
has three.

A less buggy, but not very fast function is this one:

<CODE>
/* set a single pixel in (the RGB) buffer. buffer is sized
    width by height pixels. The pixel position is given by
    row and col, the colour by red, green, and blue */
void setPixel (unsigned char *buffer, uint32 width, uint32 height
     , uint32 row, uint32 col, uint32 red, uint32 green, uint32 blue)
{
     unsigned char *p;

     if ((row < height) && (col < width))
     {
         p = buffer + 3 * (col + width * row);
         *p++ = (unsigned char) (red & 0xff);
         *p++ = (unsigned char) (green & 0xff);
         *p = (unsigned char) (blue & 0xff);
     }
}
</CODE>

Best Regards / Mit freundlichen Grüßen
Rainer Wiesenfarth

-- 
INPHO GmbH   *   Smaragdweg 1   *   70174 Stuttgart   *   Germany
phone: +49 711 2288 10              *  fax: +49 711 2288 111
mailto:Rainer.Wiesenfarth at inpho.de  *  www.inpho.de
Leader in Photogrammetry and Digital Surface Modelling
You will meet INPHO at www.inpho.de/events.htm



More information about the Geotiff mailing list