/
cmd.go
52 lines (47 loc) · 934 Bytes
/
cmd.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
package main
import (
"flag"
"image"
"image/color"
"image/png"
"io/ioutil"
"os"
)
func read(name string) []byte {
bin, _ := ioutil.ReadFile(name)
return bin
}
func main() {
in := flag.String("i", "none", "input file")
out := flag.String("o", "out.png", "output file")
flag.Parse()
if *in == "none" {
return
}
bin := read(*in)
binLength := len(bin)
width := 384
height := len(bin) / width
rect := image.Rect(0, 0, width, height)
img := image.NewRGBA(rect)
p := 0
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
var c color.RGBA
for addr := p; addr < p+3 && addr < binLength; addr++ {
if addr%3 == 0 {
c.R = uint8(bin[addr])
} else if addr%3 == 1 {
c.G = uint8(bin[addr])
} else if addr%3 == 2 {
c.B = uint8(bin[addr])
}
c.A = 0xff
}
p += 3
img.Set(x, y, c)
}
}
f, _ := os.OpenFile(*out, os.O_CREATE|os.O_WRONLY, 0666)
png.Encode(f, img)
}