How to use LibRaw for writing one's own demosaicking algorithm

Dear All, thank you very much for this really nice piece of opensource software !

I would like to write an inverse problem solver, that take as input multiple RAW files (taken from the same image), and outputs 3 RGB files for the image.
The way demosaicking is performed is up to me, but I would like to be able to rely on LibRaw in order to consistently know the mapping between each pixel coordinate (x_i, y_i) and its color channels (R,V or B), wether the input raw comes from cannon/nikon/sony/pentax,... .

I would like to use the C++ interface of LibRaw, but unfortunately, I cannot find my usecase in the documentation.

Can someone help me, or point me to some examples of getting a simple way to map coordinates to color channel ?
It would be even better if I was able to derive a C++ mapping code that can be run on GPU, where I would need a switch like this:

switch (bayer_pattern) {
case(pattern::Bayer0):
//we know that x_i+y_i%2 = 0 stands for green channel
//else we know that yi%2 = stands for red channel
//else we know that xi%2 = stands for blue channel
break
case(pattern::Bayer1):
//other pattern known at compile time[...]
break;
}

Thank you very much in advance for your help.

Forums: 

Current libraw snapshot can

Current libraw snapshot can run user-specified demosaic routine, just set interpolate_bayer pointer to point to your function.
Look into other demosaic functions code (e.g. lin_interpolate()) to see how to loop through all pixels.

To get bayer pattern, use imgdata.idata.filters variable, or COLOR(r,c) call.

-- Alex Tutubalin @LibRaw LLC

Thank you very much for your

Thank you very much for your fast response, looking at the data structure documentation for imgdata.idata.filters, I see this:

unsigned filters;
Bit mask describing the order of color pixels in the matrix (0 for full-color images). 32 bits of this field describe 16 pixels (8 rows with two pixels in each, from left to right and from top to bottom). Each two bits have values 0 to 3, which correspond to four possible colors. Convenient work with this field is ensured by the COLOR(row,column) function, which returns the number of the active color for a given pixel.
values less than 1000 are reserved as special cases:

1 - Leaf Catchlight with 16x16 bayer matrix;
9 - Fuji X-Trans (6x6 matrix)
3..8 and 10..999 - are unused.

Some points are not very clear to me:
-32 bits for 16 pixels, 2bits per pixels to code color, but what is the mapping ? (0->red, 1->green, 2->blue)
-What does this 1-->999 code means ?

I will take a look at the source code tomorrow

0...999 range is reserved for

0...999 range is reserved for future custom layouts, like Fuji 6x6.

32 bits describes 16 pixels, 4x4 block. For all today cameras it is repeated 2x2 blocks.
00 - red, 01 - green, 10 - blue, 11 - green2 channels

(just use LibRaw::COLOR(row,col) call to get layout)

-- Alex Tutubalin @LibRaw LLC

Thank you

Never had the chance to say thank you for your help.
After talking to a friend, I recently regained interest in using LibRaw for personal R&D purpose. And this is exactly what I needed.

I am going to give it a try soon with some CR2 files from my rather old canon 350D.