Debian Codegraph @

core/libs/threadimageio/thumb/thumbnailcreator_engine.cpp

raw
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2007-07-20
7 * Description : Loader for thumbnails - thumbnail generation and image handling
8 *
9 * SPDX-FileCopyrightText: 2003-2005 by Renchi Raju <renchi dot raju at gmail dot com>
10 * SPDX-FileCopyrightText: 2003-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
11 * SPDX-FileCopyrightText: 2006-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
12 *
13 * SPDX-License-Identifier: GPL-2.0-or-later
14 *
15 * ============================================================ */
16
17 #include "thumbnailcreator_p.h"
18
19 namespace Digikam
20 {
21
22 ThumbnailImage ThumbnailCreator::createThumbnail(const ThumbnailInfo& info, const QRect& detailRect) const
23 {
24 const QString path = info.filePath;
25 QFileInfo fileInfo(path);
26
27 if (!info.isAccessible || !fileInfo.exists() || !fileInfo.isFile())
28 {
29 d->error = i18n("File does not exist or is not a file");
30 return ThumbnailImage();
31 }
32
33 QImage qimage;
34 QScopedPointer<DMetadata> metadata(new DMetadata(path));
35 bool fromEmbeddedPreview = false;
36 bool fromDetail = false;
37 bool failedAtDImg = false;
38 bool failedAtJPEGScaled = false;
39 bool failedAtPGFScaled = false;
40
41 // -- Get the image preview --------------------------------
42
43 IccProfile profile;
44 bool colorManage = IccSettings::instance()->useManagedPreviews();
45
46 if (!detailRect.isNull())
47 {
48 qCDebug(DIGIKAM_GENERAL_LOG) << "Trying to get thumbnail with details for" << path;
49
50 // When taking a detail, we have to load the image full size
51
52 qimage = loadImageDetail(info, *metadata, detailRect, &profile);
53 fromDetail = !qimage.isNull();
54 }
55 else
56 {
57 qCDebug(DIGIKAM_GENERAL_LOG) << "Trying to get thumbnail from" << path << "(" << info.mimeType << ")";
58 QString ext = fileInfo.suffix().toUpper();
59
60 if (
61 (info.mimeType == QLatin1String("image")) ||
62 (ext == QLatin1String("AVIF")) || // See bug #109060
63 (ext == QLatin1String("JPX"))
64 )
65 {
66 if (qimage.isNull())
67 {
68 // Try to extract Exif/IPTC preview first.
69
70 qimage = loadImagePreview(*metadata);
71 }
72
73 if (d->observer && !d->observer->continueQuery())
74 {
75 return ThumbnailImage();
76 }
77
78 // To speed-up thumb extraction, we now try to load the images by the file extension.
79
80 if (qimage.isNull() && !ext.isEmpty())
81 {
82 if (
83 (ext == QLatin1String("JPEG")) ||
84 (ext == QLatin1String("JPG")) ||
85 (ext == QLatin1String("JPE"))
86 )
87 {
88 if (colorManage)
89 {
90 qimage = loadWithDImgScaled(path, &profile);
91 }
92 else
93 {
94 // Use jpegutils
95
96 JPEGUtils::loadJPEGScaled(qimage, path, d->storageSize());
97 }
98
99 failedAtJPEGScaled = qimage.isNull();
100 }
101 else if (
102 (ext == QLatin1String("PNG")) ||
103 (ext == QLatin1String("TIFF")) ||
104 (ext == QLatin1String("TIF"))
105 )
106 {
107 // Use DImg load scaled mode
108
109 qimage = loadWithDImgScaled(path, &profile);
110 failedAtDImg = qimage.isNull();
111 }
112 else if (ext == QLatin1String("PGF"))
113 {
114 // Use pgf library to extract reduced version
115
116 PGFUtils::loadPGFScaled(qimage, path, d->storageSize());
117 failedAtPGFScaled = qimage.isNull();
118 }
119
120 if (d->observer && !d->observer->continueQuery())
121 {
122 return ThumbnailImage();
123 }
124 }
125
126 // Trying to load with libraw: RAW files.
127
128 if (qimage.isNull())
129 {
130 qCDebug(DIGIKAM_GENERAL_LOG) << "Trying to get thumbnail from Embedded preview with libraw for" << path;
131
132 if (DRawDecoder::loadEmbeddedPreview(qimage, path))
133 {
134 fromEmbeddedPreview = true;
135 profile = metadata->getIccProfile();
136 }
137 }
138
139 if (qimage.isNull())
140 {
141 qCDebug(DIGIKAM_GENERAL_LOG) << "Trying to get thumbnail from half preview with libraw for" << path;
142
143 // TODO: Use DImg based loader instead?
144 // We store thumbnails unrotated, loading
145 // half preview from libraw unrotated here.
146
147 DRawDecoder::loadHalfPreview(qimage, path, false);
148 }
149
150 // Special case with DNG file. See bug #338081
151
152 if (qimage.isNull())
153 {
154 qCDebug(DIGIKAM_GENERAL_LOG) << "Trying to get thumbnail from Embedded preview with Exiv2 for" << path;
155
156 MetaEnginePreviews preview(path);
157 qimage = preview.image();
158
159 if (
160 !qimage.isNull() &&
161 (
162 (qimage.width() < ThumbnailSize::maxThumbsSize()) ||
163 (qimage.height() < ThumbnailSize::maxThumbsSize())
164 )
165 )
166 {
167 qCDebug(DIGIKAM_GENERAL_LOG) << "Embedded preview too small for a thumbnail ("
168 << qimage.width() << "x" << qimage.height() << ")";
169
170 qimage = QImage();
171 }
172 }
173
174 // DImg-dependent loading methods: TIFF, PNG, everything supported by QImage
175
176 if (qimage.isNull() && !failedAtDImg)
177 {
178 qimage = loadWithDImgScaled(path, &profile);
179
180 if (d->observer && !d->observer->continueQuery())
181 {
182 return ThumbnailImage();
183 }
184 }
185
186 // Try JPEG anyway
187
188 if (qimage.isNull() && !failedAtJPEGScaled)
189 {
190 // Use jpegutils
191
192 JPEGUtils::loadJPEGScaled(qimage, path, d->storageSize());
193 }
194
195 // Try PGF anyway
196
197 if (qimage.isNull() && !failedAtPGFScaled)
198 {
199 // Use pgfutils
200
201 PGFUtils::loadPGFScaled(qimage, path, d->storageSize());
202 }
203 }
204 else
205 {
206 // Try video thumbnail anyway
207
208 #ifdef HAVE_MEDIAPLAYER
209
210 qCDebug(DIGIKAM_GENERAL_LOG) << "Trying to load video preview with FFmpeg";
211
212 VideoThumbnailer thumbnailer;
213 VideoStripFilter videoStrip;
214
215 thumbnailer.addFilter(&videoStrip);
216 thumbnailer.setThumbnailSize(d->storageSize());
217 thumbnailer.generateThumbnail(path, qimage);
218
219 #else
220
221 qDebug(DIGIKAM_GENERAL_LOG) << "Cannot load video preview for" << path;
222 qDebug(DIGIKAM_GENERAL_LOG) << "Video support is not available";
223
224 #endif
225
226 }
227 }
228
229 if (qimage.isNull())
230 {
231 d->error = i18n("Cannot create thumbnail for %1", path);
232 qCWarning(DIGIKAM_GENERAL_LOG) << "Cannot create thumbnail for" << path;
233
234 return ThumbnailImage();
235 }
236
237 qimage = scaleForStorage(qimage);
238
239 if (colorManage && !profile.isNull())
240 {
241 IccManager::transformToSRGB(qimage, profile);
242 }
243
244 ThumbnailImage image;
245 image.qimage = qimage;
246 image.exifOrientation = exifOrientation(info, *metadata, fromEmbeddedPreview, fromDetail);
247
248 return image;
249 }
250
251 QImage ThumbnailCreator::loadWithDImgScaled(const QString& path, IccProfile* const profile) const
252 {
253 DImg img;
254 img.setAttribute(QLatin1String("scaledLoadingSize"), d->storageSize());
255
256 if (!img.load(path, false, profile ? true : false, false, false, d->observer, d->rawSettings))
257 {
258 return QImage();
259 }
260
261 if (profile)
262 {
263 *profile = img.getIccProfile();
264 }
265
266 return img.copyQImage32();
267 }
268
269 QImage ThumbnailCreator::loadImageDetail(const ThumbnailInfo& info,
270 const DMetadata& metadata,
271 const QRect& detailRect,
272 IccProfile* const profile) const
273 {
274 const QString& path = info.filePath;
275
276 qDebug(DIGIKAM_GENERAL_LOG) << "Try get thumbnail from Metadata preview for" << path;
277
278 // Check the first and largest preview (Raw files)
279
280 MetaEnginePreviews previews(path);
281
282 if (!previews.isEmpty())
283 {
284 QSize orgSize;
285
286 if (metadata.getExifTagString("Exif.Image.Make").toUpper() == QLatin1String("FUJIFILM"))
287 {
288 QString sHeight = metadata.getExifTagString("Exif.Fujifilm.RawImageFullHeight");
289 QString sWidth = metadata.getExifTagString("Exif.Fujifilm.RawImageFullWidth");
290
291 if (!sWidth.isEmpty() && !sHeight.isEmpty())
292 {
293 orgSize = QSize(sWidth.toInt(),
294 sHeight.toInt());
295 }
296 }
297
298 if (!orgSize.isValid())
299 {
300 orgSize = previews.originalSize();
301 }
302
303 // Discard if smaller than half preview
304
305 QImage qimage = previews.image();
306 int acceptableWidth = lround(orgSize.width() * 0.5);
307 int acceptableHeight = lround(orgSize.height() * 0.5);
308
309 if (!qimage.isNull() && (previews.width() >= acceptableWidth) && (previews.height() >= acceptableHeight))
310 {
311 qimage = exifRotate(qimage, exifOrientation(info, metadata, true, false));
312
313 if (
314 ((qimage.width() < qimage.height()) && (orgSize.width() > orgSize.height())) ||
315 ((qimage.width() > qimage.height()) && (orgSize.width() < orgSize.height()))
316 )
317 {
318 orgSize.transpose();
319 }
320
321 QRect reducedSizeDetail = TagRegion::mapFromOriginalSize(orgSize, qimage.size(), detailRect);
322
323 return qimage.copy(reducedSizeDetail.intersected(qimage.rect()));
324 }
325 }
326
327 // load DImg
328
329 DImg img;
330
331 DImgLoader::LoadFlags loadFlags = DImgLoader::LoadItemInfo |
332 DImgLoader::LoadMetadata |
333 DImgLoader::LoadICCData |
334 DImgLoader::LoadPreview;
335
336 qDebug(DIGIKAM_GENERAL_LOG) << "Try to get thumbnail from DImg preview for" << path;
337
338 if (img.load(path, loadFlags, d->observer, d->fastRawSettings))
339 {
340 // Discard if smaller than half preview
341
342 unsigned int acceptableWidth = lround(img.originalRatioSize().width() * 0.5);
343 unsigned int acceptableHeight = lround(img.originalRatioSize().height() * 0.5);
344
345 if ((img.width() < acceptableWidth) && (img.height() < acceptableHeight))
346 {
347 qDebug(DIGIKAM_GENERAL_LOG) << "Preview image is smaller than the accepted size:"
348 << acceptableWidth << "x" << acceptableHeight;
349 img.reset();
350 }
351 }
352
353 if (img.isNull())
354 {
355 qDebug(DIGIKAM_GENERAL_LOG) << "Try to get thumbnail from DImg scaled for" << path;
356
357 // TODO: scaledLoading if detailRect is large
358 // TODO: use code from PreviewTask, including cache storage
359
360 if (!img.load(path, false, profile ? true : false, false, false, d->observer, d->fastRawSettings))
361 {
362 return QImage();
363 }
364 }
365
366 if (profile)
367 {
368 *profile = img.getIccProfile();
369 }
370
371 // We must rotate before clipping because the rect refers to the oriented image.
372 // I do not know currently how to back-rotate the rect for clipping before rotation.
373 // If someone has the mathematics, have a go.
374
375 img.rotateAndFlip(exifOrientation(info, metadata, false, false));
376
377 QRect mappedDetail = TagRegion::mapFromOriginalSize(img, detailRect);
378 img.crop(mappedDetail.intersected(QRect(0, 0, img.width(), img.height())));
379
380 return img.copyQImage32();
381 }
382
383 QImage ThumbnailCreator::loadImagePreview(const DMetadata& metadata) const
384 {
385 QImage image;
386
387 qCDebug(DIGIKAM_GENERAL_LOG) << "Trying to get thumbnail with Exiv2 for" << metadata.getFilePath();
388
389 if (metadata.getItemPreview(image))
390 {
391 qCDebug(DIGIKAM_GENERAL_LOG) << "Get thumbnail from Exif/IPTC preview. Size of image: "
392 << image.width() << "x" << image.height();
393 }
394 else
395 {
396 // load DImg preview
397
398 qCDebug(DIGIKAM_GENERAL_LOG) << "Trying to get thumbnail with DImg preview for" << metadata.getFilePath();
399
400 DImg img;
401 DImgLoader::LoadFlags loadFlags = DImgLoader::LoadItemInfo |
402 DImgLoader::LoadMetadata |
403 DImgLoader::LoadICCData |
404 DImgLoader::LoadPreview;
405
406 if (img.load(metadata.getFilePath(), loadFlags, d->observer, d->fastRawSettings))
407 {
408 image = img.copyQImage32();
409 }
410 }
411
412 return image;
413 }
414
415 QImage ThumbnailCreator::handleAlphaChannel(const QImage& qimage) const
416 {
417 switch (qimage.format())
418 {
419 case QImage::Format_RGB32:
420 {
421 break;
422 }
423
424 case QImage::Format_ARGB32:
425 case QImage::Format_ARGB32_Premultiplied:
426 {
427 QImage newImage(qimage.size(), QImage::Format_RGB32);
428 newImage.fill(Qt::transparent);
429 QPainter p(&newImage);
430
431 if (d->removeAlphaChannel)
432 {
433 QBrush brush(d->alphaImage);
434 p.fillRect(newImage.rect(), brush);
435 }
436
437 p.drawImage(0, 0, qimage);
438 p.end();
439
440 return newImage;
441 }
442
443 default: // indexed and monochrome formats
444 {
445 return qimage.convertToFormat(QImage::Format_RGB32);
446 }
447 }
448
449 return qimage;
450 }
451
452 int ThumbnailCreator::exifOrientation(const ThumbnailInfo& info,
453 const DMetadata& metadata,
454 bool fromEmbeddedPreview,
455 bool fromDetail) const
456 {
457 if (fromDetail)
458 {
459 return DMetadata::ORIENTATION_NORMAL;
460 }
461
462 return LoadSaveThread::exifOrientation(info.filePath, metadata,
463 DImg::fileFormat(info.filePath) == DImg::RAW,
464 fromEmbeddedPreview);
465 }
466
467 QImage ThumbnailCreator::exifRotate(const QImage& thumb, int orientation) const
468 {
469 if ((orientation == DMetadata::ORIENTATION_NORMAL) ||
470 (orientation == DMetadata::ORIENTATION_UNSPECIFIED))
471 {
472 return thumb;
473 }
474
475 QTransform matrix = MetaEngineRotation::toTransform((MetaEngine::ImageOrientation)orientation);
476
477 // Transform accordingly
478
479 return thumb.transformed(matrix);
480 }
481
482 } // namespace Digikam