Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NativeFrame GetImage conversion test, fix row selection in conversion. #155

Merged
merged 2 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/frame/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
93 changes: 93 additions & 0 deletions pkg/frame/native_test.go
Original file line number Diff line number Diff line change
@@ -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
}