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) {