-
Notifications
You must be signed in to change notification settings - Fork 5
/
gx2FormConv_cy.pyx
144 lines (107 loc) · 3.87 KB
/
gx2FormConv_cy.pyx
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright © 2018 AboodXD
# Licensed under GNU GPLv3
################################################################
################################################################
from cpython cimport array
from cython cimport view
from libc.stdlib cimport malloc, free
ctypedef unsigned char u8
ctypedef unsigned short u16
ctypedef unsigned int u32
cdef u8 *getComponentsFromPixel(str format_, pixel, u8 *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
cpdef bytes torgba8(u32 width, u32 height, bytearray data_, str format_, u32 bpp, list compSel_):
cdef:
array.array dataArr = array.array('B', data_)
u8 *data = dataArr.data.as_uchars
u32[4] compSel
u32 i, elem
for i, elem in enumerate(compSel_):
compSel[i] = elem
assert len(data_) >= width * height * bpp
cdef:
u32 size = width * height * 4
u8 *new_data = <u8 *>malloc(size)
u32 x, y, pos, pos_, pixel
u8* comp = <u8 *>malloc(6) # "u8[6] comp" causes issues
comp[0] = 0
comp[1] = 0
comp[2] = 0
comp[3] = 0xFF
comp[4] = 0
comp[5] = 0xFF
if bpp not in [1, 2, 4]:
try:
return bytes(<u8[:size]>new_data)
finally:
free(new_data)
free(comp)
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] = <u8>comp[compSel[3]]
new_data[pos_ + 2] = <u8>comp[compSel[2]]
new_data[pos_ + 1] = <u8>comp[compSel[1]]
new_data[pos_ + 0] = <u8>comp[compSel[0]]
try:
return bytes(<u8[:size]>new_data)
finally:
free(new_data)
free(comp)
cpdef bytes rgb8torgbx8(bytearray data):
cdef:
u32 numPixels = len(data) // 3
u8 *new_data = <u8 *>malloc(numPixels * 4)
u32 i
try:
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(<u8[:numPixels * 4]>new_data)
finally:
free(new_data)