* Encodes the given binary content as an Aztec symbol * * @param data input data string * @param minECCPercent minimal percentage of error check words (According to ISO/IEC 24778:2008, * a minimum of 23% + 3 words is recommended) * @param userSpecifiedLayers if non-zero, a user-specified value for the number of layers * @return Aztec symbol matrix with metadata */
| 163 | * @return Aztec symbol matrix with metadata |
| 164 | */ |
| 165 | EncodeResult |
| 166 | Encoder::Encode(const std::string& data, int minECCPercent, int userSpecifiedLayers) |
| 167 | { |
| 168 | // High-level encode |
| 169 | BitArray bits = HighLevelEncoder::Encode(data); |
| 170 | |
| 171 | // stuff bits and choose symbol size |
| 172 | int eccBits = bits.size() * minECCPercent / 100 + 11; |
| 173 | int totalSizeBits = bits.size() + eccBits; |
| 174 | bool compact; |
| 175 | int layers; |
| 176 | int totalBitsInLayer; |
| 177 | int wordSize; |
| 178 | BitArray stuffedBits; |
| 179 | if (userSpecifiedLayers != DEFAULT_AZTEC_LAYERS) { |
| 180 | compact = userSpecifiedLayers < 0; |
| 181 | layers = std::abs(userSpecifiedLayers); |
| 182 | if (layers > (compact ? MAX_NB_BITS_COMPACT : MAX_NB_BITS)) { |
| 183 | throw std::invalid_argument("Illegal value for layers: " + std::to_string(userSpecifiedLayers)); |
| 184 | } |
| 185 | totalBitsInLayer = TotalBitsInLayer(layers, compact); |
| 186 | wordSize = WORD_SIZE[layers]; |
| 187 | int usableBitsInLayers = totalBitsInLayer - (totalBitsInLayer % wordSize); |
| 188 | StuffBits(bits, wordSize, stuffedBits); |
| 189 | if (stuffedBits.size() + eccBits > usableBitsInLayers) { |
| 190 | throw std::invalid_argument("Data to large for user specified layer"); |
| 191 | } |
| 192 | if (compact && stuffedBits.size() > wordSize * 64) { |
| 193 | // Compact format only allows 64 data words, though C4 can hold more words than that |
| 194 | throw std::invalid_argument("Data to large for user specified layer"); |
| 195 | } |
| 196 | } |
| 197 | else { |
| 198 | wordSize = 0; |
| 199 | // We look at the possible table sizes in the order Compact1, Compact2, Compact3, |
| 200 | // Compact4, Normal4,... Normal(i) for i < 4 isn't typically used since Compact(i+1) |
| 201 | // is the same size, but has more data. |
| 202 | for (int i = 0; ; i++) { |
| 203 | if (i > MAX_NB_BITS) { |
| 204 | throw std::invalid_argument("Data too large for an Aztec code"); |
| 205 | } |
| 206 | compact = i <= 3; |
| 207 | layers = compact ? i + 1 : i; |
| 208 | totalBitsInLayer = TotalBitsInLayer(layers, compact); |
| 209 | if (totalSizeBits > totalBitsInLayer) { |
| 210 | continue; |
| 211 | } |
| 212 | // [Re]stuff the bits if this is the first opportunity, or if the |
| 213 | // wordSize has changed |
| 214 | if (wordSize != WORD_SIZE[layers]) { |
| 215 | wordSize = WORD_SIZE[layers]; |
| 216 | StuffBits(bits, wordSize, stuffedBits); |
| 217 | } |
| 218 | int usableBitsInLayers = totalBitsInLayer - (totalBitsInLayer % wordSize); |
| 219 | if (compact && stuffedBits.size() > wordSize * 64) { |
| 220 | // Compact format only allows 64 data words, though C4 can hold more words than that |
| 221 | continue; |
| 222 | } |
nothing calls this directly
no test coverage detected