summaryrefslogtreecommitdiffstats
path: root/camera.cpp
blob: 879cad6dc9d0f7ca48f76da30c4b1afb6fa85f64 (plain)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <iostream>
#include <fstream>
#include <cassert>
#include <vector>

void print(std::vector<bool> &vec);
void print(std::vector<std::vector<bool>> &symbol);
void print_codewords(std::vector<bool> &vec);
void vec_xor(std::vector<bool> &v1, const std::vector<bool> v2);
unsigned int distance(std::vector<bool> &v1, const std::vector<bool> &v2);
unsigned int convert_uint(std::vector<bool> &vec);
std::string add_preceding_zeros(unsigned int n);
void read_from_file(std::string filename, std::vector<std::vector<bool>> &symbol);
void retrieve_codewords(std::vector<std::vector<bool>> &symbol, std::vector<bool> &codewords, bool is_upwards, std::size_t row, std::size_t col);
void retrieve_special_codewords(std::vector<std::vector<bool>> &symbol, std::vector<bool> &codewords, bool is_upwards, int row, int col);

std::vector<bool> get_format_info(std::vector<std::vector<bool>> &symbol);
void decode_mask(std::vector<std::vector<bool>> &symbol, std::size_t mask_pattern);
std::vector<bool> get_codewords_from_symbol(std::vector<std::vector<bool>> &symbol);
std::string get_input_data(std::vector<bool> &input_data, unsigned int character_count_indicator);
std::string decode_symbol(std::vector<std::vector<bool>> &symbol);

int main(){
	std::vector<std::string> test_cases = {"012345678", "876543210", "000000000", "999999999", "123456789", "873703846", "884158624", "827515735", "869871935", "804235824"};
	unsigned int test_counter = 1;
	for (auto test : test_cases){
		std::vector<std::vector<bool>> symbol(21, std::vector<bool>(21,0));
		read_from_file(test + ".pbm", symbol);
		print(symbol);
		std::cout << "Test: " << test_counter << std::endl;
		std::string result = decode_symbol(symbol);
		std::cout << "Test Passed: " << (result == test) << std::endl;
		std::cout << "Test:   " << test << std::endl;
		std::cout << "Result: " << result << std::endl;
		test_counter++;
	}
	return 0;
}


void print(std::vector<std::vector<bool>> &symbol){
	for (const auto &i : symbol){
		for (auto j : i){
			std::cout << j;
	      }
		std::cout << std::endl;
	}
	std::cout << std::endl;
}

void print(std::vector<bool> &vec){
	for (auto i : vec){
		std::cout << i;
	}
	std::cout << std::endl;
}

void print_codewords(std::vector<bool> &vec){
	for (std::size_t i{0}; i < vec.size(); ++i){
		if (i != 0 && i % 8 == 0){std::cout << ' ';}
		std::cout << vec[i];
	}
	std::cout << std::endl;
}

void vec_xor(std::vector<bool> &v1, const std::vector<bool> v2){
	for (std::size_t i{0}; i < v1.size(); ++i){
		v1[i] = v1[i] ^ v2[i];
	}
}

unsigned int distance(std::vector<bool> &v1, const std::vector<bool> &v2){
	unsigned int dist{0};
	for (std::size_t i{0}; i < v1.size(); ++i){
		if (v1[i] != v2[i]){
			++dist;
		}

		if (dist > 3){
			return dist;
		}
	}
	return dist;
}
unsigned int convert_uint(std::vector<bool> &vec){
	unsigned int result{0};
	for (unsigned int i{0}; i < vec.size(); ++i){
		result += std::pow(2,vec.size() - i - 1) * vec[i];
	}
	return result;
}

std::string add_preceding_zeros(unsigned int n){
	std::string result = std::to_string(n);
	if (result.size() == 1){
		result.insert(0, "0");
		result.insert(0, "0");
	}
	else if (result.size() == 2){
		result.insert(0, "0");
	}
	return result;
}
	

