-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (69 loc) · 1.86 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
package main
import (
"bufio"
"errors"
"image"
"io"
"log"
"os"
"image/jpeg"
"github.com/jessevdk/go-flags"
"github.com/topherbullock/randomartist/art"
)
type RandomArtistCmd struct {
InputFile string `long:"input" short:"i" description:"Source file containing randart"`
OutputFile string `long:"output" short:"o" default:"randart.jpg" description:"output file destination"`
Scale int `long:"scale" short:"s" default:"25" description:"scaling factor for the image file"`
JpegQuality int `long:"quality" short:"q" default:"85" description:"jpeg quality for the output"`
Palette string `long:"palette" short:"p" default:"SharryNight" choice:"SharryNight" choice:"RandyWarhol" choice:"ShasiliyRandinsky" description:"color palette to use"`
}
func (r *RandomArtistCmd) readfile() (*[][]byte, error) {
var (
source io.Reader
err error
)
if r.InputFile != "" {
source, err = os.Open(r.InputFile)
if err != nil {
return nil, err
}
} else {
source = os.Stdin
}
return art.ReadRandart(bufio.NewReader(source))
}
func (r *RandomArtistCmd) paintImage(contents [][]byte) (image.Image, error) {
palette, ok := art.Palettes[r.Palette]
if !ok {
return nil, errors.New("Palette not found")
}
return art.PaintImage(contents, palette, r.Scale), nil
}
func (r *RandomArtistCmd) saveFile(img image.Image) error {
out, err := os.Create(r.OutputFile)
if err != nil {
log.Fatal(err)
}
return jpeg.Encode(out, img, &jpeg.Options{Quality: r.JpegQuality})
}
func main() {
cmd := &RandomArtistCmd{}
parser := flags.NewParser(cmd, flags.Default)
parser.NamespaceDelimiter = "-"
_, err := parser.Parse()
if err != nil {
os.Exit(1)
}
contents, err := cmd.readfile()
if err != nil {
log.Fatal(err)
}
img, err := cmd.paintImage(*contents)
if err != nil {
log.Fatal(err)
}
err = cmd.saveFile(img)
if err != nil {
log.Fatal(err)
}
}