Skip to content

Commit

Permalink
Merge pull request #4 from okieraised/dev
Browse files Browse the repository at this point in the history
Fixed bug with bytes.Reader
  • Loading branch information
okieraised authored Feb 13, 2023
2 parents 16e9b37 + 35b3295 commit 32ef3d7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
36 changes: 31 additions & 5 deletions io.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func WithReadHeaderFile(headerFile string) func(*nifti.NiiReader) error {
}
// Check the content type to see if the file is gzipped. Do not depend on just the extensions of the file
bData, err = deflateFileContent(bData)
if err != nil {
return err
}
w.SetHdrReader(bytes.NewReader(bData))
return nil
}
Expand All @@ -77,22 +80,45 @@ func WithReadImageFile(niiFile string) func(*nifti.NiiReader) error {
}
// Check the content type to see if the file is gzipped. Do not depend on just the extensions of the file
bData, err = deflateFileContent(bData)
if err != nil {
return err
}
w.SetReader(bytes.NewReader(bData))
return nil
}
}

// WithReadImageReader allows option for users to specify the NIfTI bytes reader (.nii.gz or .nii)
func WithReadImageReader(r *bytes.Reader) func(*nifti.NiiReader) {
return func(w *nifti.NiiReader) {
w.SetReader(r)
func WithReadImageReader(r *bytes.Reader) func(*nifti.NiiReader) error {
return func(w *nifti.NiiReader) error {
bArr := make([]byte, r.Len())
_, err := r.Read(bArr)
if err != nil {
return err
}
bArr, err = deflateFileContent(bArr)
if err != nil {
return err
}
w.SetReader(bytes.NewReader(bArr))
return nil
}
}

// WithReadHeaderReader allows option for users to specify the separate header file reader in case of NIfTI pair .hdr/.img
func WithReadHeaderReader(r *bytes.Reader) func(*nifti.NiiReader) {
return func(w *nifti.NiiReader) {
func WithReadHeaderReader(r *bytes.Reader) func(*nifti.NiiReader) error {
return func(w *nifti.NiiReader) error {
bArr := make([]byte, r.Len())
_, err := r.Read(bArr)
if err != nil {
return err
}
bArr, err = deflateFileContent(bArr)
if err != nil {
return err
}
w.SetHdrReader(r)
return nil
}
}

Expand Down
29 changes: 29 additions & 0 deletions io_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package gonii

import (
"bytes"
"encoding/binary"
"fmt"
"github.com/okieraised/gonii/pkg/matrix"
"github.com/okieraised/gonii/pkg/nifti"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

Expand Down Expand Up @@ -280,3 +282,30 @@ func TestNewNiiWriter_90MB(t *testing.T) {
err = rd.Parse()
assert.NoError(err)
}

func TestNewNiiWriter_Nii2_BytesReader(t *testing.T) {
assert := assert.New(t)

filePath := "./test_data/int16.nii.gz"

bContent, err := os.ReadFile(filePath)
assert.NoError(err)

rd, err := NewNiiReader(WithReadImageReader(bytes.NewReader(bContent)))
assert.NoError(err)
err = rd.Parse()
assert.NoError(err)

voxels := rd.GetNiiData().GetVoxels()

err = rd.GetNiiData().SetVoxelToRawVolume(voxels)
assert.NoError(err)

writer, err := NewNiiWriter("./test_data/int16_nii2.nii.gz",
WithWriteNIfTIData(rd.GetNiiData()),
WithWriteCompression(true),
WithWriteVersion(2),
)
err = writer.WriteToFile()
assert.NoError(err)
}

0 comments on commit 32ef3d7

Please sign in to comment.