std::vector<bool> get_format_info(std::vector<std::vector<bool>> &symbol){
	std::vector<bool> format_info(15,0);
	for (std::size_t row{0}, counter{14}; row < 21; ++row){
		if (row == 6 || (row >= 9 && row <= 13)){
			continue;
		}
		format_info[counter] = (symbol[row][8]);
		--counter;
	}

	std::vector<std::vector<bool>> valid_bit_format_info = {
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,1,0,1,0,0,1,1,0,1,1,1},
		{0,0,0,1,0,1,0,0,1,1,0,1,1,1,0},
		{0,0,0,1,1,1,1,0,1,0,1,1,0,0,1},
		{0,0,1,0,0,0,1,1,1,1,0,1,0,1,1},
		{0,0,1,0,1,0,0,1,1,0,1,1,1,0,0},
		{0,0,1,1,0,1,1,1,0,0,0,0,1,0,1},
		{0,0,1,1,1,1,0,1,0,1,1,0,0,1,0},
		{0,1,0,0,0,1,1,1,1,0,1,0,1,1,0},
		{0,1,0,0,1,1,0,1,1,1,0,0,0,0,1},
		{0,1,0,1,0,0,1,1,0,1,1,1,0,0,0},
		{0,1,0,1,1,0,0,1,0,0,0,1,1,1,1},
		{0,1,1,0,0,1,0,0,0,1,1,1,1,0,1},
		{0,1,1,0,1,1,1,0,0,0,0,1,0,1,0},
		{0,1,1,1,0,0,0,0,1,0,1,0,0,1,1},
		{0,1,1,1,1,0,1,0,1,1,0,0,1,0,0},
		{1,0,0,0,0,1,0,1,0,0,1,1,0,1,1},
		{1,0,0,0,1,1,1,1,0,1,0,1,1,0,0},
		{1,0,0,1,0,0,0,1,1,1,1,0,1,0,1},
		{1,0,0,1,1,0,1,1,1,0,0,0,0,1,0},
		{1,0,1,0,0,1,1,0,1,1,1,0,0,0,0},
		{1,0,1,0,1,1,0,0,1,0,0,0,1,1,1},
		{1,0,1,1,0,0,1,0,0,0,1,1,1,1,0},
		{1,0,1,1,1,0,0,0,0,1,0,1,0,0,1},
		{1,1,0,0,0,0,1,0,1,0,0,1,1,0,1},
		{1,1,0,0,1,0,0,0,1,1,1,1,0,1,0},
		{1,1,0,1,0,1,1,0,0,1,0,0,0,1,1},
		{1,1,0,1,1,1,0,0,0,0,1,0,1,0,0},
		{1,1,1,0,0,0,0,1,0,1,0,0,1,1,0},
		{1,1,1,0,1,0,1,1,0,0,1,0,0,0,1},
		{1,1,1,1,0,1,0,1,1,0,0,1,0,0,0},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
	};
	//print(format_info);	
	std::vector<bool> mask = {1,0,1,0,1,0,0,0,0,0,1,0,0,1,0};
	vec_xor(format_info, mask);

	unsigned int min_dist = distance(format_info, valid_bit_format_info[0]);
	std::size_t min_dist_index{0};
	for (std::size_t i{0}; i < valid_bit_format_info.size(); ++i){
		unsigned int curr_dist = distance(format_info, valid_bit_format_info[i]);
		if (curr_dist <= min_dist){
			min_dist = curr_dist;
			min_dist_index = i;
			if (curr_dist == 0){
				break;
			}
		}
	}
	//std::cout << min_dist << " " << std::endl; 
	if (min_dist <= 3){
		format_info = valid_bit_format_info[min_dist_index];
	}
	else{
		throw std::runtime_error("format information incorrectly scanned!");
	}

	return format_info;
}

