-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpl.go
316 lines (256 loc) · 6.34 KB
/
tpl.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 libtpl
import (
"bytes"
"encoding/binary"
"image"
"image/color"
)
const TPLMagic uint32 = 0x0020AF30
type FileHeader struct {
Magic uint32
NumOfImages uint32
ImageTableOff uint32
}
type ImageHeader struct {
Height uint16
Width uint16
Format TextureFormat
DataOffset uint32
WrapS uint32
WrapT uint32
MinFilter uint32
MagFilter uint32
LODBias float32
EdgeLOD uint8
MidLOD uint8
MaxLOD uint8
Unpacked uint8
_ [8]byte
}
type TPL struct {
Header FileHeader
ImageOff uint32
PaletteOff uint32
Image ImageHeader
}
// TextureFormat is a format that an image can be converted into
type TextureFormat uint32
const (
I4 TextureFormat = iota
I8
IA4
IA8
RGB565
RGB5A3
RGBA8
CI4 = 8
CI8 = 9
CI14X2
CMP = 14
)
// makeTPLHeader makes the TPL header.
func makeTPLHeader(raw []byte, format TextureFormat, width, height int) ([]byte, error) {
buf := bytes.NewBuffer(nil)
tpl := TPL{
Header: FileHeader{
Magic: TPLMagic,
NumOfImages: 1,
ImageTableOff: 0x0C,
},
ImageOff: 20,
PaletteOff: 0,
Image: ImageHeader{
Height: uint16(height),
Width: uint16(width),
Format: format,
DataOffset: 64,
WrapS: 0,
WrapT: 0,
MinFilter: 1,
MagFilter: 1,
LODBias: 0,
EdgeLOD: 0,
MidLOD: 0,
MaxLOD: 0,
Unpacked: 0,
},
}
err := binary.Write(buf, binary.BigEndian, tpl)
if err != nil {
return nil, err
}
buf.Write(raw)
return buf.Bytes(), nil
}
// ToI4 converts an image.Image to I4 TPL format
func ToI4(img image.Image) ([]byte, error) {
raw := imageToRGBA(img)
width := img.Bounds().Max.X
height := img.Bounds().Max.Y
inp := 0
output := make([]byte, addPadding(width, 8)*addPadding(height, 8)/2)
for y1 := 0; y1 < height; y1 += 8 {
for x1 := 0; x1 < width; x1 += 8 {
for y := y1; y < y1+8; y++ {
for x := x1; x < x1+8; x += 2 {
var newPixel byte
if x >= width || y >= height {
newPixel = 0
} else {
rgba := raw[x+(y*width)]
r := (rgba >> 0) & 0xff
g := (rgba >> 8) & 0xff
b := (rgba >> 16) & 0xff
i1 := ((r + g + b) / 3) & 0xff
if (x + (y * width) + 1) >= len(raw) {
rgba = 0
} else {
rgba = raw[x+(y*width)+1]
}
r = (rgba >> 0) & 0xff
g = (rgba >> 8) & 0xff
b = (rgba >> 16) & 0xff
i2 := ((r + g + b) / 3) & 0xff
newPixel = (byte)((((i1 * 15) / 255) << 4) | (((i2 * 15) / 255) & 0xf))
}
output[inp] = newPixel
inp++
}
}
}
}
return makeTPLHeader(output, I4, width, height)
}
// ToIA4 converts an image.Image to IA4 TPL format
func ToIA4(img image.Image) ([]byte, error) {
raw := imageToRGBA(img)
width := img.Bounds().Max.X
height := img.Bounds().Max.Y
inp := 0
output := make([]byte, addPadding(width, 8)*addPadding(height, 4))
for y1 := 0; y1 < height; y1 += 4 {
for x1 := 0; x1 < width; x1 += 8 {
for y := y1; y < y1+4; y++ {
for x := x1; x < x1+8; x++ {
var newPixel byte
if y >= height || x >= width {
newPixel = 0
} else {
rgba := raw[x+(y*width)]
r := (rgba >> 0) & 0xff
g := (rgba >> 8) & 0xff
b := (rgba >> 16) & 0xff
i := ((r + g + b) / 3) & 0xff
a := (rgba >> 24) & 0xff
newPixel = byte((((i * 15) / 255) & 0xf) | (((a * 15) / 255) << 4))
}
output[inp] = newPixel
inp++
}
}
}
}
return makeTPLHeader(output, IA4, width, height)
}
func ToRGB5A3(img image.Image) ([]byte, error) {
raw := imageToRGBA(img)
width := img.Bounds().Max.X
height := img.Bounds().Max.Y
z := -1
output := make([]byte, addPadding(width, 4)*addPadding(height, 4)*2)
for y1 := 0; y1 < height; y1 += 4 {
for x1 := 0; x1 < width; x1 += 4 {
for y := y1; y < y1+4; y++ {
for x := x1; x < x1+4; x++ {
var newPixel int
if y >= height || x >= width {
newPixel = 0
} else {
rgba := int(raw[x+(y*width)])
newPixel = 0
r := (rgba >> 16) & 0xff
g := (rgba >> 8) & 0xff
b := (rgba >> 0) & 0xff
a := (rgba >> 24) & 0xff
if a <= 0xda {
newPixel &= ^(1 << 15)
r = ((r * 15) / 255) & 0xf
g = ((g * 15) / 255) & 0xf
b = ((b * 15) / 255) & 0xf
a = ((a * 7) / 255) & 0x7
newPixel |= (a << 12) | (r << 8) | (g << 4) | b
} else {
newPixel |= 1 << 15
r = ((r * 31) / 255) & 0x1f
g = ((g * 31) / 255) & 0x1f
b = ((b * 31) / 255) & 0x1f
newPixel |= (r << 10) | (g << 5) | b
}
z++
output[z] = byte(newPixel >> 8)
z++
output[z] = byte(newPixel)
}
}
}
}
}
return makeTPLHeader(output, RGB5A3, width, height)
}
// ToRGB565 converts an image.Image to RGB565 TPL format
func ToRGB565(img image.Image) ([]byte, error) {
raw := imageToRGBA(img)
width := img.Bounds().Max.X
height := img.Bounds().Max.Y
z := -1
output := make([]byte, addPadding(width, 4)*addPadding(height, 4)*2)
for y1 := 0; y1 < height; y1 += 4 {
for x1 := 0; x1 < width; x1 += 4 {
for y := y1; y < y1+4; y++ {
for x := x1; x < x1+4; x++ {
var newPixel uint16
if y >= height || x >= width {
newPixel = 0
} else {
rgb := raw[x+y*width]
b := (rgb >> 16) & 0xff
g := (rgb >> 8) & 0xff
r := (rgb >> 0) & 0xff
newPixel = uint16(((b >> 3) << 11) | ((g >> 2) << 5) | ((r >> 3) << 0))
}
z += 1
output[z] = byte(newPixel >> 8)
z += 1
output[z] = byte(newPixel & 0xff)
}
}
}
}
return makeTPLHeader(output, RGB565, width, height)
}
// addPadding calculates the amount of padding the width or height needs.
func addPadding(value, padding int) int {
if value%padding != 0 {
value = value + (padding - (value % padding))
}
return value
}
// imageToRGBA converts an image.Image value to an RGBA bitmap.
func imageToRGBA(img image.Image) []uint32 {
size := img.Bounds()
width, height := size.Max.X, size.Max.Y
raw := make([]uint32, width*height)
idx := 0
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
// Retrieve the color at the current pixel
pixelColor := img.At(x, y)
// Convert the color to RGBA components
rgba := color.RGBAModel.Convert(pixelColor).(color.RGBA)
packedColor := (uint32(rgba.A) << 24) | (uint32(rgba.R) << 16) | (uint32(rgba.G) << 8) | uint32(rgba.B)
raw[idx] = packedColor
idx++
}
}
return raw
}