Skip to content

fix: decode text in sequence items with the right SpecificCharacterSet (sync and async readers) - #609

Merged
wayfarer3130 merged 9 commits into
dcmjs-org:masterfrom
namespaceMarcello:fix/async-item-charset
Nov 24, 2026
Merged

wayfarer3130 merged 9 commits into
dcmjs-org:masterfrom
namespaceMarcello:fix/async-item-charset

Conversation

@namespaceMarcello

@namespaceMarcello namespaceMarcello commented Oct 17, 2026 •

Copy link
Copy Markdown

Fixes #503
Fixes #508

This PR now contains #520. The two fixes must merge together: #520 fixes the sync reader (DicomMessage.readFile), and this PR fixes the async reader (AsyncDicomReader). Each fix alone leaves the two readers with different results. #520 is closed in favour of this PR.

Summary

PS3.5 7.5.3 says that a Specific Character Set applies to the data set and to the items nested in it. A Specific Character Set declared in a sequence item applies only to that item and the items nested in it. dcmjs 0.x had two bugs:

  1. Sync reader (from fix: decode text in sequence items with the dataset's SpecificCharacterSet #520). With SpecificCharacterSet = ISO_IR 192, text at the top level reads back correctly, but text inside a sequence item does not. "lesione – café 肝" comes back as "lesione â café è". This bug affects Structured Reports (ContentSequence), SEG segment labels and RTSTRUCT ROI names. A write of the dataset makes the damage permanent.
  2. Async reader. AsyncDicomReader keeps the character set of an item after the item ends. The next items and the rest of the data set decode with the character set of the item. In a data set in ISO_IR 192 whose first item declares ISO_IR 144, the Dupré^Anaïs of the second item comes back as DuprУЉ^AnaУЏs. An element after the sequence reads ContrУДle aprУЈs opУЉration.

@hromsdahl (#503) and @rkikinis (#508) both found the sync bug. @pieper agreed on #503 that dcmjs must fix it. The sync fix uses the approach from #503: the sub-stream gets the decoder of the parent stream. The answer to the question in #508: the parser passes the character set down through the sequence parse. The naturalize step does not resolve the character set for each item.

Cause

Changes

  • ReadBufferStream accepts a decoder option. The default is latin1, as before.
  • BufferStream.more() gives its decoder to the new stream. Items at all depths get the decoder.
  • ParsedUnknownValue.read() had the same problem. The parser reads a UN element again from a new stream when its tag has a known VR, so a UN-encoded LO also came back wrong. ParsedUnknownValue.read() now also gives the decoder to the new stream.
  • _read still calls setDecoder on the stream that it reads. An item that declares a SpecificCharacterSet changes the decoder for that item and its nested items only, not for its parent or for the later items.
  • AsyncDicomReader.readSequence saves the decoder of the stream before each item, and sets it again after the item. A nested item still gets the character set of its parent item, because the reader reads the nested item while the decoder of the parent item is in place.

Verified

  • test/data-encoding.test.js (sync reader), all under ISO_IR 192:
    • Text in sequence items at depth 1 and 2, and a UN-encoded LO. On master, both tests fail on the value (Received: "lesione â café è"). With the fix, the tests pass.
    • A worklist data set whose first ScheduledProcedureStepSequence item declares ISO_IR 144. The text of that item is in ISO 8859-5 bytes. One comparison checks the patient name before the sequence, the physician name of the first item and its nested CodeMeaning (Cyrillic), the physician name of the second item, and an element after the sequence (UTF-8). Without the fix, the nested value comes back as ÀÕÝâÓÕÝ, not Рентген.
    • The depth tests and the worklist test read the data in four ways: Explicit VR and Implicit VR Little Endian, each with undefined lengths and with defined lengths.
  • test/async-sequence-charset.test.js (async reader): the same worklist data set, read in four ways. The test reads the data with undefined lengths and with defined lengths, each from one buffer and from 7-byte pieces through fromAsyncStream. A 2 KB comment after the sequence makes the reader decode the items before the last pieces arrive. On master, the test fails on the second item and on the element after the sequence. With the fix, the test passes.
  • Each new assertion failed alone when the related part of the fix was removed:
    • The decoder passed only one level down: only the depth-2 assertion fails.
    • No decoder in more(): the depth and worklist tests fail in all four encodings.
    • No decoder for UN: only the UN test fails.
    • One restore for each sequence and not for each item: the second-item value fails in all four async cases.
  • On 2026-11-24, a maintainer merged origin/master into each branch, and then merged fix: decode text in sequence items with the dataset's SpecificCharacterSet #520 into this branch. The merges had no conflicts. With both fixes, pnpm test passed 387 tests. pnpm run build, eslint and prettier --check also passed.

Risk and revert

  • Only text in sequence items decodes differently.
  • Release note: a file that declares UTF-8 but has windows-1252 bytes in its items now shows replacement characters (U+FFFD) in those items. Before this change, the sync reader decoded these bytes correctly by accident.
  • To revert, revert the merge commit of this PR.

1.x

origin/1.0-beta has both bugs:

  • packages/core/src/BufferStream.js more(), and packages/core/src/ValueRepresentation.js near L1545, do not pass the decoder.
  • packages/legacy/src/AsyncDicomReader.js readSequence does not restore the decoder.

A separate PR must port both fixes to 1.x.


Written by Claude Code (AI assistant) on behalf of @namespaceMarcello, who directs this work. Updated by Claude Code for a maintainer to include #520.

🤖 Generated with Claude Code

namespaceMarcello and others added 4 commits September 25, 2026 23:44
…erSet

ReadBufferStream starts with TextDecoder("latin1"), and more() creates a new
ReadBufferStream for every sequence item without passing the parent's decoder.
Text inside sequences (SR ContentSequence, SEG segment labels, RTSTRUCT ROI
names) was therefore decoded as windows-1252, which is what "latin1" means for
TextDecoder, even when SpecificCharacterSet is ISO_IR 192. Writing the dataset
back made the damage permanent.

more() now passes its decoder to the new stream. ParsedUnknownValue had the
same problem for UN elements re-read with their dictionary VR, and now passes
the decoder too.

Fixes dcmjs-org#503
Fixes dcmjs-org#508

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TWHfLbjyVsKtycUjne5QSs
PS3.5 7.5.3: a Specific Character Set in an item applies only to that item
and the items nested in it. The new test reads a worklist-shaped data set in
ISO_IR 192 whose first scheduled step declares ISO_IR 144, with that item's
text in ISO 8859-5 bytes, and checks the five values that tell the cases
apart: the item itself and its nested item in Cyrillic, the next item and the
element after the sequence still in UTF-8.

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

PS3.5 7.5.3: a Specific Character Set in an item applies only to that item
and the items nested in it. AsyncDicomReader reads items on the parent's
stream, and readSingle switches that stream's decoder when it meets
(0008,0005). Nothing switched it back when the item ended, so the next items
and the rest of the data set were decoded with the item's character set.

readSequence now puts back, after each item, the decoder the item started
with.

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

netlify Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for dcmjs2 ready!

Name Link
🔨 Latest commit 56c6e5d
🔍 Latest deploy log https://app.netlify.com/projects/dcmjs2/deploys/6b05f5a301724bdb9336e9da
😎 Deploy Preview https://deploy-preview-609--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.

@stevewranovsky stevewranovsky left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good!

The test now reads the same worklist four ways: with the undefined
lengths dcmjs writes and with every sequence and item rewritten to a
defined length, each from one buffer and from 7-byte pieces fed through
fromAsyncStream. A long comment after the sequence keeps the reader
working before the last pieces arrive, since it waits for 1 KB ahead.

Without the restore all four cases fail; reading defined-length items
to the end of the sequence fails only the two defined-length cases.

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

Copy link
Copy Markdown
Author

Since this review I pushed 26320d8, which only touches the test. The same worklist is now also read with every sequence and item at a defined length, and fed in 7-byte pieces through fromAsyncStream, so both ways an item can end and input that arrives while it is being read are covered. The fix in src/AsyncDicomReader.js is unchanged; the description is updated to match.


Written by Claude Code (AI assistant) on behalf of @namespaceMarcello, who directs this work.

The sub-stream and worklist tests now read their data four ways: in
Explicit and Implicit VR Little Endian, each with the undefined lengths
dcmjs writes and with every sequence and item rewritten to a defined
length. Both kinds of item reach the same more() call in
SequenceOfItems.readBytes.

Without the decoder in more() all eight cases fail; reading
defined-length items two bytes short fails only the four
defined-length cases.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wayfarer3130 wayfarer3130 changed the title fix: scope a SpecificCharacterSet declared in a sequence item to that item fix: decode text in sequence items with the right SpecificCharacterSet (sync and async readers) Nov 24, 2026
@wayfarer3130
wayfarer3130 merged commit 9c1ad94 into dcmjs-org:master Nov 24, 2026
8 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.53.1 🎉

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 ParsedUnknownValue.read(): the re-read stream
keeps the byte order fix of this branch (bufferIsLittleEndian) and also
gets the decoder of the outer stream (from #609).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
wayfarer3130 added a commit that referenced this pull request Nov 25, 2026
…t (port of #609) (#659)

PS3.5 7.5.3: items use the character set of the data set that contains
them, and a character set declared in an item applies only to that item
and the items nested in it.

- ReadBufferStream takes a `decoder` option; BufferStream.more() and
  ParsedUnknownValue.read() pass the decoder of the parent stream, so the
  sync reader and the UN re-read decode items with the data set charset.
- AsyncDicomReader.readSequence restores the decoder after each item.
- The event-stream reader already scopes the charset per item; it had
  only the UN re-read bug, which the ParsedUnknownValue change fixes.
- Tests run the sync, async and event-stream readers on Explicit and
  Implicit VR, with undefined and defined lengths.
- bundle-size ceilings 1.084 and 1.066 for the added source.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

3 participants