void decode_mask(std::vector<std::vector<bool>> &symbol, std::size_t mask_pattern){
	std::vector<std::vector<bool>> data_mask(21, std::vector<bool> (21, 0));
	for (std::size_t i{}; i < 21; ++i){
		for (std::size_t j{}; j < 21; ++j){
			if ((i < 9 && j < 9) || (i > 12 && j < 9) || (i < 9 && j > 12) || i == 6 || j == 6){
				continue;
			}
			// 000
			if (mask_pattern == 0 && (i+j)%2 == 0){
				data_mask[i][j] = 1;
			}
			// 001
			else if (mask_pattern == 1 && i % 2 == 0){
				data_mask[i][j] = 1;
			}
			// 010
			else if (mask_pattern == 2 && j % 3 == 0){
				data_mask[i][j] = 1;
			}
			// 011
			else if (mask_pattern == 3 && (i+j)%3 == 0){
				data_mask[i][j] = 1;
			}
			// 100
			else if (mask_pattern == 4 && (i / 2 + j / 3) % 2 == 0){
				data_mask[i][j] = 1;
			}
			// 101
			else if (mask_pattern == 5 && (i * j) % 2 + (i * j) % 3 == 0){
				data_mask[i][j] = 1;
			}	
			// 110
			else if (mask_pattern == 6 && ((i * j) % 2 + (i * j) % 3 ) % 2 == 0){
				data_mask[i][j] = 1;
			}	
			// 111
			else if (mask_pattern == 7 && (((i + j) % 2 + (i * j) % 3 ) % 2 == 0)){
				data_mask[i][j] = 1;
			}	
		}
	}	 
	
	for (std::size_t i{0}; i < 21; ++i){
		for (std::size_t j{0}; j < 21; ++j){
			symbol[i][j] = symbol[i][j] ^ data_mask[i][j];
		}
	}
}

void retrieve_codewords(std::vector<std::vector<bool>> &symbol, std::vector<bool> &codewords, bool is_upwards, int row, int col){
	if (is_upwards){
		for (int r{row + 3}; r >= row; --r){
			for (int c{col + 1}; c >= col; --c){
				//std::cout << r << ' ' << c << std::endl;
				codewords.push_back(symbol[r][c]);
			}
		}
	}
	else {
		for (int r{row}; r <= row + 3; ++r){
			for (int c{col + 1}; c >= col; --c){
				//std::cout << r << ' ' << c << std::endl;
				codewords.push_back(symbol[r][c]);
			}
		}
	}
}

void retrieve_special_codewords(std::vector<std::vector<bool>> &symbol, std::vector<bool> &codewords, bool is_upwards, int row, int col){
	if (is_upwards){
		for (int r{row + 4}; r >= row; --r){
			for (int c{col + 1}; c >= col; --c){
				if (r == 6){continue;}
				//std::cout << r << ' ' << c << std::endl;
				codewords.push_back(symbol[r][c]);
			}
		}
	}
	else {
		for (int r{row}; r <= row + 4; ++r){
			for (int c{col + 1}; c >= col; --c){
				if (r == 6){continue;}
				//std::cout << r << ' ' << c << std::endl;
				codewords.push_back(symbol[r][c]);
			}
		}
	}
}

