Repository navigation
fix: byte order of binary value fields in Explicit VR Big Endian (port of #622) - #663
Merged
Merged
Conversation
…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]>
…-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]>
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]>
…-622-big-endian-swap
…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]>
…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]>
|
🎉 This PR is included in version 1.0.0-beta.21 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
This was referenced Nov 25, 2026
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]>
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.
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:src/utilities/byteSwap.jswithswapBytes(in place) andswappedCopy(on a copy).src/index.jsexports both functions.BinaryRepresentationgets awordWidthgetter 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 thatregisterVradds, has a word width of 1.BinaryRepresentation.readBytesswaps the value field when the stream is big endian.getBuffergives a copy, so the source buffer does not change.BinaryRepresentation.writeBytesswaps a copy, so the value of the caller does not change.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._emitSplitValuescuts each fragment to whole words and swaps each fragment. Without the cut, amaxFragmentSizeof 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 returnsexpectsRaw: 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 intagInfo.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.DicomMessageuses the core read path, so it needs no change.@dcmjs-org/parserand@dcmjs-org/generator:fromPart10andfromPart10Streamdecode each value withvr.readof core.StreamingPart10Writerwrites each native value withwriteTagObjectof core. The new tests cover both parsers.Tests and build:
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 withmaxFragmentSize: 3),fromPart10,fromPart10Stream(in chunks of 3 bytes), the UN second read, and the transcode in the two directions.DicomMessage,AsyncDicomReader,fromPart10, andfromPart10Stream. Each reader must give the bytes 1..18 in order.CT1_UNC.explicit_big_endian.dcmandCT1_UNC.explicit_little_endian.dcmfrompackages/fixtures. The test makes sure that the two files give the same pixel bytes withDicomMessage,AsyncDicomReader, andfromPart10Stream.expectsRawlistener onAsyncDicomReader. The OW value must come as little endian words, and the OB value must not change.DicomMessage,AsyncDicomReader,AsyncDicomReaderwithexpectsRaw,fromPart10, andfromPart10Stream. 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 of1.0-beta(with fix(legacy): keep untilTag and NumberOfFrames out of AsyncDicomReader sequence items (port of #657) #666),pnpm run buildgivesdcmjs.js1,513,938 bytes (ratio 1.09355) anddcmjs.es.js1,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 namesbyteSwap.jsas the one byte swap. NAT-I-BIN-4 also states that a VR code thatWORD_WIDTHSdoes not hold, also a code thatregisterVradds, has awordWidthof 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 totest/big-endian.test.js.docs/MIGRATION.md: one note about the change of the byte order, with the same VR list, and also for anexpectsRawlistener. 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 amaxFragmentSizethat is smaller than the word width. The constructor comment ofmaxFragmentSizealso states this.Verified
Before the fix (only the new test file present),
npx jest test/big-endian.test.jsgave 12 failed and 11 passed of 23 tests. All little endian tests passed. These big endian tests failed: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 withmaxFragmentSize: 3, and the fixture test.fromPart10: the read test.fromPart10Stream: the read test and the fixture test.I also removed only the whole-word cut from
_emitSplitValues. The big endianmaxFragmentSize: 3test then failed.For the frame fix, the new frame test failed before the fix with
AsyncDicomReaderonly. The reader gave[1,2,3,4,5,6,7,8,10,12,9,14,11,16,13,18,15,17].DicomMessage,fromPart10, andfromPart10Streamgave the correct bytes.After the fix (first run, before the review edits):
npx jest --runInBandontest/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, andtest/compat-052.test.js: 9 suites pass, 341 tests pass, 1 test is skipped.test/bundle-size.test.jsafterpnpm run build: 7 of 7 tests pass with the new ceilings.eslintandprettier --checkon the changed files: no findings.pnpm run build: passes.After the review edits and the merge of
1.0-beta:npx jest --runInBandontest/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, andtest/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, andpnpm run build: pass.After the
expectsRawchange and the second merge of1.0-beta:expectsRawtest failed before the change toreadRawBinary. 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.npx jestsuite: 122 suites pass, 2 suites are skipped. 1436 tests pass, 11 tests are skipped.test/bundle-size.test.jsafterpnpm run build: 7 of 7 tests pass with the ceilings 1.091 and 1.073.eslintandprettier --checkon the changed files: no findings.After the review decisions and the merge of
1.0-betawith #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 jestsuite: 124 suites pass, 2 suites are skipped. 1449 tests pass, 11 tests are skipped.test/bundle-size.test.jsafterpnpm run build: 7 of 7 tests pass with the ceilings 1.093 and 1.075.eslintandprettier --checkon the changed JavaScript files: no findings.After the final review decisions and the merge of
1.0-betawith 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 jestsuite (afterpnpm 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.eslintandprettier --checkon 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. AnAsyncDicomReaderlistener 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
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.Maintainer edits
1.0-betainto the branch (merge commit, no rebase). Two conflicts: the import list ofAsyncDicomReader.jsand theReadBufferStreamoptions inParsedUnknownValue.read. The merge keeps both sides.test/bundle-size.test.jstook the1.0-betaside, and the ceilings were measured again.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 sharedencodePart10helper 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.MIGRATION.mdnote, and two notes indocs/AsyncDicomReader-skill.md.80cf78d(review decision: option B):readRawBinaryswaps 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, anddocs/AsyncDicomReader-skill.mdnow give the same VR list. One new test covers theexpectsRawpath.1.0-betaagain (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.c570c28(review decisions S1/Q1, N1, N2, N3/Q2):BinaryRepresentation.wordWidthgives 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,DicomMessageandfromPart10swapped the value, andAsyncDicomReaderdid not swap the value. The new test failed before the change. The commit also fixes thereadRawBinarydoc comment, rewraps aMIGRATION.mdline, and adds the UN clause to the docs.12effda: merged1.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.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.4fe4a25(final review decisions Q1, Q2, S3, N1 to N4):wordWidthisWORD_WIDTHS[this.type] ?? 1again. After fix: support the OL, OV and SV value representations #653, every key ofWORD_WIDTHShas a VR class, so theVrscheck ofc570c28had no effect. The message ofc570c28says 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 thatWORD_WIDTHSdoes not hold. NAT-U-BIN-7,MIGRATION.md, anddocs/AsyncDicomReader-skill.mdname 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 withfromPart10Stream, adds SV toLENGTH_32_VRS, and fixes the comment for (0066,0040).802c231: merged1.0-beta(with fix(legacy): keep untilTag and NumberOfFrames out of AsyncDicomReader sequence items (port of #657) #666). Two conflicts: the constructor comments ofAsyncDicomReader(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