Skip to content

fix: decode text in sequence items with the right SpecificCharacterSet (port of #609) - #659

Merged
wayfarer3130 merged 2 commits into
1.0-betafrom
fix/1.0-beta-port-609-item-charset
Nov 25, 2026
Merged

wayfarer3130 merged 2 commits into
1.0-betafrom
fix/1.0-beta-port-609-item-charset

Conversation

@wayfarer3130

Copy link
Copy Markdown
Contributor

This PR is the 1.0 port of #609 from 0.x (which contains #520).

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 in a sequence item applies only to that item and to the items nested in it. origin/1.0-beta had the two bugs that #609 fixed in 0.x:

  1. Sync reader (DicomMessage.readFile). With SpecificCharacterSet = ISO_IR 192, text in sequence items came back as windows-1252. "lesione – café 肝" came back as "lesione – café è‚".
  2. Async reader (AsyncDicomReader). The reader kept the character set of an item after the item ended. The next items and the elements after the sequence decoded with the character set of the item.

Cause

  • Sync reader. ReadBufferStream starts with new TextDecoder("latin1"). BufferStream.more() makes a new stream for each item, and that new stream started again with the default decoder. ParsedUnknownValue.read() reads a UN element again from a new stream, and had the same problem.
  • Async reader. AsyncDicomReader reads items on the stream of the parent. When the reader reads (0008,0005) in an item, it changes the decoder of that stream. Nothing set the decoder back when the item ended.
  • Event-stream reader. fromPart10Stream gives the decoder to each item as a value (parseSqItems, parseItemElements). Thus the event-stream reader already used the correct character set for items, and an item charset did not go out of the item. The event-stream reader had only the UN bug, because it uses ParsedUnknownValue.read() for a UN element with a known dictionary VR.

Changes

  • packages/core/src/BufferStream.js:
    • 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.
  • packages/core/src/ValueRepresentation.js: ParsedUnknownValue.read() gives the decoder of the stream to the new stream. This change fixes the sync reader and the event-stream reader.
  • packages/legacy/src/AsyncDicomReader.js: readSequence saves the decoder of the stream before each item, and sets the decoder again after the item. A nested item still gets the character set of its parent item.
  • test/data-encoding.test.js: the tests of fix: decode text in sequence items with the right SpecificCharacterSet (sync and async readers) #609, adapted to 1.x. Each test runs on three readers: DicomMessage.readFile, AsyncDicomReader, and fromPart10 with NaturalizedListener. The depth test and the worklist test read four encodings: Explicit VR and Implicit VR Little Endian, each with undefined lengths and with defined lengths. The UN test reads Explicit VR with undefined lengths.
    • Text in sequence items at depth 1 and 2.
    • A UN-encoded LO.
    • A worklist data set in ISO_IR 192 whose first ScheduledProcedureStepSequence item declares ISO_IR 144. The test checks the text before the sequence, in the first item, in its nested item, in the second item, and after the sequence.
  • test/async-sequence-charset.test.js: the async test of fix: decode text in sequence items with the right SpecificCharacterSet (sync and async readers) #609, without changes. The test reads the worklist data set from one buffer and from 7-byte pieces through fromAsyncStream.
  • test/bundle-size.test.js: the ceilings change from 1.083 to 1.084 (dcmjs.js) and from 1.065 to 1.066 (dcmjs.es.js). The fix adds about 0.5 KB of documented source. The ratios were 1.0831 and 1.0656, so the old ceilings failed.

Verified

  • Before the fix, the new tests had 18 failures and 19 passes:
    • DicomMessage.readFile: the depth test, the UN test and the worklist test failed in all encodings (Received: "lesione – café è‚").
    • AsyncDicomReader: the worklist test failed in all four encodings, and the test of test/async-sequence-charset.test.js failed in all four cases.
    • fromPart10 event stream: only the UN test failed. The depth test and the worklist test passed.
  • With the fix, npx jest --runInBand passed on these files: test/data-encoding.test.js, test/async-sequence-charset.test.js, test/readBufferStream.test.js, test/SequenceOfItems.test.js (64 tests). It also passed on test/charset, test/sync-async-parser-equivalency.test.js, test/defined-length-sequence.test.js, test/eventStream/fromPart10Stream.test.js, test/eventStream/NaturalizedListener.test.js and test/writeBufferStream.test.js (144 passed, 1 skipped). test/bundle-size.test.js passed with the new ceilings (7 tests).
  • pnpm run build, eslint and prettier --check passed on the changed files.
  • I did not run the full suite locally. CI runs the full suite on this PR.

Release note

A file that declares UTF-8 but stores windows-1252 bytes in its sequence items now shows replacement characters (U+FFFD) in those items. Before this change, the sync reader decoded these bytes correctly by accident.

Not ported, and why

🤖 Generated with Claude Code

…t (port of #609)

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]>
wayfarer3130 added a commit that referenced this pull request Nov 25, 2026
…662

The combined build measures x1.0841 (dcmjs.js) and x1.0665 (dcmjs.es.js).
The ceiling comment is now one line.

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

* fix(normalizers): normalize multiframe datasets missing functional groups (port of #603)

normalizeMultiframe dereferenced a missing SharedFunctionalGroupsSequence
and threw a TypeError. When the groups are missing, fill the shared
PlaneOrientationSequence / PixelMeasuresSequence and the per-frame
PlanePositionSequence from the top-level attributes, through the static
ImageNormalizer.sharedImagePlaneGroups and planePositionGroup that
convertToMultiframe now also uses. Lower NumberOfFrames to the whole
frames PixelData holds (never raise it), with a warning. A per-frame
sequence that is already an array is no longer wrapped again, so a
one-frame item is an object and not [[item]]. log.error becomes log.warn.

Raise the bundle-size ceilings to 1.084 and 1.067 for the ~1.7 KB added.

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

* fix(normalizers): keep the declared NumberOfFrames when functional groups are missing

The normalizer no longer lowers NumberOfFrames to the frames that PixelData
appears to hold. Encapsulated PixelData can have more or fewer fragments than
frames, so a byte count says nothing about the frame count. framesInPixelData
is removed.

A dataset with no PerFrameFunctionalGroupsSequence now gets one per-frame item
for each declared frame, each with PlanePositionSequence from the top-level
ImagePositionPatient and its own FrameContentSequence.

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

* test(bundle-size): set the ceilings from a build with #660, #659 and #662

The combined build measures x1.0841 (dcmjs.js) and x1.0665 (dcmjs.es.js).
The ceiling comment is now one line.

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@wayfarer3130
wayfarer3130 marked this pull request as ready for review November 25, 2026 14:08
…-609-item-charset

test/bundle-size.test.js: kept the 1.0-beta ceilings. The measured
ratios after the merge are dcmjs.js 1.08412 and dcmjs.es.js 1.06656,
which round up to the 1.0-beta ceilings 1.085 and 1.067.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wayfarer3130
wayfarer3130 merged commit dd67f74 into 1.0-beta Nov 25, 2026
4 checks passed
@wayfarer3130
wayfarer3130 deleted the fix/1.0-beta-port-609-item-charset branch November 25, 2026 16:54
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.0.0-beta.19 🎉

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