2019-05-10 18:56:19 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QImage>
|
2020-02-17 19:21:36 +01:00
|
|
|
#include <QtMath>
|
2019-05-10 18:56:19 +02:00
|
|
|
#include "bitmapline.h"
|
|
|
|
|
2020-02-17 19:21:36 +01:00
|
|
|
|
2024-02-07 22:10:40 +01:00
|
|
|
static QImage img2line(const QImage &img, int width, int offset)
|
2019-05-10 18:56:19 +02:00
|
|
|
{
|
|
|
|
Q_ASSERT(img.format() == QImage::Format_ARGB32_Premultiplied);
|
2024-02-07 22:10:40 +01:00
|
|
|
QImage res(width, img.height(), QImage::Format_ARGB32_Premultiplied);
|
2019-05-10 18:56:19 +02:00
|
|
|
const int srcBpl = img.bytesPerLine();
|
|
|
|
const int dstBpl = res.bytesPerLine();
|
|
|
|
const uchar *srcBits = img.bits();
|
|
|
|
uchar *dstBits = res.bits();
|
|
|
|
|
|
|
|
for (int i = 0; i < img.height(); i++) {
|
|
|
|
const uchar *srcLine = srcBits + srcBpl * i;
|
|
|
|
uchar *dstLine = dstBits + dstBpl * i;
|
2024-02-07 22:10:40 +01:00
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
if (offset) {
|
|
|
|
size = qMin(dstBpl, srcBpl - 4 * offset);
|
|
|
|
memcpy(dstLine, srcLine + 4 * offset, size);
|
|
|
|
dstLine += size;
|
|
|
|
}
|
|
|
|
for (int j = dstBpl - size; j > 0; j -= srcBpl, dstLine += srcBpl)
|
2019-05-10 18:56:19 +02:00
|
|
|
memcpy(dstLine, srcLine, qMin(j, srcBpl));
|
|
|
|
}
|
|
|
|
|
2024-02-06 22:37:01 +01:00
|
|
|
res.setDevicePixelRatio(img.devicePixelRatio());
|
|
|
|
|
2019-05-10 18:56:19 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitmapLine::draw(QPainter *painter, const QPolygonF &line,
|
|
|
|
const QImage &img)
|
|
|
|
{
|
2024-02-07 22:10:40 +01:00
|
|
|
int offset = 0;
|
|
|
|
|
2019-05-10 18:56:19 +02:00
|
|
|
for (int i = 1; i < line.size(); i++) {
|
|
|
|
QLineF segment(line.at(i-1).x(), line.at(i-1).y(), line.at(i).x(),
|
|
|
|
line.at(i).y());
|
2024-02-07 22:10:40 +01:00
|
|
|
int len = qCeil(segment.length() * img.devicePixelRatio());
|
2019-05-10 18:56:19 +02:00
|
|
|
|
|
|
|
painter->save();
|
|
|
|
painter->translate(segment.p1());
|
|
|
|
painter->rotate(-segment.angle());
|
2024-02-07 22:10:40 +01:00
|
|
|
painter->drawImage(0.0, -img.height()/2.0, img2line(img, len, offset));
|
2019-05-10 18:56:19 +02:00
|
|
|
painter->restore();
|
2024-02-07 22:10:40 +01:00
|
|
|
|
|
|
|
offset = (len + offset) % img.width();
|
2019-05-10 18:56:19 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-07 09:31:23 +02:00
|
|
|
|
|
|
|
void BitmapLine::draw(QPainter *painter, const QVector<QPolygonF> &lines,
|
|
|
|
const QImage &img)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < lines.size(); i++)
|
|
|
|
draw(painter, lines.at(i), img);
|
|
|
|
}
|