1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qtexturefilereader_p.h"
5
6#include "qpkmhandler_p.h"
7#include "qktxhandler_p.h"
8#include "qastchandler_p.h"
9
10#include <QFileInfo>
11
12QT_BEGIN_NAMESPACE
13
14QTextureFileHandler::~QTextureFileHandler() = default;
15
16QTextureFileReader::QTextureFileReader(QIODevice *device, const QString &fileName)
17 : m_device(device), m_fileName(fileName)
18{
19}
20
21QTextureFileReader::~QTextureFileReader()
22{
23 delete m_handler;
24}
25
26QTextureFileData QTextureFileReader::read()
27{
28 if (!canRead())
29 return QTextureFileData();
30 return m_handler->read();
31}
32
33bool QTextureFileReader::canRead()
34{
35 if (!checked) {
36 checked = true;
37 if (!init())
38 return false;
39
40 QByteArray headerBlock = m_device->peek(maxlen: 64);
41 QFileInfo fi(m_fileName);
42 QByteArray suffix = fi.suffix().toLower().toLatin1();
43 QByteArray logName = fi.fileName().toUtf8();
44
45 // Currently the handlers are hardcoded; later maybe a list of plugins
46 if (QPkmHandler::canRead(suffix, block: headerBlock)) {
47 m_handler = new QPkmHandler(m_device, logName);
48 } else if (QKtxHandler::canRead(suffix, block: headerBlock)) {
49 m_handler = new QKtxHandler(m_device, logName);
50 } else if (QAstcHandler::canRead(suffix, block: headerBlock)) {
51 m_handler = new QAstcHandler(m_device, logName);
52 }
53 // else if OtherHandler::canRead() ...etc.
54 }
55 return (m_handler != nullptr);
56}
57
58QList<QByteArray> QTextureFileReader::supportedFileFormats()
59{
60 // Hardcoded for now
61 return {QByteArrayLiteral("pkm"), QByteArrayLiteral("ktx"), QByteArrayLiteral("astc")};
62}
63
64bool QTextureFileReader::init()
65{
66 if (!m_device)
67 return false;
68 return m_device->isReadable();
69}
70
71QT_END_NAMESPACE
72

source code of qtbase/src/gui/util/qtexturefilereader.cpp