digiKam
Loading...
Searching...
No Matches
dimgloader.h
Go to the documentation of this file.
1/* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2005-06-14
7 * Description : DImg image loader interface
8 *
9 * SPDX-FileCopyrightText: 2005 by Renchi Raju <renchi dot raju at gmail dot com>
10 * SPDX-FileCopyrightText: 2005-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
11 *
12 * SPDX-License-Identifier: GPL-2.0-or-later
13 *
14 * ============================================================ */
15
16#pragma once
17
18// C++ includes
19
20#include <limits>
21
22// Qt includes
23
24#include <QMap>
25#include <QString>
26#include <QVariant>
27
28// Local includes
29
30#include "digikam_debug.h"
31#include "digikam_export.h"
32#include "dimg.h"
33
34namespace Digikam
35{
36
37class DImgLoaderObserver;
38class DMetadata;
39
40class DIGIKAM_EXPORT DImgLoader
41{
42public:
43
48 {
50
51 LoadItemInfo = 1,
52 LoadMetadata = 2,
53 LoadICCData = 4,
54
55 LoadImageData = 8,
56 LoadUniqueHash = 16,
57 LoadImageHistory = 32,
58
60
61 LoadPreview = 64,
62
64
65 LoadAll = LoadItemInfo | LoadMetadata | LoadICCData | LoadImageData | LoadUniqueHash | LoadImageHistory
66 };
67 Q_DECLARE_FLAGS(LoadFlags, LoadFlag)
68
69public:
70
71 void setLoadFlags(LoadFlags flags);
72
73 virtual ~DImgLoader();
74
75 virtual bool load(const QString& filePath, DImgLoaderObserver* const observer) = 0;
76 virtual bool save(const QString& filePath, DImgLoaderObserver* const observer) = 0;
77
78 virtual bool hasLoadedData() const;
79 virtual bool hasAlpha() const = 0;
80 virtual bool sixteenBit() const = 0;
81 virtual bool isReadOnly() const = 0;
82
83 static unsigned char* new_failureTolerant(size_t unsecureSize);
84 static unsigned char* new_failureTolerant(quint64 w, quint64 h, uint typesPerPixel);
85 static unsigned short* new_short_failureTolerant(size_t unsecureSize);
86 static unsigned short* new_short_failureTolerant(quint64 w, quint64 h, uint typesPerPixel);
87
88 static int convertCompressionForLibPng(int value);
89 static int convertCompressionForLibJpeg(int value);
90
96 static qint64 checkAllocation(qint64 fullSize);
97
98 template <typename Type> static Type* new_failureTolerant(size_t unsecureSize);
99 template <typename Type> static Type* new_failureTolerant(quint64 w, quint64 h, uint typesPerPixel);
100
101protected:
102
103 explicit DImgLoader(DImg* const image);
104
105 unsigned char*& imageData();
106 unsigned int& imageWidth();
107 unsigned int& imageHeight();
108
109 bool imageHasAlpha() const;
110 bool imageSixteenBit() const;
111
112 quint64 imageNumBytes() const;
113 int imageBitsDepth() const;
114 int imageBytesDepth() const;
115
116 void imageSetIccProfile(const IccProfile& profile);
117 QVariant imageGetAttribute(const QString& key) const;
118 void imageSetAttribute(const QString& key,
119 const QVariant& value);
120
121 QMap<QString, QString>& imageEmbeddedText() const;
122 QString imageGetEmbbededText(const QString& key) const;
123 void imageSetEmbbededText(const QString& key,
124 const QString& text);
125
126 void loadingFailed();
127 bool checkExifWorkingColorSpace() const;
128 void purgeExifWorkingColorSpace();
129 void storeColorProfileInMetadata();
130
131 virtual bool readMetadata(const QString& filePath);
132 virtual bool saveMetadata(const QString& filePath);
133 virtual int granularity(DImgLoaderObserver* const observer, int total, float progressSlice = 1.0F);
134
135protected:
136
137 DImg* m_image = nullptr;
138 LoadFlags m_loadFlags = LoadAll;
139
140private:
141
142 // Disable
143 DImgLoader() = delete;
144
145private:
146
147 Q_DISABLE_COPY(DImgLoader)
148};
149
150// ---------------------------------------------------------------------------------------------------
151
156template <typename Type>
157Q_INLINE_TEMPLATE Type* DImgLoader::new_failureTolerant(quint64 w, quint64 h, uint typesPerPixel)
158{
159 quint64 requested = w * h * (quint64)typesPerPixel;
160
161 if (requested >= std::numeric_limits<size_t>::max())
162 {
163 qCCritical(DIGIKAM_DIMG_LOG) << "Requested memory of" << requested * quint64(sizeof(Type))
164 << "is larger than size_t supported by platform.";
165 return nullptr;
166 }
167
168 return new_failureTolerant<Type>(requested);
169}
170
171template <typename Type>
172Q_INLINE_TEMPLATE Type* DImgLoader::new_failureTolerant(size_t size)
173{
174 qint64 res = checkAllocation(size);
175
176 switch (res)
177 {
178 case 0: // parse failure from supported platform
179 {
180 return nullptr;
181 }
182
183 case -1: // unsupported platform
184 {
185 // We will try to continue to allocate
186 break;
187 }
188
189 default: // parse done with success from supported platform
190 {
191 break;
192 }
193 }
194
195 Type* const reserved = new (std::nothrow) Type[size];
196
197 if (!reserved)
198 {
199 qCCritical(DIGIKAM_DIMG_LOG) << "Failed to allocate chunk of memory of size" << size;
200 }
201
202 return reserved;
203}
204
205Q_DECLARE_OPERATORS_FOR_FLAGS(DImgLoader::LoadFlags)
206
207} // namespace Digikam
Definition dimgloaderobserver.h:31
Definition dimgloader.h:41
virtual bool hasAlpha() const =0
static unsigned char * new_failureTolerant(size_t unsecureSize)
Definition dimgloader.cpp:286
static Type * new_failureTolerant(quint64 w, quint64 h, uint typesPerPixel)
static Type * new_failureTolerant(size_t unsecureSize)
LoadFlag
Definition dimgloader.h:48
virtual bool isReadOnly() const =0
virtual bool save(const QString &filePath, DImgLoaderObserver *const observer)=0
static qint64 checkAllocation(qint64 fullSize)
Definition dimgloader.cpp:159
virtual bool load(const QString &filePath, DImgLoaderObserver *const observer)=0
virtual bool sixteenBit() const =0
Definition dimg.h:52
Definition iccprofile.h:33
qulonglong value
Definition itemviewutilities.cpp:585
Definition datefolderview.cpp:34
Type
Definition gpsitemcontainer.h:36