-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (111 loc) · 2.64 KB
/
main.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
package main
import (
"flag"
"image"
"image/color"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"os"
"runtime"
"strings"
"sync"
"golang.org/x/image/draw"
"golang.org/x/image/tiff"
)
const (
topBorder = 1.0 / 12.0
bottomBorder = 5.0 / 18.0
sideBorder = 1.0 / 13.0
fileSuffix = "pola.tif"
)
var (
edgeRatio float64
)
func main() {
flag.Float64Var(&edgeRatio, "r", 1, "edge blur in milli-widths")
flag.Parse()
wg := &sync.WaitGroup{}
jobs := make(chan string)
for i := 0; i < runtime.NumCPU(); i++ {
wg.Add(1)
go worker(jobs, wg)
}
for _, fName := range flag.Args() {
jobs <- fName
}
close(jobs)
wg.Wait()
}
func worker(jobs chan string, wg *sync.WaitGroup) {
defer wg.Done()
for job := range jobs {
doOneFrame(job)
}
}
func doOneFrame(fName string) {
if strings.HasSuffix(fName, fileSuffix) {
return
}
f, err := os.Open(fName)
if err != nil {
log.Println(err)
return
}
defer f.Close()
inImage, _, err := image.Decode(f)
if err != nil {
log.Println(err)
return
}
inBounds := inImage.Bounds()
scaledSideBorder := int(float64(inBounds.Dx()) * sideBorder)
scaledTopBorder := int(float64(inBounds.Dy()) * topBorder)
scaledBottomBorder := int(float64(inBounds.Dy()) * bottomBorder)
outBounds := image.Rect(
inBounds.Min.X-scaledSideBorder, inBounds.Min.Y-scaledTopBorder,
inBounds.Max.X+scaledSideBorder, inBounds.Max.Y+scaledBottomBorder)
outImage := image.NewRGBA64(outBounds)
// Draw background
white := image.NewUniform(color.White)
draw.Copy(outImage, outBounds.Min, white, outBounds, draw.Over, nil)
// Draw main image
draw.Copy(outImage, inBounds.Min, inImage, inBounds, draw.Over, nil)
// Create blurred image
edgePixels := float64(inBounds.Dx()) * (edgeRatio / 1000)
blur := gaussianBlur(outImage, edgePixels)
// Create destination mask
mask := image.NewRGBA64(outBounds)
draw.Copy(mask, outBounds.Min, image.Opaque, outBounds, draw.Over, nil)
draw.Copy(mask, inBounds.Min, image.Transparent, inBounds, draw.Src, nil)
// Overlay blurred image using mask
draw.Copy(outImage, outBounds.Min, blur, outBounds, draw.Over, &draw.Options{
DstMask: mask,
})
saveImage(outImage, reSuffix(fName, fileSuffix))
}
func saveImage(i image.Image, fName string) {
w, err := os.Create(fName)
if err != nil {
log.Println(err)
return
}
defer w.Close()
err = tiff.Encode(w, i, &tiff.Options{
Compression: tiff.Deflate,
Predictor: true,
})
if err != nil {
log.Println(err)
}
}
func reSuffix(fName, suffix string) string {
segs := strings.Split(fName, ".")
if len(segs) > 1 {
segs[len(segs)-1] = suffix
} else {
segs = append(segs, suffix)
}
return strings.Join(segs, ".")
}