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 Jun 1, 2024
1 parent 386a707 commit d04b27e
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 @@ -177,7 +178,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 @@ -202,7 +203,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 @@ -229,7 +230,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 @@ -853,19 +854,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 d04b27e

Please sign in to comment.