Skip to content

LR-IMP-FORMATS: decode packed 12-bit Sony ARW (DSLR-A900) from its exact 1.5 bytes per pixel strip - #573

Closed
radialmonster wants to merge 1 commit into
storytold:mainfrom
radialmonster:sony-a900-packed
Closed

radialmonster wants to merge 1 commit into
storytold:mainfrom
radialmonster:sony-a900-packed

Conversation

@radialmonster

@radialmonster radialmonster commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: Sony DSLR-A900 raw files are plain packed 12-bit data (two pixels per three bytes), not a compressed format. This PR decodes them instead of showing the embedded preview. Item 6 of #535. The A850 is not covered by any measurement: there is no A850 sample in the test archive.

Change

  • crates/raw/src/vendor/arw.rs: a Sony ARW with Compression 32767, BitsPerSample 12, one strip and an even width is read as packed 12-bit data when the strip is exactly width x height x 1.5 bytes. Two pixels are stored in three bytes, least-significant-bit first: b0 | (b1 & 0xf) << 8 and b1 >> 4 | b2 << 4. Any other strip size still returns the existing Unsupported("... packed compressed variant ..."), so the A200/A230/A290/A350/A380/A390 behave as before.
  • Placement and colour for this layout: CFA pattern from the file's own tag (RGGB, also measured), black 128 (the 14-bit 512 of the other bodies, on the 12-bit scale), crop = the maker note's FullImageSize window centred in the frame (the file has no crop tags), as-shot white balance from a plain block in the maker note (read only when that block has the exact size seen in the sample and both green levels are unity).
  • crates/engine/src/media.rs: RENDER_CACHE_VERSION 22 -> 24. Main is at 22 and other open PRs use 23, so 24 is the next free number here; it will need adjusting at merge.

How the rule was established

Only from values measured in the sample file and LightCraft's own code (full record in the investigation log).

  • Size. 6080 x 4048 x 1.5 = 36 917 760 = the single strip, exactly; the strip ends at the last byte of the file; no row padding (a padded row would drift the decode, and the per-row smoothness is the same on all 4048 rows). Of the 115 Sony files with Compression 32767 in the archive, the A900 is the only one at 1.5; the others are exactly 1.000 (decode as today) or the 0.93 - 1.32 sizes of the older bodies (still refused).
  • Bit order. Eight orders tried; for each, the mean difference between same-colour neighbours two pixels apart. Least-significant-first: 16.0 (horizontal) / 13.1 (vertical). Every most-significant-first or mixed order: 171 - 199. The only close alternative (the two pixels of each pair swapped) changes nothing in that measure and is decided by CFA phase: it would give GRBG, and the file says RGGB.
  • CFA phase. The mosaic was registered to the embedded camera JPEG; for each of the four phase planes the plane's relative brightness was correlated with the JPEG's R, G and B relative brightness. Red plane (even row, even column): R +0.89; the two green planes: G +0.89 and +0.90; blue: B +0.92; all others negative or near zero. That is RGGB, equal to the file's own pattern tag.
  • Black and white. No black tag in the raw directory; the data floor (minimum 123, darkest blocks 132 - 141, noise 1.5 - 3 codes) matches 128. There are no clipped pixels in the sample (maximum 3029 of 4095), so the white level cannot be measured; the generic rule applies (plateau if present, else 4095). If a clipped A900 frame turns up, this is the number to check.
  • Crop. The camera JPEG (1616 x 1080) lines up with the middle of the 6080 x 4048 frame (its edges are within 15 - 24 px of a centred 6048 x 4032 window on each side, against 31 / 8 px for one anchored at the top-left), and the frame has no padding or masked columns at either edge, so the window is centred. One file only, so this is the least certain item; being wrong would shift the picture by 16 x 8 pixels.
  • White balance. The as-shot levels (R 672, G 256, G 256, B 379 per 256) sit as four big-endian numbers inside maker-note tag 0x0020 (19148 bytes, at byte 188). How the position was found: by searching the file for the levels ExifTool reports for it (ExifTool was used only as a tag reader, never its source). Only the position came from that search; no table of offsets was read. The four numbers are in plain form in the file, and they equal the levels in the camera's enciphered block (2688 / 1024 / 1024 / 1516 on that block's scale, as ExifTool reads it back), so the block is consistent within the file. The rule reads them only when the tag has exactly this size and both greens are unity.
  • Black level cross-check. Black 128 is measured from the data floor (per-plane minima 123–133, darkest blocks 132–141). As a cross-check only, the enciphered block (read back with ExifTool) says 512 on the 14-bit scale, i.e. 128.

Results

  • The A900 sample renders with natural colour and the camera JPEG's framing (figure below: camera JPEG left, this render right; no people in the frame).
  • Rendered all 199 Sony files in the archive with main and with this branch (500 px): exactly one render differs, the A900 (embedded preview -> decoded raw). The other 198 are byte-identical.
DSLR-A900 camera JPEG and the decoded raw

CC0 sample from raw.pixls.us (id 5858). Left: camera JPEG; right: this change.

Tests

  • packed_12_bit_strip_of_one_and_a_half_bytes_per_pixel_decodes: hand-made TIFF bytes (an 8 x 4 image packed with a reference packer) decode to the exact samples in header and full mode, with RGGB, black 128 and 12 bits; the three bytes 12 A3 45 unpack to 0x312 and 0x45A. It fails without the change.
  • packed_12_bit_needs_the_exact_strip_size: strips one byte short, one byte over, or padded per row, and an odd width, stay Unsupported.
  • a900_white_balance_block_is_read_only_when_it_matches: the measured block gives 2.625 / 1 / 1.480; a wrong size, a non-unity green or an absurd red gain is rejected.
  • The existing strip_larger_or_smaller_than_one_byte_per_pixel_is_the_packed_variant still passes.

