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

Utils function for read/write value from unprocessed data #306

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
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
2 changes: 1 addition & 1 deletion dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (d *Dataset) FindElementByTag(tag tag.Tag) (*Element, error) {
return nil, ErrorElementNotFound
}

func (d *Dataset) transferSyntax() (binary.ByteOrder, bool, error) {
func (d *Dataset) TransferSyntax() (binary.ByteOrder, bool, error) {
elem, err := d.FindElementByTag(tag.TransferSyntaxUID)
if err != nil {
return nil, false, err
Expand Down
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ require (
golang.org/x/text v0.3.8
)

require golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -12,3 +25,7 @@ golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
143 changes: 143 additions & 0 deletions pkg/frame/inplace/pixeldata_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package inplace

import (
"encoding/binary"
"errors"
"fmt"
"strconv"

"github.com/suyashkumar/dicom"
"github.com/suyashkumar/dicom/pkg/tag"
)

// GetIntFromValue is similar to dicom.MustGetInts but without panic
func GetIntFromValue(v dicom.Value) (int, error) {
if v.ValueType() != dicom.Ints {
return 0, errors.New("value is not ints")
}
arr, ok := v.GetValue().([]int)
if !ok {
return 0, errors.New("actual value is not ints")
}
if len(arr) == 0 {
return 0, errors.New("empty ints value")
}
return arr[0], nil
}

// GetStringFromValue is similar to dicom.MustGetStrings but without panic
func GetStringFromValue(v dicom.Value) (string, error) {
if v.ValueType() != dicom.Strings {
return "", errors.New("value is not ints")
}
arr, ok := v.GetValue().([]string)
if !ok {
return "", errors.New("actual value is not ints")
}
if len(arr) == 0 {
return "", errors.New("empty ints value")
}
return arr[0], nil
}

// PixelDataMetadata is the metadata for tag.PixelData
type PixelDataMetadata struct {
Rows int
Cols int
Frames int
SamplesPerPixel int
BitsAllocated int
PlanarConfiguration int
Bo binary.ByteOrder
}

// GetPixelDataMetadata returns the pixel data metadata.
func GetPixelDataMetadata(ds *dicom.Dataset) (*PixelDataMetadata, error) {
re := &PixelDataMetadata{}
rows, err := ds.FindElementByTag(tag.Rows)
if err != nil {
return nil, fmt.Errorf("get Rows element: %w", err)
}
if re.Rows, err = GetIntFromValue(rows.Value); err != nil {
return nil, fmt.Errorf("convert Rows element to int: %w", err)
}

cols, err := ds.FindElementByTag(tag.Columns)
if err != nil {
return nil, fmt.Errorf("get Columns element: %w", err)
}
if re.Cols, err = GetIntFromValue(cols.Value); err != nil {
return nil, fmt.Errorf("convert Columns element to int: %w", err)
}

numberOfFrames, err := ds.FindElementByTag(tag.NumberOfFrames)
if err != nil {
re.Frames = 1
} else {
var framesStr string
framesStr, err = GetStringFromValue(numberOfFrames.Value)
if err != nil {
return nil, fmt.Errorf("convert NumberOfFrames element to str: %w", err)
}
if re.Frames, err = strconv.Atoi(framesStr); err != nil {
return nil, fmt.Errorf("convert NumberOfFrames to int: %w", err)
}
}

samplesPerPixel, err := ds.FindElementByTag(tag.SamplesPerPixel)
if err != nil {
return nil, fmt.Errorf("get SamplesPerPixel element: %w", err)
}
if re.SamplesPerPixel, err = GetIntFromValue(samplesPerPixel.Value); err != nil {
return nil, fmt.Errorf("convert SamplesPerPixel element to int: %w", err)
}
bitsAllocated, err := ds.FindElementByTag(tag.BitsAllocated)
if err != nil {
return nil, fmt.Errorf("get BitsAllocated element: %w", err)
}
if re.BitsAllocated, err = GetIntFromValue(bitsAllocated.Value); err != nil {
return nil, fmt.Errorf("convert BitsAllocated element to int: %w", err)
}
re.Bo, _, err = ds.TransferSyntax()
if err != nil {
return nil, fmt.Errorf("get byteOrder: %w", err)
}

planarConfElement, err := ds.FindElementByTag(tag.PlanarConfiguration)
if err != nil {
re.PlanarConfiguration = 0
} else {
if re.PlanarConfiguration, err = GetIntFromValue(planarConfElement.Value); err != nil {
return nil, fmt.Errorf("convert Rows element to int: %w", err)
}
}

return re, nil
}

// IsSafeForUnprocessedValueDataHandling check if we can support in-place read-write
// from Pixeldata.UnprocessedValueData
// This avoids the case that we can not handle it, yet.
func IsSafeForUnprocessedValueDataHandling(info *PixelDataMetadata, unprocessedValueData []byte) error {
// https://dicom.innolitics.com/ciods/enhanced-mr-image/enhanced-mr-image/00280006
if info.PlanarConfiguration == 1 {
ducquangkstn marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("unsupported PlanarConfiguration: %d", info.PlanarConfiguration)
}
// TODO: support for BitsAllocated == 1
switch info.BitsAllocated {
case 8, 16, 32:
default: // bitsAllocated = 1 and other cases
return fmt.Errorf("unsupported bit allocated: %d", info.BitsAllocated)
}
pixelsPerFrame := info.Rows * info.Cols
bytesAllocated := info.BitsAllocated / 8
expectedBytes := bytesAllocated * info.SamplesPerPixel * info.Frames * pixelsPerFrame
// odd number of bytes.
if expectedBytes%2 == 1 {
expectedBytes += 1
}
if len(unprocessedValueData) != expectedBytes {
return errors.New("mismatch data size")
}
return nil
}
29 changes: 29 additions & 0 deletions pkg/frame/inplace/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Package inplace contains code for handling UnprocessedValueData
package inplace

// ReadUnprocessedValueData read the value of Dicom image directly
// from the byte array of PixelData.UnprocessedValueData with given frame ID.
// This ease the memory usage of reading DICOM image.
func ReadUnprocessedValueData(info *PixelDataMetadata, unprocessedValueData []byte, frameIndex int) ([][]int, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this returns [][]int, this will still have the same issue of expanding the memory from the smaller int to the 64-bit int right? Is the main place this saves memory in the fact that it only does one frame at a time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the main place this saves memory in the fact that it only does one frame at a time

Yes, most of the DICOM with the issues contains multiple frames.

But, I think I would change to a new structure of storing data.

B/c users need to adapt to new way of read/write pixel data anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed at 16701fa

pixelsPerFrame := info.Rows * info.Cols
bytesAllocated := info.BitsAllocated / 8
offset := frameIndex * pixelsPerFrame * info.SamplesPerPixel * bytesAllocated
samplesPerPixel := info.SamplesPerPixel

re := make([][]int, pixelsPerFrame)
for i := 0; i < pixelsPerFrame; i++ {
re[i] = make([]int, samplesPerPixel)
for j := 0; j < samplesPerPixel; j++ {
pointOffset := offset + i*info.SamplesPerPixel*bytesAllocated + j*bytesAllocated
switch bytesAllocated {
case 1:
re[i][j] = int(unprocessedValueData[pointOffset])
case 2:
re[i][j] = int(info.Bo.Uint16(unprocessedValueData[pointOffset : pointOffset+bytesAllocated]))
case 4:
re[i][j] = int(info.Bo.Uint32(unprocessedValueData[pointOffset : pointOffset+bytesAllocated]))
}
}
}
return re, nil
}
Loading
Loading