Skip to content

Commit 7d7efe2

Browse files
aykevldeadprogram
authored andcommitted
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.
1 parent 0186d09 commit 7d7efe2

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

pixel/image.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ func (img Image[T]) setPixel(index int, c T) {
138138
switch {
139139
case zeroColor.BitsPerPixel() == 1:
140140
// Monochrome.
141-
x := index % int(img.width)
142141
offset := index / 8
142+
bits := index % 8
143143

144144
ptr := (*byte)(unsafe.Add(img.data, offset))
145145
if c != zeroColor {
146-
*((*byte)(ptr)) |= (1 << (7 - uint8(x%8)))
146+
*((*byte)(ptr)) |= (1 << (7 - uint8(bits)))
147147
} else {
148-
*((*byte)(ptr)) &^= (1 << (7 - uint8(x%8)))
148+
*((*byte)(ptr)) &^= (1 << (7 - uint8(bits)))
149149
}
150150

151151
return
@@ -202,7 +202,7 @@ func (img Image[T]) Get(x, y int) T {
202202
// Monochrome.
203203
var c Monochrome
204204
offset := index / 8
205-
bits := index - (offset * 8)
205+
bits := index % 8
206206
ptr := (*byte)(unsafe.Add(img.data, offset))
207207
c = ((*ptr >> (7 - uint8(bits))) & 0x1) > 0
208208
return any(c).(T)

pixel/image_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ func TestImageNoise(t *testing.T) {
194194
t.Run("RGB444BE", func(t *testing.T) {
195195
testImageNoise[pixel.RGB444BE](t)
196196
})
197+
t.Run("Monochrome", func(t *testing.T) {
198+
testImageNoise[pixel.Monochrome](t)
199+
})
197200
}
198201

199202
func testImageNoise[T pixel.Color](t *testing.T) {

0 commit comments

Comments
 (0)