-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
163 lines (143 loc) · 6.43 KB
/
common.h
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
/*
QRMatrix - QR pixels presentation.
Copyright © 2023 duongpq/soleilpqd.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifndef COMMON_H
#define COMMON_H
#include "constants.h"
#include <iostream>
namespace QRMatrix {
struct ErrorCorrectionInfo {
UnsignedByte version;
ErrorCorrectionLevel level;
Unsigned2Bytes codewords;
Unsigned2Bytes ecCodewordsPerBlock;
Unsigned2Bytes group1Blocks;
Unsigned2Bytes group1BlockCodewords;
Unsigned2Bytes group2Blocks;
Unsigned2Bytes group2BlockCodewords;
inline ErrorCorrectionInfo() {
version = 0;
level = ErrorCorrectionLevel::low;
codewords = 0;
ecCodewordsPerBlock = 0;
group1Blocks = 0;
group1BlockCodewords = 0;
group2Blocks = 0;
group2BlockCodewords = 0;
}
inline ErrorCorrectionInfo(UnsignedByte ver, ErrorCorrectionLevel lev, const Unsigned2Bytes data[6]) {
version = ver;
level = lev;
codewords = data[0];
ecCodewordsPerBlock = data[1];
group1Blocks = data[2];
group1BlockCodewords = data[3];
group2Blocks = data[4];
group2BlockCodewords = data[5];
}
/// Number of bytes for QR data.
/// @ref: https://www.thonky.com/qr-code-tutorial/error-correction-table.
static ErrorCorrectionInfo errorCorrectionInfo(
UnsignedByte version,
ErrorCorrectionLevel level
);
static ErrorCorrectionInfo microErrorCorrectionInfo(
UnsignedByte version,
ErrorCorrectionLevel level
);
inline UnsignedByte groupCount() { return (group2Blocks == 0 ? 1 : 2); }
inline UnsignedByte ecBlockTotalCount() { return group1Blocks + group2Blocks; }
inline unsigned int ecCodewordsTotalCount() { return ecBlockTotalCount() * ecCodewordsPerBlock; }
};
/// Common functions
class Common {
public:
/// Current environment
static bool isLittleEndian;
/// Get QR dimension by its version (version in 1...40 ~ dimension 21...177).
static inline UnsignedByte dimensionByVersion(UnsignedByte version) { return (version - 1) * QR_VERSION_OFFSET + QR_MIN_DIMENSION; }
/// Get MicroQR dimension by its version (version in 1...4 ~ dimension 11...17).
static inline UnsignedByte microDimensionByVersion(UnsignedByte version) { return (version - 1) * MICROQR_VERSION_OFFSET + MICROQR_MIN_DIMENSION; }
/// Number of bits for character counts indicator.
static unsigned int charactersCountIndicatorLength(
UnsignedByte version,
EncodingMode mode
);
/// Number of bits of character counts indicator for MicroQR.
static unsigned int microCharactersCountIndicatorLength(
UnsignedByte version,
EncodingMode mode
);
/// Number of bits of mode indicator for MicroQR.
static unsigned int microModeIndicatorLength(
UnsignedByte version,
EncodingMode mode
);
/// Number of bits of terminator for MicroQR.
static unsigned int microTerminatorLength(
UnsignedByte version
);
/// Map Encoding mode value to MicroQR
static UnsignedByte microQREncodingModeValue(
EncodingMode mode
);
/// Map ErrorCorrectionLevel value to MicroQR
static UnsignedByte microQRErrorCorrectionLevelValue(
ErrorCorrectionLevel level,
UnsignedByte version
);
/// Allocate `count` of bytes (and reset to 0)
static UnsignedByte* allocate(unsigned int count);
/// Copy `count` bits of source BYTE (from bit `sourceStartIndex`) into `destination` BYTE stating from bit at `destStartIndex`.
/// Bits index is from left to right.
/// Bit index must be 0...7 (because this copies 1 byte only).
/// `count` must be 1...8 (bits - size of `source`), also `count` must be < 8 - destStartIndex && 8 - sourceStartIndex.
static void copyBits(
/// Source Byte.
UnsignedByte sourceByte,
/// Source starting bit.
unsigned int sourceStartIndex,
/// Destination memory allocation.
UnsignedByte* destination,
/// Destination starting bit.
unsigned int destStartIndex,
/// Number of bits to be copied.
unsigned int count
);
/// Copy `count` bits of source (from bit 0th) into `destination` stating from bit at `startIndex`.
static void copyBits(
/// Source.
UnsignedByte* source,
/// Source length in byte.
unsigned int sourceLength,
/// Source starting bit index
unsigned int sourceStartIndex,
/// Source order (little/big endian).
bool isSourceOrderReversed,
/// Destination (bytes array - not care about order here).
UnsignedByte* destination,
/// Destintation starting bit index.
unsigned int destStartIndex,
/// Number of bits to be copied.
unsigned int count
);
/// Return array of 6 items contains QR aligment locations for version 2...40
static const UnsignedByte* alignmentLocations(UnsignedByte version);
};
}
#endif // COMMON_H