/src/kio-extras/thumbnail/jpegcreator.cpp
Line | Count | Source |
1 | | /* This file is part of the KDE libraries |
2 | | SPDX-FileCopyrightText: 2008 Andre Gemünd <scroogie@gmail.com> |
3 | | SPDX-FileCopyrightText: 2016 Alexander Volkov <a.volkov@rusbitech.ru> |
4 | | SPDX-FileCopyrightText: 2022 Kai Uwe Broulik <kde@broulik.de> |
5 | | |
6 | | SPDX-License-Identifier: LGPL-2.0-or-later |
7 | | */ |
8 | | |
9 | | #include "config-thumbnail.h" |
10 | | |
11 | | #include "jpegcreator.h" |
12 | | #include "jpegcreatorsettings5.h" |
13 | | |
14 | | #include <QImage> |
15 | | #include <QImageReader> |
16 | | |
17 | | #include <KPluginFactory> |
18 | | |
19 | | #if HAVE_KEXIV2 |
20 | | #include <KExiv2/KExiv2> |
21 | | #endif |
22 | | |
23 | 0 | K_PLUGIN_CLASS_WITH_JSON(JpegCreator, "jpegthumbnail.json") Unexecuted instantiation: jpegthumbnail_factory::tr(char const*, char const*, int) Unexecuted instantiation: jpegthumbnail_factory::~jpegthumbnail_factory() |
24 | 0 |
|
25 | 0 | JpegCreator::JpegCreator(QObject *parent, const QVariantList &args) |
26 | 25.1k | : KIO::ThumbnailCreator(parent, args) |
27 | 25.1k | { |
28 | 25.1k | } |
29 | | |
30 | | KIO::ThumbnailResult JpegCreator::exifThumbnail(const KIO::ThumbnailRequest &request) const |
31 | 25.1k | { |
32 | 25.1k | #if HAVE_KEXIV2 |
33 | 25.1k | KExiv2Iface::KExiv2 exiv2Image(request.url().toLocalFile()); |
34 | 25.1k | QImage image = exiv2Image.getExifThumbnail(JpegCreatorSettings::self()->rotate()); |
35 | | |
36 | 25.1k | if (image.isNull()) { |
37 | 25.0k | return KIO::ThumbnailResult::fail(); |
38 | 25.0k | } |
39 | | |
40 | | // skip embedded thumbnail if strictly smaller |
41 | 56 | if (image.size().width() < request.targetSize().width() && image.size().height() < request.targetSize().height()) { |
42 | 0 | return KIO::ThumbnailResult::fail(); |
43 | 0 | } |
44 | | |
45 | 56 | return KIO::ThumbnailResult::pass(image); |
46 | | #else |
47 | | Q_UNUSED(request) |
48 | | return KIO::ThumbnailResult::fail(); |
49 | | #endif // HAVE_KEXIV2 |
50 | 56 | } |
51 | | |
52 | | KIO::ThumbnailResult JpegCreator::imageReaderThumbnail(const KIO::ThumbnailRequest &request) const |
53 | 25.0k | { |
54 | 25.0k | QImageReader imageReader(request.url().toLocalFile(), "jpeg"); |
55 | 25.0k | const QSize imageSize = imageReader.size(); |
56 | 25.0k | if (imageSize.isValid() && (imageSize.width() > request.targetSize().width() || imageSize.height() > request.targetSize().height())) { |
57 | 5.82k | const QSize thumbnailSize = imageSize.scaled(request.targetSize(), Qt::KeepAspectRatio); |
58 | 5.82k | imageReader.setScaledSize(thumbnailSize); // fast downscaling |
59 | 5.82k | } |
60 | 25.0k | imageReader.setQuality(75); // set quality so that the jpeg handler will use a high quality downscaler |
61 | | |
62 | 25.0k | imageReader.setAutoTransform(JpegCreatorSettings::self()->rotate()); |
63 | | |
64 | 25.0k | QImage image = imageReader.read(); |
65 | | |
66 | 25.0k | if (image.isNull()) { |
67 | 22.0k | return KIO::ThumbnailResult::fail(); |
68 | 22.0k | } |
69 | | |
70 | 2.98k | return KIO::ThumbnailResult::pass(image); |
71 | 25.0k | } |
72 | | |
73 | | KIO::ThumbnailResult JpegCreator::create(const KIO::ThumbnailRequest &request) |
74 | 25.1k | { |
75 | 25.1k | JpegCreatorSettings::self()->load(); |
76 | | |
77 | 25.1k | if (auto result = exifThumbnail(request); result.isValid()) { |
78 | 56 | return result; |
79 | 56 | } |
80 | | |
81 | 25.0k | if (auto result = imageReaderThumbnail(request); result.isValid()) { |
82 | 2.98k | return result; |
83 | 2.98k | } |
84 | | |
85 | 22.0k | return KIO::ThumbnailResult::fail(); |
86 | 25.0k | } |
87 | | |
88 | | #include "jpegcreator.moc" |
89 | | #include "moc_jpegcreator.cpp" |