1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
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))
}
}
}
|