Newsgroups: sci.image.processing
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!news.mathworks.com!news.alpha.net!uwm.edu!fnnews.fnal.gov!lakesis.fapesp.br!eu.ansp.br!ecsl
From: ecsl@ansp.br (Embrapa - Centro de Soja de Londrina)
Subject: Tiff 5.0 ColorMap allocation &Palettes
Message-ID: <D3qun8.5CE@lakesis.fapesp.br>
Sender: news@lakesis.fapesp.br
Organization: FAPESP
X-Newsreader: TIN [version 1.2 PL2]
Date: Thu, 9 Feb 1995 17:45:08 GMT
Lines: 86


(This message was originaly sent to C-L@indycms.bitnet)
Hello, people!

I'm trying to make a reader (MSDOS, TC++ 3.1) for TIFF 5.0 images, 
but I'm having some troubles.,

Well, to begin with, I downloaded from ftp oak.oakland.edu 
(directory /SimTel/msdos/tif) 
"an Aldus/Microsoft Technical Memorandum (8/8/1988)" that discusses
TIFF 5.0 file format. Pretty Good Stuff, BTW.

I learned how to follow the tags through the Image File Directory (IFD),
and I know how to display a black&white image (two colors) or a 
gray-scale image (256 shades of gray).

So, I know how to get SamplePerPixel, BitsPerSample, X & YResolution,
RowsPerStrip, StripOffsets, StripByteCounts, ImageLength, etc.

Right now, my problem is HOW TO ALLOCATE THE COLOR MAP for
an RGB image correctly?

According to the Memorandum, ColorMap data group the samples of the
3 primary cromaticities (Red, Green, Blue): First 256 values for the
red subcurve, then 256 values for green, then 256 values for blue.
Every sample has the length of a SHORT (16 bits), so this table is
256 * 2 bytes long. OK.

Now we have to gatter every "RGB triplet" to set up our 256 color Palette.

FIRST DOUBT: as I said, the original samples are 16bits and the VGA
palette samples are 8 bits. Then, to put those samples in my palette 
I must shift to the left 8 times (short_sample >>=8) to put the less
significant byte of short_sample into a byte. 
For example, if short_sample is 8000h, then the real sample is 80h.
IS THIS CORRECT?

If the above is correct, then putting every RGB triplet at its position
in my Palette_Array[256] and issuing the correct interrupt I can modify
the default palette. OK.

But the displayed image palette still has nothing to do with the original!
Well, here comes the PhotometricInterpretation field. Based on the Memorandum,
if a TIFF has PhotometricInterp = 2 or 3 it must provide a 
ColorResponseCurves, to adjust e refine the meaning of the RGB triplets
in the ColorMap. The Memo also says that if there's no ColorResponseCurves,
the reader program should assume a "gamma" of 2.2 for each of the primaries.
The Memo supplied this formula to apply gamma to each of the primaries:
BitsPersample = 8;
  ValuesPerSample = 1 << BitsPerSample;
  for (curve[0] = 0, i = 1; i < ValuesPerSample; i++)
       curve[i] = floor(pow(i / (ValuesPerSample -1.0), 2.2) * 65535.0 + .5);

I did it, but no progress. 
QUESTION: How is this gamma really applied to the primaries?


Finally, two more related questions:

1. 
The Memorandum talks about "White Point". 
It says that the "default is the SMPTE whitepoint, D65: x = 0.313, y = 0.329. 
Is this (D65) an Hex number? What do I do with these x and y values? 
(SMPTE is Society of Motion Picture and Television Engineers).

2.
Then, there is the PrimaryChromaticities:

"Default is the SMPTE primary color chromaticities:
        Red:    x = 0.635  y = 0.340
        Green:  x = 0.305  y = 0.595
        Blue:   x = 0.155  y = 0.070
 The ordering is red x, red y, green x, green y, blue x, blue y.",

says the Memorandum...

What do I do with these values? How to apply them?
----------------------------------------------------------------
Thanks to every one.
Andre Felipe Carvalho
National Soybean Research Center
ecsl@eu.ansp.br
andre@cnpso1.embrapa.anpr.br



