-
Notifications
You must be signed in to change notification settings - Fork 4
/
ansigo.go
279 lines (219 loc) · 6.24 KB
/
ansigo.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
// AnsiGo 1.01
// Copyright (c) 2012-2019, Frederic Cambus
// https://github.com/fcambus/ansigo
//
// Created: 2012-02-14
// Last Updated: 2019-06-11
//
// AnsiGo is released under the BSD 2-Clause license.
// See LICENSE file for details.
package main
import (
"fmt"
"image"
"image/draw"
"image/png"
"io/ioutil"
"os"
"strconv"
"strings"
)
func main() {
fmt.Println("-------------------------------------------------------------------------------\n AnsiGo 1.01 (c) by Frederic CAMBUS 2012-2019\n-------------------------------------------------------------------------------\n")
// Check input parameters and show usage
if len(os.Args) != 2 {
fmt.Println("USAGE: ansigo inputfile\n")
fmt.Println("EXAMPLES: ansigo ansi.ans\n")
os.Exit(1)
}
input := os.Args[1]
output := input + ".png"
fmt.Println("Input File:", input)
fmt.Println("Output File:", output)
var ansi Ansi
ansi.SetPalette()
ansi.SetFont()
// Load input file
data, err := ioutil.ReadFile(input)
if err != nil {
fmt.Println("\nERROR: Can't open or read", input, "\n")
os.Exit(1)
}
// Process ANSI
for i := 0; i < len(data); i++ {
ansi.character = data[i]
// 80th column wrapping
if ansi.positionX == 80 {
ansi.positionY++
ansi.positionX = 0
}
// CR (Carriage Return)
if ansi.character == '\r' {
continue
}
// LF (Line Feed)
if ansi.character == '\n' {
ansi.positionY++
ansi.positionX = 0
continue
}
// HT (Horizontal Tabulation)
if ansi.character == '\t' {
ansi.positionX += 8
continue
}
// SUB (Substitute)
if ansi.character == '\x1a' {
break
}
// ANSI Sequence: ESC (Escape) + [
if ansi.character == '\x1b' && data[i+1] == '[' {
ansiSequence := []byte{}
sequence:
for j := 0; j < 12; j++ {
ansiSequenceCharacter := data[i+2+j]
switch ansiSequenceCharacter {
case 'H', 'f': // Cursor Position
ansiSequenceValues := strings.SplitN(string(ansiSequence), ";", -1)
valueY, _ := strconv.Atoi(ansiSequenceValues[0])
ansi.positionY = valueY - 1
valueX, _ := strconv.Atoi(ansiSequenceValues[1])
ansi.positionX = valueX - 1
i += j + 2
break sequence
case 'A': // Cursor Up
valueY, _ := strconv.Atoi(string(ansiSequence))
if valueY == 0 {
valueY++
}
ansi.positionY = ansi.positionY - valueY
i += j + 2
break sequence
case 'B': // Cursor Down
valueY, _ := strconv.Atoi(string(ansiSequence))
if valueY == 0 {
valueY++
}
ansi.positionY = ansi.positionY + valueY
i += j + 2
break sequence
case 'C': // Cursor Forward
valueX, _ := strconv.Atoi(string(ansiSequence))
if valueX == 0 {
valueX++
}
ansi.positionX = ansi.positionX + valueX
if ansi.positionX > 80 {
ansi.positionX = 80
}
i += j + 2
break sequence
case 'D': // Cursor Backward
valueX, _ := strconv.Atoi(string(ansiSequence))
if valueX == 0 {
valueX++
}
ansi.positionX = ansi.positionX - valueX
if ansi.positionX < 0 {
ansi.positionX = 0
}
i += j + 2
break sequence
case 's': // Save Cursor Position
ansi.savedPositionY = ansi.positionY
ansi.savedPositionX = ansi.positionX
i += j + 2
break sequence
case 'u': // Restore Cursor Position
ansi.positionY = ansi.savedPositionY
ansi.positionX = ansi.savedPositionX
i += j + 2
break sequence
case 'J': // Erase Display
value, _ := strconv.Atoi(string(ansiSequence))
if value == 2 {
ansi.buffer = nil
ansi.positionX = 0
ansi.positionY = 0
ansi.sizeX = 0
ansi.sizeY = 0
}
i += j + 2
break sequence
case 'm': // Set Graphic Rendition
ansiSequenceValues := strings.SplitN(string(ansiSequence), ";", -1)
for j := 0; j < len(ansiSequenceValues); j++ {
valueColor, _ := strconv.Atoi(ansiSequenceValues[j])
switch valueColor {
case 0:
ansi.colorBackground = 0
ansi.colorForeground = 7
ansi.bold = false
case 1:
ansi.colorForeground += 8
ansi.bold = true
case 5:
ansi.colorBackground += 8
case 30, 31, 32, 33, 34, 35, 36, 37:
ansi.colorForeground = valueColor - 30
if ansi.bold {
ansi.colorForeground += 8
}
case 40, 41, 42, 43, 44, 45, 46, 47:
ansi.colorBackground = valueColor - 40
}
}
i += j + 2
break sequence
case 'h', 'l': // Skipping Set Mode And Reset Mode Sequences
i += j + 2
break sequence
}
ansiSequence = append(ansiSequence, ansiSequenceCharacter)
}
} else {
// Record Number Of Columns And Lines Used
if ansi.positionX > ansi.sizeX {
ansi.sizeX = ansi.positionX
}
if ansi.positionY > ansi.sizeY {
ansi.sizeY = ansi.positionY
}
// Write Current Character Info In A Temporary Array
ansi.buffer = append(ansi.buffer, Character{ansi.colorBackground, ansi.colorForeground, ansi.positionX, ansi.positionY, ansi.character})
ansi.positionX++
}
}
// Allocate Image Buffer Memory
canvasSize := image.Rect(0, 0, 640, (ansi.sizeY+1)*16)
canvas := image.NewRGBA(canvasSize)
// Draw The Canvas Background
draw.Draw(canvas, canvas.Bounds(), &image.Uniform{ansi.palette[0]}, image.ZP, draw.Src)
// Render ANSI
for i := 0; i < len(ansi.buffer); i++ {
character := ansi.buffer[i]
// Set Background
draw.Draw(canvas, image.Rect(character.positionX*8, character.positionY*16, character.positionX*8+8, character.positionY*16+16), &image.Uniform{ansi.palette[character.colorBackground]}, image.ZP, draw.Src)
// Draw Character
for line := 0; line < 16; line++ {
for column := 0; column < 8; column++ {
if (ansi.font[line+(int(character.code)*16)] & (0x80 >> uint(column))) != 0 {
canvas.Set(character.positionX*8+column, line+character.positionY*16, ansi.palette[character.colorForeground])
}
}
}
}
// Create Output File
outputFile, err := os.Create(output)
if err != nil {
fmt.Println("ERROR: Can't create output file.")
os.Exit(1)
}
// Encode PNG image
if err = png.Encode(outputFile, canvas); err != nil {
fmt.Println("ERROR: Can't encode PNG file.")
os.Exit(1)
}
outputFile.Close()
fmt.Println("\nSuccessfully created file", output, "\n")
}