mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-11 02:13:42 +00:00
pixel: add Grayscale2bit color (#817)
* pixel: add GrayScale2bit color * pixel: fix spelling of 'GrayScale' to 'Grayscale'
This commit is contained in:
@@ -149,6 +149,20 @@ func (img Image[T]) setPixel(index int, c T) {
|
||||
}
|
||||
|
||||
return
|
||||
case zeroColor.BitsPerPixel() == 2:
|
||||
// Grayscale2bit.
|
||||
offset := index / 4 // 4 pixels per byte
|
||||
shift := 6 - (index%4)*2 // bits: 6, 4, 2, 0
|
||||
|
||||
ptr := (*byte)(unsafe.Add(img.data, offset))
|
||||
|
||||
raw := *(*uint8)(unsafe.Pointer(&c))
|
||||
gray := raw & 0b11
|
||||
|
||||
mask := byte(0b11 << shift)
|
||||
*ptr = (*ptr &^ mask) | (gray << shift)
|
||||
return
|
||||
|
||||
case zeroColor.BitsPerPixel()%8 == 0:
|
||||
// Each color starts at a whole byte offset.
|
||||
// This is the easy case.
|
||||
@@ -206,6 +220,13 @@ func (img Image[T]) Get(x, y int) T {
|
||||
ptr := (*byte)(unsafe.Add(img.data, offset))
|
||||
c = ((*ptr >> (7 - uint8(bits))) & 0x1) > 0
|
||||
return any(c).(T)
|
||||
case zeroColor.BitsPerPixel() == 2:
|
||||
// Grayscale2bit.
|
||||
offset := index / 4 // 4 pixels per byte
|
||||
shift := 6 - (index%4)*2 // bits: 6, 4, 2, 0
|
||||
ptr := (*byte)(unsafe.Add(img.data, offset))
|
||||
value := ((*ptr) >> shift) & 0b11
|
||||
return any(Grayscale2bit(value)).(T)
|
||||
case zeroColor.BitsPerPixel()%8 == 0:
|
||||
// Colors like RGB565, RGB888, etc.
|
||||
offset := index * int(unsafe.Sizeof(zeroColor))
|
||||
|
||||
Reference in New Issue
Block a user