From 45aa03fdcacabf07eb59c9f6df524e3052d1d122 Mon Sep 17 00:00:00 2001 From: favoritas37 Date: Sun, 18 Oct 2015 19:36:09 +0300 Subject: [PATCH] bug fixing in QR encoder. Still not functional --- source/qzxing.cpp | 8 ++++---- source/zxing/zxing/common/BitArray.cpp | 7 ++++++- source/zxing/zxing/common/BitArray.h | 1 + .../zxing/common/reedsolomon/GenericGFPoly.cpp | 4 ++-- .../zxing/common/reedsolomon/ReedSolomonEncoder.cpp | 6 +++++- source/zxing/zxing/qrcode/encoder/ByteMatrix.cpp | 2 +- source/zxing/zxing/qrcode/encoder/QREncoder.cpp | 13 +++++++------ 7 files changed, 26 insertions(+), 15 deletions(-) diff --git a/source/qzxing.cpp b/source/qzxing.cpp index 3685b11..1e6668f 100644 --- a/source/qzxing.cpp +++ b/source/qzxing.cpp @@ -291,10 +291,10 @@ QImage QZXing::encodeData(const QString& data) Ref barcode = qrcode::Encoder::encode(data, qrcode::ErrorCorrectionLevel::L ); Ref bytesRef = barcode->getMatrix(); const std::vector< std::vector >& bytes = bytesRef->getArray(); - QImage image; - for(int i=0; igetWidth(); ++i) - for(int j=0; jgetHeight(); ++i) - image.setPixel(i,j,bytes[i][j]); + QImage image(bytesRef->getWidth(), bytesRef->getHeight(), QImage::Format_ARGB32); + for(int i=0; igetWidth(); i++) + for(int j=0; jgetHeight(); j++) + image.setPixel(i,j,bytes[i][j] ? 0 : 255); return image; } diff --git a/source/zxing/zxing/common/BitArray.cpp b/source/zxing/zxing/common/BitArray.cpp index 703d1df..cb6b959 100644 --- a/source/zxing/zxing/common/BitArray.cpp +++ b/source/zxing/zxing/common/BitArray.cpp @@ -39,7 +39,12 @@ BitArray::~BitArray() { } int BitArray::getSize() const { - return bits->size(); + return size; +} + +int BitArray::getSizeInBytes() const +{ + return (size + 7)/8;\ } void BitArray::setBulk(int i, int newBits) { diff --git a/source/zxing/zxing/common/BitArray.h b/source/zxing/zxing/common/BitArray.h index 6b6b623..6281f3e 100644 --- a/source/zxing/zxing/common/BitArray.h +++ b/source/zxing/zxing/common/BitArray.h @@ -44,6 +44,7 @@ public: BitArray(int size); ~BitArray(); int getSize() const; + int getSizeInBytes() const; bool get(int i) const { return (bits[i >> logBits] & (1 << (i & bitsMask))) != 0; diff --git a/source/zxing/zxing/common/reedsolomon/GenericGFPoly.cpp b/source/zxing/zxing/common/reedsolomon/GenericGFPoly.cpp index a601fd3..a752f67 100644 --- a/source/zxing/zxing/common/reedsolomon/GenericGFPoly.cpp +++ b/source/zxing/zxing/common/reedsolomon/GenericGFPoly.cpp @@ -212,7 +212,7 @@ std::vector > GenericGFPoly::divide(Ref other) } std::vector > returnValue; - returnValue[0] = quotient; - returnValue[1] = remainder; + returnValue.push_back(quotient); + returnValue.push_back(remainder); return returnValue; } diff --git a/source/zxing/zxing/common/reedsolomon/ReedSolomonEncoder.cpp b/source/zxing/zxing/common/reedsolomon/ReedSolomonEncoder.cpp index d37f4d8..61c7fd3 100644 --- a/source/zxing/zxing/common/reedsolomon/ReedSolomonEncoder.cpp +++ b/source/zxing/zxing/common/reedsolomon/ReedSolomonEncoder.cpp @@ -44,7 +44,11 @@ void ReedSolomonEncoder::encode(std::vector &toEncode, int ecBytes) } Ref generator = buildGenerator(ecBytes); ArrayRef infoCoefficients(dataBytes); - memcpy(infoCoefficients.operator ->(), toEncode.data(), dataBytes); + //memcpy(infoCoefficients.operator ->(), toEncode.data(), dataBytes); + //to-do optimize the following loop + for(int i=0; i< dataBytes; ++i) + infoCoefficients[i] = toEncode[i]; + Ref info(new GenericGFPoly(field_, infoCoefficients)); info = info->multiplyByMonomial(ecBytes, 1); Ref remainder = info->divide(generator)[1]; diff --git a/source/zxing/zxing/qrcode/encoder/ByteMatrix.cpp b/source/zxing/zxing/qrcode/encoder/ByteMatrix.cpp index 72add93..062939f 100644 --- a/source/zxing/zxing/qrcode/encoder/ByteMatrix.cpp +++ b/source/zxing/zxing/qrcode/encoder/ByteMatrix.cpp @@ -8,7 +8,7 @@ ByteMatrix::ByteMatrix(size_t width, size_t height) : width_(width), height_(height) { bytes_.resize(height_); - for(size_t i=0; i Encoder::encode(const QString& content, ErrorCorrectionLevel &ecLeve BitArray headerAndDataBits; headerAndDataBits.appendBitArray(headerBits); // Find "length" of main segment and write it - int numLetters = (*mode == Mode::BYTE) ? dataBits.getSize() : content.length(); + int numLetters = (*mode == Mode::BYTE) ? dataBits.getSizeInBytes() : content.length(); appendLengthInfo(numLetters, *version, *mode, headerAndDataBits); // Put data together into the overall payload headerAndDataBits.appendBitArray(dataBits); @@ -256,14 +256,15 @@ void Encoder::terminateBits(int numDataBytes, BitArray& bits) } // Append termination bits. See 8.4.8 of JISX0510:2004 (p.24) for details. // If the last byte isn't 8-bit aligned, we'll add padding bits. - int numBitsInLastByte = bits.getSize() & 0x07; + int numBitsInLastByte = bits.getSize() % 8; if (numBitsInLastByte > 0) { for (int i = numBitsInLastByte; i < 8; i++) { bits.appendBit(false); } } // If we have more space, we'll fill the space with padding patterns defined in 8.4.9 (p.24). - int numPaddingBytes = numDataBytes - bits.getSize();//bits.getSizeInBytes(); + int bitSizeInBytes = bits.getSizeInBytes(); + int numPaddingBytes = numDataBytes - bitSizeInBytes; for (int i = 0; i < numPaddingBytes; ++i) { bits.appendBits((i & 0x01) == 0 ? 0xEC : 0x11, 8); } @@ -347,7 +348,7 @@ BitArray* Encoder::interleaveWithECBytes(const BitArray& bits, { // "bits" must have "getNumDataBytes" bytes of data. - if (bits.getSize() != numDataBytes) + if (bits.getSizeInBytes() != numDataBytes) throw new WriterException("Number of bits and data bytes does not match"); // Step 1. Divide data bytes into blocks and generate error correction bytes for them. We'll @@ -401,11 +402,11 @@ BitArray* Encoder::interleaveWithECBytes(const BitArray& bits, } } } - if (numTotalBytes != result->getSize()) { // Should be same. + if (numTotalBytes != result->getSizeInBytes()) { // Should be same. QString message("Interleaving error: "); message += QString::number(numTotalBytes); message += " and "; - message += QString(result->getSize()); + message += QString(result->getSizeInBytes()); message += " differ."; throw new WriterException(message.toStdString().c_str()); }