Hello,
First of all, thanks for your great work.
I have an issue with reading monochromatic DNG image. The result is pretty weird. I can't figure out if there are problems with the source DNG image itself, or with LibRaw. Maybe I'm using it wrong, but usually DNG images are loaded pretty well. But not this one.
In the attachement there is comparison image just to take a quick look at the problem.
- Here is the source DNG file
- Here is the result of processing source DNG with LibRaw
- Here is the result of processing source DNG with Adobe DNG SDK (the expected result)
- Here is the result of processing source DNG with RawTherapee (slightly different, but still close to correct)
I'm using LibRaw 0.17.3, but I've tried latest LibRaw 0.20 beta, with the same results.
My LibRaw-related code (slightly simplified):
LibRaw rawProcessor; int errCode = 0; errCode = rawProcessor.open_file(fileName.toUtf8().data()); if (errCode != LIBRAW_SUCCESS) { setLastError(QString("Can not open raw image: %1").arg(QString(libraw_strerror(errCode)))); return nullptr; } rawProcessor.imgdata.params.use_camera_wb = applyWhiteBalance; rawProcessor.imgdata.params.use_auto_wb = 0; rawProcessor.imgdata.params.no_auto_bright = 1; rawProcessor.imgdata.params.no_interpolation = 0; rawProcessor.imgdata.params.use_camera_matrix = applyColorMatrix; rawProcessor.imgdata.params.output_color = 0; // Raw rawProcessor.imgdata.params.output_bps = 16; // 16 bits per sample rawProcessor.imgdata.params.user_qual = 12; // AHD interpolation if ((errCode = rawProcessor.unpack()) != LIBRAW_SUCCESS) { setLastError(QString("Can not unpack raw image: %1").arg(QString(libraw_strerror(errCode)))); return nullptr; } errCode = rawProcessor.dcraw_process(); if (errCode != LIBRAW_SUCCESS) { qDebug() << "LibRaw dcraw error:" << libraw_strerror(errCode); if (LIBRAW_FATAL_ERROR(errCode)) { setLastError(QString("Can not process raw image: %1").arg(QString(libraw_strerror(errCode)))); return nullptr; } } libraw_processed_image_t *rawImage = rawProcessor.dcraw_make_mem_image(&errCode); if (!rawImage) { setLastError(QString("Can not unpack raw image: %1").arg(QString(libraw_strerror(errCode)))); return nullptr; } if (rawImage->type != LIBRAW_IMAGE_BITMAP) { setLastError("LibRaw unpacked image into JPEG instead of bitmap, this is not supported yet."); LibRaw::dcraw_clear_mem(rawImage); return nullptr; } if (rawImage->bits == 8) { // ... No sense for this case ... } else { // rawImage->bits == 16 RgbBufferFloat *realBuf = new RgbBufferFloat(QSize(rawImage->width, rawImage->height)); if (realBuf->data() == nullptr) { setLastError("Out of memory"); return nullptr; } float *imgPtr = realBuf->data(); unsigned short *rawPtr = reinterpret_cast<unsigned short *>(rawImage->data); for (unsigned i = 0; i < rawImage->data_size; i += 2) { *(imgPtr++) = *(rawPtr++) / 65535.0f; // Convert into floating point values normalized to 0..1 } return realBuf; }
Attachment | Size |
---|---|
Comparison: Adobe DNG SDK result v.s. LibRaw result | 265.59 KB |
Thank you for the sample and
Thank you for the sample and for detailed explanation.
Tested with current LibRaw 0.20(beta):
dcraw_emu produces correct pgm file
dcraw_emu -T makes correct tiff file.
So, LibRaw::dcraw_process() and all previous steps are OK.
mem_image_sample.c fails to produce correct results, the problem not in LibRaw::dcraw_make_mem_image() (it is also correct, see below), but in sample source code:
1) write_ppm() do not handle img->colors != 3 case and just returns. This is expected, but need to be fixed.
2) write_jpeg() do not check for colors count, but assumes that 3-color data passed that is wrong.
Quick fixes:
A. replace
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
with
cinfo.in_color_space = img->colors==3?JCS_RGB:JCS_GRAYSCALE; /* colorspace of input image */
B. replace:
row_stride = img->width * 3; /* JSAMPLEs per row in image_buffer */
with:
row_stride = img->width * img->colors; /* JSAMPLEs per row in image_buffer */
Fixed version will be uploaded to github soon (likely tomorrow, we want to fix write_ppm sample code too).
Here are processing results (pgm file from dcraw_emu and .jpg from fixed mem_image_sample): https://www.dropbox.com/sh/a6aksx5opyzeeyv/AACG3f9GCZ703UksKjc5H5Rta?dl=0
-- Alex Tutubalin @LibRaw LLC
Thanks for your fast reply.
Thanks for your fast reply.
I've found what I'm doing wrong. I always assume that resulting image is 3-channel when copying it into my internal RGB buffer for further processing, but this one is obviously 1-channel. Very stupid and obvious error, and I've found it in half of an hour after submitting my first post. Now I check value of
rawImage->colors
and for 1-channel images I copy value of first channel into the other two. I'll do some performance tests, maybe your solution with setting proper color space will be faster.Interesting that digiKam gives similar triple-image result when processing my file. digiKam uses LibRaw for the processing of DNG files, and it's behaviour led my thoughts wrong way. Possibly I should report an issue into digiKam bugtracker.
Anyway, thanks again! The problem is solved. Should I somehow close this forum thread?
Exactly same problem present
Exactly same problem present in our mem_image_sample: channel count assumed to be also 3 :)
We usually do not close threads (unless lot of spam come to specific thread).
-- Alex Tutubalin @LibRaw LLC
mem_image_sample problem
mem_image_sample problem fixed in this patch: https://github.com/LibRaw/LibRaw/commit/253f7ac76d03163497019302e3d1f967...
-- Alex Tutubalin @LibRaw LLC