Skip to content

Commit

Permalink
Add NativeFrame GetImage conversion test, fix row selection in conver…
Browse files Browse the repository at this point in the history
…sion. (#155)

This adds a more comprehensive test for converting NativeFrame data into an image, and also fixes the row selector in GetImage.
  • Loading branch information
suyashkumar authored Dec 15, 2020
1 parent 327782b commit 72c72a2
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
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
}

0 comments on commit 72c72a2

Please sign in to comment.