-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (102 loc) · 2.23 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
package main
import (
"github.com/urfave/cli/v2"
"image-wav/decoder"
"image-wav/encoder"
"image/png"
"io/ioutil"
"os"
)
// Encodes a given binary file into an image with given dimensions
func encode(in string, out string, width int, height int) {
binary, err := ioutil.ReadFile(in)
if err != nil {
panic(err)
}
encoder := encoder.NewEncoder(binary, width, height)
encoder.Encode()
if err = encoder.Out(out); err != nil {
panic(err)
}
}
// Decodes a given image file into a binary file
func decode(in string, out string) {
file, err := os.Open(in)
if err != nil {
panic(err)
}
defer file.Close()
if _, err = file.Seek(0, 0); err != nil {
panic(err)
}
img, err := png.Decode(file)
if err != nil {
panic(err)
}
decoder := decoder.NewDecoder(img)
decoder.Decode()
err = decoder.Out(out)
if err != nil {
panic(err)
}
}
func actionMiddleware(c *cli.Context) error {
if c.Args().Len() < 2 {
return cli.Exit("Too little arguments were given", 1)
}
return nil
}
func main() {
app := &cli.App{
Name: "image-binary",
Usage: "encode and decode binary files into images",
Commands: []*cli.Command{
{
Name: "encode",
Usage: "Encodes a binary file into an image",
Aliases: []string{"e"},
ArgsUsage: "[in] [out]",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "width",
Aliases: []string{"x"},
Usage: "Rendered image width",
Required: false,
Value: 1000,
},
&cli.IntFlag{
Name: "height",
Aliases: []string{"y"},
Usage: "Rendered image height",
Required: false,
Value: 1000,
},
},
Action: func(c *cli.Context) error {
if err := actionMiddleware(c); err != nil {
return err
}
encode(c.Args().Get(0), c.Args().Get(1), c.Int("width"), c.Int("height"))
return nil
},
},
{
Name: "decode",
Usage: "Decodes an image into a binary file",
Aliases: []string{"e"},
ArgsUsage: "[in] [out]",
Action: func(c *cli.Context) error {
if err := actionMiddleware(c); err != nil {
return err
}
decode(c.Args().Get(0), c.Args().Get(1))
return nil
},
},
},
}
err := app.Run(os.Args)
if err != nil {
panic(err)
}
}