From d2a12713488261ff8870291b7dbfff40253bf677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Mon, 1 Feb 2021 20:06:05 +0100 Subject: [PATCH] Optimization --- src/common/util.h | 2 +- src/map/IMG/bitstream.h | 2 +- src/map/IMG/huffmanbuffer.cpp | 2 +- src/map/IMG/lblfile.cpp | 16 ++++++------ src/map/IMG/netfile.cpp | 4 +-- src/map/IMG/nodfile.cpp | 6 ++--- src/map/IMG/rgnfile.cpp | 22 ++++++++-------- src/map/IMG/style.cpp | 48 +++++++++++++++++------------------ src/map/IMG/subfile.cpp | 6 ++--- src/map/IMG/subfile.h | 31 +++++++++++----------- src/map/IMG/trefile.cpp | 4 +-- 11 files changed, 71 insertions(+), 72 deletions(-) diff --git a/src/common/util.h b/src/common/util.h index 62cf69c8..ccd6ca1c 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -5,7 +5,7 @@ namespace Util { - int str2int(const char *str, int len); + int str2int(const char *str, int len); double niceNum(double x, bool round); QString file2name(const QString &path); } diff --git a/src/map/IMG/bitstream.h b/src/map/IMG/bitstream.h index d307c71c..ffe57d20 100644 --- a/src/map/IMG/bitstream.h +++ b/src/map/IMG/bitstream.h @@ -77,7 +77,7 @@ bool BitStream1::read(int bits, T &val) for (int pos = 0; pos < bits; ) { if (!_remaining) { - if (!_length || !_file.readUInt8(_hdl, _data)) + if (!_length || !_file.readByte(_hdl, &_data)) return false; _remaining = 8; _length--; diff --git a/src/map/IMG/huffmanbuffer.cpp b/src/map/IMG/huffmanbuffer.cpp index 24378d0e..e00b51c3 100644 --- a/src/map/IMG/huffmanbuffer.cpp +++ b/src/map/IMG/huffmanbuffer.cpp @@ -17,7 +17,7 @@ bool HuffmanBuffer::load(const RGNFile *rgn, SubFile::Handle &rgnHdl) resize(recordSize); for (int i = 0; i < QByteArray::size(); i++) - if (!rgn->readUInt8(rgnHdl, *((quint8*)(data() + i)))) + if (!rgn->readByte(rgnHdl, (quint8*)(data() + i))) return false; return true; diff --git a/src/map/IMG/lblfile.cpp b/src/map/IMG/lblfile.cpp index f4c3e96a..10d748d7 100644 --- a/src/map/IMG/lblfile.cpp +++ b/src/map/IMG/lblfile.cpp @@ -59,7 +59,7 @@ static QString capitalized(const QString &str) LBLFile::~LBLFile() { delete _huffmanText; - delete[] _table; + delete[] _table; } bool LBLFile::load(Handle &hdl, const RGNFile *rgn, Handle &rgnHdl) @@ -68,10 +68,10 @@ bool LBLFile::load(Handle &hdl, const RGNFile *rgn, Handle &rgnHdl) if (!(seek(hdl, _gmpOffset) && readUInt16(hdl, hdrLen) && seek(hdl, _gmpOffset + 0x15) && readUInt32(hdl, _offset) - && readUInt32(hdl, _size) && readUInt8(hdl, _multiplier) - && readUInt8(hdl, _encoding) && seek(hdl, _gmpOffset + 0x57) + && readUInt32(hdl, _size) && readByte(hdl, &_multiplier) + && readByte(hdl, &_encoding) && seek(hdl, _gmpOffset + 0x57) && readUInt32(hdl, _poiOffset) && readUInt32(hdl, _poiSize) - && readUInt8(hdl, _poiMultiplier) && seek(hdl, _gmpOffset + 0xAA) + && readByte(hdl, &_poiMultiplier) && seek(hdl, _gmpOffset + 0xAA) && readUInt16(hdl, codepage))) return false; @@ -127,7 +127,7 @@ bool LBLFile::load(Handle &hdl, const RGNFile *rgn, Handle &rgnHdl) void LBLFile::clear() { delete _huffmanText; - delete[] _table; + delete[] _table; _huffmanText = 0; _table = 0; } @@ -144,7 +144,7 @@ Label LBLFile::label6b(Handle &hdl, quint32 offset, bool capitalize) const return Label(); while (true) { - if (!(readUInt8(hdl, b1) && readUInt8(hdl, b2) && readUInt8(hdl, b3))) + if (!(readByte(hdl, &b1) && readByte(hdl, &b2) && readByte(hdl, &b3))) return Label(); int c[]= {b1>>2, (b1&0x3)<<4|b2>>4, (b2&0xF)<<2|b3>>6, b3&0x3F}; @@ -228,7 +228,7 @@ Label LBLFile::label8b(Handle &hdl, quint32 offset, bool capitalize) const return Label(); do { - if (!readUInt8(hdl, c)) + if (!readByte(hdl, &c)) return Label(); str.append(c); } while (c); @@ -339,7 +339,7 @@ QImage LBLFile::readImage(Handle &hdl, quint32 id) const QByteArray ba; ba.resize(_rasters.at(id).size); for (int i = 0; i < ba.size(); i++) - if (!readUInt8(hdl, *(ba.data() + i))) + if (!readByte(hdl, (quint8*)(ba.data() + i))) return QImage(); return QImage::fromData(ba); diff --git a/src/map/IMG/netfile.cpp b/src/map/IMG/netfile.cpp index df27a1ff..d42e2ade 100644 --- a/src/map/IMG/netfile.cpp +++ b/src/map/IMG/netfile.cpp @@ -378,7 +378,7 @@ bool NETFile::load(Handle &hdl, const RGNFile *rgn, Handle &rgnHdl) if (!(seek(hdl, _gmpOffset) && readUInt16(hdl, hdrLen) && seek(hdl, _gmpOffset + 0x15) && readUInt32(hdl, _offset) - && readUInt32(hdl, _size) && readUInt8(hdl, _shift))) + && readUInt32(hdl, _size) && readByte(hdl, &_shift))) return false; if (hdrLen >= 0x47) { @@ -386,7 +386,7 @@ bool NETFile::load(Handle &hdl, const RGNFile *rgn, Handle &rgnHdl) if (!(seek(hdl, _gmpOffset + 0x37) && readUInt32(hdl, info))) return false; if (!(seek(hdl, _gmpOffset + 0x43) && readUInt32(hdl, _linksOffset) - && readUInt32(hdl, _linksSize) && readUInt8(hdl, _linksShift))) + && readUInt32(hdl, _linksSize) && readByte(hdl, &_linksShift))) return false; quint8 tableId = ((info >> 2) & 0xF); diff --git a/src/map/IMG/nodfile.cpp b/src/map/IMG/nodfile.cpp index e9fa019f..d2a3f145 100644 --- a/src/map/IMG/nodfile.cpp +++ b/src/map/IMG/nodfile.cpp @@ -92,7 +92,7 @@ bool NODFile::load(Handle &hdl) return true; if (!(seek(hdl, _gmpOffset + 0x1d) && readUInt32(hdl, _flags) - && readUInt8(hdl, _blockShift) && readUInt8(hdl, _nodeShift))) + && readByte(hdl, &_blockShift) && readByte(hdl, &_nodeShift))) return false; if (!(seek(hdl, _gmpOffset + 0x67) && readUInt32(hdl, _blockOffset) @@ -122,8 +122,8 @@ bool NODFile::readBlock(Handle &hdl, quint32 blockOffset, if (!(seek(hdl, blockInfo.offset + _blockOffset) && readUInt16(hdl, blockInfo.hdr.s0) && readUInt32(hdl, blockInfo.hdr.s2) && readUInt32(hdl, blockInfo.hdr.s6) && readUInt32(hdl, blockInfo.hdr.sa) - && readUInt16(hdl, blockInfo.hdr.se) && readUInt8(hdl, blockInfo.hdr.s10) - && readUInt8(hdl, blockInfo.hdr.s11) && readUInt8(hdl, blockInfo.hdr.s12))) + && readUInt16(hdl, blockInfo.hdr.se) && readByte(hdl, &blockInfo.hdr.s10) + && readByte(hdl, &blockInfo.hdr.s11) && readByte(hdl, &blockInfo.hdr.s12))) return false; return true; diff --git a/src/map/IMG/rgnfile.cpp b/src/map/IMG/rgnfile.cpp index ac5f60c6..5bafc0de 100644 --- a/src/map/IMG/rgnfile.cpp +++ b/src/map/IMG/rgnfile.cpp @@ -36,7 +36,7 @@ bool RGNFile::readClassFields(Handle &hdl, SegmentType segmentType, quint8 flags; quint32 rs; - if (!readUInt8(hdl, flags)) + if (!readByte(hdl, &flags)) return false; switch (flags >> 5) { @@ -189,18 +189,18 @@ bool RGNFile::polyObjects(Handle &hdl, const SubDiv *subdiv, while (pos(hdl) < segment.end()) { MapData::Poly poly; - if (!(readUInt8(hdl, type) && readUInt24(hdl, labelPtr) + if (!(readByte(hdl, &type) && readUInt24(hdl, labelPtr) && readInt16(hdl, lon) && readInt16(hdl, lat))) return false; if (type & 0x80) { if (!readUInt16(hdl, len)) return false; } else { - if (!readUInt8(hdl, len8)) + if (!readByte(hdl, &len8)) return false; len = len8; } - if (!readUInt8(hdl, bitstreamInfo)) + if (!readByte(hdl, &bitstreamInfo)) return false; poly.type = (segmentType == Polygon) @@ -265,7 +265,7 @@ bool RGNFile::extPolyObjects(Handle &hdl, const SubDiv *subdiv, quint32 shift, MapData::Poly poly; QPoint pos; - if (!(readUInt8(hdl, type) && readUInt8(hdl, subtype) + if (!(readByte(hdl, &type) && readByte(hdl, &subtype) && readInt16(hdl, lon) && readInt16(hdl, lat) && readVUInt32(hdl, len))) return false; @@ -315,7 +315,7 @@ bool RGNFile::extPolyObjects(Handle &hdl, const SubDiv *subdiv, quint32 shift, poly.points.append(QPointF(c.lon(), c.lat())); quint8 bitstreamInfo; - if (!readUInt8(hdl, bitstreamInfo)) + if (!readByte(hdl, &bitstreamInfo)) return false; qint32 lonDelta, latDelta; @@ -375,11 +375,11 @@ bool RGNFile::pointObjects(Handle &hdl, const SubDiv *subdiv, qint16 lon, lat; quint32 labelPtr; - if (!(readUInt8(hdl, type) && readUInt24(hdl, labelPtr) + if (!(readByte(hdl, &type) && readUInt24(hdl, labelPtr) && readInt16(hdl, lon) && readInt16(hdl, lat))) return false; if (labelPtr & 0x800000) { - if (!readUInt8(hdl, subtype)) + if (!readByte(hdl, &subtype)) return false; } else subtype = 0; @@ -418,7 +418,7 @@ bool RGNFile::extPointObjects(Handle &hdl, const SubDiv *subdiv, quint8 type, subtype; quint32 labelPtr = 0; - if (!(readUInt8(hdl, type) && readUInt8(hdl, subtype) + if (!(readByte(hdl, &type) && readByte(hdl, &subtype) && readInt16(hdl, lon) && readInt16(hdl, lat))) return false; @@ -472,7 +472,7 @@ bool RGNFile::links(Handle &hdl, const SubDiv *subdiv, quint32 shift, quint32 entryStart = pos(hdl); - if (!(readUInt8(hdl, flags) && readVUInt32(hdl, nod->indexIdSize(), + if (!(readByte(hdl, &flags) && readVUInt32(hdl, nod->indexIdSize(), blockIndexId))) return false; @@ -514,7 +514,7 @@ bool RGNFile::links(Handle &hdl, const SubDiv *subdiv, quint32 shift, Q_ASSERT(lineId > 4); } } else { - if (!readUInt8(hdl, linkId)) + if (!readByte(hdl, &linkId)) return false; lineId = 0; } diff --git a/src/map/IMG/style.cpp b/src/map/IMG/style.cpp index 4fd449d4..1b852626 100644 --- a/src/map/IMG/style.cpp +++ b/src/map/IMG/style.cpp @@ -461,7 +461,7 @@ static bool readBitmap(SubFile *file, SubFile::Handle &hdl, QImage &img, for (int y = 0; y < img.height(); y++) { for (int x = 0; x < img.width(); x += 8/bpp) { quint8 color; - if (!file->readUInt8(hdl, color)) + if (!file->readByte(hdl, &color)) return false; for (int i = 0; i < 8/bpp && x + i < img.width(); i++) { @@ -486,8 +486,8 @@ static bool readColor(SubFile *file, SubFile::Handle &hdl, QColor &color) { quint8 b, g, r; - if (!(file->readUInt8(hdl, b) && file->readUInt8(hdl, g) - && file->readUInt8(hdl, r))) + if (!(file->readByte(hdl, &b) && file->readByte(hdl, &g) + && file->readByte(hdl, &r))) return false; color = qRgb(r, g, b); @@ -516,7 +516,7 @@ bool Style::itemInfo(SubFile *file, SubFile::Handle &hdl, if (section.arrayItemSize == 5) { if (!(file->readUInt16(hdl, t16_1) && file->readUInt16(hdl, t16_2) - && file->readUInt8(hdl, t8))) + && file->readByte(hdl, &t8))) return false; info.offset = t16_2 | (t8<<16); } else if (section.arrayItemSize == 4) { @@ -524,7 +524,7 @@ bool Style::itemInfo(SubFile *file, SubFile::Handle &hdl, return false; info.offset = t16_2; } else if (section.arrayItemSize == 3) { - if (!(file->readUInt16(hdl, t16_1) && file->readUInt8(hdl, t8))) + if (!(file->readUInt16(hdl, t16_1) && file->readByte(hdl, &t8))) return false; info.offset = t8; } else @@ -555,7 +555,7 @@ bool Style::parsePolygons(SubFile *file, SubFile::Handle &hdl, quint8 t8, flags; if (!(file->seek(hdl, section.offset + info.offset) - && file->readUInt8(hdl, t8))) + && file->readByte(hdl, &t8))) return false; flags = t8 & 0x0F; @@ -671,7 +671,7 @@ bool Style::parseLines(SubFile *file, SubFile::Handle &hdl, quint8 t8_1, t8_2, flags, rows; if (!(file->seek(hdl, section.offset + info.offset) - && file->readUInt8(hdl, t8_1) && file->readUInt8(hdl, t8_2))) + && file->readByte(hdl, &t8_1) && file->readByte(hdl, &t8_2))) return false; flags = t8_1 & 0x07; rows = t8_1 >> 3; @@ -696,7 +696,7 @@ bool Style::parseLines(SubFile *file, SubFile::Handle &hdl, _lines[type] = Line(img); } else { - if (!(file->readUInt8(hdl, w1) && file->readUInt8(hdl, w2))) + if (!(file->readByte(hdl, &w1) && file->readByte(hdl, &w2))) return false; _lines[type] = (w2 > w1) @@ -723,7 +723,7 @@ bool Style::parseLines(SubFile *file, SubFile::Handle &hdl, _lines[type] = Line(img); } else { - if (!(file->readUInt8(hdl, w1) && file->readUInt8(hdl, w2))) + if (!(file->readByte(hdl, &w1) && file->readByte(hdl, &w2))) return false; _lines[type] = (w2 > w1) @@ -750,7 +750,7 @@ bool Style::parseLines(SubFile *file, SubFile::Handle &hdl, _lines[type] = Line(img); } else { - if (!(file->readUInt8(hdl, w1) && file->readUInt8(hdl, w2))) + if (!(file->readByte(hdl, &w1) && file->readByte(hdl, &w2))) return false; _lines[type] = Line(QPen(c1, w1, Qt::SolidLine, @@ -773,7 +773,7 @@ bool Style::parseLines(SubFile *file, SubFile::Handle &hdl, _lines[type] = Line(img); } else { - if (!(file->readUInt8(hdl, w1) && file->readUInt8(hdl, w2))) + if (!(file->readByte(hdl, &w1) && file->readByte(hdl, &w2))) return false; _lines[type] = (w2 > w1) @@ -799,7 +799,7 @@ bool Style::parseLines(SubFile *file, SubFile::Handle &hdl, _lines[type] = Line(img); } else { - if (!file->readUInt8(hdl, w1)) + if (!file->readByte(hdl, &w1)) return false; _lines[type] = Line(QPen(c1, w1, Qt::SolidLine, @@ -821,7 +821,7 @@ bool Style::parseLines(SubFile *file, SubFile::Handle &hdl, _lines[type] = Line(img); } else { - if (!file->readUInt8(hdl, w1)) + if (!file->readByte(hdl, &w1)) return false; _lines[type] = Line(QPen(c1, w1, Qt::SolidLine, @@ -844,7 +844,7 @@ bool Style::parseLines(SubFile *file, SubFile::Handle &hdl, if (fontInfo) { quint8 labelFlags; - if (!file->readUInt8(hdl, labelFlags)) + if (!file->readByte(hdl, &labelFlags)) return false; if (labelFlags & 0x08) { if (!readColor(file, hdl, c1)) @@ -906,7 +906,7 @@ static bool readColorTable(SubFile *file, SubFile::Handle &hdl, QImage& img, for (int i = 0; i < colors; i++) { while (bits < 28) { - if (!file->readUInt8(hdl, byte)) + if (!file->readByte(hdl, &byte)) return false; mask = 0x000000FF << bits; @@ -956,9 +956,9 @@ bool Style::parsePoints(SubFile *file, SubFile::Handle &hdl, quint8 t8_1, width, height, numColors, imgType; if (!(file->seek(hdl, section.offset + info.offset) - && file->readUInt8(hdl, t8_1) && file->readUInt8(hdl, width) - && file->readUInt8(hdl, height) && file->readUInt8(hdl, numColors) - && file->readUInt8(hdl, imgType))) + && file->readByte(hdl, &t8_1) && file->readByte(hdl, &width) + && file->readByte(hdl, &height) && file->readByte(hdl, &numColors) + && file->readByte(hdl, &imgType))) return false; bool label = t8_1 & 0x04; @@ -975,8 +975,8 @@ bool Style::parsePoints(SubFile *file, SubFile::Handle &hdl, _points[type] = Point(img); if (t8_1 == 0x03) { - if (!(file->readUInt8(hdl, numColors) - && file->readUInt8(hdl, imgType))) + if (!(file->readByte(hdl, &numColors) + && file->readByte(hdl, &imgType))) return false; if ((bpp = colors2bpp(numColors, imgType)) < 0) continue; @@ -985,8 +985,8 @@ bool Style::parsePoints(SubFile *file, SubFile::Handle &hdl, if (!readBitmap(file, hdl, img, bpp)) return false; } else if (t8_1 == 0x02) { - if (!(file->readUInt8(hdl, numColors) - && file->readUInt8(hdl, imgType))) + if (!(file->readByte(hdl, &numColors) + && file->readByte(hdl, &imgType))) return false; if ((bpp = colors2bpp(numColors, imgType)) < 0) continue; @@ -1000,7 +1000,7 @@ bool Style::parsePoints(SubFile *file, SubFile::Handle &hdl, if (fontInfo) { quint8 labelFlags; QColor color; - if (!file->readUInt8(hdl, labelFlags)) + if (!file->readByte(hdl, &labelFlags)) return false; if (labelFlags & 0x08) { if (!readColor(file, hdl, color)) @@ -1029,7 +1029,7 @@ bool Style::parseDrawOrder(SubFile *file, SubFile::Handle &hdl, quint8 type; quint32 subtype; - if (!(file->readUInt8(hdl, type) && file->readUInt32(hdl, subtype))) + if (!(file->readByte(hdl, &type) && file->readUInt32(hdl, subtype))) return false; if (!subtype) diff --git a/src/map/IMG/subfile.cpp b/src/map/IMG/subfile.cpp index 12fd33a5..865c190d 100644 --- a/src/map/IMG/subfile.cpp +++ b/src/map/IMG/subfile.cpp @@ -44,7 +44,7 @@ bool SubFile::readVUInt32(Handle &hdl, quint32 &val) const { quint8 bytes, shift, b; - if (!readByte(hdl, b)) + if (!readByte(hdl, &b)) return false; if ((b & 1) == 0) { @@ -63,7 +63,7 @@ bool SubFile::readVUInt32(Handle &hdl, quint32 &val) const val = b >> (8 - shift); for (int i = 1; i <= bytes; i++) { - if (!readByte(hdl, b)) + if (!readByte(hdl, &b)) return false; val |= (((quint32)b) << (i * 8)) >> (8 - shift); } @@ -91,7 +91,7 @@ bool SubFile::readVBitfield32(Handle &hdl, quint32 &bitfield) const { quint8 bits; - if (!readUInt8(hdl, bits)) + if (!readByte(hdl, &bits)) return false; if (!(bits & 1)) { diff --git a/src/map/IMG/subfile.h b/src/map/IMG/subfile.h index f528d2eb..748fba82 100644 --- a/src/map/IMG/subfile.h +++ b/src/map/IMG/subfile.h @@ -60,11 +60,19 @@ public: bool seek(Handle &handle, quint32 pos) const; quint32 pos(Handle &handle) const {return handle._pos;} + bool readByte(Handle &handle, quint8 *val) const + { + *val = handle._data.at(handle._blockPos++); + handle._pos++; + return (handle._blockPos >= handle._data.size()) + ? seek(handle, handle._pos) : true; + } + template bool readUInt8(Handle &handle, T &val) const { quint8 b; - if (!readByte(handle, b)) + if (!readByte(handle, &b)) return false; val = b; return true; @@ -74,7 +82,7 @@ public: bool readUInt16(Handle &handle, T &val) const { quint8 b0, b1; - if (!(readByte(handle, b0) && readByte(handle, b1))) + if (!(readByte(handle, &b0) && readByte(handle, &b1))) return false; val = b0 | ((quint16)b1) << 8; return true; @@ -92,8 +100,8 @@ public: bool readUInt24(Handle &handle, quint32 &val) const { quint8 b0, b1, b2; - if (!(readByte(handle, b0) && readByte(handle, b1) - && readByte(handle, b2))) + if (!(readByte(handle, &b0) && readByte(handle, &b1) + && readByte(handle, &b2))) return false; val = b0 | ((quint32)b1) << 8 | ((quint32)b2) << 16; return true; @@ -111,8 +119,8 @@ public: bool readUInt32(Handle &handle, quint32 &val) const { quint8 b0, b1, b2, b3; - if (!(readByte(handle, b0) && readByte(handle, b1) - && readByte(handle, b2) && readByte(handle, b3))) + if (!(readByte(handle, &b0) && readByte(handle, &b1) + && readByte(handle, &b2) && readByte(handle, &b3))) return false; val = b0 | ((quint32)b1) << 8 | ((quint32)b2) << 16 | ((quint32)b3) << 24; @@ -125,7 +133,7 @@ public: val = 0; for (quint32 i = bytes; i; i--) { - if (!readByte(hdl, b)) + if (!readByte(hdl, &b)) return false; val |= ((quint32)b) << ((i-1) * 8); } @@ -143,15 +151,6 @@ protected: quint32 _gmpOffset; private: - bool readByte(Handle &handle, quint8 &val) const - { - int blockSize = _img ? 1U<<_img->blockBits() : 1U<= blockSize) - ? seek(handle, handle._pos) : true; - } - IMG *_img; QVector *_blocks; const QString *_path; diff --git a/src/map/IMG/trefile.cpp b/src/map/IMG/trefile.cpp index ad3e46e5..2c61a9ab 100644 --- a/src/map/IMG/trefile.cpp +++ b/src/map/IMG/trefile.cpp @@ -55,7 +55,7 @@ bool TREFile::init() if (!(seek(hdl, _gmpOffset) && readUInt16(hdl, hdrLen) - && seek(hdl, _gmpOffset + 0x0D) && readUInt8(hdl, locked))) + && seek(hdl, _gmpOffset + 0x0D) && readByte(hdl, &locked))) return false; // Tile bounds @@ -93,7 +93,7 @@ bool TREFile::init() return false; quint8 levels[64]; for (quint32 i = 0; i < levelsSize; i++) - if (!readUInt8(hdl, levels[i])) + if (!readByte(hdl, &levels[i])) return false; if (locked) { quint32 key;