-
-
Notifications
You must be signed in to change notification settings - Fork 382
/
gui.go
536 lines (472 loc) · 13.2 KB
/
gui.go
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package caire
import (
"fmt"
"image"
"image/color"
"image/draw"
"math"
"math/rand"
"time"
"gioui.org/app"
"gioui.org/f32"
"gioui.org/font/gofont"
"gioui.org/io/key"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/esimov/caire/imop"
"github.com/esimov/caire/utils"
)
const (
// The starting colors for the linear gradient, used when the image is resized both horizontally and vertically.
// In this case the preview mode is deactivated and a dynamic gradient overlay is shown.
redStart = 137
greenStart = 47
blueStart = 54
// The ending colors for the linear gradient. The starting colors and ending colors are lerped.
redEnd = 255
greenEnd = 112
blueEnd = 105
)
var (
maxScreenX float32 = 1024
maxScreenY float32 = 640
defaultBkgColor = color.Transparent
defaultFillColor = color.Black
)
type interval struct {
min, max float64
}
// Gui is the basic struct containing all of the information needed for the UI operation.
// It receives the resized image transferred through a channel which is called in a separate goroutine.
type Gui struct {
cfg struct {
x interval
y interval
chrot bool
angle float32
window struct {
w float32
h float32
title string
}
color struct {
randR uint8
randG uint8
randB uint8
background color.Color
fill color.Color
}
timeStamp time.Time
}
proc struct {
isDone bool
img image.Image
seams []Seam
wrk <-chan worker
err chan<- error
}
cp *Processor
cop *imop.Composite
bop *imop.Blend
th *material.Theme
ctx layout.Context
huds map[int]*hudCtrl
view struct {
huds layout.List
}
}
type hudCtrl struct {
visible widget.Bool
index int
title string
}
// NewGUI initializes the Gio interface.
func NewGUI(w, h int) *Gui {
defaultColor := color.NRGBA{R: 0x2d, G: 0x23, B: 0x2e, A: 0xff}
gui := &Gui{
ctx: layout.Context{
Ops: new(op.Ops),
Constraints: layout.Constraints{
Max: image.Pt(w, h),
},
},
cop: imop.InitOp(),
bop: imop.NewBlend(),
huds: make(map[int]*hudCtrl),
th: material.NewTheme(),
}
gui.th.Shaper = text.NewShaper(text.WithCollection(gofont.Collection()))
gui.th.TextSize = unit.Sp(12)
gui.th.Palette.ContrastBg = defaultColor
gui.th.FingerSize = 10
gui.initWindow(w, h)
return gui
}
// Add adds a new hud control for debugging.
func (g *Gui) Add(index int, title string, enabled bool) {
control := &hudCtrl{
index: index,
title: title,
visible: widget.Bool{},
}
control.visible.Value = enabled
g.huds[index] = control
}
// initWindow creates and initializes the GUI window.
func (g *Gui) initWindow(w, h int) {
rand.NewSource(time.Now().UnixNano())
g.cfg.angle = 45
g.cfg.color.randR = uint8(random(1, 2))
g.cfg.color.randG = uint8(random(1, 2))
g.cfg.color.randB = uint8(random(1, 2))
g.cfg.window.w, g.cfg.window.h = float32(w), float32(h)
g.cfg.x = interval{min: 0, max: float64(w)}
g.cfg.y = interval{min: 0, max: float64(h)}
g.cfg.color.background = defaultBkgColor
g.cfg.color.fill = defaultFillColor
if !resizeXY {
g.cfg.window.w, g.cfg.window.h = g.getWindowSize()
}
g.cfg.window.title = "Preview"
}
// getWindowSize returns the resized image dimension.
func (g *Gui) getWindowSize() (float32, float32) {
w, h := g.cfg.window.w, g.cfg.window.h
// Maintain the image aspect ratio in case the image width and height is greater than the predefined window.
r := getRatio(w, h)
if w > maxScreenX && h > maxScreenY {
w = w * r
h = h * r
}
return w, h
}
// Run is the core method of the Gio GUI application.
// This updates the window with the resized image received from a channel
// and terminates when the image resizing operation completes.
func (g *Gui) Run() error {
var (
rc uint8 = redStart
gc uint8 = greenStart
bc uint8 = blueStart
descRed, descGreen, descBlue bool
)
w := app.NewWindow(app.Title(g.cfg.window.title), app.Size(
unit.Dp(g.cfg.window.w),
unit.Dp(g.cfg.window.h),
))
w.Perform(system.ActionCenter)
g.cfg.timeStamp = time.Now()
if g.cp.Debug {
g.Add(0, "Show seams", true)
if len(g.cp.MaskPath) > 0 || len(g.cp.RMaskPath) > 0 || g.cp.FaceDetect {
g.Add(1, "Debug mask", false)
}
}
abortFn := func() {
var dx, dy int
if g.proc.img != nil {
bounds := g.proc.img.Bounds()
dx, dy = bounds.Max.X, bounds.Max.Y
}
if !g.proc.isDone {
if (g.cp.NewWidth > 0 && g.cp.NewWidth != dx) ||
(g.cp.NewHeight > 0 && g.cp.NewHeight != dy) {
errorMsg := fmt.Sprintf("%s %s %s",
utils.DecorateText("⚡ CAIRE", utils.StatusMessage),
utils.DecorateText("⇢ process aborted by the user...", utils.DefaultMessage),
utils.DecorateText("✘\n", utils.ErrorMessage),
)
g.cp.Spinner.StopMsg = errorMsg
g.cp.Spinner.Stop()
}
}
g.cp.Spinner.RestoreCursor()
}
for {
select {
case e := <-w.Events():
switch e := e.(type) {
case system.FrameEvent:
gtx := layout.NewContext(g.ctx.Ops, e)
key.InputOp{Tag: w, Keys: key.NameEscape}.Add(gtx.Ops)
for _, ev := range gtx.Queue.Events(w) {
if e, ok := ev.(key.Event); ok && e.Name == key.NameEscape {
w.Perform(system.ActionClose)
}
}
{ // red
if descRed {
rc--
} else {
rc++
}
if rc >= redEnd {
descRed = !descRed
}
if rc == redStart {
descRed = !descRed
}
}
{ // green
if descGreen {
gc--
} else {
gc++
}
if gc >= greenEnd {
descGreen = !descGreen
}
if gc == greenStart {
descGreen = !descGreen
}
}
{ // blue
if descBlue {
bc--
} else {
bc++
}
if bc >= blueEnd {
descBlue = !descBlue
}
if bc == blueStart {
descBlue = !descBlue
}
}
g.draw(gtx, color.NRGBA{R: rc, G: gc, B: bc})
e.Frame(gtx.Ops)
case system.DestroyEvent:
abortFn()
return e.Err
}
case res := <-g.proc.wrk:
if res.done {
g.proc.isDone = true
break
}
if resizeXY {
continue
}
g.proc.img = res.img
g.proc.seams = res.carver.Seams
if mask, ok := g.huds[1]; ok {
if mask.visible.Value {
srcBitmap := imop.NewBitmap(res.img.Bounds())
dstBitmap := imop.NewBitmap(res.img.Bounds())
uniform := image.NewNRGBA(res.img.Bounds())
col := color.RGBA{R: 0x2f, G: 0xf3, B: 0xe0, A: 0xff}
draw.Draw(uniform, uniform.Bounds(), &image.Uniform{col}, image.Point{}, draw.Src)
g.cop.Set(imop.DstIn)
g.cop.Draw(srcBitmap, res.debug, uniform, nil)
g.bop.Set(imop.ColorMode)
g.cop.Set(imop.DstOver)
g.cop.Draw(dstBitmap, res.img, srcBitmap.Img, g.bop)
g.proc.img = dstBitmap.Img
}
}
if g.cp.vRes {
g.proc.img = res.carver.RotateImage270(g.proc.img.(*image.NRGBA))
}
w.Invalidate()
}
}
}
type (
C = layout.Context
D = layout.Dimensions
)
// draw draws the resized image in the GUI window (obtained from a channel)
// and in case the debug mode is activated it prints out the seams.
func (g *Gui) draw(gtx layout.Context, bgCol color.NRGBA) {
g.ctx = gtx
op.InvalidateOp{}.Add(gtx.Ops)
c := g.setColor(g.cfg.color.background)
paint.Fill(g.ctx.Ops, c)
if g.proc.img != nil {
src := paint.NewImageOp(g.proc.img)
src.Add(g.ctx.Ops)
layout.Stack{}.Layout(g.ctx,
layout.Stacked(func(gtx C) D {
paint.FillShape(gtx.Ops, c,
clip.Rect{Max: g.ctx.Constraints.Max}.Op(),
)
return layout.UniformInset(unit.Dp(0)).Layout(gtx,
func(gtx C) D {
widget.Image{
Src: src,
Scale: 1 / float32(unit.Dp(1)),
Fit: widget.Contain,
}.Layout(gtx)
if seam, ok := g.huds[0]; ok {
if seam.visible.Value {
tr := f32.Affine2D{}
screen := layout.FPt(g.ctx.Constraints.Max)
width, height := float32(g.proc.img.Bounds().Dx()), float32(g.proc.img.Bounds().Dy())
sw, sh := float32(screen.X), float32(screen.Y)
if sw > width {
ratio := sw / width
tr = tr.Scale(f32.Pt(sw/2, sh/2), f32.Pt(1, ratio))
} else if sh > height {
ratio := sh / height
tr = tr.Scale(f32.Pt(sw/2, sh/2), f32.Pt(ratio, 1))
}
if g.cp.vRes {
angle := float32(270 * math.Pi / 180)
half := float32(math.Round(float64(sh*0.5-height*0.5) * 0.5))
ox := math.Abs(float64(sw - (sw - (sw/2 - sh/2))))
oy := math.Abs(float64(sh - (sh - (sw/2 - height/2 + half))))
tr = tr.Rotate(f32.Pt(sw/2, sh/2), -angle)
if screen.X > screen.Y {
tr = tr.Offset(f32.Pt(float32(ox), float32(oy)))
} else {
tr = tr.Offset(f32.Pt(float32(-ox), float32(-oy)))
}
}
op.Affine(tr).Add(gtx.Ops)
for _, s := range g.proc.seams {
dpx := unit.Dp(s.X)
dpy := unit.Dp(s.Y)
// Convert the image coordinates from pixel values to DP units.
dpiy := unit.Dp(float32(g.cfg.window.w) / float32(300))
dpix := unit.Dp(float32(g.cfg.window.h) / float32(300))
g.DrawSeam(g.cp.ShapeType, float32(dpx*dpix), float32(dpy*dpiy), 2.0)
}
}
}
return layout.Dimensions{Size: gtx.Constraints.Max}
})
}),
)
}
if g.cp.Debug {
layout.Stack{}.Layout(g.ctx,
layout.Stacked(func(gtx C) D {
hudHeight := 40
r := image.Rectangle{
Max: image.Point{
X: gtx.Constraints.Max.X,
Y: hudHeight,
},
}
defer op.Offset(image.Pt(0, gtx.Constraints.Max.Y-hudHeight)).Push(gtx.Ops).Pop()
return layout.Stack{}.Layout(gtx,
layout.Expanded(func(gtx C) D {
paint.FillShape(gtx.Ops, color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xcc}, clip.Rect(r).Op())
return layout.Dimensions{Size: r.Max}
}),
layout.Stacked(func(gtx C) D {
border := image.Rectangle{
Max: image.Point{
X: gtx.Constraints.Max.X,
Y: gtx.Dp(unit.Dp(0.5)),
},
}
paint.FillShape(gtx.Ops, color.NRGBA{R: 0x3B, G: 0x41, B: 0x3C, A: 0xaa}, clip.Rect(border).Op())
return layout.Dimensions{Size: r.Max}
}),
layout.Stacked(func(gtx C) D {
return g.view.huds.Layout(gtx, len(g.huds),
func(gtx layout.Context, index int) D {
if hud, ok := g.huds[index]; ok {
checkbox := material.CheckBox(g.th, &hud.visible, fmt.Sprintf("%v", hud.title))
checkbox.Size = 20
return checkbox.Layout(gtx)
}
return D{}
})
}),
)
}),
)
}
// Disable the preview mode and warn the user in case the image is resized both horizontally and vertically.
if resizeXY {
var msg string
if !g.proc.isDone {
msg = "Preview is not available while the image is resized both horizontally and vertically!"
} else {
msg = "Done, you may close this window!"
bgCol = color.NRGBA{R: 45, G: 45, B: 42, A: 0xff}
}
g.displayMessage(g.ctx, bgCol, msg)
}
}
// displayMessage show a static message when the image is resized both horizontally and vertically.
func (g *Gui) displayMessage(ctx layout.Context, bgCol color.NRGBA, msg string) {
g.th.Palette.Fg = color.NRGBA{R: 251, G: 254, B: 249, A: 0xff}
paint.ColorOp{Color: bgCol}.Add(ctx.Ops)
rect := image.Rectangle{
Max: ctx.Constraints.Max,
}
defer clip.Rect(rect).Push(ctx.Ops).Pop()
paint.PaintOp{}.Add(ctx.Ops)
layout.Stack{}.Layout(ctx,
layout.Stacked(func(gtx C) D {
return layout.UniformInset(unit.Dp(4)).Layout(ctx, func(gtx C) D {
if !g.proc.isDone {
gtx.Constraints.Min.Y = 0
tr := f32.Affine2D{}
dr := image.Rectangle{Max: gtx.Constraints.Min}
tr = tr.Rotate(f32.Pt(float32(ctx.Constraints.Max.X/2), float32(ctx.Constraints.Max.Y/2)), 0.005*-g.cfg.angle)
op.Affine(tr).Add(gtx.Ops)
since := time.Since(g.cfg.timeStamp)
if since.Seconds() > 5 {
g.cfg.timeStamp = time.Now()
g.cfg.color.randR = uint8(random(1, 2))
g.cfg.color.randG = uint8(random(1, 2))
g.cfg.color.randB = uint8(random(1, 2))
}
paint.LinearGradientOp{
Stop1: layout.FPt(dr.Min.Div(2)),
Stop2: layout.FPt(dr.Max.Mul(2)),
Color1: color.NRGBA{R: 41, G: bgCol.G * g.cfg.color.randG, B: bgCol.B * g.cfg.color.randB, A: 0xFF},
Color2: color.NRGBA{R: bgCol.R * g.cfg.color.randR, G: 29, B: 54, A: 0xFF},
}.Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
if g.cfg.chrot {
g.cfg.angle--
} else {
g.cfg.angle++
}
if g.cfg.angle == -90 || g.cfg.angle == 90 {
g.cfg.chrot = !g.cfg.chrot
}
}
return layout.Dimensions{
Size: gtx.Constraints.Max,
}
})
}),
layout.Stacked(func(gtx C) D {
return layout.UniformInset(unit.Dp(4)).Layout(ctx, func(gtx C) D {
return layout.Center.Layout(ctx, func(gtx C) D {
m := material.Label(g.th, unit.Sp(40), msg)
m.Alignment = text.Middle
return m.Layout(gtx)
})
})
}),
layout.Stacked(func(gtx C) D {
info := "(You will be notified once the process is finished.)"
if g.proc.isDone {
return layout.Dimensions{}
}
return layout.Inset{Top: 70}.Layout(ctx, func(gtx C) D {
return layout.Center.Layout(ctx, func(gtx C) D {
return material.Label(g.th, unit.Sp(13), info).Layout(gtx)
})
})
}),
)
}
// random generates a random number between two numbers.
func random(min, max float32) float32 {
return rand.Float32()*(max-min) + min
}