File indexing completed on 2024-11-10 04:30:40

0001 /*  This file is part of the KDE libraries
0002     SPDX-FileCopyrightText: 2004 Brad Hards <bradh@frogmouth.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "exrcreator.h"
0008 #include "thumbnail-exr-logsettings.h"
0009 
0010 #include <QFile>
0011 #include <QImage>
0012 
0013 #include <ImfHeader.h>
0014 #include <ImfInputFile.h>
0015 #include <ImfPreviewImage.h>
0016 
0017 #include <KConfigGroup>
0018 #include <KPluginFactory>
0019 #include <KSharedConfig>
0020 
0021 #include <limits>
0022 
0023 K_PLUGIN_CLASS_WITH_JSON(EXRCreator, "exrthumbnail.json")
0024 
0025 EXRCreator::EXRCreator(QObject *parent, const QVariantList &args)
0026     : KIO::ThumbnailCreator(parent, args)
0027 {
0028 }
0029 
0030 KIO::ThumbnailResult EXRCreator::create(const KIO::ThumbnailRequest &request)
0031 {
0032     try {
0033         const QByteArray encodedPath = QFile::encodeName(request.url().toLocalFile());
0034         Imf::InputFile in(encodedPath.constData());
0035         const Imf::Header &h = in.header();
0036         if (h.hasPreviewImage()) {
0037             qCDebug(KIO_THUMBNAIL_EXR_LOG) << "EXRcreator - using preview";
0038             const Imf::PreviewImage &preview = h.previewImage();
0039             QImage qpreview(preview.width(), preview.height(), QImage::Format_RGB32);
0040             for (unsigned int y = 0; y < preview.height(); y++) {
0041                 for (unsigned int x = 0; x < preview.width(); x++) {
0042                     const Imf::PreviewRgba &q = preview.pixels()[x + (y * preview.width())];
0043                     qpreview.setPixel(x, y, qRgba(q.r, q.g, q.b, q.a));
0044                 }
0045             }
0046             return KIO::ThumbnailResult::pass(qpreview);
0047         }
0048     } catch (const std::exception &e) {
0049         qCDebug(KIO_THUMBNAIL_EXR_LOG) << e.what();
0050         return KIO::ThumbnailResult::fail();
0051     }
0052 
0053     // do it the hard way
0054     // We ignore maximum size when just extracting the thumbnail
0055     // from the header, but it is very expensive to render large
0056     // EXR images just to turn it into an icon, so we go back
0057     // to honoring it in here.
0058     qCDebug(KIO_THUMBNAIL_EXR_LOG) << "EXRcreator - using original image";
0059     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0060     KConfigGroup configGroup(config, "PreviewSettings");
0061     const qint64 maxSize = configGroup.readEntry("MaximumSize", std::numeric_limits<qint64>::max());
0062     const qint64 fileSize = QFile(request.url().toLocalFile()).size();
0063     if ((fileSize > 0) && (fileSize < maxSize)) {
0064         QImage img;
0065         if (!img.load(request.url().toLocalFile())) {
0066             return KIO::ThumbnailResult::fail();
0067         }
0068         return KIO::ThumbnailResult::pass(img);
0069     } else {
0070         return KIO::ThumbnailResult::fail();
0071     }
0072 }
0073 
0074 #include "exrcreator.moc"
0075 #include "moc_exrcreator.cpp"