Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor API, add Parser Interface #13

Merged
merged 8 commits into from
Jul 9, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move over readNativeFrames to parse.go
  • Loading branch information
suyashkumar committed Jul 7, 2018
commit da54abef4c19880ddb316a65a7952c6ebd560446
78 changes: 0 additions & 78 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import (
"fmt"
"strings"

"strconv"

"github.com/gradienthealth/go-dicom/dicomio"
"github.com/gradienthealth/go-dicom/dicomlog"
"github.com/gradienthealth/go-dicom/dicomtag"
)

Expand Down Expand Up @@ -454,81 +451,6 @@ func readExplicit(buffer *dicomio.Decoder, tag dicomtag.Tag) (string, uint32) {
return vr, vl
}

// readNativeFrames reads Native frames from a Decoder based on already parsed pixel information
// that should be available in parsedData (elements like NumberOfFrames, rows, columns, etc)
func readNativeFrames(d *dicomio.Decoder, parsedData *DataSet) (pixelData *PixelDataInfo, bytesRead int, err error) {
image := PixelDataInfo{
Encapsulated: false,
}

// Parse information from previously parsed attributes that are needed to parse Native Frames:
rows, err := parsedData.FindElementByTag(dicomtag.Rows)
if err != nil {
return nil, 0, err
}

cols, err := parsedData.FindElementByTag(dicomtag.Columns)
if err != nil {
return nil, 0, err
}

nof, err := parsedData.FindElementByTag(dicomtag.NumberOfFrames)
nFrames := 0
if err == nil {
// No error, so parse number of frames
nFrames, err = strconv.Atoi(nof.MustGetString()) // odd that number of frames is encoded as a string...
if err != nil {
dicomlog.Vprintf(1, "ERROR converting nof")
return nil, 0, err
}
} else {
// error fetching NumberOfFrames, so default to 1. TODO: revisit
nFrames = 1
}

b, err := parsedData.FindElementByTag(dicomtag.BitsAllocated)
if err != nil {
dicomlog.Vprintf(1, "ERROR finding bits allocated.")
return nil, 0, err
}
bitsAllocated := int(b.MustGetUInt16())
image.BitsPerSample = bitsAllocated

s, err := parsedData.FindElementByTag(dicomtag.SamplesPerPixel)
if err != nil {
dicomlog.Vprintf(1, "ERROR finding samples per pixel")
}
samplesPerPixel := int(s.MustGetUInt16())

pixelsPerFrame := int(rows.MustGetUInt16()) * int(cols.MustGetUInt16())

dicomlog.Vprintf(1, "Image size: %decoder x %decoder", rows.MustGetUInt16(), cols.MustGetUInt16())
dicomlog.Vprintf(1, "Pixels Per Frame: %decoder", pixelsPerFrame)
dicomlog.Vprintf(1, "Number of frames %decoder", nFrames)

// Parse the pixels:
image.NativeFrames = make([][][]int, nFrames)
for frame := 0; frame < nFrames; frame++ {
currentFrame := make([][]int, pixelsPerFrame)
for pixel := 0; pixel < int(pixelsPerFrame); pixel++ {
currentPixel := make([]int, samplesPerPixel)
for value := 0; value < samplesPerPixel; value++ {
if bitsAllocated == 8 {
currentPixel[value] = int(d.ReadUInt8())
} else if bitsAllocated == 16 {
currentPixel[value] = int(d.ReadUInt16())
}
}
currentFrame[pixel] = currentPixel
}
image.NativeFrames[frame] = currentFrame
}

bytesRead = (bitsAllocated / 8) * samplesPerPixel * pixelsPerFrame * nFrames

return &image, bytesRead, nil
}

func tagInList(tag dicomtag.Tag, tags []dicomtag.Tag) bool {
for _, t := range tags {
if tag == t {
Expand Down
76 changes: 76 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"os"
"strconv"
"strings"

"github.com/gradienthealth/go-dicom/dicomio"
Expand Down Expand Up @@ -488,3 +489,78 @@ func getTransferSyntax(ds *DataSet) (bo binary.ByteOrder, implicit dicomio.IsImp
}
return dicomio.ParseTransferSyntaxUID(transferSyntaxUID)
}

// readNativeFrames reads Native frames from a Decoder based on already parsed pixel information
// that should be available in parsedData (elements like NumberOfFrames, rows, columns, etc)
func readNativeFrames(d *dicomio.Decoder, parsedData *DataSet) (pixelData *PixelDataInfo, bytesRead int, err error) {
image := PixelDataInfo{
Encapsulated: false,
}

// Parse information from previously parsed attributes that are needed to parse Native Frames:
rows, err := parsedData.FindElementByTag(dicomtag.Rows)
if err != nil {
return nil, 0, err
}

cols, err := parsedData.FindElementByTag(dicomtag.Columns)
if err != nil {
return nil, 0, err
}

nof, err := parsedData.FindElementByTag(dicomtag.NumberOfFrames)
nFrames := 0
if err == nil {
// No error, so parse number of frames
nFrames, err = strconv.Atoi(nof.MustGetString()) // odd that number of frames is encoded as a string...
if err != nil {
dicomlog.Vprintf(1, "ERROR converting nof")
return nil, 0, err
}
} else {
// error fetching NumberOfFrames, so default to 1. TODO: revisit
nFrames = 1
}

b, err := parsedData.FindElementByTag(dicomtag.BitsAllocated)
if err != nil {
dicomlog.Vprintf(1, "ERROR finding bits allocated.")
return nil, 0, err
}
bitsAllocated := int(b.MustGetUInt16())
image.BitsPerSample = bitsAllocated

s, err := parsedData.FindElementByTag(dicomtag.SamplesPerPixel)
if err != nil {
dicomlog.Vprintf(1, "ERROR finding samples per pixel")
}
samplesPerPixel := int(s.MustGetUInt16())

pixelsPerFrame := int(rows.MustGetUInt16()) * int(cols.MustGetUInt16())

dicomlog.Vprintf(1, "Image size: %decoder x %decoder", rows.MustGetUInt16(), cols.MustGetUInt16())
dicomlog.Vprintf(1, "Pixels Per Frame: %decoder", pixelsPerFrame)
dicomlog.Vprintf(1, "Number of frames %decoder", nFrames)

// Parse the pixels:
image.NativeFrames = make([][][]int, nFrames)
for frame := 0; frame < nFrames; frame++ {
currentFrame := make([][]int, pixelsPerFrame)
for pixel := 0; pixel < int(pixelsPerFrame); pixel++ {
currentPixel := make([]int, samplesPerPixel)
for value := 0; value < samplesPerPixel; value++ {
if bitsAllocated == 8 {
currentPixel[value] = int(d.ReadUInt8())
} else if bitsAllocated == 16 {
currentPixel[value] = int(d.ReadUInt16())
}
}
currentFrame[pixel] = currentPixel
}
image.NativeFrames[frame] = currentFrame
}

bytesRead = (bitsAllocated / 8) * samplesPerPixel * pixelsPerFrame * nFrames

return &image, bytesRead, nil
}