std::vector<bool> get_codewords_from_symbol(std::vector<std::vector<bool>> &symbol){
	std::vector<bool> codewords{};
	int c{}, r{};
	// refer to codewords by their top left corner
	
	for (c = 19, r = 17;c >= 13; c-=2){
		for (r = 17;r >= 21 - 3 * 4;r-=4){
			retrieve_codewords(symbol, codewords, true, r, c);
		}
		c -= 2;
		for (r = 9;r < 20; r+=4){
			retrieve_codewords(symbol, codewords, false, r, c);
		}
	}

	for (r = 17;r >= 21 - 3 * 4;r-=4){
		retrieve_codewords(symbol, codewords, true, r, c);
	}
	//std::cout << r << ' ' << c << std::endl;
	// up special 
	r = 4;
	retrieve_special_codewords(symbol, codewords, true, r, c);
	
	r = 0;
	retrieve_codewords(symbol, codewords, true, r, c);
	c-=2;
	retrieve_codewords(symbol, codewords, false, r, c);

	// down special
	r = 4;
	retrieve_special_codewords(symbol, codewords, false, r, c);

	for (r=9;r < 20;r+=4){
		retrieve_codewords(symbol, codewords, false, r, c);
	}

	// data that is between two finder patterns before timing pattern
	c -= 2;
	r = 9;
	retrieve_codewords(symbol, codewords, true, r, c);
	
	// data after timing pattern
	c -= 3;
	retrieve_codewords(symbol, codewords, false, r, c);

	c -= 2;
	retrieve_codewords(symbol, codewords, true, r, c);
	
	c -= 2;
	retrieve_codewords(symbol, codewords, false, r, c);
	return codewords;
}

std::string get_input_data(std::vector<bool> &input_data, unsigned int character_count_indicator){ std::string data = "";
	unsigned int data_length = character_count_indicator / 3 * 10;
	for (std::size_t i{0}; i < data_length; i += 10){
		std::vector<bool> subvec(input_data.begin() + i, input_data.begin() + i + 10);
		data += add_preceding_zeros(convert_uint(subvec));
	}
	if (character_count_indicator % 3 == 1){
		std::vector<bool> subvec(input_data.begin() + data_length, input_data.begin() + data_length + 4);
		data += std::to_string(convert_uint(subvec));
	}
	else if (character_count_indicator % 3 == 2){
		std::vector<bool> subvec(input_data.begin() + data_length, input_data.begin() + data_length + 7);
		data += std::to_string(convert_uint(subvec));
	}
	return data;
}
		

std::string decode_symbol(std::vector<std::vector<bool>> &symbol){
	std::vector<bool> format_info = get_format_info(symbol);

	//std::cout << "Format Info: "; print(format_info); std::cout << std::endl;
	std::bitset<3> mask_pattern{};	
	mask_pattern[0] = format_info[2];
	mask_pattern[1] = format_info[3];
	mask_pattern[2] = format_info[4];
	decode_mask(symbol, mask_pattern.to_ulong());
	//print(symbol);

	std::vector<bool> codewords = get_codewords_from_symbol(symbol);
	//print_codewords(codewords);
	//std::cout << "Number of codewords: " << codewords.size() / 8.0 << std::endl;
	std::vector<bool> data_codewords(codewords.begin(), codewords.begin() + 16 * 8), error_correction_codewords(codewords.begin() + 16 * 8, codewords.end());

	//std::cout << "Number of data codewords: " << data_codewords.size() / 8.0 << std::endl;
	//std::cout << "Number of error correction codewords: " << error_correction_codewords.size() / 8.0 << std::endl;
	//print_codewords(data_codewords);
	std::vector<bool> mode_indicator(data_codewords.begin(), data_codewords.begin() + 4);
	std::vector<bool> character_count(data_codewords.begin() + 4, data_codewords.begin() + 14);
	unsigned int character_count_indicator = convert_uint(character_count);
	//std::cout << character_count_indicator << std::endl;	
	std::vector<bool> input_data(data_codewords.begin() + 14, data_codewords.end());

	return get_input_data(input_data, character_count_indicator);
}


void read_from_file(std::string filename, std::vector<std::vector<bool>> &symbol){
	std::ifstream file{ filename };
	if (!file.is_open()) {
	std::cout << "[ERROR] " << filename << " not found or could not open file" << std::endl;
	}
	assert( file.is_open() );
	
	file.ignore(9);
	std::size_t row{0};
	for (std::string line; std::getline(file, line);++row){
		for (std::size_t pos{0}; pos < line.size(); pos+=2){
			symbol[row][pos/2] = (line[pos] == '1');
		}
	}
	file.close();
}