Need help for rendering lossy DNG

I’m trying to add support for lossy DNG in my quickraw library.

Now the JPEG tiles can be decoded(directly though a general JPEG decoder to RGB values) and the whole photo can be viewed.
But I cannot figure out how to correctly convert the colorspace of the decoded image.


[Thumbnail in the DNG]



[decode only]
It looks like the white balance has already be applied. (weird, the same dng can be opened in darktable with whitebalance disabled).


[decode → whitebalance apply]
This looks wrong, so the decoded image already contains the whitebalance fix I think.


[decode → CameraToXYZ(by using ColorMatrix2) → XYZtoSRGB]
This looks closer to the thumbnail.


Lossy DNG contains general YCbCr JPEG tiles. So I just put the raw tile bytes into a general JPEG decoder to get the [decode only] result.

The PhotometricInterpretation is 38492. According to the DNG spec doc, the colorspace of the pixel data should be the same color space as the raw image data. So I don’t know why the image appears like the [decode only] one.


sample.dng (6.2 MB)

A quick look shows that those lossy DNGs claim to be scene-referred/linear encoded. That might mean you’ll need also a log transform (plus possibly a tone curve) for your data.

But as you didn’t post the DNG file, that’s just a guess.

Thanks for the reply. I’ve appended the dng file in the main thread.
I can understand that usually a linear raw file needs a tone curve(or gamma correction) to “brighten” the image. But this decoded one seems to have already brightened.

I think you’re still expected to apply the color matrix and white balance to LinerRaw data (there are also other DCP lookup tables present, but I’d start w/ the basics).

Also, this sample has:

OpcodeList2                     : MapPolynomial, MapPolynomial, MapPolynomial

that needs to be analyzed (and applied straight after decoding the raw data and conversion to 0-1.0 range).

1 Like

But the decoded image is not green enough to apply white balance adjustment.

After applying the white balance value in the dng file: 0.43686 1 0.468864:


[decode → whitebalance adjust]

I got the impression that you are using a program you wrote. In that case, shouldn’t you know what is done with the image? For instance, my understanding was that demosaicing needs at least an approximate white balance applied to function well. Then again, does this type of image need demosaicing?

No, lossy DNG contains general YCbCr JPEG tiles, so there is no demosaicing part in processing. When you put the raw tile bytes into a general JPEG decoder, it’ll just output an uint8 array data contains RGB values. The image is the [decode only] in the first post.

I did say you need to deal with the OpcodeList2 first. Please read the DNG spec as many times and as long as needed.

Re the white balance tag, one applies the inverse of the coeffs eventually.

1 Like

Sorry, I misunderstand the usage of OpcodeList. I thought it was used for distortion correction only and I was wrong about it.

Thanks so much for your help, now I can get the correct color for lossy DNG files.

1 Like