1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-12-01 07:01:16 +01:00

Some more code cleanup

This commit is contained in:
Martin Tůma 2019-09-21 00:36:49 +02:00
parent 36555b3140
commit 1bb9908936
2 changed files with 29 additions and 44 deletions

View File

@ -50,17 +50,9 @@ bool RGNFile::BitStream::read(int bits, quint32 &val)
return true; return true;
} }
bool RGNFile::BitStream::flush()
{
while (_length--)
if (!_file.readByte(_hdl, _data))
return false;
return true;
}
RGNFile::DeltaStream::DeltaStream(const SubFile &file, Handle &hdl, RGNFile::DeltaStream::DeltaStream(const SubFile &file, Handle &hdl,
quint32 length, quint8 info, bool extraBit, bool extended) quint32 length, quint8 info, bool extraBit, bool extended)
: BitStream(file, hdl, length), _lonBits(0), _latBits(0) : BitStream(file, hdl, length), _readBits(0xFFFFFFFF)
{ {
_extraBit = extraBit ? 1 : 0; _extraBit = extraBit ? 1 : 0;
if (!(sign(_lonSign) && sign(_latSign))) if (!(sign(_lonSign) && sign(_latSign)))
@ -72,6 +64,7 @@ RGNFile::DeltaStream::DeltaStream(const SubFile &file, Handle &hdl,
} }
_lonBits = bitSize(info & 0x0F, !_lonSign, extraBit); _lonBits = bitSize(info & 0x0F, !_lonSign, extraBit);
_latBits = bitSize(info >> 4, !_latSign, false); _latBits = bitSize(info >> 4, !_latSign, false);
_readBits = _lonBits + _latBits;
} }
bool RGNFile::DeltaStream::readDelta(int bits, int sign, int extraBit, bool RGNFile::DeltaStream::readDelta(int bits, int sign, int extraBit,
@ -195,23 +188,17 @@ bool RGNFile::polyObjects(const RectC &rect, Handle &hdl, const SubDiv *subdiv,
poly.type = (segment.type() == Segment::Polygon) poly.type = (segment.type() == Segment::Polygon)
? ((quint32)(type & 0x7F)) << 8 : ((quint32)(type & 0x3F)) << 8; ? ((quint32)(type & 0x7F)) << 8 : ((quint32)(type & 0x3F)) << 8;
RectC br;
QPoint pos(subdiv->lon() + ((qint32)lon<<(24-subdiv->bits())), QPoint pos(subdiv->lon() + ((qint32)lon<<(24-subdiv->bits())),
subdiv->lat() + ((qint32)lat<<(24-subdiv->bits()))); subdiv->lat() + ((qint32)lat<<(24-subdiv->bits())));
Coordinates c(toWGS84(pos.x()), toWGS84(pos.y())); Coordinates c(toWGS84(pos.x()), toWGS84(pos.y()));
br = br.united(c); RectC br(c, c);
poly.points.append(QPointF(c.lon(), c.lat())); poly.points.append(QPointF(c.lon(), c.lat()));
qint32 lonDelta, latDelta;
DeltaStream stream(*this, hdl, len, bitstreamInfo, labelPtr & 0x400000, DeltaStream stream(*this, hdl, len, bitstreamInfo, labelPtr & 0x400000,
false); false);
if (!stream.isValid()) while (stream.readNext(lonDelta, latDelta)) {
return false;
while (stream.hasNext()) {
qint32 lonDelta, latDelta;
if (!(stream.readNext(lonDelta, latDelta)))
return false;
pos.rx() += lonDelta<<(24-subdiv->bits()); pos.rx() += lonDelta<<(24-subdiv->bits());
pos.ry() += latDelta<<(24-subdiv->bits()); pos.ry() += latDelta<<(24-subdiv->bits());
@ -219,7 +206,7 @@ bool RGNFile::polyObjects(const RectC &rect, Handle &hdl, const SubDiv *subdiv,
poly.points.append(QPointF(c.lon(), c.lat())); poly.points.append(QPointF(c.lon(), c.lat()));
br = br.united(c); br = br.united(c);
} }
if (!stream.flush()) if (!(stream.atEnd() && stream.flush()))
return false; return false;
if (!rect.intersects(br)) if (!rect.intersects(br))
@ -245,7 +232,7 @@ bool RGNFile::extPolyObjects(const RectC &rect, Handle &hdl,
const SubDiv *subdiv, const Segment &segment, LBLFile *lbl, Handle &lblHdl, const SubDiv *subdiv, const Segment &segment, LBLFile *lbl, Handle &lblHdl,
QList<IMG::Poly> *polys) const QList<IMG::Poly> *polys) const
{ {
quint32 labelPtr, len; quint32 len, labelPtr = 0;
quint8 type, subtype, bitstreamInfo; quint8 type, subtype, bitstreamInfo;
qint16 lon, lat; qint16 lon, lat;
@ -268,22 +255,15 @@ bool RGNFile::extPolyObjects(const RectC &rect, Handle &hdl,
poly.type = 0x10000 + (quint16(type) << 8) + (subtype & 0x1F); poly.type = 0x10000 + (quint16(type) << 8) + (subtype & 0x1F);
RectC br;
QPoint pos(subdiv->lon() + ((qint32)lon<<(24-subdiv->bits())), QPoint pos(subdiv->lon() + ((qint32)lon<<(24-subdiv->bits())),
subdiv->lat() + ((qint32)lat<<(24-subdiv->bits()))); subdiv->lat() + ((qint32)lat<<(24-subdiv->bits())));
Coordinates c(toWGS84(pos.x()), toWGS84(pos.y())); Coordinates c(toWGS84(pos.x()), toWGS84(pos.y()));
br = br.united(c); RectC br(c, c);
poly.points.append(QPointF(c.lon(), c.lat())); poly.points.append(QPointF(c.lon(), c.lat()));
DeltaStream stream(*this, hdl, len - 1, bitstreamInfo, false, true);
if (!stream.isValid())
return false;
while (stream.hasNext()) {
qint32 lonDelta, latDelta; qint32 lonDelta, latDelta;
DeltaStream stream(*this, hdl, len - 1, bitstreamInfo, false, true);
if (!(stream.readNext(lonDelta, latDelta))) while (stream.readNext(lonDelta, latDelta)) {
return false;
pos.rx() += lonDelta<<(24-subdiv->bits()); pos.rx() += lonDelta<<(24-subdiv->bits());
pos.ry() += latDelta<<(24-subdiv->bits()); pos.ry() += latDelta<<(24-subdiv->bits());
@ -291,19 +271,21 @@ bool RGNFile::extPolyObjects(const RectC &rect, Handle &hdl,
poly.points.append(QPointF(c.lon(), c.lat())); poly.points.append(QPointF(c.lon(), c.lat()));
br = br.united(c); br = br.united(c);
} }
if (!stream.flush()) if (!(stream.atEnd() && stream.flush()))
return false; return false;
if (subtype & 0x20) { if ((subtype & 0x20)) {
if (!readUInt24(hdl, labelPtr)) if (!readUInt24(hdl, labelPtr))
return false; return false;
if (lbl && (labelPtr & 0x3FFFFF)) } else
poly.label = lbl->label(lblHdl, labelPtr & 0x3FFFFF); labelPtr = 0;
}
if (!rect.intersects(br)) if (!rect.intersects(br))
continue; continue;
if (lbl && (labelPtr & 0x3FFFFF))
poly.label = lbl->label(lblHdl, labelPtr & 0x3FFFFF);
polys->append(poly); polys->append(poly);
} }

View File

@ -60,8 +60,8 @@ private:
: _file(file), _hdl(hdl), _length(length), _remaining(0) {} : _file(file), _hdl(hdl), _length(length), _remaining(0) {}
bool read(int bits, quint32 &val); bool read(int bits, quint32 &val);
bool flush(); bool flush() {return _file.seek(_hdl, _hdl.pos + _length);}
quint64 bits() const {return _length * 8 + _remaining;} quint32 bitsAvailable() const {return _length * 8 + _remaining;}
private: private:
const SubFile &_file; const SubFile &_file;
@ -75,19 +75,22 @@ private:
DeltaStream(const SubFile &file, Handle &hdl, quint32 length, DeltaStream(const SubFile &file, Handle &hdl, quint32 length,
quint8 info, bool extraBit, bool extended); quint8 info, bool extraBit, bool extended);
bool isValid() const {return _lonBits && _latBits;}
bool hasNext() const
{return bits() >= _lonBits + _latBits;}
bool readNext(qint32 &lonDelta, qint32 &latDelta) bool readNext(qint32 &lonDelta, qint32 &latDelta)
{return (readDelta(_lonBits, _lonSign, _extraBit, lonDelta) {
&& readDelta(_latBits, _latSign, false, latDelta));} return hasNext()
? (readDelta(_lonBits, _lonSign, _extraBit, lonDelta)
&& readDelta(_latBits, _latSign, false, latDelta))
: false;
}
bool atEnd() const {return (_readBits != 0xFFFFFFFF && !hasNext());}
private: private:
bool hasNext() const {return bitsAvailable() >= _readBits;}
bool sign(int &val); bool sign(int &val);
bool readDelta(int bits, int sign, int extraBit, qint32 &delta); bool readDelta(int bits, int sign, int extraBit, qint32 &delta);
int _lonSign, _latSign, _extraBit; int _lonSign, _latSign, _extraBit;
quint32 _lonBits, _latBits; quint32 _lonBits, _latBits, _readBits;
}; };
bool init(); bool init();