Skip to content

feat: direct-USB LS-50 (Coolscan V) capture driver - #21

Open
rjerham12 wants to merge 1 commit into
rohanpandula:mainfrom
rjerham12:feat/ls50-direct-usb-capture
Open

rjerham12 wants to merge 1 commit into
rohanpandula:mainfrom
rjerham12:feat/ls50-direct-usb-capture

Conversation

@rjerham12

Copy link
Copy Markdown

Ports the Nikon Coolscan wire grammar from nkscan (LS-5000/LS-9000 protocol spec, byte-identical to the LS-50) into a new protocol/ls50 package so the LS-50 ED can be driven over raw USB without SANE.

What's included

  • protocol/ls50/capture.py: session + RGBI capture. The image READ carries DTC in byte 2 and DTQ (color<<8|element-width) in bytes 4-5 per the Nikon spec, reads in whole-granule chunks, and treats a short read / sense 05-2C (ILI, OutOfSequence) as the normal end of the image stream — fixing the per-line READ desync seen on the physical LS-50.
  • protocol/ls50/artifacts.py: raw linear 16-bit RGB + -ir.tif sidecar + receipt (4000:16:1:rgbi).
  • protocol/ls50/workflow.py: Ls50Roll — preview (per-slot streaming, end-of-strip detection), set_spacing_offset, scan_many.
  • protocol/ls50/pass_runner.py: calibration-pass CLI.
  • protocol/ls50/frame_edges.py: row-density seam detection.
  • tests/protocol/ls50/: hardware-free tests.

Attribution

Wire grammar adapted from activexray/nkscan (MIT/Apache-2.0 dual), which documents the LS-5000/LS-9000 ED protocol and lists the LS-50 (Coolscan V) as supported.

Verification

Live-tested against a physical LS-50 ED (color-negative strip, SA-30): RGBI frame captures at 400/4000 dpi are correct raw negatives; preview completes with end-of-strip detection.

Port the Nikon Coolscan wire grammar from nkscan (LS-5000/LS-9000 protocol
spec, byte-identical to the LS-50) into a new protocol/ls50 package so the
LS-50 ED can be driven over raw USB without SANE:

- protocol/ls50/capture.py: session + RGBI capture (RESERVE, MODE_SELECT,
  SET_BOUNDARY, SET_WINDOW x4, SCAN with RGBI mask, chunked image READ).
  The READ carries the data type code (DTC) in byte 2 and the data type
  qualifier (DTQ = color << 8 | element width) in bytes 4-5, transfer length
  in 6-8, control 0x80 -- per the Nikon spec -- and treats a short read or
  sense 05-2C (ILI, OutOfSequence) as the normal end of the image stream.
  This replaces per-line READs that desynchronized the LS-50.
- protocol/ls50/artifacts.py: write raw linear 16-bit RGB + -ir.tif sidecar
  (marker scanstudio.infrared.linear.uint16.v1) + receipt with
  settingsFingerprint 4000:16:1:rgbi.
- protocol/ls50/workflow.py: Ls50Roll -- preview (per-slot streaming, end of
  strip detection), set_spacing_offset (frame adjust), scan_many.
- protocol/ls50/pass_runner.py: CLI for the calibration passes.
- protocol/ls50/frame_edges.py: row-density seam detection.
- tests/protocol/ls50/: hardware-free coverage of the geometry, read CDB
  grammar, and end-of-strip preview logic.

The wire grammar is adapted from nkscan (activexray/nkscan, MIT/Apache-2.0),
which documents the LS-5000/LS-9000 ED protocol and supports the LS-50.

Live-verified against a physical LS-50 ED with a color-negative strip in an
SA-30 adapter: preview completes with end-of-strip detection and full-res
RGBI captures are correct raw negatives.
@rohanpandula

rohanpandula commented Sep 12, 2026 •

Copy link
Copy Markdown
Owner

Standalone LS-50 review: implementation handoff

Updated September 12 after an independent adversarial review with Claude and verification of the key corrections against the same reviewed snapshot. This update does not claim a fresh review of any newer commits. Claude performed static analysis; the previously reported test runs and boundary reproductions were performed separately. Corrections below narrow the library/transport claims, explain the actual smear and manifest effects, and add the missed debug log.

Reviewed head: a17791e50de1bb47fa116940da770365aa6841c8. This PR is behind the LS-50 implementation in ScanStudio #123 at 768ddcd368a340223c46f0dc285f0831d0f16857. Do not merge or copy that implementation blindly: it also has confirmed defects. The companion ScanStudio comment contains the full cross-stack findings and runnable reproductions; read the full handoff and runnable probes here.

