/src/kio-extras/thumbnail/exrcreator.cpp
Line | Count | Source |
1 | | /* This file is part of the KDE libraries |
2 | | SPDX-FileCopyrightText: 2004 Brad Hards <bradh@frogmouth.net> |
3 | | |
4 | | SPDX-License-Identifier: LGPL-2.0-or-later |
5 | | */ |
6 | | |
7 | | #include "exrcreator.h" |
8 | | #include "thumbnail-exr-logsettings.h" |
9 | | |
10 | | #include <QFile> |
11 | | #include <QImage> |
12 | | |
13 | | #include <ImfHeader.h> |
14 | | #include <ImfInputFile.h> |
15 | | #include <ImfPreviewImage.h> |
16 | | |
17 | | #include <KConfigGroup> |
18 | | #include <KPluginFactory> |
19 | | #include <KSharedConfig> |
20 | | |
21 | | #include <KIO/Global> |
22 | | |
23 | | #include <limits> |
24 | | |
25 | 0 | K_PLUGIN_CLASS_WITH_JSON(EXRCreator, "exrthumbnail.json") Unexecuted instantiation: exrthumbnail_factory::tr(char const*, char const*, int) Unexecuted instantiation: exrthumbnail_factory::~exrthumbnail_factory() |
26 | 0 |
|
27 | 0 | EXRCreator::EXRCreator(QObject *parent, const QVariantList &args) |
28 | 18.0k | : KIO::ThumbnailCreator(parent, args) |
29 | 18.0k | { |
30 | 18.0k | } |
31 | | |
32 | | KIO::ThumbnailResult EXRCreator::create(const KIO::ThumbnailRequest &request) |
33 | 18.0k | { |
34 | 18.0k | try { |
35 | 18.0k | const QByteArray encodedPath = QFile::encodeName(request.url().toLocalFile()); |
36 | 18.0k | Imf::InputFile in(encodedPath.constData()); |
37 | 18.0k | const Imf::Header &h = in.header(); |
38 | 18.0k | if (h.hasPreviewImage()) { |
39 | 45 | qCDebug(KIO_THUMBNAIL_EXR_LOG) << "EXRcreator - using preview"; |
40 | 45 | const Imf::PreviewImage &preview = h.previewImage(); |
41 | 45 | QImage qpreview(preview.width(), preview.height(), QImage::Format_RGB32); |
42 | 3.22k | for (unsigned int y = 0; y < preview.height(); y++) { |
43 | 294k | for (unsigned int x = 0; x < preview.width(); x++) { |
44 | 291k | const Imf::PreviewRgba &q = preview.pixels()[x + (y * preview.width())]; |
45 | 291k | qpreview.setPixel(x, y, qRgba(q.r, q.g, q.b, q.a)); |
46 | 291k | } |
47 | 3.18k | } |
48 | 45 | return KIO::ThumbnailResult::pass(qpreview); |
49 | 45 | } |
50 | 18.0k | } catch (const std::exception &e) { |
51 | 12.4k | qCDebug(KIO_THUMBNAIL_EXR_LOG) << e.what(); |
52 | 12.4k | return KIO::ThumbnailResult::fail(); |
53 | 12.4k | } |
54 | | |
55 | | // do it the hard way |
56 | | // We ignore maximum size when just extracting the thumbnail |
57 | | // from the header, but it is very expensive to render large |
58 | | // EXR images just to turn it into an icon, so we go back |
59 | | // to honoring it in here. |
60 | 18.0k | qCDebug(KIO_THUMBNAIL_EXR_LOG) << "EXRcreator - using original image"; |
61 | 5.56k | KSharedConfig::Ptr config = KSharedConfig::openConfig(); |
62 | 5.56k | KConfigGroup configGroup(config, "PreviewSettings"); |
63 | 5.56k | const KIO::filesize_t maxSize = configGroup.readEntry("MaximumSize", std::numeric_limits<KIO::filesize_t>::max()); |
64 | 5.56k | const KIO::filesize_t fileSize = QFile(request.url().toLocalFile()).size(); |
65 | 5.56k | if ((fileSize > 0) && (fileSize < maxSize)) { |
66 | 5.56k | QImage img; |
67 | 5.56k | if (!img.load(request.url().toLocalFile())) { |
68 | 5.00k | return KIO::ThumbnailResult::fail(); |
69 | 5.00k | } |
70 | 560 | return KIO::ThumbnailResult::pass(img); |
71 | 5.56k | } else { |
72 | 0 | return KIO::ThumbnailResult::fail(); |
73 | 0 | } |
74 | 5.56k | } |
75 | | |
76 | | #include "exrcreator.moc" |
77 | | #include "moc_exrcreator.cpp" |