KImageFormats

ora.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 "ora.h"
12 
13 #include <QImage>
14 #include <QScopedPointer>
15 
16 #include <kzip.h>
17 
18 static constexpr char s_magic[] = "image/openraster";
19 static constexpr int s_magic_size = sizeof(s_magic) - 1; // -1 to remove the last \0
20 
21 OraHandler::OraHandler()
22 {
23 }
24 
25 bool OraHandler::canRead() const
26 {
27  if (canRead(device())) {
28  setFormat("ora");
29  return true;
30  }
31  return false;
32 }
33 
34 bool 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 
53 bool OraHandler::canRead(QIODevice *device)
54 {
55  if (!device) {
56  qWarning("OraHandler::canRead() called with no device");
57  return false;
58  }
59 
60  char buff[54];
61  if (device->peek(buff, sizeof(buff)) == sizeof(buff)) {
62  return memcmp(buff + 0x26, s_magic, s_magic_size) == 0;
63  }
64 
65  return false;
66 }
67 
68 QImageIOPlugin::Capabilities OraPlugin::capabilities(QIODevice *device, const QByteArray &format) const
69 {
70  if (format == "ora" || format == "ORA") {
71  return Capabilities(CanRead);
72  }
73  if (!format.isEmpty()) {
74  return {};
75  }
76  if (!device->isOpen()) {
77  return {};
78  }
79 
80  Capabilities cap;
81  if (device->isReadable() && OraHandler::canRead(device)) {
82  cap |= CanRead;
83  }
84  return cap;
85 }
86 
87 QImageIOHandler *OraPlugin::create(QIODevice *device, const QByteArray &format) const
88 {
89  QImageIOHandler *handler = new OraHandler;
90  handler->setDevice(device);
91  handler->setFormat(format);
92  return handler;
93 }
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.