| 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 |
| 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 |
ThumbnailCreator::ThumbnailCreator(StorageMethod method) |
| 23 |
: d(new Private) |
| 24 |
{ |
| 25 |
d->thumbnailStorage = method; |
| 26 |
initialize(); |
| 27 |
} |
| 28 |
|
| 29 |
ThumbnailCreator::ThumbnailCreator(int thumbnailSize, StorageMethod method) |
| 30 |
: d(new Private) |
| 31 |
{ |
| 32 |
setThumbnailSize(thumbnailSize); |
| 33 |
d->thumbnailStorage = method; |
| 34 |
initialize(); |
| 35 |
} |
| 36 |
|
| 37 |
ThumbnailCreator::~ThumbnailCreator() |
| 38 |
{ |
| 39 |
delete d; |
| 40 |
} |
| 41 |
|
| 42 |
void ThumbnailCreator::initialize() |
| 43 |
{ |
| 44 |
QString alphaPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, |
| 45 |
QLatin1String("thumbnail/background.png")); |
| 46 |
|
| 47 |
if (QFile::exists(alphaPath)) |
| 48 |
{ |
| 49 |
if (d->alphaImage.load(alphaPath, "PNG")) |
| 50 |
{ |
| 51 |
int max = qMax(d->alphaImage.width(), d->alphaImage.height()); |
| 52 |
int min = qMin(d->alphaImage.width(), d->alphaImage.height()); |
| 53 |
|
| 54 |
if ((max > ThumbnailSize::MAX) || (min < 10)) |
| 55 |
{ |
| 56 |
d->alphaImage = QImage(); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
if (d->alphaImage.isNull()) |
| 62 |
{ |
| 63 |
d->alphaImage = QImage(20, 20, QImage::Format_RGB32); |
| 64 |
|
| 65 |
// create checkerboard image |
| 66 |
|
| 67 |
QPainter p(&d->alphaImage); |
| 68 |
p.fillRect( 0, 0, 20, 20, Qt::white); |
| 69 |
p.fillRect( 0, 10 ,10, 10, Qt::lightGray); |
| 70 |
p.fillRect(10, 0, 10, 10, Qt::lightGray); |
| 71 |
p.end(); |
| 72 |
} |
| 73 |
|
| 74 |
if (d->thumbnailStorage == FreeDesktopStandard) |
| 75 |
{ |
| 76 |
initThumbnailDirs(); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
int ThumbnailCreator::Private::storageSize() const |
| 81 |
{ |
| 82 |
// on-disk thumbnail sizes according to freedesktop spec |
| 83 |
// for thumbnail db it's always max size |
| 84 |
|
| 85 |
double dpr = qApp->devicePixelRatio(); |
| 86 |
|
| 87 |
if (onlyLargeThumbnails) |
| 88 |
{ |
| 89 |
if ((dpr > 1.0) && (thumbnailStorage == ThumbnailDatabase)) |
| 90 |
{ |
| 91 |
return ThumbnailSize::getUseLargeThumbs() ? ThumbnailSize::MAX |
| 92 |
: ThumbnailSize::HD; |
| 93 |
} |
| 94 |
else |
| 95 |
{ |
| 96 |
return ThumbnailSize::maxThumbsSize(); |
| 97 |
} |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
if ((dpr > 1.0) && (thumbnailStorage == ThumbnailDatabase)) |
| 102 |
{ |
| 103 |
return (thumbnailSize <= ThumbnailSize::Small) ? ThumbnailSize::Huge |
| 104 |
: ThumbnailSize::HD; |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
return (thumbnailSize <= ThumbnailSize::Medium) ? ThumbnailSize::Medium |
| 109 |
: ThumbnailSize::Huge; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
void ThumbnailCreator::setThumbnailSize(int thumbnailSize) |
| 115 |
{ |
| 116 |
d->thumbnailSize = thumbnailSize; |
| 117 |
} |
| 118 |
|
| 119 |
void ThumbnailCreator::setExifRotate(bool rotate) |
| 120 |
{ |
| 121 |
d->exifRotate = rotate; |
| 122 |
} |
| 123 |
|
| 124 |
void ThumbnailCreator::setOnlyLargeThumbnails(bool onlyLarge) |
| 125 |
{ |
| 126 |
d->onlyLargeThumbnails = onlyLarge; |
| 127 |
} |
| 128 |
|
| 129 |
void ThumbnailCreator::setRemoveAlphaChannel(bool removeAlpha) |
| 130 |
{ |
| 131 |
d->removeAlphaChannel = removeAlpha; |
| 132 |
} |
| 133 |
|
| 134 |
void ThumbnailCreator::setLoadingProperties(DImgLoaderObserver* const observer, const DRawDecoding& settings) |
| 135 |
{ |
| 136 |
d->observer = observer; |
| 137 |
d->rawSettings = settings; |
| 138 |
} |
| 139 |
|
| 140 |
void ThumbnailCreator::setThumbnailInfoProvider(ThumbnailInfoProvider* const provider) |
| 141 |
{ |
| 142 |
d->infoProvider = provider; |
| 143 |
} |
| 144 |
|
| 145 |
int ThumbnailCreator::thumbnailSize() const |
| 146 |
{ |
| 147 |
return d->thumbnailSize; |
| 148 |
} |
| 149 |
|
| 150 |
int ThumbnailCreator::storedSize() const |
| 151 |
{ |
| 152 |
return d->storageSize(); |
| 153 |
} |
| 154 |
|
| 155 |
QString ThumbnailCreator::errorString() const |
| 156 |
{ |
| 157 |
return d->error; |
| 158 |
} |
| 159 |
|
| 160 |
QImage ThumbnailCreator::load(const ThumbnailIdentifier& identifier, bool onlyStorage) const |
| 161 |
{ |
| 162 |
return load(identifier, QRect(), false, onlyStorage); |
| 163 |
} |
| 164 |
|
| 165 |
QImage ThumbnailCreator::loadDetail(const ThumbnailIdentifier& identifier, |
| 166 |
const QRect& rect, bool onlyStorage) const |
| 167 |
{ |
| 168 |
if (!rect.isValid()) |
| 169 |
{ |
| 170 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Invalid rectangle" << rect; |
| 171 |
|
| 172 |
return QImage(); |
| 173 |
} |
| 174 |
|
| 175 |
return load(identifier, rect, false, onlyStorage); |
| 176 |
} |
| 177 |
|
| 178 |
void ThumbnailCreator::pregenerate(const ThumbnailIdentifier& identifier) const |
| 179 |
{ |
| 180 |
load(identifier, QRect(), true); |
| 181 |
} |
| 182 |
|
| 183 |
void ThumbnailCreator::pregenerateDetail(const ThumbnailIdentifier& identifier, const QRect& rect) const |
| 184 |
{ |
| 185 |
if (!rect.isValid()) |
| 186 |
{ |
| 187 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Invalid rectangle" << rect; |
| 188 |
|
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
load(identifier, rect, true); |
| 193 |
} |
| 194 |
|
| 195 |
QImage ThumbnailCreator::load(const ThumbnailIdentifier& identifier, |
| 196 |
const QRect& rect, bool pregenerate, bool onlyStorage) const |
| 197 |
{ |
| 198 |
if (d->storageSize() <= 0) |
| 199 |
{ |
| 200 |
d->error = i18n("No or invalid size specified"); |
| 201 |
qCWarning(DIGIKAM_GENERAL_LOG) << "No or invalid size specified"; |
| 202 |
|
| 203 |
return QImage(); |
| 204 |
} |
| 205 |
|
| 206 |
if (d->thumbnailStorage == ThumbnailDatabase) |
| 207 |
{ |
| 208 |
d->dbIdForReplacement = -1; // Just to prevent bugs |
| 209 |
} |
| 210 |
|
| 211 |
ThumbnailInfo info; |
| 212 |
ThumbnailImage image; |
| 213 |
|
| 214 |
{ |
| 215 |
FileReadLocker lock(identifier.filePath); |
| 216 |
|
| 217 |
// Get info about path |
| 218 |
|
| 219 |
info = makeThumbnailInfo(identifier, rect); |
| 220 |
|
| 221 |
// Load pregenerated thumbnail |
| 222 |
|
| 223 |
switch (d->thumbnailStorage) |
| 224 |
{ |
| 225 |
case ThumbnailDatabase: |
| 226 |
{ |
| 227 |
if (pregenerate) |
| 228 |
{ |
| 229 |
if (isInDatabase(info)) |
| 230 |
{ |
| 231 |
return QImage(); |
| 232 |
} |
| 233 |
|
| 234 |
// Otherwise, fall through and generate |
| 235 |
} |
| 236 |
else |
| 237 |
{ |
| 238 |
image = loadFromDatabase(info); |
| 239 |
} |
| 240 |
|
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
case NoThumbnailStorage: |
| 245 |
case FreeDesktopStandard: |
| 246 |
{ |
| 247 |
image = loadFreedesktop(info); |
| 248 |
break; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
// For images in offline collections we can stop here, they are not available on disk |
| 254 |
|
| 255 |
if (image.isNull() && (onlyStorage || info.filePath.isEmpty())) |
| 256 |
{ |
| 257 |
return QImage(); |
| 258 |
} |
| 259 |
|
| 260 |
// If pre-generated thumbnail is not available, generate |
| 261 |
|
| 262 |
if (image.isNull()) |
| 263 |
{ |
| 264 |
FileWriteLocker lock(identifier.filePath); |
| 265 |
|
| 266 |
switch (d->thumbnailStorage) |
| 267 |
{ |
| 268 |
case ThumbnailDatabase: |
| 269 |
{ |
| 270 |
if (isInDatabase(info)) |
| 271 |
{ |
| 272 |
image = loadFromDatabase(info); |
| 273 |
} |
| 274 |
else |
| 275 |
{ |
| 276 |
qCDebug(DIGIKAM_GENERAL_LOG) |
| 277 |
<< "Generating thumbnail for:" << identifier.filePath; |
| 278 |
|
| 279 |
image = createThumbnail(info, rect); |
| 280 |
|
| 281 |
if (!image.isNull()) |
| 282 |
{ |
| 283 |
storeInDatabase(info, image); |
| 284 |
|
| 285 |
qCDebug(DIGIKAM_GENERAL_LOG) |
| 286 |
<< "Thumbnail stored in database for:" << identifier.filePath; |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
qCWarning(DIGIKAM_GENERAL_LOG) |
| 291 |
<< "Failed to generate thumbnail for:" << identifier.filePath; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
break; |
| 296 |
} |
| 297 |
|
| 298 |
case NoThumbnailStorage: |
| 299 |
case FreeDesktopStandard: |
| 300 |
{ |
| 301 |
image = createThumbnail(info, rect); |
| 302 |
|
| 303 |
if (!image.isNull()) |
| 304 |
{ |
| 305 |
// Image is stored rotated |
| 306 |
|
| 307 |
if (d->exifRotate) |
| 308 |
{ |
| 309 |
image.qimage = exifRotate(image.qimage, image.exifOrientation); |
| 310 |
} |
| 311 |
|
| 312 |
if (d->thumbnailStorage == FreeDesktopStandard) |
| 313 |
{ |
| 314 |
storeFreedesktop(info, image); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
break; |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
if (image.isNull()) |
| 324 |
{ |
| 325 |
d->error = i18n("Thumbnail is null"); |
| 326 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Thumbnail is null for " << identifier.filePath; |
| 327 |
|
| 328 |
return image.qimage; |
| 329 |
} |
| 330 |
|
| 331 |
// If we only pregenerate, we have now created and stored in the database |
| 332 |
|
| 333 |
if (pregenerate) |
| 334 |
{ |
| 335 |
return QImage(); |
| 336 |
} |
| 337 |
|
| 338 |
// Prepare for usage in digikam |
| 339 |
|
| 340 |
image.qimage = image.qimage.scaled(d->thumbnailSize, |
| 341 |
d->thumbnailSize, |
| 342 |
Qt::KeepAspectRatio, |
| 343 |
Qt::SmoothTransformation); |
| 344 |
|
| 345 |
image.qimage = handleAlphaChannel(image.qimage); |
| 346 |
|
| 347 |
if (d->thumbnailStorage == ThumbnailDatabase) |
| 348 |
{ |
| 349 |
// Image is stored, or created, unrotated, and is now rotated for display |
| 350 |
// detail thumbnails are stored readily rotated |
| 351 |
|
| 352 |
if ((d->exifRotate && rect.isNull()) || (info.mimeType == QLatin1String("video"))) |
| 353 |
{ |
| 354 |
image.qimage = exifRotate(image.qimage, image.exifOrientation); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
if (!info.customIdentifier.isNull()) |
| 359 |
{ |
| 360 |
image.qimage.setText(QLatin1String("customIdentifier"), info.customIdentifier); |
| 361 |
} |
| 362 |
|
| 363 |
return image.qimage; |
| 364 |
} |
| 365 |
|
| 366 |
QImage ThumbnailCreator::scaleForStorage(const QImage& qimage) const |
| 367 |
{ |
| 368 |
if ((qimage.width() > d->storageSize()) || (qimage.height() > d->storageSize())) |
| 369 |
{ |
| 370 |
/* |
| 371 |
Cheat scaling is disabled because of quality problems - see bug #224999 |
| 372 |
|
| 373 |
// Perform cheat scaling (https://www.qtcentre.org/threads/28415-Creating-thumbnails-efficiently) |
| 374 |
|
| 375 |
int cheatSize = maxSize - (3*(maxSize - d->storageSize()) / 4); |
| 376 |
qimage = qimage.scaled(cheatSize, cheatSize, Qt::KeepAspectRatio, Qt::FastTransformation); |
| 377 |
*/ |
| 378 |
QImage scaledThumb = qimage.scaled(d->storageSize(), |
| 379 |
d->storageSize(), |
| 380 |
Qt::KeepAspectRatio, |
| 381 |
Qt::SmoothTransformation); |
| 382 |
|
| 383 |
return scaledThumb; |
| 384 |
} |
| 385 |
|
| 386 |
return qimage; |
| 387 |
} |
| 388 |
|
| 389 |
QString ThumbnailCreator::identifierForDetail(const ThumbnailInfo& info, const QRect& rect) |
| 390 |
{ |
| 391 |
QUrl url = QUrl::fromLocalFile(info.filePath); |
| 392 |
url.setScheme(QLatin1String("detail")); |
| 393 |
/* |
| 394 |
A scheme to support loading by database id, but this is a hack. Solve cleanly later (schema update) |
| 395 |
|
| 396 |
url.setPath(identifier.fileName); |
| 397 |
|
| 398 |
if (!identifier.uniqueHash.isNull()) |
| 399 |
{ |
| 400 |
url.addQueryItem("hash", identifier.uniqueHash); |
| 401 |
url.addQueryItem("filesize", QString::number(identifier.fileSize)); |
| 402 |
} |
| 403 |
else |
| 404 |
{ |
| 405 |
url.addQueryItem("path", identifier.filePath); |
| 406 |
} |
| 407 |
*/ |
| 408 |
QString r = QString::fromLatin1("%1,%2-%3x%4") |
| 409 |
.arg(rect.x()) |
| 410 |
.arg(rect.y()) |
| 411 |
.arg(rect.width()) |
| 412 |
.arg(rect.height()); |
| 413 |
|
| 414 |
QUrlQuery q(url); |
| 415 |
q.addQueryItem(QLatin1String("rect"), r); |
| 416 |
url.setQuery(q); |
| 417 |
|
| 418 |
return url.toString(); |
| 419 |
} |
| 420 |
|
| 421 |
ThumbnailInfo ThumbnailCreator::makeThumbnailInfo(const ThumbnailIdentifier& identifier, const QRect& rect) const |
| 422 |
{ |
| 423 |
ThumbnailInfo info; |
| 424 |
|
| 425 |
if (d->infoProvider) |
| 426 |
{ |
| 427 |
info = d->infoProvider->thumbnailInfo(identifier); |
| 428 |
} |
| 429 |
else |
| 430 |
{ |
| 431 |
info = fileThumbnailInfo(identifier.filePath); |
| 432 |
} |
| 433 |
|
| 434 |
if (!rect.isNull()) |
| 435 |
{ |
| 436 |
// Important: Pass the filled info, not the possibly half-filled identifier here because the hash is preferred for the customIdentifier! |
| 437 |
|
| 438 |
info.customIdentifier = identifierForDetail(info, rect); |
| 439 |
} |
| 440 |
|
| 441 |
return info; |
| 442 |
} |
| 443 |
|
| 444 |
void ThumbnailCreator::store(const QString& path, const QImage& i) const |
| 445 |
{ |
| 446 |
store(path, i, QRect()); |
| 447 |
} |
| 448 |
|
| 449 |
void ThumbnailCreator::storeDetailThumbnail(const QString& path, const QRect& detailRect, const QImage& i) const |
| 450 |
{ |
| 451 |
store(path, i, detailRect); |
| 452 |
} |
| 453 |
|
| 454 |
void ThumbnailCreator::store(const QString& path, const QImage& i, const QRect& rect) const |
| 455 |
{ |
| 456 |
if (i.isNull() || (d->thumbnailStorage == NoThumbnailStorage)) |
| 457 |
{ |
| 458 |
return; |
| 459 |
} |
| 460 |
|
| 461 |
QImage qimage = scaleForStorage(i); |
| 462 |
ThumbnailInfo info = makeThumbnailInfo(ThumbnailIdentifier(path), rect); |
| 463 |
ThumbnailImage image; |
| 464 |
image.qimage = qimage; |
| 465 |
|
| 466 |
switch (d->thumbnailStorage) |
| 467 |
{ |
| 468 |
case ThumbnailDatabase: |
| 469 |
{ |
| 470 |
// We must call isInDatabase or loadFromDatabase before storeInDatabase for d->dbIdForReplacement! |
| 471 |
|
| 472 |
if (!isInDatabase(info)) |
| 473 |
{ |
| 474 |
storeInDatabase(info, image); |
| 475 |
} |
| 476 |
|
| 477 |
break; |
| 478 |
} |
| 479 |
|
| 480 |
case FreeDesktopStandard: |
| 481 |
{ |
| 482 |
storeFreedesktop(info, image); |
| 483 |
break; |
| 484 |
} |
| 485 |
|
| 486 |
default: |
| 487 |
{ |
| 488 |
break; |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
void ThumbnailCreator::deleteThumbnailsFromDisk(const QString& filePath) const |
| 494 |
{ |
| 495 |
switch (d->thumbnailStorage) |
| 496 |
{ |
| 497 |
case FreeDesktopStandard: |
| 498 |
{ |
| 499 |
deleteFromDiskFreedesktop(filePath); |
| 500 |
break; |
| 501 |
} |
| 502 |
|
| 503 |
case ThumbnailDatabase: |
| 504 |
{ |
| 505 |
ThumbnailInfo info; |
| 506 |
|
| 507 |
if (d->infoProvider) |
| 508 |
{ |
| 509 |
info = d->infoProvider->thumbnailInfo(ThumbnailIdentifier(filePath)); |
| 510 |
} |
| 511 |
else |
| 512 |
{ |
| 513 |
info = fileThumbnailInfo(filePath); |
| 514 |
} |
| 515 |
|
| 516 |
deleteFromDatabase(info); |
| 517 |
break; |
| 518 |
} |
| 519 |
|
| 520 |
default: |
| 521 |
{ |
| 522 |
break; |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
} // namespace Digikam |