Skip to content

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

Merged
wayfarer3130 merged 13 commits into
1.0-betafrom
fix/1.0-beta-port-622-big-endian-swap
Nov 25, 2026
Merged

wayfarer3130 merged 13 commits into
1.0-betafrom
fix/1.0-beta-port-622-big-endian-swap

Conversation

@wayfarer3130

@wayfarer3130 wayfarer3130 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

This PR is the 1.0 port of #622 from 0.x.

Summary

Explicit VR Big Endian (1.2.840.10008.1.2.2, a retired transfer syntax) stores each word of a value field with the most significant byte first (PS3.5 Section 7.3). dcmjs 1.0 did not swap the bytes of OW, OF, and OD values. The readers gave wrong pixel data, LUT data, and point coordinates, and the writer wrote wrong bytes. This PR swaps the bytes on every read path and on the write path. The in-memory form of a value is little endian, because JavaScript typed arrays are little endian on all supported platforms. The code does no host detection.

Cause

packages/ had no byte swap. The readers copied the value field in the file byte order, and the writer copied the caller bytes as they were. The existing big endian equivalence tests compare two readers that both had the error, so the tests passed.

Changes

@dcmjs-org/core:

  • New src/utilities/byteSwap.js with swapBytes (in place) and swappedCopy (on a copy). src/index.js exports both functions.
  • BinaryRepresentation gets a wordWidth getter from the table { OD: 8, OF: 4, OL: 4, OV: 8, OW: 2 }. OB and UN have a word width of 1, so dcmjs does not swap them. A VR code that the table does not hold, also a code that registerVr adds, has a word width of 1.
  • BinaryRepresentation.readBytes swaps the value field when the stream is big endian. getBuffer gives a copy, so the source buffer does not change.
  • BinaryRepresentation.writeBytes swaps a copy, so the value of the caller does not change.
  • The swap applies only to native data. Encapsulated pixel data has no swap.
  • ParsedUnknownValue.read (an explicit UN element with a dictionary VR) sets the byte order of the second read. Before this PR, the second read was always little endian, so a big endian UN with the dictionary VR US gave 4096 for 16.
  • BufferStream.readUint16Array: the off-by-one fix of fix: byte order of binary value fields in Explicit VR Big Endian #622. The method has no callers.