Not covered

  • A850: no sample, nothing measured. A file with the same strip rule would decode; its white balance is read only if its maker note has the same block.
  • Highlight clipping point (see above) and the crop origin rest on one file.

CI

  • cargo fmt --check: clean
  • cargo clippy --workspace --all-targets -- -D warnings: clean
  • cargo test --workspace --no-fail-fast (with the corpus): all pass except headless::tests::browse_a_local_folder in ui-egui (a path-separator mismatch in a temp folder on the P: drive, P:\... against P:/...; it fails the same way on unmodified main in this environment, and nothing in this change touches the UI)
  • cargo xtask parity, layers, assets, wasm: pass (parity prints a note that the summary in docs/parity.md is stale; unrelated to this change)

Update after review: rebased onto main. The as-shot white balance now comes from #575's SR2 WB_RGGBLevels, which gives the same values as the offset-188 block did, so that block is removed. RENDER_CACHE_VERSION is 41.

@radialmonster

Copy link
Copy Markdown
Contributor Author

Note for review: @AlexChambers' #575 (stacked on #542) adds one decryptor for the SR2SubIFD and reads the applied white balance from it for every ARW from the DSLR-A200 on. The A900 stores the same levels there (2688 / 1024 / 1024 / 1516, the same ratios as the plain block this PR reads: 2.625 and 1.480). Once #575 is on main, I'll drop this PR's maker-note white-balance rule (tag 0x0020 at the exact size seen in the sample) and let the A900 use the shared SR2 path. That removes the one position here that was found by searching for reported values. The packed 12-bit decode itself doesn't depend on it. If you'd rather merge this first, the simplification can follow as a small separate change.

@echelon

echelon commented Oct 10, 2026

Copy link
Copy Markdown
Member

Thank you @radialmonster! Requiring the exact 1.5-bytes-per-pixel strip size is a nice tight check for the A900.

It can't land as it stands: #575 (SR2SubIFD white balance and black level) and #580 (16:9 crop on pre-crop-tag bodies) landed in #657, and both change default_crop's signature and the white-balance chain in arw.rs. Could you rebase onto main? While you're there, please check whether #575's SR2 WB_RGGBLevels already covers the A900. If it does, the hard-coded a900_wb block at offset 188 can go.

Please join our Discord if you haven't already: https://discord.gg/artcraft

…act 1.5 bytes per pixel strip

Compression 32767 with BitsPerSample 12 and one strip of exactly width x height x 1.5 bytes is plain packed 12-bit data: two pixels per three bytes, least-significant-bit first (measured on the DSLR-A900 sample by same-colour neighbour smoothness, 12x smoother than the other orders). RGGB per the file's pattern tag (checked against the camera JPEG), black 128, crop = FullImageSize centred in the frame (on even offsets).

White balance: the hard-coded maker-note block at offset 188 is gone. The SR2SubIFD WB_RGGBLevels read since storytold#575 give 2688/1024/1024/1516 on the CC0 sample, i.e. 2.625 / 1.0 / 1.480, identical to the block's values, so the existing chain covers the A900.

Rebased onto main after storytold#575/storytold#580 (storytold#657): keeps main's default_crop signature and recorded_size_crop; the A900 centring is applied afterwards by centred_in_frame. RENDER_CACHE_VERSION 37 -> 41 (other branches take 38-40; adjust at merge).
@radialmonster

Copy link
Copy Markdown
Contributor Author

@echelon rebased onto main, and you were right about the white balance:

  • WB: LR-IMP-CAMERA-COVERAGE: Sony ARW white balance and black level from one SR2SubIFD decryptor #575's SR2 WB_RGGBLevels on the CC0 A900 sample reads 2688 / 1024 / 1024 / 1516, i.e. R 2.625, B 1.4805. That's exactly what the hard-coded block at offset 188 gave (672 / 256 / 256 / 379). So a900_wb, its constants and its test are gone, and the render is byte-identical with and without it.
  • Crop: main's default_crop signature, recorded_size_crop and the WB chain are kept as they are. The A900's centred crop is now a small centred_in_frame() applied only to packed-12 frames.
  • Checks: all 198 Sony ARW, SR2 and SRF files on raw.pixls.us render through the CLI, and only the A900 changes against main. Clippy and fmt are clean. The only test failures are ones that also fail on current main on Windows: the 7 tests_sync path-separator tests (Synchronize Folder tests: expected paths spelled as the OS does (Windows) #660) and the MCP export_progress_and_cancel timing test.
  • Cache: RENDER_CACHE_VERSION is 41, past main's 37 and distinct from our other open PRs.

@echelon

echelon commented Oct 10, 2026

Copy link
Copy Markdown
Member

Thank you @radialmonster! The A900 decoder landed in #692, from the maintainer merge on this branch (20d7d9e):

Please join our Discord if you haven't already: https://discord.gg/artcraft

mysbryce pushed a commit to mysbryce/lightcraft that referenced this pull request Oct 10, 2026
mysbryce pushed a commit to mysbryce/lightcraft that referenced this pull request Oct 10, 2026
…ultCrop helper side by side (merge put the flag on the wrong function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@radialmonster

Copy link
Copy Markdown
Contributor Author

Thanks @echelon! Everything here landed in #692; the extra lines on this branch are the older crop/WB helpers that #580 and #575 replaced on main. Closing.

clydecbrown pushed a commit to clydecbrown/lightcraft that referenced this pull request Oct 26, 2026
…plicates

Synchronize Folder: import suspected duplicates on request (upstream storytold#2034)
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.

2 participants