Skip to content

feat: write the deflated transfer syntax - #560

Merged
awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/deflate-on-write
Sep 30, 2026
Merged

awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/deflate-on-write

Conversation

@awatson1978

Copy link
Copy Markdown
Collaborator

This ports deflate-on-write from the v2 rewrite, closing the last write-side transfer-syntax gap on this branch. Until now DicomDict.write could read Deflated Explicit VR Little Endian (1.2.840.10008.1.2.1.99) but not write it: asked to write under that syntax, it emitted an uncompressed body beneath a meta group that declared it deflated, which no conformant reader can consume. That is exactly why PR #553's corpus gate had to skip-list the deflate trio (image_dfl, report_dfl, wave_dfl).

The fix is the v2 branch's deflate arm of DicomDict.write, ported nearly verbatim minus the lazy-core passthrough context that does not exist here: per PS3.10 A.5 the preamble, DICM and the file meta group are written uncompressed, then the body is encoded as plain explicit little endian into a scratch stream and appended raw-deflated (pako.deflateRaw, the exact mirror of the read side's inflateRaw — pako was already a dependency for reading, so no new package). BufferStream gains the small writeRawBytes helper the branch needs, adapted from v2 without its zero-copy window machinery.

Writing a deflated file and reading it back now just works, through both read paths:

import dcmjs from "dcmjs";
const { DicomDict, DicomMessage } = dcmjs.data;

const dicomDict = new DicomDict({
    "00020010": { vr: "UI", Value: ["1.2.840.10008.1.2.1.99"] } // deflated ELE
});
dicomDict.upsertTag("00100010", "PN", [{ Alphabetic: "Doe^Jane" }]);

const bytes = dicomDict.write();          // body is actually deflate-compressed
const back = DicomMessage.readFile(bytes); // eager reader inflates it
// ...and fromPart10Stream(bytes, listener) inflates it incrementally

Tests came first, failing on the unfixed code with pako's "invalid stored block lengths" — the raw-inflate of what was really an uncompressed body. The new test/write-deflate.test.js adapts v2's 398-line write-deflate.test.js to this branch: the helpers (fixture reading, byte-diff reporting) came over verbatim, but the lazy-core passthrough byte-identity assertions became a core-valid equivalent — inflating the deflated output body must be byte-identical to writing the same dict as plain ELE — and the v2 "both cores" re-reads became this branch's two readers, eager readFile and fromPart10Stream. The v2 suite's @dcmjs/parser and published dicom-parser cross-checks were replaced by the independent test/issues/part10Walker.js, which proves the meta group stays uncompressed using no dcmjs read code at all. Seven tests cover: the three fixtures round-tripping as deflated, an edited element re-encoded under the wrapper, ELE→deflated and deflated→ELE conversions, and a freshly built dict written deflated.

With the capability in place, the #553 skip-list is deleted outright — the corpus gate now runs the deflate trio through bytes → events → Part 10 → readback like every other fixture, so deflate round-trips are gate-covered from here on. One pre-existing test moved with the behavior: the lossless-read-write "deflated pixel data has explicit length" test scanned the raw output buffer, which only worked because the body was (wrongly) uncompressed; it now inflates the body first and scans that.

On the budget: 418 inserted lines total, of which roughly 85 are verbatim or near-verbatim from v2 (the DicomDict.write deflate branch, writeRawBytes, and the test helpers), leaving about 330 hand-written — most of it the reworked test suite. Suite goes from 832 passing / 846 total to 842 passing / 856 total: seven new tests plus the three un-skipped corpus fixtures.

🤖 Generated with Claude Code

DicomDict.write now emits Deflated Explicit VR Little Endian
(1.2.840.10008.1.2.1.99): the preamble, DICM magic and file meta group
stay uncompressed per PS3.10 A.5, and the body is encoded as explicit
little endian then raw-deflated (pako.deflateRaw, the mirror of the read
side's inflateRaw). Ported from the v2 rewrite's DicomDict deflate
branch, minus the lazy-core passthrough context that does not exist on
this branch. BufferStream gains writeRawBytes for appending the
already-encoded deflated body.

The corpus gate's deflate skip-list (image_dfl, report_dfl, wave_dfl) is
removed — the trio now round-trips bytes -> events -> Part 10 ->
readback like every other fixture. test/write-deflate.test.js adapts the
v2 suite to this branch's two read paths (eager readFile and
fromPart10Stream), and the lossless-read-write deflate pixel-data test
now inflates the body before scanning, since the body is actually
compressed.

Co-Authored-By: Claude Fable 5 <[email protected]>
@awatson1978
awatson1978 merged commit aa78ada into release/1.0-beta-core Sep 30, 2026
8 checks passed
@awatson1978
awatson1978 deleted the core/deflate-on-write branch September 30, 2026 19:54
@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