mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-16 12:53:23 +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:
|
case zeroColor.BitsPerPixel() == 1:
|
||||||
// Monochrome.
|
// Monochrome.
|
||||||
x := index % int(img.width)
|
x := index % int(img.width)
|
||||||
y := index / int(img.width)
|
offset := index / 8
|
||||||
offset := x + (y/8)*int(img.width)
|
|
||||||
ptr := (*byte)(unsafe.Add(img.data, offset))
|
ptr := (*byte)(unsafe.Add(img.data, offset))
|
||||||
if c != zeroColor {
|
if c != zeroColor {
|
||||||
*((*byte)(ptr)) |= 1 << uint8(y%8)
|
*((*byte)(ptr)) |= (1 << (7 - uint8(x%8)))
|
||||||
} else {
|
} else {
|
||||||
*((*byte)(ptr)) &^= 1 << uint8(y%8)
|
*((*byte)(ptr)) &^= (1 << (7 - uint8(x%8)))
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
case zeroColor.BitsPerPixel()%8 == 0:
|
case zeroColor.BitsPerPixel()%8 == 0:
|
||||||
// Each color starts at a whole byte offset.
|
// 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:
|
case zeroColor.BitsPerPixel() == 1:
|
||||||
// Monochrome.
|
// Monochrome.
|
||||||
var c 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))
|
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)
|
return any(c).(T)
|
||||||
case zeroColor.BitsPerPixel()%8 == 0:
|
case zeroColor.BitsPerPixel()%8 == 0:
|
||||||
// Colors like RGB565, RGB888, etc.
|
// Colors like RGB565, RGB888, etc.
|
||||||
|
|||||||
Reference in New Issue
Block a user