Skip to content

Commit 2f3b5ca

Browse files
committed
pixel: correct and clarify code for monochrome get/set pixels
Signed-off-by: deadprogram <ron@hybridgroup.com>
1 parent ecae5e2 commit 2f3b5ca

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

pixel/image.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,15 @@ func (img Image[T]) setPixel(index int, c T) {
139139
case zeroColor.BitsPerPixel() == 1:
140140
// Monochrome.
141141
x := index % int(img.width)
142-
y := index / int(img.width)
143-
offset := x + (y/8)*int(img.width)
142+
offset := index / 8
143+
144144
ptr := (*byte)(unsafe.Add(img.data, offset))
145145
if c != zeroColor {
146-
*((*byte)(ptr)) |= 1 << uint8(y%8)
146+
*((*byte)(ptr)) |= (1 << (7 - uint8(x%8)))
147147
} else {
148-
*((*byte)(ptr)) &^= 1 << uint8(y%8)
148+
*((*byte)(ptr)) &^= (1 << (7 - uint8(x%8)))
149149
}
150+
150151
return
151152
case zeroColor.BitsPerPixel()%8 == 0:
152153
// Each color starts at a whole byte offset.
@@ -200,9 +201,10 @@ func (img Image[T]) Get(x, y int) T {
200201
case zeroColor.BitsPerPixel() == 1:
201202
// Monochrome.
202203
var c Monochrome
203-
offset := x + (y/8)*int(img.width)
204+
offset := index / 8
205+
bits := index - (offset * 8)
204206
ptr := (*byte)(unsafe.Add(img.data, offset))
205-
c = (*ptr >> uint8(y%8) & 0x1) == 1
207+
c = ((*ptr >> (7 - uint8(bits))) & 0x1) > 0
206208
return any(c).(T)
207209
case zeroColor.BitsPerPixel()%8 == 0:
208210
// Colors like RGB565, RGB888, etc.

0 commit comments

Comments
 (0)