Improved bitmap lines drawing
[GPXSee.git] / src / map / bitmapline.cpp
blob8eeb901894276abd543dbdd6f54ea13802dc2c8f
1 #include <QPainter>
2 #include <QImage>
3 #include <QtMath>
4 #include "bitmapline.h"
7 static QImage img2line(const QImage &img, int width, int offset)
9 Q_ASSERT(img.format() == QImage::Format_ARGB32_Premultiplied);
10 QImage res(width, img.height(), QImage::Format_ARGB32_Premultiplied);
11 const int srcBpl = img.bytesPerLine();
12 const int dstBpl = res.bytesPerLine();
13 const uchar *srcBits = img.bits();
14 uchar *dstBits = res.bits();
16 for (int i = 0; i < img.height(); i++) {
17 const uchar *srcLine = srcBits + srcBpl * i;
18 uchar *dstLine = dstBits + dstBpl * i;
19 int size = 0;
21 if (offset) {
22 size = qMin(dstBpl, srcBpl - 4 * offset);
23 memcpy(dstLine, srcLine + 4 * offset, size);
24 dstLine += size;
26 for (int j = dstBpl - size; j > 0; j -= srcBpl, dstLine += srcBpl)
27 memcpy(dstLine, srcLine, qMin(j, srcBpl));
30 res.setDevicePixelRatio(img.devicePixelRatio());
32 return res;
35 void BitmapLine::draw(QPainter *painter, const QPolygonF &line,
36 const QImage &img)
38 int offset = 0;
40 for (int i = 1; i < line.size(); i++) {
41 QLineF segment(line.at(i-1).x(), line.at(i-1).y(), line.at(i).x(),
42 line.at(i).y());
43 int len = qCeil(segment.length() * img.devicePixelRatio());
45 painter->save();
46 painter->translate(segment.p1());
47 painter->rotate(-segment.angle());
48 painter->drawImage(0.0, -img.height()/2.0, img2line(img, len, offset));
49 painter->restore();
51 offset = (len + offset) % img.width();
55 void BitmapLine::draw(QPainter *painter, const QVector<QPolygonF> &lines,
56 const QImage &img)
58 for (int i = 0; i < lines.size(); i++)
59 draw(painter, lines.at(i), img);