I tried to find some topic that already exists, and describes this problem, but I did not find one. Therefore, I decided to create this topic to seek help. I have a CR2 raw file, and I just want to get the raw pixel values, without "demosaic". Here is my source code so far:
int main() { char* image_path = "../../assets/example_images/IMG_8117.CR2"; libraw_data_t *libraw = libraw_init(0); libraw->rawparams.use_rawspeed = 1; int open_status = libraw_open_file(libraw, image_path); if (open_status != LIBRAW_SUCCESS) { printf("Failed to open\n"); exit(1); } int unpack_status = libraw_unpack(libraw); if (unpack_status != LIBRAW_SUCCESS) { printf("Failed to unpack\n"); exit(1); } int rawimage_status = libraw_raw2image(libraw); if (rawimage_status != LIBRAW_SUCCESS) { printf("Failed to rawimage\n"); exit(1); } int i = 0; int j = 0; ushort width = libraw->sizes.width; ushort height = libraw->sizes.height; for(; i < height; i++) { for(; j < width; j++) { int color = libraw_COLOR(libraw, i, j); char cname = libraw->idata.cdesc[color]; ushort v1 = libraw->image[(i * width) + j][0]; ushort v2 = libraw->image[(i * width) + j][1]; ushort v3 = libraw->image[(i * width) + j][2]; ushort v4 = libraw->image[(i * width) + j][3]; printf("Color: %c, A=%d B=%d C=%d D=%d\n", cname, v1, v2, v3, v4); } } printf("Width: %d | Height: %d\n", width, height); printf("Size: %d | Index: %d\n", total_size, i); return 0; }
I can execute libraw_init
, libraw_unpack
and libraw_raw2image
successfully. When I look at libraw->idata.colors
I get 3, and when I look at libraw->idata.cdesc
I get "RGBG".
The problem is that, inside my doubly-nested for loop, the libraw_COLOR
function return only 0 (RED) and 1 (GREEN) for all pixels in libraw->image
. I would expect to get the value 2 (which represents BLUE) in some of the pixels, to represent the blue channel of the image. Why I'm getting only RED and GREEN values in the pixels? Is my for loop wrong?
1st:It is quite difficult to
1st:It is quite difficult to discuss the results of code running on a file without having that file in hand. Please share sample file you're testing with.
2nd: generally, libraw_init and libraw_unpack is enough. libraw_raw2image copies pixels from imgdata.rawdata....array to the imgdata.image array.
rawdata is described here: https://www.libraw.org/docs/API-datastruct-eng.html#libraw_rawdata_t
-- Alex Tutubalin @LibRaw LLC
Sorry, my bad. To make the
Sorry, my bad. To make the files easily available, I just published the code and the image file inside a GitHub repository: https://github.com/pedropark99/libraw-example
Now, to answer your 2nd point, I understand that libraw_unpack should be enough. However, for some reason that I also don't understand, when I try to access the
libraw->rawdata.raw_image
array, or literally any of the arrays that are present inside thelibraw->rawdata
structure, I get a "Cannot access memory at address" error message. So apparently, they are all "not-valid pointers", even though they are not-null pointers. You can see this by looking at the screenshot that I took from a GDB session: https://github.com/pedropark99/libraw-example/blob/main/screenshot.pngThank you anyway for your attention!
Please make sure
Please make sure
1) you use same LibRaw version (.h files) for your code and for linking
2) you use same compilation options (esp. structure aligning) in libraw build and in your code build.
The symptoms you describe seem to be either 1) different versions of header files 2) or different calculated offsets due to different structure fields alignment.
-- Alex Tutubalin @LibRaw LLC
Ok, thank you very much!
Ok, thank you very much!
For anyone that reaches this
For anyone that reaches this topic at some point, and are facing the exact same problem, here is what seems to be the fix. At least, it seems to fixed the problem for me. As Alex described, the problem seems to be in how I built the Libraw library in my machine. I just made a brand new clone of the Libraw repo, and I built the library from source again, following the steps described in the INSTALL file: https://github.com/LibRaw/LibRaw/blob/master/INSTALL
Then, I payed close attention in the compiler flags that were used in the Makefile that was generated by automake. Then, I changed the build command in my Makefile to use the same compiler flags that were used in the Makefile that was generated by the automake. As a result, I got this Makefile:
There are still some issues in the results, but it is already much better with this change.