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

pw p.willis at telus.net
Wed Jan 5 09:57:14 PST 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


Thanks Rainer,

As I wrote, it's just an example.
Your points regarding the 3 byte space are valid.
I will modify the code to work with 24 bits instead
of 32.

I think I started off the code as RGBA, which is why
I am using the 32 bit value in this code.

Thanks again for the reminders,

Peter





More information about the Geotiff mailing list