Skip to content

Commit

Permalink
Merge pull request ome#4168 from melissalinkert/omegh-4165
Browse files Browse the repository at this point in the history
DICOM reader: better handling of nested sequences that include pixel data
  • Loading branch information
dgault authored Apr 11, 2024
2 parents 6fa35f6 + daaa2f9 commit edf5418
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
36 changes: 33 additions & 3 deletions components/formats-bsd/src/loci/formats/dicom/DicomTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,33 @@ else if (elementLength <= 44) {
break;
}
else if (child.attribute == PIXEL_DATA) {
stop = fp;
break;
child.parent = this;
children.add(child);
if (child.elementLength == -1) {
// look for end of sequence
long seek = fp - 2;
int nextTag = 0;
while (seek < in.length() && (nextTag != SEQUENCE_DELIMITATION_ITEM.getTag() && nextTag != ITEM_DELIMITATION_ITEM.getTag())) {
seek += 2;
in.seek(seek);
try {
nextTag = getNextTag(in);
}
catch (Exception e) {
}
if (nextTag == 0xfeffdde0 || nextTag == 0xfeff0d0e) {
in.order(!in.isLittleEndian());
break;
}
}
}
}
else if (child.attribute != ITEM && child.attribute != ITEM_DELIMITATION_ITEM) {
child.parent = this;
children.add(child);
}
}
if (elementLength == 0) {
if (elementLength <= 0) {
elementLength = (int) (stop - start);
}
in.seek(stop);
Expand Down Expand Up @@ -438,6 +456,7 @@ private int getLength(RandomAccessInputStream in) throws IOException {

switch (vr) {
case OB:
case OV:
case OW:
case SQ:
case UN:
Expand Down Expand Up @@ -700,6 +719,17 @@ else if (!(value instanceof long[])) {
}
}

/**
* See https://dicom.nema.org/dicom/2013/output/chtml/part05/sect_7.8.html
*
* @return true if this is a private content creator tag
*/
public boolean isPrivateContentCreator() {
int highWord = (tag >> 16) & 0xffff;
int lowWord = tag & 0xffff;
return highWord % 2 == 1 && lowWord == 0x0010;
}

@Override
public String toString() {
if (key == null) {
Expand Down
12 changes: 11 additions & 1 deletion components/formats-bsd/src/loci/formats/in/DicomReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
Expand Down Expand Up @@ -127,6 +129,8 @@ public class DicomReader extends SubResolutionFormatReader {

private List<DicomTag> tags;

private Set<Integer> privateContentHighWords = new HashSet<Integer>();

// -- Constructor --

/** Constructs a new DICOM reader. */
Expand Down Expand Up @@ -368,6 +372,7 @@ public void close(boolean fileOnly) throws IOException {
concatenationNumber = null;
edf = false;
tags = null;
privateContentHighWords.clear();
}
}

Expand Down Expand Up @@ -1168,6 +1173,10 @@ else if (infoString.startsWith("MONOCHROME")) {
* rely upon the original metadata table.
*/
private void addOriginalMetadata(String key, DicomTag info) {
if (info.isPrivateContentCreator()) {
privateContentHighWords.add(info.tag >> 16);
}

if (info.value != null && !(info.value instanceof byte[]) &&
!(info.value instanceof short[]))
{
Expand All @@ -1179,7 +1188,8 @@ private void addOriginalMetadata(String key, DicomTag info) {
}
}
if (info.attribute != PER_FRAME_FUNCTIONAL_GROUPS_SEQUENCE &&
info.attribute != REFERENCED_IMAGE_NAVIGATION_SEQUENCE)
info.attribute != REFERENCED_IMAGE_NAVIGATION_SEQUENCE &&
!privateContentHighWords.contains(info.tag >> 16))
{
for (DicomTag child : info.children) {
String childKey = DicomAttribute.formatTag(child.tag);
Expand Down

0 comments on commit edf5418

Please sign in to comment.