Repository navigation
fix: narrow the UN sequence detection and keep Big Endian items (follow-up to #496) - #664
Open
wayfarer3130 wants to merge 10 commits into
Open
wayfarer3130 wants to merge 10 commits into
wayfarer3130 wants to merge 10 commits into
Conversation
…ow-up to #496) - Sync reader: set the stream little endian while it reads a UN sequence in Explicit VR Big Endian (the items were lost). - isUnSequenceValue: apply PS3.5 6.2.2 only when the dictionary VR is SQ or the tag has no dictionary entry, and when the first item length is undefined or fits in the value. Both readers use the check. - Writer: a UN value is a sequence only when each value is an item with tag-shaped keys, so { InlineBinary } / { BulkDataURI } stay UN. 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. |
…oups 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. Same change as the 1.x port #662. 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. DicomMessage was already correct. Co-Authored-By: Claude Opus 5.5 <[email protected]>
- isUnSequenceValue compares the first 4 bytes with SEQUENCE_ITEM_TAG, and its doc comment ends with a full sentence. - The InlineBinary write test expects a TypeError, not the V8 error text. Co-Authored-By: Claude Opus 5.5 <[email protected]>
An empty PerFrameFunctionalGroupsSequence no longer reduces the number of frames: the normalizer builds one item per declared frame, as for a missing sequence. Each generated item gets its own ImagePositionPatient copy. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 25, 2026
wayfarer3130
added a commit
that referenced
this pull request
Nov 26, 2026
…magePositionPatient array per frame (#665) * fix(normalizers): treat an empty per-frame sequence as missing; one ImagePositionPatient array per frame An empty PerFrameFunctionalGroupsSequence no longer reduces the number of frames: the normalizer builds one item per declared frame, as for a missing sequence. Each generated item gets its own ImagePositionPatient copy. Port of the 0.x fix in #664 (follow-up to #662). Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(normalizers): treat an empty shared sequence as missing; complete the per-frame sequence - An empty SharedFunctionalGroupsSequence array counts as missing, and a one-item array is used as its item, so the shared groups the normalizer sets (PixelValueTransformationSequence and the others) are written. - The per-frame sequence is filled up to NumberOfFrames, and each item gets its FrameContentSequence, not only the first item. - planePositionGroup passes a non-array ImagePositionPatient through. - Raise the dcmjs.js bundle ceiling to x1.091 (measured x1.09014). Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(normalizers): keep an existing FrameContentSequence; replace a null per-frame item - The normalizer adds a FrameContentSequence only to a per-frame item that has none; an existing item keeps its own (dimension index values, temporal position, stack ID). - A null or missing per-frame item becomes an empty item again, so [null, {}] no longer throws a TypeError. - Tests: an existing item keeps its FrameContentSequence, and the shared groups survive a denaturalize/naturalize round trip. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(normalizers): keep the shared group accessors and skip null items after NumberOfFrames - Call addAccessors on the SharedFunctionalGroupsSequence item, so that both SharedFunctionalGroupsSequence.X and SharedFunctionalGroupsSequence[0].X work, also for a hand-built one-item array. - The window guess skips a null per-frame item after NumberOfFrames and does not throw. - Tests: a [null, {}] case in the completes-a-per-frame-sequence it.each, a null item after NumberOfFrames, a one-item shared array. The test "keeps a complete multiframe dataset unchanged" now uses NumberOfFrames 1. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
…t of #665) - An empty SharedFunctionalGroupsSequence array counts as missing. A one-item array gets the list accessors (addAccessors), so the shared groups the normalizer sets are written. A plain object stays as is. - The per-frame sequence is filled up to NumberOfFrames. Existing items keep their content, and only an item without a FrameContentSequence gets one. - A null per-frame item becomes an empty item; a null item after NumberOfFrames is kept and the window guess does not throw. - planePositionGroup copies an array ImagePositionPatient per frame and passes a non-array value through. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…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]>
…byte order - normalizers: copy PerFrameFunctionalGroupsSequence to a plain array before the fill, so that push never goes through the addAccessors Proxy of a naturalized one-item sequence (item 0 got keys "1", "2" and the write threw). - normalizers: restore ImageNormalizer.framesInPixelData as a deprecated static method; the normalizer does not call it. - normalizers: the doc comment says that a shared sequence with 2 or more items keeps only item 0. - AsyncDicomReader: _emitSplitValues and readUncompressed decide the Big Endian swap from the stream byte order, so that the Little Endian items of a UN sequence in a Big Endian file are not swapped. - AsyncDicomReader: fix the stale readRawBinary doc comment. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… items A one-frame per-frame sequence from naturalizeDataset keeps its list accessors, as in 0.53.6. Comment why the 1-bit pixel path can use the file syntax for the swap. Co-Authored-By: Claude Opus 5.5 <[email protected]>
wayfarer3130
marked this pull request as ready for review
November 26, 2026 02:23
Contributor
Author
|
This PR is ready for your review. The changes in this PR come from the 1.0-beta work. During the review of the 1.0-beta ports, we found "should fix" issues that also apply to 0.x. This PR is the single 0.x PR for those fixes:
A clean review checked each change. The reviews found one blocker and some smaller items, and this PR fixes them. The section "Behavior changes" of the PR description lists the changes that a user can see. CI passes on |
This branch has not been deployed
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 a follow-up to #496, #603 and #622. The reviews of the 1.x ports #658, #662 and #663 found these bugs. This PR is also the single 0.x PR for the fixes from the 1.0-beta work. B4, B5 and B6 are ports of #665 and #663 from
1.0-beta.Summary
UNsequence in Explicit VR Big Endian. The sync reader now reads the sequence little endian.UNvalue as a sequence when the value started with the item tag only. Now the readers also check the dictionary VR and the length of the first item.UNwith an{ InlineBinary }value asSQ. Now the writer writes aUNasSQonly when each value is an item with tag-shaped keys.NumberOfFramesfrom the size ofPixelData(fix(normalizers): normalize multiframe datasets missing functional group sequences #603). Now the normalizer keeps the declaredNumberOfFrames.OWpixel data, when a frame is not a whole number of words (fix: byte order of binary value fields in Explicit VR Big Endian #622). Now the async reader swaps the whole value first.FrameContentSequence(port of fix(normalizers): treat an empty per-frame sequence as missing; one ImagePositionPatient array per frame #665).expectsRaw) (port of fix: byte order of binary value fields in Explicit VR Big Endian (port of #622) #663).OVVR. The readers did not swap the bytes of anOVvalue in Explicit VR Big Endian (port of fix: byte order of binary value fields in Explicit VR Big Endian (port of #622) #663).Behavior changes
This PR changes these public behaviors:
OVelement now reads asvr: "OV", not asvr: "UN". The change applies to all three readers. The writer now writes the VROVfor a dict entry withvr: "OV". dcmjs 0.53.6 wroteUN. The naturalized_vrMapno longer hasExtendedOffsetTable: "UN". The value shape ([ArrayBuffer]) does not change.ImageNormalizer.framesInPixelDatais deprecated. The method stays, with the same signature and the same result as in 0.53.6. The normalizer does not call the method.SharedFunctionalGroupsSequencewith 2 or more items now keeps only item 0.B1. Explicit VR Big Endian loses the sequence items
DicomMessage._readTagsetreadSyntaxto Implicit VR LE, but the stream stayed big endian. The SQ reader read the item tag as(FEFF,E000), found no item, and returnedValue: []. The async reader was correct, becausereadTagHeadersets the endian fromreadSyntax.1.2.840.10008.1.2.2and"00091010": { vr: "UN", Value: [<PS3.5 6.2.2 item bytes>] }. Then read the file withDicomMessage.readFile. The result is{ vr: "SQ", Value: [] }._readTagcallsstream.setEndian(true)for the sequence. The existingstream.setEndian(oldEndian)restores the endian after the value.S1. A UN that starts with the item tag, but is not a sequence, fails to read
isUnSequenceValuelooked at the first 4 bytes only. It did not look at the dictionary VR, and it did not check the item length.(0009,1010)UN with the 16 bytesFE FF 00 E0 00 00 01 00 01..08. The sync reader throwsRequest more than currently allocated buffer.(0042,0011)(dictionary VROB) as wireUNwith the bytesFE FF 00 E0 04 00 00 00 09 09 09 09. The read throws.isUnSequenceValue(stream, length, tag)returns true only when these conditions are all true:SEQUENCE_ITEM_TAGconstant.SQ, or the tag has no dictionary entry.0xFFFFFFFF, or the first item length is not more thanlength - 8.S2. The writer writes
{ InlineBinary }as SQisItemListinsrc/Tag.jsaccepted each object that is not binary data.{ InlineBinary }and{ BulkDataURI }are such objects."00091010": { vr: "UN", Value: [{ InlineBinary: "AAECAw==" }] }. The writer writes anSQwith a junk item.isItemListalso requires that each key of each value has 8 hex digits, asisItemDictLikedoes in the 1.x parser.UNwriter does not supportInlineBinary. After this fix, the write throws aTypeError, as it did before fix/bill un sq implicit vr ps35622 #496. The test checks the error type, not the V8 error text. Before this fix, the writer wrote an invalidSQand gave no error. Support forInlineBinaryin aUNwrite is a separate issue.B2. The normalizer lowers NumberOfFrames (#603)
framesInPixelDatatosrc/normalizers.js. WhenSharedFunctionalGroupsSequencewas missing,normalizeMultiframeloweredNumberOfFramesto the number of whole uncompressed frames inPixelData. EncapsulatedPixelDatacan have more fragments than frames, or fewer fragments than frames (for example, the video transfer syntaxes). The byte count does not give the frame count.NumberOfFrames4, 2x2 16-bitPixelDataof 1 frame, and no functional groups. The normalizer gaveNumberOfFrames1 and 1 per-frame item.NumberOfFrames8 and encapsulatedPixelDataof 2 fragments of 10 bytes. The normalizer gaveNumberOfFrames2.NumberOfFrames. The normalizer does not callframesInPixelDatanow.ImageNormalizer.framesInPixelDatastays as a public static method with the@deprecatedtag, because a caller outside dcmjs can use it. WhenPerFrameFunctionalGroupsSequenceis missing, the normalizer makes one per-frame item for each declared frame. Each item hasPlanePositionSequencefrom the top-levelImagePositionPatient, and its ownFrameContentSequence. The 1.x port fix(normalizers): normalize multiframe datasets missing functional groups (port of #603) #662 has the same change.PerFrameFunctionalGroupsSequence([]) gets the same result as a missing sequence: one per-frame item for each declared frame. Each item has its own copy ofImagePositionPatient.B3. The async reader mixes the frames of Big Endian OW pixel data (#622)
AsyncDicomReader.readUncompressedcut the value into frames first, and swapped the bytes of each frame alone. The words ofOWrun across the whole value. When a frame is not a whole number of words, one word spans two frames. The sync reader (DicomMessage) swaps the whole value, so its result was correct.1.2.840.10008.1.2.2file withRows3,Columns3,BitsAllocated8,NumberOfFrames2, andPixelDataOWwith the bytes 1..18. Read the file.DicomMessagegives[1..9]and[10..18].AsyncDicomReadergave[1,2,3,4,5,6,7,8,10]and[12,9,14,11,16,13,18,15,17].readUncompressedBitFrame) already does this.B4. The normalizer does not complete the functional groups (port of #665)
normalizeMultiframeinsrc/normalizers.jsusedif (!ds.SharedFunctionalGroupsSequence), so an empty array[]counted as present. The normalizer then setPixelValueTransformationSequenceon the array, and the write lost it.NumberOfFrames.FrameContentSequenceof each existing item. The dimension index values, the temporal position and the stack ID of the file were lost.normalizeMultiframe:SharedFunctionalGroupsSequencearray counts as missing.addAccessors). ThusSharedFunctionalGroupsSequence.XandSharedFunctionalGroupsSequence[0].Xboth work. A plain object stays a plain object.normalizeMultiframestates this rule.NumberOfFrames, and fills the per-frame sequence up toNumberOfFrames.naturalizeDatasetwraps a one-item sequence in anaddAccessorsProxy. Apushon thatProxywrote the new items into item 0 as the keys"1"and"2", anddenaturalizeDatasetthen threw aTypeError.FrameContentSequenceonly to an item that has none.NumberOfFramesbecomes an empty item (||= {}). An item afterNumberOfFramesstays unchanged. The window guess usesfunctionalGroup?.FrameVOILUT, so a null item does not throw.ImagePositionPatientarray. A non-array value goes through unchanged.addAccessors, andnaturalizeDatasetusesaddAccessorsfor a one-item sequence. Thus the 1.0addAccessorschange applies to 0.x. On 0.x,addAccessorsgives aProxyof a one-item array, and not the item itself as in 1.0. The tests checkshared[0].X, and notshared[0] === shared.B5. The raw binary path does not swap Big Endian words (port of #663)
AsyncDicomReader.readRawBinarycalled_emitSplitValues(length)with no word width. A listener that returns{ expectsRaw: true }got theOW,OF,ODandOLwords in big endian order.wordWidth(tagInfo)gives the word width of the resolved VR.readRawBinarypasses the word width to_emitSplitValues.1.0-beta:OWhas 2 bytes for each word.OFandOLhave 4 bytes.ODandOVhave 8 bytes.OBand aUNwith no dictionary VR do not swap.UNelement with the dictionary VROW,OF,OD,OLorOVswaps as that VR.wordWidth(tagInfo). Thus aUNPixel Data with the dictionary VRoxswaps asOW, as on1.0-beta._emitSplitValues(raw binary path) andreadUncompressed(pixel data path) decide the swap from the byte order of the stream, not from the transfer syntax of the file. The items of aUNsequence are Implicit VR Little Endian, also in a Big Endian file. Before this change, anexpectsRawlistener got the words of(0028,1201)1,2,3,4in such an item as2,1,4,3.readSingleand the sync reader already use the byte order of the stream.1.0-betagets the same change in a follow-up PR.B6. 0.x has no OV VR (port of #663)
ValueRepresentation.createByTypeString("OV")gaveUN. An explicitOVvalue and aUNvalue with the dictionary VROV(Extended Offset Table(7FE0,0001)) got no byte swap in Explicit VR Big Endian. All three readers gave the bytes in file order.OL: aUNwith the dictionary VROLalready resolved toOLwith a word width of 4. Only the raw binary path (B5) did not swap the bytes.OVVR (OtherVeryLongString).OVhas a 32-bit length, a single value and 8 bytes for each word, as on1.0-beta.Verified
I added the new tests to the #496 test block in
test/sync-async-parser-equivalency.test.js. I ran each new test on themastersource before the fix:masterRequest more than currently allocated bufferSQI ran the tests of B2 and B3 on this branch before each fix:
test/segIncompleteMultiframe.test.js: SEG derivation from a dataset withNumberOfFrames4 and no functional groups (includeSliceSpacingtrue and false) keeps 4 frames and gets 4 per-frame itemsNumberOfFrames1, expected 4test/segIncompleteMultiframe.test.js:NumberOfFrames8 stays for encapsulatedPixelDatawith 2 fragments and with 10 fragmentsNumberOfFrames2 (2 fragments); 1 per-frame item, expected 8 (10 fragments)test/big-endian.test.js: the sync reader and the async reader readOWframes that are not a whole number of words (LE and BE)[1,2,3,4,5,6,7,8,10]for frame 1I ran the tests of B4, B5 and B6 on this branch before each fix:
test/segIncompleteMultiframe.test.js: new tests from1.0-beta(empty shared sequence, existingFrameContentSequence, short per-frame sequence and[null, {}], null item afterNumberOfFrames, one-item shared array)test/big-endian.test.js: a Big EndianOW,OForOLvalue (also asUN) read asexpectsRawraw binarytest/big-endian.test.js: a Big EndianOVvalue, and aUNvalue with the dictionary VROV, with all three readersI ran the tests of the review fixes on this branch before each fix:
test/segIncompleteMultiframe.test.js: a one-item per-frame sequence fromnaturalizeDatasetwithNumberOfFrames3 gets 3 items, anddenaturalizeDatasetwrites the dataset"1"and"2"test/big-endian.test.js: anOWvalue1,2,3,4in aUNsequence of a Big Endian file, read with anexpectsRawlistener2,1,4,3I merged
origin/masterinto this branch before the ports.npm test: 33 test suites and 459 tests pass.eslint,prettier --checkandnpm run buildpass. The 0.x line has no bundle-size test.PR #658 gets the same fixes for 1.x.
🤖 Generated with Claude Code