forked from aboood40091/LayoutExporterU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gx2FormConv.py
90 lines (67 loc) · 2.69 KB
/
gx2FormConv.py
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
def getComponentsFromPixel(format_, pixel, comp):
if format_ == 'l8':
comp[0] = pixel & 0xFF
elif format_ == 'la8':
comp[0] = pixel & 0xFF
comp[1] = (pixel & 0xFF00) >> 8
elif format_ == 'la4':
comp[0] = (pixel & 0xF) * 17
comp[1] = ((pixel & 0xF0) >> 4) * 17
elif format_ == 'rgb565':
comp[0] = int((pixel & 0x1F) / 0x1F * 0xFF)
comp[1] = int(((pixel & 0x7E0) >> 5) / 0x3F * 0xFF)
comp[2] = int(((pixel & 0xF800) >> 11) / 0x1F * 0xFF)
elif format_ == 'rgb5a1':
comp[0] = int((pixel & 0x1F) / 0x1F * 0xFF)
comp[1] = int(((pixel & 0x3E0) >> 5) / 0x1F * 0xFF)
comp[2] = int(((pixel & 0x7c00) >> 10) / 0x1F * 0xFF)
comp[3] = ((pixel & 0x8000) >> 15) * 0xFF
elif format_ == 'rgba4':
comp[0] = (pixel & 0xF) * 17
comp[1] = ((pixel & 0xF0) >> 4) * 17
comp[2] = ((pixel & 0xF00) >> 8) * 17
comp[3] = ((pixel & 0xF000) >> 12) * 17
elif format_ == 'rgb8':
comp[0] = pixel & 0xFF
comp[1] = (pixel & 0xFF00) >> 8
comp[2] = (pixel & 0xFF0000) >> 16
elif format_ == 'bgr10a2':
comp[0] = int((pixel & 0x3FF) / 0x3FF * 0xFF)
comp[1] = int(((pixel & 0xFFC00) >> 10) / 0x3FF * 0xFF)
comp[2] = int(((pixel & 0x3FF00000) >> 20) / 0x3FF * 0xFF)
comp[3] = int(((pixel & 0xC0000000) >> 30) / 0x3 * 0xFF)
elif format_ == 'rgba8':
comp[0] = pixel & 0xFF
comp[1] = (pixel & 0xFF00) >> 8
comp[2] = (pixel & 0xFF0000) >> 16
comp[3] = (pixel & 0xFF000000) >> 24
return comp
def torgba8(width, height, data, format_, bpp, compSel):
assert len(data) >= width * height * bpp
size = width * height * 4
new_data = bytearray(size)
comp = bytearray([0, 0, 0, 0xFF, 0, 0xFF])
if bpp not in [1, 2, 4]:
return bytes(new_data)
for y in range(height):
for x in range(width):
pos = (y * width + x) * bpp
pos_ = (y * width + x) * 4
pixel = 0
for i in range(bpp):
pixel |= data[pos + i] << (8 * i)
comp = getComponentsFromPixel(format_, pixel, comp)
new_data[pos_ + 3] = comp[compSel[3]]
new_data[pos_ + 2] = comp[compSel[2]]
new_data[pos_ + 1] = comp[compSel[1]]
new_data[pos_ + 0] = comp[compSel[0]]
return bytes(new_data)
def rgb8torgbx8(data):
numPixels = len(data) // 3
new_data = bytearray(numPixels * 4)
for i in range(numPixels):
new_data[4 * i + 0] = data[3 * i + 0]
new_data[4 * i + 1] = data[3 * i + 1]
new_data[4 * i + 2] = data[3 * i + 2]
new_data[4 * i + 3] = 0xFF
return bytes(new_data)