From 7d7efe25e71711297043572129ad0ae794c90432 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Mon, 28 Oct 2024 09:48:09 +0100 Subject: [PATCH] pixel: fix Monochrome setPixel Set/setPixel and Get weren't using the same indices. I've taken the ones used in Get and applied them to setPixel too. This fixes testImageNoise for the monochrome image. --- pixel/image.go | 8 ++++---- pixel/image_test.go | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pixel/image.go b/pixel/image.go index 3275d63..b3ef562 100644 --- a/pixel/image.go +++ b/pixel/image.go @@ -138,14 +138,14 @@ func (img Image[T]) setPixel(index int, c T) { switch { case zeroColor.BitsPerPixel() == 1: // Monochrome. - x := index % int(img.width) offset := index / 8 + bits := index % 8 ptr := (*byte)(unsafe.Add(img.data, offset)) if c != zeroColor { - *((*byte)(ptr)) |= (1 << (7 - uint8(x%8))) + *((*byte)(ptr)) |= (1 << (7 - uint8(bits))) } else { - *((*byte)(ptr)) &^= (1 << (7 - uint8(x%8))) + *((*byte)(ptr)) &^= (1 << (7 - uint8(bits))) } return @@ -202,7 +202,7 @@ func (img Image[T]) Get(x, y int) T { // Monochrome. var c Monochrome offset := index / 8 - bits := index - (offset * 8) + bits := index % 8 ptr := (*byte)(unsafe.Add(img.data, offset)) c = ((*ptr >> (7 - uint8(bits))) & 0x1) > 0 return any(c).(T) diff --git a/pixel/image_test.go b/pixel/image_test.go index f192427..5e4804c 100644 --- a/pixel/image_test.go +++ b/pixel/image_test.go @@ -194,6 +194,9 @@ func TestImageNoise(t *testing.T) { t.Run("RGB444BE", func(t *testing.T) { testImageNoise[pixel.RGB444BE](t) }) + t.Run("Monochrome", func(t *testing.T) { + testImageNoise[pixel.Monochrome](t) + }) } func testImageNoise[T pixel.Color](t *testing.T) {