-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcolor_conversion.py
More file actions
143 lines (95 loc) · 4.69 KB
/
color_conversion.py
File metadata and controls
143 lines (95 loc) · 4.69 KB
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
"""
Conversion functions for RGB565, ARGB1555, ARGB4444 and RGBA8888 color spaces
Split into individual functions for speed. You know what speed is, right, Python?
"""
import numpy
'''
16 bit to RGBA8888
'''
def convertRGB565toRGBA8888(pixel_byte_array) -> numpy.array:
numpy_pixel_byte_array = numpy.frombuffer(pixel_byte_array, dtype=numpy.uint8)
# Base convertion
R_numpy = (numpy_pixel_byte_array[1::2] & 0b11111000)
G_numpy = ((numpy_pixel_byte_array[1::2] & 0b00000111) << 5) + ((numpy_pixel_byte_array[::2] & 0b11100000) >> 3)
B_numpy = (numpy_pixel_byte_array[::2] & 0b00011111) << 3
A_numpy = numpy.full(len(pixel_byte_array)//2, 255, dtype=numpy.uint8)
# Color correction
R_numpy = R_numpy + (R_numpy // 8) // 4
G_numpy = G_numpy + (G_numpy // 4) // 16
B_numpy = B_numpy + (B_numpy // 8) // 4
rgba8888_pixel_list_numpy = numpy.stack((R_numpy, G_numpy, B_numpy, A_numpy), axis=1)
return rgba8888_pixel_list_numpy
def convertARGB1555toRGBA8888(pixel_byte_array) -> numpy.array:
numpy_pixel_byte_array = numpy.frombuffer(pixel_byte_array, dtype=numpy.uint8)
# Base convertion
A_numpy = (numpy_pixel_byte_array[1::2] & 0b10000000) >> 7
R_numpy = (numpy_pixel_byte_array[1::2] & 0b01111100) << 1
G_numpy = ((numpy_pixel_byte_array[1::2] & 0b00000011) << 6) + ((numpy_pixel_byte_array[::2] & 0b11100000) >> 2)
B_numpy = (numpy_pixel_byte_array[::2] & 0b00011111) << 3
# Color correction
R_numpy = R_numpy + (R_numpy // 8) // 4
G_numpy = G_numpy + (G_numpy // 8) // 4
B_numpy = B_numpy + (B_numpy // 8) // 4
A_numpy = A_numpy * 255
rgba8888_pixel_list_numpy = numpy.stack((R_numpy, G_numpy, B_numpy, A_numpy), axis=1)
return rgba8888_pixel_list_numpy
def convertARGB4444toRGBA8888(pixel_byte_array) -> numpy.array:
numpy_pixel_byte_array = numpy.frombuffer(pixel_byte_array, dtype=numpy.uint8)
# Base convertion
A_numpy = (numpy_pixel_byte_array[1::2] & 0b11110000)
R_numpy = (numpy_pixel_byte_array[1::2] & 0b00001111) << 4
G_numpy = (numpy_pixel_byte_array[::2] & 0b11110000)
B_numpy = (numpy_pixel_byte_array[::2] & 0b00001111) << 4
# Color correction
R_numpy = R_numpy + (R_numpy // 16)
G_numpy = G_numpy + (G_numpy // 16)
B_numpy = B_numpy + (B_numpy // 16)
A_numpy = A_numpy + (A_numpy // 16)
rgba8888_pixel_list_numpy = numpy.stack((R_numpy, G_numpy, B_numpy, A_numpy), axis=1)
return rgba8888_pixel_list_numpy
'''
RGBA8888 to 16 bit
'''
def convertRGBA8888toRGB565bytes(png_numpy_array) -> bytes:
color_channel_count = png_numpy_array.shape[2]
png_flat_numpy_array = png_numpy_array.ravel()
R_numpy = png_flat_numpy_array[2::color_channel_count]
G_numpy = png_flat_numpy_array[1::color_channel_count]
B_numpy = png_flat_numpy_array[0::color_channel_count]
R_numpy = R_numpy & 0b11111000
G_numpy = G_numpy & 0b11111100
B_numpy = B_numpy & 0b11111000
RG_numpy = (R_numpy >> 3) + (G_numpy << 3)
GB_numpy = (G_numpy >> 5) + B_numpy
color_bytes_array = numpy.stack((RG_numpy, GB_numpy), axis=1)
return color_bytes_array.tobytes()
def convertRGBA8888toARGB1555bytes(png_numpy_array) -> bytes:
color_channel_count = png_numpy_array.shape[2]
png_flat_numpy_array = png_numpy_array.ravel()
R_numpy = png_flat_numpy_array[0::color_channel_count]
G_numpy = png_flat_numpy_array[1::color_channel_count]
B_numpy = png_flat_numpy_array[2::color_channel_count]
A_numpy = png_flat_numpy_array[3::color_channel_count]
A_numpy = A_numpy & 0b10000000
R_numpy = R_numpy & 0b11111000
G_numpy = G_numpy & 0b11111000
B_numpy = B_numpy & 0b11111000
ARG_numpy = A_numpy + (R_numpy >> 1) + (G_numpy >> 6)
GB_numpy = (G_numpy << 2) + (B_numpy >> 3)
color_bytes_array = numpy.stack((GB_numpy, ARG_numpy), axis=1)
return color_bytes_array.tobytes()
def convertRGBA8888toARGB4444bytes(png_numpy_array) -> bytes:
color_channel_count = png_numpy_array.shape[2]
png_flat_numpy_array = png_numpy_array.ravel()
R_numpy = png_flat_numpy_array[0::color_channel_count]
G_numpy = png_flat_numpy_array[1::color_channel_count]
B_numpy = png_flat_numpy_array[2::color_channel_count]
A_numpy = png_flat_numpy_array[3::color_channel_count]
A_numpy = A_numpy & 0b11110000
R_numpy = R_numpy & 0b11110000
G_numpy = G_numpy & 0b11110000
B_numpy = B_numpy & 0b11110000
AR_numpy = A_numpy + (R_numpy >> 4)
GB_numpy = G_numpy + (B_numpy >> 4)
color_bytes_array = numpy.stack((GB_numpy, AR_numpy), axis=1)
return color_bytes_array.tobytes()