Skip to content

Commit

Permalink
Fix: remove test helpers library and change the byte array order
Browse files Browse the repository at this point in the history
  • Loading branch information
quang18 committed May 28, 2024
1 parent 2a226e4 commit 16701fa
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 76 deletions.
9 changes: 1 addition & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,4 @@ require (
golang.org/x/text v0.3.8
)

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
)
require golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
17 changes: 0 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
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 @@ -25,7 +12,3 @@ 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=
10 changes: 5 additions & 5 deletions pkg/frame/inplace/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ func ReadUnprocessedValueData(info *PixelDataMetadata, unprocessedValueData []by
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
re := make([][]int, samplesPerPixel)
for i := 0; i < samplesPerPixel; i++ {
re[i] = make([]int, pixelsPerFrame)
for j := 0; j < pixelsPerFrame; j++ {
pointOffset := offset + j*info.SamplesPerPixel*bytesAllocated + i*bytesAllocated
switch bytesAllocated {
case 1:
re[i][j] = int(unprocessedValueData[pointOffset])
Expand Down
64 changes: 51 additions & 13 deletions pkg/frame/inplace/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package inplace

import (
"bytes"
"fmt"
"io"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

"github.com/suyashkumar/dicom"
"github.com/suyashkumar/dicom/pkg/frame"
Expand All @@ -15,8 +16,11 @@ import (
)

func mustNewElement(t *testing.T, tag tag.Tag, data interface{}) *dicom.Element {
t.Helper()
elem, err := dicom.NewElement(tag, data)
require.NoError(t, err)
if err != nil {
t.Errorf("dicom.NewElement: %v", err)
}
return elem
}

Expand Down Expand Up @@ -189,31 +193,65 @@ func TestReadReadUnprocessedValueData(t *testing.T) {
for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
var filesOut bytes.Buffer
require.NoError(t, dicom.Write(io.Writer(&filesOut), tc.existingData))
var err error
if err = dicom.Write(io.Writer(&filesOut), tc.existingData); err != nil {
t.Errorf("Write DICOM obj to bytes: %v", err)
}
rawData := filesOut.Bytes()

dataset, err := dicom.Parse(bytes.NewReader(rawData), int64(len(rawData)), nil, dicom.SkipProcessingPixelDataValue())
require.NoError(t, err)
if err != nil {
t.Errorf("dicom.Parse: %v", err)
}
metadata, err := GetPixelDataMetadata(&dataset)
require.NoError(t, err)
if err != nil {
t.Errorf("GetPixelDataMetadata: %v", err)
}
pixelElement, err := dataset.FindElementByTag(tag.PixelData)
require.NoError(t, err)
if err != nil {
t.Errorf("tag.PixelData not found: %v", err)
}
pixelDataInfo := dicom.MustGetPixelDataInfo(pixelElement.Value)
assert.Equal(t, pixelDataInfo.IntentionallyUnprocessed, true)
assert.Equal(t, pixelDataInfo.IsEncapsulated, false)
if pixelDataInfo.IsEncapsulated || !pixelDataInfo.IntentionallyUnprocessed {
t.Errorf("pixelDataInfo should be IntentionallyUnprocessed: IsEncapsulated=%t IntentionallyUnprocessed%t",
pixelDataInfo.IsEncapsulated, pixelDataInfo.IntentionallyUnprocessed)
}

require.NoError(t, IsSafeForUnprocessedValueDataHandling(metadata, pixelDataInfo.UnprocessedValueData))
if err = IsSafeForUnprocessedValueDataHandling(metadata, pixelDataInfo.UnprocessedValueData); err != nil {
t.Errorf("IsSafeForUnprocessedValueDataHandling(%v)", err)
}

originPixelElement, err := tc.existingData.FindElementByTag(tag.PixelData)
require.NoError(t, err)
if err != nil {
t.Errorf("Find tag.PixelData(%v)", err)
}
originPixelDataInfo := dicom.MustGetPixelDataInfo(originPixelElement.Value)

for i := 0; i < metadata.Frames; i++ {
originData := originPixelDataInfo.Frames[i].NativeData.Data
inplaceData, err := ReadUnprocessedValueData(metadata, pixelDataInfo.UnprocessedValueData, i)
require.NoError(t, err)
assert.Equal(t, originData, inplaceData, "missing match value", i)
if err != nil {
t.Errorf("ReadUnprocessedValueData(%v)", err)
}
assertUnprocessedPixelData(t, originData, inplaceData, fmt.Sprint("frame:", i))
}
})
}
}

func assertUnprocessedPixelData(t *testing.T, inplaceData [][]int, originData [][]int, msg string) {
if len(inplaceData[0]) != len(originData) || len(inplaceData) != len(originData[0]) {
t.Errorf("Mismatch data size: origin-data(%d-%d) inplace-data(%d-%d)",
len(originData), len(originData[0]), len(inplaceData), len(inplaceData[0]))
}
tmp := make([][]int, len(originData))
for i := 0; i < len(tmp); i++ {
tmp[i] = make([]int, len(originData[0]))
for j := 0; j < len(originData[0]); j++ {
tmp[i][j] = inplaceData[j][i]
}
}
if diff := cmp.Diff(originData, tmp, cmpopts.EquateErrors()); diff != "" {
t.Errorf("assertUnprocessedPixelData(%v-%s): unexpected diff: %v", inplaceData, msg, diff)
}
}
93 changes: 60 additions & 33 deletions pkg/frame/inplace/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"io"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

"github.com/suyashkumar/dicom"
"github.com/suyashkumar/dicom/pkg/frame"
Expand Down Expand Up @@ -52,7 +52,9 @@ func TestWriteUnprocessedValueData(t *testing.T) {
},
}
var buffer bytes.Buffer
require.NoError(t, dicom.Write(io.Writer(&buffer), originDataset))
if err := dicom.Write(io.Writer(&buffer), originDataset); err != nil {
t.Errorf("write from dataset to DICOM bytes: %v", err)
}
rawData := buffer.Bytes()

for _, tc := range []struct {
Expand Down Expand Up @@ -96,21 +98,17 @@ func TestWriteUnprocessedValueData(t *testing.T) {
},
},
} {
dataset, err := dicom.Parse(bytes.NewReader(rawData), int64(len(rawData)), nil, dicom.SkipProcessingPixelDataValue())
require.NoError(t, err)
metadata, err := GetPixelDataMetadata(&dataset)
require.NoError(t, err)
pixelElement, err := dataset.FindElementByTag(tag.PixelData)
require.NoError(t, err)
pixelDataInfo := dicom.MustGetPixelDataInfo(pixelElement.Value)
assert.Equal(t, pixelDataInfo.IntentionallyUnprocessed, true)
assert.Equal(t, pixelDataInfo.IsEncapsulated, false)
// Sanity check if the dataset is suitable for in-place read-write
require.NoError(t, IsSafeForUnprocessedValueDataHandling(metadata, pixelDataInfo.UnprocessedValueData))
var err error
dataset, metadata, pixelDataInfo := setupUnprocessedPixelData(t, rawData)
// in-place write then converts to bytes
require.NoError(t, WriteUnprocessedValueData(metadata, pixelDataInfo.UnprocessedValueData, tc.rowIndex, tc.colIndex, tc.frameIndex, tc.sampleIndex, 100))
if err = WriteUnprocessedValueData(metadata, pixelDataInfo.UnprocessedValueData,
tc.rowIndex, tc.colIndex, tc.frameIndex, tc.sampleIndex, 100); err != nil {
t.Errorf("WriteUnprocessedValueData: %v", err)
}
var buffer2 bytes.Buffer
require.NoError(t, dicom.Write(io.Writer(&buffer2), dataset))
if err = dicom.Write(io.Writer(&buffer2), dataset); err != nil {
t.Errorf("Write new DICOM obj to file: %v", err)
}
outputBytes := buffer2.Bytes()
assertPixelDataInfo(t, outputBytes, tc.expectedPixelInfo)
}
Expand Down Expand Up @@ -154,7 +152,9 @@ func TestWriteUnprocessedValueData_BigEndian(t *testing.T) {
},
}
var buffer bytes.Buffer
require.NoError(t, dicom.Write(io.Writer(&buffer), originDataset))
if err := dicom.Write(io.Writer(&buffer), originDataset); err != nil {
t.Errorf("write from dataset to DICOM bytes: %v", err)
}
rawData := buffer.Bytes()

