Skip to content

Commit 8a54030

Browse files
committed
First commit
1 parent 407fd9f commit 8a54030

File tree

4 files changed

+269
-0
lines changed

4 files changed

+269
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212

1313
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
1414
.glide/
15+
16+
*.swp
17+
test*.jpg

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# How to use
2+
3+
wget https://blog.golang.org/gopher/prototype.jpg
4+
5+
go get github.com/nfnt/resize
6+
go get github.com/nsf/termbox-go
7+
go run main.go prototype.jpg
8+

main.go

+258
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
package main
2+
3+
import (
4+
"image/jpeg"
5+
"log"
6+
"os"
7+
8+
"github.com/nfnt/resize"
9+
termbox "github.com/nsf/termbox-go"
10+
)
11+
12+
func main() {
13+
imagePath := "test.jpg"
14+
if len(os.Args) == 2 {
15+
imagePath = os.Args[1]
16+
}
17+
file, err := os.Open(imagePath)
18+
if err != nil {
19+
log.Fatal(err)
20+
}
21+
22+
img, err := jpeg.Decode(file)
23+
if err != nil {
24+
log.Fatal(err)
25+
}
26+
defer file.Close()
27+
28+
err = termbox.Init()
29+
if err != nil {
30+
panic(err)
31+
}
32+
defer termbox.Close()
33+
w, h := termbox.Size()
34+
imgText := resize.Resize(uint(w), uint(h), img, resize.Lanczos3)
35+
termbox.SetOutputMode(termbox.Output256)
36+
const mojiretsu = "0123456789 ░#" //{' ', '░', '▒', '▓', '█'}
37+
nihongoRune := []rune(mojiretsu)
38+
termbox.SetCell(0, 0, nihongoRune[1], termbox.ColorRed, termbox.ColorBlue)
39+
for i := 0; i < h; i++ {
40+
for j := 0; j < w; j++ {
41+
r, g, b, _ := imgText.At(j, i).RGBA()
42+
//h, l, s := rgb2hls(int32(r), int32(g), int32(b))
43+
r = uint32(r * 3 / 65535)
44+
g = uint32(g * 3 / 65535)
45+
b = uint32(b * 3 / 65535)
46+
47+
chi := 10
48+
cidx := 0
49+
cbidx := 0
50+
if r == g && g == b && r > 0 {
51+
// white
52+
cidx = 8
53+
if r > 2 {
54+
cidx = cidx + 512
55+
}
56+
} else {
57+
if r > 0 {
58+
// r > 0
59+
if g == 0 && b == 0 {
60+
//red
61+
cidx = 2
62+
if r > 1 {
63+
// light red
64+
cidx = cidx + 512
65+
}
66+
}
67+
if g > 0 {
68+
// r>0,g>0
69+
if b == 0 {
70+
// r>0,g>0,b=0
71+
if r >= g && g > 1 {
72+
// yellow
73+
cidx = 4
74+
if g > 1 {
75+
cidx = cidx + 512
76+
}
77+
} else {
78+
// g=1
79+
if r > 2 {
80+
// red
81+
cidx = 2
82+
if r > 2 {
83+
cidx = cidx + 512
84+
}
85+
}
86+
}
87+
} else {
88+
// r>0,b>0
89+
if b > g && r >= b {
90+
// magenta
91+
cidx = 6
92+
} else {
93+
if r >= g && g > 2 {
94+
// yellow
95+
cidx = 4
96+
} else {
97+
cidx = 8
98+
}
99+
}
100+
}
101+
102+
} else {
103+
// g=0
104+
if b > 0 {
105+
if r >= b && b > 1 {
106+
// magenta
107+
cidx = 6
108+
}
109+
}
110+
}
111+
} else {
112+
// r = 0
113+
if g > 0 {
114+
if b == 0 {
115+
//green
116+
cidx = 3
117+
if g > 1 {
118+
cidx = cidx + 512
119+
}
120+
} else {
121+
// r=0,g>0,b>0
122+
if g < b {
123+
//blue
124+
cidx = 5
125+
if b > 2 {
126+
cidx = cidx + 512
127+
}
128+
129+
} else {
130+
// cyan
131+
cidx = 7
132+
if b > 1 {
133+
cidx = cidx + 512
134+
}
135+
}
136+
}
137+
} else {
138+
// r =0,g = 0
139+
if b > 0 {
140+
// blue
141+
cidx = 5
142+
if b > 1 {
143+
cidx = cidx + 512
144+
}
145+
} else {
146+
// r=0,g=0,b=0
147+
// black
148+
cidx = 1
149+
}
150+
}
151+
}
152+
}
153+
if r >= 2 && g >= 2 && b < 2 {
154+
// yellow
155+
cidx = 4
156+
}
157+
if r == 3 && g == 3 && b == 2 {
158+
// white
159+
cidx = 8 + 512
160+
}
161+
if r == 2 && g == 2 && b == 1 {
162+
// white
163+
cidx = 8
164+
}
165+
if r < 2 && g >= 2 && b >= 2 {
166+
// cyan
167+
cidx = 7
168+
}
169+
if r >= 2 && g < 2 && b >= 2 {
170+
// magenta
171+
cidx = 6
172+
}
173+
if r == 3 && g < 2 && b < 2 {
174+
// red
175+
cidx = 2
176+
}
177+
if r == 3 && g == 1 && b == 1 {
178+
// white
179+
cbidx = 2
180+
cidx = 8
181+
chi = 11
182+
}
183+
if r < 2 && g == 3 && b < 2 {
184+
// green
185+
cidx = 3
186+
}
187+
if r < 2 && g < 2 && b == 3 {
188+
// blue
189+
cidx = 5
190+
}
191+
if r == 1 && g == 1 && b == 0 {
192+
chi = 11
193+
// yellow
194+
cidx = 1
195+
cbidx = 4
196+
}
197+
if r == 1 && g == 0 && b == 0 {
198+
chi = 11
199+
// magenta
200+
cidx = 1
201+
cbidx = 6
202+
}
203+
if r == 0 && g == 1 && b == 1 {
204+
chi = 11
205+
// cyan
206+
cidx = 1
207+
cbidx = 7
208+
}
209+
210+
if r == 1 && g == 1 && b == 1 {
211+
chi = 11
212+
// white
213+
cidx = 1
214+
cbidx = 8
215+
}
216+
if r == 1 && g == 0 && b == 0 {
217+
chi = 11
218+
// red
219+
cidx = 1
220+
cbidx = 2
221+
}
222+
if r == 0 && g == 1 && b == 0 {
223+
chi = 11
224+
// green
225+
cidx = 1
226+
cbidx = 3
227+
}
228+
if r == 0 && g == 0 && b == 1 {
229+
chi = 11
230+
// blue
231+
cidx = 1
232+
cbidx = 5
233+
}
234+
termbox.SetCell(j, i, nihongoRune[chi], termbox.Attribute(cbidx), termbox.Attribute(cidx))
235+
}
236+
}
237+
termbox.Flush()
238+
239+
for {
240+
switch ev := termbox.PollEvent(); ev.Type {
241+
case termbox.EventKey:
242+
switch ev.Key {
243+
case termbox.KeyEsc:
244+
//Escキー押した時の処理
245+
print(w)
246+
print(",")
247+
println(h)
248+
os.Exit(0)
249+
case termbox.KeyArrowUp:
250+
//上キー押した時の処理
251+
case termbox.KeyArrowDown:
252+
//下キー押した時の処理
253+
default:
254+
}
255+
default:
256+
}
257+
}
258+
}

snap.png

193 KB
Loading

0 commit comments

Comments
 (0)