summaryrefslogtreecommitdiff
path: root/src/plugins/qmldesigner/utils/imageutils.cpp
blob: ccfa5bfe42172fee6626bb68dcb356df63abe3db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "imageutils.h"

#include "ktximage.h"

#include <QFile>
#include <QFileInfo>
#include <QImageReader>

namespace QmlDesigner {

QString QmlDesigner::ImageUtils::imageInfo(const QString &path)
{
    QFileInfo info(path);
    if (!info.exists())
        return {};

    int width = 0;
    int height = 0;
    const QString suffix = info.suffix();
    if (suffix == "hdr") {
        QFile file(path);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return {};

        while (!file.atEnd()) {
            QByteArray line = file.readLine();
            if (sscanf(line.constData(), "-Y %d +X %d", &height, &width))
                break;
        }
    } else if (suffix == "ktx") {
        KtxImage ktx(path);
        width = ktx.dimensions().width();
        height = ktx.dimensions().height();
    } else {
        QSize size = QImageReader(path).size();
        width = size.width();
        height = size.height();
    }

    if (width == 0 && height == 0)
        return {};

    return QLatin1String("%1 x %2\n%3 (%4)")
            .arg(QString::number(width),
                 QString::number(height),
                 QLocale::system().formattedDataSize(info.size(), 2, QLocale::DataSizeTraditionalFormat),
                 info.suffix());
}

} // namespace QmlDesigner