for _, tc := range []struct {
Expand Down Expand Up @@ -198,31 +198,58 @@ func TestWriteUnprocessedValueData_BigEndian(t *testing.T) {
},
},
} {
dataset, err := dicom.Parse(bytes.NewReader(rawData), int64(len(rawData)), nil, dicom.SkipProcessingPixelDataValue())
require.NoError(t, err)
metadata, err := GetPixelDataMetadata(&dataset)
require.NoError(t, err)
pixelElement, err := dataset.FindElementByTag(tag.PixelData)
require.NoError(t, err)
pixelDataInfo := dicom.MustGetPixelDataInfo(pixelElement.Value)
assert.Equal(t, pixelDataInfo.IntentionallyUnprocessed, true)
assert.Equal(t, pixelDataInfo.IsEncapsulated, false)
// Sanity check if the dataset is suitable for in-place read-write
require.NoError(t, IsSafeForUnprocessedValueDataHandling(metadata, pixelDataInfo.UnprocessedValueData))
dataset, metadata, pixelDataInfo := setupUnprocessedPixelData(t, rawData)
var err error
// in-place write then converts to bytes
require.NoError(t, WriteUnprocessedValueData(metadata, pixelDataInfo.UnprocessedValueData, tc.rowIndex, tc.colIndex, tc.frameIndex, tc.sampleIndex, 100))
if err = WriteUnprocessedValueData(metadata, pixelDataInfo.UnprocessedValueData,
tc.rowIndex, tc.colIndex, tc.frameIndex, tc.sampleIndex, 100); err != nil {
t.Errorf("WriteUnprocessedValueData: %v", err)
}
var buffer2 bytes.Buffer
require.NoError(t, dicom.Write(io.Writer(&buffer2), dataset))
if err = dicom.Write(io.Writer(&buffer2), dataset); err != nil {
t.Errorf("Write new DICOM obj to file: %v", err)
}
outputBytes := buffer2.Bytes()
assertPixelDataInfo(t, outputBytes, tc.expectedPixelInfo)
}
}

