-
Notifications
You must be signed in to change notification settings - Fork 0
/
van.go
179 lines (147 loc) · 5.61 KB
/
van.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
package main
import (
"errors"
"flag"
"fmt"
"image"
"image/color"
_ "image/jpeg"
"image/png"
"os"
"strings"
"encoding/hex"
"io/ioutil"
palettefy "rubygb.com/van-go/cmd/palettefy"
colors "rubygb.com/van-go/pkg/colors"
)
func isHelp(s string) bool {
return s == "help" || s == "-h" || s == "--h" || s == "-help" || s == "--help"
}
func isVersion(s string) bool {
return s == "version" || s == "-v" || s == "--v" || s == "-version" || s == "--version"
}
// Helper function. It's common to print a message then quit processing.
func quitMsg(s string) {
fmt.Println(s)
os.Exit(1)
}
const VERSION = "1.0.0"
const VAN_HELP = `Standard usage of van is 'van <command> [args]'. Implemented commands:
palettefy Restricts a source image to the colors in a given palette. See 'van palettefy -help'.`
const PALETTEFY_HELP = `Palettefy restricts a source image to the colors in a given palette. Usage:
van palettefy -s path/to/src/image.* -p {"<palette>" | @path/to/palette.*} [-o <name>] [-d <code>]
Explanation:
The options -s, -p are mandatory.
-s gives the path to the source image.
-p defines the palette to be used by the program. If followed by an argument in quotes "...", the argument is interpreted as a sequence of hex codes, prefixed by # and separated by commas and spaces. If a file path is provided, the file is parsed as above where newlines are also valid separators in addition to commas and spaces.
Examples:
-p "#000000 #ffffff" will use a monotone palette.
-p "#0f380f, #306230, #8bac0f, #9bbc0f" will use the original Gameboy palette.
-p @somedir/mypalette.txt (where mypalette.txt contains a hex value #xxxxxx on each line) will use the colors defined in mypalette.txt.
-o specifies the output file name. Default is to affix '_p' to the name of the input file.
-d specifies dithering behaviour. Default is -d 0 which implements no dithering and uses nearest neighbors in CIELAB space. Other options:
-d 1 employs Floyd-Steinberg dithering using nearest neighbors in CIELAB space. This can yield extremely good reconstructions of even photographic images using as few as 8 colors.`
func main() {
// Command line parsing
if len(os.Args) == 1 || isHelp(os.Args[1]) {
quitMsg(VAN_HELP)
} else if isVersion(os.Args[1]) {
quitMsg(VERSION)
} else {
// We have invoked a command (e.g. palettefy).
switch os.Args[1] {
case "help":
if len(os.Args) == 2 {
quitMsg(VAN_HELP)
} else {
switch os.Args[2] {
case "palettefy":
quitMsg(PALETTEFY_HELP)
default:
quitMsg(VAN_HELP)
}
}
case "palettefy":
if len(os.Args) == 2 || isHelp(os.Args[2]) {
quitMsg(PALETTEFY_HELP)
} else {
// Parse palettefy options (see PALETTEFY_HELP).
var srcFlag, paletteFlag, outputFlag string
var ditherFlag int
palettefyFlagSet := flag.NewFlagSet("", flag.ExitOnError)
palettefyFlagSet.StringVar(&srcFlag, "s", "", "Mandatory. Sets the path to the source image to palettefy.")
palettefyFlagSet.StringVar(&paletteFlag, "p", "", "Mandatory. Defines the palette to restrict the source image to. See 'van palettefy help' for formatting details.")
palettefyFlagSet.StringVar(&outputFlag, "o", "", "Sets the name of the final output file. Default is the source file affixed with '_p'.")
palettefyFlagSet.IntVar(&ditherFlag, "d", 0, "Set the dithering mode (int). Default is 0 for no dithering.")
palettefyFlagSet.Parse(os.Args[2:])
if srcFlag == "" { quitMsg("Error: Mandatory -s flag not set.") }
if paletteFlag == "" { quitMsg("Error: Mandatory -p flag not set.") }
pfy(srcFlag, paletteFlag, outputFlag, ditherFlag)
}
default:
quitMsg(VAN_HELP)
}
}
}
// The arguments to this function are the unparsed flags submitted by the user. The only guarantee at function entry is that src and palette are nonempty.
func pfy(src string, palette string, oname string, dither int) {
// Verify that the source file exists
if _, err := os.Stat(src); errors.Is(err, os.ErrNotExist) {
quitMsg("Error: -s flag file does not exist.")
}
reader, rerr := os.Open(src)
if rerr != nil { quitMsg(rerr.Error()) }
defer reader.Close()
img, _, ierr := image.Decode(reader)
if ierr != nil { quitMsg(ierr.Error()) }
// Parse palette argument
var colorPalette color.Palette
var parsePalette = func (p string) color.Palette {
p = strings.ReplaceAll(p, ",", "?")
p = strings.ReplaceAll(p, " ", "?")
p = strings.ReplaceAll(p, "\n", "?") // line break system dependent
p = strings.ReplaceAll(p, "\r", "?")
p = strings.ReplaceAll(p, "\r\n", "?")
psplit := strings.Split(p, "?")
var hexcodes []string
for _, h := range psplit {
if h != "" {
if len(h) != 7 {
fmt.Println(h)
quitMsg("Error: -p flag one or more hex values is improperly formatted.")
}
hexcodes = append(hexcodes, h[1:])
}
}
var cpal color.Palette
for _, h := range hexcodes {
bytes, berr := hex.DecodeString(h)
if berr != nil { panic(berr) } // shouldn't happen
cpal = append(cpal, colors.RGB{R:bytes[0], G:bytes[1], B:bytes[2]})
}
return cpal
}
if palette[0] == '@' {
// Load file into string and parse
file, ferr := ioutil.ReadFile(palette[1:])
if ferr != nil { quitMsg(ferr.Error()) }
colorPalette = parsePalette(string(file))
} else {
colorPalette = parsePalette(palette)
}
dstImage := palettefy.Palettefy(img, colorPalette, dither)
if oname == "" {
oname = strings.Clone(src)
oname, _, _ = strings.Cut(oname, ".")
oname = oname + "_p.png"
}
writer, werr := os.Create(oname)
if werr != nil { panic(werr) }
if err := png.Encode(writer, dstImage); err != nil {
writer.Close()
panic(err)
}
if err := writer.Close(); err != nil {
panic(err)
}
}