Repository navigation
fix: decode text in sequence items with the right SpecificCharacterSet (sync and async readers) - #609
Merged
wayfarer3130 merged 9 commits intoNov 24, 2026
Conversation
…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]>
✅ Deploy Preview for dcmjs2 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
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]>
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 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]>
This was referenced Nov 24, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…x nested charsets together Co-Authored-By: Claude Opus 5.5 <[email protected]>
wayfarer3130
approved these changes
Nov 24, 2026
|
🎉 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]>
This was referenced Nov 24, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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.AsyncDicomReaderkeeps 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, theDupré^Anaïsof the second item comes back asDuprУЉ^AnaУЏs. An element after the sequence readsContrУД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
ReadBufferStreamstarts withnew TextDecoder("latin1").DicomMessage._readchanges the decoder when it readsSpecificCharacterSet, but only on the stream that it reads.SequenceOfItemsreads each item from a new stream fromBufferStream.more(), and that new stream started again with the default decoder. In the WHATWG Encoding standard,"latin1"is a label for windows-1252. For this reason, Nested sequence items are decoded as cp1252, ignoring SpecificCharacterSet (ISO_IR 192) #508 shows cp1252 characters (€,“) and Sequence-nested string content decoded with Latin-1 instead of SpecificCharacterSet (UTF-8 mojibake) #503 shows Latin-1. The two issues are the same bug.AsyncDicomReaderreads items on the stream of the parent.readSinglechanges the decoder of that stream when it reads (0008,0005). Nothing changed the decoder back when the item ended.Changes
ReadBufferStreamaccepts adecoderoption. The default islatin1, 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 aUNelement again from a new stream when its tag has a known VR, so aUN-encodedLOalso came back wrong.ParsedUnknownValue.read()now also gives the decoder to the new stream._readstill callssetDecoderon the stream that it reads. An item that declares aSpecificCharacterSetchanges the decoder for that item and its nested items only, not for its parent or for the later items.AsyncDicomReader.readSequencesaves 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 underISO_IR 192:UN-encodedLO. Onmaster, both tests fail on the value (Received: "lesione â café è"). With the fix, the tests pass.ScheduledProcedureStepSequenceitem declaresISO_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 nestedCodeMeaning(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Рентген.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 throughfromAsyncStream. A 2 KB comment after the sequence makes the reader decode the items before the last pieces arrive. Onmaster, the test fails on the second item and on the element after the sequence. With the fix, the test passes.more(): the depth and worklist tests fail in all four encodings.UN: only theUNtest fails.origin/masterinto 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 testpassed 387 tests.pnpm run build, eslint andprettier --checkalso passed.Risk and revert
1.x
origin/1.0-betahas both bugs:packages/core/src/BufferStream.jsmore(), andpackages/core/src/ValueRepresentation.jsnear L1545, do not pass the decoder.packages/legacy/src/AsyncDicomReader.jsreadSequencedoes 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