Skip to content

Commit

Permalink
Reduce allocations during readNativeFrames with frame channel
Browse files Browse the repository at this point in the history
  • Loading branch information
segmed-lam committed Nov 6, 2023
1 parent 8f1f151 commit 98a517c
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ func (r *reader) readPixelData(vl uint32, d *Dataset, fc chan<- *frame.Frame) (V

if fc != nil {
fc <- &f
} else {
image.Frames = append(image.Frames, &f)
}

image.Frames = append(image.Frames, &f)
}
image.IntentionallySkipped = r.opts.skipPixelData
return &pixelDataValue{PixelDataInfo: image}, nil
Expand Down Expand Up @@ -440,18 +440,23 @@ func (r *reader) readNativeFrames(parsedData *Dataset, fc chan<- *frame.Frame, v
image.Frames = make([]*frame.Frame, nFrames)
bo := r.rawReader.ByteOrder()
pixelBuf := make([]byte, bytesAllocated)
nativeDataBuf := make([][]int, pixelsPerFrame)
buf := make([]int, pixelsPerFrame*samplesPerPixel)
for frameIdx := 0; frameIdx < nFrames; frameIdx++ {
if fc == nil {
nativeDataBuf = make([][]int, pixelsPerFrame)
buf = make([]int, pixelsPerFrame*samplesPerPixel)
}
// Init current frame
currentFrame := frame.Frame{
Encapsulated: false,
NativeData: frame.NativeFrame{
BitsPerSample: bitsAllocated,
Rows: MustGetInts(rows.Value)[0],
Cols: MustGetInts(cols.Value)[0],
Data: make([][]int, pixelsPerFrame),
Data: nativeDataBuf,
},
}
buf := make([]int, pixelsPerFrame*samplesPerPixel)
if bitsAllocated == 1 {
if err := fillBufferSingleBitAllocated(buf, r.rawReader, bo); err != nil {
return nil, bytesToRead, err
Expand Down Expand Up @@ -482,9 +487,10 @@ func (r *reader) readNativeFrames(parsedData *Dataset, fc chan<- *frame.Frame, v
currentFrame.NativeData.Data[pixel] = buf[pixel*samplesPerPixel : (pixel+1)*samplesPerPixel]
}
}
image.Frames[frameIdx] = &currentFrame
if fc != nil {
fc <- &currentFrame // write the current frame to the frame channel
} else {
image.Frames[frameIdx] = &currentFrame
}
}
if skipFinalPaddingByte {
Expand Down

0 comments on commit 98a517c

Please sign in to comment.