Skip to content

fix(VR): format DS numbers to 16 characters instead of cutting off the exponent - #654

Merged
wayfarer3130 merged 6 commits into
1.0-betafrom
fix/1.0-beta-ds-format
Nov 25, 2026
Merged

wayfarer3130 merged 6 commits into
1.0-betafrom
fix/1.0-beta-ds-format

Conversation

@wayfarer3130

@wayfarer3130 wayfarer3130 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR is the 1.0 port of #610 (commit aee06e4, fixes #531), with the round-trip test of #624. denaturalizeDataset now writes a DS number in 16 characters or less, and it keeps the exponent. Before, String(number) was cut to 16 characters: 3.5163338899999997e-10 became 3.51633388999999.

The 1.x port goes further than 0.x in two cases. Master still has the gap for both cases after #610.

  • A single DS number, for example SliceThickness with VM 1, is also formatted. In 1.x, a VM 1 attribute is a single number, not a list (NAT-U-VAL-1).
  • A Number object is also formatted.

Changes

  • packages/core/src/DicomMetaDictionary.js: one formatting step for DS, with vr.convertToString. The step applies to a single number, to each number in a list, and to a Number object. The JSDoc cites PS3.5 Table 6.2-1.
  • docs/specs/naturalized-dataset.md: new user requirement NAT-U-RT-8. A DS number, single or in a list, is written in 16 characters or less, and keeps the exponent. The rule cites PS3.5 Section 6.2, Table 6.2-1.
  • beta-1-release-notes.md: one bullet.
  • test/bundle-size.test.js: the ceilings increase to 1.084 (dcmjs.js) and 1.066 (dcmjs.es.js), measured after the merge of 1.0-beta (×1.0830 and ×1.0655).

Review

An independent review compared the port with #610 and #624. fromNaturalized calls denaturalizeDataset, so the fix also covers that path. A value that is read, edited, and written back already goes through the DS writer, which formats the number. The review found the single-number gap (S1) and the Number object gap (N1). This PR fixes both.

Tests

test/issues/issue531-ds-exponent.test.js has the #610 test, the #624 round trip, and one new test for the single number and the Number object.

  • On 1.0-beta without the fix, the tests fail. For example, the round trip gives _rawValue "3.51633388999999", not "3.5163338900e-10".
  • After the fix, the tests pass, and the full suite passes.

The API must do this:

  • denaturalizeDataset formats a DS number in a list, a single DS number, and a Number object to 16 characters or less.

The user experience must have this:

  • A DS value with an exponent writes, reads back, and keeps the exponent (3.5163338900e-10).

🤖 Generated with Claude Code

wayfarer3130 and others added 4 commits November 24, 2026 15:04
…e exponent

1.0 port of #610 (commit aee06e4), with the round trip test of #624.

denaturalizeDataset in packages/core now formats each number of a DS
array with DecimalString.convertToString before denaturalizeValue, so
3.5163338899999997e-10 is written as 3.5163338900e-10 and not cut to
3.51633388999999.

