-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·285 lines (245 loc) · 6.56 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* A simple command line tool for heightmap generation.
*
*/
package main
import (
"errors"
"flag"
"github.com/anthonynsimon/bild/adjust"
"github.com/anthonynsimon/bild/blend"
"github.com/anthonynsimon/bild/blur"
"github.com/anthonynsimon/bild/convolution"
"github.com/anthonynsimon/bild/effect"
"github.com/anthonynsimon/bild/histogram"
"github.com/anthonynsimon/bild/segment"
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
_ "golang.org/x/image/vp8"
_ "golang.org/x/image/webp"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io/fs"
"log"
"os"
"strings"
)
// image definition
type imgfx struct {
src image.Image
dst *image.Gray
format string
width int
height int
}
// cmdline options definition
type options struct {
input *string
in *string
output *string
out *string
blur *float64
emboss *string
gauss *float64
contrast *float64
invert *string
mono *uint
blend *float64
hist *string
}
var err error
func main() {
log.SetFlags(0)
log.SetPrefix("[info] ")
opt := new(options)
opt.input = flag.String("input", "", "Input image -- supported image formats include: [BMP, GIF, JPG, PNG, TIFF, WEBP, VP8]")
opt.in = flag.String("in", "", "Input image -- supported image formats include: [BMP, GIF, JPG, PNG, TIFF, WEBP, VP8]")
opt.output = flag.String("output", "output.png", "Output image -- supported image formats include: [BMP, GIF, JPG, PNG, TIFF]")
opt.out = flag.String("out", "output.png", "Output image -- supported image formats include: [BMP, GIF, JPG, PNG, TIFF]")
opt.blend = flag.Float64("blend", 0.5, "Blend opacity -- percent must be of range 0.0 to 1.0")
opt.blur = flag.Float64("blur", 0, "Box blur -- radius must be larger than 0.")
opt.emboss = flag.String("emboss", "low", "Emboss level -- must be [high/mid/low]")
opt.gauss = flag.Float64("gauss", 0, "Gaussian blur -- radius must be larger than 0.")
opt.contrast = flag.Float64("contrast", 0, "Contrast level -- must be of the range -100 to 100.")
opt.invert = flag.String("invert", "off", "Invert on/off -- reverses the colors of the grayscale image.")
opt.mono = flag.Uint("mono", 0, "Monochrome level -- must be of the range 0 to 255.")
opt.hist = flag.String("hist", "hist.png", "Histogram output -- produce histogram output to analyze the frequency distribution of colors in the image")
flag.Parse()
args := flag.Args()
if len(args) > 0 && args[0] == "help" {
doc()
flag.Usage()
os.Exit(1)
}
var input, output string
if len(*opt.input) > 0 {
input = *opt.input
} else if len(*opt.in) > 0 {
input = *opt.in
}
if *opt.output != "output.png" {
output = *opt.output
} else if *opt.out != "output.png" {
output = *opt.out
} else {
output = *opt.output
}
img := new(imgfx)
img.load(input)
img.proc(opt)
img.save(output)
genHist(*opt.hist, img.dst)
log.Println("Done!")
}
// Loads and decodes the input image.
func (img *imgfx) load(filename string) {
input := fopen(filename)
defer input.Close()
log.Printf("Loading input image: %s \n", filename)
img.src, img.format, err = image.Decode(input)
check(err)
size := img.src.Bounds()
img.width = size.Max.X
img.height = size.Max.Y
log.Printf("Image format: %s \n", img.format)
log.Printf("Image size: %dx%d \n", img.width, img.height)
}
// Applies image filters and processes image.
func (img *imgfx) proc(opt *options) {
log.Println("Processing image...")
if *opt.emboss == "low" {
img.emboss("low")
} else if *opt.emboss == "mid" {
img.emboss("mid")
} else if *opt.emboss == "high" {
img.emboss("high")
}
if *opt.contrast > 0 {
img.src = adjust.Contrast(img.src, *opt.contrast)
}
if *opt.invert == "on" {
img.src = effect.Invert(img.src)
}
if *opt.blur > 0 {
img.src = blur.Box(img.src, *opt.blur)
}
if *opt.gauss > 0 {
img.src = blur.Gaussian(img.src, *opt.gauss)
}
img.dst = effect.Grayscale(img.src)
if *opt.mono > 0 {
mono := segment.Threshold(img.src, uint8(*opt.mono))
chrome := blend.Opacity(img.dst, mono, *opt.blend)
img.dst = effect.Grayscale(chrome)
}
}
// Applies custom convolution kernels to image for depth depiction.
func (img *imgfx) emboss(level string) {
var kernel convolution.Kernel
if level == "low" {
kernel = convolution.Kernel{
Matrix: []float64{
-1, -1, 0,
-1, 0, 1,
0, 1, 1,
},
Width: 3,
Height: 3,
}
} else if level == "mid" {
kernel = convolution.Kernel{
Matrix: []float64{
-1, -1, -1, -1, 0,
-1, -1, -1, 0, 1,
-1, -1, 0, 1, 1,
-1, 0, 1, 1, 1,
0, 1, 1, 1, 1,
},
Width: 5,
Height: 5,
}
} else if level == "high" {
kernel = convolution.Kernel{
Matrix: []float64{
-1, -1, -1, -1, -1, -1, 0,
-1, -1, -1, -1, -1, 0, 1,
-1, -1, -1, -1, 0, 1, 1,
-1, -1, -1, 0, 1, 1, 1,
-1, -1, 0, 1, 1, 1, 1,
-1, 0, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1,
},
Width: 7,
Height: 7,
}
}
img.src = convolution.Convolve(img.src, &kernel, &convolution.Options{Bias: 128, Wrap: false, KeepAlpha: true})
}
// Encodes and saves the output image.
func (img *imgfx) save(filename string) {
output := fcreate(filename)
defer output.Close()
log.Printf("Saving output image as: %s \n", filename)
if strings.Contains(filename, ".") {
img.format = strings.Split(filename, ".")[1]
} else {
img.format = "png"
}
// encode the image using the original format
switch img.format {
case "bmp":
bmp.Encode(output, img.dst)
case "jpg":
case "jpeg":
opt := new(jpeg.Options)
opt.Quality = 100
jpeg.Encode(output, img.dst, opt)
case "png":
png.Encode(output, img.dst)
case "gif":
gif.Encode(output, img.dst, nil)
case "tiff":
tiff.Encode(output, img.dst, nil)
default:
msg := "Can't encode image file!"
log.Fatalln(msg)
}
}
// Generates histogram output from the provided image data.
func genHist(filename string, img *image.Gray) {
log.Println("Generating histogram for the output image...")
hist := histogram.NewRGBAHistogram(img)
result := hist.Image()
output := fcreate(filename)
defer output.Close()
png.Encode(output, result)
}
// Helper function for opening a file.
func fopen(filename string) *os.File {
file, err := os.Open(filename)
check(err)
return file
}
// Helper function for creating a file.
func fcreate(filename string) *os.File {
file, err := os.Create(filename)
check(err)
return file
}
// Helper function for checking errors.
func check(err error) {
if err != nil {
var pathError *fs.PathError
if errors.As(err, &pathError) {
log.Println("Path error:", pathError.Path)
} else {
log.Println(err)
}
}
}
// Helper function as part of the embedded documentation.
func doc() {
log.SetPrefix("")
}