1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2007-07-20
 * Description : Loader for thumbnails
 *
 * SPDX-FileCopyrightText: 2003-2005 by Renchi Raju <renchi dot raju at gmail dot com>
 * SPDX-FileCopyrightText: 2003-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * SPDX-FileCopyrightText: 2006-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "thumbnailcreator_p.h"

namespace Digikam
{

ThumbnailCreator::ThumbnailCreator(StorageMethod method)
    : d(new Private)
{
    d->thumbnailStorage = method;
    initialize();
}

ThumbnailCreator::ThumbnailCreator(int thumbnailSize, StorageMethod method)
    : d(new Private)
{
    setThumbnailSize(thumbnailSize);
    d->thumbnailStorage = method;
    initialize();
}

ThumbnailCreator::~ThumbnailCreator()
{
    delete d;
}

void ThumbnailCreator::initialize()
{
    QString alphaPath = QStandardPaths::locate(QStandardPaths::AppDataLocation,
                                               QLatin1String("thumbnail/background.png"));

    if (QFile::exists(alphaPath))
    {
        if (d->alphaImage.load(alphaPath, "PNG"))
        {
            int max = qMax(d->alphaImage.width(), d->alphaImage.height());
            int min = qMin(d->alphaImage.width(), d->alphaImage.height());

            if ((max > ThumbnailSize::MAX) || (min < 10))
            {
                d->alphaImage = QImage();
            }
        }
    }

    if (d->alphaImage.isNull())
    {
        d->alphaImage = QImage(20, 20, QImage::Format_RGB32);

        // create checkerboard image

        QPainter p(&d->alphaImage);
        p.fillRect( 0,  0, 20, 20, Qt::white);
        p.fillRect( 0, 10 ,10, 10, Qt::lightGray);
        p.fillRect(10,  0, 10, 10, Qt::lightGray);
        p.end();
    }

    if (d->thumbnailStorage == FreeDesktopStandard)
    {
        initThumbnailDirs();
    }
}

int ThumbnailCreator::Private::storageSize() const
{
    // on-disk thumbnail sizes according to freedesktop spec
    // for thumbnail db it's always max size

    double dpr = qApp->devicePixelRatio();

    if (onlyLargeThumbnails)
    {
        if ((dpr > 1.0) && (thumbnailStorage == ThumbnailDatabase))
        {
            return ThumbnailSize::getUseLargeThumbs() ? ThumbnailSize::MAX
                                                      : ThumbnailSize::HD;
        }
        else
        {
            return ThumbnailSize::maxThumbsSize();
        }
    }
    else
    {
        if ((dpr > 1.0) && (thumbnailStorage == ThumbnailDatabase))
        {
            return (thumbnailSize <= ThumbnailSize::Small) ? ThumbnailSize::Huge
                                                           : ThumbnailSize::HD;
        }
        else
        {
            return (thumbnailSize <= ThumbnailSize::Medium) ? ThumbnailSize::Medium
                                                            : ThumbnailSize::Huge;
        }
    }
}

void ThumbnailCreator::setThumbnailSize(int thumbnailSize)<--- Shadow argument
{
    d->thumbnailSize = thumbnailSize;
}

void ThumbnailCreator::setExifRotate(bool rotate)
{
    d->exifRotate = rotate;
}

void ThumbnailCreator::setOnlyLargeThumbnails(bool onlyLarge)
{
    d->onlyLargeThumbnails = onlyLarge;
}

void ThumbnailCreator::setRemoveAlphaChannel(bool removeAlpha)
{
    d->removeAlphaChannel = removeAlpha;
}

void ThumbnailCreator::setLoadingProperties(DImgLoaderObserver* const observer, const DRawDecoding& settings)
{
    d->observer    = observer;
    d->rawSettings = settings;
}

void ThumbnailCreator::setThumbnailInfoProvider(ThumbnailInfoProvider* const provider)
{
    d->infoProvider = provider;
}

int ThumbnailCreator::thumbnailSize() const
{
    return d->thumbnailSize;
}

int ThumbnailCreator::storedSize() const
{
    return d->storageSize();
}

QString ThumbnailCreator::errorString() const
{
    return d->error;
}

QImage ThumbnailCreator::load(const ThumbnailIdentifier& identifier, bool onlyStorage) const
{
    return load(identifier, QRect(), false, onlyStorage);
}

QImage ThumbnailCreator::loadDetail(const ThumbnailIdentifier& identifier,
                                    const QRect& rect, bool onlyStorage) const
{
    if (!rect.isValid())
    {
        qCWarning(DIGIKAM_GENERAL_LOG) << "Invalid rectangle" << rect;

        return QImage();
    }

    return load(identifier, rect, false, onlyStorage);
}

void ThumbnailCreator::pregenerate(const ThumbnailIdentifier& identifier) const
{
    load(identifier, QRect(), true);
}

void ThumbnailCreator::pregenerateDetail(const ThumbnailIdentifier& identifier, const QRect& rect) const
{
    if (!rect.isValid())
    {
        qCWarning(DIGIKAM_GENERAL_LOG) << "Invalid rectangle" << rect;

        return;
    }

    load(identifier, rect, true);
}

QImage ThumbnailCreator::load(const ThumbnailIdentifier& identifier,
                              const QRect& rect, bool pregenerate, bool onlyStorage) const<--- Shadow argument
{
    if (d->storageSize() <= 0)
    {
        d->error = i18n("No or invalid size specified");
        qCWarning(DIGIKAM_GENERAL_LOG) << "No or invalid size specified";

        return QImage();
    }

    if (d->thumbnailStorage == ThumbnailDatabase)
    {
        d->dbIdForReplacement = -1;    // Just to prevent bugs
    }

    ThumbnailInfo info;
    ThumbnailImage image;

    {
        FileReadLocker lock(identifier.filePath);

        // Get info about path

        info = makeThumbnailInfo(identifier, rect);

        // Load pregenerated thumbnail

        switch (d->thumbnailStorage)
        {
            case ThumbnailDatabase:
            {
                if (pregenerate)
                {
                    if (isInDatabase(info))
                    {
                        return QImage();
                    }

                    // Otherwise, fall through and generate
                }
                else
                {
                    image = loadFromDatabase(info);
                }

                break;
            }

            case NoThumbnailStorage:
            case FreeDesktopStandard:
            {
                image = loadFreedesktop(info);
                break;
            }
        }
    }

    // For images in offline collections we can stop here, they are not available on disk

    if (image.isNull() && (onlyStorage || info.filePath.isEmpty()))
    {
        return QImage();
    }

    // If pre-generated thumbnail is not available, generate

    if (image.isNull())
    {
        FileWriteLocker lock(identifier.filePath);

        switch (d->thumbnailStorage)
        {
            case ThumbnailDatabase:
            {
                if (isInDatabase(info))
                {
                    image = loadFromDatabase(info);
                }
                else
                {
                    qCDebug(DIGIKAM_GENERAL_LOG)
                        << "Generating thumbnail for:" << identifier.filePath;

                    image = createThumbnail(info, rect);

                    if (!image.isNull())
                    {
                        storeInDatabase(info, image);

                        qCDebug(DIGIKAM_GENERAL_LOG)
                            << "Thumbnail stored in database for:" << identifier.filePath;
                    }
                    else
                    {
                        qCWarning(DIGIKAM_GENERAL_LOG)
                            << "Failed to generate thumbnail for:" << identifier.filePath;
                    }
                }

                break;
            }

            case NoThumbnailStorage:
            case FreeDesktopStandard:
            {
                image = createThumbnail(info, rect);

                if (!image.isNull())
                {
                    // Image is stored rotated

                    if (d->exifRotate)
                    {
                        image.qimage = exifRotate(image.qimage, image.exifOrientation);
                    }

                    if (d->thumbnailStorage == FreeDesktopStandard)
                    {
                        storeFreedesktop(info, image);
                    }
                }

                break;
            }
        }
    }

    if (image.isNull())
    {
        d->error = i18n("Thumbnail is null");
        qCWarning(DIGIKAM_GENERAL_LOG) << "Thumbnail is null for " << identifier.filePath;

        return image.qimage;
    }

    // If we only pregenerate, we have now created and stored in the database

    if (pregenerate)
    {
        return QImage();
    }

    // Prepare for usage in digikam

    image.qimage = image.qimage.scaled(d->thumbnailSize,
                                       d->thumbnailSize,
                                       Qt::KeepAspectRatio,
                                       Qt::SmoothTransformation);

    image.qimage = handleAlphaChannel(image.qimage);

    if (d->thumbnailStorage == ThumbnailDatabase)
    {
        // Image is stored, or created, unrotated, and is now rotated for display
        // detail thumbnails are stored readily rotated

        if ((d->exifRotate && rect.isNull()) || (info.mimeType == QLatin1String("video")))
        {
            image.qimage = exifRotate(image.qimage, image.exifOrientation);
        }
    }

    if (!info.customIdentifier.isNull())
    {
        image.qimage.setText(QLatin1String("customIdentifier"), info.customIdentifier);
    }

    return image.qimage;
}

QImage ThumbnailCreator::scaleForStorage(const QImage& qimage) const
{
    if ((qimage.width() > d->storageSize()) || (qimage.height() > d->storageSize()))
    {
/*
        Cheat scaling is disabled because of quality problems - see bug #224999

        // Perform cheat scaling (https://www.qtcentre.org/threads/28415-Creating-thumbnails-efficiently)

        int cheatSize = maxSize - (3*(maxSize - d->storageSize()) / 4);
        qimage        = qimage.scaled(cheatSize, cheatSize, Qt::KeepAspectRatio, Qt::FastTransformation);
*/
        QImage scaledThumb = qimage.scaled(d->storageSize(),
                                           d->storageSize(),
                                           Qt::KeepAspectRatio,
                                           Qt::SmoothTransformation);

        return scaledThumb;
    }

    return qimage;
}

QString ThumbnailCreator::identifierForDetail(const ThumbnailInfo& info, const QRect& rect)
{
    QUrl url = QUrl::fromLocalFile(info.filePath);
    url.setScheme(QLatin1String("detail"));
/*
    A scheme to support loading by database id, but this is a hack. Solve cleanly later (schema update)

    url.setPath(identifier.fileName);

    if (!identifier.uniqueHash.isNull())
    {
        url.addQueryItem("hash", identifier.uniqueHash);
        url.addQueryItem("filesize", QString::number(identifier.fileSize));
    }
    else
    {
        url.addQueryItem("path", identifier.filePath);
    }
*/
    QString r = QString::fromLatin1("%1,%2-%3x%4")
                .arg(rect.x())
                .arg(rect.y())
                .arg(rect.width())
                .arg(rect.height());

    QUrlQuery q(url);
    q.addQueryItem(QLatin1String("rect"), r);
    url.setQuery(q);

    return url.toString();
}

ThumbnailInfo ThumbnailCreator::makeThumbnailInfo(const ThumbnailIdentifier& identifier, const QRect& rect) const
{
    ThumbnailInfo info;

    if (d->infoProvider)
    {
        info = d->infoProvider->thumbnailInfo(identifier);
    }
    else
    {
        info = fileThumbnailInfo(identifier.filePath);
    }

    if (!rect.isNull())
    {
        // Important: Pass the filled info, not the possibly half-filled identifier here because the hash is preferred for the customIdentifier!

        info.customIdentifier = identifierForDetail(info, rect);
    }

    return info;
}

void ThumbnailCreator::store(const QString& path, const QImage& i) const
{
    store(path, i, QRect());
}

void ThumbnailCreator::storeDetailThumbnail(const QString& path, const QRect& detailRect, const QImage& i) const
{
    store(path, i, detailRect);
}

void ThumbnailCreator::store(const QString& path, const QImage& i, const QRect& rect) const
{
    if (i.isNull() || (d->thumbnailStorage == NoThumbnailStorage))
    {
        return;
    }

    QImage         qimage = scaleForStorage(i);
    ThumbnailInfo  info   = makeThumbnailInfo(ThumbnailIdentifier(path), rect);
    ThumbnailImage image;
    image.qimage          = qimage;

    switch (d->thumbnailStorage)
    {
        case ThumbnailDatabase:
        {
            // We must call isInDatabase or loadFromDatabase before storeInDatabase for d->dbIdForReplacement!

            if (!isInDatabase(info))
            {
                storeInDatabase(info, image);
            }

            break;
        }

        case FreeDesktopStandard:
        {
            storeFreedesktop(info, image);
            break;
        }

        default:
        {
            break;
        }
    }
}

void ThumbnailCreator::deleteThumbnailsFromDisk(const QString& filePath) const
{
    switch (d->thumbnailStorage)
    {
        case FreeDesktopStandard:
        {
            deleteFromDiskFreedesktop(filePath);
            break;
        }

        case ThumbnailDatabase:
        {
            ThumbnailInfo info;

            if (d->infoProvider)
            {
                info = d->infoProvider->thumbnailInfo(ThumbnailIdentifier(filePath));
            }
            else
            {
                info = fileThumbnailInfo(filePath);
            }

            deleteFromDatabase(info);
            break;
        }

        default:
        {
            break;
        }
    }
}

} // namespace Digikam