File indexing completed on 2025-04-27 03:58:09
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2007-07-20 0007 * Description : Loader for thumbnails 0008 * 0009 * SPDX-FileCopyrightText: 2003-2005 by Renchi Raju <renchi dot raju at gmail dot com> 0010 * SPDX-FileCopyrightText: 2003-2024 by Gilles Caulier <caulier dot gilles at gmail dot com> 0011 * SPDX-FileCopyrightText: 2006-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de> 0012 * 0013 * SPDX-License-Identifier: GPL-2.0-or-later 0014 * 0015 * ============================================================ */ 0016 0017 #ifndef DIGIKAM_THUMB_NAIL_CREATOR_H 0018 #define DIGIKAM_THUMB_NAIL_CREATOR_H 0019 0020 // Qt includes 0021 0022 #include <QString> 0023 #include <QPixmap> 0024 #include <QImage> 0025 0026 // Local includes 0027 0028 #include "drawdecoding.h" 0029 #include "digikam_export.h" 0030 #include "thumbnailinfo.h" 0031 0032 namespace Digikam 0033 { 0034 0035 class IccProfile; 0036 class DImgLoaderObserver; 0037 class DMetadata; 0038 class ThumbnailImage; 0039 class ThumbsDbInfo; 0040 0041 class DIGIKAM_EXPORT ThumbnailCreator 0042 { 0043 public: 0044 0045 enum StorageMethod 0046 { 0047 FreeDesktopStandard, 0048 ThumbnailDatabase 0049 }; 0050 0051 public: 0052 0053 /** 0054 * Create a thumbnail creator object. 0055 * You must call setThumbnailSize before load. 0056 */ 0057 explicit ThumbnailCreator(StorageMethod method); 0058 0059 /** 0060 * Create a thumbnail creator object, and set the thumbnail size. 0061 */ 0062 ThumbnailCreator(int thumbnailSize, StorageMethod method); 0063 ~ThumbnailCreator(); 0064 0065 /** 0066 * Create a thumbnail for the specified file. 0067 */ 0068 QImage load(const ThumbnailIdentifier& identifier, bool onlyStorage = false) const; 0069 0070 /** 0071 * Creates a thumbnail for the specified detail of the file. 0072 * A suitable custom identifier (for cache key etc.) is inserted as image.text("customIdentifier"). 0073 */ 0074 QImage loadDetail(const ThumbnailIdentifier& identifier, 0075 const QRect& detailRect, 0076 bool onlyStorage = false) const; 0077 0078 /** 0079 * Ensures that the thumbnail is pregenerated in the database, but does not load it from there. 0080 */ 0081 void pregenerate(const ThumbnailIdentifier& identifier) const; 0082 void pregenerateDetail(const ThumbnailIdentifier& identifier, 0083 const QRect& detailRect) const; 0084 0085 /** 0086 * Sets the thumbnail size. This is the maximum size of the QImage 0087 * returned by load. 0088 */ 0089 void setThumbnailSize(int thumbnailSize); 0090 0091 /** 0092 * If you plan to load thumbnail from the context of the threadimageio framework, 0093 * you can specify the relevant parameters. They will be passed if a thumbnail 0094 * is created by loading with DImg. 0095 * Note that DImg is not used in most cases (Raw files, JPEG) 0096 */ 0097 void setLoadingProperties(DImgLoaderObserver* const observer, 0098 const DRawDecoding& settings); 0099 0100 /** 0101 * Set the Exif rotation property. 0102 * If exifRotate is true, the thumbnail will be rotated according 0103 * to the Exif information. 0104 * Default value is true. 0105 */ 0106 void setExifRotate(bool rotate); 0107 0108 /** 0109 * If you enable this property, the thumbnail creator will create only large 0110 * thumbnails on disk (256x256 as described in FreeDesktop paper). 0111 * Normally, for requested sizes below 128, thumbnails of 128x128 will be cached on disk. 0112 * Default value is false. 0113 */ 0114 void setOnlyLargeThumbnails(bool onlyLarge); 0115 0116 /** 0117 * If you enable this property, the returned QImage objects will not have an alpha channel. 0118 * Images with transparency will be blended over an opaque background. 0119 */ 0120 void setRemoveAlphaChannel(bool removeAlpha); 0121 0122 /** 0123 * Set a ThumbnailInfoProvider to provide custom ThumbnailInfos 0124 */ 0125 void setThumbnailInfoProvider(ThumbnailInfoProvider* const provider); 0126 0127 /** 0128 * Return the thumbnail size, the maximum size of the QImage 0129 * returned by load. 0130 */ 0131 int thumbnailSize() const; 0132 0133 /** 0134 * Return the stored image size, the size of the image that is stored on disk 0135 * (according to Storage Method). 0136 * This size is possibly larger than thumbnailSize. 0137 * Possible values: 128 or 256. 0138 */ 0139 int storedSize() const; 0140 0141 /** 0142 * Store the given image as thumbnail of the given path. 0143 * Image should at least have storedSize(). 0144 */ 0145 void store(const QString& path, const QImage& image) const; 0146 0147 void storeDetailThumbnail(const QString& path, 0148 const QRect& detailRect, 0149 const QImage& image) const; 0150 0151 /** 0152 * Returns the last error that occurred. 0153 * It is valid if load returned a null QImage object. 0154 */ 0155 QString errorString() const; 0156 0157 /** 0158 * Deletes all available thumbnails from the on-disk thumbnail cache. 0159 * A subsequent call to load() will recreate the thumbnail. 0160 */ 0161 void deleteThumbnailsFromDisk(const QString& filePath) const; 0162 0163 /** 0164 * Creates a default ThumbnailInfo for the given path using QFileInfo only 0165 */ 0166 static ThumbnailInfo fileThumbnailInfo(const QString& path); 0167 0168 /** 0169 * Returns the customIdentifier for the detail thumbnail 0170 */ 0171 static QString identifierForDetail(const ThumbnailInfo& info, 0172 const QRect& rect); 0173 0174 private: 0175 0176 void initialize(); 0177 0178 ThumbnailImage createThumbnail(const ThumbnailInfo& info, 0179 const QRect& detailRect = QRect()) const; 0180 0181 QImage load(const ThumbnailIdentifier& id, 0182 const QRect& rect, 0183 bool pregenerate, 0184 bool onlyStorage = false) const; 0185 QImage loadWithDImgScaled(const QString& path, 0186 IccProfile* const profile) const; 0187 QImage loadImageDetail(const ThumbnailInfo& info, 0188 const DMetadata& metadata, 0189 const QRect& detailRect, 0190 IccProfile* const profile) const; 0191 QImage loadImagePreview(const DMetadata& metadata) const; 0192 QImage loadPNG(const QString& path) const; 0193 0194 QImage handleAlphaChannel(const QImage& thumb) const; 0195 int exifOrientation(const ThumbnailInfo& info, 0196 const DMetadata& metadata, 0197 bool fromEmbeddedPreview, 0198 bool fromDetail) const; 0199 QImage exifRotate(const QImage& thumb, int orientation) const; 0200 0201 void store(const QString& path, 0202 const QImage& i, 0203 const QRect& rect) const; 0204 0205 ThumbnailInfo makeThumbnailInfo(const ThumbnailIdentifier& identifier, 0206 const QRect& rect) const; 0207 QImage scaleForStorage(const QImage& qimage) const; 0208 0209 void storeInDatabase(const ThumbnailInfo& info, 0210 const ThumbnailImage& image) const; 0211 ThumbsDbInfo loadThumbsDbInfo(const ThumbnailInfo& info) const; 0212 ThumbnailImage loadFromDatabase(const ThumbnailInfo& info) const; 0213 bool isInDatabase(const ThumbnailInfo& info) const; 0214 void deleteFromDatabase(const ThumbnailInfo& info) const; 0215 0216 void storeFreedesktop(const ThumbnailInfo& info, const ThumbnailImage& image) const; 0217 ThumbnailImage loadFreedesktop(const ThumbnailInfo& info) const; 0218 void deleteFromDiskFreedesktop(const QString& filePath) const; 0219 0220 void initThumbnailDirs(); 0221 QString thumbnailPath(const QString& uri) const; 0222 0223 // implementations in thumbnailbasic.cpp 0224 0225 static QString normalThumbnailDir(); 0226 static QString largeThumbnailDir(); 0227 static QString thumbnailPath(const QString& filePath, const QString& basePath); 0228 static QString thumbnailUri(const QString& filePath); 0229 static QString thumbnailPathFromUri(const QString& uri, const QString& basePath); 0230 0231 private: 0232 0233 // Disable 0234 ThumbnailCreator(const ThumbnailCreator&) = delete; 0235 ThumbnailCreator& operator=(const ThumbnailCreator&) = delete; 0236 0237 private: 0238 0239 class Private; 0240 Private* const d; 0241 }; 0242 0243 } // namespace Digikam 0244 0245 #endif // DIGIKAM_THUMB_NAIL_CREATOR_H