1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 11:45:53 +01:00

Code cleanup

This commit is contained in:
Martin Tůma 2021-11-04 23:01:33 +01:00
parent 0a72c729f8
commit 933ecffe93
4 changed files with 68 additions and 71 deletions

View File

@ -2,7 +2,7 @@
using namespace IMG; using namespace IMG;
bool HuffmanStreamF::init(bool line) bool HuffmanDeltaStreamF::init(bool line)
{ {
if (line) { if (line) {
if (!(sign(_lonSign) && sign(_latSign))) if (!(sign(_lonSign) && sign(_latSign)))
@ -13,7 +13,7 @@ bool HuffmanStreamF::init(bool line)
} }
quint32 eb; quint32 eb;
if (!_bs.read(1, eb)) if (!read(1, eb))
return false; return false;
Q_ASSERT(!eb); Q_ASSERT(!eb);
@ -23,7 +23,7 @@ bool HuffmanStreamF::init(bool line)
return true; return true;
} }
bool HuffmanStreamR::init() bool HuffmanDeltaStreamR::init()
{ {
if (!(sign(_lonSign) && sign(_latSign))) if (!(sign(_lonSign) && sign(_latSign)))
return false; return false;
@ -31,13 +31,13 @@ bool HuffmanStreamR::init()
return true; return true;
} }
bool HuffmanStreamR::init(int lonSign, int latSign, quint32 data, bool HuffmanDeltaStreamR::init(quint32 data, quint32 dataSize)
quint32 dataSize)
{ {
_lonSign = lonSign;
_latSign = latSign;
_symbolData = data; _symbolData = data;
_symbolDataSize = dataSize; _symbolDataSize = dataSize;
if (!(sign(_lonSign) && sign(_latSign)))
return false;
return true; return true;
} }

View File

@ -10,51 +10,24 @@ template <class BitStream>
class HuffmanStream { class HuffmanStream {
public: public:
HuffmanStream(BitStream &bitstream, const HuffmanTable &table) HuffmanStream(BitStream &bitstream, const HuffmanTable &table)
: _bs(bitstream), _table(table), _symbolDataSize(0), _symbolData(0), : _symbolDataSize(0), _symbolData(0), _bs(bitstream), _table(table) {}
_lonSign(0), _latSign(0) {}
bool read(int bits, quint32 &val); bool read(int bits, quint32 &val);
bool readSymbol(quint32 &symbol); bool readSymbol(quint32 &symbol);
bool readNext(qint32 &lonDelta, qint32 &latDelta)
{
if (!(readDelta(_lonSign, lonDelta) && readDelta(_latSign, latDelta)))
return false;
return (lonDelta || latDelta);
}
bool atEnd() const bool atEnd() const
{return _symbolDataSize + _bs.bitsAvailable() < _table.symBits();} {return _symbolDataSize + _bs.bitsAvailable() < _table.symBits();}
bool flush() {return _bs.flush();} bool flush() {return _bs.flush();}
protected: protected:
bool sign(int &val);
bool readDelta(int sign, qint32 &delta);
BitStream &_bs;
const HuffmanTable &_table;
quint32 _symbolDataSize; quint32 _symbolDataSize;
quint32 _symbolData; quint32 _symbolData;
int _lonSign, _latSign;
private:
BitStream &_bs;
const HuffmanTable &_table;
}; };
template <class BitStream>
bool HuffmanStream<BitStream>::sign(int &val)
{
quint32 bit;
val = 0;
if (!_bs.read(1, bit))
return false;
if (bit) {
if (!_bs.read(1, bit))
return false;
val = bit ? -1 : 1;
}
return true;
}
template <class BitStream> template <class BitStream>
bool HuffmanStream<BitStream>::read(int bits, quint32 &val) bool HuffmanStream<BitStream>::read(int bits, quint32 &val)
{ {
@ -102,18 +75,58 @@ bool HuffmanStream<BitStream>::readSymbol(quint32 &symbol)
} }
template <class BitStream> template <class BitStream>
bool HuffmanStream<BitStream>::readDelta(int sign, qint32 &delta) class HuffmanDeltaStream : public HuffmanStream<BitStream>
{
public:
HuffmanDeltaStream(BitStream &bitstream, const HuffmanTable &table)
: HuffmanStream<BitStream>(bitstream, table), _lonSign(0), _latSign(0) {}
bool readNext(qint32 &lonDelta, qint32 &latDelta)
{
if (!(readDelta(_lonSign, lonDelta) && readDelta(_latSign, latDelta)))
return false;
return (lonDelta || latDelta);
}
protected:
bool sign(int &val);
bool readDelta(int sign, qint32 &delta);
int _lonSign, _latSign;
};
template <class BitStream>
bool HuffmanDeltaStream<BitStream>::sign(int &val)
{
quint32 bit;
val = 0;
if (!this->read(1, bit))
return false;
if (bit) {
if (!this->read(1, bit))
return false;
val = bit ? -1 : 1;
}
return true;
}
template <class BitStream>
bool HuffmanDeltaStream<BitStream>::readDelta(int sign, qint32 &delta)
{ {
quint32 symbol; quint32 symbol;
if (!readSymbol(symbol)) if (!this->readSymbol(symbol))
return false; return false;
if (symbol && !sign) { if (symbol && !sign) {
if (!_symbolDataSize) if (!this->_symbolDataSize)
return false; return false;
else { else {
sign = ((1U << (_symbolDataSize - 1)) & _symbolData) ? -1 : 1; sign = ((1U << (this->_symbolDataSize - 1)) & this->_symbolData)
_symbolDataSize--; ? -1 : 1;
this->_symbolDataSize--;
} }
} }
delta = sign * symbol; delta = sign * symbol;
@ -121,23 +134,23 @@ bool HuffmanStream<BitStream>::readDelta(int sign, qint32 &delta)
return true; return true;
} }
class HuffmanStreamF : public HuffmanStream<BitStream4F> { class HuffmanDeltaStreamF : public HuffmanDeltaStream<BitStream4F> {
public: public:
HuffmanStreamF(BitStream4F &bitstream, const HuffmanTable &table) HuffmanDeltaStreamF(BitStream4F &bitstream, const HuffmanTable &table)
: HuffmanStream(bitstream, table) {} : HuffmanDeltaStream(bitstream, table) {}
bool init(bool line); bool init(bool line);
bool readOffset(qint32 &lonDelta, qint32 &latDelta) bool readOffset(qint32 &lonDelta, qint32 &latDelta)
{return (readDelta(1, lonDelta) && readDelta(1, latDelta));} {return (readDelta(1, lonDelta) && readDelta(1, latDelta));}
}; };
class HuffmanStreamR : public HuffmanStream<BitStream4R> { class HuffmanDeltaStreamR : public HuffmanDeltaStream<BitStream4R> {
public: public:
HuffmanStreamR(BitStream4R &bitstream, const HuffmanTable &table) HuffmanDeltaStreamR(BitStream4R &bitstream, const HuffmanTable &table)
: HuffmanStream(bitstream, table) {} : HuffmanDeltaStream(bitstream, table) {}
bool init(); bool init();
bool init(int lonSign, int latSign, quint32 data, quint32 dataSize); bool init(quint32 data, quint32 dataSize);
}; };
} }

