File indexing completed on 2024-05-12 15:49:08
0001 /* 0002 SPDX-FileCopyrightText: 2010-2016 Sune Vuorela <sune@vuorela.dk> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include "qrcodebarcode.h" 0008 #include <qrencode.h> 0009 0010 #include <memory> 0011 0012 using namespace Prison; 0013 0014 using QRcode_ptr = std::unique_ptr<QRcode, decltype(&QRcode_free)>; 0015 using QRinput_ptr = std::unique_ptr<QRinput, decltype(&QRinput_free)>; 0016 0017 QRCodeBarcode::QRCodeBarcode() 0018 : AbstractBarcode(AbstractBarcode::TwoDimensions) 0019 { 0020 } 0021 QRCodeBarcode::~QRCodeBarcode() = default; 0022 0023 static void qrEncodeString(QRcode_ptr &code, const QByteArray &data) 0024 { 0025 // try decreasing ECC levels, in case the higher levels result in overflowing the maximum content size 0026 for (auto ecc : {QR_ECLEVEL_Q, QR_ECLEVEL_M, QR_ECLEVEL_L}) { 0027 code.reset(QRcode_encodeString(data.constData(), 0, ecc, QR_MODE_8, true)); 0028 if (code) { 0029 break; 0030 } 0031 } 0032 } 0033 0034 QImage QRCodeBarcode::paintImage(const QSizeF &size) 0035 { 0036 Q_UNUSED(size); 0037 0038 QRcode_ptr code(nullptr, &QRcode_free); 0039 QRinput_ptr input(nullptr, &QRinput_free); 0040 if (!data().isEmpty()) { 0041 const QByteArray trimmedData(data().trimmed().toUtf8()); 0042 qrEncodeString(code, trimmedData); 0043 } else { 0044 const auto b = byteArrayData(); 0045 const auto isReallyBinary = std::any_of(b.begin(), b.end(), [](unsigned char c) { 0046 return std::iscntrl(c) && !std::isspace(c); 0047 }); 0048 // prefer encodeString whenever possible, as that selects the more efficient encoding 0049 // automatically, otherwise we end up needlessly in the binary encoding unconditionally 0050 if (isReallyBinary) { 0051 input.reset(QRinput_new()); 0052 QRinput_append(input.get(), QR_MODE_8, byteArrayData().size(), reinterpret_cast<const uint8_t *>(byteArrayData().constData())); 0053 code.reset(QRcode_encodeInput(input.get())); 0054 } else { 0055 qrEncodeString(code, b); 0056 } 0057 } 0058 0059 if (!code) { 0060 return QImage(); 0061 } 0062 const int margin = 4; 0063 /*32 bit colors, 8 bit pr byte*/ 0064 uchar *img = new uchar[4 * sizeof(char *) * (2 * margin + code->width) * (2 * margin * +code->width)]; 0065 uchar *p = img; 0066 QByteArray background; 0067 background.resize(4); 0068 background[3] = qAlpha(backgroundColor().rgba()); 0069 background[2] = qRed(backgroundColor().rgba()); 0070 background[1] = qGreen(backgroundColor().rgba()); 0071 background[0] = qBlue(backgroundColor().rgba()); 0072 QByteArray foreground; 0073 foreground.resize(4); 0074 foreground[3] = qAlpha(foregroundColor().rgba()); 0075 foreground[2] = qRed(foregroundColor().rgba()); 0076 foreground[1] = qGreen(foregroundColor().rgba()); 0077 foreground[0] = qBlue(foregroundColor().rgba()); 0078 for (int row = 0; row < code->width + 2 * margin; row++) { 0079 for (int col = 0; col < code->width + 2 * margin; col++) { 0080 if (row < margin || row >= (code->width + margin) || col < margin || col >= (code->width + margin)) { 0081 /*4 bytes for color*/ 0082 for (int i = 0; i < 4; i++) { 0083 *p = background[i]; 0084 p++; 0085 } 0086 } else { 0087 int c = (row - margin) * code->width + (col - margin); 0088 /*it is bit 1 that is the interesting bit for us from libqrencode*/ 0089 if (code->data[c] & 1) { 0090 /*4 bytes for color*/ 0091 for (int i = 0; i < 4; i++) { 0092 *p = foreground[i]; 0093 p++; 0094 } 0095 } else { 0096 /*4 bytes for color*/ 0097 for (int i = 0; i < 4; i++) { 0098 *p = background[i]; 0099 p++; 0100 } 0101 } 0102 } 0103 } 0104 } 0105 0106 const auto result = 0107 QImage(img, code->width + 2 * margin, code->width + 2 * margin, QImage::Format_ARGB32).copy(); // deep copy as we are going to delete img 0108 delete[] img; 0109 return result; 0110 }