[sane-devel] Unknown tables

Bertrik Sikken bertrik@zonnet.nl
Mon, 13 Sep 2004 20:29:47 +0200

JKD wrote:
> El Mon, 13 de Sep de 2004, a las 08:59:16AM +0000, gerard klaver dijo:
>>On Sun, 2004-09-12 at 15:21, JKD wrote:
>>>Debugging the windows library that manages my scanner I see that the
>>>first thing the driver does is create six tables with unknown purposes
>>>at the moment. Theese tables are created in the following way.
>>>1 - Loop with 3 iterations. I think one iteration per colour (not sure)
>>>2 - In each iteration, 2 tables are created. All tables have the same
>>>size (0xff0 bytes = 4080.) and contains data of two bytes size (0x7f8 words = 2040.)
>>>3 - The first table in each iteration is filled with zeroes.
>>>4 - The second table in each iteration is filled with 0x7f8 words with value 0x4000 (16384.)
>>>It seems to be minimum values and maximum values for something but I'm
>>>not sure of that. Could anyone have any idea about the purpose of theese
>>>values? Gain values or something else ?. Gamma tables have only 255
>>>posible values and I've located them.
>>>Sincerelly, JKD
>>Could be calibration tables, but you didn't mention the backend and
>>scanner you are using?
>>Gerard Klaver
> Thank you for your answers. The library that I'm debugging belongs to
> hp3970 scanner. Well, I've debugged a bit more. Those buffers seem to
> be calibration tables created when program starts (I know that because
> I found a function in which programmer wrote a debug string
> "FreeCalBuffer" to free those tables). However library use them depending
> on a variable. If this variable contains 0x00 then, in each scanning
> process, the same tables are created dinamically with a variable size
> (scan width * 2) filled with the same values described on a previous
> post.

The fact that the size of the table depends on the width of the scan
makes it almost certain that it is indeed a CCD calibration table.

> Despite of having experience on reverse engineering I don't know very
> much about how those tables work and how gain values are used. If anyone
> is able to help me, with information about this topic, or some reference/s
> via the internet, that could be great to work a bit faster without messing
> about discovering how those things work.

I can tell you a little about how the niash chip scanners work:

After the analog frontend has digitised the CCD signal, the signal
may contain an offset. This means that a scanned pure black pixel is
not exactly represented as RGB (0,0,0). Likewise a scanned pure white
pixel may not exactly correspond to RGB (4095,4095,4095).
(I am assuming a 12-bit signal here.)
To correct for this, the scanner contains a digital subtractor and a
digital multiplier. The subtractor and multiplier are fed with values
from a table (one value for each horizontal CCD pixel and for each
color component).
The subtractor first removes the offset and thereby corrects for the
non-zero black level, the multiplier then multiplies the resulting
signal by some number from the table such that white is mapped to

Likely your scanner contains a black and white strip on the inside, 
somewhere near the top of the scanning glass. During calibration
this strip is scanned with a neutral offset/gain setting
(offset = 0, gain = 1.0). Then the per-pixel offset value is simply the
scanned black level of each pixel. The gain value can be calculated
by looking at the difference between the scanned white and scanned
black value and then calculating the gain value that makes the
multiplied difference equal to 4095. This is also done for each pixel.

At the final step, the signal is sent through a gamma table that
converts the 12-bit RGB into 8-bit.

BTW, both you and Mbosowo I Sampson are working on this scanner.
Are you working together?
Have you been able to figure out the USB protocol yet?
Do you have any logs that you want someone to look at?

All the best,