2019-05-10 18:56:19 +02:00
|
|
|
#include "subdiv.h"
|
|
|
|
#include "units.h"
|
|
|
|
#include "trefile.h"
|
|
|
|
|
|
|
|
|
|
|
|
static void unlock(quint8 *dst, const quint8 *src, quint32 size, quint32 key)
|
|
|
|
{
|
|
|
|
static const unsigned char shuf[] = {
|
|
|
|
0xb, 0xc, 0xa, 0x0,
|
|
|
|
0x8, 0xf, 0x2, 0x1,
|
|
|
|
0x6, 0x4, 0x9, 0x3,
|
|
|
|
0xd, 0x5, 0x7, 0xe
|
|
|
|
};
|
|
|
|
|
|
|
|
int sum = shuf[((key >> 24) + (key >> 16) + (key >> 8) + key) & 0xf];
|
|
|
|
|
|
|
|
for (quint32 i = 0, ringctr = 16; i < size; i++) {
|
|
|
|
quint32 upper = src[i] >> 4;
|
|
|
|
quint32 lower = src[i];
|
|
|
|
|
|
|
|
upper -= sum;
|
|
|
|
upper -= key >> ringctr;
|
|
|
|
upper -= shuf[(key >> ringctr) & 0xf];
|
|
|
|
ringctr = ringctr ? ringctr - 4 : 16;
|
|
|
|
|
|
|
|
lower -= sum;
|
|
|
|
lower -= key >> ringctr;
|
|
|
|
lower -= shuf[(key >> ringctr) & 0xf];
|
|
|
|
ringctr = ringctr ? ringctr - 4 : 16;
|
|
|
|
|
|
|
|
dst[i] = ((upper << 4) & 0xf0) | (lower & 0xf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TREFile::~TREFile()
|
|
|
|
{
|
2019-07-04 18:18:03 +02:00
|
|
|
clear();
|
2019-05-10 18:56:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TREFile::init()
|
|
|
|
{
|
|
|
|
Handle hdl;
|
2019-07-16 22:08:23 +02:00
|
|
|
quint8 locked;
|
|
|
|
quint16 hdrLen;
|
|
|
|
|
|
|
|
if (!(seek(hdl, 0) && readUInt16(hdl, hdrLen)
|
|
|
|
&& seek(hdl, 0x0D) && readByte(hdl, locked)))
|
|
|
|
return false;
|
2019-05-10 18:56:19 +02:00
|
|
|
|
|
|
|
// Tile bounds
|
|
|
|
qint32 north, east, south, west;
|
|
|
|
if (!(seek(hdl, 0x15) && readInt24(hdl, north) && readInt24(hdl, east)
|
|
|
|
&& readInt24(hdl, south) && readInt24(hdl, west)))
|
|
|
|
return false;
|
|
|
|
_bounds = RectC(Coordinates(toWGS84(west), toWGS84(north)),
|
|
|
|
Coordinates(toWGS84(east), toWGS84(south)));
|
|
|
|
|
2019-07-16 22:08:23 +02:00
|
|
|
// Levels & subdivs info
|
|
|
|
quint32 levelsOffset, levelsSize, subdivSize;
|
2019-07-04 09:05:30 +02:00
|
|
|
if (!(seek(hdl, 0x21) && readUInt32(hdl, levelsOffset)
|
2019-07-16 22:08:23 +02:00
|
|
|
&& readUInt32(hdl, levelsSize) && readUInt32(hdl, _subdivOffset)
|
2019-07-04 09:05:30 +02:00
|
|
|
&& readUInt32(hdl, subdivSize)))
|
2019-05-10 18:56:19 +02:00
|
|
|
return false;
|
|
|
|
|
2019-07-16 22:08:23 +02:00
|
|
|
// TRE7 info
|
2019-05-10 18:56:19 +02:00
|
|
|
if (hdrLen > 0x9A) {
|
2019-07-16 22:08:23 +02:00
|
|
|
if (!(seek(hdl, 0x7C) && readUInt32(hdl, _extended.offset)
|
|
|
|
&& readUInt32(hdl, _extended.size)
|
|
|
|
&& readUInt16(hdl, _extended.itemSize)))
|
2019-05-10 18:56:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tile levels
|
|
|
|
if (levelsSize > 64 || !seek(hdl, levelsOffset))
|
|
|
|
return false;
|
|
|
|
quint8 levels[64];
|
|
|
|
for (quint32 i = 0; i < levelsSize; i++)
|
|
|
|
if (!readByte(hdl, levels[i]))
|
|
|
|
return false;
|
|
|
|
if (locked) {
|
|
|
|
quint32 key;
|
|
|
|
quint8 unlocked[64];
|
|
|
|
if (!seek(hdl, 0xAA) || !readUInt32(hdl, key))
|
|
|
|
return false;
|
|
|
|
unlock(unlocked, levels, levelsSize, key);
|
|
|
|
memcpy(levels, unlocked, levelsSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
quint32 levelsCount = levelsSize / 4;
|
2019-07-16 22:08:23 +02:00
|
|
|
_levels = QVector<MapLevel>(levelsCount);
|
2019-05-10 18:56:19 +02:00
|
|
|
|
|
|
|
for (quint32 i = 0; i < levelsCount; i++) {
|
|
|
|
quint8 *zoom = levels + (i * 4);
|
2019-07-16 22:08:23 +02:00
|
|
|
_levels[i].level = *zoom;
|
|
|
|
_levels[i].bits = *(zoom + 1);
|
|
|
|
_levels[i].subdivs = *(zoom + 2) | (quint16)(*(zoom + 3)) << 8;
|
|
|
|
if (_levels[i].bits > 24 || !_levels[i].subdivs)
|
2019-05-10 18:56:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-16 22:08:23 +02:00
|
|
|
// Get first non-inherited level
|
|
|
|
_firstLevel = -1;
|
|
|
|
for (int i = 0; i < _levels.size(); i++) {
|
|
|
|
if (!(_levels.at(i).level & 0x80)) {
|
|
|
|
_firstLevel = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (_firstLevel >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TREFile::load(int idx)
|
|
|
|
{
|
|
|
|
Handle hdl;
|
2019-07-18 00:26:27 +02:00
|
|
|
QList<SubDiv*> sl;
|
|
|
|
SubDiv *s = 0;
|
|
|
|
SubDivTree *tree = new SubDivTree();
|
|
|
|
|
|
|
|
|
|
|
|
_subdivs.insert(_levels.at(idx).bits, tree);
|
2019-07-16 22:08:23 +02:00
|
|
|
|
|
|
|
quint32 skip = 0;
|
|
|
|
for (int i = 0; i < idx; i++)
|
|
|
|
skip += _levels.at(i).subdivs;
|
|
|
|
|
|
|
|
if (!seek(hdl, _subdivOffset + skip * 16))
|
2019-05-10 18:56:19 +02:00
|
|
|
return false;
|
|
|
|
|
2019-07-16 22:08:23 +02:00
|
|
|
for (int j = 0; j < _levels.at(idx).subdivs; j++) {
|
|
|
|
quint32 offset;
|
|
|
|
qint32 lon, lat;
|
|
|
|
quint8 objects;
|
|
|
|
quint16 width, height, nextLevel;
|
2019-05-10 18:56:19 +02:00
|
|
|
|
2019-07-16 22:08:23 +02:00
|
|
|
if (!(readUInt24(hdl, offset) && readByte(hdl, objects)
|
|
|
|
&& readInt24(hdl, lon) && readInt24(hdl, lat)
|
|
|
|
&& readUInt16(hdl, width) && readUInt16(hdl, height)))
|
2019-07-18 00:26:27 +02:00
|
|
|
goto error;
|
2019-07-16 22:08:23 +02:00
|
|
|
if (idx != _levels.size() - 1)
|
|
|
|
if (!readUInt16(hdl, nextLevel))
|
2019-07-18 00:26:27 +02:00
|
|
|
goto error;
|
2019-07-16 22:08:23 +02:00
|
|
|
|
|
|
|
if (s)
|
|
|
|
s->setEnd(offset);
|
|
|
|
|
|
|
|
width &= 0x7FFF;
|
|
|
|
width <<= (24 - _levels.at(idx).bits);
|
|
|
|
height <<= (24 - _levels.at(idx).bits);
|
|
|
|
|
|
|
|
s = new SubDiv(offset, lon, lat, _levels.at(idx).bits, objects);
|
|
|
|
sl.append(s);
|
|
|
|
|
|
|
|
double min[2], max[2];
|
|
|
|
RectC bounds(Coordinates(toWGS84(lon - width),
|
|
|
|
toWGS84(lat + height + 1)), Coordinates(toWGS84(lon + width + 1),
|
|
|
|
toWGS84(lat - height)));
|
|
|
|
|
|
|
|
min[0] = bounds.left();
|
|
|
|
min[1] = bounds.bottom();
|
|
|
|
max[0] = bounds.right();
|
|
|
|
max[1] = bounds.top();
|
2019-07-18 00:26:27 +02:00
|
|
|
|
|
|
|
tree->Insert(min, max, s);
|
2019-07-16 22:08:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (idx != _levels.size() - 1) {
|
|
|
|
quint32 offset;
|
|
|
|
if (!readUInt24(hdl, offset))
|
2019-07-18 00:26:27 +02:00
|
|
|
goto error;
|
2019-07-16 22:08:23 +02:00
|
|
|
s->setEnd(offset);
|
2019-05-10 18:56:19 +02:00
|
|
|
}
|
|
|
|
|
2019-07-16 22:08:23 +02:00
|
|
|
|
|
|
|
// Objects with extended types (TRE7)
|
|
|
|
if (_extended.size && _extended.itemSize >= 12) {
|
|
|
|
/* Some maps skip entries for the inherited levels, some don't. Our
|
|
|
|
decision is based on the difference between the extended subdivs
|
|
|
|
count and the total subdivs count. */
|
|
|
|
quint32 totalSubdivs = 0;
|
|
|
|
for (int i = 0; i < _levels.size(); i++)
|
|
|
|
totalSubdivs += _levels.at(i).subdivs;
|
|
|
|
quint32 extendedSubdivs = _extended.size / _extended.itemSize;
|
|
|
|
quint32 diff = totalSubdivs - extendedSubdivs + 1;
|
|
|
|
|
2019-05-10 18:56:19 +02:00
|
|
|
quint32 polygons, lines, points;
|
2019-07-16 22:08:23 +02:00
|
|
|
if (!seek(hdl, _extended.offset + (skip - diff) * _extended.itemSize))
|
2019-07-18 00:26:27 +02:00
|
|
|
goto error;
|
2019-05-12 00:10:56 +02:00
|
|
|
|
2019-07-16 22:08:23 +02:00
|
|
|
for (int i = 0; i < sl.size(); i++) {
|
2019-05-10 18:56:19 +02:00
|
|
|
if (!(readUInt32(hdl, polygons) && readUInt32(hdl, lines)
|
2019-05-12 00:10:56 +02:00
|
|
|
&& readUInt32(hdl, points)))
|
2019-07-18 00:26:27 +02:00
|
|
|
goto error;
|
2019-07-16 22:08:23 +02:00
|
|
|
|
|
|
|
sl.at(i)->setExtOffsets(polygons, lines, points);
|
|
|
|
if (i)
|
2019-05-10 18:56:19 +02:00
|
|
|
sl.at(i-1)->setExtEnds(polygons, lines, points);
|
2019-07-16 22:08:23 +02:00
|
|
|
|
|
|
|
if (!seek(hdl, hdl.pos + _extended.itemSize - 12))
|
2019-07-18 00:26:27 +02:00
|
|
|
goto error;
|
2019-05-10 18:56:19 +02:00
|
|
|
}
|
|
|
|
|
2019-07-16 22:08:23 +02:00
|
|
|
if (idx != _levels.size() - 1) {
|
|
|
|
if (!(readUInt32(hdl, polygons) && readUInt32(hdl, lines)
|
|
|
|
&& readUInt32(hdl, points)))
|
2019-07-18 00:26:27 +02:00
|
|
|
goto error;
|
2019-07-16 22:08:23 +02:00
|
|
|
sl.last()->setExtEnds(polygons, lines, points);
|
|
|
|
}
|
|
|
|
}
|
2019-07-04 09:05:30 +02:00
|
|
|
|
2019-05-10 18:56:19 +02:00
|
|
|
return true;
|
2019-07-18 00:26:27 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
qDeleteAll(sl);
|
|
|
|
tree->RemoveAll();
|
|
|
|
|
|
|
|
return false;
|
2019-05-10 18:56:19 +02:00
|
|
|
}
|
|
|
|
|
2019-07-04 18:18:03 +02:00
|
|
|
void TREFile::clear()
|
|
|
|
{
|
|
|
|
SubDivTree::Iterator jt;
|
|
|
|
|
|
|
|
for (QMap<int, SubDivTree*>::iterator it = _subdivs.begin();
|
|
|
|
it != _subdivs.end(); ++it) {
|
|
|
|
SubDivTree *tree = *it;
|
|
|
|
for (tree->GetFirst(jt); !tree->IsNull(jt); tree->GetNext(jt))
|
|
|
|
delete tree->GetAt(jt);
|
|
|
|
}
|
|
|
|
|
|
|
|
qDeleteAll(_subdivs);
|
|
|
|
|
|
|
|
_subdivs.clear();
|
|
|
|
}
|
|
|
|
|
2019-07-04 09:05:30 +02:00
|
|
|
int TREFile::level(int bits)
|
|
|
|
{
|
2019-07-16 22:08:23 +02:00
|
|
|
int idx = _firstLevel;
|
2019-07-04 09:05:30 +02:00
|
|
|
|
2019-07-16 22:08:23 +02:00
|
|
|
for (int i = idx + 1; i < _levels.size(); i++) {
|
|
|
|
if (_levels.at(i).bits > bits)
|
2019-07-04 09:05:30 +02:00
|
|
|
break;
|
2019-07-16 22:08:23 +02:00
|
|
|
idx++;
|
2019-07-04 09:05:30 +02:00
|
|
|
}
|
|
|
|
|
2019-07-16 22:08:23 +02:00
|
|
|
if (!_subdivs.contains(_levels.at(idx).bits) && !load(idx))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return _levels.at(idx).bits;
|
2019-07-04 09:05:30 +02:00
|
|
|
}
|
|
|
|
|
2019-05-10 18:56:19 +02:00
|
|
|
static bool cb(SubDiv *subdiv, void *context)
|
|
|
|
{
|
|
|
|
QList<SubDiv*> *list = (QList<SubDiv*>*)context;
|
|
|
|
list->append(subdiv);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-04 09:05:30 +02:00
|
|
|
QList<SubDiv*> TREFile::subdivs(const RectC &rect, int bits)
|
2019-05-10 18:56:19 +02:00
|
|
|
{
|
|
|
|
QList<SubDiv*> list;
|
2019-07-04 09:05:30 +02:00
|
|
|
SubDivTree *tree = _subdivs.value(level(bits));
|
2019-05-10 18:56:19 +02:00
|
|
|
double min[2], max[2];
|
|
|
|
|
|
|
|
min[0] = rect.left();
|
|
|
|
min[1] = rect.bottom();
|
|
|
|
max[0] = rect.right();
|
|
|
|
max[1] = rect.top();
|
|
|
|
|
2019-07-04 09:05:30 +02:00
|
|
|
if (tree)
|
|
|
|
tree->Search(min, max, cb, &list);
|
2019-05-10 18:56:19 +02:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2019-07-16 22:08:23 +02:00
|
|
|
|
|
|
|
#ifndef QT_NO_DEBUG
|
|
|
|
QDebug operator<<(QDebug dbg, const TREFile::MapLevel &level)
|
|
|
|
{
|
|
|
|
dbg.nospace() << "MapLevel(" << level.level << ", " << level.bits << ", "
|
|
|
|
<< level.subdivs << ")";
|
|
|
|
return dbg.space();
|
|
|
|
}
|
|
|
|
#endif // QT_NO_DEBUG
|