Repository navigation
rw2: support raw format 8 (V8), fix panic on undersized raw sections - #60
Open
akilegaspi wants to merge 2 commits into
Open
akilegaspi wants to merge 2 commits into
akilegaspi wants to merge 2 commits into
Conversation
decode_panasonic computes bit-pump block offsets assuming the classic
9/8-bytes-per-pixel Panasonic packing. Newer bodies (S5 II / S5 II X /
S1R II / G9 II generation) write a ~1 byte/pixel compression, so the
raw section is ~25% smaller than the assumed packing and the unguarded
slice at the top of each row group panics on the rayon decode threads:
range start index 26673152 out of range for slice of length 25587216
Add a minimum-size guard at both decode_panasonic call sites, computed
from the block-aligned offset of the last row group, so these files
return a descriptive Err instead. The bound is strictly below the size
of any working 9/8-packed file, so existing decodes are unaffected.
Verified with a DC-S5M2X file (6000x4008): previously panicked, now
returns Err("RW2: raw section is 25587216 bytes, too small for
6008x4008; unsupported compression variant").
Fixes pedrocr#59
Port of rawspeed's PanasonicV8Decompressor (LGPL-2.1). V8 files carry a RawFormat tag (0x002d) that is now checked before decoding to select the decoder generation: - RawFormat 8: Huffman-coded strips of 2x2 CFA tiles (lossless-JPEG-like prediction). All V8 tags are validated up front: identity gamma curve, zero shift-down, clip value 0xffff, consistent strip tables, grid tiling covering the whole image, and in-bounds strip data. Anything unexpected returns a descriptive Err instead of decoding garbage. - RawFormat 4..=7 or tag absent: legacy decoders, with the undersized-buffer guard from the previous commit kept as a safety net. - Any other version: clean Err. The strip bitstream is MSB-first with the bits of each byte reversed; each strip starts from the 2x2 initial prediction in the metadata and each symbol is a difference category plus JPEG-style extended diff bits. Also adds camera entries for DC-S5M2 / DC-S5M2X (blackpoint 512, whitepoint 16383, RGGB, rawspeed color matrix). Verified against DC-S5M2X files (6008x4008, RawFormat 8): decode succeeds with sane levels (min ~= blackpoint, max = whitepoint) and a normalized correlation of 0.87 with macOS ImageIO's native decode of the same file (raw Bayer vs. demosaicked render, so <1.0 is expected).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #59.
Summary
Adds support for Panasonic RW2 raw format 8 (V8) — the Huffman-coded strip encoding written by the S5 II / S5 II X / S1R II / G9 II generation — and fixes the out-of-bounds panic these files triggered in the legacy
decode_panasonicpath:Changes
1. Generation validation before decoding. The
RawFormattag (0x002d) is read before any pixel decoding and selects the decoder:8→ the new V8 decoder4..=7or tag absent → the legacy decoders, unchangedErr("RW2: unsupported raw format version …")2. V8 decoder (port of rawspeed's
PanasonicV8Decompressor, LGPL-2.1 — V8 is lossless-JPEG-style prediction over strips of 2×2 CFA tiles):RevMsbPump)Errinstead of decoding garbage or panicking3. Safety net for legacy paths.
panasonic_min_buf_size()guards bothdecode_panasoniccall sites: the bound is the block-aligned bit-pump offset of the last row group, strictly below the size of any working 9/8-packed file, so no file that decodes today can be rejected.4. Camera entries for DC-S5M2 / DC-S5M2X (blackpoint 512, whitepoint 16383, RGGB, rawspeed color matrix).
Verification
cargo testpasses (5 passed, 2 ignored).