mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-07-26 10:38:41 +00:00
pixel: correct and clarify code for monochrome get/set pixels
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
+8
-6
@@ -139,14 +139,15 @@ func (img Image[T]) setPixel(index int, c T) {
|
||||
case zeroColor.BitsPerPixel() == 1:
|
||||
// Monochrome.
|
||||
x := index % int(img.width)
|
||||
y := index / int(img.width)
|
||||
offset := x + (y/8)*int(img.width)
|
||||
offset := index / 8
|
||||
|
||||
ptr := (*byte)(unsafe.Add(img.data, offset))
|
||||
if c != zeroColor {
|
||||
*((*byte)(ptr)) |= 1 << uint8(y%8)
|
||||
*((*byte)(ptr)) |= (1 << (7 - uint8(x%8)))
|
||||
} else {
|
||||
*((*byte)(ptr)) &^= 1 << uint8(y%8)
|
||||
*((*byte)(ptr)) &^= (1 << (7 - uint8(x%8)))
|
||||
}
|
||||
|
||||
return
|
||||
case zeroColor.BitsPerPixel()%8 == 0:
|
||||
// Each color starts at a whole byte offset.
|
||||
@@ -200,9 +201,10 @@ func (img Image[T]) Get(x, y int) T {
|
||||
case zeroColor.BitsPerPixel() == 1:
|
||||
// Monochrome.
|
||||
var c Monochrome
|
||||
offset := x + (y/8)*int(img.width)
|
||||
offset := index / 8
|
||||
bits := index - (offset * 8)
|
||||
ptr := (*byte)(unsafe.Add(img.data, offset))
|
||||
c = (*ptr >> uint8(y%8) & 0x1) == 1
|
||||
c = ((*ptr >> (7 - uint8(bits))) & 0x1) > 0
|
||||
return any(c).(T)
|
||||
case zeroColor.BitsPerPixel()%8 == 0:
|
||||
// Colors like RGB565, RGB888, etc.
|
||||
|
||||
Reference in New Issue
Block a user