Skip to content

demosaic: add Menon (2007) DDFAPD and ARI algorithms#20800

Open
dregsist wants to merge 5 commits intodarktable-org:masterfrom
dregsist:pr/menon-ari-demosaic
Open

demosaic: add Menon (2007) DDFAPD and ARI algorithms#20800
dregsist wants to merge 5 commits intodarktable-org:masterfrom
dregsist:pr/menon-ari-demosaic

Conversation

@dregsist
Copy link
Copy Markdown

@dregsist dregsist commented Apr 12, 2026

Summary

darktable's demosaicing lineup covers the speed-vs-quality axis well at
the extremes (VNG/PPG for speed, AMaZE for quality), but the mid-range
lacks per-pixel adaptive selection. This PR adds two algorithms that
fill that gap:

  • Menon (2007) — Directional De-mosaicing for Frequency-Adaptive
    Pattern (DDFAPD): a two-pass algorithm that separates horizontal and
    vertical interpolation and merges them via local gradient energy
    weighting. Produces sharp edges with reduced zipper artifacts at a
    cost comparable to RCD.

  • ARI — Adaptive Resolution Interpolation (Monno et al. 2017):
    generates multiple green-channel candidates (MLRI via Laplacian
    energy direction, and HA via Hamilton-Adams), then selects per-pixel
    using noise-adaptive criteria. Three quality levels: Fast (MLRI only),
    Balanced (2 candidates), Quality (3 candidates with guided-filter
    smoothing on color differences). ARI's per-pixel candidate selection
    also addresses the same high-ISO use case as the existing
    AMaZE+bilinear blend, without requiring manual threshold tuning.

Implementation notes

  • Both are pure CPU implementations using OpenMP, consistent with the
    existing RCD and LMMSE modules.
  • Negative Bayer values (possible after rawprepare) are clamped to zero
    at algorithm entry, consistent with RCD's _safe_in() approach.
  • ARI exposes a quality parameter (1–3) in the UI; stored in the
    params struct, version bumped 6→7 with legacy_params migration.
  • Menon reuses the existing procmax scaler used by RCD/LMMSE.
  • Both algorithms are Bayer-only; X-Trans paths are unaffected.

References

  • D. Menon, S. Andriani, G. Calvagno, "Demosaicing With Directional
    Filtering and a posteriori Decision," IEEE Trans. Image Process.,
    vol. 16, no. 1, pp. 132–141, Jan. 2007.
  • Y. Monno, D. Kiku, M. Tanaka, M. Okutomi, "Adaptive Residual
    Interpolation for Color and Multispectral Image Demosaicking,"
    Sensors, vol. 17, no. 12, 2807, 2017.

Testing

  • Tested on Bayer RAW files from Canon and Nikon bodies.
  • No purple fringing or zipper artifacts observed on high-frequency
    test targets.
  • ARI Quality mode shows improved fine-detail retention compared to
    bilinear and VNG on high-ISO crops.
  • No regressions in existing demosaic methods (RCD, LMMSE, VNG,
    AMaZE) confirmed.

Copy link
Copy Markdown
Member

@TurboGit TurboGit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some style issues. TIA.

Comment thread src/iop/demosaicing/menon.c Outdated
Comment thread src/iop/demosaicing/menon.c Outdated
@dregsist
Copy link
Copy Markdown
Author

Thank you for the review!

  • Copyright year corrected to 2026 in menon.c; standard darktable GPL header added to ari.c as well.
  • All multi-parameter lines split to one parameter per line across both files.

@jenshannoschwalm
Copy link
Copy Markdown
Collaborator

Would you be able to hint at raw images plus the region to inspect where you think there is an improvement over current demosaicers?

Menon is ~half performance of rcd here, can you confirm? Btw there definitely is room for perf gains by internal tiling.

@da-phil
Copy link
Copy Markdown
Contributor

da-phil commented Apr 14, 2026

Can we use the integration test repo images to have a quantitative comparison between demosaicers?

@jenshannoschwalm
Copy link
Copy Markdown
Collaborator

Can we use the integration test repo images to have a quantitative comparison between demosaicers?

nope.

@jenshannoschwalm
Copy link
Copy Markdown
Collaborator

I just did a first round of detail-checks on various images i have in my collection and must say, i am not convinced by quality of both algos . Maybe i am testing on "wrong" images, please share examples where you would say the new algos do better. A summary of my results

On high ISO images both algos do quite similar to LMMSE

On low-ISO images both algos

  1. introduce maze patterns (as RCD) in areas where VNG4 is stable
  2. introduce artefacts at areas with strong gradients

Using RCD as reference
Bildschirmfoto vom 2026-04-15 08-43-51

Menon
Bildschirmfoto vom 2026-04-15 08-45-48

