Prison

datamatrixbarcode.cpp
1/*
2 SPDX-FileCopyrightText: 2010-2014 Sune Vuorela <sune@vuorela.dk>
3
4 SPDX-License-Identifier: MIT
5*/
6
7#include "datamatrixbarcode_p.h"
8#include <dmtx.h>
9using namespace Prison;
10
11DataMatrixBarcode::DataMatrixBarcode()
12 : AbstractBarcodePrivate(Barcode::TwoDimensions)
13{
14}
15DataMatrixBarcode::~DataMatrixBarcode() = default;
16
17QImage DataMatrixBarcode::paintImage()
18{
19 const auto data = m_data.toString();
20 if (data.size() > 1200) {
21 return QImage();
22 }
23
24 DmtxEncode *enc = dmtxEncodeCreate();
25 dmtxEncodeSetProp(enc, DmtxPropPixelPacking, DmtxPack32bppRGBX);
26 dmtxEncodeSetProp(enc, DmtxPropModuleSize, 1);
27 dmtxEncodeSetProp(enc, DmtxPropMarginSize, 2);
28
29 QByteArray trimmedData(data.trimmed().toUtf8());
30 DmtxPassFail result = dmtxEncodeDataMatrix(enc, trimmedData.length(), reinterpret_cast<unsigned char *>(trimmedData.data()));
31 if (result == DmtxFail) {
32 dmtxEncodeDestroy(&enc);
33 return QImage();
34 }
35 Q_ASSERT(enc->image->width == enc->image->height);
36
37 QImage ret;
38
39 if (m_foreground == Qt::black && m_background == Qt::white) {
40 QImage tmp(enc->image->pxl, enc->image->width, enc->image->height, QImage::Format_ARGB32);
41 // we need to copy, because QImage generated from a char pointer requires the
42 // char pointer to be kept around forever, and manually deleted.
43 ret = tmp.copy();
44 } else {
45 if (enc->image->width > 0) {
46 int size = enc->image->width * enc->image->height * 4;
47 uchar *img = new uchar[size];
48 QByteArray background(4, '\0');
49 background[3] = qAlpha(m_background.rgba());
50 background[2] = qRed(m_background.rgba());
51 background[1] = qGreen(m_background.rgba());
52 background[0] = qBlue(m_background.rgba());
53 QByteArray foreground(4, '\0');
54 foreground[3] = qAlpha(m_foreground.rgba());
55 foreground[2] = qRed(m_foreground.rgba());
56 foreground[1] = qGreen(m_foreground.rgba());
57 foreground[0] = qBlue(m_foreground.rgba());
58 for (int i = 1; i < size; i += 4) {
59 QByteArray color;
60 if (enc->image->pxl[i] == 0x00) {
61 color = foreground;
62 } else {
63 color = background;
64 }
65 for (int j = 0; j < 4; j++) {
66 img[i - 1 + j] = color[j];
67 }
68 }
69 QImage tmp(img, enc->image->width, enc->image->height, QImage::Format_ARGB32);
70 // we need to copy, because QImage generated from a char pointer requires the
71 // char pointer to be kept around forever, and manually deleted.
72 ret = tmp.copy();
73 delete[] img;
74 }
75 }
76 dmtxEncodeDestroy(&enc);
77 return ret;
78}
A barcode generator for a fixed barcode format.
Definition barcode.h:40
Provides classes and methods for generating barcodes.
Definition barcode.h:24
QImage copy(const QRect &rectangle) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:55:54 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.