-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
98 lines (86 loc) · 2.35 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
package main
import (
"encoding/hex"
"flag"
"image"
"image/color"
"image/jpeg"
"image/png"
"log"
"os"
"strings"
"golang.org/x/image/bmp"
)
var (
PixelMin int
Threshold float64
Levels int
ShowGrid bool
oFile string
InputBorderColor string
InputGridColor string
BorderColor color.RGBA
GridColor color.RGBA
AverageColor color.RGBA
)
func init() {
flag.IntVar(&PixelMin, "m", 1, "minimum size a block can be")
flag.Float64Var(&Threshold, "t", 25, "color difference threshold")
flag.IntVar(&Levels, "l", 7, "max recursive levels")
flag.BoolVar(&ShowGrid, "g", false, "render grid lines")
flag.StringVar(&InputBorderColor, "bc", "333333", "border color (hex)")
flag.StringVar(&InputGridColor, "gc", "", "grid color (hex)")
flag.StringVar(&oFile, "o", "quad.png", "output file name with extension")
flag.Parse()
}
func main() {
// f, err := os.Create("trace.out")
// if err != nil {
// panic(err)
// }
// trace.Start(f)
// defer trace.Stop()
// open the image to quadify
reader, err := os.Open(os.Args[len(os.Args)-1])
if err != nil {
log.Fatal(err)
}
defer reader.Close()
img, _, err := image.Decode(reader)
if err != nil {
log.Fatal(err)
}
bounds := img.Bounds()
q := newQuad(&img, bounds.Min.X, bounds.Min.Y, bounds.Max.X, bounds.Max.Y, Threshold, int32(Levels), 1)
// use the input color if available or the default color for borders.
if len(InputBorderColor) == 6 {
colorHex, _ := hex.DecodeString(InputBorderColor)
BorderColor = color.RGBA{uint8(colorHex[0]), uint8(colorHex[1]), uint8(colorHex[2]), 0xff}
} else {
// use the average color of the image
BorderColor = q.color
}
// use the input color if available or the default color for borders.
if len(InputGridColor) == 6 {
colorHex, _ := hex.DecodeString(InputGridColor)
GridColor = color.RGBA{uint8(colorHex[0]), uint8(colorHex[1]), uint8(colorHex[2]), 0xff}
} else {
// use the average color of the image
GridColor = q.color
}
// fmt.Println("rendering artwork")
canvas := image.NewRGBA(image.Rect(0, 0, q.width, q.height))
q.draw(canvas)
// save art to the filesystem
o, _ := os.Create(oFile)
defer o.Close()
ext := strings.Split(oFile, ".")[1]
switch ext {
case "jpg":
jpeg.Encode(o, canvas, &jpeg.Options{100})
case "png":
png.Encode(o, canvas)
case "bmp":
bmp.Encode(o, canvas)
}
}