253 lines
6.1 KiB
Go
253 lines
6.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/binary"
|
||
|
"github.com/BurntSushi/xgb"
|
||
|
"github.com/BurntSushi/xgb/render"
|
||
|
"github.com/BurntSushi/xgb/xproto"
|
||
|
"log"
|
||
|
"os"
|
||
|
"time"
|
||
|
|
||
|
"image"
|
||
|
_ "image/gif"
|
||
|
_ "image/jpeg"
|
||
|
_ "image/png"
|
||
|
)
|
||
|
|
||
|
func F64ToFixed(f float64) render.Fixed { return render.Fixed(f * 65536) }
|
||
|
func FixedToF64(f render.Fixed) float64 { return float64(f) / 65536 }
|
||
|
|
||
|
func main() {
|
||
|
/*
|
||
|
pf, err := os.Create("pprof.out")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
pprof.StartCPUProfile(pf)
|
||
|
defer pprof.StopCPUProfile()
|
||
|
*/
|
||
|
|
||
|
// Load a picture from the command line.
|
||
|
f, err := os.Open(os.Args[1])
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
img, name, err := image.Decode(f)
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
log.Println("image type is", name)
|
||
|
|
||
|
// Miscellaneous X11 initialization.
|
||
|
X, err := xgb.NewConn()
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
|
||
|
if err := render.Init(X); err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
|
||
|
setup := xproto.Setup(X)
|
||
|
screen := setup.DefaultScreen(X)
|
||
|
|
||
|
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.
|
||
|
for _, i := range screen.AllowedDepths {
|
||
|
for _, v := range i.Visuals {
|
||
|
// TODO: Could/should check other parameters.
|
||
|
if i.Depth == 32 && v.Class == xproto.VisualClassTrueColor {
|
||
|
visual, depth = v.VisualId, i.Depth
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mid, err := xproto.NewColormapId(X)
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
|
||
|
_ = xproto.CreateColormap(
|
||
|
X, xproto.ColormapAllocNone, mid, screen.Root, visual)
|
||
|
|
||
|
wid, err := xproto.NewWindowId(X)
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
|
||
|
// Border pixel and colormap are required when depth differs from parent.
|
||
|
_ = xproto.CreateWindow(X, depth, wid, screen.Root,
|
||
|
0, 0, 500, 500, 0, xproto.WindowClassInputOutput,
|
||
|
visual, xproto.CwBackPixel|xproto.CwBorderPixel|xproto.CwEventMask|
|
||
|
xproto.CwColormap, []uint32{0x80808080, 0,
|
||
|
xproto.EventMaskStructureNotify | xproto.EventMaskExposure,
|
||
|
uint32(mid)})
|
||
|
|
||
|
title := []byte("Image")
|
||
|
_ = xproto.ChangeProperty(X, xproto.PropModeReplace, wid, xproto.AtomWmName,
|
||
|
xproto.AtomString, 8, uint32(len(title)), title)
|
||
|
|
||
|
_ = xproto.MapWindow(X, wid)
|
||
|
|
||
|
pformats, err := render.QueryPictFormats(X).Reply()
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
|
||
|
// Similar to XRenderFindVisualFormat.
|
||
|
// The DefaultScreen is almost certain to be zero.
|
||
|
var pformat render.Pictformat
|
||
|
for _, pd := range pformats.Screens[X.DefaultScreen].Depths {
|
||
|
// This check seems to be slightly extraneous.
|
||
|
if pd.Depth != depth {
|
||
|
continue
|
||
|
}
|
||
|
for _, pv := range pd.Visuals {
|
||
|
if pv.Visual == visual {
|
||
|
pformat = pv.Format
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Wrap the window's surface in a picture.
|
||
|
pid, err := render.NewPictureId(X)
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
render.CreatePicture(X, pid, xproto.Drawable(wid), pformat, 0, []uint32{})
|
||
|
|
||
|
// 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 == 32 {
|
||
|
if pf.BitsPerPixel != 32 || pf.ScanlinePad != 32 {
|
||
|
log.Fatalln("unsuported X server")
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pixid, err := xproto.NewPixmapId(X)
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
_ = 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 == 32 && pf.Direct == wanted {
|
||
|
bgraFormat = pf.Id
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
if bgraFormat == 0 {
|
||
|
log.Fatalln("ARGB format not found")
|
||
|
}
|
||
|
|
||
|
// We could also look for the inverse pictformat.
|
||
|
var encoding binary.ByteOrder
|
||
|
if setup.ImageByteOrder == xproto.ImageOrderMSBFirst {
|
||
|
encoding = binary.BigEndian
|
||
|
} else {
|
||
|
encoding = binary.LittleEndian
|
||
|
}
|
||
|
|
||
|
pixpicid, err := render.NewPictureId(X)
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
render.CreatePicture(X, pixpicid, xproto.Drawable(pixid), bgraFormat,
|
||
|
0, []uint32{})
|
||
|
|
||
|
// Do we really need this? :/
|
||
|
cid, err := xproto.NewGcontextId(X)
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
_ = xproto.CreateGC(X, cid, xproto.Drawable(pixid),
|
||
|
xproto.GcGraphicsExposures, []uint32{0})
|
||
|
|
||
|
// Load data into the pixmap. Also see xgbutil/xgraphics/xsurface.go.
|
||
|
// We're being lazy and resolve the 1<<16 limit of requests by sending
|
||
|
// a row at a time. The encoding is also done inefficiently.
|
||
|
bounds := img.Bounds()
|
||
|
|
||
|
Lstart := time.Now()
|
||
|
|
||
|
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++ {
|
||
|
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, 32, row)
|
||
|
}
|
||
|
|
||
|
log.Println("uploading took", time.Now().Sub(Lstart))
|
||
|
|
||
|
// TODO: SHM, for an example see https://github.com/BurntSushi/xgb/issues/28
|
||
|
|
||
|
var scale float64 = 1
|
||
|
for {
|
||
|
ev, xerr := X.WaitForEvent()
|
||
|
if xerr != nil {
|
||
|
log.Printf("Error: %s\n", xerr)
|
||
|
return
|
||
|
}
|
||
|
if ev == nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
log.Printf("Event: %s\n", ev)
|
||
|
switch e := ev.(type) {
|
||
|
case xproto.UnmapNotifyEvent:
|
||
|
return
|
||
|
|
||
|
case xproto.ConfigureNotifyEvent:
|
||
|
w, h := e.Width, e.Height
|
||
|
|
||
|
scaleX := float64(bounds.Dx()) / float64(w)
|
||
|
scaleY := float64(bounds.Dy()) / float64(h)
|
||
|
|
||
|
if scaleX < scaleY {
|
||
|
scale = scaleY
|
||
|
} else {
|
||
|
scale = scaleX
|
||
|
}
|
||
|
|
||
|
_ = render.SetPictureTransform(X, pixpicid, render.Transform{
|
||
|
F64ToFixed(scale), F64ToFixed(0), F64ToFixed(0),
|
||
|
F64ToFixed(0), F64ToFixed(scale), F64ToFixed(0),
|
||
|
F64ToFixed(0), F64ToFixed(0), F64ToFixed(1),
|
||
|
})
|
||
|
_ = render.SetPictureFilter(X, pixpicid, 8, "bilinear", nil)
|
||
|
|
||
|
case xproto.ExposeEvent:
|
||
|
_ = render.Composite(X, render.PictOpSrc,
|
||
|
pixpicid, render.PictureNone, pid,
|
||
|
0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */
|
||
|
uint16(float64(img.Bounds().Dx())/scale),
|
||
|
uint16(float64(img.Bounds().Dy())/scale))
|
||
|
}
|
||
|
}
|
||
|
}
|