Questions about black-level and white-levels in colordata

Hi,
I had posted a question about this yesterday, but I dont think that post was well-researched (my bad!). I am reposting after doing some digging around myself. Hopefully these questions are a little more pointed.

The comments that I am specifying below is after calling unpack(). I am trying to use libraw to read the CFA data and all metadata to build my own RAW processing pipeline. Hence, I am using libraw primarily to read the data.

1. Is the correct way to determine the black-level per channel to do black + cblack as one defines the baseline and cblack contains the corrections?

2. I noticed that Fuji X-trans and some DNG files define black-level over a region. Is this what is captured in the cblack[4] and cblack[5]? For a Fuji X-trans file in my test files, cblack[4] = cblack[5] = 6. The values from cblack[6] -> cblack[41] were 256. The black property was 0. Does it mean the black-level to use is actually 256? Similar for some DNG files, the values of cblack[4] and cblack[5] were 2.

3. Is it fair to say that cblack is meant to handle the two ways black-level is specified for different RAW files? When cblack[4] and cblack[5] is 0, is it fair to assume that cblack[0] -> cblack[3] values are to be taken as the per channel correction. And when cblack[4] and cblack[5] is non-zero, then you must use a repeated pattern of this region starting at the top-left of the visible area? Also what is the order or cblack[0...3] i.e. are they R G1 B G2 order or the Bayer pattern order?

4. Is the value of maximum the white-level or saturation? This seems to hold for DNG files and the WhiteLevel tag. Is maximum or linear_max more reliable? I was not clear what the comment about black-level not being subtracted when linear_max is not available meant.

Regards,
Dinesh

Forums: 

Yes, black/cblack reflects

Yes, black/cblack reflects different black level specifications in RAW metadata
black - single black level
cblack[0-3] per channel BL in channel numbering order (channel numbers are returned by COLOR(..))
cblack[4-5] defines pattern BL (e.g. 6x6 for X-Trans)

As a rule: black is base level and cblack is correction, so if black is non-zero and cblack[.. is non-zero (for given channel or row/col), resulting BL for pixel/channel is a sum of black + cblack[channel] + cblack[pattern]. In practice all three are rarely present.

white is either format maximum (defined by bits per pixel), or maximum value defined by metadata (DNG whitelevel).
linear_max is 'specular white' read from metadata (if present in file)

-- Alex Tutubalin

Thanks!

Alex,
Thanks for this clarification. So it looks like my best bet would be to call raw2image() and subtract_black() to leverage libraw to do the black-level correction for me. I still would like to return the CFA data as a rows x columns matrix instead of the output of raw2image. I guess I have to add some custom logic to do the copying.

Also, it appears that raw2image() only returns the visible portion of the image. It typically has only 1 out of the 4 channels as non-zero. I am assuming this does not hold true for Fovean sensors or DNG files that are created from Sigma cameras?

Can I use the COLOR function for non-Bayer sensors? I have a DNG file that appears to be created from a Sigma camera. The output of COLOR(0, 0), COLOR(0, 1) returned 6.

Dinesh

COLOR() for non-bayer(x-trans

COLOR() for non-bayer(x-trans) data is useless, check filters too before use.

-- Alex Tutubalin

Followup, 6 is intended value

Followup, 6 is intended value for non-bayer:

int COLOR(int row, int col)
  {
    if (!imgdata.idata.filters)
      return 6; /* Special value 0+1+2+3 */
  ...

-- Alex Tutubalin

A quick follow up

Alex,
For the black-levels that are specified in cblack[0]->cblack[3], do those also need to be applied starting at the visible region. I am assuming this is the case because the subtract_black_internal operates on imgdata.image which only operates on the visible image.

Also, for the white-levels, if linear_max is all zeros, then do I just assume maximum applies to all the channels.

Also, if both maximum and linear_max value is present, which value should I use?

Regards,
Dinesh

I do not know what you want

I do not know what you want to achieve, so I could not answer (both) your questions.

-- Alex Tutubalin

Clarifying

Alex,
I am trying to apply my own black-level subtraction and image normalization. So, I just wanted to clarify on what image range I should apply the black-level subtraction. Also, I am not sure if I should use maximum or linear_max for normalization.

Dinesh

Dinesh

I don’t know what exactly you

I don’t know what exactly you are doing and what you mean by normalization.
Therefore, do as you think is right.

-- Alex Tutubalin

Apologies for not being clear earlier

Alex,
I am trying to implement my own version of subtract_black. Following which I am trying to rescale the image to the range (0,1). For this, I need to know the black-level and white-level. Given, there are two properties that contain white-level information, I was curious to know which one to use if both maximum and linear_max are both populated.

Dinesh

As mentioned above:

As mentioned above:
> linear_max is 'specular white' read from metadata (if present in file)

Hope this helps.

-- Alex Tutubalin