KImageFormats

kra.cpp
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
19static constexpr char s_magic[] = "application/x-krita";
20static constexpr int s_magic_size = sizeof(s_magic) - 1; // -1 to remove the last \0
21
22KraHandler::KraHandler()
23{
24}
25
26bool KraHandler::canRead() const
27{
28 if (canRead(device())) {
29 setFormat("kra");
30 return true;
31 }
32 return false;
33}
34
35bool KraHandler::read(QImage *image)
36{
37 KZip zip(device());
38 if (!zip.open(QIODevice::ReadOnly)) {
39 return false;
40 }
41
42 const KArchiveEntry *entry = zip.directory()->entry(QStringLiteral("mergedimage.png"));
43 if (!entry || !entry->isFile()) {
44 return false;
45 }
46
47 const KZipFileEntry *fileZipEntry = static_cast<const KZipFileEntry *>(entry);
48
49 image->loadFromData(fileZipEntry->data(), "PNG");
50
51 return true;
52}
53
54bool KraHandler::canRead(QIODevice *device)
55{
56 if (!device) {
57 qWarning("KraHandler::canRead() called with no device");
58 return false;
59 }
60 if (device->isSequential()) {
61 return false;
62 }
63
64 char buff[57];
65 if (device->peek(buff, sizeof(buff)) == sizeof(buff)) {
66 return memcmp(buff + 0x26, s_magic, s_magic_size) == 0;
67 }
68
69 return false;
70}
71
72QImageIOPlugin::Capabilities KraPlugin::capabilities(QIODevice *device, const QByteArray &format) const
73{
74 if (format == "kra" || format == "KRA") {
75 return Capabilities(CanRead);
76 }
77 if (!format.isEmpty()) {
78 return {};
79 }
80 if (!device->isOpen()) {
81 return {};
82 }
83
84 Capabilities cap;
85 if (device->isReadable() && KraHandler::canRead(device)) {
86 cap |= CanRead;
87 }
88 return cap;
89}
90
91QImageIOHandler *KraPlugin::create(QIODevice *device, const QByteArray &format) const
92{
93 QImageIOHandler *handler = new KraHandler;
94 handler->setDevice(device);
95 handler->setFormat(format);
96 return handler;
97}
98
99#include "moc_kra.cpp"
virtual bool isFile() const
QByteArray data() const override
bool isEmpty() const const
bool loadFromData(QByteArrayView data, const char *format)
QIODevice * device() const const
void setDevice(QIODevice *device)
void setFormat(const QByteArray &format)
bool isOpen() const const
bool isReadable() const const
virtual bool isSequential() const const
QByteArray peek(qint64 maxSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:21 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.