Skip to content

Add support for embedded lens corrections in RawNIND/AI-denoised DNGs#21507

Open
trougnouf wants to merge 1 commit into
darktable-org:masterfrom
trougnouf:rawnind-dng-embedded-lens-corrections
Open

Add support for embedded lens corrections in RawNIND/AI-denoised DNGs#21507
trougnouf wants to merge 1 commit into
darktable-org:masterfrom
trougnouf:rawnind-dng-embedded-lens-corrections

Conversation

@trougnouf

Copy link
Copy Markdown
Contributor

Implement serialization of DNG OpcodeList2 and OpcodeList3 to preserve embedded lens correction data (warp rectilinear, vignette radial, gain maps) when writing DNG files.

Key changes:

  • Add dt_dng_opcode_serialize_opcode_list_2() and _3() functions to serialize correction data from dt_image_t to DNG opcode list format
  • Add _write_dng_opcode_lists() helper in imageio_dng.c to write opcode lists to TIFF directories using libtiff's TIFFTAG_OPCODELIST2/3 tags
  • Modify dt_imageio_dng_write_cfa_bayer() and dt_imageio_dng_write_linear() to call _write_dng_opcode_lists() and preserve correction data
  • Add fallback definitions for TIFFTAG_OPCODELIST* tags for older libtiff

This ensures that when RawNIND/AI-denoise creates a DNG file from a source image that has embedded lens corrections (e.g., from a DNG with OpcodeList3), those corrections are preserved in the output DNG and will be automatically applied when the denoised DNG is re-imported into darktable.

Fixes #21504

Generated by Mistral Vibe.

This was entirely vibe coded. I tested it on a Sony A7C ARW picture on Arch Linux and it worked well; there is a distinct geometry and vignetting difference between no lens correction, lensfun, and embedded.

@trougnouf trougnouf marked this pull request as draft July 7, 2026 09:24
Implement serialization of DNG OpcodeList2 and OpcodeList3 to preserve
embedded lens correction data (warp rectilinear, vignette radial, gain maps)
when writing DNG files.

Key changes:
- Add dt_dng_opcode_serialize_opcode_list_2() and _3() functions to serialize
  correction data from dt_image_t to DNG opcode list format
- Add _write_dng_opcode_lists() helper in imageio_dng.c to write opcode lists
  to TIFF directories using libtiff's TIFFTAG_OPCODELIST2/3 tags
- Modify dt_imageio_dng_write_cfa_bayer() and dt_imageio_dng_write_linear()
  to call _write_dng_opcode_lists() and preserve correction data
- Add fallback definitions for TIFFTAG_OPCODELIST* tags for older libtiff

This ensures that when RawNIND/AI-denoise creates a DNG file from a source
image that has embedded lens corrections (e.g., from a DNG with OpcodeList3),
those corrections are preserved in the output DNG and will be automatically
applied when the denoised DNG is re-imported into darktable.

Fixes: darktable-org#21504

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
@trougnouf trougnouf force-pushed the rawnind-dng-embedded-lens-corrections branch from bec5527 to 7784061 Compare July 7, 2026 09:30
@trougnouf

trougnouf commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Re-tested after ensuring that it compiles on the CI, it still allows for embedded lens corrections :)

@trougnouf trougnouf marked this pull request as ready for review July 7, 2026 09:38
@andriiryzhkov

Copy link
Copy Markdown
Collaborator

I tested it on a Sony A7C ARW picture on Arch Linux and it worked well; there is a distinct geometry and vignetting difference between no lens correction, lensfun, and embedded.

Testing note: Sony ARWs are already handled by #21449 (which sets CORRECTION_TYPE_SONY and preserves the SubImage1 tags). Your PR's serializer bails out on its first line for non-DNG correction types, so the corrections you saw round-trip on the Sony A7C are #21449 at work, not this PR.

To exercise this PR's actual code path, source needs to be a file where exif_correction_type == CORRECTION_TYPE_DNG – iPhone ProRAW, Pixel / Samsung Expert RAW, Leica M/Q/SL DNG, DJI drone DNG, Ricoh GR III, or anything through Adobe DNG Converter. If possible please also verify the LinearRaw path (X-Trans) since it also calls the new writer.

@andriiryzhkov andriiryzhkov added bugfix pull request fixing a bug scope: AI features AI features related issues and PR labels Jul 7, 2026
@trougnouf

Copy link
Copy Markdown
Contributor Author

Sorry about that, I should have tested the master branch first!

@andriiryzhkov

Copy link
Copy Markdown
Collaborator

No problems, and thank you for the contribution. The underlying change is still valid and worth landing. Though it focuses on DNG-source workflows in neural restore module.

@trougnouf

Copy link
Copy Markdown
Contributor Author

If possible please also verify the LinearRaw path (X-Trans) since it also calls the new writer.

I confirm that the current master branch supports embedded lens corrections on both the Sony A7C and (X-Trans) Fujifilm X-E2.

No problems, and thank you for the contribution. The underlying change is still valid and worth landing. Though it focuses on DNG-source workflows in neural restore module.

Should I not close this PR then?

@andriiryzhkov

Copy link
Copy Markdown
Collaborator

Should I not close this PR then?

Keep it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix pull request fixing a bug scope: AI features AI features related issues and PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FR: embedded lens corrections in RawNIND/AI-denoised images

2 participants