| 62 | } |
| 63 | |
| 64 | Ref<DecoderResult> Decoder::decode(Ref<BitMatrix> bits) { |
| 65 | // Construct a parser and read version, error-correction level |
| 66 | BitMatrixParser parser(bits); |
| 67 | |
| 68 | // std::cerr << *bits << std::endl; |
| 69 | |
| 70 | Version *version = parser.readVersion(); |
| 71 | ErrorCorrectionLevel &ecLevel = parser.readFormatInformation()->getErrorCorrectionLevel(); |
| 72 | |
| 73 | |
| 74 | // Read codewords |
| 75 | ArrayRef<char> codewords(parser.readCodewords()); |
| 76 | |
| 77 | |
| 78 | // Separate into data blocks |
| 79 | std::vector<Ref<DataBlock> > dataBlocks(DataBlock::getDataBlocks(codewords, version, ecLevel)); |
| 80 | |
| 81 | |
| 82 | // Count total number of data bytes |
| 83 | int totalBytes = 0; |
| 84 | for (size_t i = 0; i < dataBlocks.size(); i++) { |
| 85 | totalBytes += dataBlocks[i]->getNumDataCodewords(); |
| 86 | } |
| 87 | ArrayRef<char> resultBytes(totalBytes); |
| 88 | int resultOffset = 0; |
| 89 | |
| 90 | |
| 91 | // Error-correct and copy data blocks together into a stream of bytes |
| 92 | for (size_t j = 0; j < dataBlocks.size(); j++) { |
| 93 | Ref<DataBlock> dataBlock(dataBlocks[j]); |
| 94 | ArrayRef<char> codewordBytes = dataBlock->getCodewords(); |
| 95 | int numDataCodewords = dataBlock->getNumDataCodewords(); |
| 96 | correctErrors(codewordBytes, numDataCodewords); |
| 97 | for (int i = 0; i < numDataCodewords; i++) { |
| 98 | resultBytes[resultOffset++] = codewordBytes[i]; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return DecodedBitStreamParser::decode(resultBytes, |
| 103 | version, |
| 104 | ecLevel, |
| 105 | DecodedBitStreamParser::Hashtable()); |
| 106 | } |
| 107 |
no test coverage detected