I’m reading a DNG file on Android with libraw in C++ which works pretty fine.
I want to pass the pixel data to Cimg for manipulation purposes (rotating etc.).
The problem is that the image in Cimg looks weird.
I think that problem is that the original pixel data from libraw are stored in a different order than in Cimg. In libraw (due to the DNG file) its RGBGRGBGRGBG while Cimg stores the pixel data in the way RRRRRR….GGGGGGG….BBBBBB.
Does anyone know how to convert from RGBGRGBGRGBG to RRRRRR….GGGGGGG….BBBBBB?
Here are both images, on the left is dng file and on the right is tiff file exported from cimg (both imported into Photoshop).
Here is my code:
LibRaw rawProcessor;
rawProcessor.open_file("file.dng");
rawProcessor.unpack();
int ret2=rawProcessor.dcraw_process();
libraw_processed_image_t* image = rawProcessor.dcraw_make_mem_image(&ret2);
float xres=72.0;
CImg<unsigned char> cimgOriginal(image->data,image->width,image->height, true);
cimgOriginal.shift(10*(-1), 10*(-1),0,0,0);
cimgOriginal.rotate(360.0-5, (float) 1200, (float) 800,0,0);
cimgOriginal.save_tiff("file.tiff", 0, &xres,0,false);
Thanks!
Mike
2
Answers
Thank you for your help, Mark! In the meantime I've figured out that Cimg can do it from packed RGB to planar RGB - with a method called permute_axes(). The documentation about permute_axes() is very short but here is a good explanation:
https://www.codefull.org/2014/11/cimg-does-not-store-pixels-in-the-interleaved-format/
With the following code I was able to get the image in Cimg. The attached screenshots shows DNG on the left and the image exported to tiff from Cimg.The image with leaves (taken from here: https://www.kenrockwell.com/leica/m9/sample-photos-3.htm) is a 8 bit DNG. The second example (with mouse and pen) shows my 16 bit DNG from Pixel 5. The code works also with 16 bit DNG files:
libraw
stores pixels in "packed" (a.k.a. "interleaved") format. So a 3×2 image will be stored like this:CImg
, on the other hand, stores pixels in "planar" format, which bunches all the red pixels together in a plane, followed by all the green, then all the blue ones. So that same 3×2 image will be stored like this:I haven’t done any C++ for years, so my code will be shockingly awful quality, but it does work at least:
Compiled on Raspberry Pi OS with:
I tested it by downloading the first DNG sample picture from Ken Rockwell, here.