-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
125 lines (116 loc) · 3.51 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
package main
import (
"errors"
"fmt"
"os"
"runtime"
"github.com/disintegration/imaging"
)
type picture struct {
currentPath string
targetPath string
currentRgbHistogram rgbHistogram
targetRgbHistogram rgbHistogram
}
func main() {
//Initial console output
printInfo()
//Read parameters from console
config = collectConfigInformation()
//Initialize Window from config and start GUI
initalizeWindow()
window.Main()
os.Exit(0)
}
func runDeflickering() error {
//Prepare
configError := validateConfigInformation()
if configError != nil {
return configError
}
clear()
runtime.GOMAXPROCS(config.threads)
pictures, picturesError := readDirectory(config.sourceDirectory, config.destinationDirectory)
if picturesError != nil {
return picturesError
}
progress := createProgressBars(len(pictures))
progress.container.Start()
//Analyze and create Histograms
var analyzeError error
pictures, analyzeError = forEveryPicture(pictures, progress.bars["analyze"], config.threads, func(pic picture) (picture, error) {
img, err := imaging.Open(pic.currentPath)
if err != nil {
return pic, errors.New(pic.currentPath + " | " + err.Error())
}
pic.currentRgbHistogram = generateRgbHistogramFromImage(img)
return pic, nil
})
if analyzeError != nil {
progress.container.Stop()
return analyzeError
}
//Calculate global or rolling average
if config.rollingAverage < 1 {
var averageRgbHistogram rgbHistogram
for i := range pictures {
for j := 0; j < 256; j++ {
averageRgbHistogram.r[j] += pictures[i].currentRgbHistogram.r[j]
averageRgbHistogram.g[j] += pictures[i].currentRgbHistogram.g[j]
averageRgbHistogram.b[j] += pictures[i].currentRgbHistogram.b[j]
}
}
for i := 0; i < 256; i++ {
averageRgbHistogram.r[i] /= uint32(len(pictures))
averageRgbHistogram.g[i] /= uint32(len(pictures))
averageRgbHistogram.b[i] /= uint32(len(pictures))
}
for i := range pictures {
pictures[i].targetRgbHistogram = averageRgbHistogram
}
} else {
for i := range pictures {
var averageRgbHistogram rgbHistogram
var start = i - config.rollingAverage
if start < 0 {
start = 0
}
var end = i + config.rollingAverage
if end > len(pictures)-1 {
end = len(pictures) - 1
}
for i := start; i <= end; i++ {
for j := 0; j < 256; j++ {
averageRgbHistogram.r[j] += pictures[i].currentRgbHistogram.r[j]
averageRgbHistogram.g[j] += pictures[i].currentRgbHistogram.g[j]
averageRgbHistogram.b[j] += pictures[i].currentRgbHistogram.b[j]
}
}
for i := 0; i < 256; i++ {
averageRgbHistogram.r[i] /= uint32(end - start + 1)
averageRgbHistogram.g[i] /= uint32(end - start + 1)
averageRgbHistogram.b[i] /= uint32(end - start + 1)
}
pictures[i].targetRgbHistogram = averageRgbHistogram
}
}
var adjustError error
pictures, adjustError = forEveryPicture(pictures, progress.bars["adjust"], config.threads, func(pic picture) (picture, error) {
var img, _ = imaging.Open(pic.currentPath)
lut := generateRgbLutFromRgbHistograms(pic.currentRgbHistogram, pic.targetRgbHistogram)
img = applyRgbLutToImage(img, lut)
err := imaging.Save(img, pic.targetPath, imaging.JPEGQuality(config.jpegCompression), imaging.PNGCompressionLevel(0))
if err != nil {
return pic, errors.New(pic.currentPath + " | " + err.Error())
}
return pic, nil
})
if adjustError != nil {
progress.container.Stop()
return adjustError
}
progress.container.Stop()
clear()
fmt.Printf("Saved %v pictures into %v", len(pictures),config.destinationDirectory)
return nil
}