-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.py
175 lines (163 loc) · 8.55 KB
/
common.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
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
def is_prime(num):
"""Check if the number in paramter is a prime number, return True if it's the case, return False otherwise
"""
return all(num % i for i in range(2, num))
def parity_bit(num):
binary = bin(num).replace("0b", "")
count = str(binary).count('1')
return True if count % 2 == 0 else False
def la_to_binary(bits_quantity, channel, l, a=None):
l_binary = '{0:08b}'.format(l)
if a is not None:
a_binary = '{0:08b}'.format(a)
if channel == "LA":
return l_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "AL":
return a_binary[-bits_quantity:] + l_binary[-bits_quantity:]
elif channel == "L":
return l_binary[-bits_quantity:]
elif channel == "A":
return a_binary[-bits_quantity:]
def rgba_to_binary(bits_quantity, channel, r, g, b, a=None):
r_binary = '{0:08b}'.format(r)
g_binary = '{0:08b}'.format(g)
b_binary = '{0:08b}'.format(b)
if a is not None:
a_binary = '{0:08b}'.format(a)
if channel == "RGBA":
return r_binary[-bits_quantity:] + g_binary[-bits_quantity:] + b_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "RGAB":
return r_binary[-bits_quantity:] + g_binary[-bits_quantity:] + a_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "RAGB":
return r_binary[-bits_quantity:] + a_binary[-bits_quantity:] + g_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "RABG":
return r_binary[-bits_quantity:] + a_binary[-bits_quantity:] + b_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "RBGA":
return r_binary[-bits_quantity:] + b_binary[-bits_quantity:] + g_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "RBAG":
return r_binary[-bits_quantity:] + b_binary[-bits_quantity:] + a_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "BGRA":
return b_binary[-bits_quantity:] + g_binary[-bits_quantity:] + r_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "BGAR":
return b_binary[-bits_quantity:] + g_binary[-bits_quantity:] + a_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "BAGR":
return b_binary[-bits_quantity:] + a_binary[-bits_quantity:] + g_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "BARG":
return b_binary[-bits_quantity:] + a_binary[-bits_quantity:] + r_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "BRGA":
return b_binary[-bits_quantity:] + r_binary[-bits_quantity:] + g_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "BRAG":
return b_binary[-bits_quantity:] + r_binary[-bits_quantity:] + a_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "GRBA":
return g_binary[-bits_quantity:] + r_binary[-bits_quantity:] + b_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "GRAB":
return g_binary[-bits_quantity:] + r_binary[-bits_quantity:] + a_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "GARB":
return g_binary[-bits_quantity:] + a_binary[-bits_quantity:] + r_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "GABR":
return g_binary[-bits_quantity:] + a_binary[-bits_quantity:] + b_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "GBRA":
return g_binary[-bits_quantity:] + b_binary[-bits_quantity:] + r_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "GBAR":
return g_binary[-bits_quantity:] + b_binary[-bits_quantity:] + a_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "AGBR":
return a_binary[-bits_quantity:] + g_binary[-bits_quantity:] + b_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "AGRB":
return a_binary[-bits_quantity:] + g_binary[-bits_quantity:] + r_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "ARGB":
return a_binary[-bits_quantity:] + r_binary[-bits_quantity:] + g_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "ARBG":
return a_binary[-bits_quantity:] + r_binary[-bits_quantity:] + b_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "ABGR":
return a_binary[-bits_quantity:] + b_binary[-bits_quantity:] + g_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "ABRG":
return a_binary[-bits_quantity:] + b_binary[-bits_quantity:] + r_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "RGB":
return r_binary[-bits_quantity:] + g_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "RBG":
return r_binary[-bits_quantity:] + b_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "GRB":
return g_binary[-bits_quantity:] + r_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "GBR":
return g_binary[-bits_quantity:] + b_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "BGR":
return b_binary[-bits_quantity:] + g_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "BRG":
return b_binary[-bits_quantity:] + r_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "AGB":
return a_binary[-bits_quantity:] + g_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "ABG":
return a_binary[-bits_quantity:] + b_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "GAB":
return g_binary[-bits_quantity:] + a_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "GBA":
return g_binary[-bits_quantity:] + b_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "BGA":
return b_binary[-bits_quantity:] + g_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "BAG":
return b_binary[-bits_quantity:] + a_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "RAB":
return r_binary[-bits_quantity:] + a_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "RBA":
return r_binary[-bits_quantity:] + b_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "ARB":
return a_binary[-bits_quantity:] + r_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "ABR":
return a_binary[-bits_quantity:] + b_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "BAR":
return b_binary[-bits_quantity:] + a_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "BRA":
return b_binary[-bits_quantity:] + r_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "RGA":
return r_binary[-bits_quantity:] + g_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "RAG":
return r_binary[-bits_quantity:] + a_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "GRA":
return g_binary[-bits_quantity:] + r_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "GAR":
return g_binary[-bits_quantity:] + a_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "AGR":
return a_binary[-bits_quantity:] + g_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "ARG":
return a_binary[-bits_quantity:] + r_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "RG":
return r_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "RA":
return r_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "RB":
return r_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "GA":
return g_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "GB":
return g_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "GR":
return g_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "BA":
return b_binary[-bits_quantity:] + a_binary[-bits_quantity:]
elif channel == "BG":
return b_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "BR":
return b_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "AR":
return a_binary[-bits_quantity:] + r_binary[-bits_quantity:]
elif channel == "AG":
return a_binary[-bits_quantity:] + g_binary[-bits_quantity:]
elif channel == "AB":
return a_binary[-bits_quantity:] + b_binary[-bits_quantity:]
elif channel == "R":
return r_binary[-bits_quantity:]
elif channel == "B":
return b_binary[-bits_quantity:]
elif channel == "G":
return g_binary[-bits_quantity:]
elif channel == "A":
return a_binary[-bits_quantity:]
def binary_to_ascii(binary_string):
"""Basic function that takes a binary string in parameters. e.g: '00111010101010' and transform it to an ascii
string.
"""
binary_bytes = [binary_string[i:i + 8] for i in range(0, len(binary_string), 8)]
buff = ''
for character in binary_bytes:
buff += chr(int(character, 2))
return buff