Skip to content

feat(dicomdir): the @dcmjs-org/dicomdir package — DICOMDIR reading and writing - #573

Merged
awatson1978 merged 3 commits into
release/1.0-beta-dicomdirfrom
dicomdir/package-port
Oct 2, 2026
Merged

awatson1978 merged 3 commits into
release/1.0-beta-dicomdirfrom
dicomdir/package-port

Conversation

@awatson1978

Copy link
Copy Markdown
Collaborator

This PR ports the DICOMDIR builder from v2.0-development into a new packages/dicomdir — the first (and only) wave for the @dcmjs-org/dicomdir package. The three source files (dicomdir.js, directoryOffsets.js, index.js) and the 16-test suite come over from the v2 branch's src/media/, and the package depends on @dcmjs-org/core and nothing else at runtime.

Same commit shape as the split-wave PRs (#564–#567):

  1. The first commit is the scaffold: packages/dicomdir/package.json (version 0.0.0, publishable, "@dcmjs-org/core": "workspace:*" as the only runtime dependency) plus a README, and the lockfile picking up the new workspace project.
  2. The second commit is the verbatim v2 port — all four files byte-for-byte as they sit on v2.0-development.
  3. The third commit is the hand-written rewiring, and it's where the one interesting decision lives. The v2 code imported from what is now @dcmjs-org/legacy in two places, and the hard rule for this package is that it never does. directoryOffsets.js measured element sizes through DicomMessage.write / DicomMessage.writeTagObject; those statics are one-line delegates to core's writeDataSet / writeTagObject, so the measurement calls core directly and the measured sizes can't change. dicomdir.js built a DicomDict and called dicomDict.write(); that envelope (128-byte preamble + "DICM", meta group behind its group-length element, then the body) is now a ~35-line writePart10 on the same core primitives, minus the deflate branch a DICOMDIR can never take.

Byte-exactness is the property that matters here — the directory offsets are measured against the exact layout the final write produces — so I checked it two ways. The ported suite itself proves the offsets: every nonzero offset value in the written file must land exactly on an FFFE,E000 item tag in the actual bytes, the next/lower chains must address every record exactly once, and the write must be deterministic. And I ran a scratch test (not committed) feeding the identical denaturalized { meta, dict } through both the inlined writePart10 and legacy DicomDict.write — the outputs are byte-identical, same length, same content.

Using it looks like this:

import { writeDicomDir } from "@dcmjs-org/dicomdir";

const arrayBuffer = writeDicomDir(
    [
        {
            referencedFileID: ["DICOM", "IM000001"],
            sopClassUid: "1.2.840.10008.5.1.4.1.1.4",
            sopInstanceUid: "1.2.3.4.100",
            transferSyntaxUid: "1.2.840.10008.1.2.1",
            patient: { PatientID: "998877", PatientName: "DOE^JANE" },
            study: { StudyInstanceUID: "1.2.3.4" },
            series: { SeriesInstanceUID: "1.2.3.4.5", Modality: "MR" },
            instance: { InstanceNumber: 1 }
        }
    ],
    { fileSetID: "MY_CD" }
);

The tests live in packages/dicomdir/test/ (jest's default testMatch picks the directory up — verified), fully synthetic, no fixtures. The readback oracle is DicomMessage.readFile from @dcmjs-org/legacy, declared as a devDependency only — it never appears in src/.

The ported lines are exempt per the budget convention; the hand-written total is about 120 lines (scaffold 55: package.json 18 + README 37; rewiring commit ~68 of its 93 inserted lines — the envelope, the import blocks, and comment-path updates — the rest being prettier rewraps of ported lines). Full suite after the port: 860 passed / 12 skipped / 872 total (the baseline 844 plus the 16 new), pnpm run build green, lint and format clean.

🤖 Generated with Claude Code

awatson1978 and others added 3 commits October 2, 2026 12:21
package.json modeled on @dcmjs-org/core's (version 0.0.0, publishable,
MIT, main ./src/index.js, repository directory), depending on
@dcmjs-org/core at runtime and on @dcmjs-org/legacy only as a
devDependency for test readback. Plus a short README with a usage
sample.

Co-Authored-By: Claude Fable 5 <[email protected]>
src/media/dicomdir.js, src/media/directoryOffsets.js, and
src/media/index.js from the v2.0-development branch land as
packages/dicomdir/src/{dicomdir,directoryOffsets,index}.js, and
test/dicomdir.test.js as packages/dicomdir/test/dicomdir.test.js —
byte-for-byte v2 content. Imports still point at the v2 tree; the next
commit rewires them onto @dcmjs-org/core and replaces the DicomDict
dependency.

Co-Authored-By: Claude Fable 5 <[email protected]>
The two legacy imports the v2 code carried are gone, keeping the hard
rule that this package never depends on @dcmjs-org/legacy:

- directoryOffsets.js measured element sizes through DicomMessage.write
  and DicomMessage.writeTagObject; it now calls core's writeDataSet and
  writeTagObject directly — the exact functions those statics are
  one-line delegates to, so measured sizes are unchanged.
- dicomdir.js built a DicomDict and called dicomDict.write(); the Part
  10 envelope (preamble + DICM, meta group behind its group length,
  then the body) is now inlined as writePart10 on the same core
  primitives, minus the deflate branch a DICOMDIR can never take.
  Verified byte-identical against legacy DicomDict.write on the same
  denaturalized { meta, dict }.

The test readback oracle rewires from the root dcmjs facade to
DicomMessage from @dcmjs-org/legacy (devDependency) plus
DicomMetaDictionary and validationLog from @dcmjs-org/core, and the
local EXPLICIT_LITTLE_ENDIAN constant gives way to core's export.

Full suite: 860 passed, 12 skipped, 872 total.

Co-Authored-By: Claude Fable 5 <[email protected]>
@awatson1978
awatson1978 merged commit 4e1d976 into release/1.0-beta-dicomdir Oct 2, 2026
10 checks passed
@awatson1978
awatson1978 deleted the dicomdir/package-port branch October 2, 2026 17:45
@awatson1978 awatson1978 changed the title feat: port the DICOMDIR builder into packages/dicomdir feat(dicomdir): the @dcmjs-org/dicomdir package — DICOMDIR reading and writing Oct 2, 2026
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.

1 participant