From df3ee11f42af35934d01dcad49553149e31b28bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Tue, 1 Dec 2020 19:03:58 +0100 Subject: [PATCH] Properly handle skewed charts --- src/map/bsbmap.cpp | 31 +++++++++++++++++++++++++++---- src/map/bsbmap.h | 4 +++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/map/bsbmap.cpp b/src/map/bsbmap.cpp index b262a96e..f7ee4511 100644 --- a/src/map/bsbmap.cpp +++ b/src/map/bsbmap.cpp @@ -157,6 +157,11 @@ bool BSBMap::parseKNP(const QByteArray &line, QString &datum, QString &proj, _errorString = "Invalid KNP PP field"; return false; } + _skew = parameter(map.value("SK"), &ok); + if (!ok) { + _errorString = "Invalid KNP SK field"; + return false; + } return true; } @@ -253,8 +258,16 @@ bool BSBMap::readHeader(QFile &file) return false; } -bool BSBMap::createTransform(const QList &points) +bool BSBMap::createTransform(QList &points) { + if (_skew > 0.0 && _skew < 360.0) { + QTransform matrix; + matrix.rotate(-_skew); + QTransform t(QImage::trueMatrix(matrix, _size.width(), _size.height())); + for (int i = 0; i < points.size(); i++) + points[i].setXY(t.map(points.at(i).xy().toPointF())); + } + _transform = Transform(points); if (!_transform.isValid()) { _errorString = _transform.errorString(); @@ -400,7 +413,9 @@ Coordinates BSBMap::xy2ll(const QPointF &p) QRectF BSBMap::bounds() { - return QRectF(QPointF(0, 0), _size / _ratio); + return _skewSize.isValid() + ? QRectF(QPointF(0, 0), _skewSize / _ratio) + : QRectF(QPointF(0, 0), _size / _ratio); } void BSBMap::draw(QPainter *painter, const QRectF &rect, Flags flags) @@ -420,8 +435,16 @@ void BSBMap::setDevicePixelRatio(qreal deviceRatio, qreal mapRatio) void BSBMap::load() { - if (!_img) - _img = new Image(readImage()); + if (!_img) { + if (_skew > 0.0 && _skew < 360.0) { + QTransform matrix; + matrix.rotate(-_skew); + QImage img(readImage().transformed(matrix)); + _skewSize = img.size(); + _img = new Image(img); + } else + _img = new Image(readImage()); + } } void BSBMap::unload() diff --git a/src/map/bsbmap.h b/src/map/bsbmap.h index f27820de..d6ae0bba 100644 --- a/src/map/bsbmap.h +++ b/src/map/bsbmap.h @@ -42,7 +42,7 @@ private: bool readHeader(QFile &file); bool createProjection(const QString &datum, const QString &proj, double params[9]); - bool createTransform(const QList &points); + bool createTransform(QList &points); QImage readImage(); bool readRow(QFile &file, char bits, uchar *buf); @@ -50,8 +50,10 @@ private: QString _name; Projection _projection; Transform _transform; + qreal _skew; Image *_img; QSize _size; + QSize _skewSize; qreal _ratio; qint64 _dataOffset; QVector _palette;