Skip to content

feat: add the element-decode primitives and character-set decoders - #535

Merged
awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/decode-primitives
Sep 29, 2026
Merged

awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/decode-primitives

Conversation

@awatson1978

@awatson1978 awatson1978 commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

This PR lands the element-decode primitives and the character-set decoders. It's the half of the decode core that doesn't depend on the vendored tokenizer, and it's what the streaming reader (arriving in a later PR) builds on.

A quick gloss for anyone who hasn't been following this branch. Every DICOM element carries a VR — a value representation, the two-letter code (CS, LO, SQ, ...) that says how the element's bytes should be interpreted. In "implicit VR" transfer syntaxes the file doesn't store that code at all. The reader has to resolve it from the data dictionary. Separately, a dataset can declare its text encoding through SpecificCharacterSet (0008,0005). That's what the character-set modules here handle.

What's actually in the PR:

  • src/core/decodeCore.js — pure decode functions extracted from the lazy reader. They take read-only window and policy inputs instead of the lazy reader's mutable context. The centerpiece is resolveVrInstance, which is now the single canonical implicit-VR contract for every read path. It follows the classic eager rules: a dictionary miss with a defined length resolves to UN and is never promoted to SQ by peeking at the data; pixel data falls back to OW; private creators fall back to LO. Around it sit shapeReadValues (string multiplicity splitting and the raw-value quirks, replicated verbatim from DicomMessage._readTag), retainRaw, buildElementStream, decodeElementValues, decodeWithEagerReadTag (a narrow fallback that hands rare shapes to the exact eager code), and classifyElement, which routes each element to one of four decode strategies: sequence, encapsulated pixel data, eager fallback, or plain value.
  • src/core/normalizeSyntax.js — transfer-syntax normalization as a standalone module.
  • src/charset/latin1.js and src/charset/iso2022.js — the Latin-1 and ISO 2022 decoders, plus resolveCharsetDecoder, which turns a SpecificCharacterSet value into a decoder. It keeps the eager reader's exact error policy.

Here's the charset resolver in use:

import { resolveCharsetDecoder, Iso2022Decoder } from "./src/charset/iso2022.js";

// A single supported charset returns a working decoder.
const latin1 = resolveCharsetDecoder(["ISO_IR 100"]);
latin1.decode(new Uint8Array([0xe9])); // "é"

// ISO 2022 code-extension lists return a stateful Iso2022Decoder.
const jp = resolveCharsetDecoder(["ISO 2022 IR 13", "ISO 2022 IR 87"]);
jp instanceof Iso2022Decoder; // true

// Unsupported charsets throw by default, and degrade gracefully under ignoreErrors.
resolveCharsetDecoder(["nope"]);                        // throws "Unsupported character set"
resolveCharsetDecoder(["nope"], { ignoreErrors: true }); // null (default repertoire)

One thing did NOT come along, and that's deliberate. The lazy reader's seedReadContext, its pako inflater shim, and its parseDicom import existed only to drive the vendored tokenizer. The release plan removes that tokenizer from 1.0, so this module arrives already free of the dependency.

For testing: test/core/decodeCore.test.js exercises the pure primitives with synthetic elements, and test/charset/resolveCharsetDecoder.test.js drives the charset resolver directly. Corpus-level coverage of these same functions arrives with the streaming reader's suite — the streaming reader is what produces elements once the tokenizer is gone.

🤖 Generated with Claude Code

@awatson1978
awatson1978 merged commit 78210e2 into release/1.0-beta-core Sep 29, 2026
8 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.0.0-beta.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant