smartphone with several lenses/lensfun/darktable

I got a new phone Samsung Galaxy S22 which is able to shoot raws (dng format). The dedicated app from Samsung for raws, called Expert Raw, unfortunately cannot be imported in darktable:

Anyway, the dngs from the default camera app can be opened. Apparently they do not contain metadata for lens correction. I therefore started to build a lensfun entry and here the problems started.

As the phone has four cameras (one front, three rear) the standard lensfun entries are not compatible as they require a single crop factor over all “lenses”. These are not identical for the four cameras. Moreover the dng is agnostic to the actual camera used. Thus darktable will not know which camera is used and cannot apply the camera specific corrections.

However from the dng metadata the correct camera can be derived as each camera has a different focal length (and different pixel sizes). I therefore wrote a small script which adds the “lens” information to the dng metadata.

cat /usr/local/bin/modify_dngs.sh


#!/usr/bin/env bash

BASE_DIR="/mnt/media/georg/Bilder/import/Galaxy S22"
CAM_MODEL="SM-S901B"

# Ultra wide: 2.2 mm
exiftool -overwrite_original -r -ext dng \
  -if '$Model eq "'"$CAM_MODEL"'" and $FocalLength# == 2.2' \
  -LensMake="Samsung" \
  -LensModel="ultra wide rear" \
  -Model="SM-S901B-uw" \
  "$BASE_DIR"

# Wide: 5.4 mm
exiftool -overwrite_original -r -ext dng \
  -if '$Model eq "'"$CAM_MODEL"'" and $FocalLength# == 5.4' \
  -LensMake="Samsung" \
  -LensModel="wide rear" \
  -Model="SM-S901B-w" \
  "$BASE_DIR"

# Tele: 7 mm
exiftool -overwrite_original -r -ext dng \
  -if '$Model eq "'"$CAM_MODEL"'" and $FocalLength# == 7' \
  -LensMake="Samsung" \
  -LensModel="tele rear" \
  -Model="SM-S901B-t" \
  "$BASE_DIR"

# Front: 3.3 mm
exiftool -overwrite_original -r -ext dng \
  -if '$Model eq "'"$CAM_MODEL"'" and $FocalLength# == 3.3' \
  -LensMake="Samsung S22" \
  -LensModel="front" \
  -Model="SM-S901B-f" \
  "$BASE_DIR"

touch "$BASE_DIR/modify_dngs_last_run" # gives a quick check when script was run for the last time

exit 0 # required as exiftools throws an exit code of 2 if no file is changed

Basically we change the camera model to one of four subtypes and introduce the camera information also as LensModel.

With this it is now possible to create four sets of lensfun entries which cover the four possibilities:

~/.local/share/lensfun$ cat s22_all.xml 

<lensdatabase version="1">
	<camera>
        <maker>Samsung</maker>
        <model>SM-S901B-w</model>
        <model lang="en">Galaxy S22 wide</model>
        <mount>samsung-S22-w</mount>
        <cropfactor>4.25</cropfactor>
  </camera>
    
	<lens>
        <maker>Samsung</maker>
        <model>Samsung S22 wide</model>
        <model lang="en">fixed lens</model>
        <model lang="de">festes Objektiv</model>
        <mount>samsung-S22-w</mount>
        <type>rectilinear</type>
        <cropfactor>4.25</cropfactor>
        <aspect-ratio>4:3</aspect-ratio>
        <calibration>
        <distortion model="ptlens" focal="5.4" a="-0.003" b="0.000" c="0.031"/>
        <tca model="poly3" focal="5.4" br="0.0000233" vr="0.9999704" bb="-0.0000653" vb="1.0000950"/>
        <vignetting model="pa" focal="5.4" aperture="1.8" distance="10" k1="-0.1496052" k2="0.2121462" k3="-0.1891344"/>
        <vignetting model="pa" focal="5.4" aperture="1.8" distance="1000" k1="-0.1496052" k2="0.2121462" k3="-0.1891344"/>
      </calibration>
  </lens>

	<camera>
        <maker>Samsung</maker>
        <model>SM-S901B-uw</model>
        <model lang="en">Galaxy S22 ultra wide</model>
        <mount>samsung-S22-uw</mount>
        <cropfactor>5.9</cropfactor>
  </camera>
    
	<lens>
        <maker>Samsung</maker>
        <model>Samsung S22 ultra wide</model>
        <model lang="en">fixed lens</model>
        <model lang="de">festes Objektiv</model>
        <mount>samsung-S22-uw</mount>
        <type>rectilinear</type>
        <cropfactor>5.9</cropfactor>
        <aspect-ratio>4:3</aspect-ratio>
        <calibration>
            <distortion model="ptlens" focal="2.2" a="-0.082" b="0.165" c="-0.085" />
            <tca model="poly3" focal="2.2" br="0.0000065" vr="1.0001798" bb="-0.0000500" vb="1.0002138" />
        </calibration>
  </lens>

  <camera>
        <maker>Samsung</maker>
        <model>SM-S901B-t</model>
        <model lang="en">Galaxy S22 tele</model>
        <mount>samsung-S22-t</mount>
        <cropfactor>9.85</cropfactor>
  </camera>
    
	<lens>
        <maker>Samsung</maker>
        <model>Samsung S22 tele</model>
        <model lang="en">fixed lens</model>
        <model lang="de">festes Objektiv</model>
        <mount>samsung-S22-t</mount>
        <type>rectilinear</type>
        <cropfactor>9.85</cropfactor>
        <aspect-ratio>4:3</aspect-ratio>
        <calibration>
            <distortion model="ptlens" focal="7.0" a="-0.008" b="0.039" c="-0.048" />
            <tca model="poly3" focal="7.0" br="0.0000474" vr="0.9996693" bb="-0.0000918" vb="1.0002451" />
            <vignetting model="pa" focal="7.0" aperture="2.4" distance="10" k1="-0.0629400" k2="0.0418255" k3="-0.0825312" />
            <vignetting model="pa" focal="7.0" aperture="2.4" distance="1000" k1="-0.0629400" k2="0.0418255" k3="-0.0825312" />
        </calibration>
  </lens>


  <camera>
        <maker>Samsung</maker>
        <model>SM-S901B-f</model>
        <model lang="en">Galaxy S22 front</model>
        <mount>samsung-S22-f</mount>
        <cropfactor>7.57</cropfactor>
  </camera>
    
	<lens>
        <maker>Samsung</maker>
        <model>Samsung S22 front</model>
        <model lang="en">fixed lens</model>
        <model lang="de">festes Objektiv</model>
        <mount>samsung-S22-f</mount>
        <type>rectilinear</type>
        <cropfactor>7.57</cropfactor>
        <aspect-ratio>4:3</aspect-ratio>
        <calibration>
            <distortion model="ptlens" focal="3.3" a="-0.006" b="0.003" c="0.025" />
        </calibration>
  </lens>

