KImageFormats

ora.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 "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())) {
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(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(fileZipEntry->data(), "PNG");
49
50 return true;
51}
52
53bool OraHandler::canRead(QIODevice *device)
54{
55 if (!device) {
56 qWarning("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(buff, sizeof(buff)) == sizeof(buff)) {
65 return memcmp(buff + 0x26, s_magic, 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"
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)
typedef Capabilities
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 Tue Mar 26 2024 11:12:40 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.