-
-
Notifications
You must be signed in to change notification settings - Fork 138
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
Use a once-allocated buffer for reading native pixel values instead of using binary.Read (which allocates a slice under the hood) #163
Changes from 9 commits
1780bba
7883858
1e175d1
e423725
7f107ef
3ef4870
fb1393c
9e5de7d
25288ff
075bb74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,6 +23,7 @@ var ( | |||||
// ErrorUnsupportedVR indicates that this VR is not supported. | ||||||
ErrorUnsupportedVR = errors.New("unsupported VR") | ||||||
errorUnableToParseFloat = errors.New("unable to parse float type") | ||||||
ErrorIncompleteRead = errors.New("unable to read specified number of bytes") | ||||||
) | ||||||
|
||||||
func readTag(r dicomio.Reader) (*tag.Tag, error) { | ||||||
|
@@ -212,6 +213,9 @@ func readNativeFrames(d dicomio.Reader, parsedData *Dataset, fc chan<- *frame.Fr | |||||
|
||||||
// Parse the pixels: | ||||||
image.Frames = make([]frame.Frame, nFrames) | ||||||
bo := d.ByteOrder() | ||||||
bytesAllocated := bitsAllocated / 8 | ||||||
pixelBuf := make([]byte, bytesAllocated) | ||||||
for frameIdx := 0; frameIdx < nFrames; frameIdx++ { | ||||||
// Init current frame | ||||||
currentFrame := frame.Frame{ | ||||||
|
@@ -226,24 +230,22 @@ func readNativeFrames(d dicomio.Reader, parsedData *Dataset, fc chan<- *frame.Fr | |||||
buf := make([]int, int(pixelsPerFrame)*samplesPerPixel) | ||||||
for pixel := 0; pixel < int(pixelsPerFrame); pixel++ { | ||||||
for value := 0; value < samplesPerPixel; value++ { | ||||||
if bitsAllocated == 8 { | ||||||
val, err := d.ReadUInt8() | ||||||
if err != nil { | ||||||
return nil, bytesRead, err | ||||||
|
||||||
n, err := d.Read(pixelBuf) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thought about this a little more the second time around (sorry for not bringing this up earlier). Looking at the In reality for values like 16 or 32 bits, calls to It also looks like based on the implementation, it may return a nice ErrUnexpectedEOF if it reads less than n bytes, saving us an error, but we can leave it in there for not and not depend on that to make it easy and get this merged for now. WDYT?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. |
||||||
if err != nil || n < bytesAllocated { | ||||||
if err == nil { | ||||||
err = ErrorIncompleteRead | ||||||
} | ||||||
buf[(pixel*samplesPerPixel)+value] = int(val) | ||||||
return nil, bytesRead, | ||||||
fmt.Errorf("could not read uint%d from input: %w", bitsAllocated, err) | ||||||
} | ||||||
suyashkumar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
if bitsAllocated == 8 { | ||||||
buf[(pixel*samplesPerPixel)+value] = int(pixelBuf[0]) | ||||||
} else if bitsAllocated == 16 { | ||||||
val, err := d.ReadUInt16() | ||||||
if err != nil { | ||||||
return nil, bytesRead, err | ||||||
} | ||||||
buf[(pixel*samplesPerPixel)+value] = int(val) | ||||||
buf[(pixel*samplesPerPixel)+value] = int(bo.Uint16(pixelBuf)) | ||||||
} else if bitsAllocated == 32 { | ||||||
val, err := d.ReadUInt32() | ||||||
if err != nil { | ||||||
return nil, bytesRead, err | ||||||
} | ||||||
buf[(pixel*samplesPerPixel)+value] = int(val) | ||||||
buf[(pixel*samplesPerPixel)+value] = int(bo.Uint32(pixelBuf)) | ||||||
} | ||||||
} | ||||||
currentFrame.NativeData.Data[pixel] = buf[pixel*samplesPerPixel : (pixel+1)*samplesPerPixel] | ||||||
|
@@ -254,7 +256,7 @@ func readNativeFrames(d dicomio.Reader, parsedData *Dataset, fc chan<- *frame.Fr | |||||
} | ||||||
} | ||||||
|
||||||
bytesRead = (bitsAllocated / 8) * samplesPerPixel * pixelsPerFrame * nFrames | ||||||
bytesRead = bytesAllocated * samplesPerPixel * pixelsPerFrame * nFrames | ||||||
|
||||||
return &image, bytesRead, nil | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: no need for a new line here (but I can also just fix in a cleanup pr later, don't need to block this).