digiKam
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  * Copyright (C) 2005 by Renchi Raju <renchi dot raju at gmail dot com>
10  * Copyright (C) 2005-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
11  *
12  * This program is free software; you can redistribute it
13  * and/or modify it under the terms of the GNU General
14  * Public License as published by the Free Software Foundation;
15  * either version 2, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * ============================================================ */
24 
25 #ifndef DIGIKAM_DIMG_LOADER_H
26 #define DIGIKAM_DIMG_LOADER_H
27 
28 // C++ includes
29 
30 #include <limits>
31 
32 // Qt includes
33 
34 #include <QMap>
35 #include <QString>
36 #include <QVariant>
37 
38 // Local includes
39 
40 #include "digikam_debug.h"
41 #include "digikam_export.h"
42 #include "dimg.h"
43 
44 namespace Digikam
45 {
46 
47 class DImgLoaderObserver;
48 class DMetadata;
49 
50 class DIGIKAM_EXPORT DImgLoader
51 {
52 public:
53 
57  enum LoadFlag
58  {
60 
61  LoadItemInfo = 1,
62  LoadMetadata = 2,
63  LoadICCData = 4,
64 
65  LoadImageData = 8,
66  LoadUniqueHash = 16,
67  LoadImageHistory = 32,
68 
70 
71  LoadPreview = 64,
72 
74 
75  LoadAll = LoadItemInfo | LoadMetadata | LoadICCData | LoadImageData | LoadUniqueHash | LoadImageHistory
76  };
77  Q_DECLARE_FLAGS(LoadFlags, LoadFlag)
78 
79 public:
80 
81  void setLoadFlags(LoadFlags flags);
82 
83  virtual ~DImgLoader();
84 
85  virtual bool load(const QString& filePath, DImgLoaderObserver* const observer) = 0;
86  virtual bool save(const QString& filePath, DImgLoaderObserver* const observer) = 0;
87 
88  virtual bool hasLoadedData() const;
89  virtual bool hasAlpha() const = 0;
90  virtual bool sixteenBit() const = 0;
91  virtual bool isReadOnly() const = 0;
92 
93  static unsigned char* new_failureTolerant(size_t unsecureSize);
94  static unsigned char* new_failureTolerant(quint64 w, quint64 h, uint typesPerPixel);
95  static unsigned short* new_short_failureTolerant(size_t unsecureSize);
96  static unsigned short* new_short_failureTolerant(quint64 w, quint64 h, uint typesPerPixel);
97 
103  static qint64 checkAllocation(qint64 fullSize);
104 
105  template <typename Type> static Type* new_failureTolerant(size_t unsecureSize);
106  template <typename Type> static Type* new_failureTolerant(quint64 w, quint64 h, uint typesPerPixel);
107 
108 protected:
109 
110  explicit DImgLoader(DImg* const image);
111 
112  unsigned char*& imageData();
113  unsigned int& imageWidth();
114  unsigned int& imageHeight();
115 
116  bool imageHasAlpha() const;
117  bool imageSixteenBit() const;
118 
119  quint64 imageNumBytes() const;
120  int imageBitsDepth() const;
121  int imageBytesDepth() const;
122 
123  void imageSetIccProfile(const IccProfile& profile);
124  QVariant imageGetAttribute(const QString& key) const;
125  void imageSetAttribute(const QString& key,
126  const QVariant& value);
127 
128  QMap<QString, QString>& imageEmbeddedText() const;
129  QString imageGetEmbbededText(const QString& key) const;
130  void imageSetEmbbededText(const QString& key,
131  const QString& text);
132 
133  void loadingFailed();
134  bool checkExifWorkingColorSpace() const;
135  void purgeExifWorkingColorSpace();
136  void storeColorProfileInMetadata();
137 
138  virtual bool readMetadata(const QString& filePath);
139  virtual bool saveMetadata(const QString& filePath);
140  virtual int granularity(DImgLoaderObserver* const observer, int total, float progressSlice = 1.0F);
141 
142 protected:
143 
145  LoadFlags m_loadFlags;
146 
147 private:
148 
149  // Disable
150  DImgLoader() = delete;
151 
152 private:
153 
154  Q_DISABLE_COPY(DImgLoader)
155 };
156 
157 // ---------------------------------------------------------------------------------------------------
158 
163 template <typename Type>
164 Q_INLINE_TEMPLATE Type* DImgLoader::new_failureTolerant(quint64 w, quint64 h, uint typesPerPixel)
165 {
166  quint64 requested = w * h * (quint64)typesPerPixel;
167 
168  if (requested >= std::numeric_limits<size_t>::max())
169  {
170  qCCritical(DIGIKAM_DIMG_LOG) << "Requested memory of" << requested * quint64(sizeof(Type))
171  << "is larger than size_t supported by platform.";
172  return nullptr;
173  }
174 
175  return new_failureTolerant<Type>(requested);
176 }
177 
178 template <typename Type>
179 Q_INLINE_TEMPLATE Type* DImgLoader::new_failureTolerant(size_t size)
180 {
181  qint64 res = checkAllocation(size);
182 
183  switch (res)
184  {
185  case 0: // parse failure from supported platform
186  {
187  return nullptr;
188  }
189 
190  case -1: // unsupported platform
191  {
192  // We will try to continue to allocate
193  break;
194  }
195 
196  default: // parse done with success from supported platform
197  {
198  break;
199  }
200  }
201 
202  Type* const reserved = new (std::nothrow) Type[size];
203 
204  if (!reserved)
205  {
206  qCCritical(DIGIKAM_DIMG_LOG) << "Failed to allocate chunk of memory of size" << size;
207  }
208 
209  return reserved;
210 }
211 
212 Q_DECLARE_OPERATORS_FOR_FLAGS(DImgLoader::LoadFlags)
213 
214 } // namespace Digikam
215 
216 #endif // DIGIKAM_DIMG_LOADER_H
Definition: dimgloaderobserver.h:41
Definition: dimgloader.h:51
virtual bool hasAlpha() const =0
DImg * m_image
Definition: dimgloader.h:144
static unsigned char * new_failureTolerant(size_t unsecureSize)
Definition: dimgloader.cpp:282
LoadFlag
Definition: dimgloader.h:58
virtual bool isReadOnly() const =0
LoadFlags m_loadFlags
Definition: dimgloader.h:145
virtual bool save(const QString &filePath, DImgLoaderObserver *const observer)=0
static qint64 checkAllocation(qint64 fullSize)
Definition: dimgloader.cpp:168
virtual bool load(const QString &filePath, DImgLoaderObserver *const observer)=0
virtual bool sixteenBit() const =0
static Type * new_failureTolerant(size_t unsecureSize)
static Type * new_failureTolerant(quint64 w, quint64 h, uint typesPerPixel)
Definition: dimg.h:62
Definition: iccprofile.h:43
qulonglong value
Definition: itemviewutilities.cpp:592
Definition: datefolderview.cpp:43
Type
Definition: gpsitemcontainer.h:45