File indexing completed on 2024-05-12 04:01:32

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: MIT
0004 */
0005 
0006 #include "zxingutil_p.h"
0007 
0008 #include <QImage>
0009 #include <QVariant>
0010 
0011 #include <ZXing/BitMatrix.h>
0012 
0013 using namespace Prison;
0014 
0015 std::wstring ZXingUtil::toStdWString(const QVariant &data)
0016 {
0017     if (data.userType() == QMetaType::QString) {
0018         return data.toString().toStdWString();
0019     }
0020     if (data.userType() == QMetaType::QByteArray) {
0021         const auto b = data.toByteArray();
0022         std::wstring ws;
0023         ws.reserve(b.size());
0024         // ensure we explicitly copy unsigned bytes here, ie. each byte ends up in the least significant byte of
0025         // the std::wstring. If we end up converting to a signed value inbetween, values > 127 end up negative and
0026         // will be wrongly represented in the std::wstring
0027         for (uint8_t c : b) {
0028             ws.push_back(c);
0029         }
0030         return ws;
0031     }
0032 
0033     return {};
0034 }
0035 
0036 QImage ZXingUtil::toImage(const ZXing::BitMatrix &matrix, const QColor &foreground, const QColor &background)
0037 {
0038     QImage image(matrix.width(), matrix.height(), QImage::Format_ARGB32);
0039     for (int y = 0; y < matrix.height(); ++y) {
0040         for (int x = 0; x < matrix.width(); ++x) {
0041             image.setPixel(x, y, matrix.get(x, y) ? foreground.rgb() : background.rgb());
0042         }
0043     }
0044     return image;
0045 }