-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2bs.go
60 lines (51 loc) · 1.21 KB
/
i2bs.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
package main
import (
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"os"
"path"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: i2bs <image1> [image2] ... [imageN]")
os.Exit(0)
}
imagePaths := os.Args[1:]
for _, imagePath := range imagePaths {
file, err := os.Open(imagePath)
if err != nil {
fmt.Fprintln(os.Stderr, "Can't", err.Error())
os.Exit(1)
}
img, _, err := image.Decode(file)
if err != nil {
fmt.Fprintln(os.Stderr, "Can't open", imagePath, err.Error())
os.Exit(1)
}
basePath := path.Base(imagePath)
outputPath := basePath[:len(basePath)-len(path.Ext(imagePath))] + ".skindata"
output, err := os.Create(outputPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Can't", err.Error())
os.Exit(1)
}
bounds := img.Bounds()
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, a := img.At(x, y).RGBA()
_, err := output.Write([]byte{uint8(r), uint8(g), uint8(b), uint8(a)})
if err != nil {
fmt.Fprintln(os.Stderr, "Can't", err.Error())
os.Exit(1)
}
}
}
if err := output.Close(); err != nil {
fmt.Fprintln(os.Stderr, "Can't", err.Error())
os.Exit(1)
}
}
}