diff --git a/pkg/frame/native.go b/pkg/frame/native.go index 6e7a5c47..9d00e459 100644 --- a/pkg/frame/native.go +++ b/pkg/frame/native.go @@ -35,7 +35,7 @@ func (n *NativeFrame) GetEncapsulatedFrame() (*EncapsulatedFrame, error) { func (n *NativeFrame) GetImage() (image.Image, error) { i := image.NewGray16(image.Rect(0, 0, n.Cols, n.Rows)) for j := 0; j < len(n.Data); j++ { - i.SetGray16(j%n.Cols, j/n.Rows, color.Gray16{Y: uint16(n.Data[j][0])}) // for now, assume we're not overflowing uint16, assume gray image + i.SetGray16(j%n.Cols, j/n.Cols, color.Gray16{Y: uint16(n.Data[j][0])}) // for now, assume we're not overflowing uint16, assume gray image } return i, nil } diff --git a/pkg/frame/native_test.go b/pkg/frame/native_test.go new file mode 100644 index 00000000..3d3ed3ba --- /dev/null +++ b/pkg/frame/native_test.go @@ -0,0 +1,93 @@ +package frame_test + +import ( + "image" + "testing" + + "github.com/suyashkumar/dicom/pkg/frame" +) + +// point represents a 2D point for testing. +type point struct { + x int + y int +} + +func TestNativeFrame_GetImage(t *testing.T) { + cases := []struct { + Name string + NativeFrame frame.NativeFrame + SetPoints []point + }{ + { + Name: "Square", + NativeFrame: frame.NativeFrame{ + Rows: 2, + Cols: 2, + Data: [][]int{{0}, {0}, {1}, {0}}, + }, + SetPoints: []point{{0, 1}}, + }, + { + Name: "Rectangle", + NativeFrame: frame.NativeFrame{ + Rows: 3, + Cols: 2, + Data: [][]int{{0}, {0}, {0}, {0}, {1}, {0}}, + }, + SetPoints: []point{{0, 2}}, + }, + { + Name: "Rectangle - multiple points", + NativeFrame: frame.NativeFrame{ + Rows: 5, + Cols: 3, + Data: [][]int{{0}, {0}, {0}, {0}, {1}, {1}, {0}, {0}, {0}, {0}, {1}, {0}, {0}, {0}, {0}}, + }, + SetPoints: []point{{1, 1}, {2, 1}, {1, 3}}, + }, + } + + for _, tc := range cases { + t.Run(tc.Name, func(t *testing.T) { + img, err := tc.NativeFrame.GetImage() + if err != nil { + t.Fatalf("GetImage(%v) got unexpected error: %v", tc.NativeFrame, err) + } + imgGray, ok := img.(*image.Gray16) + if !ok { + t.Fatalf("GetImage(%v) did not return an image convertible to Gray16", tc.NativeFrame) + } + + // Check that all pixels are zero except at the + // (ExpectedSetPointX, ExpectedSetPointY) point. + for x := 0; x < tc.NativeFrame.Cols; x++ { + for y := 0; y < tc.NativeFrame.Rows; y++ { + currValue := imgGray.Gray16At(x, y).Y + if within(point{x, y}, tc.SetPoints) { + if currValue != 1 { + t.Errorf("GetImage(%v), unexpected value at set point. got: %v, want: %v", tc.NativeFrame, currValue, 1) + } + continue + } + + if currValue != 0 { + t.Errorf("GetImage(%v), unexpected value outside of set point. At (%d, %d) got: %v, want: %v", tc.NativeFrame, x, y, currValue, 0) + } + + } + } + }) + + } +} + +// within returns true if pt is in the []point +func within(pt point, set []point) bool { + for _, item := range set { + if pt.x == item.x && pt.y == item.y { + return true + } + } + return false +}