/
main.go
316 lines (263 loc) · 6.9 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package main
// ------------------------------------------------------------------------- //
// Imports //
// ------------------------------------------------------------------------- //
import (
"fmt"
"image"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"image/color"
"image/draw"
_ "image/gif" // initialize decoder
_ "image/jpeg" // initialize decoder
_ "image/png" // initialize decoder
"github.com/disintegration/imaging"
colorful "github.com/lucasb-eyer/go-colorful"
"golang.org/x/crypto/ssh/terminal"
_ "golang.org/x/image/bmp" // initialize decoder
_ "golang.org/x/image/tiff" // initialize decoder
_ "golang.org/x/image/webp" // initialize decoder
)
// ------------------------------------------------------------------------- //
// Constants //
// ------------------------------------------------------------------------- //
const (
BlockSizeX int = 4
BlockSizeY int = 8
)
// ------------------------------------------------------------------------- //
// Main Entrypoint //
// ------------------------------------------------------------------------- //
func main() {
//gopath := os.Getenv("GOPATH")
path := filepath.FromSlash(os.Args[1])
fmt.Println(path)
// black background
bg := color.Gray16{0}
imgOriginal, err := loadImageFromFile(path)
if err != nil {
panic(err)
}
imgScaled := scaleImage(imgOriginal)
ansiImage := createAnsiImage(imgScaled, bg)
clearTerminal()
drawAnsiImage(ansiImage)
}
// ------------------------------------------------------------------------- //
// Types //
// ------------------------------------------------------------------------- //
type ansiPixel struct {
Brightness uint8
R, G, B uint8
source *ansiImage
}
type ansiImage struct {
w, h int
bgR uint8
bgG uint8
bgB uint8
pixels [][]*ansiPixel
}
// ------------------------------------------------------------------------- //
// Package Methods //
// ------------------------------------------------------------------------- //
func isTerminal() bool {
fd := os.Stdout.Fd()
return terminal.IsTerminal(int(fd))
}
func getTerminalSize() (width, height int, err error) {
// VT100 terminal size
width = 80
height = 24
if isTerminal() {
fd := os.Stdout.Fd()
width, height, err = terminal.GetSize(int(fd))
}
if width > 200 {
width = 120
}
if height < 70 || height > 120 {
height = 100
}
return
}
// ClearTerminal clears current terminal buffer using ANSI escape code.
// (Nice info for ANSI escape codes - http://unix.stackexchange.com/questions/124762/how-does-clear-command-work)
func clearTerminal() {
fmt.Print("\033[H\033[2J")
}
func loadImageFromFile(name string) (image.Image, error) {
reader, err := os.Open(name)
if err != nil {
return nil, err
}
defer reader.Close()
return loadImageFromReader(reader)
}
func loadImageFromReader(reader io.Reader) (image.Image, error) {
// (JPEG, PNG, GIF, BMP, TIFF)
img, _, err := image.Decode(reader)
if err != nil {
return nil, err
}
return img, nil
}
func scaleImage(img image.Image) image.Image {
sfy, sfx := BlockSizeY, BlockSizeX // 8x4 --> with dithering
tx, ty, err := getTerminalSize()
if err != nil {
panic(err)
}
scaledImage := imaging.Resize(img, sfy*ty, sfx*tx, imaging.Lanczos)
return scaledImage
}
func createAnsiImage(img image.Image, bg color.Color) *ansiImage {
bounds := img.Bounds()
yMax, xMax := bounds.Max.Y, bounds.Max.X
yMax = yMax / BlockSizeY // always sets 1 ANSIPixel block...
xMax = xMax / BlockSizeX // per 8x4 real pixels --> with dithering
rgbaOut := composeImage(img, bg)
ansiimage := newAnsiImage(xMax, yMax, bg)
newAnsiPixels(ansiimage, rgbaOut)
return ansiimage
}
func composeImage(img image.Image, bg color.Color) *image.RGBA {
// http://stackoverflow.com/questions/36595687/transparent-pixel-color-go-lang-image
var rgbaOut *image.RGBA
bounds := img.Bounds()
if _, _, _, a := bg.RGBA(); a >= 0xffff {
rgbaOut = image.NewRGBA(bounds)
draw.Draw(rgbaOut, bounds, image.NewUniform(bg), image.ZP, draw.Src)
draw.Draw(rgbaOut, bounds, img, image.ZP, draw.Over)
} else {
if v, ok := img.(*image.RGBA); ok {
rgbaOut = v
} else {
rgbaOut = image.NewRGBA(bounds)
draw.Draw(rgbaOut, bounds, img, image.ZP, draw.Src)
}
}
return rgbaOut
}
func newAnsiImage(w, h int, bg color.Color) *ansiImage {
// create instance
r, g, b, _ := bg.RGBA()
ansimage := &ansiImage{
w: w,
h: h,
bgR: uint8(r),
bgG: uint8(g),
bgB: uint8(b),
pixels: nil,
}
// initialize pixels to (0,0,0,0)
ansimage.pixels = func() [][]*ansiPixel {
v := make([][]*ansiPixel, h)
for y := 0; y < h; y++ {
v[y] = make([]*ansiPixel, w)
for x := 0; x < w; x++ {
v[y][x] = &ansiPixel{
R: 0,
G: 0,
B: 0,
Brightness: 0,
source: ansimage,
}
}
}
return v
}()
return ansimage
}
func newAnsiPixels(img *ansiImage, rgba *image.RGBA) {
// calculate brightness
pixelCount := BlockSizeY * BlockSizeX
for y := 0; y < img.h; y++ {
for x := 0; x < img.w; x++ {
var sumR, sumG, sumB, sumBri float64
for dy := 0; dy < BlockSizeY; dy++ {
py := BlockSizeY*y + dy
for dx := 0; dx < BlockSizeX; dx++ {
px := BlockSizeX*x + dx
pixel := rgba.At(px, py)
color, _ := colorful.MakeColor(pixel)
_, _, v := color.Hsv()
sumR += color.R
sumG += color.G
sumB += color.B
sumBri += v
}
}
r := uint8(sumR/float64(pixelCount)*255.0 + 0.5)
g := uint8(sumG/float64(pixelCount)*255.0 + 0.5)
b := uint8(sumB/float64(pixelCount)*255.0 + 0.5)
brightness := uint8(sumBri/float64(pixelCount)*255.0 + 0.5)
img.pixels[y][x].R = r
img.pixels[y][x].G = g
img.pixels[y][x].B = b
img.pixels[y][x].Brightness = brightness
}
}
}
func drawAnsiImage(img *ansiImage) {
type renderData struct {
row int
render string
}
maxprocs := runtime.NumCPU()
rows := make([]string, img.h)
for y := 0; y < img.h; y += maxprocs {
ch := make(chan renderData, maxprocs)
for n, r := 0, y; (n <= maxprocs) && (r+1 < img.h); n, r = n+1, y+n+1 {
go func(y int) {
var str string
for x := 0; x < img.w; x++ {
pixel := img.pixels[y][x]
str += drawAnsiPixel(pixel)
}
str += "\033[0m\n" // reset ansi style
ch <- renderData{row: y, render: str}
}(r)
}
for n, r := 0, y; (n <= maxprocs) && (r+1 < img.h); n, r = n+1, y+n+1 {
data := <-ch
rows[data.row] = data.render
}
}
fmt.Println(strings.Join(rows, ""))
}
func drawAnsiPixel(px *ansiPixel) string {
block := " "
switch bri := px.Brightness; {
case bri > 230:
block = "#"
case bri > 207:
block = "&"
case bri > 184:
block = "$"
case bri > 161:
block = "X"
case bri > 138:
block = "x"
case bri > 115:
block = "="
case bri > 92:
block = "+"
case bri > 69:
block = ";"
case bri > 46:
block = ":"
case bri > 23:
block = "."
}
return fmt.Sprintf(
"\033[48;2;%d;%d;%dm\033[38;2;%d;%d;%dm%s",
px.source.bgR, px.source.bgG, px.source.bgB,
px.R, px.G, px.B,
block,
)
}