This repository has been archived by the owner on Jul 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
234 lines (196 loc) · 5.29 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
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
package main
import (
"encoding/hex"
"errors"
"fmt"
"image"
"image/color"
"image/draw"
"math"
"os"
"strings"
"github.com/spf13/pflag"
"github.com/lucasb-eyer/go-colorful"
"github.com/brouxco/dithering"
)
type imageProcessor interface{
Decode(*os.File)
Swap(inverter)
Colorize(bool, draw.Drawer)
Write(*os.File)
}
type processImage struct{
imgs []image.Image
processed []image.Image
format string
}
type inverter func(color.Color) color.Color
var queue []imageProcessor
var palette color.Palette
func main() {
inFlag := pflag.StringP("input", "i", "", "Path to input image")
outFlag := pflag.StringP("output", "o", "", "Path to output")
paletteFlag := pflag.StringP("palette", "p", "", "Palette of the image")
paletteFileFlag := pflag.StringP("paletteFile", "P", "", "Path to a file which contains newline delimited colors")
ditherFlag := pflag.BoolP("dither", "d", true, "Whether to use dithering on the image or not")
ditherAlgoFlag := pflag.StringP("ditherAlgorithm", "D", "floydsteinberg", "The dithering algorithm to use.")
swapFlag := pflag.BoolP("swap", "s", false, "Swap luminance of image before colorizing")
swapOnlyFlag := pflag.BoolP("swapOnly", "S", false, "Only swap luminance and dont colorize. This implies -s (luminance swap)")
grayscaleSwapFlag := pflag.BoolP("grayscaleSwap", "g", false, "Only invert parts of the image that are calculated to be grayscale (blacks/whites)")
pywalFlag := pflag.BoolP("pywal", "w", false, "Use pywal colors as the palette")
xresourcesFlag := pflag.BoolP("xresources", "x", false, "Use colors from Xresources as the palette")
pflag.Parse()
check(inFlag, "input")
check(outFlag, "output")
// check(paletteFlag, "palette")
if *swapOnlyFlag {
f := true
swapFlag = &f
}
var dither draw.Drawer
if *ditherFlag {
var err error
dither, err = getDitherAlgo(*ditherAlgoFlag)
if err != nil {
perr("Invalid dither algorithm", *ditherAlgoFlag)
}
}
if *paletteFileFlag != "" {
var err error
palette, err = colorsFromFile(*paletteFileFlag)
if err != nil {
perr(err.Error())
}
}
if *xresourcesFlag {
var err error
palette, err = xresourcesColors()
if err != nil {
perr(err.Error())
}
}
if *pywalFlag {
var err error
palette, err = pywalColors()
if err != nil {
perr(err.Error())
}
}
if len(*paletteFlag) != 0 && len(palette) == 0 {
for _, colorStr := range strings.Split(*paletteFlag, " ") {
col, err := strToColor(colorStr)
if err != nil {
fmt.Fprintln(os.Stderr, "Invalid color", colorStr)
continue
}
palette = append(palette, col)
}
}
if len(palette) < 2 && !*swapOnlyFlag {
perr("Provided palette has less than 2 colors")
}
inFile, err := os.Open(*inFlag)
if err != nil {
perr("Could not open input file")
}
outFile, err := os.Create(*outFlag)
if err != nil {
perr("Could not create output file")
}
var colorInverter inverter = normalInverter
if *grayscaleSwapFlag {
colorInverter = grayscaleInverter
}
var processor imageProcessor
splits := strings.Split(*inFlag, ".")
ext := splits[len(splits) - 1]
switch ext {
case "gif": processor = &gifProcessor{}
case "png", "jpg", "jpeg": processor = &singleImageProcessor{}
}
queue := append(queue, processor)
for _, im := range queue {
im.Decode(inFile)
if *swapFlag {
im.Swap(colorInverter)
}
if !*swapOnlyFlag {
im.Colorize(*ditherFlag, dither)
}
im.Write(outFile)
}
}
func check(flagVal *string, name string) {
if *flagVal == "" {
fmt.Fprintln(os.Stderr, "Missing flag", name)
pflag.Usage()
os.Exit(1)
}
}
func pwarn(str ...interface{}) {
fmt.Fprintln(os.Stderr, str...)
}
func perr(str ...interface{}) {
fmt.Fprintln(os.Stderr, str...)
os.Exit(1)
}
func normalInverter(cl color.Color) color.Color {
clr, _ := colorful.MakeColor(cl)
h, s, l := clr.Hsl()
swap := colorful.Hsl(h, s, 1 - l)
return swap
}
func grayscaleInverter(cl color.Color) color.Color {
clr, _ := colorful.MakeColor(cl)
h, s, l := clr.Hsl()
r := math.Round(clr.R * 100) / 100
g := math.Round(clr.B * 100) / 100
b := math.Round(clr.G * 100) / 100
diff := math.Abs((r - b) / g)
deriv := 0.1
if /* s > 0.5 && */ diff >= deriv {
return cl
}
swap := colorful.Hsl(h, s, 1 - l)
return swap
}
func strToColor(str string) (color.Color, error) {
if str[0] == '#' {
// hexadecimal
normalStr, err := normalizeHex(str)
if err != nil {
return nil, err
}
b, err := hex.DecodeString(normalStr)
if err != nil {
return nil, err
}
// r g b a
return color.RGBA{b[0], b[1], b[2], 0xff}, nil
}
return nil, errors.New("Invalid format for color")
}
func normalizeHex(hx string) (string, error) {
switch len(hx) {
case 4: // #fff
longHex := ""
for i, c := range hx {
if i == 0 { continue }
longHex = longHex + string(c) + string(c)
}
return longHex, nil
case 7: // #ffffff
return hx[1:], nil
default:
return "", errors.New("invalid string for hex")
}
}
func getDitherAlgo(algo string) (alg draw.Drawer, err error) {
switch algo {
case "floydsteinberg": alg = draw.FloydSteinberg // stdlib floyd is more optimized, from reading the code
case "atkinson": alg = dithering.NewDither(dithering.Atkinson)
case "jjn", "jarvisjudiceninke": alg = dithering.NewDither(dithering.JarvisJudiceNinke)
default: err = errors.New("invalid dither algorithm")
}
return
}