2009.09.10 13:31 "[Tiff] Manually convert 16-bit grayscale image data to 8-bit grayscale in Borland c++", by Robert Zermeno

2009.09.10 13:31 "[Tiff] Manually convert 16-bit grayscale image data to 8-bit grayscale in Borland c++", by Robert Zermeno

Hi everyone,

I have been using this website and mail archieve exclusively finding answers to the questions I had recently.  Thanks a bunch for your knowledge.  However, I am stuck with this issue i cannot find and answer to because everyone is using the libtiff library to perform the action. 

I am creating an application to manually read in the bits from the file into memory to be displayed in Borland (I have to read certain segment of a file given an offset and extract an (x,y) size sub-image of an image.  What I mean by manually, is I parse through the header and footer myself using fseek() and fread(), and use the stripOffset and StripByteCount to perform a fread() for each strip of image data.

How on earth may I be able to convert my 16-bit grayscale image data to 8-bit?  I have been able to take 2 bytes to a short value, then truncate it to a single byte like so:

 unsigned short value16 = *TmpBits++;
                       value16 = value16 + (*TmpBits++ << 8)
pBits[curPix++] = (value16/257. + .5)  //convert value to double and round up.

where pBits is my BYTE array to hold image data and TmpBits is another BYTE array of size StripByteCount.

When I dislpay my data in Borland through a TImage object, I will recieve my image (meaning, you can make out my object I am looking at, but the gray values for each pixel is way off.  Sometimes too much white and some pixels are correct for black)  but the gray coloration is way off.  I have to do this because in Borland, to create my HBITMAP from a bmp object it must be in 1,4,8 or 24 bpp image.  I then pass this created HBITMAP to an TBitmap object and associate this Bitmap to an TImage for display purposes.

It works well with 8-bit images, but I cannot get the 16-bit data to work.  Here is my tag information to help out what my issue is:

NewSubfileType                   254        LONG          1         0

ImageWidth                        256        LONG          1         14592

ImageLength                       257        LONG          1         6940

BitsPerSample                    258        SHORT         1         16

Compression                      259        SHORT         1         1

PhotometricInterpretation     262        SHORT         1         1

ImageDescription                270        ASCII         13        Space Imaging

StripOffsets                        273        LONG          6940      56040 85224 114408 143592...

SamplesPerPixel                277        SHORT         1         1

RowsPerStrip                      278        SHORT         1         1

StripByteCounts                 279        LONG          6940      29184 29184 29184 29184...

MinSampleValue                280        SHORT         1         0

MaxSampleValue               281        SHORT         1