1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-30 22:51:16 +01:00

Code cleanup

This commit is contained in:
Martin Tůma 2021-10-22 22:04:46 +02:00
parent 8e867851e9
commit ccfda7ca7f
3 changed files with 60 additions and 62 deletions

View File

@ -24,7 +24,7 @@ public:
} }
bool atEnd() const bool atEnd() const
{return _symbolDataSize + _bs.bitsAvailable() < _table.maxSymbolSize();} {return _symbolDataSize + _bs.bitsAvailable() < _table.maxDataBits();}
bool flush() {return _bs.flush();} bool flush() {return _bs.flush();}
protected: protected:

View File

@ -8,8 +8,8 @@ static inline quint32 readVUint32(const quint8 *buffer, quint32 bytes)
{ {
quint32 val = 0; quint32 val = 0;
for (quint32 i = bytes; i; i--) for (quint32 i = 0; i < bytes; i++)
val |= ((quint32)*(buffer + i)) << ((i-1) * 8); val |= ((quint32)*(buffer + i)) << (i * 8);
return val; return val;
} }
@ -19,89 +19,87 @@ bool HuffmanTable::load(const RGNFile *rgn, SubFile::Handle &rgnHdl)
if (!_buffer.load(rgn, rgnHdl)) if (!_buffer.load(rgn, rgnHdl))
return false; return false;
_s0 = (quint8)_buffer.at(0) & 0x0F; _aclBits = (quint8)_buffer.at(0) & 0x0F;
_s1e = (quint8)_buffer.at(0) & 0x10; _huffman = (quint8)_buffer.at(0) & 0x10;
_s2 = (quint8)_buffer.at(1); _symBits = (quint8)_buffer.at(1);
_s3 = bs(_s2); _symBytes = bs(_symBits);
_s1d = (quint8)_buffer.at(2); _bsrchEntries = (quint8)_buffer.at(2);
_s1f = (quint8)_buffer.at(3); _symbolBits = (quint8)_buffer.at(3);
_s20 = bs(_s1f); _symbolBytes = bs(_symbolBits);
_s1 = _s20 + 1; _aclEntryBytes = _symbolBytes + 1;
_s22 = vs(_buffer.at(4)); _indexBytes = vs(_buffer.at(4));
_s1c = _s3 + 1 + _s22; _bsrchEntryBytes = _symBytes + 1 + _indexBytes;
_s14 = (quint8*)(_buffer.data()) + 4 + _s22; _bsrchTable = (quint8*)(_buffer.data()) + 4 + _indexBytes;
_s10 = _s14 + _s1c * _s1d; _aclTable = _bsrchTable + _bsrchEntryBytes * _bsrchEntries;
_s18 = _s10 + (_s1 << _s0); _huffmanTable = _aclTable + (_aclEntryBytes << _aclBits);
return true; return true;
} }
quint32 HuffmanTable::symbol(quint32 data, quint8 &size) const quint32 HuffmanTable::symbol(quint32 data, quint8 &size) const
{ {
quint32 ss, sym; quint32 lo, hi;
quint8 *tp; quint8 *tp;
if (_s0 == 0) { if (!_aclBits) {
sym = _s1d - 1; hi = _bsrchEntries - 1;
ss = 0; lo = 0;
} else { } else {
quint32 offset = _s1 * (data >> (0x20U - _s0)); quint32 offset = _aclEntryBytes * (data >> (32 - _aclBits));
tp = _s10 + offset; tp = _aclTable + offset;
if ((*tp & 1) != 0) { if (*tp & 1) {
sym = readVUint32(tp, _s20);
size = *tp >> 1; size = *tp >> 1;
return sym; return readVUint32(tp + 1, _symbolBytes);;
} }
ss = *tp >> 1; lo = *tp >> 1;
sym = tp[1]; hi = *(tp + 1);
} }
tp = ss * _s1c + _s14; tp = _bsrchTable + (lo * _bsrchEntryBytes);
data = data >> (0x20U - _s2); data >>= 32 - _symBits;
while (lo < hi) {
quint8 *prev = tp; quint8 *prev = tp;
while (ss < sym) { quint32 m = (lo + 1 + hi) >> 1;
quint32 cnt = (ss + 1 + sym) >> 1; tp = _bsrchTable + (m * _bsrchEntryBytes);
tp = _s14 + (cnt * _s1c); quint32 nd = readVUint32(tp, _symBytes);
quint32 nd = readVUint32(tp - 1, _s3);
if (data <= nd) { if (data <= nd) {
if (data == nd) if (data == nd)
ss = cnt; lo = m;
else else
tp = prev; tp = prev;
sym = cnt - (data < nd); hi = m - (data < nd);
cnt = ss; m = lo;
} }
ss = cnt; lo = m;
prev = tp;
} }
sym = readVUint32(tp - 1, _s3); quint32 i = readVUint32(tp, _symBytes);
tp = tp + _s3; tp = tp + _symBytes;
ss = readVUint32(tp, _s22);
size = *tp; size = *tp;
sym = (data - sym) >> (_s2 - *tp);
if (!_s1e) if (!_huffman)
sym = readVUint32(tp, _s20); return readVUint32(tp + 1, _symbolBytes);
else { else {
sym = (sym + ss) * _s1f; quint32 bi = readVUint32(tp + 1, _indexBytes);
ss = sym >> 3; quint32 ci = (data - i) >> (_symBits - size);
sym = sym & 7; quint32 si = (ci + bi) * _symbolBits;
quint32 shift = 8 - sym; quint32 sby = si >> 3;
sym = *(_s18 + ss) >> sym; quint32 sbi = si & 7;
quint32 shift = 8 - sbi;
quint32 sym = *(_huffmanTable + sby) >> sbi;
if (shift < _s1f) { if (shift < _symbolBits) {
tp = _s18 + ss; tp = _huffmanTable + sby;
ss = readVUint32(tp, ((_s1f + 7) - shift) >> 3); quint32 val = readVUint32(tp + 1, bs(_symbolBits - shift));
sym = (ss << shift) | sym; sym = (val << shift) | sym;
}
} }
return sym; return sym;
} }
}

View File

@ -12,18 +12,18 @@ public:
HuffmanTable(quint8 id) : _buffer(id) {} HuffmanTable(quint8 id) : _buffer(id) {}
bool load(const RGNFile *rgn, SubFile::Handle &rgnHdl); bool load(const RGNFile *rgn, SubFile::Handle &rgnHdl);
quint8 maxSymbolSize() const {return _s2;} quint8 maxDataBits() const {return _symBits;}
quint32 symbol(quint32 data, quint8 &size) const; quint32 symbol(quint32 data, quint8 &size) const;
quint8 id() const {return _buffer.id();} quint8 id() const {return _buffer.id();}
private: private:
HuffmanBuffer _buffer; HuffmanBuffer _buffer;
quint8 _s0, _s1, _s2, _s3; quint8 _aclBits, _aclEntryBytes, _symBits, _symBytes;
quint8 *_s10, *_s14, *_s18; quint8 *_aclTable, *_bsrchTable, *_huffmanTable;
quint8 _s1c, _s1d, _s1f, _s20; quint8 _bsrchEntryBytes, _bsrchEntries, _symbolBits, _symbolBytes;
quint16 _s22; quint16 _indexBytes;
bool _s1e; bool _huffman;
}; };
} }