Accessing Raw Bayer data


I just downloaded and compiled the libraw APIs. I was trying to use the APIs to access the Raw bayer stream from a canon CR2 file.

After instantiating a LibRaw object and calling its open_file(), unpack() and raw2image() functions, im able to print out pixel values using the imgdata.image[pixelindex][channelindex] data array. (I see a nice RGBG pattern coming out here)

However, if i just instantiate the libraw object, call open_file(), unpack() and then access pixel values from the .imgdata.rawdata.raw_image array, i see only 16384 (the max possible 16bit value) getting dumped out.

After raw2image(), does the imgdata.image array contain raw bayer data or does it undergo any processing? If it is infact the raw bayer, i was hoping to see the same values in the raw_image array (but as a single channel of pixel values) What am i missing here?


Code snippet :
LibRaw RawProcessor;
for(i = 0;i < 100; i++)
printf("%d ",(RawProcessor.imgdata.rawdata.raw_image[i]));
output : 16384 16384 16384 ....


1) The max possible 16-bit

1) The max possible 16-bit value is 65535
2) Many cameras, including Canons, contains 'masked pixels' around visible area. So, for first 100 values you query these service pixels.
Try this code:

#define R RawProcessor
int first_visible_pixel = R.imgdata.sizes.raw_width*R.imgdata_sizes.top_margin + R.imgdata.sizes.left_margin;
for(i=0; i< 100; i++)
  printf("%d ", R.imgdata.rawdata.raw_image(i+first_visible_pixel);

3) After raw2image the image[][] array contains values (only visible area!) for one component out of 4, three other pixel components are zero.

-- Alex Tutubalin

Thanks Alex. I see the

Thanks Alex. I see the correct pixel values now!

I was not aware of these masked pixels ... are they provided for black-level calibration?


Yes, in Canon data these

Yes, in Canon data these pixels used for black level calibration.
On other cameras (e.g. Nikons) these pixels may be used for banding noise elimination

-- Alex Tutubalin

What do you mean for one

What do you mean for one component out of 4? I actually noticed this, but I don't really understand what's going on.

Bayer image contains only one

Bayer image contains only one color component per pixel.
image[][4] array has 4 components per pixel (R,G,B,G2 or CMYG or RGBE).

raw2image() copies bayer data into image array

-- Alex Tutubalin