-
Notifications
You must be signed in to change notification settings - Fork 0
/
texbloader.ooc
397 lines (345 loc) · 11.6 KB
/
texbloader.ooc
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
use zlib
import structs/ArrayList
import io/BinarySequence
import io/[FileReader, FileWriter, BufferReader]
Color: cover{
R, G, B, A: UInt8
}
Bitmap: class{
pixels: ArrayList<Color> = ArrayList<Color> new()
init: func
addPixel: func ~color (c: Color){ pixels add(c) }
addPixel: func ~rgb (r,g,b: Int8){ pixels add((r,g,b,255) as Color) }
addPixel: func ~rgba (r,g,b,a: Int8){ pixels add((r,g,b,a) as Color) }
save: func(buffer: BinarySequenceWriter, height, width: Int){
for(i in 0..height){
for(j in 0..width){
buffer u8(pixels[(height-i-1)*width+j] B)
buffer u8(pixels[(height-i-1)*width+j] G)
buffer u8(pixels[(height-i-1)*width+j] R)
buffer u8(pixels[(height-i-1)*width+j] A)
}
}
}
size: func -> UInt32{
pixels size * 4 * UInt8 size
}
}
Vertex: cover{
x, y, z: Float
}
UV: cover{
u, v: Float
}
Attribute: cover{
id, type, value: UInt32
str: String
is3dModel: Bool = false
loadUVIndex: Bool = false
init: func(buffer: BinarySequenceReader){
id = buffer u8()
type = buffer u8()
match(id){
case 11 => // 3dmodel
is3dModel = true
case =>
}
match(type){
case 0 =>
value = buffer u32()
if(is3dModel){
loadUVIndex = value & 0x80000000 ? true : false
}
case 1 =>
value = buffer u32()
if(is3dModel){
loadUVIndex = value & 0x80000000 ? true : false
}
case 2 => // string
str = buffer pascalString(2)
case => Exception new("Invalid Image Attribute Type") throw()
}
}
}
Tile: class{
verticesList: ArrayList<Vertex> = ArrayList<Vertex> new()
uvList: ArrayList<UV> = ArrayList<UV> new()
indexesList: ArrayList<Int> = ArrayList<Int> new()
boundWidth, boundHeight: Float
vertices, indexes: Int
right, bottom: Int
centerX, centerY: Int
init: func(buffer: BinarySequenceReader, is3dModel, loadUVIndex, compactUV: Bool){
if(is3dModel){
vertices = buffer u16()
indexes = buffer u16()
} else {
vertices = buffer u8()
indexes = buffer u8()
}
right = buffer u16()
bottom = buffer u16()
centerX = buffer u16()
centerY = buffer u16()
max := const static 65536.0f
maxX := const static 9999.0f
maxY := const static 9999.0f
minX := const static -9999.0f
minY := const static -9999.0f
for(n in 0.. vertices){
x := buffer u32() as Float / max - centerX
y := buffer u32() as Float / max - centerY
z := 0.0f
if(is3dModel){
z = buffer u32() as Float / max
} else {
if(x < minX) minX = x
if(x > maxX) maxX = x
if(y < minY) minY = y
if(y > maxY) maxY = y
}
verticesList add((x, y, z) as Vertex)
uvmax := const static 32768.0f
if(loadUVIndex){
if(compactUV){
uvList add((buffer u16() as Float / uvmax, buffer u16() as Float / uvmax) as UV)
} else {
uvList add((buffer u32() as Float / uvmax, buffer u32() as Float / uvmax) as UV)
}
}
}
if(loadUVIndex){
for(i in 0..indexes){
indexesList add(buffer u8())
}
}
boundWidth = maxX - minX
boundHeight = maxY - minY
}
}
Timg: class{
path: String
size: Int
is3dModel := false
loadUVIndex := true
compactUV := false
attributesList: ArrayList<Attribute> = ArrayList<Attribute> new()
tilesList: ArrayList<Tile> = ArrayList<Tile> new()
toString: func -> String{
"Timg: %s, size: %d\n" format(path, size) + \
"tiles: %d, attributes: %d\n" format(tilesList size, attributesList size) + \
"is3dModel: %s, loadUVIndex: %s, compactUV: %s" format(is3dModel toString(), loadUVIndex toString(), compactUV toString())
}
init: func(buffer: BinarySequenceReader){
if(buffer u32() != 0x54494d47) Exception new("Incorrect Magic of Timg") throw()
size = buffer u16()
if(size == 0xFFFF) size = buffer u32()
path = buffer pascalString(2)
subTiles := buffer u16()
if(subTiles == 0xFFFE){ compactUV = true }
if(subTiles = 0xFFFF){
attribs := buffer u16()
for(i in 0..attribs){
att := Attribute new(buffer)
is3dModel |= att is3dModel
loadUVIndex |= att loadUVIndex
attributesList add(att)
}
subTiles = buffer u16()
}
for(i in 0..subTiles){
tilesList add(Tile new(buffer, is3dModel, loadUVIndex, compactUV))
}
}
}
PixelFormat: enum{
RGB565,
RGB5551,
RGB4444,
RGB8888
}
Texb: class{
path: String
size: Int
width: Int
height: Int
type: Int
isCompressed: Bool
isMipmap: Bool
isDoubleBuffered: Bool
pixelFormat: Int
pf: PixelFormat
channelCount: Int = 0
vertices: Int
indexes: Int
images: Int
timgs: ArrayList<Timg> = ArrayList<Timg> new()
bitmap: Bitmap = Bitmap new()
save: func(buffer: BinarySequenceWriter){
// bmp file header
buffer bytes("BM")
buffer u32(0x0e+0x28+bitmap size())
buffer u16(0)
buffer u16(0)
buffer u32(0x36)
// DIB header
buffer u32(0x28)
buffer u32(width)
buffer u32(height)
buffer u16(0x01)
buffer u16(0x20)
buffer u32(0)
buffer u32(bitmap size())
buffer u32(0)
buffer u32(0)
buffer u32(0)
buffer u32(0)
bitmap save(buffer, height, width)
}
toString: func -> String{
"Texb: %s, size: %d\n" format(path, size) + \
"w: %d, h: %d, type: %d\n" format(width, height, type) + \
"compressed: %s, mipmap: %s, doublefbuf: %s\n" format(isCompressed toString(), isMipmap toString(), isDoubleBuffered toString()) + \
"pixelformat: %d\n" format(pixelFormat) + \
"v: %d, i: %d, images: %d" format(vertices, indexes, images)
}
init: func(buffer: BinarySequenceReader){
if(buffer u32() != 0x54455842) Exception new("Incorrect Magic for Texb") throw()
size = buffer u32()
path = buffer pascalString(2)
width = buffer u16()
height = buffer u16()
tp := buffer u16()
type = tp & 0x07
isCompressed = ((tp >> 3) & 0x01) as Bool
isMipmap = ((tp >> 4) & 0x01) as Bool
isDoubleBuffered = ((tp >> 5) & 0x01) as Bool
pixelFormat = (tp >> 6) & 0x03
vertices = buffer u16()
if(vertices == 0xFFFF){ vertices = buffer u32() }
indexes = buffer u16()
if(indexes == 0xFFFF){ indexes = buffer u32() }
images = buffer u16()
uvOffset := 0
if(tp & 0x8000){
uvOffset = buffer u32()
} else {
uvOffset = vertices * 2
vertices *= 4
}
for(i in 0..images){ timgs add(Timg new(buffer)) }
channelCount = match(type){
case 0 => 1
case 1 => 0
case 2 => 2
case 3 => 3
case => 4
}
// all remaining data is bitmap data
hasClick := 0
bytePerPixel := 0
match(pixelFormat){
case 0 =>
hasClick = 0
bytePerPixel = 2
pf = PixelFormat RGB565
case 1 =>
hasClick = 1
bytePerPixel = 2
pf = PixelFormat RGB5551
case 2 =>
hasClick = 1
bytePerPixel = 2
pf = PixelFormat RGB4444
case =>
bytePerPixel = channelCount ? channelCount : 1
hasClick = 1
pf = PixelFormat RGB8888
}
bmpReader := buffer
if(isCompressed){
// compress type
match(compresstype := buffer u32()){
case 0 => //zlib
clength : ULong = size + 8 - buffer bytesRead
cbuffer: UChar* = buffer bytes(clength)
rlength : ULong = width * height * bytePerPixel
destbuffer : UChar* = gc_malloc(rlength * UChar size)
errno: Int
if(errno = Zlib decompress(destbuffer, rlength&, cbuffer, clength)){
Exception new("decompress error, error no %d" format(errno)) throw()
}
bmpReader = BinarySequenceReader new(BufferReader new(Buffer new(destbuffer, rlength)))
case 0x8D64 => // Adaptive Scalable Texture Compression
bytePerPixel = 4
pixelFormat = 0x1401 // GL_UNSIGNED_BYTE, but we don't care because it won't be used
Exception new("need to be implemented") throw()
case 0x8363 => pf = PixelFormat RGB565
case 0x8034 => pf = PixelFormat RGB5551
case 0x8033 => pf = PixelFormat RGB4444
case 0x1401 => pf = PixelFormat RGB8888
case =>
Exception new("Unsupported Compress Format: 0x%x" format(compresstype)) throw()
}
}
// Playground also support "lowRes" translation for small devices.
// Clicking Alpha will also be computed here
// currently we will ignore them
match(pf){
case PixelFormat RGB8888 =>
loadRGB8888(bmpReader)
case PixelFormat RGB565 =>
loadRGB565(bmpReader)
case PixelFormat RGB4444 =>
loadRGB4444(bmpReader)
case PixelFormat RGB5551 =>
loadRGB5551(bmpReader)
case =>
Exception new("Unsupported BMP Pixel Format") throw()
}
}
loadRGB8888: func(buffer: BinarySequenceReader){
for(i in 0..width*height){
bitmap addPixel(buffer u8(), buffer u8(), buffer u8(), buffer u8())
}
}
loadRGB4444: func(buffer: BinarySequenceReader){
for(i in 0..width*height){
b1 : UInt16 = buffer u16()
tmp : UInt8 = (b1 & 0xF000) >> 8
r := tmp | (tmp >> 4)
tmp = (b1 & 0x0F00) >> 4
g := tmp | (tmp >> 4)
tmp = (b1 & 0x00F0)
b := tmp | (tmp >> 4)
tmp = (b1 & 0x000F)
a := tmp | (tmp << 4)
bitmap addPixel(r, g, b, a)
}
}
loadRGB5551: func(buffer: BinarySequenceReader){
for(i in 0..width*height){
b1 : UInt16 = buffer u16()
tmp : UInt8 = (b1 & 0xF800) >> 8
r := tmp | (tmp >> 5)
tmp = (b1 & 0x07C0) >> 3
g := tmp | (tmp >> 5)
tmp = (b1 & 0x003E) << 2
b := tmp | (tmp >> 5)
a := ((tmp & 0x01) as UInt8 << 7) >> 7
bitmap addPixel(r, g, b, a)
}
}
loadRGB565: func(buffer: BinarySequenceReader){
for(i in 0..width*height){
b1 : UInt16 = buffer u16()
tmp : UInt8 = (b1 & 0xF800) >> 8
r := tmp | (tmp >> 5)
tmp = (b1 & 0x07E0) >> 3
g := tmp | (tmp >> 6)
tmp = (b1 & 0x001F) << 3
b := tmp | (tmp >> 5)
bitmap addPixel(r, g, b, 255)
}
}
}