-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbin.go
95 lines (77 loc) · 2.44 KB
/
bin.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
// bin.go
// go-ansi
//
// Copyright (C) 2017 ActiveState Software Inc.
// Written by Pete Garcin (@rawktron)
//
// Based on ansilove/C
// Copyright (C) 2011-2017 Stefan Vogt, Brian Cassidy, and Frederic Cambus.
// All rights reserved.
// ansilove/C is licensed under the BSD-2 License.
//
// go-ansi is licensed under the BSD 3-Clause License.
// See the file LICENSE for details.
//
package goansi
import (
"fmt"
"image"
"image/color"
"image/draw"
"os"
)
// binary processes inputFileBuffer and generates an image
func binfile(inputFileBuffer []byte, inputFileSize int64, columns int, fontName string, bits int, icecolors bool) image.Image {
// some type declarations
var f font
// font selection
alSelectFont(&f, fontName)
// libgd image pointers
var imBinary draw.Image
imBinary = image.NewRGBA(image.Rect(0, 0, columns*bits, (int(inputFileSize)/2)/columns*f.sizeY))
if imBinary == nil {
fmt.Printf("\nError, can't allocate buffer image memory.\n\n")
os.Exit(6)
}
black := color.RGBA{0, 0, 0, 255}
draw.Draw(imBinary, imBinary.Bounds(), &image.Uniform{black}, image.ZP, draw.Src)
// allocate color palette
var colors [16]color.RGBA
colors[0] = color.RGBA{0, 0, 0, 255}
colors[1] = color.RGBA{0, 0, 170, 255}
colors[2] = color.RGBA{0, 170, 0, 255}
colors[3] = color.RGBA{0, 170, 170, 255}
colors[4] = color.RGBA{170, 0, 0, 255}
colors[5] = color.RGBA{170, 0, 170, 255}
colors[6] = color.RGBA{170, 85, 0, 255}
colors[7] = color.RGBA{170, 170, 170, 255}
colors[8] = color.RGBA{85, 85, 85, 255}
colors[9] = color.RGBA{85, 85, 255, 255}
colors[10] = color.RGBA{85, 255, 85, 255}
colors[11] = color.RGBA{85, 255, 255, 255}
colors[12] = color.RGBA{255, 85, 85, 255}
colors[13] = color.RGBA{255, 85, 255, 255}
colors[14] = color.RGBA{255, 255, 85, 255}
colors[15] = color.RGBA{255, 255, 255, 255}
// process binary
var character, attribute, colorBackground, colorForeground int
var loop, positionX, positionY int = 0, 0, 0
for loop < int(inputFileSize) {
if positionX == columns {
positionX = 0
positionY++
}
character = int(inputFileBuffer[loop])
attribute = int(inputFileBuffer[loop+1])
colorBackground = (attribute & 240) >> 4
colorForeground = (attribute & 15)
if colorBackground > 8 && !icecolors {
colorBackground -= 8
}
alDrawChar(imBinary, f.data, bits, f.sizeY,
positionX, positionY, colors[colorBackground], colors[colorForeground], byte(character))
positionX++
loop += 2
}
return imBinary
}