KImageFormats

kra.cpp
1 /*
2  This file is part of the KDE project
3  SPDX-FileCopyrightText: 2013 Boudewijn Rempt <[email protected]>
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 {
24 }
25 
26 bool KraHandler::canRead() const
27 {
28  if (canRead(device())) {
29  setFormat("kra");
30  return true;
31  }
32  return false;
33 }
34 
35 bool 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 
54 bool KraHandler::canRead(QIODevice *device)
55 {
56  if (!device) {
57  qWarning("KraHandler::canRead() called with no device");
58  return false;
59  }
60 
61  char buff[57];
62  if (device->peek(buff, sizeof(buff)) == sizeof(buff)) {
63  return memcmp(buff + 0x26, s_magic, s_magic_size) == 0;
64  }
65 
66  return false;
67 }
68 
69 QImageIOPlugin::Capabilities KraPlugin::capabilities(QIODevice *device, const QByteArray &format) const
70 {
71  if (format == "kra" || format == "KRA") {
72  return Capabilities(CanRead);
73  }
74  if (!format.isEmpty()) {
75  return {};
76  }
77  if (!device->isOpen()) {
78  return {};
79  }
80 
81  Capabilities cap;
82  if (device->isReadable() && KraHandler::canRead(device)) {
83  cap |= CanRead;
84  }
85  return cap;
86 }
87 
88 QImageIOHandler *KraPlugin::create(QIODevice *device, const QByteArray &format) const
89 {
90  QImageIOHandler *handler = new KraHandler;
91  handler->setDevice(device);
92  handler->setFormat(format);
93  return handler;
94 }
bool loadFromData(const uchar *data, int len, const char *format)
bool isEmpty() const const
bool isReadable() const const
qint64 peek(char *data, qint64 maxSize)
void setDevice(QIODevice *device)
bool isOpen() const const
typedef Capabilities
QByteArray data() const override
void setFormat(const QByteArray &format)
virtual bool isFile() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Dec 2 2021 22:45:39 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.