Simplified read/verify header process

Where possible, QIODevice::peek has been used instead of transactions or instead of using ungetchar() for sequential access devices and seek() for random access devices.

Furthermore:
- RAS format gained the ability of read on sequential devices.
- Removed unused code in XCF (still related to ungetchar and sequential devices).
- These changes should prevent errors like the ones fixed by MR !258
This commit is contained in:
Mirco Miranda
2024-10-06 17:26:25 +00:00
committed by Albert Astals Cid
parent fee0165bef
commit 97120b2537
7 changed files with 51 additions and 180 deletions

View File

@ -195,26 +195,13 @@ static QImage::Format imageFormat(const TgaHeader &head)
*/
static bool peekHeader(QIODevice *device, TgaHeader &header)
{
qint64 oldPos = device->pos();
QByteArray head = device->read(TgaHeader::SIZE);
int readBytes = head.size();
if (device->isSequential()) {
for (int pos = readBytes - 1; pos >= 0; --pos) {
device->ungetChar(head[pos]);
}
} else {
device->seek(oldPos);
}
if (readBytes < TgaHeader::SIZE) {
auto head = device->peek(TgaHeader::SIZE);
if (head.size() < TgaHeader::SIZE) {
return false;
}
QDataStream stream(head);
stream.setByteOrder(QDataStream::LittleEndian);
stream >> header;
return true;
}
@ -561,22 +548,6 @@ bool TGAHandler::canRead(QIODevice *device)
return false;
}
qint64 oldPos = device->pos();
QByteArray head = device->read(TgaHeader::SIZE);
int readBytes = head.size();
if (device->isSequential()) {
for (int pos = readBytes - 1; pos >= 0; --pos) {
device->ungetChar(head[pos]);
}
} else {
device->seek(oldPos);
}
if (readBytes < TgaHeader::SIZE) {
return false;
}
TgaHeader tga;
if (!peekHeader(device, tga)) {
qWarning("TGAHandler::canRead() error while reading the header");