Skip to content

Commit

Permalink
Add metadata elements to required context
Browse files Browse the repository at this point in the history
  • Loading branch information
suyashkumar committed May 27, 2024
1 parent 8847799 commit f60af73
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ type reader struct {
opts parseOptSet
// datasetCtx is the top-level context Dataset holding only elements that
// may be needed to parse future elements (e.g. like tag.Rows). See
// datasetContextElementPassList for elements that will be automatically
// requiredContextElements for elements that will be automatically
// included in this context.
// TODO(suyashkumar): should this be initialized to the Metadata elements?
datasetCtx *Dataset
}

Expand Down Expand Up @@ -167,7 +168,7 @@ func (r *reader) readHeader() ([]*Element, error) {

// Must read metadata as LittleEndian explicit VR
// Read the length of the metadata elements: (0002,0000) MetaElementGroupLength
maybeMetaLen, err := r.readElementWithContext(nil, nil)
maybeMetaLen, err := r.readElementWithContext(r.datasetCtx, nil)
if err != nil {
return nil, err
}
Expand All @@ -192,7 +193,7 @@ func (r *reader) readHeader() ([]*Element, error) {
}
defer r.rawReader.PopLimit()
for !r.rawReader.IsLimitExhausted() {
elem, err := r.readElementWithContext(nil, nil)
elem, err := r.readElementWithContext(r.datasetCtx, nil)
if err != nil {
// TODO: see if we can skip over malformed elements somehow
return nil, err
Expand All @@ -219,7 +220,7 @@ func (r *reader) readHeader() ([]*Element, error) {
if group != 0x0002 {
break
}
elem, err := r.readElementWithContext(nil, nil)
elem, err := r.readElementWithContext(r.datasetCtx, nil)
if err != nil {
// TODO: see if we can skip over malformed elements somehow
return nil, err
Expand Down Expand Up @@ -843,19 +844,27 @@ func (r *reader) moreToRead() bool {
return !r.rawReader.IsLimitExhausted()
}

// datasetContextElementPassList holds the set of DICOM Element Tags that should
// requiredContextElements holds the set of DICOM Element Tags that should
// be included in the context Dataset. These are elements that may be needed to
// read downstream elements in the future.
var datasetContextElementPassList = tag.Tags{
// addToContextIfNeeded also always adds all Metadata group 2 elements.
var requiredContextElements = tag.Tags{
&tag.Rows,
&tag.Columns,
&tag.NumberOfFrames,
&tag.BitsAllocated,
&tag.SamplesPerPixel,
}

// addToContextIfNeeded adds the element to the provided dataset context if
// the tag is in requiredContextElements or if the tag has a group of 2
// (meaning it is a metadata element). In the future we can probably filter this
// down further.
func addToContextIfNeeded(datasetCtx *Dataset, e *Element) {
if datasetContextElementPassList.Contains(&e.Tag) {
if datasetCtx == nil || e == nil {
return
}
if e.Tag.Group == 0x0002 || requiredContextElements.Contains(&e.Tag) {
datasetCtx.Elements = append(datasetCtx.Elements, e)
}
}

0 comments on commit f60af73

Please sign in to comment.