-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.go
More file actions
142 lines (132 loc) · 3.89 KB
/
Copy pathmain.go
File metadata and controls
142 lines (132 loc) · 3.89 KB
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
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()
if shouldRunCli(config) {
deflickeringError := runDeflickering()
if deflickeringError != nil {
clear()
fmt.Println("An error occured:")
fmt.Println(deflickeringError)
os.Exit(1)
}
os.Exit(0)
}
//Initialize Window from config and start GUI
guiError := startGUI()
if guiError != nil {
fmt.Println(guiError)
os.Exit(1)
}
os.Exit(0)
}
func shouldRunCli(config configuration) bool {
return config.sourceDirectory != "" || config.destinationDirectory != ""
}
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
}