-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxbin.go
192 lines (160 loc) · 4.75 KB
/
xbin.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
// xbin.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"
)
// Xbin processes inputFileBuffer and outputs image data
func xbin(inputFileBuffer []byte, inputFileSize int64) image.Image {
var f font
if string(inputFileBuffer[0:4]) == "XBIN\x1a" {
fmt.Print("\nNot an XBin.\n\n")
os.Exit(4)
}
var xbinWidth, xbinHeight, xbinFontSize, xbinFlags int
xbinWidth = (int(inputFileBuffer[6]) << 8) + int(inputFileBuffer[5])
xbinHeight = (int(inputFileBuffer[8]) << 8) + int(inputFileBuffer[7])
xbinFontSize = int(inputFileBuffer[9])
xbinFlags = int(inputFileBuffer[10])
var imXBIN draw.Image
imXBIN = image.NewRGBA(image.Rect(0, 0, 8*int(xbinWidth), int(xbinFontSize)*int(xbinHeight)))
if imXBIN == nil {
fmt.Printf("\nError, can't allocate buffer image memory.\n\n")
os.Exit(6)
}
black := color.RGBA{0, 0, 0, 255}
draw.Draw(imXBIN, imXBIN.Bounds(), &image.Uniform{black}, image.ZP, draw.Src)
var colors [16]color.RGBA
offset := 11
// palette
if (xbinFlags & 1) == 1 {
var index int
for loop := 0; loop < 16; loop++ {
index = (loop * 3) + offset
colors[loop] = color.RGBA{(inputFileBuffer[index]<<2 | inputFileBuffer[index]>>4),
(inputFileBuffer[index+1]<<2 | inputFileBuffer[index+1]>>4),
(inputFileBuffer[index+2]<<2 | inputFileBuffer[index+2]>>4), 255}
}
offset += 48
} else {
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}
}
// font
if (xbinFlags & 2) == 2 {
var numchars int
if (xbinFlags & 0x10) != 0 {
numchars = 512
} else {
numchars = 256
}
f.data = inputFileBuffer[offset : offset+(int(xbinFontSize)*numchars)]
f.sizeY = int(xbinFontSize)
f.sizeX = 8
f.isAmigaFont = false
offset += (int(xbinFontSize) * numchars)
} else {
// using default 80x25 font
alSelectFont(&f, "80x25")
}
var positionX, positionY int = 0, 0
var character, attribute, colorForeground, colorBackground int
// read compressed xbin
if (xbinFlags & 4) == 4 {
for offset < int(inputFileSize) && positionY != int(xbinHeight) {
ctype := inputFileBuffer[offset] & 0xC0
counter := (inputFileBuffer[offset] & 0x3F) + 1
character = -1
attribute = -1
offset++
for i := counter; i > 0; i-- {
// none
if ctype == 0 {
character = int(inputFileBuffer[offset])
attribute = int(inputFileBuffer[offset+1])
offset += 2
} else if ctype == 0x40 {
// char
if character == -1 {
character = int(inputFileBuffer[offset])
offset++
}
attribute = int(inputFileBuffer[offset])
offset++
} else if ctype == 0x80 {
// attr
if attribute == -1 {
attribute = int(inputFileBuffer[offset])
offset++
}
character = int(inputFileBuffer[offset])
offset++
} else {
// both
if character == -1 {
character = int(inputFileBuffer[offset])
offset++
}
if attribute == -1 {
attribute = int(inputFileBuffer[offset])
offset++
}
}
colorBackground = (attribute & 240) >> 4
colorForeground = attribute & 15
alDrawChar(imXBIN, f.data, 8, 16, positionX, positionY, colors[colorBackground], colors[colorForeground], byte(character))
positionX++
if positionX == int(xbinWidth) {
positionX = 0
positionY++
}
}
}
} else {
// read uncompressed xbin
for offset < int(inputFileSize) && positionY != int(xbinHeight) {
if positionX == int(xbinWidth) {
positionX = 0
positionY++
}
character = int(inputFileBuffer[offset])
attribute = int(inputFileBuffer[offset+1])
colorBackground = (attribute & 240) >> 4
colorForeground = attribute & 15
alDrawChar(imXBIN, f.data, 8, int(xbinFontSize), positionX, positionY, colors[colorBackground], colors[colorForeground], byte(character))
positionX++
offset += 2
}
}
return imXBIN
}