-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (69 loc) · 1.95 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
package main
import (
"encoding/json"
"flag"
"fmt"
"image"
"log"
"net/http"
"os"
"path/filepath"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/csams/pixelation/pkg/pixelate"
"github.com/csams/pixelation/pkg/serve"
)
func main() {
shouldServe := flag.Bool("serve", false, "start the server")
hostPort := flag.String("host-port", ":8080", "server parameters")
colors := flag.Int("colors", 16, "number of colors")
blockWidth := flag.Int("block-width", 8, "width of blocks in pixels")
render := flag.Bool("render", false, "render a JSON image to some format")
format := flag.String("format", "jpeg", "Specify the format to render")
input := flag.String("input", "", "the file to read")
output := flag.String("output", "", "the file to write")
flag.Parse()
if *shouldServe {
root := chi.NewRouter()
root.Use(middleware.RequestID)
root.Use(middleware.Logger)
root.Use(middleware.Compress(5))
// Create a route along /files that will serve contents from
// the ./data/ folder.
workDir, _ := os.Getwd()
filesDir := http.Dir(filepath.Join(workDir, "web"))
serve.FileServer(root, "/web", filesDir)
api := serve.ApiRouter(*colors, *blockWidth)
root.Mount("/api", api)
log.Printf("Listening on %s\n", *hostPort)
if err := http.ListenAndServe(*hostPort, root); err != nil {
log.Fatalln(err)
}
}
if *input == "" {
log.Fatalln("You must specify an input file name.")
}
outputFile := *output
if *output == "" {
outputFile = fmt.Sprintf("output.%s", *format)
}
var img image.Image
if *render {
f, err := os.Open(*input)
if err != nil {
log.Fatalln(err)
}
var p pixelate.BlockImage
if err := json.NewDecoder(f).Decode(&p); err != nil {
log.Fatalln(err)
}
img = p.ToImage()
} else {
i, _, err := pixelate.DecodeImage(*input)
if err != nil {
log.Fatalln(err)
}
img = pixelate.Pixelate(i, *colors, *blockWidth).ToImage()
}
pixelate.EncodeImage(outputFile, img, *format)
}