Skip to content

Commit

Permalink
image: add extra test for monochrome images
Browse files Browse the repository at this point in the history
This test needs tinygo-org/drivers#683 to pass,
but I think it's a useful test regardless.
  • Loading branch information
aykevl committed May 25, 2024
1 parent dce2ce8 commit 5606c1e
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,50 @@ func TestMono(t *testing.T) {
})
}
}

// Extra test to make sure decoding the image as pixel.Monochrome works as
// expected. (It previously didn't due to
// https://github.com/tinygo-org/drivers/pull/683).
func TestMonoMonochrome(t *testing.T) {
data, err := os.ReadFile("testdata/tinygo-logo-monochrome.raw")
if err != nil {
t.Fatal("could not read input file:", err)
}
img, e := image.NewMono[pixel.Monochrome](true, false, 304, 255, string(data))
if e != nil {
t.Fatal("could not decode input file:", e)
}

// Decode the image.
width, height := img.Size()
buf := pixel.NewImage[pixel.Monochrome](width, height)
img.Draw(buf, 0, 0, 1)

// Load the reference image.
f, err := os.Open("testdata/tinygo-logo-monochrome.png")
if err != nil {
t.Fatal("could not open reference image:", err)
}
defer f.Close()
golden, err := png.Decode(f)
if err != nil {
t.Fatal("could not decode reference image:", err)
}

// Check that the decoded image matches the golden image.
fg := color.RGBA{192, 192, 192, 255}
bg := color.RGBA{32, 32, 32, 255}
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
c1 := bg
if buf.Get(x, y) {
c1 = fg
}
r2, g2, b2, _ := golden.At(x, y).RGBA()
c2 := color.RGBA{R: uint8(r2 >> 8), G: uint8(g2 >> 8), B: uint8(b2 >> 8), A: 255}
if c1 != c2 {
t.Fatalf("mismatch at %dx%d: expected %v got %v", x, y, c2, c1)
}
}
}
}

0 comments on commit 5606c1e

Please sign in to comment.