</lensdatabase>

With these settings the correct camera will be automatically selected when doing edits in darktable.

In my workflow I use syncthing to synchronize the raws from my smartphone to my main computer. With the following two systemd services the folder is checked on the computer for changes and “ameliorates” the freshly arriving dng files.

cat /etc/systemd/system/import_dir_watch.service
[Unit]
Description=Run script when files change in /mnt/media/georg/Bilder/import/Galaxy S22

[Service]
Type=oneshot
Restart=on-failure
RestartSec=5
ExecStart=/usr/local/bin/modify_dngs.sh



# Trigger when anything in the directory changes
# PathChanged=/mnt/media/georg/Bilder/import/Galaxy S22
# or to trigger on new/removed files:
PathModified=/mnt/media/georg/Bilder/import/Galaxy S22

Unit=import_dir_watch.service

# Avoid hitting a limit caused by syncthing synchronization
TriggerLimitIntervalSec=1s
TriggerLimitBurst=100


[Install]
WantedBy=multi-user.targetx

I did a few test over the weekend it it worked quite nicely.

The issue might be of broader and growing concern as most smartphones have several cameras now and in most cases the raws will not embed the correction information. To cover smartphone raws in future it could be required to create a new format of xml which would allow the matching of smartphone and camera in one run without the necessity to manipulate the dng metadata beforehand.

Greetings!

If you convert these with Adobe’s software to “normalize” them do they keep the necessary info?? Just curious

Sorry pure linux here. I do not use/have the Adobe DNG converter.

This does sound like a useful capability for darktable to handle natively.

You can install and run Abody DNG Converter on Linux using wine. It works for me just fine on Fedora.

1 Like

What does the Adobe DNG converter do with the dng file? distortion correction? TCA? vignetting? This would put the lens correction step from darktable towards the converter.
Are you using dngs from Pro mode or ExpertRaw?

If you want to share one I can run it through Adobe DNG convertor and you can have a look at what the generated file is…if that would be of any use for you… You can set a few things in the software like version for compatibility etc… just ignore this if it is of no interest to you…

Dear Todd, thanks for the offer. You find attached the original dng from the camera and the non-lens corrected jpeg straight from darktable (only presets). I picked a shot with the ultra wide rear camera I used for distortion determination with hugin. Here the effect of a missing lens correction is most obvious.

20260219_082054.dng (34.4 MB)

The other couple is the modified dng and the corresponding jpeg with an admittedly non perfect distortion correction.

20260219_082054.dng.xmp (7.8 KB)
modified_20260219_082054.dng (34.4 MB)

Interesting to see how your jpegs come out after conversion via Adobe DNG converter & darktable. It would be most convenient if the dng could be batch processed (without user intervention) and then used in darktable.

These appear to be demosaiced dng and they open fine in DT…I was thinking you were sending your expert raw for conversion or am I confused about what you need… In any case they look about the same after running them through adobe and I am not sure the jpg will differ. I will update this with a dropbox link when I get home…