@dcmjs-org/legacy:

  • AsyncDicomReader._emitSplitValues cuts each fragment to whole words and swaps each fragment. Without the cut, a maxFragmentSize of an odd number splits a word between two fragments.
  • readUncompressedBitFrame (BitsAllocated 1) swaps the full value before it unpacks the bits.
  • readUncompressed: when a frame is not a whole number of words, a word spans two frames. An example is 8-bit data in OW with an odd number of samples in each frame. For a big endian stream, the reader now reads and swaps the whole value first, and then cuts the value into frames. The 1-bit path uses the same approach. Before this change, the reader swapped each frame alone, and it mixed the bytes at each frame boundary. fix: narrow the UN sequence detection and keep Big Endian items (follow-up to #496) #664 has the same fix for 0.x.
  • readRawBinary (a listener that returns expectsRaw: true) gives the word width of the VR to _emitSplitValues. Thus OW, OF, OL, OD, and OV raw values come as little endian words, the same as on the other read paths. OB, UN, and all other VRs (also US, SS, UL, SL, SV, UV, FL, FD, and AT) come in the byte order of the file. The VR is the VR in tagInfo.vr: an element that the file stores as UN, with the dictionary VR OW, OF, OD, OL, or OV, comes with that VR and with little endian words.
  • A UN pixel data element with the dictionary VR "ox" uses the word width of OW.
  • DicomMessage uses the core read path, so it needs no change.

@dcmjs-org/parser and @dcmjs-org/generator:

  • No change. fromPart10 and fromPart10Stream decode each value with vr.read of core. StreamingPart10Writer writes each native value with writeTagObject of core. The new tests cover both parsers.

Tests and build:

  • New test/big-endian.test.js, ported from fix: byte order of binary value fields in Explicit VR Big Endian #622. The test has an independent explicit VR encoder that makes the expected bytes. The tests run for Explicit VR Big Endian. One little endian write test is the control. The tests cover the write, DicomMessage, AsyncDicomReader (also with maxFragmentSize: 3), fromPart10, fromPart10Stream (in chunks of 3 bytes), the UN second read, and the transcode in the two directions.
  • A new test reads 8-bit OW pixel data with 2 frames of 3x3 pixels (the bytes 1..18) in big endian. The test runs on DicomMessage, AsyncDicomReader, fromPart10, and fromPart10Stream. Each reader must give the bytes 1..18 in order.
  • A new test reads CT1_UNC.explicit_big_endian.dcm and CT1_UNC.explicit_little_endian.dcm from packages/fixtures. The test makes sure that the two files give the same pixel bytes with DicomMessage, AsyncDicomReader, and fromPart10Stream.
  • A new test reads the synthetic big endian file with an expectsRaw listener on AsyncDicomReader. The OW value must come as little endian words, and the OB value must not change.
  • A new test reads (7FE0,0001) UN, (7FE0,0001) OV, (0066,0040) OL, (0028,1201) UN (dictionary VR OW), and (0066,0016) UN (dictionary VR OF) in big endian. The readers are DicomMessage, AsyncDicomReader, AsyncDicomReader with expectsRaw, fromPart10, and fromPart10Stream. Each reader must give the same little endian words.
  • test/bundle-size.test.js: the change adds about 3.4 KB to the unminified bundles. After the merge of 1.0-beta (with fix(legacy): keep untilTag and NumberOfFrames out of AsyncDicomReader sequence items (port of #657) #666), pnpm run build gives dcmjs.js 1,513,938 bytes (ratio 1.09355) and dcmjs.es.js 1,459,480 bytes (ratio 1.07519). The ceilings change from 1.091 to 1.094 and from 1.073 to 1.076.

Documentation:

  • docs/specs/naturalized-dataset.md: new user requirement NAT-U-BIN-7. The bytes of an OW, OF, OD, OL, or OV value are in little endian order for every transfer syntax, also Explicit VR Big Endian. The rule also applies when a caller asks for the raw value bytes. New implementation requirement NAT-I-BIN-4 cites NAT-U-BIN-7 and names byteSwap.js as the one byte swap. NAT-I-BIN-4 also states that a VR code that WORD_WIDTHS does not hold, also a code that registerVr adds, has a wordWidth of 1. NAT-U-BIN-7 also states that a value that the file stores as UN, with the dictionary VR OW, OF, OD, OL, or OV, gets the swap. The test table maps NAT-U-BIN-7 to test/big-endian.test.js.
  • docs/MIGRATION.md: one note about the change of the byte order, with the same VR list, and also for an expectsRaw listener. Code that swapped the bytes of these values itself now gets them swapped two times.
  • docs/AsyncDicomReader-skill.md: raw delivery (expectsRaw, readRawBinary) gives OW, OF, OD, OL, and OV values as little endian words. OB, UN, and other VRs (also SV and UV) come in the byte order of the file. In Explicit VR Big Endian, a fragment is at least one word, so a fragment can be larger than a maxFragmentSize that is smaller than the word width. The constructor comment of maxFragmentSize also states this.

Verified

Before the fix (only the new test file present), npx jest test/big-endian.test.js gave 12 failed and 11 passed of 23 tests. All little endian tests passed. These big endian tests failed:

  • Writer (DicomDict.write): the write test and the two transcode tests.
  • DicomMessage: the read test, the UN test, and the fixture test.
  • AsyncDicomReader: the read test, the test with maxFragmentSize: 3, and the fixture test.
  • fromPart10: the read test.
  • fromPart10Stream: the read test and the fixture test.
  • The big endian round trip test and the "leaves the value untouched" test passed before the fix. Two errors in sequence cancel each other, so these tests are guards only.

I also removed only the whole-word cut from _emitSplitValues. The big endian maxFragmentSize: 3 test then failed.

For the frame fix, the new frame test failed before the fix with AsyncDicomReader only. The reader gave [1,2,3,4,5,6,7,8,10,12,9,14,11,16,13,18,15,17]. DicomMessage, fromPart10, and fromPart10Stream gave the correct bytes.

After the fix (first run, before the review edits):

  • npx jest --runInBand on test/big-endian.test.js, test/lossless-read-write.test.js, test/async-data.test.js, test/sync-async-parser-equivalency.test.js, test/eventStream/fromPart10Stream.test.js, test/eventStream/streamEquivalence.test.js, test/eventStream/StreamingPart10Writer.test.js, test/readBufferStream.test.js, and test/compat-052.test.js: 9 suites pass, 341 tests pass, 1 test is skipped.
  • test/bundle-size.test.js after pnpm run build: 7 of 7 tests pass with the new ceilings.
  • eslint and prettier --check on the changed files: no findings.
  • pnpm run build: passes.
  • I did not run the full jest suite locally. CI runs the full suite.

After the review edits and the merge of 1.0-beta:

  • npx jest --runInBand on test/big-endian.test.js, test/bundle-size.test.js, test/async-data.test.js, test/async-sequence-charset.test.js, test/sync-async-parser-equivalency.test.js, test/readBufferStream.test.js, and test/writeBufferStream.test.js: 7 suites pass, 129 tests pass.
  • test/big-endian.test.js: 19 tests (before the edits: 23 tests).
  • eslint, pnpm run format:check, and pnpm run build: pass.

After the expectsRaw change and the second merge of 1.0-beta:

  • The new expectsRaw test failed before the change to readRawBinary. The two middle LUT words (0x7fff and 0x8000) came with swapped bytes. The test passes after the change.
  • test/big-endian.test.js: 20 tests pass.
  • The full npx jest suite: 122 suites pass, 2 suites are skipped. 1436 tests pass, 11 tests are skipped.
  • test/bundle-size.test.js after pnpm run build: 7 of 7 tests pass with the ceilings 1.091 and 1.073.
  • eslint and prettier --check on the changed files: no findings.

After the review decisions and the merge of 1.0-beta with #653:

  • The new reader agreement test failed before the change to wordWidth. The test passes after the change.

  • test/big-endian.test.js: 23 tests pass.

  • The full npx jest suite: 124 suites pass, 2 suites are skipped. 1449 tests pass, 11 tests are skipped.

  • test/bundle-size.test.js after pnpm run build: 7 of 7 tests pass with the ceilings 1.093 and 1.075.

  • eslint and prettier --check on the changed JavaScript files: no findings.
    After the final review decisions and the merge of 1.0-beta with fix(legacy): keep untilTag and NumberOfFrames out of AsyncDicomReader sequence items (port of #657) #666:

  • test/big-endian.test.js: 25 tests pass.

  • The full npx jest suite (after pnpm run build): 125 suites pass, 2 suites are skipped. 1453 tests pass, 11 tests are skipped.

  • test/bundle-size.test.js: 7 of 7 tests pass with the ceilings 1.094 and 1.076.

  • eslint and prettier --check on the changed JavaScript files: no findings.

Release note

For an Explicit VR Big Endian file, Value[0] of an OW, OF, OD, OL, or OV element now holds little endian words. An AsyncDicomReader listener that asks for raw bytes (expectsRaw) also gets little endian words for these VRs. A big endian write now swaps these values from little endian. A caller that swapped the bytes itself now gets data that is swapped two times, and must remove its own swap. A caller that gave big endian bytes to the writer on purpose must now give little endian bytes.

Not ported, and why

  • OL and OV elements in the main test dataset: the main dataset has no OL or OV element. fix: support the OL, OV and SV value representations #653 added the OL and OV VR classes to 1.0-beta, and a separate test now covers explicit OL and OV values. The UN test checks (0066,0040) as a scalar UL, from the dictionary VR.
  • SV and UV are scalar VRs and use the byte order of the stream already, so they need no swap.

Maintainer edits

  • Merged 1.0-beta into the branch (merge commit, no rebase). Two conflicts: the import list of AsyncDicomReader.js and the ReadBufferStream options in ParsedUnknownValue.read. The merge keeps both sides. test/bundle-size.test.js took the 1.0-beta side, and the ceilings were measured again.
  • Fixed the async reader for big endian OW frames that are not a whole number of words (port of the approach of fix: narrow the UN sequence detection and keep Big Endian items (follow-up to #496) #664), with a test on the four readers.
  • Cut test/big-endian.test.js: the tests run for big endian only, plus one little endian write test as a control. 8 little endian variants that cannot fail from this change are removed. The encoder stays, because the control and the transcode tests use its little endian output. A shared encodePart10 helper now writes the preamble and the meta group. The file has 19 tests (before: 23) and 710 lines (before: 651), because of the new frame test.
  • Added NAT-U-BIN-7 and NAT-I-BIN-4, a MIGRATION.md note, and two notes in docs/AsyncDicomReader-skill.md.
  • Set the bundle-size ceilings to 1.090 and 1.072.
  • Commit 80cf78d (review decision: option B): readRawBinary swaps OW, OF, OL, OD, and OV words in Explicit VR Big Endian. fix: byte order of binary value fields in Explicit VR Big Endian #622 did not change this path. NAT-U-BIN-7, MIGRATION.md, and docs/AsyncDicomReader-skill.md now give the same VR list. One new test covers the expectsRaw path.
  • Merged 1.0-beta again (with fix(VR): format DS numbers to 16 characters instead of cutting off the exponent #654, build(deps): drop the adm-zip override; the devDependency pins 0.6.1 #656 and chore(adapters): mark the adapters export of @dcmjs-org/dcmjs deprecated #647), with no conflicts. Set the bundle-size ceilings to 1.091 and 1.073 from a new measurement.
  • Commit c570c28 (review decisions S1/Q1, N1, N2, N3/Q2): BinaryRepresentation.wordWidth gives 1 for a type with no registered VR class. Before fix: support the OL, OV and SV value representations #653, all readers then read (7FE0,0001) UN with no swap. Before this change, DicomMessage and fromPart10 swapped the value, and AsyncDicomReader did not swap the value. The new test failed before the change. The commit also fixes the readRawBinary doc comment, rewraps a MIGRATION.md line, and adds the UN clause to the docs.
  • Merge commit 12effda: merged 1.0-beta (with fix: support the OL, OV and SV value representations #653). One conflict, in the bundle-size ceilings. The ceilings are now 1.093 and 1.075, from a new measurement.
  • Commit 05bcc5b: with the classes of fix: support the OL, OV and SV value representations #653, the new test covers (7FE0,0001) UN, (7FE0,0001) OV, and (0066,0040) OL. All readers give the same little endian words.
  • Commit 4fe4a25 (final review decisions Q1, Q2, S3, N1 to N4): wordWidth is WORD_WIDTHS[this.type] ?? 1 again. After fix: support the OL, OV and SV value representations #653, every key of WORD_WIDTHS has a VR class, so the Vrs check of c570c28 had no effect. The message of c570c28 says that the check makes the readers agree. On the merged branch, fix: support the OL, OV and SV value representations #653 makes the readers agree, and the check does not. NAT-I-BIN-4 now gives the width rule for a code that WORD_WIDTHS does not hold. NAT-U-BIN-7, MIGRATION.md, and docs/AsyncDicomReader-skill.md name OW, OF, OD, OL, and OV with no qualifier, also for the UN rule. Item 7 of the skill doc lists SV and UV. The test adds two UN rows (dictionary VR OW and OF), reads with fromPart10Stream, adds SV to LENGTH_32_VRS, and fixes the comment for (0066,0040).
  • Merge commit 802c231: merged 1.0-beta (with fix(legacy): keep untilTag and NumberOfFrames out of AsyncDicomReader sequence items (port of #657) #666). Two conflicts: the constructor comments of AsyncDicomReader (the merge keeps both sides), and the bundle-size ceilings. The ceilings are now 1.094 and 1.076, from a new measurement.

🤖 Generated with Claude Code

…t 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]>
wayfarer3130 and others added 10 commits November 25, 2026 09:23
…-622-big-endian-swap

test/bundle-size.test.js: took the 1.0-beta side and set the ceilings
by measurement after the merge: dcmjs.js 1.08595 -> 1.086, dcmjs.es.js
1.06840 -> 1.069.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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]>
…; 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]>
…dian

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]>
…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]>
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]>
…-622-big-endian-swap

# Conflicts:
#	test/bundle-size.test.js
…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]>
wayfarer3130 and others added 2 commits November 25, 2026 14:16
…p 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]>
…-622-big-endian-swap

Brings in #666. Conflicts: the AsyncDicomReader constructor comments (keep both), and the bundle-size ceilings (measured after the build: x1.09355, x1.07519; ceilings 1.094, 1.076).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wayfarer3130
wayfarer3130 marked this pull request as ready for review November 25, 2026 20:21
@wayfarer3130
wayfarer3130 merged commit 6c1a4dd into 1.0-beta Nov 25, 2026
4 checks passed
@wayfarer3130
wayfarer3130 deleted the fix/1.0-beta-port-622-big-endian-swap branch November 25, 2026 20:21
@github-actions

Copy link
Copy Markdown

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

The release is available on:

Your semantic-release bot 📦🚀

wayfarer3130 added a commit that referenced this pull request Nov 26, 2026
…VR (port of #663)

- readRawBinary passes the word width of the resolved VR to
  _emitSplitValues, so an expectsRaw OW/OF/OD/OL/OV value (also a UN
  element with that dictionary VR) is read as little endian words.
  OB and an unresolved UN do not swap.
- A new wordWidth(tagInfo) helper gives the width from tagInfo.vr; the
  pixel data paths use it too, so a UN "ox" Pixel Data is OW.
- Add OV (Other 64-bit Very Long, 8 byte words, 32-bit length). 0.x read
  OV as UN, so an OV value and a UN value with dictionary VR OV (Extended
  Offset Table) were not swapped in Explicit VR Big Endian.

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