Coverage Report

Created: 2026-06-14 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/kimageformats/src/imageformats/kra.cpp
Line
Count
Source
1
/*
2
    This file is part of the KDE project
3
    SPDX-FileCopyrightText: 2013 Boudewijn Rempt <boud@valdyas.org>
4
5
    SPDX-License-Identifier: LGPL-2.0-or-later
6
7
    This code is based on Thacher Ulrich PSD loading code released
8
    on public domain. See: http://tulrich.com/geekstuff/
9
*/
10
11
#include "kra.h"
12
13
#include <kzip.h>
14
15
#include <QFile>
16
#include <QIODevice>
17
#include <QImage>
18
19
static constexpr char s_magic[] = "application/x-krita";
20
static constexpr int s_magic_size = sizeof(s_magic) - 1; // -1 to remove the last \0
21
22
KraHandler::KraHandler()
23
8.27k
{
24
8.27k
}
25
26
bool KraHandler::canRead() const
27
8.27k
{
28
8.27k
    if (canRead(device())) {
29
8
        setFormat("kra");
30
8
        return true;
31
8
    }
32
8.26k
    return false;
33
8.27k
}
34
35
bool KraHandler::read(QImage *image)
36
8.27k
{
37
8.27k
    KZip zip(device());
38
8.27k
    if (!zip.open(QIODevice::ReadOnly)) {
39
3.42k
        return false;
40
3.42k
    }
41
42
4.84k
    const KArchiveEntry *entry = zip.directory()->entry(QStringLiteral("mergedimage.png"));
43
4.84k
    if (!entry || !entry->isFile()) {
44
38
        return false;
45
38
    }
46
47
4.80k
    const KZipFileEntry *fileZipEntry = static_cast<const KZipFileEntry *>(entry);
48
49
4.80k
    image->loadFromData(fileZipEntry->data(), "PNG");
50
51
4.80k
    return true;
52
4.84k
}
53
54
bool KraHandler::canRead(QIODevice *device)
55
8.27k
{
56
8.27k
    if (!device) {
57
0
        qWarning("KraHandler::canRead() called with no device");
58
0
        return false;
59
0
    }
60
8.27k
    if (device->isSequential()) {
61
0
        return false;
62
0
    }
63
64
8.27k
    char buff[57];
65
8.27k
    if (device->peek(buff, sizeof(buff)) == sizeof(buff)) {
66
7.30k
        return memcmp(buff + 0x26, s_magic, s_magic_size) == 0;
67
7.30k
    }
68
69
969
    return false;
70
8.27k
}
71
72
QImageIOPlugin::Capabilities KraPlugin::capabilities(QIODevice *device, const QByteArray &format) const
73
0
{
74
0
    if (format == "kra" || format == "KRA") {
75
0
        return Capabilities(CanRead);
76
0
    }
77
0
    if (!format.isEmpty()) {
78
0
        return {};
79
0
    }
80
0
    if (!device->isOpen()) {
81
0
        return {};
82
0
    }
83
84
0
    Capabilities cap;
85
0
    if (device->isReadable() && KraHandler::canRead(device)) {
86
0
        cap |= CanRead;
87
0
    }
88
0
    return cap;
89
0
}
90
91
QImageIOHandler *KraPlugin::create(QIODevice *device, const QByteArray &format) const
92
0
{
93
0
    QImageIOHandler *handler = new KraHandler;
94
0
    handler->setDevice(device);
95
0
    handler->setFormat(format);
96
0
    return handler;
97
0
}
98
99
#include "moc_kra.cpp"