ARI

Bildschirmfoto vom 2026-04-15 08-47-25

Didn't lookup original source. Could it be we need "normalizing" of input/output (as we do for RCD, LMMSE) ?

@dregsist
Copy link
Copy Markdown
Author

Thank you @jenshannoschwalm for running the comparison —
the color fringing on mire1 is clearly not right.

I'll investigate first — likely a bug in the R/B interpolation
direction or a Bayer pattern offset issue in the Menon implementation.
Will also verify ARI output correctness. Fixing both before anything else.

After that, planning to:

  • add internal tiling for both Menon and ARI as you suggested
  • run CPSNR/SSIM benchmarks (Kodak etc.) across all demosaic methods
    for a proper quantitative comparison

@da-phil yes, will include results against the integration test images as well.

@dregsist dregsist force-pushed the pr/menon-ari-demosaic branch from 164e521 to 83fa112 Compare April 16, 2026 22:56
@dregsist dregsist requested a review from LebedevRI as a code owner April 16, 2026 22:56
dregsist added a commit to dregsist/darktable-rawforge that referenced this pull request Apr 19, 2026
Accompanies darktable PR darktable-org#20800 (Menon 2007 + ARI Monno 2015 demosaic).

Contents:
- BENCHMARK.md / BENCHMARK_JA.md: full writeup, English + Japanese
- bench_*.py: reproducible scripts for Kodak-24 (low/mid/high ISO),
  SIDD medium (iso_clean/iso_noise), chroma zipper, iteration sweep
- _paper_ari_ref.py / _paper_ri_ref.py: Python paper-exact references
- test_*.c / test_amaze.cc: standalone C test harnesses
- viewer_data_paper_vs_simple/: 5 Kodak edge images x 7 methods PNGs
- refs/matlab/: authors' original MATLAB code (RI/MLRI/ARI)
- dt_tests_dtbuild/mire1_*.jpg: end-to-end darktable integration

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@dregsist
Copy link
Copy Markdown
Author

