using example, image data not understood

Reading a CR2 canon file from a Canon 6D MK1, known good. Using the example code on the "" page; the max value out of any RGBG sample is about 5600. Scaling the data up ((data/5600.0)*65535) to a full 16bit sample results in what looks like fairly even tempered noise, not anything like the image data that is actually in the file.

I want to read out the RGB values of the image, and this doesn't seem to do that:


Followed by a loop reading the data, as follows, where v step through the image size as shown in the example:


The image dimensions are right, and I am not getting any errors (API call results are 0.)

It is presently unclear to me just what I am supposed to do, other than the example, to read back valid 16 bit/channel RGB image data.


1st: raw2image does not

1st: raw2image does not subtract black level, most likely you need to do that (by calling LibRaw::subtract_black() after raw2image).

Also, you may access raw data via imgdata.rawdata.raw_image (this is flat array with unaltered raw values; pixels outside of active area are included too).

max value of 5600 (before black subtracted) is about ~2 stops lower than this camera data range, so your image is, most likely, heavy underexposed.

-- Alex Tutubalin @LibRaw LLC


I found an error with my code; I can now read the data. Thank you for your prompt reply.

However, it appears to be Bayer sensor mosaic data; does libraw do conversion to adjusted, de-mosaiced 16-bit image data? Or is that something I am expected to do from the bayer mosaic data?

Here's part of the image as displayed in Adobe Lightroom:

Here's what I'm reading from more or less the same region:

I have not done a black level subtraction, inasmuch as that would dim the image even more.

LibRaw::dcraw_process does

LibRaw::dcraw_process does full postprocessing pipeline (white balance, demosaic, color conversion...)

-- Alex Tutubalin @LibRaw LLC


Good to know. Is there an example somewhere that shows how to do that in a step by step manner?

What I would like is to be able to load an image from the raw file as the camera generally would have the user see it using the user's white balance and so on, exposed as they see it on the camera's preview. Ideally, it would be ready to view and do further adjustments to if desired.

This sequence:


resulted in a deep red image. Not correct at all.

If I load it with just this...


...I can, with a serious gaussian blur and a window and level adjustment, turn it into something close to what it should be.

So I'm still missing something.

win win win


Here's a comparison between the two. I turned around and looked at the guitar, and the libraw result is truer to the actual colors of the instrument than the lightroom first-pass rendering:

Alex, I suggest you add these to your example on the API intro page:

// Convert from imgdata.rawdata to imgdata.image:
// ----------------------------------------------

Kudos. Fine work you've done. :)

Could you please convert this

Could you please convert this image to DNG and look at the value of the baseline exposure tag? LibRaw doesn't apply that correction of lightness.

Iliah Borg

conversion to DNG

How would you have me convert it? What tool, etc. I'm using OS X 10.12