This Markdown is intended to be pasted directly into an implementation LLM. Read the actual current files first, preserve the shared LS-5000 transport contract, and make small fixes with regression tests. Review covered every changed line and all seven added files. No physical scanner qualification was performed.

Fix these in the standalone package

  1. Broken CLI entry point: pass_runner.py calls roll.capture_frame, but Ls50Roll has no such method. Route through the supported capture API and the existing artifacts.py writer. The subsequent roll.preview.frames expression also treats a method as a preview result and needs repair. Simply copying ScanStudio's switch to scan_many is insufficient: without optional --preview, that implementation has no geometry and produces no captures. Test the documented command end to end with only the hardware boundary replaced.
  2. READ/transaction mismatch: capture.py::_read_lines delegates to a transaction reader that insists on the full declared transfer. A short packet followed by zero raises DesynchronizedProtocolError before the outer EOF logic can run. A full payload plus error sense is accepted by the outer loop. Test partial/zero/full transfers and nonzero sense against the actual worker, not a mocked transaction return that hides this contract. The short-transfer probe does not establish that real LS-50 EOF uses a short phase-3 transfer; phase-1 no-data responses are different. The unreachable branch and accepted error sense are code facts, while real EOF framing needs hardware evidence. Determine valid behavior from traces and preserve recovery requirements after desynchronization.
  3. Capture settings discarded: scan_many(..., samples_per_scan=4) goes into unused **kwargs; newly constructed options retain the default of one sample. In this standalone repository this is a latent API issue; the app caller lives in ScanStudio. Advertise and accept only LS-50 modes backed by evidence, rejecting others before motion. Do not infer four-sample hardware support from the LS-5000 code or silently propagate four into SET_WINDOW. Do the same for CLI DPI/depth/exposure and ensure receipt values match executed options.
  4. Preview stop poisons a later scan: this head's scan_many checks the stop event but does not establish a fresh operation. After preview_stop, a new scan sees the old cancellation. The later ScanStudio patch clears the flag inside a generator and creates a different race: a stop between iterator construction and first iteration gets erased. Fix operation setup and cancellation ordering explicitly; test both sequences.
  5. Spacing adjustment is not persisted/applied: set_spacing_offset replaces an entry in a temporary frames dictionary, without updating the stored preview frames. scan_many still uses the original native origin. Persist the selected offset, use it for capture geometry, and verify that subsequent thumbnails and receipts agree. Test an offset on a non-first slot and check the actual capture options; validate geometry bounds.
  6. CLI physical slot, frame label and repetitions are mixed up: the slot-map comprehension uses the mapped frame label for both values. Preserve physical slot 1 separately from label 20. Repetitions hardcode slot 20 and reuse the same stem; honor --page-frame/--page-frames, define the ArepNN naming contract, and prevent intended multiple captures from overwriting one another. Test outputs with distinguishable image pixels and enumerate all resulting artifacts.
  7. Evidence must describe what happened: inspect Ls50FrameReceiptMinimal and avoid presenting placeholder quality verdicts/identity as measured evidence. Use actual exposure/device/geometry values and explicit unavailable telemetry where measurement does not exist. Coordinate schema changes with ScanStudio consumers instead of filling required fields with invented passing values. Those consumers are not part of this standalone PR. In the newer monorepo implementation, smear none is out of vocabulary and flags all captures as smeared because consumers expect clean; copying those defaults is not a fix. The older standalone receipt also has fields that would fail the monorepo deserializer, including null fresh fingerprint and empty clipping fractions. Use explicit unknown telemetry supported by the schema and test its downstream interpretation.

Source entry points: capture.py, workflow.py, pass_runner.py, artifacts.py.

Minimum completion evidence

  • Run the LS-50 tests plus existing facade/session/transport and relevant shared worker tests using this repository's normal development environment.
  • Add tests that call real CLI/workflow/transaction code with fake USB/session boundaries; do not replace the code whose behavior is being checked.
  • Demonstrate: default CLI behavior, preview-stop-new-scan, stop-before-first-iteration, persisted spacing offset, requested multisampling, nonzero READ sense rejection, distinct repeat outputs, mapped physical slot selection, and complete RGB/IR/receipt artifacts.
  • Port verified fixes between the two repositories and compare the shared package files and tests. Keep ScanStudio-only UI/Rust/bridge integration work in the monorepo.
  • Report hardware observations separately from simulated checks. Do not assert the protocol is validated from these tests alone.
  • Return a finding-by-finding disposition with code and test references. If a finding has changed at a newer head, show the resolving code rather than editing an already-correct path.

The companion reproduction script targets the reviewed ScanStudio head and imports APIs absent from this older standalone head; use it there as supplied, or adapt each probe deliberately to this head. In particular, do not claim its cancellation-clear race already exists in this older implementation.

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