Compare commits

..

No commits in common. "a927713a81321f84b1a1cd1675a142bf931ae510" and "ef24d7980c50f706286d99ab6d80a98af89970e3" have entirely different histories.

1 changed files with 30 additions and 76 deletions

View File

@ -2,19 +2,17 @@ package main
import (
"encoding/binary"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/render"
"github.com/BurntSushi/xgb/shm"
"github.com/BurntSushi/xgb/xproto"
"log"
"os"
"reflect"
"time"
"unsafe"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/render"
"github.com/BurntSushi/xgb/shm"
"github.com/BurntSushi/xgb/xproto"
"image"
"image/color"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
@ -27,47 +25,6 @@ import "C"
func F64ToFixed(f float64) render.Fixed { return render.Fixed(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() {
/*
pf, err := os.Create("pprof.out")
@ -105,35 +62,18 @@ func main() {
screen := setup.DefaultScreen(X)
visual, depth := screen.RootVisual, screen.RootDepth
// 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.)
// 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.
for _, i := range screen.AllowedDepths {
for _, v := range i.Visuals {
// TODO: Could/should check other parameters.
if v.Class != xproto.VisualClassTrueColor {
continue
}
if prefer30 && i.Depth == 30 ||
!prefer30 && i.Depth == 32 {
if i.Depth == 32 && v.Class == xproto.VisualClassTrueColor {
visual, depth = v.VisualId, i.Depth
break
}
}
}
format, ok := formats[depth]
if !ok {
log.Fatalln("unsupported bit depth")
}
mid, err := xproto.NewColormapId(X)
if err != nil {
log.Fatalln(err)
@ -151,7 +91,7 @@ func main() {
_ = xproto.CreateWindow(X, depth, wid, screen.Root,
0, 0, 500, 500, 0, xproto.WindowClassInputOutput,
visual, xproto.CwBackPixel|xproto.CwBorderPixel|xproto.CwEventMask|
xproto.CwColormap, []uint32{format.transform(color.Alpha{0x80}), 0,
xproto.CwColormap, []uint32{0x80808080, 0,
xproto.EventMaskStructureNotify | xproto.EventMaskExposure,
uint32(mid)})
@ -191,7 +131,7 @@ func main() {
// setup.BitmapFormatScanline{Pad,Unit} and setup.BitmapFormatBitOrder
// don't interest us here since we're only using Z format pixmaps.
for _, pf := range setup.PixmapFormats {
if pf.Depth == depth {
if pf.Depth == 32 {
if pf.BitsPerPixel != 32 || pf.ScanlinePad != 32 {
log.Fatalln("unsuported X server")
}
@ -202,18 +142,28 @@ func main() {
if err != nil {
log.Fatalln(err)
}
_ = xproto.CreatePixmap(X, depth, pixid, xproto.Drawable(screen.Root),
_ = xproto.CreatePixmap(X, 32, pixid, xproto.Drawable(screen.Root),
uint16(img.Bounds().Dx()), uint16(img.Bounds().Dy()))
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 {
if pf.Depth == depth && pf.Direct == format.format {
if pf.Depth == 32 && pf.Direct == wanted {
bgraFormat = pf.Id
break
}
}
if bgraFormat == 0 {
log.Fatalln("picture format not found")
log.Fatalln("ARGB format not found")
}
// We could also look for the inverse pictformat.
@ -251,12 +201,14 @@ func main() {
row := make([]byte, bounds.Dx()*4)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
encoding.PutUint32(row[x*4:], format.transform(img.At(x, y)))
r, g, b, a := img.At(x, y).RGBA()
encoding.PutUint32(row[x*4:],
(a>>8)<<24|(r>>8)<<16|(g>>8)<<8|(b>>8))
}
_ = xproto.PutImage(X, xproto.ImageFormatZPixmap,
xproto.Drawable(pixid), cid, uint16(bounds.Dx()), 1,
0, int16(y),
0, depth, row)
0, 32, row)
}
} else {
rep, err := shm.QueryVersion(X).Reply()
@ -289,7 +241,9 @@ func main() {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
row := data[y*bounds.Dx()*4:]
for x := bounds.Min.X; x < bounds.Max.X; x++ {
encoding.PutUint32(row[x*4:], format.transform(img.At(x, y)))
r, g, b, a := img.At(x, y).RGBA()
encoding.PutUint32(row[x*4:],
(a>>8)<<24|(r>>8)<<16|(g>>8)<<8|(b>>8))
}
}
@ -307,7 +261,7 @@ func main() {
_ = shm.PutImage(X, xproto.Drawable(pixid), cid,
uint16(bounds.Dx()), uint16(bounds.Dy()), 0, 0,
uint16(bounds.Dx()), uint16(bounds.Dy()), 0, 0,
depth, xproto.ImageFormatZPixmap,
32, xproto.ImageFormatZPixmap,
0 /* SendEvent */, segid, 0 /* Offset */)
}