Skip to content

feat: private block reservation per PS3.5 Section 7.8.1 - #626

Merged
wayfarer3130 merged 3 commits into
masterfrom
private-block-api
Nov 24, 2026
Merged

wayfarer3130 merged 3 commits into
masterfrom
private-block-api

Conversation

@pieper

@pieper pieper commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

The problem

Writing a private element means hand-managing the reservation — pick a block, hope nothing else has it, write the creator element yourself, build each tag by string concatenation:

dicomDict.upsertTag("4D430010", "LO", ["ACME 1.0"]);
dicomDict.upsertTag("4D431001", "CS", ["HYBRID"]);

That hard-codes the block number, which is the one thing a caller must not assume. Per PS3.5 §7.8.1 a creator at (gggg,00xx) reserves (gggg,xx00)–(gggg,xxFF), and which xx a creator holds is decided by whoever wrote the file. A reader that looks at (4D43,1001) because the writer happened to use block 0x10 reads the wrong element as soon as the file came from somewhere that used 0x11.

This PR

// writing - the block is allocated, not chosen
const block = dicomDict.privateBlock(0x4d43, "ACME 1.0");
block.set(0x01, "CS", ["HYBRID"]);        // -> (4D43,1001), or wherever it landed

// reading, from any parsed dict - found by creator, never by block
const block = findPrivateBlock(dict, 0x4d43, "ACME 1.0");
block && block.get(0x01).Value;

findPrivateBlock() takes a plain dict, because that is what a reader has after DicomMessage.readFile(). DicomDict#privateBlock() is the writing side and reserves a block when the creator has none — finding the creator if it is already present, otherwise taking the lowest free block, so blocks another writer holds are left alone. Creator values are compared trimmed, since LO pads an odd length with a space.

Validation follows the standard rather than ad hoc checks: an even group is rejected (private data elements are in odd groups), blocks are 0x10–0xFF (0x00–0x0F are reserved), elements are 0x00–0xFF within the block, and a creator must be a non-empty string within LO's 64 characters.

Tests

12 in test/privateBlock.test.js: allocation, idempotence for the same creator, skipping blocks another writer holds, lookup without reserving, a full file round trip found by creator, a creator pushed to block 0x30, trailing-pad tolerance, and the validation cases.

Two of them cover what a private block is usually for — carrying an opaque payload:

  • a 512 KB OB round-tripping byte-exact, so the 32-bit length path is exercised rather than assumed;
  • an odd-length OB coming back one byte longer with a trailing pad, since DICOM value fields are even length (PS3.5 §7.1.1). That is worth pinning: it is why a payload needs its own length recorded alongside it rather than relying on the element length.

Full suite: 26 suites / 366 tests.

Not included

This does not touch Tag#isPrivateCreator(), which accepts elements 0x01–0x0F as creators and so disagrees with the block range used here. That is #356, and it deserves its own change because it alters how existing files parse.

Disclosure

AI-assisted (Claude), per the process in #517. Motivated by needing to write a reserved private block in a downstream project, but the API is general. Pairs naturally with #625 (registering such a block's tags by name), though neither depends on the other.

Writing a private element meant hand-managing the reservation: pick a
block, hope nothing else has it, write the creator element yourself, and
build each tag by string concatenation.

    dicomDict.upsertTag("4D430010", "LO", ["ACME 1.0"]);
    dicomDict.upsertTag("4D431001", "CS", ["HYBRID"]);

That hard-codes the block number, which is the one thing a caller must not
assume: which block a creator holds is decided by whoever wrote the file.
A reader that looks at (4D43,1001) because the writer happened to use
block 0x10 reads the wrong element as soon as the file came from somewhere
that used 0x11.

    // writing - the block is allocated, not chosen
    const block = dicomDict.privateBlock(0x4d43, "ACME 1.0");
    block.set(0x01, "CS", ["HYBRID"]);

    // reading, from any parsed dict - found by creator, never by block
    const block = findPrivateBlock(dict, 0x4d43, "ACME 1.0");
    block && block.get(0x01).Value;

findPrivateBlock() takes a plain dict, because that is what a reader has
after DicomMessage.readFile(); DicomDict#privateBlock() is the writing
side and reserves a block when the creator has none. Reservation finds the
creator if it is already there, otherwise takes the lowest free block, so
blocks another writer holds are left alone. Creator values are compared
trimmed, since LO pads an odd length with a space.

Validation follows the standard rather than the previous ad hoc checks: an
even group is rejected (private data elements are in odd groups), blocks
are 0x10 to 0xFF (0x00 to 0x0F are reserved), elements are 0x00 to 0xFF
within the block, and a creator must be a non-empty string within LO's 64
characters.

Note this does not touch Tag#isPrivateCreator(), which accepts elements
0x01 to 0x0F as creators and so disagrees with the block range used here -
that is #356, and worth its own change since it alters how existing files
parse.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@netlify

netlify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for dcmjs2 ready!

