2018-08-25 05:51:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"log"
|
|
|
|
"os"
|
2018-08-25 23:17:15 +02:00
|
|
|
"reflect"
|
2018-08-25 05:51:33 +02:00
|
|
|
"time"
|
2018-08-25 23:17:15 +02:00
|
|
|
"unsafe"
|
2018-08-25 05:51:33 +02:00
|
|
|
|
2021-07-14 10:56:50 +02:00
|
|
|
"github.com/BurntSushi/xgb"
|
|
|
|
"github.com/BurntSushi/xgb/render"
|
|
|
|
"github.com/BurntSushi/xgb/shm"
|
|
|
|
"github.com/BurntSushi/xgb/xproto"
|
|
|
|
|
2018-08-25 05:51:33 +02:00
|
|
|
"image"
|
2021-07-15 01:43:13 +02:00
|
|
|
"image/color"
|
2018-08-25 05:51:33 +02:00
|
|
|
_ "image/gif"
|
|
|
|
_ "image/jpeg"
|
|
|
|
_ "image/png"
|
|
|
|
)
|
|
|
|
|
2018-08-25 23:17:15 +02:00
|
|
|
// #include <sys/ipc.h>
|
|
|
|
// #include <sys/shm.h>
|
|
|
|
import "C"
|
|
|
|
|
2018-08-25 05:51:33 +02:00
|
|
|
func F64ToFixed(f float64) render.Fixed { return render.Fixed(f * 65536) }
|
|
|
|
func FixedToF64(f render.Fixed) float64 { return float64(f) / 65536 }
|
|
|
|
|
2021-07-15 01:43:13 +02:00
|
|
|
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)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-25 05:51:33 +02:00
|
|
|
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
|
2021-07-15 01:43:13 +02:00
|
|
|
|
|
|
|
// 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.)
|
2018-08-25 05:51:33 +02:00
|
|
|
for _, i := range screen.AllowedDepths {
|
|
|
|
for _, v := range i.Visuals {
|
|
|
|
// TODO: Could/should check other parameters.
|
2021-07-15 01:43:13 +02:00
|
|
|
if v.Class != xproto.VisualClassTrueColor {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if prefer30 && i.Depth == 30 ||
|
|
|
|
!prefer30 && i.Depth == 32 {
|
2018-08-25 05:51:33 +02:00
|
|
|
visual, depth = v.VisualId, i.Depth
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 01:43:13 +02:00
|
|
|
format, ok := formats[depth]
|
|
|
|
if !ok {
|
|
|
|
log.Fatalln("unsupported bit depth")
|
|
|
|
}
|
|
|
|
|
2018-08-25 05:51:33 +02:00
|
|
|
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|
|
2021-07-15 01:43:13 +02:00
|
|
|
xproto.CwColormap, []uint32{format.transform(color.Alpha{0x80}), 0,
|
2018-08-25 05:51:33 +02:00
|
|
|
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 {
|
2021-07-15 01:43:13 +02:00
|
|
|
if pf.Depth == depth {
|
2018-08-25 05:51:33 +02:00
|
|
|
if pf.BitsPerPixel != 32 || pf.ScanlinePad != 32 {
|
|
|
|
log.Fatalln("unsuported X server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pixid, err := xproto.NewPixmapId(X)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2021-07-15 01:43:13 +02:00
|
|
|
_ = xproto.CreatePixmap(X, depth, pixid, xproto.Drawable(screen.Root),
|
2018-08-25 05:51:33 +02:00
|
|
|
uint16(img.Bounds().Dx()), uint16(img.Bounds().Dy()))
|
|
|
|
|
|
|
|
var bgraFormat render.Pictformat
|
|
|
|
for _, pf := range pformats.Formats {
|
2021-07-15 01:43:13 +02:00
|
|
|
if pf.Depth == depth && pf.Direct == format.format {
|
2018-08-25 05:51:33 +02:00
|
|
|
bgraFormat = pf.Id
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bgraFormat == 0 {
|
2021-07-15 01:43:13 +02:00
|
|
|
log.Fatalln("picture format not found")
|
2018-08-25 05:51:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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})
|
|
|
|
|
|
|
|
bounds := img.Bounds()
|
|
|
|
Lstart := time.Now()
|
|
|
|
|
2018-08-25 23:17:15 +02:00
|
|
|
if err := shm.Init(X); err != nil {
|
|
|
|
log.Println("MIT-SHM unavailable")
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
// Also see xgbutil/xgraphics/xsurface.go.
|
|
|
|
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++ {
|
2021-07-15 01:43:13 +02:00
|
|
|
encoding.PutUint32(row[x*4:], format.transform(img.At(x, y)))
|
2018-08-25 23:17:15 +02:00
|
|
|
}
|
|
|
|
_ = xproto.PutImage(X, xproto.ImageFormatZPixmap,
|
|
|
|
xproto.Drawable(pixid), cid, uint16(bounds.Dx()), 1,
|
|
|
|
0, int16(y),
|
2021-07-15 01:43:13 +02:00
|
|
|
0, depth, row)
|
2018-08-25 23:17:15 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rep, err := shm.QueryVersion(X).Reply()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
if rep.PixmapFormat != xproto.ImageFormatZPixmap ||
|
|
|
|
!rep.SharedPixmaps {
|
|
|
|
log.Fatalln("MIT-SHM configuration unfit")
|
|
|
|
}
|
|
|
|
|
|
|
|
shmSize := bounds.Dx() * bounds.Dy() * 4
|
|
|
|
|
|
|
|
// As a side note, to clean up unreferenced segments (orphans):
|
|
|
|
// ipcs -m | awk '$6 == "0" { print $2 }' | xargs ipcrm shm
|
|
|
|
shmID := int(C.shmget(C.IPC_PRIVATE,
|
|
|
|
C.size_t(shmSize), C.IPC_CREAT|0777))
|
|
|
|
if shmID == -1 {
|
|
|
|
// TODO: We should handle this case by falling back to PutImage,
|
|
|
|
// if only because the allocation may hit a system limit.
|
|
|
|
log.Fatalln("memory allocation failed")
|
2018-08-25 05:51:33 +02:00
|
|
|
}
|
2018-08-25 23:17:15 +02:00
|
|
|
|
|
|
|
dataRaw := C.shmat(C.int(shmID), nil, 0)
|
|
|
|
defer C.shmdt(dataRaw)
|
|
|
|
defer C.shmctl(C.int(shmID), C.IPC_RMID, nil)
|
|
|
|
|
|
|
|
data := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
|
|
|
Data: uintptr(dataRaw), Len: shmSize, Cap: shmSize}))
|
|
|
|
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++ {
|
2021-07-15 01:43:13 +02:00
|
|
|
encoding.PutUint32(row[x*4:], format.transform(img.At(x, y)))
|
2018-08-25 23:17:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
segid, err := shm.NewSegId(X)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to have it attached on the server before we unload the segment.
|
|
|
|
c := shm.AttachChecked(X, segid, uint32(shmID), true /* RO */)
|
|
|
|
if err := c.Check(); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = shm.PutImage(X, xproto.Drawable(pixid), cid,
|
|
|
|
uint16(bounds.Dx()), uint16(bounds.Dy()), 0, 0,
|
|
|
|
uint16(bounds.Dx()), uint16(bounds.Dy()), 0, 0,
|
2021-07-15 01:43:13 +02:00
|
|
|
depth, xproto.ImageFormatZPixmap,
|
2018-08-25 23:17:15 +02:00
|
|
|
0 /* SendEvent */, segid, 0 /* Offset */)
|
2018-08-25 05:51:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("uploading took", time.Now().Sub(Lstart))
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|