-
Notifications
You must be signed in to change notification settings - Fork 0
/
Printer.cpp
117 lines (110 loc) · 3.93 KB
/
Printer.cpp
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
#include "Printer.h"
Printer::Printer(qrInfo* inf) : info(inf){}
void Printer::printAllPatterns(QR_Field& qr_field)
{
// initialize qr_field
for (int i = 0; i < info->size; i++)
qr_field.push_back(std::vector<QR_Module>(info->size, QR_Module()));
// Print finder pattern (Large squares)
int c = 0;
for (int i = 0; i < 7; i++){
c = 0;
for (Byte p = 0x1; c != 7; p <<= 1, c++){
qr_field[i][c].write_lock(finder_dat[i] & p, ModType::M_FINDER);
qr_field[i][c + info->size - 7].write_lock(finder_dat[i] & p, ModType::M_FINDER);
qr_field[i + info->size - 7][c].write_lock(finder_dat[i] & p, ModType::M_FINDER);
}
}
c = 0;
int d = 0;
// Print Alignment Pattern
unsigned int x, y;
if (info->version > 0){
auto posVector = alignmentDat[info->version - 1];
for (int i = 0; i < posVector.size(); i++)
for (int j = 0; j < posVector.size(); j++){
x = posVector[i];
y = posVector[j];
//std::cout << x << ' ' << y << std::endl;
d = 0;
if (qr_field[x][y].type == M_VOID)
for (auto& p : align_dat){
c = 0;
for (Byte b = static_cast<Byte>(0x1); c != 5; b <<= 1, c++){
qr_field[x - 2 + c][y - 2 + d].write_lock(static_cast<bool>(p & b), ModType::M_ALIGN);
}
d++;
}
}
}
// Print Timing Pattern
bool tim = false;
for (int i = 0; i < info->version * 4 + 21; i++){
tim = !tim;
if (qr_field[6][i].type == M_VOID) qr_field[6][i].write_lock(tim, M_TIMING);
if (qr_field[i][6].type == M_VOID) qr_field[i][6].write_lock(tim, M_TIMING);
}
// Reserve Spaces
// 0: Dark Module
qr_field[8][4 * (info->version + 1) + 9].write_lock(true, M_DARK_MODULE);
// 1: Separators
for (int i = 0; i < 8; i++){
qr_field[7][i].write_lock(false, M_SEP);
qr_field[info->size - 8][i].write_lock(false, M_SEP);
qr_field[i][7].write_lock(false, M_SEP);
qr_field[i][info->size - 8].write_lock(false, M_SEP);
qr_field[i + info->size - 8][7].write_lock(false, M_SEP);
qr_field[7][i + info->size - 8].write_lock(false, M_SEP);
}
// 2: Format information
if (info->version + 1 >= 7){
for (int i = 0; i < 3; i++){
for (int j = 0; j < 6; j++){
qr_field[info->size - 11 + i][j].write_lock(false, M_FORMAT);
qr_field[j][info->size - 11 + i].write_lock(false, M_FORMAT);
}
}
}
for (int i = 0; i < 9; i++){
qr_field[8][i].write_lock(false, M_FORMAT);
qr_field[i][8].write_lock(false, M_FORMAT);
}
for (int i = 0; i < 8; i++){
qr_field[i + info->size - 8][8].write_lock(false, M_FORMAT);
qr_field[8][i + info->size - 8].write_lock(false, M_FORMAT);
}
}
void Printer::printFinal(sf::Image& qrCode, QR_Field& qr_field)
{
// Create image + Quiet Zone
qrCode.create(info->size + 8, info->size + 8, sf::Color::White);
for (int i = 0; i < info->size; i++)
for (int j = 0; j < info->size; j++){
qrCode.setPixel(i + 4, j + 4, qr_field[i][j].state() ?
sf::Color::Black : sf::Color::White);
}
}
void Printer::printDebugFinal(sf::Image& qrCode, QR_Field& qr_field)
{
// Create image + Quiet Zone
sf::Color tempCol;
qrCode.create(info->size + 8, info->size + 8, sf::Color(100, 100, 100));
for (int i = 0; i < info->size; i++){
for (int j = 0; j < info->size; j++){
qrCode.setPixel(i + 4, j + 4, qr_field[i][j].state() ?
sf::Color::Black : sf::Color::White);
tempCol = qrCode.getPixel(i + 4, j + 4);
switch (qr_field[i][j].type){
case M_SEP:
case M_FINDER: tempCol = tempCol.r == 0 ? sf::Color(0, 50, 0) : sf::Color(0, 200, 0); break;
case M_TIMING: tempCol = tempCol.r == 0 ? sf::Color(0, 0, 100) : sf::Color(130, 130, 200); break;
case M_ALIGN: tempCol = tempCol.r == 0 ? sf::Color(30, 0, 0) : sf::Color(200, 0, 0); break;
case M_DATA: tempCol = tempCol.r == 0 ? sf::Color(100, 0, 0) : sf::Color(200, 100, 100); break;
case M_FORMAT: tempCol = tempCol.r == 0 ? sf::Color::Black : sf::Color::White; break;
case M_DARK_MODULE: tempCol = sf::Color::Blue; break;
default: tempCol = sf::Color(50, 50, 50); break;
}
qrCode.setPixel(i + 4, j + 4, tempCol);
}
}
}