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 "ora.h"
12
13#include <QImage>
14#include <QScopedPointer>
15
16#include <kzip.h>
17
18static constexpr char s_magic[] = "image/openraster";
19static constexpr int s_magic_size = sizeof(s_magic) - 1; // -1 to remove the last \0
20
21OraHandler::OraHandler()
22{
23}
24
25bool OraHandler::canRead() const
26{
27 if (canRead(device: device())) {
28 setFormat("ora");
29 return true;
30 }
31 return false;
32}
33
34bool OraHandler::read(QImage *image)
35{
36 KZip zip(device());
37 if (!zip.open(mode: QIODevice::ReadOnly)) {
38 return false;
39 }
40
41 const KArchiveEntry *entry = zip.directory()->entry(QStringLiteral("mergedimage.png"));
42 if (!entry || !entry->isFile()) {
43 return false;
44 }
45
46 const KZipFileEntry *fileZipEntry = static_cast<const KZipFileEntry *>(entry);
47
48 image->loadFromData(data: fileZipEntry->data(), format: "PNG");
49
50 return true;
51}
52
53bool OraHandler::canRead(QIODevice *device)
54{
55 if (!device) {
56 qWarning(msg: "OraHandler::canRead() called with no device");
57 return false;
58 }
59 if (device->isSequential()) {
60 return false;
61 }
62
63 char buff[54];
64 if (device->peek(data: buff, maxlen: sizeof(buff)) == sizeof(buff)) {
65 return memcmp(s1: buff + 0x26, s2: s_magic, n: s_magic_size) == 0;
66 }
67
68 return false;
69}
70
71QImageIOPlugin::Capabilities OraPlugin::capabilities(QIODevice *device, const QByteArray &format) const
72{
73 if (format == "ora" || format == "ORA") {
74 return Capabilities(CanRead);
75 }
76 if (!format.isEmpty()) {
77 return {};
78 }
79 if (!device->isOpen()) {
80 return {};
81 }
82
83 Capabilities cap;
84 if (device->isReadable() && OraHandler::canRead(device)) {
85 cap |= CanRead;
86 }
87 return cap;
88}
89
90QImageIOHandler *OraPlugin::create(QIODevice *device, const QByteArray &format) const
91{
92 QImageIOHandler *handler = new OraHandler;
93 handler->setDevice(device);
94 handler->setFormat(format);
95 return handler;
96}
97
98#include "moc_ora.cpp"
99

source code of kimageformats/src/imageformats/ora.cpp