Skip to content

Commit 5606c1e

Browse files
committed
image: add extra test for monochrome images
This test needs tinygo-org/drivers#683 to pass, but I think it's a useful test regardless.
1 parent dce2ce8 commit 5606c1e

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

image/image_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,50 @@ func TestMono(t *testing.T) {
9999
})
100100
}
101101
}
102+
103+
// Extra test to make sure decoding the image as pixel.Monochrome works as
104+
// expected. (It previously didn't due to
105+
// https://github.com/tinygo-org/drivers/pull/683).
106+
func TestMonoMonochrome(t *testing.T) {
107+
data, err := os.ReadFile("testdata/tinygo-logo-monochrome.raw")
108+
if err != nil {
109+
t.Fatal("could not read input file:", err)
110+
}
111+
img, e := image.NewMono[pixel.Monochrome](true, false, 304, 255, string(data))
112+
if e != nil {
113+
t.Fatal("could not decode input file:", e)
114+
}
115+
116+
// Decode the image.
117+
width, height := img.Size()
118+
buf := pixel.NewImage[pixel.Monochrome](width, height)
119+
img.Draw(buf, 0, 0, 1)
120+
121+
// Load the reference image.
122+
f, err := os.Open("testdata/tinygo-logo-monochrome.png")
123+
if err != nil {
124+
t.Fatal("could not open reference image:", err)
125+
}
126+
defer f.Close()
127+
golden, err := png.Decode(f)
128+
if err != nil {
129+
t.Fatal("could not decode reference image:", err)
130+
}
131+
132+
// Check that the decoded image matches the golden image.
133+
fg := color.RGBA{192, 192, 192, 255}
134+
bg := color.RGBA{32, 32, 32, 255}
135+
for y := 0; y < height; y++ {
136+
for x := 0; x < width; x++ {
137+
c1 := bg
138+
if buf.Get(x, y) {
139+
c1 = fg
140+
}
141+
r2, g2, b2, _ := golden.At(x, y).RGBA()
142+
c2 := color.RGBA{R: uint8(r2 >> 8), G: uint8(g2 >> 8), B: uint8(b2 >> 8), A: 255}
143+
if c1 != c2 {
144+
t.Fatalf("mismatch at %dx%d: expected %v got %v", x, y, c2, c1)
145+
}
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)