From 731f2d7e6db109bc0d853f2d16fd7185d4a90d06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Wed, 3 Feb 2021 22:16:00 +0100 Subject: [PATCH] Added an optimized subfile block read --- src/map/IMG/huffmanbuffer.cpp | 6 +----- src/map/IMG/lblfile.cpp | 5 ++--- src/map/IMG/subfile.cpp | 26 +++++++++++++++++++++++++- src/map/IMG/subfile.h | 2 ++ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/map/IMG/huffmanbuffer.cpp b/src/map/IMG/huffmanbuffer.cpp index e00b51c3..a53c687b 100644 --- a/src/map/IMG/huffmanbuffer.cpp +++ b/src/map/IMG/huffmanbuffer.cpp @@ -16,9 +16,5 @@ bool HuffmanBuffer::load(const RGNFile *rgn, SubFile::Handle &rgnHdl) }; resize(recordSize); - for (int i = 0; i < QByteArray::size(); i++) - if (!rgn->readByte(rgnHdl, (quint8*)(data() + i))) - return false; - - return true; + return rgn->read(rgnHdl, (quint8*)data(), recordSize); } diff --git a/src/map/IMG/lblfile.cpp b/src/map/IMG/lblfile.cpp index 10d748d7..0555aa3d 100644 --- a/src/map/IMG/lblfile.cpp +++ b/src/map/IMG/lblfile.cpp @@ -338,9 +338,8 @@ QImage LBLFile::readImage(Handle &hdl, quint32 id) const return QImage(); QByteArray ba; ba.resize(_rasters.at(id).size); - for (int i = 0; i < ba.size(); i++) - if (!readByte(hdl, (quint8*)(ba.data() + i))) - return QImage(); + if (!read(hdl, (quint8*)ba.data(), _rasters.at(id).size)) + return QImage(); return QImage::fromData(ba); } diff --git a/src/map/IMG/subfile.cpp b/src/map/IMG/subfile.cpp index 865c190d..ce1d1373 100644 --- a/src/map/IMG/subfile.cpp +++ b/src/map/IMG/subfile.cpp @@ -1,4 +1,4 @@ -#include +#include #include "img.h" #include "subfile.h" @@ -115,3 +115,27 @@ bool SubFile::readVBitfield32(Handle &hdl, quint32 &bitfield) const return true; } + +bool SubFile::read(Handle &handle, quint8 *buff, quint32 size) const +{ + while (size) { + quint32 remaining = handle._data.size() - handle._blockPos; + if (size < remaining) { + memcpy(buff, handle._data.constData() + handle._blockPos, size); + handle._blockPos += size; + handle._pos++; + return true; + } else { + memcpy(buff, handle._data.constData() + handle._blockPos, + remaining); + buff += remaining; + size -= remaining; + handle._blockPos = 0; + handle._pos += remaining; + if (!seek(handle, handle._pos)) + return false; + } + } + + return true; +} diff --git a/src/map/IMG/subfile.h b/src/map/IMG/subfile.h index 748fba82..cc86ba63 100644 --- a/src/map/IMG/subfile.h +++ b/src/map/IMG/subfile.h @@ -60,6 +60,8 @@ public: bool seek(Handle &handle, quint32 pos) const; quint32 pos(Handle &handle) const {return handle._pos;} + bool read(Handle &handle, quint8 *buff, quint32 size) const; + bool readByte(Handle &handle, quint8 *val) const { *val = handle._data.at(handle._blockPos++);