xgb-image: support 10-bit colours

Apparently, a DefaultDepth 30 Xserver won't do translucency.
This commit is contained in:
Přemysl Eric Janouch 2021-07-15 01:43:13 +02:00
parent 5ae8c24b8d
commit a927713a81
Signed by: p
GPG Key ID: A0420B94F92B9493

View File

@ -14,6 +14,7 @@ import (
"github.com/BurntSushi/xgb/xproto" "github.com/BurntSushi/xgb/xproto"
"image" "image"
"image/color"
_ "image/gif" _ "image/gif"
_ "image/jpeg" _ "image/jpeg"
_ "image/png" _ "image/png"
@ -26,6 +27,47 @@ import "C"
func F64ToFixed(f float64) render.Fixed { return render.Fixed(f * 65536) } func F64ToFixed(f float64) render.Fixed { return render.Fixed(f * 65536) }
func FixedToF64(f render.Fixed) float64 { return float64(f) / 65536 } func FixedToF64(f render.Fixed) float64 { return float64(f) / 65536 }
var formats = map[byte]struct {
format render.Directformat
transform func(color.Color) uint32
}{
32: {
format: render.Directformat{
RedShift: 16,
RedMask: 0xff,
GreenShift: 8,
GreenMask: 0xff,
BlueShift: 0,
BlueMask: 0xff,
AlphaShift: 24,
AlphaMask: 0xff,
},
transform: func(color color.Color) uint32 {
r, g, b, a := color.RGBA()
return (a>>8)<<24 | (r>>8)<<16 | (g>>8)<<8 | (b >> 8)
},
},
30: {
/*
// Alpha makes compositing unbearably slow.
AlphaShift: 30,
AlphaMask: 0x3,
*/
format: render.Directformat{
RedShift: 20,
RedMask: 0x3ff,
GreenShift: 10,
GreenMask: 0x3ff,
BlueShift: 0,
BlueMask: 0x3ff,
},
transform: func(color color.Color) uint32 {
r, g, b, a := color.RGBA()
return (a>>14)<<30 | (r>>6)<<20 | (g>>6)<<10 | (b >> 6)
},
},
}
func main() { func main() {
/* /*
pf, err := os.Create("pprof.out") pf, err := os.Create("pprof.out")
@ -63,18 +105,35 @@ func main() {
screen := setup.DefaultScreen(X) screen := setup.DefaultScreen(X)
visual, depth := screen.RootVisual, screen.RootDepth visual, depth := screen.RootVisual, screen.RootDepth
// TODO: We should check that we find it, though we don't /need/ alpha here,
// it's just a minor improvement--affects the backpixel value. // Only go for 10-bit when the picture can make use of that range.
prefer30 := false
switch img.(type) {
case *image.Gray16, *image.RGBA64, *image.NRGBA64:
prefer30 = true
}
// XXX: We don't /need/ alpha here, it's just a minor improvement--affects
// the backpixel value. (And we reject it in 30-bit depth anyway.)
for _, i := range screen.AllowedDepths { for _, i := range screen.AllowedDepths {
for _, v := range i.Visuals { for _, v := range i.Visuals {
// TODO: Could/should check other parameters. // TODO: Could/should check other parameters.
if i.Depth == 32 && v.Class == xproto.VisualClassTrueColor { if v.Class != xproto.VisualClassTrueColor {
continue
}
if prefer30 && i.Depth == 30 ||
!prefer30 && i.Depth == 32 {
visual, depth = v.VisualId, i.Depth visual, depth = v.VisualId, i.Depth
break break
} }
} }
} }
format, ok := formats[depth]
if !ok {
log.Fatalln("unsupported bit depth")
}
mid, err := xproto.NewColormapId(X) mid, err := xproto.NewColormapId(X)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
@ -92,7 +151,7 @@ func main() {
_ = xproto.CreateWindow(X, depth, wid, screen.Root, _ = xproto.CreateWindow(X, depth, wid, screen.Root,
0, 0, 500, 500, 0, xproto.WindowClassInputOutput, 0, 0, 500, 500, 0, xproto.WindowClassInputOutput,
visual, xproto.CwBackPixel|xproto.CwBorderPixel|xproto.CwEventMask| visual, xproto.CwBackPixel|xproto.CwBorderPixel|xproto.CwEventMask|
xproto.CwColormap, []uint32{0x80808080, 0, xproto.CwColormap, []uint32{format.transform(color.Alpha{0x80}), 0,
xproto.EventMaskStructureNotify | xproto.EventMaskExposure, xproto.EventMaskStructureNotify | xproto.EventMaskExposure,
uint32(mid)}) uint32(mid)})
@ -132,7 +191,7 @@ func main() {
// setup.BitmapFormatScanline{Pad,Unit} and setup.BitmapFormatBitOrder // setup.BitmapFormatScanline{Pad,Unit} and setup.BitmapFormatBitOrder
// don't interest us here since we're only using Z format pixmaps. // don't interest us here since we're only using Z format pixmaps.
for _, pf := range setup.PixmapFormats { for _, pf := range setup.PixmapFormats {
if pf.Depth == 32 { if pf.Depth == depth {
if pf.BitsPerPixel != 32 || pf.ScanlinePad != 32 { if pf.BitsPerPixel != 32 || pf.ScanlinePad != 32 {
log.Fatalln("unsuported X server") log.Fatalln("unsuported X server")
} }
@ -143,28 +202,18 @@ func main() {
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
_ = xproto.CreatePixmap(X, 32, pixid, xproto.Drawable(screen.Root), _ = xproto.CreatePixmap(X, depth, pixid, xproto.Drawable(screen.Root),
uint16(img.Bounds().Dx()), uint16(img.Bounds().Dy())) uint16(img.Bounds().Dx()), uint16(img.Bounds().Dy()))
var bgraFormat render.Pictformat var bgraFormat render.Pictformat
wanted := render.Directformat{
RedShift: 16,
RedMask: 0xff,
GreenShift: 8,
GreenMask: 0xff,
BlueShift: 0,
BlueMask: 0xff,
AlphaShift: 24,
AlphaMask: 0xff,
}
for _, pf := range pformats.Formats { for _, pf := range pformats.Formats {
if pf.Depth == 32 && pf.Direct == wanted { if pf.Depth == depth && pf.Direct == format.format {
bgraFormat = pf.Id bgraFormat = pf.Id
break break
} }
} }
if bgraFormat == 0 { if bgraFormat == 0 {
log.Fatalln("ARGB format not found") log.Fatalln("picture format not found")
} }
// We could also look for the inverse pictformat. // We could also look for the inverse pictformat.
@ -202,14 +251,12 @@ func main() {
row := make([]byte, bounds.Dx()*4) row := make([]byte, bounds.Dx()*4)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ { for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ { for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, a := img.At(x, y).RGBA() encoding.PutUint32(row[x*4:], format.transform(img.At(x, y)))
encoding.PutUint32(row[x*4:],
(a>>8)<<24|(r>>8)<<16|(g>>8)<<8|(b>>8))
} }
_ = xproto.PutImage(X, xproto.ImageFormatZPixmap, _ = xproto.PutImage(X, xproto.ImageFormatZPixmap,
xproto.Drawable(pixid), cid, uint16(bounds.Dx()), 1, xproto.Drawable(pixid), cid, uint16(bounds.Dx()), 1,
0, int16(y), 0, int16(y),
0, 32, row) 0, depth, row)
} }
} else { } else {
rep, err := shm.QueryVersion(X).Reply() rep, err := shm.QueryVersion(X).Reply()
@ -242,9 +289,7 @@ func main() {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ { for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
row := data[y*bounds.Dx()*4:] row := data[y*bounds.Dx()*4:]
for x := bounds.Min.X; x < bounds.Max.X; x++ { for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, a := img.At(x, y).RGBA() encoding.PutUint32(row[x*4:], format.transform(img.At(x, y)))
encoding.PutUint32(row[x*4:],
(a>>8)<<24|(r>>8)<<16|(g>>8)<<8|(b>>8))
} }
} }
@ -262,7 +307,7 @@ func main() {
_ = shm.PutImage(X, xproto.Drawable(pixid), cid, _ = shm.PutImage(X, xproto.Drawable(pixid), cid,
uint16(bounds.Dx()), uint16(bounds.Dy()), 0, 0, uint16(bounds.Dx()), uint16(bounds.Dy()), 0, 0,
uint16(bounds.Dx()), uint16(bounds.Dy()), 0, 0, uint16(bounds.Dx()), uint16(bounds.Dy()), 0, 0,
32, xproto.ImageFormatZPixmap, depth, xproto.ImageFormatZPixmap,
0 /* SendEvent */, segid, 0 /* Offset */) 0 /* SendEvent */, segid, 0 /* Offset */)
} }