func setupUnprocessedPixelData(t testing.TB, rawData []byte) (dicom.Dataset, *PixelDataMetadata, dicom.PixelDataInfo) {
dataset, err := dicom.Parse(bytes.NewReader(rawData), int64(len(rawData)), nil, dicom.SkipProcessingPixelDataValue())
if err != nil {
t.Errorf("dicom.Parse: %v", err)
}
metadata, err := GetPixelDataMetadata(&dataset)
if err != nil {
t.Errorf("GetPixelDataMetadata: %v", err)
}
pixelElement, err := dataset.FindElementByTag(tag.PixelData)
if err != nil {
t.Errorf("tag.PixelData not found: %v", err)
}
pixelDataInfo := dicom.MustGetPixelDataInfo(pixelElement.Value)
if !pixelDataInfo.IntentionallyUnprocessed || pixelDataInfo.IsEncapsulated {
t.Errorf("unexpected pixelDataInfo: IntentionallyUnprocessed=%t IsEncapsulated=%t",
pixelDataInfo.IntentionallyUnprocessed, pixelDataInfo.IsEncapsulated)
}
// Sanity check if the dataset is suitable for in-place read-write
if err = IsSafeForUnprocessedValueDataHandling(metadata, pixelDataInfo.UnprocessedValueData); err != nil {
t.Errorf("IsSafeForUnprocessedValueDataHandling(%v)", err)
}
return dataset, metadata, pixelDataInfo
}

func assertPixelDataInfo(t testing.TB, rawData []byte, expectedPixelDataInfo dicom.PixelDataInfo) {
dataset2, err := dicom.Parse(bytes.NewReader(rawData), int64(len(rawData)), nil)
require.NoError(t, err)
if err != nil {
t.Errorf("read new create dcm file: %v", err)
}
pixelElement, err := dataset2.FindElementByTag(tag.PixelData)
require.NoError(t, err)
if err != nil {
t.Errorf("pixel data not found: %v", err)
}
pixelDataInfo := dicom.MustGetPixelDataInfo(pixelElement.Value)
require.Equal(t, pixelDataInfo, expectedPixelDataInfo)
if diff := cmp.Diff(pixelDataInfo, expectedPixelDataInfo, cmpopts.EquateErrors()); diff != "" {
t.Errorf("assert pixelDataInfo in dicom file(%v): unexpected diff: %v", pixelDataInfo, diff)
}
}

0 comments on commit 16701fa

Please sign in to comment.