-
Notifications
You must be signed in to change notification settings - Fork 4
/
image2raw.go
158 lines (136 loc) · 2.98 KB
/
image2raw.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"flag"
"fmt"
"image"
"image/color"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"os"
)
var (
format = flag.String("f", "rgba", "output format")
reverse = flag.Bool("r", false, "reverse color for bit format")
status = 0
)
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
}
for _, name := range flag.Args() {
ek(convert(name))
}
os.Exit(status)
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: [-f format] [-r] <img> ...")
fmt.Fprintln(os.Stderr, "supported formats: bit gray alpha rgb rgba bgr bgra cymk ycbcr")
flag.PrintDefaults()
os.Exit(2)
}
func ek(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "image2raw:", err)
status = 1
}
}
func convert(name string) error {
f, err := os.Open(name)
if err != nil {
return err
}
defer f.Close()
m, _, err := image.Decode(f)
if err != nil {
return err
}
switch *format {
case "bit":
outputBit(os.Stdout, m)
case "gray":
output(m, gray)
case "alpha":
output(m, alpha)
case "rgb":
output(m, rgb)
case "rgba":
output(m, rgba)
case "bgr":
output(m, bgr)
case "bgra":
output(m, bgra)
case "cymk":
output(m, cymk)
case "ycbcr":
output(m, ycbcr)
default:
return fmt.Errorf("unknown image format %q", *format)
}
return nil
}
func outputBit(w io.Writer, m image.Image) {
v := uint8(0)
n := uint(0)
r := m.Bounds()
for y := r.Min.Y; y < r.Max.Y; y++ {
for x := r.Min.X; x < r.Max.X; x++ {
cr, cg, cb, _ := m.At(x, y).RGBA()
on := cr != 0 && cg != 0 && cb != 0
if (!*reverse && on) || (*reverse && !on) {
v |= 1 << n
}
if n++; n >= 8 {
w.Write([]byte{v})
v = 0
n = 0
}
}
}
if n > 0 {
w.Write([]byte{v})
}
}
func output(m image.Image, f func(io.Writer, color.Color)) {
r := m.Bounds()
for y := r.Min.Y; y < r.Max.Y; y++ {
for x := r.Min.X; x < r.Max.X; x++ {
f(os.Stdout, m.At(x, y))
}
}
}
func gray(w io.Writer, c color.Color) {
p := color.GrayModel.Convert(c).(color.Gray)
w.Write([]byte{p.Y})
}
func alpha(w io.Writer, c color.Color) {
p := color.AlphaModel.Convert(c).(color.Alpha)
w.Write([]byte{p.A})
}
func rgb(w io.Writer, c color.Color) {
p := color.RGBAModel.Convert(c).(color.RGBA)
w.Write([]byte{p.R, p.G, p.B})
}
func rgba(w io.Writer, c color.Color) {
p := color.RGBAModel.Convert(c).(color.RGBA)
w.Write([]byte{p.R, p.G, p.B, p.A})
}
func bgr(w io.Writer, c color.Color) {
p := color.RGBAModel.Convert(c).(color.RGBA)
w.Write([]byte{p.B, p.G, p.R})
}
func bgra(w io.Writer, c color.Color) {
p := color.RGBAModel.Convert(c).(color.RGBA)
w.Write([]byte{p.B, p.G, p.R, p.A})
}
func cymk(w io.Writer, c color.Color) {
p := color.RGBAModel.Convert(c).(color.CMYK)
w.Write([]byte{p.C, p.M, p.Y, p.K})
}
func ycbcr(w io.Writer, c color.Color) {
p := color.RGBAModel.Convert(c).(color.YCbCr)
w.Write([]byte{p.Y, p.Cb, p.Cr})
}