View File

@ -116,7 +116,7 @@ static bool readLine(BitStream4R &bs, const SubDiv *subdiv,
poly.boundingRect = RectC(c, c); poly.boundingRect = RectC(c, c);
poly.points.append(QPointF(c.lon(), c.lat())); poly.points.append(QPointF(c.lon(), c.lat()));
HuffmanStreamR stream(bs, *table); HuffmanDeltaStreamR stream(bs, *table);
if (!stream.init()) if (!stream.init())
return false; return false;
qint32 lonDelta, latDelta; qint32 lonDelta, latDelta;
@ -164,18 +164,6 @@ static bool skipNodes(const NODFile *nod, SubFile::Handle &nodHdl,
return true; return true;
} }
static int sign(quint32 flags, quint32 &bits)
{
if (!((flags >> bits) & 1)) {
bits--;
return 0;
} else {
quint32 sb = bits - 1;
bits -= 2;
return ((flags >> sb) & 1) ? -1 : 1;
}
}
static bool readShape(const NODFile *nod, SubFile::Handle &nodHdl, static bool readShape(const NODFile *nod, SubFile::Handle &nodHdl,
NODFile::AdjacencyInfo &adj, BitStream4R &bs, const HuffmanTable *table, NODFile::AdjacencyInfo &adj, BitStream4R &bs, const HuffmanTable *table,
const SubDiv *subdiv, quint32 shift, MapData::Poly &poly, const SubDiv *subdiv, quint32 shift, MapData::Poly &poly,
@ -197,12 +185,8 @@ static bool readShape(const NODFile *nod, SubFile::Handle &nodHdl,
bool startWithStream = flags & (1 << (v2b + 6)); bool startWithStream = flags & (1 << (v2b + 6));
bool useEosBit = flags & (1 << (v2b + 5)); bool useEosBit = flags & (1 << (v2b + 5));
quint32 extraBits = v2b + 4; HuffmanDeltaStreamR stream(bs, *table);
int lonSign = sign(flags, extraBits); if (!stream.init(flags, v2b + 5))
int latSign = sign(flags, extraBits);
HuffmanStreamR stream(bs, *table);
if (!stream.init(lonSign, latSign, flags, extraBits + 1))
return false; return false;

View File

@ -287,7 +287,7 @@ bool RGNFile::extPolyObjects(Handle &hdl, const SubDiv *subdiv, quint32 shift,
qint32 lonDelta, latDelta; qint32 lonDelta, latDelta;
BitStream4F bs(*this, hdl, len); BitStream4F bs(*this, hdl, len);
HuffmanStreamF stream(bs, *_huffmanTable); HuffmanDeltaStreamF stream(bs, *_huffmanTable);
if (!stream.init(segmentType == Line)) if (!stream.init(segmentType == Line))
return false; return false;