-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
resizer.go
89 lines (82 loc) · 1.83 KB
/
resizer.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
// Seam Carving for Content-Aware Image Resizing
//
// very slow resizer
//
// package requires cgo
package caire
import (
"image"
"image/draw"
"github.com/esimov/caire"
"github.com/srlehn/termimg/internal/consts"
"github.com/srlehn/termimg/internal/errors"
"github.com/srlehn/termimg/term"
)
type resizer struct {
proc caire.Processor
}
var _ term.Resizer = (*resizer)(nil)
// shapeType can be "circle" or "line"
func NewResizer(blurRadius, sobelThreshold int, faceDetect bool, shapeType string) term.Resizer {
if blurRadius == 0 {
// BlurRadius: 4 - github.com/esimov/caire/cmd/caire/main.go
// BlurRadius: 1
blurRadius = 4
}
if sobelThreshold == 0 {
// SobelThreshold: 2 - github.com/esimov/caire/cmd/caire/main.go
// SobelThreshold: 4
sobelThreshold = 2
}
if len(shapeType) == 0 {
// ShapeType: "circle" - github.com/esimov/caire/cmd/caire/main.go
shapeType = `circle`
}
return &resizer{
proc: caire.Processor{
BlurRadius: blurRadius,
SobelThreshold: sobelThreshold,
FaceDetect: faceDetect,
ShapeType: shapeType,
},
}
}
func (r *resizer) Resize(img image.Image, size image.Point) (image.Image, error) {
if r == nil {
return nil, errors.NilReceiver()
}
r.proc.NewWidth = size.X
r.proc.NewHeight = size.Y
var nimg *image.NRGBA
im := img
var lvls int
maxLvl := 5
repeat:
switch it := im.(type) {
case *image.NRGBA:
nimg = it
goto end
case *term.Image:
if it.Original == nil {
if lvls > maxLvl {
break
}
lvls++
if err := it.Decode(); err != nil {
return nil, err
}
if it.Original == nil {
return nil, errors.New(consts.ErrNilImage)
}
im = it
goto repeat
}
}
if nimg == nil {
b := img.Bounds()
nimg = image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(nimg, nimg.Bounds(), img, b.Min, draw.Src)
}
end:
return r.proc.Resize(nimg)
}