Looking for samples - smartphone DNGs with embedded GainMap

It would be a simple one line change to have the Adobe DNG SDK also perform the OpcodeList3 processing in addition to the OpcodeList2 processing, but that would mean the Adobe DNG SDK also has to demosaic the image in between. I’m not sure how it performs demosaicing but it doesn’t appear to have any sophisticated algorithm like AMaZE or RCD, so there could be some loss of quality.

As for supporting an image like this in Darktable - it probably would be worthwhile to add support for the FixVignetteRadial opcode as it’s also used by some iPhones. Some of the code from the recent change to support GainMaps could also be reused to make other DNG opcodes available to other modules, so the lens correction module could use the FixVignetteRadial data for vignetting correction instead of lensfun.

The GainMap in OpcodeList3 could be trickier - one possibility is to add a separate GainMap implementation either in the demosaic module or in a new module that comes after it. Another possibility is to take that GainMap in OpcodeList3 and transform it into equivalent OpcodeList2 GainMaps that have approximately the same effect but are applied before demosaic. It might not be exactly the same result bit for bit as if it was applied at the correct place, but would probably be close enough.

1 Like

Thanks for taking the time to explain it. I noticed in ART that you can select embedded for lens correction and it seems to apply one. I was going to ask you if it was a similar thing is grabbing from the meta data…

Hi Paolo,

Here are a couple of .dng files taken with my Fairphone 4 - they appear to have Exif entries for Opcode List 2 and Opcode List 3, so hope they might be of use regarding embedded DNG Gainmaps. Kind regards, Paul

IMG_20220409_101408.dng (22.9 MB)
IMG_20220413_170247.dng (22.9 MB)

Thanks! These have the typical smartphone GainMaps in OpcodeList2, which are supported by ART and the current Darktable master. The OpcodeList3 has a WarpRectilinear opcode for distortion correction - Darktable does not support that and I’m not sure what other apps do.

Adobe used AHD, Adaptive Homogeneity-Directed, as demosaicing method last time I checked.
Possibile to check by using this Bayer moire | LibRaw

It might be hard to see but here is your image…note dark corners…
image

Now with gainmap applied…

image

Yes, I can see the second image is definitely lighter.

How do I know if the dngs from my smartphone are using the GainMap in darktable? I checked that exiftool shows that they have gainmaps:

Opcode List 2 : GainMap, GainMap, GainMap, GainMap

In the “raw black/white point module”, if the “embedded GainMap” option appears in the “flat field correction” dropdown, then it is using it. You can switch it back and forth from disabled to see the effect of the GainMap.

Oh, I see the dropdown perfectly on new photos. On already existing ones, I can’t make it show up, even by resetting the history :pensive:

For the existing photos - select them in lighttable, under selected images module go to metadata tab and press “refresh EXIF”. The dropdown should appear after that.

1 Like

Here’s a DNG created by a DJI Mini 3 Pro, also using Opcode List 3 as far as I can tell.
DJI_0014.DNG (23.5 MB)

I wonder if there is a chance to translate the opcode processing into a lensfun profile. A while ago I tried to figure out what a Mini 3’s opcode list means from the dng spec, but was not yet able to solve this puzzle. I did not even understand if it contains lens calibration data, but at least the amount of entries makes it likely.

In particular, this is how the opcode list of your image starts according to exiv2:

Undefined 12564  0 0 0 2 0 0 0 9 1

I have no clue, what the “Undefined” means, for other tags it describes the data type, and the 12564 is likely the count. Next, the first opcode should start but there is no opcode “0”. Any hint welcome!

The OpcodeList3 in this DNG contains a GainMap opcode which applies both vignetting and color shading correction. It also contains a WarpRectilinear opcode which in theory could correct distortion and CA, but in this file the tag values do nothing and it could have been left out.

The file is valid, but it won’t work with the GainMap implementation that I added to darktable 4.0 because that implementation only works with GainMap in OpcodeList2 (before demosaic) and this one is in OpcodeList3 (after demosaic). At some point I’d like to try to fix that, maybe by converting it to an OpcodeList2 GainMap that has almost the exact same effect.

1 Like

It would be possible to convert vignetting, distortion, and CA correction from DNG opcodes to lensfun profiles, but the color shading can’t be handled by lensfun. It can only handle vignetting correction which is applied to all channels equally, not color sensitivity that varies from the center of the image to the edge.

Comparing the JPG to the corresponding DNG it looks like there’s either no distortion or the correction is already baked into the DNG.

Getting the GainMap to work in darktable would be awasome! I’m currently using Davinci to load and edit the DNG which does seem to apply the GainMap, but it’s not ideal :wink:

“Undefined” means exactly that - exiv2 doesn’t know how to parse this (because it’s a complex structure, and not a type), so it just spews out the contents, all 12564 bytes of it one by one…

It’s all in the DNG spec: you need to look at more than one byte, in this case first 4 bytes for a 32-bit number (“0 0 0 2”, i.e. 0x0002, which in big-endian is 2) signify there are 2 opcodes in this tag, the first opcode being 0x0009, which, indeed, is a GainMap, etc.

You might also want to do a binary/hex dump of this tag only from exiv2 rather than this text interpretation of individual decimal bytes w/ spaces, and look at it an a hex editor.

In any case, sounds like (some of) this is already being worked on!

I don’t think anyone has talked about supporting OpcodeList3 GainMap in the lens correction module yet, although that would be the appropriate place in the pipeline according to dng spec - it’s after demosaic but still in the camera color space. Can either apply it there, or convert it into a set of OpcodeList2 GainMaps for each color channel and use the existing code to apply it in rawprepare.

Hence the “some of” OpCodeList3’s…