Name Link
🔨 Latest commit 42bc50e
🔍 Latest deploy log https://app.netlify.com/projects/dcmjs2/deploys/6af5d215c22c72e28798b852
😎 Deploy Preview https://deploy-preview-626--dcmjs2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

wayfarer3130 and others added 2 commits November 12, 2026 09:29
- The accessor gives key, group, creator and reservedBlock, as the 1.0
  naturalized block does. Calls accept the block key or (group, creator),
  and an element as 0x00-0xFF or "xEE".
- A line break in the creator is the text \n (and \r) in the block key.
- A block that holds elements with no creator, or an empty creator, is
  not free.
- The creator match strips leading spaces and trailing spaces and NULs,
  and reads a creator held as bytes (UN) as Latin-1.
- set() adds the tag accessors, as upsertTag does.
- Export privateBlockKey in place of privateTag.
- Remove the two OB writer tests; they test the Part 10 writer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@wayfarer3130 wayfarer3130 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made some changes so that this is compatible with 1.0-beta, and am creating the equivalent PR there with the same test code.

@wayfarer3130

Copy link
Copy Markdown
Contributor

@pieper - I also created #641 to match this PR. From my point of view I'm ok wiht the changes now so that both 0.x and 1.x are compatible, but if you review the changes and then merge if ready.

@pieper

pieper commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks @wayfarer3130 - I'll be tied up for a week or so but hope to catch up later this month.

@wayfarer3130
wayfarer3130 merged commit 9368b00 into master Nov 24, 2026
20 checks passed
@wayfarer3130
wayfarer3130 deleted the private-block-api branch November 24, 2026 16:48
wayfarer3130 added a commit that referenced this pull request Nov 24, 2026
The 1.0 port of #626. findPrivateBlock() and reservePrivateBlock() in
@dcmjs-org/core, and DicomDict#privateBlock() in legacy, name a block by
the naturalized block key "gggg:creator" (or group and creator) and an
element by "xEE" (or its number). They reuse creatorText and
firstFreeBlock of privateBlocks.js.

privateBlockKey() now builds every block key: a line break in the
creator is the text \n (and \r) in the key, and parsePrivateBlockKey
reverses it. The #639 tests expect the escaped key.

test/privateBlock.test.js is the same file as on master. NAT-U-PRIV-1
gets the escape rule, NAT-U-PRIV-11 is new, and the dcmjs.js bundle
ceiling moves to x1.075.

Co-authored-by: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.53.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

wayfarer3130 added a commit that referenced this pull request Nov 24, 2026
Resolve the conflict in src/index.js: keep both export sets in the data
namespace, dictionary and registerTag from this branch, and
findPrivateBlock, reservePrivateBlock and privateBlockKey from #626.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
wayfarer3130 added a commit that referenced this pull request Nov 24, 2026
registerTag() no longer replaces a name that nameMap already has for a
different tag (a standard keyword or an earlier registration). The
existing entry wins, as in 1.x, and a warning names both tags. One
helper, addRegisteredToNameMap(), applies the rule at load and on each
later registerTag() call.

The registerTag() doc comment now says that a private element registered
by position assumes block 0x10, and points to the private-block
functions of #626.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
wayfarer3130 added a commit that referenced this pull request Nov 24, 2026
* fix(dictionary): keep nameMap current when a tag is registered

registerTag() added an entry to the dictionary but not to
DicomMetaDictionary.nameMap, which is what denaturalizeDataset() looks a
name up in. So a registered tag could be read but never written: the
value was dropped with nothing but a "[warn] Unknown name in dataset",
which is why this was easy to miss.

nameMap could not simply be regenerated either. It is built once at
module load, and the loop that was meant to pick up dictionary additions
walks Object.keys(dictionary) - which sees only entries assigned directly
onto the Proxy, never anything registerTag() put in the module's cache.
That loop stays, because assigning onto the dictionary is the documented
custom dictionary pattern (#215) and a test covers it; registered entries
are now contributed alongside it, from a map kept for the purpose, and a
registrar callback keeps nameMap current for anything registered later.

Two further fixes needed to make the feature usable:

- Normalize the tag to upper case. getAllStandardTagEntries() carries
  upper case, and Tag.toCleanString() upper-cases when reading a file, so
  a tag registered in lower case denaturalized to a key no reader ever
  produces. Lookups still accept either case, as they do for the standard
  dictionary.
- Export registerTag and dictionary from index.js. They were reachable
  only by importing src/dictionary.fast.js directly, so the feature was
  not part of the public API at all.

The shipped private dictionary is still kept out of nameMap: those names
are vendor-specific and collide, and writing one because a caller's field
happened to match would be wrong. Entries from registerTag() default to
version "User" and are included.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* fix: a registered name never replaces an existing keyword

registerTag() no longer replaces a name that nameMap already has for a
different tag (a standard keyword or an earlier registration). The
existing entry wins, as in 1.x, and a warning names both tags. One
helper, addRegisteredToNameMap(), applies the rule at load and on each
later registerTag() call.

The registerTag() doc comment now says that a private element registered
by position assumes block 0x10, and points to the private-block
functions of #626.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Bill Wallace <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants