(matrix: BitMatrix)
| 188 | } |
| 189 | |
| 190 | function readFormatInformation(matrix: BitMatrix) { |
| 191 | let topLeftFormatInfoBits = 0; |
| 192 | for (let x = 0; x <= 8; x++) { |
| 193 | if (x !== 6) { // Skip timing pattern bit |
| 194 | topLeftFormatInfoBits = pushBit(matrix.get(x, 8), topLeftFormatInfoBits); |
| 195 | } |
| 196 | } |
| 197 | for (let y = 7; y >= 0; y--) { |
| 198 | if (y !== 6) { // Skip timing pattern bit |
| 199 | topLeftFormatInfoBits = pushBit(matrix.get(8, y), topLeftFormatInfoBits); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | const dimension = matrix.height; |
| 204 | let topRightBottomRightFormatInfoBits = 0; |
| 205 | for (let y = dimension - 1; y >= dimension - 7; y--) { // bottom left |
| 206 | topRightBottomRightFormatInfoBits = pushBit(matrix.get(8, y), topRightBottomRightFormatInfoBits); |
| 207 | } |
| 208 | for (let x = dimension - 8; x < dimension; x++) { // top right |
| 209 | topRightBottomRightFormatInfoBits = pushBit(matrix.get(x, 8), topRightBottomRightFormatInfoBits); |
| 210 | } |
| 211 | |
| 212 | let bestDifference = Infinity; |
| 213 | let bestFormatInfo = null; |
| 214 | for (const {bits, formatInfo} of FORMAT_INFO_TABLE) { |
| 215 | if (bits === topLeftFormatInfoBits || bits === topRightBottomRightFormatInfoBits) { |
| 216 | return formatInfo; |
| 217 | } |
| 218 | let difference = numBitsDiffering(topLeftFormatInfoBits, bits); |
| 219 | if (difference < bestDifference) { |
| 220 | bestFormatInfo = formatInfo; |
| 221 | bestDifference = difference; |
| 222 | } |
| 223 | if (topLeftFormatInfoBits !== topRightBottomRightFormatInfoBits) { // also try the other option |
| 224 | difference = numBitsDiffering(topRightBottomRightFormatInfoBits, bits); |
| 225 | if (difference < bestDifference) { |
| 226 | bestFormatInfo = formatInfo; |
| 227 | bestDifference = difference; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | // Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits differing means we found a match |
| 232 | if (bestDifference <= 3) { |
| 233 | return bestFormatInfo; |
| 234 | } |
| 235 | return null; |
| 236 | } |
| 237 | |
| 238 | function getDataBlocks(codewords: number[], version: Version, ecLevel: number) { |
| 239 | const ecInfo = version.errorCorrectionLevels[ecLevel]; |
no test coverage detected
searching dependent graphs…