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.
This commit is contained in:
Ayke van Laethem
2024-10-28 09:48:09 +01:00
committed by Ron Evans
parent 0186d0905d
commit 7d7efe25e7
2 changed files with 7 additions and 4 deletions
+4 -4
View File
@@ -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)
+3
View File
@@ -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) {