Skip to content

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

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

demosaic: add Menon (2007) DDFAPD and ARI algorithms#20800
dregsist wants to merge 4 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.

dregsist and others added 3 commits April 13, 2026 00:31
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>
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
@@ -0,0 +1,551 @@
/*
This file is part of darktable,
Copyright (C) 2024-2025 darktable developers.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

only 2026 obviously. And the same header must be added into ari.c.

Comment thread src/iop/demosaicing/menon.c Outdated
/* Helper: mirror-boundary 1D horizontal convolution with a 3-tap kernel.
kernel[] is indexed [-1..+1] stored as kernel[0..2]. */
static inline float _menon_cnv_h3(const float *const restrict buf,
const int row, const int col,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

style: one parameter per line (there is quite some instances of this).

- 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>
@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.

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