| 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 - Database thumbnail storage |
| 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 |
void ThumbnailCreator::storeInDatabase(const ThumbnailInfo& info, const ThumbnailImage& image) const |
| 23 |
{ |
| 24 |
ThumbsDbInfo dbInfo; |
| 25 |
|
| 26 |
// We rely on loadThumbsDbInfo() being called before, so we do not need to look up |
| 27 |
// by filepath of uniqueHash to find out if a thumb need to be replaced. |
| 28 |
|
| 29 |
dbInfo.id = d->dbIdForReplacement; |
| 30 |
d->dbIdForReplacement = -1; |
| 31 |
dbInfo.type = DatabaseThumbnail::PGF; |
| 32 |
dbInfo.modificationDate = info.modificationDate; |
| 33 |
dbInfo.orientationHint = image.exifOrientation; |
| 34 |
|
| 35 |
if (dbInfo.type == DatabaseThumbnail::PGF) |
| 36 |
{ |
| 37 |
// NOTE: see bug #233094: using PGF compression level 4 there. Do not use a value > 4, |
| 38 |
// else image is blurred due to down-sampling. |
| 39 |
|
| 40 |
if (!PGFUtils::writePGFImageData(image.qimage, dbInfo.data, 4)) |
| 41 |
{ |
| 42 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Cannot save PGF thumb in DB"; |
| 43 |
return; |
| 44 |
} |
| 45 |
} |
| 46 |
else if (dbInfo.type == DatabaseThumbnail::JPEG) |
| 47 |
{ |
| 48 |
QBuffer buffer(&dbInfo.data); |
| 49 |
buffer.open(QIODevice::WriteOnly); |
| 50 |
image.qimage.save(&buffer, "JPEG", 90); // Here we will use JPEG quality = 90 to reduce artifacts. |
| 51 |
buffer.close(); |
| 52 |
|
| 53 |
if (dbInfo.data.isNull()) |
| 54 |
{ |
| 55 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Cannot save JPEG thumb in DB"; |
| 56 |
return; |
| 57 |
} |
| 58 |
} |
| 59 |
else if (dbInfo.type == DatabaseThumbnail::JPEG2000) |
| 60 |
{ |
| 61 |
QBuffer buffer(&dbInfo.data); |
| 62 |
buffer.open(QIODevice::WriteOnly); |
| 63 |
image.qimage.save(&buffer, "JP2"); |
| 64 |
buffer.close(); |
| 65 |
|
| 66 |
if (dbInfo.data.isNull()) |
| 67 |
{ |
| 68 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Cannot save JPEG2000 thumb in DB"; |
| 69 |
return; |
| 70 |
} |
| 71 |
} |
| 72 |
else if (dbInfo.type == DatabaseThumbnail::PNG) |
| 73 |
{ |
| 74 |
QBuffer buffer(&dbInfo.data); |
| 75 |
buffer.open(QIODevice::WriteOnly); |
| 76 |
image.qimage.save(&buffer, "PNG", 0); |
| 77 |
buffer.close(); |
| 78 |
|
| 79 |
if (dbInfo.data.isNull()) |
| 80 |
{ |
| 81 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Cannot save PNG thumb in DB"; |
| 82 |
return; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
ThumbsDbAccess access; |
| 87 |
BdEngineBackend::QueryState lastQueryState = BdEngineBackend::QueryState(BdEngineBackend::ConnectionError); |
| 88 |
|
| 89 |
while (lastQueryState == BdEngineBackend::ConnectionError) |
| 90 |
{ |
| 91 |
lastQueryState = access.backend()->beginTransaction(); |
| 92 |
|
| 93 |
if (BdEngineBackend::NoErrors != lastQueryState) |
| 94 |
{ |
| 95 |
continue; |
| 96 |
} |
| 97 |
|
| 98 |
// Insert thumbnail data |
| 99 |
|
| 100 |
if (dbInfo.id == -1) |
| 101 |
{ |
| 102 |
QVariant id; |
| 103 |
lastQueryState = access.db()->insertThumbnail(dbInfo, &id); |
| 104 |
|
| 105 |
if (BdEngineBackend::NoErrors != lastQueryState) |
| 106 |
{ |
| 107 |
continue; |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
dbInfo.id = id.toInt(); |
| 112 |
} |
| 113 |
} |
| 114 |
else |
| 115 |
{ |
| 116 |
lastQueryState = access.db()->replaceThumbnail(dbInfo); |
| 117 |
|
| 118 |
if (BdEngineBackend::NoErrors != lastQueryState) |
| 119 |
{ |
| 120 |
continue; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// Insert lookup data used to locate thumbnail data |
| 125 |
|
| 126 |
if (!info.customIdentifier.isNull()) |
| 127 |
{ |
| 128 |
lastQueryState = access.db()->insertCustomIdentifier(info.customIdentifier, dbInfo.id); |
| 129 |
|
| 130 |
if (BdEngineBackend::NoErrors != lastQueryState) |
| 131 |
{ |
| 132 |
continue; |
| 133 |
} |
| 134 |
} |
| 135 |
else |
| 136 |
{ |
| 137 |
if (!info.uniqueHash.isNull()) |
| 138 |
{ |
| 139 |
lastQueryState = access.db()->insertUniqueHash(info.uniqueHash, info.fileSize, dbInfo.id); |
| 140 |
|
| 141 |
if (BdEngineBackend::NoErrors != lastQueryState) |
| 142 |
{ |
| 143 |
continue; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if (!info.filePath.isNull()) |
| 148 |
{ |
| 149 |
lastQueryState = access.db()->insertFilePath(info.filePath, dbInfo.id); |
| 150 |
|
| 151 |
if (BdEngineBackend::NoErrors != lastQueryState) |
| 152 |
{ |
| 153 |
continue; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
lastQueryState = access.backend()->commitTransaction(); |
| 159 |
|
| 160 |
if (BdEngineBackend::NoErrors != lastQueryState) |
| 161 |
{ |
| 162 |
continue; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
ThumbsDbInfo ThumbnailCreator::loadThumbsDbInfo(const ThumbnailInfo& info) const |
| 168 |
{ |
| 169 |
ThumbsDbAccess access; |
| 170 |
ThumbsDbInfo dbInfo; |
| 171 |
|
| 172 |
// Custom identifier takes precedence |
| 173 |
|
| 174 |
if (!info.customIdentifier.isEmpty()) |
| 175 |
{ |
| 176 |
dbInfo = access.db()->findByCustomIdentifier(info.customIdentifier); |
| 177 |
} |
| 178 |
else |
| 179 |
{ |
| 180 |
if (!info.uniqueHash.isEmpty()) |
| 181 |
{ |
| 182 |
dbInfo = access.db()->findByHash(info.uniqueHash, info.fileSize); |
| 183 |
} |
| 184 |
|
| 185 |
if (dbInfo.data.isNull() && !info.filePath.isEmpty()) |
| 186 |
{ |
| 187 |
dbInfo = access.db()->findByFilePath(info.filePath, info.uniqueHash); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// Store for use in storeInDatabase() |
| 192 |
|
| 193 |
d->dbIdForReplacement = dbInfo.id; |
| 194 |
|
| 195 |
return dbInfo; |
| 196 |
} |
| 197 |
|
| 198 |
bool ThumbnailCreator::isInDatabase(const ThumbnailInfo& info) const |
| 199 |
{ |
| 200 |
ThumbsDbInfo dbInfo = loadThumbsDbInfo(info); |
| 201 |
|
| 202 |
if (dbInfo.data.isNull()) |
| 203 |
{ |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
// Check modification date |
| 208 |
|
| 209 |
if (dbInfo.modificationDate < info.modificationDate) |
| 210 |
{ |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
return true; |
| 215 |
} |
| 216 |
|
| 217 |
ThumbnailImage ThumbnailCreator::loadFromDatabase(const ThumbnailInfo& info) const |
| 218 |
{ |
| 219 |
ThumbsDbInfo dbInfo = loadThumbsDbInfo(info); |
| 220 |
ThumbnailImage image; |
| 221 |
|
| 222 |
if (dbInfo.data.isNull()) |
| 223 |
{ |
| 224 |
return ThumbnailImage(); |
| 225 |
} |
| 226 |
|
| 227 |
// Check modification date |
| 228 |
|
| 229 |
if (dbInfo.modificationDate < info.modificationDate) |
| 230 |
{ |
| 231 |
return ThumbnailImage(); |
| 232 |
} |
| 233 |
|
| 234 |
// Read QImage from data blob |
| 235 |
|
| 236 |
if (dbInfo.type == DatabaseThumbnail::PGF) |
| 237 |
{ |
| 238 |
if (!PGFUtils::readPGFImageData(dbInfo.data, image.qimage)) |
| 239 |
{ |
| 240 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Cannot load PGF thumb from DB"; |
| 241 |
return ThumbnailImage(); |
| 242 |
} |
| 243 |
} |
| 244 |
else if (dbInfo.type == DatabaseThumbnail::JPEG) |
| 245 |
{ |
| 246 |
QBuffer buffer(&dbInfo.data); |
| 247 |
buffer.open(QIODevice::ReadOnly); |
| 248 |
image.qimage.load(&buffer, "JPEG"); |
| 249 |
buffer.close(); |
| 250 |
|
| 251 |
if (dbInfo.data.isNull()) |
| 252 |
{ |
| 253 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Cannot load JPEG thumb from DB"; |
| 254 |
return ThumbnailImage(); |
| 255 |
} |
| 256 |
} |
| 257 |
else if (dbInfo.type == DatabaseThumbnail::JPEG2000) |
| 258 |
{ |
| 259 |
QBuffer buffer(&dbInfo.data); |
| 260 |
buffer.open(QIODevice::ReadOnly); |
| 261 |
image.qimage.load(&buffer, "JP2"); |
| 262 |
buffer.close(); |
| 263 |
|
| 264 |
if (dbInfo.data.isNull()) |
| 265 |
{ |
| 266 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Cannot load JPEG2000 thumb from DB"; |
| 267 |
return ThumbnailImage(); |
| 268 |
} |
| 269 |
} |
| 270 |
else if (dbInfo.type == DatabaseThumbnail::PNG) |
| 271 |
{ |
| 272 |
QBuffer buffer(&dbInfo.data); |
| 273 |
buffer.open(QIODevice::ReadOnly); |
| 274 |
image.qimage.load(&buffer, "PNG"); |
| 275 |
buffer.close(); |
| 276 |
|
| 277 |
if (dbInfo.data.isNull()) |
| 278 |
{ |
| 279 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Cannot load PNG thumb from DB"; |
| 280 |
return ThumbnailImage(); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
// Give priority to main database's rotation flag |
| 285 |
// NOTE: Breaks rotation of RAWs which do not contain JPEG previews |
| 286 |
|
| 287 |
image.exifOrientation = info.orientationHint; |
| 288 |
|
| 289 |
if ((image.exifOrientation == DMetadata::ORIENTATION_UNSPECIFIED) && |
| 290 |
!info.filePath.isEmpty() && |
| 291 |
LoadSaveThread::infoProvider()) |
| 292 |
{ |
| 293 |
image.exifOrientation = LoadSaveThread::infoProvider()->orientationHint(info.filePath); |
| 294 |
} |
| 295 |
|
| 296 |
if (image.exifOrientation == DMetadata::ORIENTATION_UNSPECIFIED) |
| 297 |
{ |
| 298 |
image.exifOrientation = dbInfo.orientationHint; |
| 299 |
} |
| 300 |
|
| 301 |
return image; |
| 302 |
} |
| 303 |
|
| 304 |
void ThumbnailCreator::deleteFromDatabase(const ThumbnailInfo& info) const |
| 305 |
{ |
| 306 |
ThumbsDbAccess access; |
| 307 |
BdEngineBackend::QueryState lastQueryState = BdEngineBackend::QueryState(BdEngineBackend::ConnectionError); |
| 308 |
|
| 309 |
while (BdEngineBackend::ConnectionError == lastQueryState) |
| 310 |
{ |
| 311 |
lastQueryState = access.backend()->beginTransaction(); |
| 312 |
|
| 313 |
if (BdEngineBackend::NoErrors != lastQueryState) |
| 314 |
{ |
| 315 |
continue; |
| 316 |
} |
| 317 |
|
| 318 |
if (!info.uniqueHash.isNull()) |
| 319 |
{ |
| 320 |
lastQueryState = access.db()->removeByUniqueHash(info.uniqueHash, info.fileSize); |
| 321 |
|
| 322 |
if (BdEngineBackend::NoErrors != lastQueryState) |
| 323 |
{ |
| 324 |
continue; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
if (!info.filePath.isNull()) |
| 329 |
{ |
| 330 |
lastQueryState = access.db()->removeByFilePath(info.filePath); |
| 331 |
|
| 332 |
if (BdEngineBackend::NoErrors != lastQueryState) |
| 333 |
{ |
| 334 |
continue; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
lastQueryState = access.backend()->commitTransaction(); |
| 339 |
|
| 340 |
if (BdEngineBackend::NoErrors != lastQueryState) |
| 341 |
{ |
| 342 |
continue; |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
} // namespace Digikam |