Both tests go in test/issues/issue531-ds-exponent.test.js (#610 fixes
#531).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review items S1 and N1 of the 1.0 port of #610. The 1.x port now also
formats the scalar case: a VM 1 DS attribute, for example
SliceThickness or SliceLocation, is a single number in a 1.x
naturalized dataset, and it went through String() and was cut to 16
characters. A Number object (new Number(...)) is formatted the same way.
One formatDs function serves the scalar and the list form.

master (0.x) has the same scalar gap after #610.

The JSDoc of denaturalizeDataset cites PS3.5 Table 6.2-1. The
bundle-size ceilings move to 1.081 and 1.063 for this change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Maintainer decision Q2 of the review of the 1.0 port of #610: NAT-U-RT-8 in docs/specs/naturalized-dataset.md, with the PS3.5 Section 6.2, Table 6.2-1 reference and the test file, and one bullet in beta-1-release-notes.md. No NAT-I rule covers the DS write, so no NAT-I rule changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The rebase onto 1.0-beta adds the declared length clamp of #652 to the DS formatting of this branch: dcmjs.es.js is x1.0632.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wayfarer3130
wayfarer3130 marked this pull request as ready for review November 24, 2026 20:18
wayfarer3130 and others added 2 commits November 24, 2026 20:33
…ormat

# Conflicts:
#	beta-1-release-notes.md
#	test/bundle-size.test.js
…ormat

test/bundle-size.test.js: kept the 1.0-beta ceilings. The measured
ratios after the merge are dcmjs.js 1.08400 and dcmjs.es.js 1.06658,
which fit under the 1.0-beta ceilings 1.085 and 1.067.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wayfarer3130
wayfarer3130 merged commit c836e51 into 1.0-beta Nov 25, 2026
4 checks passed
@wayfarer3130
wayfarer3130 deleted the fix/1.0-beta-ds-format branch November 25, 2026 17:40
wayfarer3130 added a commit that referenced this pull request Nov 25, 2026
…rge of 1.0-beta

dcmjs.js is 1,509,340 bytes (ratio 1.09022) and dcmjs.es.js is
1,456,286 bytes (ratio 1.07284) after the merge of #654, #656 and #647.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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 📦🚀

wayfarer3130 added a commit that referenced this pull request Nov 25, 2026
…t of #622) (#663)

* fix: byte order of binary value fields in Explicit VR Big Endian (port of #622)

Explicit VR Big Endian stores the words of OW, OF and OD (and OL / OV)
value fields most significant byte first. dcmjs 1.0 gave them to the
caller, and wrote them, in the order of the caller's memory.

- core: swapBytes / swappedCopy (utilities/byteSwap.js, exported);
  BinaryRepresentation.wordWidth; swap on read, swap a copy on write;
  ParsedUnknownValue re-reads in the right byte order;
  BufferStream.readUint16Array off-by-one.
- legacy: AsyncDicomReader cuts fragments to whole words and swaps them,
  also on the bit-packed path; a UN "ox" pixel data uses the OW width.
- The event-stream parser and StreamingPart10Writer go through core.
- test/big-endian.test.js: independent encoder; every reader and the
  writer, plus the CT1_UNC big endian / little endian fixture pair.
- bundle-size ceilings 1.085 / 1.068.

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

* fix(async): swap Big Endian OW pixel data before it is cut into frames

When a frame of OW pixel data is not a whole number of words (8-bit data
stored as OW, odd samples per frame), a word spans two frames. The async
reader cut the value into frames first and swapped each frame alone, so it
mixed the bytes of the two frames. It now reads and swaps the whole value
first and then cuts it into frames, as the 1-bit path already does.

The big endian test now runs for Explicit VR Big Endian only, with one
little endian write test as a control, and checks the odd frame case on
DicomMessage, AsyncDicomReader, fromPart10 and fromPart10Stream.

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

* docs: little endian binary values in memory for every transfer syntax; bundle-size ceilings

Add NAT-U-BIN-7 and NAT-I-BIN-4 to the naturalized dataset spec, a
MIGRATION.md note for callers that swapped the bytes themselves, and the
raw byte order and maxFragmentSize rounding to the AsyncDicomReader doc.
Set the bundle-size ceilings to the measured ratios (x1.08949, x1.07200).

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

* fix(async): swap expectsRaw OW/OF/OD words to little endian in Big Endian

AsyncDicomReader.readRawBinary now passes the word width of the VR to
_emitSplitValues, so a listener that returns expectsRaw gets OW, OF, OL,
OD and OV words as little endian, the same as on the other read paths.
OB, UN and other VRs keep the byte order of the file.

Use one VR list in NAT-U-BIN-7, MIGRATION.md and the AsyncDicomReader
skill doc: OW, OF and OD, and OL and OV where the VR is supported.

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

* test(bundle-size): raise the ceilings to 1.091 and 1.073 after the merge of 1.0-beta

dcmjs.js is 1,509,340 bytes (ratio 1.09022) and dcmjs.es.js is
1,456,286 bytes (ratio 1.07284) after the merge of #654, #656 and #647.

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

* fix(VR): give a VR with no registered class a word width of 1

A UN element whose dictionary VR is OV (or OL) with no registered VR
class, for example (7FE0,0001), now reads the same with DicomMessage,
fromPart10 and AsyncDicomReader: BinaryRepresentation.wordWidth is 1
for a type that Vrs does not hold, so no reader swaps it.

Docs: NAT-I-BIN-4 states this rule; NAT-U-BIN-7, MIGRATION.md and the
AsyncDicomReader skill say that a UN element whose dictionary VR is
OW, OF or OD reads as that VR and gets the swap. Fix the readRawBinary
doc comment (fragments of maxFragmentSize, not 64KB chunks) and rewrap
a long MIGRATION.md line.

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

* test(big-endian): read explicit OL and OV, and UN with dictionary VR OV, the same with every reader

With the OL and OV VR classes of #653, DicomMessage, fromPart10 and
AsyncDicomReader (normal and expectsRaw) give little endian words for
an explicit OL or OV value and for a UN value whose dictionary VR is
OV. Drop the stale "no OL VR class" sentence of the dataset comment.

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

* fix(VR): drop the Vrs check from wordWidth; name OL and OV in the swap docs

After #653, every key of WORD_WIDTHS has a built-in VR class, so the
Vrs check in BinaryRepresentation.wordWidth had no effect. NAT-I-BIN-4 now
says that a code not in WORD_WIDTHS, also one that registerVr adds, has a
width of 1. NAT-U-BIN-7, MIGRATION.md and the AsyncDicomReader skill doc
name OW, OF, OD, OL and OV for the swap and for the UN rule, and item 7 of
the skill doc lists SV and UV.

Tests: UN rows for OW (0028,1201) and OF (0066,0016), fromPart10Stream
in the every-reader check, SV in LENGTH_32_VRS, and a corrected comment
for (0066,0040).

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

---------

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant