Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
70 changes: 43 additions & 27 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,36 +546,52 @@ func readNativeFrame[I constraints.Integer](bitsAllocated, rows, cols, bytesToRe
}

bo := rawReader.ByteOrder()
for pixel := 0; pixel < pixelsPerFrame; pixel++ {
for value := 0; value < samplesPerPixel; value++ {
_, err := io.ReadFull(rawReader, pixelBuf)
if err != nil {
return frame.Frame{}, bytesToRead,
fmt.Errorf("could not read uint%d from input: %w", bitsAllocated, err)
if bitsAllocated < 8 || bitsAllocated%8 != 0 {
return frame.Frame{}, bytesToRead, fmt.Errorf("readNativeFrame unsupported bitsAllocated=%d : %w", bitsAllocated, ErrorUnsupportedBitsAllocated)
}

bytesPerSample := bitsAllocated / 8

// Use a 4KB chunk buffer to minimize io.ReadFull calls
chunkSize := 4096
// ensure chunkSize is a multiple of bytesPerSample
chunkSize = (chunkSize / bytesPerSample) * bytesPerSample
chunkBuf := make([]byte, chunkSize)

totalSamples := pixelsPerFrame * samplesPerPixel
samplesRead := 0

for samplesRead < totalSamples {
samplesLeft := totalSamples - samplesRead
bytesToReadChunk := samplesLeft * bytesPerSample
if bytesToReadChunk > chunkSize {
bytesToReadChunk = chunkSize
}

_, err := io.ReadFull(rawReader, chunkBuf[:bytesToReadChunk])
if err != nil {
return frame.Frame{}, bytesToRead,
fmt.Errorf("could not read uint%d from input: %w", bitsAllocated, err)
}

samplesInChunk := bytesToReadChunk / bytesPerSample
switch bitsAllocated {
case 8:
for i := 0; i < samplesInChunk; i++ {
nativeFrame.RawData[samplesRead+i] = I(chunkBuf[i])
}
switch bitsAllocated {
case 8:
v, ok := any(pixelBuf[0]).(I)
if !ok {
return frame.Frame{}, bytesToRead, fmt.Errorf("internal error - readNativeFrame unexpectedly unable to type cast pixel buffer data to the I type (%T), where bitsAllocated=%v", *new(I), bitsAllocated)
}
nativeFrame.RawData[(pixel*samplesPerPixel)+value] = v
case 16:
v, ok := any(bo.Uint16(pixelBuf)).(I)
if !ok {
return frame.Frame{}, bytesToRead, fmt.Errorf("internal error - readNativeFrame unexpectedly unable to type cast pixel buffer data to the I type (%T), where bitsAllocated=%v", *new(I), bitsAllocated)
}
nativeFrame.RawData[(pixel*samplesPerPixel)+value] = v
case 32:
v, ok := any(bo.Uint32(pixelBuf)).(I)
if !ok {
return frame.Frame{}, bytesToRead, fmt.Errorf("internal error - readNativeFrame unexpectedly unable to type cast pixel buffer data to the I type (%T), where bitsAllocated=%v", *new(I), bitsAllocated)
}
nativeFrame.RawData[(pixel*samplesPerPixel)+value] = v
default:
return frame.Frame{}, bytesToRead, fmt.Errorf("readNativeFrame unsupported bitsAllocated=%d : %w", bitsAllocated, ErrorUnsupportedBitsAllocated)
case 16:
for i := 0; i < samplesInChunk; i++ {
nativeFrame.RawData[samplesRead+i] = I(bo.Uint16(chunkBuf[i*2 : i*2+2]))
}
case 32:
for i := 0; i < samplesInChunk; i++ {
nativeFrame.RawData[samplesRead+i] = I(bo.Uint32(chunkBuf[i*4 : i*4+4]))
}
default:
return frame.Frame{}, bytesToRead, fmt.Errorf("readNativeFrame unsupported bitsAllocated=%d : %w", bitsAllocated, ErrorUnsupportedBitsAllocated)
}
samplesRead += samplesInChunk
}
return currentFrame, bytesToRead, nil
}
Expand Down
12 changes: 10 additions & 2 deletions read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,8 +1032,15 @@ func BenchmarkReadNativeFrames(b *testing.B) {
dataset, rawReader := buildReadNativeFramesInput(c.Rows, c.Cols, c.NumFrames, c.SamplesPerPixel, b)
r := &reader{rawReader: rawReader}
b.ResetTimer()
// BitsAllocated is hardcoded to 16, meaning 2 bytes per sample.
bytesAllocated := 2
vl := uint32(c.Rows * c.Cols * c.NumFrames * c.SamplesPerPixel * bytesAllocated)

for i := 0; i < b.N; i++ {
_, _, _ = r.readNativeFrames(dataset, nil, uint32(c.Rows*c.Cols*c.NumFrames))
_, _, err := r.readNativeFrames(dataset, nil, vl)
if err != nil {
b.Fatalf("readNativeFrames error: %v", err)
}
}
})
}
Expand Down Expand Up @@ -1064,7 +1071,8 @@ func buildReadNativeFramesInput(rows, cols, numFrames, samplesPerPixel int, b *t
}
}

return &dataset, dicomio.NewReader(bufio.NewReader(&dcmdata), binary.LittleEndian, int64(dcmdata.Len()))
rawReader := dicomio.NewReader(bufio.NewReader(&dcmdata), binary.LittleEndian, int64(2*numFrames*rows*cols*samplesPerPixel))
return &dataset, rawReader
}

func buildTagData(t *testing.T, tg tag.Tag) []byte {
Expand Down
Loading