← Back to Pull requests

Egami.Formats.Extended: camera RAW stage B (maker formats) #220

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 31, 2026 13:59 UTC
Author

<!-- claude-projects-attribution: {"github_login":"inexin"} -->
_Requested by Lars_

What this changes

RAW Build Plan stage B (tasks 6 to 10): the maker RAW formats in Egami.Formats.Extended. Nothing is released; RAW ships later with stages C and D.

  • Formats (tasks 8 and 9): RawFormat now also reads Canon CR2 (with sRAW and mRAW), Nikon NEF and NRW, Pentax PEF, Sony ARW and SR2, Olympus ORF, Panasonic RW2 (RawFormat 4 to 8) and Fujifilm RAF (uncompressed 16-bit and 12-bit, and the compressed codec: lossless and lossy, X-Trans and Bayer, 12 to 16 bits). Each maker's white balance, black levels and crops come from the file and its maker note (ExifTool's public tag pages read as facts). Sony's SR2 private IFD is decrypted for levels and white balance.

  • Camera table (task 6): 556 cameras, measured by tools/make-raw-camera-table.py from Adobe DNG Converter's output on one CC0 raw.pixls.us sample each (black box). It gives colour matrices, BaselineExposure, and the levels and crops a file does not give. It also records each RAF sample's exposure bias (so a Fuji DR setting moves the baseline exposure as in Adobe's DNGs) and each Canon body's sRAW white level. The tool now measures full-resolution samples only; 16 Canon bodies had been measured from sRAW files before, so their lines changed.

  • Canon sRAW and mRAW: decoded to LinearRaw RGB with the spec's [L] formula sets A and B. The decode stops before the spec's sRAW multipliers. That leaves exactly the values Adobe DNG Converter stores; the multipliers' levels divide the as-shot neutral instead.

  • X-Trans demosaic: Egami's own gradient-weighted method (green along the flatter axis, then red and blue as colour differences weighted by green similarity).

  • Panasonic zeroed defective pixels are filled from same-colour neighbours.

  • Lens corrections (task 10, extended per Lars): Lensfun XML and Adobe LCP profiles loaded at run time from the new RawReaderOptions.LensProfiles (plus system Lensfun folders); no profile data ships; the CC BY-SA status of Lensfun's data is in the docs. RawDetails.LensProfile names what was applied. An automatic lateral CA correction for files nothing covers is behind the new RawReaderOptions.AutoChromaticAberration (off by default, because it costs about as much as the demosaic).

  • Fuzz fix: the RAW header sniffing did 32-bit offset arithmetic that a far IFD offset wrapped. Stage A's DNG check had the same bug.
  • Public API added (for API review)

  • RawReaderOptions.LensProfiles (IReadOnlyList<string>), RawReaderOptions.AutoChromaticAberration (bool), RawDetails.LensProfile (string?).

  • RawFormat.Info gains the extensions and MIME types of the maker formats.
  • Decoders against Adobe DNG Converter (black box)

    I converted each CC0 sample with Adobe DNG Converter -u and compared Egami's RawOutput.Mosaic with the DNG's raw data, sample by sample, over the DNG's area:

    | Files | Result |
    | --- | --- |
    | RAF: X-T3 (lossless and uncompressed), X-T4 lossless, X-T5, X-H2S and X-E5 lossy X-Trans, GFX 50S lossless Bayer, GFX100 II lossy 14-bit, GFX100S lossless 16-bit, X-M1 12-bit packed, X-E2 uncompressed | bit-exact (11 files, up to 102 M samples each) |
    | Canon 350D; Nikon D850, Z 7; Olympus E-M10; Panasonic GH5, GH6 (RawFormat 8), S1, S1H (RawFormat 6); Pentax K-3, K-5; Sony A6000 | bit-exact (11 files) |
    | Panasonic GH4 | 47 of 12 M differ by at most 21: the camera's zeroed defective pixels, which Adobe fills its own way |
    | Sony A7 IV lossless | 4,070 saturated samples above 16383, which Adobe clips to the white level (the develop clips them too) |
    | Canon sRAW and mRAW: 40D, 5D Mark II, 5D Mark III, 6D, 70D, 80D | within 1 code value (rounding of the chroma interpolation) |
    | Canon 7D mRAW (set A) | within about 2 %. [L]'s hue offset for this body; Adobe matches a hue of f × 2 instead (open point below) |
    | Nikon D90 | not comparable this way: Adobe stores the pre-curve values plus a linearization table |

    The Nikon D850 and Z 7 results check the [L] initial-predictor order on real files, and GH6 checks the RawFormat 8 green mapping. The synthetic test NikonInitialPredictorsFollowTheSpecOrder fails loudly if the order is swapped.

    With baseline exposure and the profile look off, Egami's develop of each RAF matches Egami's develop of Adobe's DNG of it within 0.05 % mean absolute difference. White balance, matrices, levels and crop all agree. With them on, the per-file exposure bias makes the X-T5 file match as well.

    Against LibRaw (Magick.NET 14.17.2, camera WB, AHD, no auto-bright), mean ΔE76

    | File | ΔE76 | | File | ΔE76 |
    | --- | --- | --- | --- | --- |
    | Sony A6000 | 1.01 | | Fuji X-E5 lossy | 1.31 |
    | Nikon D90 | 1.58 | | Fuji X-M1 12-bit | 1.61 |
    | Canon 40D sRAW | 1.89 | | Canon 5D II sRAW | 0.80 |
    | Canon 350D | 2.79 | | Panasonic GH5 | 2.95 |
    | Fuji X-H2S lossy | 3.31 | | Nikon Z 7 | 3.88 |
    | Fuji X-T5 | 4.09 | | Fuji GFX 50S | 4.14 |
    | Olympus E-M10 | 4.32 | | Nikon D850 | 4.61 |
    | Panasonic S1H | 4.76 | | Panasonic GH4 | 5.53 |
    | Pentax K-3 | 5.70 | | Panasonic GH6 | 5.96 |
    | Fuji X-T4 | 6.27 | | Sony A7 IV | 6.53 |
    | Panasonic S1 | 8.95 | | Fuji X-T3 | 12.0 |
    | Pentax K-5 | 23.9 | | Canon 7D mRAW | 16.2 |

    Where these are large, Egami agrees with Adobe and LibRaw differs:

  • Fuji X-T3: LibRaw renders about 10 % brighter.

  • 7D mRAW: LibRaw's white balance differs.

  • K-5: crop alignment in my harness plus LibRaw's as-shot WB (colour means agree under auto WB).
  • The formal gates are stage D in egami-compare.

    Open points (no decision needed for this PR)

    1. Sony A100: the ARW1 data at its SubIFD offset decodes as noise with every layout I tried, so it is refused with NotSupportedException. The study spec also lists it as unconfirmed.
    2. Canon 7D mRAW: [L]'s hue rule gives (f + 1) × 4 for this body; Adobe's values match f × 2. I kept the spec's rule.
    3. Fujifilm GFX 100 files (100 MP) need MaxTotalBytes above the 2 GB default for a full develop; previews, half size and mosaic fit. This is already true for 100 MP DNGs in stage A, and the docs say so.
    4. Not read, as planned: CR3 (stage C), SRF, sNEF, Nikon HE, pre-2012 non-TIFF RAFs and 14-bit packed RAFs (no sample).

    Provenance (required)

  • Everything in this change is my own work, written from specs and the libraries in docs/references.md.

  • This change contains no wrapped native code and no new package references in src/ beyond the approved ones in docs/references.md.
  • Sources used:

  • The maker codecs were written only from the clean-room specifications in the study notes:

  • - Fujifilm compressed RAF, sections 2 to 10
    - NEF, sections 3 to 6
    - PEF
    - ARW, sections 3 to 7
    - ORF, sections 4 to 6
    - RW2, RawFormat 4 to 8
    - CR2, sections 3 to 6

    A separate, study-only session wrote those specs after reading LibRaw and rawspeed for structure only, under Lars's 2026-10-24 study-only rule. The implementing session (this one) never opened LibRaw, rawspeed, dcraw, darktable, RawTherapee or ExifTool source. Nothing was copied, ported or paraphrased from them. docs/references.md now says this in the camera RAW paragraph.

  • Maker-note tags and RAF meta tags: ExifTool's public tag-name pages, read as facts.

  • Black boxes, for measurement and gates only:

  • - Adobe DNG Converter: the camera table, bit-exact comparisons, the Fuji bias and sRAW white facts
    - LibRaw through Magick.NET: ΔE comparisons
  • Measured facts:

  • - Fuji 12-bit uncompressed packing: least significant bits first, found from image smoothness
    - Panasonic zero pixels: found by comparing with Adobe's DNGs
  • Lens profiles: the Lensfun XML format and the Adobe LCP (XMP camera-profile) format from their published documentation. Lensfun's LGPL code was never read, and no profile data is included.

  • X-Trans demosaic and auto CA: my own methods.

  • Sample files: raw.pixls.us, CC0 only. None are committed; the fuzz seeds and tests use files generated in code.
  • Nothing was translated from a permissive library, so THIRD-PARTY-NOTICES.md is unchanged.

    Testing

  • Unit tests added or updated:

  • - Fujifilm compressed round trip through a test-only encoder (spec section 10) for X-Trans and Bayer, 12, 14 and 16 bits, including a narrow last strip
    - the RAF container
    - refusal of a compressed RAF with a different X-Trans phase
    - Nikon initial-predictor order
    - the header-arithmetic fuzz regression
    - lens profile and auto CA tests

    Egami.Formats.Extended.Tests: 216 passed, 4 skipped. Egami.DocSamples: 630 passed; Text.InstalledFonts fails on this PC on main too (installed fonts).

  • dotnet format Egami.slnx --verify-no-changes: clean. python tools/check-docs.py src/Egami.Formats.Extended: 0 problems.

  • AFL++ raw target with 4 new maker seeds, 10 minutes:

  • - first run: 3 crashes, all the header overflow, fixed
    - rerun after the fix: 0 crashes, 0 hangs
  • Allocation-budget tests pass.

  • Benchmarks run. DNG develop (RawBenchmarks, 3000 × 2000 14-bit, one thread, BenchmarkDotNet medium job):
  • | Develop | main | this PR |
    | --- | --- | --- |
    | Mosaic | 90.3 ms ± 2.4 | 85.3 ms ± 2.9 |
    | HalfSize | 160.0 ms ± 15.1 | 138.3 ms ± 5.9 |
    | Bilinear | 253.8 ms ± 14.0 | 248.0 ms ± 12.5 |
    | Malvar | 264.3 ms ± 11.6 | 230.6 ms ± 4.2 |
    | Ahd | 793.1 ms ± 14.2 | 823.6 ms ± 36.6 |
    | Default | 1,261.8 ms ± 49.0 | 1,155.1 ms ± 44.4 |

    AHD is within its error bars; the AHD code is unchanged. Before AutoChromaticAberration became opt-in, every develop was about 0.9 s slower; that is why it is off by default.

    Maker files (RawMakerBenchmarks, new; one thread; main has no maker decoders, so there is no before):

    | File | Codec | Mosaic (decode) | Default develop |
    | --- | --- | --- | --- |
    | Canon 350D (8 MP) | CR2 lossless JPEG | 139 ms | 2.27 s |
    | Canon 80D sRAW2 (6 MP) | CR2 sRAW | 247 ms | 1.00 s |
    | Nikon D850 (46 MP) | NEF Huffman | 420 ms | 6.98 s |
    | Olympus E-M10 (16 MP) | ORF | 798 ms | 4.64 s |
    | Panasonic GH6 (25 MP) | RW2 RawFormat 8 | 831 ms | 6.74 s |
    | Pentax K-3 (24 MP) | PEF Huffman | 317 ms | 6.30 s |
    | Sony A6000 (24 MP) | ARW2 | 139 ms | 6.11 s |
    | Fuji X-T3 (26 MP) | RAF compressed lossless, X-Trans | 1.55 s | 8.08 s |
    | Fuji X-E5 (40 MP) | RAF compressed lossy, X-Trans | 2.73 s | 12.06 s |
    | Fuji GFX 50S (51 MP) | RAF compressed lossless, Bayer | 2.84 s | 19.48 s |

    BenchmarkDotNet short job, one thread (MaxParallelism = 1), so these are CPU totals; with the default parallelism a 26 MP develop takes about 0.7 to 1 s on this PC. The Fuji rows were rerun after the table fix; before it, the lossy X-E5 mosaic allocated 167 MB and now 0.9 MB.

    Checklist

  • Every commit is signed off (git commit -s)

  • Public API changes are documented and listed in PublicAPI.Unshipped.txt
  • 🤖 Generated with Claude Code

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: claude[bot] Review duration: 16m
    All Vouch review confidence checks passed
    Residual risk 0.30 = Change Risk (0.57) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.57 · 650 lines diff across 8 files
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #220"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57% (MEDIUM) · Higher = larger blast radius
    57%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 30.4% · Safe to merge
    30.4%
    Repository