The previous ARI implementation had quality issues (color fringing, maze patterns on mire1) because it cut too many corners in the adaptive selection heuristic. This rewrites ARI to match the Monno 2015 paper line-by-line (structurally equivalent to the authors' MATLAB reference, within ±0.002 dB CPSNR of a Python port).

Quality is indeed better:

  • Kodak low-ISO CPSNR 39.94 dB, +0.8 dB over amaze
  • Chroma zipper smallest across all tested methods and ISOs
  • SIDD real raw iso_clean: +0.38 dB over amaze+dual

However, the paper-exact pipeline is slow:

  • 30-80x slower than existing methods
  • 53s per 15 MP patch at 16 threads (standalone)
  • 113s through darktable pipeline on mire1.cr2
  • Memory-bandwidth bound; OpenCL is unlikely to reach practical speeds

Optimizations applied (all preserve paper-exact output):

  • Rolling sum box filter replacing double integral image
  • Row-major vertical pass for cache-line reuse on wide images
  • Paired GF sum sharing (~33 % box-call reduction)
  • Separable Gaussian 5x5 for criterion smoothing
  • Separable bicubic 7x7 for R/B residual upsample
  • Removed redundant refined-estimate recomputation in RI-H
  • Criterion-side adjacent-loop fusion

Menon (2007) is separately included as a reference implementation. It is fast (~amaze speed), but benchmarking across Kodak-24 + SIDD-medium did not show a clear advantage over amaze — chroma zipper is similar or worse (especially cz_peak), and CPSNR is within 0.1 dB.

Both algorithms looked strong on paper; extensive benchmarking showed neither clearly beats the existing methods across the tested conditions. I'll leave the merge decision to maintainers. The benchmark numbers themselves should be useful regardless; full writeup + reproducible scripts + Python reference ported from the original MATLAB are available at:

https://github.com/dregsist/darktable-rawforge/tree/demosaic-benchmarks

menon.c: keep the unused FIR/k_b locals (they're referenced via OpenMP firstprivate clauses) to fix -Werror=unused-variable with GCC 15.

dregsist and others added 5 commits April 20, 2026 07:22
Add two new Bayer demosaicing methods:
- Menon (2007) DDFAPD: directional filtering with a posteriori decision
- ARI: adaptive residual interpolation with quality levels (fast/balanced/quality)

Both are CPU-only, OpenMP parallelized. Introspection v6->v7 migration added.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
rawprepare can produce negative pixel values; clamp to zero at the
start of Menon processing, consistent with how RCD handles this case.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
rawprepare can produce negative pixel values. Create a clamped copy
of the input at the entry of ari_demosaic so that all gradient and
residual computations (which depend on consistent non-negative data)
see a uniform view of the CFA. Mirrors the approach used in RCD.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Update copyright year to 2026 in menon.c
- Replace custom header in ari.c with standard darktable GPL header
- Split multi-parameter function signatures to one parameter per line
  in both ari.c and menon.c (style requirement)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The previous ARI implementation had quality issues (color fringing,
maze patterns on mire1) because it cut too many corners in the
adaptive selection heuristic. This rewrites ARI to match the Monno
2015 paper line-by-line (structurally equivalent to the authors'
MATLAB reference, within 0.002 dB CPSNR of a Python port).

Quality is indeed better:
- Kodak low-ISO CPSNR 39.94 dB, +0.8 dB over amaze
- Chroma zipper smallest across all tested methods and ISOs
- SIDD real raw iso_clean: +0.38 dB over amaze+dual

However, the paper-exact pipeline is slow:
- 30-80x slower than existing methods
- 53s per 15 MP patch at 16 threads (standalone)
- 113s through darktable pipeline on mire1.cr2
- Memory-bandwidth bound; OpenCL is unlikely to reach practical speeds

Optimizations applied (all preserve paper-exact output):
- Rolling sum box filter replacing double integral image
- Row-major vertical pass for cache-line reuse on wide images
- Paired GF sum sharing (~33 % box-call reduction)
- Separable Gaussian 5x5 for criterion smoothing
- Separable bicubic 7x7 for R/B residual upsample
- Removed redundant refined-estimate recomputation in RI-H
- Criterion-side adjacent-loop fusion

Menon (2007) is separately included as a reference implementation.
It is fast (~amaze speed), but benchmarking across Kodak-24 +
SIDD-medium did not show a clear advantage over amaze - chroma
zipper is similar or worse (especially cz_peak), and CPSNR is
within 0.1 dB.

Both algorithms looked strong on paper; extensive benchmarking
showed neither clearly beats the existing methods across the
tested conditions. I'll leave the merge decision to maintainers.
The benchmark numbers themselves should be useful regardless;
full writeup + reproducible scripts + Python reference ported
from the original MATLAB are available in the sibling
darktable-rawforge/demosaic_test/ directory (BENCHMARK.md).

menon.c: keep the unused FIR/k_b locals (they're referenced via
OpenMP firstprivate clauses) to fix -Werror=unused-variable with
GCC 15.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@dregsist dregsist force-pushed the pr/menon-ari-demosaic branch from f937bd7 to de30883 Compare April 19, 2026 22:23
@jenshannoschwalm
Copy link
Copy Markdown
Collaborator

Thanks for reworking ARI and especially the "benchmarking" :-)

  1. I think the good noise data for the dual demosaicers on high ISO images are mostly due to the low-pass effect of the low-frequency content (bilinear part of VNG4 plus smoothing)
  2. The "amount of calculated detail" is difficult to benchmark.
  3. I guess we agree that there is no big point to include menon in dt.
  4. I looked at the re-implementation of ARI, that indeed uses a large amount of memory (memory bound performance) per pixel. As that is comparable to markje3 we could guess-assume a two-fold performance gain if using internal CPU tiling for L2 cachesize. When implementing such code in OpenCL we mostly find a perf-gain of ~4-fold on capable hardware. Expected performance still pretty slow. Not sure if we should include this ...

@dregsist
Copy link
Copy Markdown
Author

Thanks for looking at this and for the honest read.

Fully agree with your assessment. Both methods I picked from recent papers hoping they'd bring something new, but the benchmarks across Kodak-24, SIDD real raw, and chroma-zipper metrics made it clear:

  • ARI: quality wins at low ISO (+0.8 dB Kodak, +0.38 dB SIDD clean over amaze+dual), but 30–80× slower is just not viable for an interactive tool. Even with OpenCL the paper algorithm (11 iter × RI+MLRI × per-pixel argmin) is memory-bandwidth bound, so the gap to amaze/rcd wouldn't really close.
  • Menon: fast enough, but no margin over amaze at any ISO tested. Hard to argue for inclusion on quality grounds.

The existing rcd/amaze/+dual lineup is well-balanced and I don't see a use case these two would unlock in practice. I'll leave the merge/close decision to you — happy either way.

The benchmark writeup and reproducible scripts are at dregsist/darktable-rawforge/demosaic_test in case the numbers are useful for future demosaic discussions.

@jenshannoschwalm
Copy link
Copy Markdown
Collaborator

I tried the reference ARI again (on my pretty fast machine) it a) is very slow b) seems to give slightly more details without introducing artefacts compared with rcd or amaze c) it has better "chroma noise resistance". Although when using capture sharpening on rcd the details advantage is not visibly any more to me.

I am still working on pixelpipe caching improvements, with current ROI concept ARI is unfortunately not practical.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants