diff --git a/read_test.go b/read_test.go index ce92ed40..d4bcd9ee 100644 --- a/read_test.go +++ b/read_test.go @@ -604,18 +604,66 @@ func TestReadNativeFrames(t *testing.T) { } } +func TestReadNativeFrames_MaxUInt8PixelValue(t *testing.T) { + // This tests that reading the maximum uint8 pixel value still works, when + // PixelRepresentation is 0, mostly as a regression test. + existingDS := Dataset{Elements: []*Element{ + mustNewElement(tag.Rows, []int{1}), + mustNewElement(tag.Columns, []int{1}), + mustNewElement(tag.NumberOfFrames, []string{"1"}), + mustNewElement(tag.BitsAllocated, []int{8}), + mustNewElement(tag.SamplesPerPixel, []int{1}), + mustNewElement(tag.PixelRepresentation, []int{0}), + }} + pixelData := []uint8{math.MaxUint8} + want := &PixelDataInfo{ + IsEncapsulated: false, + Frames: []*frame.Frame{ + { + Encapsulated: false, + NativeData: frame.NativeFrame{ + BitsPerSample: 8, + Rows: 1, + Cols: 1, + Data: [][]int{{math.MaxUint8}}, + }, + }, + }, + } + + dcmdata := bytes.Buffer{} + for _, item := range pixelData { + if err := binary.Write(&dcmdata, binary.LittleEndian, item); err != nil { + t.Errorf("TestReadNativeFrames: Unable to setup test buffer") + } + } + + r := &reader{ + rawReader: dicomio.NewReader(bufio.NewReader(&dcmdata), binary.LittleEndian, int64(dcmdata.Len())), + } + + parsedPixelData, _, err := r.readNativeFrames(&existingDS, nil, uint32(dcmdata.Len())) + if err != nil { + t.Errorf("TestReadNativeFrames(): did not get expected error. got: %v, want: %v", err, nil) + } + + if diff := cmp.Diff(want, parsedPixelData, cmpopts.EquateErrors()); diff != "" { + t.Errorf("TestReadNativeFrames(): unexpected diff: %v", diff) + } +} + func TestReadNativeFrames_MaxUInt16PixelValue(t *testing.T) { // This tests that reading the maximum uint16 pixel value still works, when // PixelRepresentation is 0, mostly as a regression test. existingDS := Dataset{Elements: []*Element{ - mustNewElement(tag.Rows, []int{5}), - mustNewElement(tag.Columns, []int{5}), + mustNewElement(tag.Rows, []int{1}), + mustNewElement(tag.Columns, []int{1}), mustNewElement(tag.NumberOfFrames, []string{"1"}), mustNewElement(tag.BitsAllocated, []int{16}), mustNewElement(tag.SamplesPerPixel, []int{1}), mustNewElement(tag.PixelRepresentation, []int{0}), }} - pixelData := []uint16{math.MaxUint16, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + pixelData := []uint16{math.MaxUint16} want := &PixelDataInfo{ IsEncapsulated: false, Frames: []*frame.Frame{ @@ -623,9 +671,57 @@ func TestReadNativeFrames_MaxUInt16PixelValue(t *testing.T) { Encapsulated: false, NativeData: frame.NativeFrame{ BitsPerSample: 16, - Rows: 5, - Cols: 5, - Data: [][]int{{math.MaxUint16}, {2}, {3}, {4}, {5}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}}, + Rows: 1, + Cols: 1, + Data: [][]int{{math.MaxUint16}}, + }, + }, + }, + } + + dcmdata := bytes.Buffer{} + for _, item := range pixelData { + if err := binary.Write(&dcmdata, binary.LittleEndian, item); err != nil { + t.Errorf("TestReadNativeFrames: Unable to setup test buffer") + } + } + + r := &reader{ + rawReader: dicomio.NewReader(bufio.NewReader(&dcmdata), binary.LittleEndian, int64(dcmdata.Len())), + } + + parsedPixelData, _, err := r.readNativeFrames(&existingDS, nil, uint32(dcmdata.Len())) + if err != nil { + t.Errorf("TestReadNativeFrames(): did not get expected error. got: %v, want: %v", err, nil) + } + + if diff := cmp.Diff(want, parsedPixelData, cmpopts.EquateErrors()); diff != "" { + t.Errorf("TestReadNativeFrames(): unexpected diff: %v", diff) + } +} + +func TestReadNativeFrames_MaxUInt32PixelValue(t *testing.T) { + // This tests that reading the maximum uint32 pixel value still works, when + // PixelRepresentation is 0, mostly as a regression test. + existingDS := Dataset{Elements: []*Element{ + mustNewElement(tag.Rows, []int{1}), + mustNewElement(tag.Columns, []int{1}), + mustNewElement(tag.NumberOfFrames, []string{"1"}), + mustNewElement(tag.BitsAllocated, []int{32}), + mustNewElement(tag.SamplesPerPixel, []int{1}), + mustNewElement(tag.PixelRepresentation, []int{0}), + }} + pixelData := []uint32{math.MaxUint32} + want := &PixelDataInfo{ + IsEncapsulated: false, + Frames: []*frame.Frame{ + { + Encapsulated: false, + NativeData: frame.NativeFrame{ + BitsPerSample: 32, + Rows: 1, + Cols: 1, + Data: [][]int{{math.MaxUint32}}, }, }, },