- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.py
More file actions
Latest commit
314 lines (229 loc) · 12.1 KB
/
Copy pathFunctions.py
File metadata and controls
314 lines (229 loc) · 12.1 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
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
fromjsonimportload
frommathimportceil
fromMasksimportMASKS
withopen('alphabet.json', 'r') asinput_file:
ALPHABET=load(input_file)
withopen('amount of data.json', 'r') asinput_file:
MAXIMUM_SIZE=load(input_file)
withopen('lengthes of service fields.json', 'r') asinput_file:
SERVICE_FIELDS_LENGTHES=load(input_file)
withopen('amount of blocks.json', 'r') asinput_file:
AMOUNT_OF_BLOCKS=load(input_file)
withopen('galois field.json', 'r') asinput_file:
GALOIS_FIELD=load(input_file)
withopen('reverse galois field.json', 'r') asinput_file:
REVERSE_GALOIS_FIELD=load(input_file)
withopen('generating polynomials.json', 'r') asinput_file:
GENERATING_POLYNOMIALS=load(input_file)
withopen('codes of mask and correction level.json', 'r') asinput_file:
MASK_AND_CORRECTION_LEVEL_CODES=load(input_file)
withopen('amount of correction bytes.json', 'r') asinput_file:
AMOUNT_OF_CORRECTION_BYTES=load(input_file)
defverify_data(string, encoding_type):
ifstring.isdigit() andencoding_type=='digit':
returnTrue
elifencoding_type=='alphadigit':
forsymbolinstring.upper():
ifsymbolnotinALPHABET:
returnFalse
else:
returnTrue
elifencoding_type=='UTF8':
returnTrue
else:
returnFalse
defdigit_encoding(string):
blocks= []
foriinrange(ceil(len(string) /3)):
blocks.append(string[i*3:i*3+3])
encoded_data=''
encoded_blocks_lengthes= [4, 7, 10]
forblockinblocks:
encoded_block=bin(int(block))[2:]
encoded_data+=encoded_block.zfill(encoded_blocks_lengthes[len(block)-1])
returnencoded_data
defalphadigit_encoding(string):
encoded_data=''
foriinrange(len(string) //2):
encoded_block=bin(ALPHABET[string[i*2]] *45+ALPHABET[string[i*2+1]])[2:]
encoded_data+=encoded_block.zfill(11)
iflen(string) %2:
encoded_block=bin(ALPHABET[string[-1]])[2:]
encoded_data+=encoded_block.zfill(6)
returnencoded_data
defUTF8_encoding(string):
encoded_data=''
list_of_bytes=list(string.encode("utf-8"))
forbyteinlist_of_bytes:
encoded_data+=bin(byte)[2:].zfill(8)
returnencoded_data
defdetermine_version(correction_level, encoding_type, information_size):
flag=False
version=0
foriinrange(40):
ifi<9:
index=0
elif9<=i<26:
index=1
else:
index=2
if (information_size+4+SERVICE_FIELDS_LENGTHES[encoding_type][index]) <=MAXIMUM_SIZE[correction_level][i]:
flag=True
version=i+1
break
returnflag, version, index
deffill_data(correction_level, version, bit_data):
amount_of_zeros= (8- (len(bit_data) %8)) %8
bit_data+='0'*amount_of_zeros
foriinrange((MAXIMUM_SIZE[correction_level][version-1] -len(bit_data)) //8):
ifi%2==0:
bit_data+='11101100'
else:
bit_data+='00010001'
byte_data= []
foriinrange(len(bit_data) //8):
byte_data.append(bit_data[i*8:(i+1)*8])
returnbyte_data
defsplit_into_blocks(correction_level, version, byte_data):
blocks_amount=AMOUNT_OF_BLOCKS[correction_level][version-1]
normal_blocks_amount=blocks_amount-len(byte_data) %blocks_amount
extended_blocks_amount=len(byte_data) %blocks_amount
normal_blocks_length=len(byte_data) //blocks_amount
extended_blocks_length=ceil(len(byte_data) /blocks_amount)
blocks= [[] foriinrange(blocks_amount)]
foriinrange(normal_blocks_amount):
blocks[i] =byte_data[i*normal_blocks_length:(i+1)*normal_blocks_length]
foriinrange(extended_blocks_amount):
blocks[normal_blocks_amount+i] =byte_data[normal_blocks_amount*normal_blocks_length+i*extended_blocks_length:normal_blocks_amount*normal_blocks_length+(i+1)*extended_blocks_length]
returnblocks
defRS_encoding(correction_level, version, byte_data):
correction_bytes_amount=AMOUNT_OF_CORRECTION_BYTES[correction_level][version-1]
generating_polynomial=GENERATING_POLYNOMIALS[str(correction_bytes_amount)]
list_of_bytes= [int(x, 2) forxinbyte_data]
list_of_bytes.extend([0] *abs(correction_bytes_amount-len(list_of_bytes)))
foriinrange(len(byte_data)):
a=list_of_bytes.pop(0)
list_of_bytes.append(0)
ifa!=0:
b=REVERSE_GALOIS_FIELD[a]
forjinrange(correction_bytes_amount):
c=GALOIS_FIELD[(generating_polynomial[j] +b) %255]
list_of_bytes[j] ^=c
final_list= []
foriinrange(correction_bytes_amount):
final_list.append(bin(list_of_bytes[i])[2:].zfill(8))
returnfinal_list
defdraw_info_codes(correction_level, mask, size, pixels, white, black):
forjinrange(6):
color=blackif (MASK_AND_CORRECTION_LEVEL_CODES[correction_level][mask][j] =='1') elsewhite
pixels[8, size[1] -1-j], pixels[j, 8] =color, color
color=blackif (MASK_AND_CORRECTION_LEVEL_CODES[correction_level][mask][6] =='1') elsewhite
pixels[8, size[1] -7], pixels[7, 8] =color, color
color=blackif (MASK_AND_CORRECTION_LEVEL_CODES[correction_level][mask][7] =='1') elsewhite
pixels[size[1] -8, 8], pixels[8, 8] =color, color
color=blackif (MASK_AND_CORRECTION_LEVEL_CODES[correction_level][mask][8] =='1') elsewhite
pixels[size[1] -7, 8], pixels[8, 7] =color, color
foriinrange(9, 15):
color=blackif (MASK_AND_CORRECTION_LEVEL_CODES[correction_level][mask][i] =='1') elsewhite
pixels[size[1] -15+i, 8], pixels[8, 14-i] =color, color
defapply_data(mask, size, pixels, bin_output, white, black):
i=size[0] -1
current=0
length=len(bin_output)
flag=True
whilei>0:
j=size[1] -1
whilej>=0:
ifflag:
ifpixels[i, j][3] ==0:
color=blackif (MASKS[mask](i, j, int(current<lengthandbin_output[current] =='1'))) elsewhite
pixels[i, j] =color
current+=1
ifpixels[i-1, j][3] ==0:
color=blackif (MASKS[mask](i-1, j, int(current<lengthandbin_output[current] =='1'))) elsewhite
pixels[i-1, j] =color
current+=1
else:
ifpixels[i, size[1] -1-j][3] ==0:
color=blackif (MASKS[mask](i, size[1] -1-j, int(current<lengthandbin_output[current] =='1'))) elsewhite
pixels[i, size[1] -1-j] =color
current+=1
ifpixels[i-1, size[1] -1-j][3] ==0:
color=blackif (MASKS[mask](i-1, size[1] -1-j, int(current<lengthandbin_output[current] =='1'))) elsewhite
pixels[i-1, size[1] -1-j] =color
current+=1
j-=1
i-=3if (i==8) else2
flag=notflag
defchoose_mask(image, correction_level, size, pixels, bin_output, white, black):
penalty_points= [0foriinrange(8)]
foriinrange(8):
current_image=image.copy()
current_pixels=current_image.load()
draw_info_codes(correction_level, i, size, current_pixels, white, black)
apply_data(i, size, current_pixels, bin_output, white, black)
forjinrange(size[1]):
current_streak=0
current_color=white
forkinrange(size[0]):
ifcurrent_pixels[k, j] ==current_color:
current_streak+=1
else:
ifcurrent_streak>=5:
penalty_points[i] +=max(0, current_streak-2)
current_streak=1
current_color=current_pixels[k, j]
ifcurrent_streak>=5:
penalty_points[i] +=max(0, current_streak-2)
forjinrange(size[0]):
current_streak=0
current_color=white
forkinrange(size[1]):
ifcurrent_pixels[j, k] ==current_color:
current_streak+=1
else:
ifcurrent_streak>=5:
penalty_points[i] +=max(0, current_streak-2)
current_streak=1
current_color=current_pixels[j, k]
ifcurrent_streak>=5:
penalty_points[i] +=max(0, current_streak-2)
forjinrange(size[1] -1):
forkinrange(size[0] -1):
ifcurrent_pixels[k,j] ==current_pixels[k+1,j] ==current_pixels[k,j+1] ==current_pixels[k+1,j+1]:
penalty_points[i] +=3
forjinrange(size[1]):
forkinrange(size[0] -6):
if [current_pixels[k,j], current_pixels[k+1,j], current_pixels[k+2,j], current_pixels[k+3,j], current_pixels[k+4,j],
current_pixels[k+5,j], current_pixels[k+6,j]] == [black, white, black, black, black, white, black]:
flag=False
ifk>3:
ifcurrent_pixels[k-4,j] ==current_pixels[k-3,j] ==current_pixels[k-2,j] ==current_pixels[k-1,j] ==white:
flag=True
ifk<size[0] -10:
ifcurrent_pixels[k+7,j] ==current_pixels[k+8,j] ==current_pixels[k+9,j] ==current_pixels[k+10,j] ==white:
flag=True
ifflag:
penalty_points[i] +=40
forjinrange(size[1]):
forkinrange(size[0] -6):
if [current_pixels[k,j], current_pixels[k+1,j], current_pixels[k+2,j], current_pixels[k+3,j], current_pixels[k+4,j],
current_pixels[k+5,j], current_pixels[k+6,j]] == [black, white, black, black, black, white, black]:
flag=False
ifk>3:
ifcurrent_pixels[k-4,j] ==current_pixels[k-3,j] ==current_pixels[k-2,j] ==current_pixels[k-1,j] ==white:
flag=True
ifk<size[1] -10:
ifcurrent_pixels[k+7,j] ==current_pixels[k+8,j] ==current_pixels[k+9,j] ==current_pixels[k+10,j] ==white:
flag=True
ifflag:
penalty_points[i] +=40
black_count=0
forjinrange(size[1]):
forkinrange(size[0]):
ifcurrent_pixels[k, j] ==black:
black_count+=1
penalty_points[i] +=2*abs(int(100*black_count/ (size[0] *size[1]) -50))
mask=penalty_points.index(min(penalty_points))
draw_info_codes(correction_level, mask, size, pixels, white, black)
apply_data(mask, size, pixels, bin_output, white, black)