digiKam
dimg.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 : digiKam 8/16 bits image management API
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  * Copyright (C) 2006-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
12  *
13  * This program is free software; you can redistribute it
14  * and/or modify it under the terms of the GNU General
15  * Public License as published by the Free Software Foundation;
16  * either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * ============================================================ */
25 
26 #ifndef DIGIKAM_DIMG_H
27 #define DIGIKAM_DIMG_H
28 
29 // Qt includes
30 
31 #include <QExplicitlySharedDataPointer>
32 #include <QByteArray>
33 #include <QFileInfo>
34 #include <QFlags>
35 #include <QSize>
36 #include <QRect>
37 #include <QVariant>
38 
39 // Local includes
40 
41 #include "digikam_export.h"
42 #include "drawdecoding.h"
43 #include "dcolor.h"
44 #include "dcolorcomposer.h"
45 #include "historyimageid.h"
46 #include "iccprofile.h"
47 #include "metaengine_data.h"
48 
49 class QImage;
50 class QPixmap;
51 
52 namespace Digikam
53 {
54 
55 class ExposureSettingsContainer;
56 class DImageHistory;
57 class FilterAction;
58 class IccTransform;
59 class DImgLoaderObserver;
60 
61 class DIGIKAM_EXPORT DImg
62 {
63 public:
64 
65  enum FORMAT
66  {
71  NONE = 0,
73  PNG,
76  PGF,
78  // Others file formats.
79  RAW,
80  QIMAGE
81  };
82 
83  enum ANGLE
84  {
85  ROT90 = 0,
88  ROTNONE
89  };
90 
91  enum FLIP
92  {
93  HORIZONTAL = 0,
94  VERTICAL
95  };
96 
98  {
99  COLORMODELUNKNOWN = 0,
107  COLORMODELRAW
108  };
109 
110 public:
111 
115  static FORMAT fileFormat(const QString& filePath);
116 
117  static QString formatToMimeType(FORMAT frm);
118 
119 public:
120 
121  class Private;
122 
123 public:
124 
128  DImg();
129 
133  explicit DImg(const QByteArray& filePath, DImgLoaderObserver* const observer = nullptr,
134  const DRawDecoding& rawDecodingSettings = DRawDecoding());
135 
139  explicit DImg(const QString& filePath, DImgLoaderObserver* const observer = nullptr,
140  const DRawDecoding& rawDecodingSettings = DRawDecoding());
141 
146  DImg(const DImg& image);
147 
152  explicit DImg(const QImage& image);
153 
161  DImg(uint width, uint height, bool sixteenBit, bool alpha = false, uchar* const data = nullptr, bool copyData = true);
162 
163  ~DImg();
164 
168  DImg& operator=(const DImg& image);
169 
176  void detach();
177 
187  bool operator==(const DImg& image) const;
188 
192  void putImageData(uint width, uint height, bool sixteenBit, bool alpha, uchar* const data, bool copyData = true);
193 
201  void putImageData(uchar* const data, bool copyData = true);
202 
206  void reset();
207 
211  void resetMetaData();
212 
217  uchar* stripImageData();
218 
219  bool load(const QString& filePath, DImgLoaderObserver* const observer = nullptr,
220  const DRawDecoding& rawDecodingSettings = DRawDecoding());
221 
222  bool load(const QString& filePath,
223  bool loadMetadata, bool loadICCData, bool loadUniqueHash, bool loadHistory,
224  DImgLoaderObserver* const observer = nullptr,
225  const DRawDecoding& rawDecodingSettings = DRawDecoding());
226 
227  bool load(const QString& filePath, int loadFlags, DImgLoaderObserver* const observer,
228  const DRawDecoding& rawDecodingSettings = DRawDecoding());
229 
230  bool save(const QString& filePath, FORMAT frm, DImgLoaderObserver* const observer = nullptr);
231  bool save(const QString& filePath, const QString& format, DImgLoaderObserver* const observer = nullptr);
232 
240  void imageSavedAs(const QString& savePath);
241 
247  bool loadItemInfo(const QString& filePath, bool loadMetadata = true,
248  bool loadICCData = true, bool loadUniqueHash = true,
249  bool loadImageHistory = true);
250 
251  bool isNull() const;
252  uint width() const;
253  uint height() const;
254  QSize size() const;
255  uchar* copyBits() const;
256  uchar* bits() const;
257  uchar* scanLine(uint i) const;
258  bool hasAlpha() const;
259  bool sixteenBit() const;
260  quint64 numBytes() const;
261  quint64 numPixels() const;
262 
266  int bytesDepth() const;
267 
271  int bitsDepth() const;
272 
277  QString originalFilePath() const;
278 
284  QString lastSavedFilePath() const;
285 
290  COLORMODEL originalColorModel() const;
291 
295  int originalBitDepth() const;
296 
300  QSize originalSize() const;
301 
306  QSize originalRatioSize() const;
307 
317  FORMAT detectedFormat() const;
318 
325  QString format() const;
326 
334  QString savedFormat() const;
335 
340  DRawDecoding rawDecodingSettings() const;
341 
348  DColor getPixelColor(uint x, uint y) const;
349  void setPixelColor(uint x, uint y, const DColor& color);
350  void prepareSubPixelAccess();
351  DColor getSubPixelColor(float x, float y) const;
352  DColor getSubPixelColorFast(float x, float y) const;
353 
360  bool hasTransparentPixels() const;
361 
368  bool isReadOnly() const;
369 
373  MetaEngineData getMetadata() const;
374  IccProfile getIccProfile() const;
375  void setMetadata(const MetaEngineData& data);
376  void setIccProfile(const IccProfile& profile);
377 
378  void setAttribute(const QString& key, const QVariant& value);
379  QVariant attribute(const QString& key) const;
380  bool hasAttribute(const QString& key) const;
381  void removeAttribute(const QString& key);
382 
383  void setEmbeddedText(const QString& key, const QString& text);
384  QString embeddedText(const QString& key) const;
385 
386  const DImageHistory& getItemHistory() const;
387  DImageHistory& getItemHistory();
388  void setItemHistory(const DImageHistory& history);
389  bool hasImageHistory() const;
390  DImageHistory getOriginalImageHistory() const;
391  void addFilterAction(const FilterAction& action);
392 
401  void setHistoryBranchAfter(const DImageHistory& historyBeforeBranch, bool isBranch = true);
402  void setHistoryBranchForLastSteps(int numberOfLastHistorySteps, bool isBranch = true);
403  void setHistoryBranch(bool isBranch = true);
404 
419  {
424  RemoveOldMetadataPreviews = 1 << 0,
428  CreateNewMetadataPreview = 1 << 1,
433  ResetExifOrientationTag = 1 << 2,
438  CreateNewImageHistoryUUID = 1 << 3,
439 
440  PrepareMetadataFlagsAll = RemoveOldMetadataPreviews |
441  CreateNewMetadataPreview |
442  ResetExifOrientationTag |
443  CreateNewImageHistoryUUID
444  };
445  Q_DECLARE_FLAGS(PrepareMetadataFlags, PrepareMetadataFlag)
446 
447  void prepareMetadataToSave(const QString& intendedDestPath,
448  const QString& destMimeType,
449  const QString& originalFileName = QString(),
450  PrepareMetadataFlags flags = PrepareMetadataFlagsAll);
451 
456  void prepareMetadataToSave(const QString& intendedDestPath,
457  const QString& destMimeType,
458  bool resetExifOrientationTag);
459 
463  HistoryImageId createHistoryImageId(const QString& filePath, HistoryImageId::Type type);
464 
477  HistoryImageId addAsReferredImage(const QString& filePath, HistoryImageId::Type type = HistoryImageId::Intermediate);
478  void addAsReferredImage(const HistoryImageId& id);
479  void insertAsReferredImage(int afterHistoryStep, const HistoryImageId& otherImagesId);
480 
486  void addCurrentUniqueImageId(const QString& uuid);
487 
492  int exifOrientation(const QString& filePath);
493 
511  QVariant fileOriginData() const;
512  void setFileOriginData(const QVariant& data);
513  QVariant lastSavedFileOriginData() const;
514  void switchOriginToLastSaved();
515 
519  DImg copy() const;
520 
524  DImg copyImageData() const;
525 
532  DImg copyMetaData() const;
533 
537  DImg copy(const QRect& rect) const;
538  DImg copy(const QRectF& relativeRect) const;
539  DImg copy(int x, int y, int w, int h) const;
540 
550  void bitBltImage(const DImg* const src, int dx, int dy);
551  void bitBltImage(const DImg* const src, int sx, int sy, int dx, int dy);
552  void bitBltImage(const DImg* const src, int sx, int sy, int w, int h, int dx, int dy);
553  void bitBltImage(const uchar* const src, int sx, int sy, int w, int h, int dx, int dy,
554  uint swidth, uint sheight, int sdepth);
555 
561  void bitBlendImage(DColorComposer* const composer, const DImg* const src,
562  int sx, int sy, int w, int h, int dx, int dy,
563  DColorComposer::MultiplicationFlags multiplicationFlags =
565 
571  void bitBlendImageOnColor(DColorComposer* const composer, const DColor& color,
572  int x, int y, int w, int h,
573  DColorComposer::MultiplicationFlags multiplicationFlags =
575  void bitBlendImageOnColor(const DColor& color, int x, int y, int w, int h);
576  void bitBlendImageOnColor(const DColor& color);
577 
581  QImage copyQImage() const;
582  QImage copyQImage(const QRect& rect) const;
583  QImage copyQImage(const QRectF& relativeRect) const;
584  QImage copyQImage(int x, int y, int w, int h) const;
585 
589  void crop(const QRect& rect);
590  void crop(int x, int y, int w, int h);
591 
595  void resize(int w, int h);
596 
604  void removeAlphaChannel(const DColor& destColor);
605  void removeAlphaChannel();
606 
611  DImg smoothScale(int width, int height, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio) const;
612  DImg smoothScale(const QSize& destSize, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio) const;
613 
623  DImg smoothScaleClipped(int width, int height, int clipx, int clipy, int clipwidth, int clipheight) const;
624  DImg smoothScaleClipped(const QSize& destSize, const QRect& clip) const;
625 
630  DImg smoothScaleSection(int sx, int sy, int sw, int sh, int dw, int dh) const;
631  DImg smoothScaleSection(const QRect& sourceRect, const QSize& destSize) const;
632 
633  void rotate(ANGLE angle);
634  void flip(FLIP direction);
635 
642  bool rotateAndFlip(int orientation);
643 
647  bool reverseRotateAndFlip(int orientation);
648 
656  bool wasExifRotated();
657  bool exifRotate(const QString& filePath);
658 
662  bool reverseExifRotate(const QString& filePath);
663 
667  int orientation() const;
668 
673  bool transform(int transformAction);
674 
675  QPixmap convertToPixmap() const;
676  QPixmap convertToPixmap(IccTransform& monitorICCtrans) const;
677 
682  QImage pureColorMask(ExposureSettingsContainer* const expoSettings) const;
683 
689  void convertDepth(int depth);
690 
694  void convertToSixteenBit();
695  void convertToEightBit();
696  void convertToDepthOfImage(const DImg* const otherImage);
697 
702  void fill(const DColor& color);
703 
722  QByteArray getUniqueHash();
723  static QByteArray getUniqueHash(const QString& filePath);
724 
738  QByteArray getUniqueHashV2();
739  static QByteArray getUniqueHashV2(const QString& filePath);
740 
749  QByteArray createImageUniqueId();
750 
754  static QString colorModelToString(COLORMODEL colorModel);
755 
759  static bool isAnimatedImage(const QString& filePath);
760 
761 private:
762 
763  DImg(const DImg& image, uint w, uint h);
764 
765  void copyMetaData(const QExplicitlySharedDataPointer<Private>& src);
766  void copyImageData(const QExplicitlySharedDataPointer<Private>& src);
767  void setImageData(bool null, uint width, uint height, bool sixteenBit, bool alpha);
768  void setImageDimension(uint width, uint height);
769  size_t allocateData() const;
770 
771  bool clipped(int& x, int& y, int& w, int& h, uint width, uint height) const;
772 
773  QDateTime creationDateFromFilesystem(const QFileInfo& fileInfo) const;
774 
775  static QByteArray createUniqueHash(const QString& filePath, const QByteArray& ba);
776  static QByteArray createUniqueHashV2(const QString& filePath);
777 
778  void bitBlt(const uchar* const src, uchar* const dest,
779  int sx, int sy, int w, int h, int dx, int dy,
780  uint swidth, uint sheight, uint dwidth, uint dheight,
781  bool sixteenBit, int sdepth, int ddepth);
782  void bitBlend(DColorComposer* const composer, uchar* const src, uchar* const dest,
783  int sx, int sy, int w, int h, int dx, int dy,
784  uint swidth, uint sheight, uint dwidth, uint dheight,
785  bool sixteenBit, int sdepth, int ddepth,
786  DColorComposer::MultiplicationFlags multiplicationFlags);
787  void bitBlendOnColor(DColorComposer* const composer, const DColor& color,
788  uchar* data, int x, int y, int w, int h,
789  uint width, uint height, bool sixteenBit, int depth,
790  DColorComposer::MultiplicationFlags multiplicationFlags);
791  bool normalizeRegionArguments(int& sx, int& sy, int& w, int& h, int& dx, int& dy,
792  uint swidth, uint sheight, uint dwidth, uint dheight) const;
793 
794 private:
795 
796  QExplicitlySharedDataPointer<Private> m_priv;
797 
798  friend class DImgLoader;
799 };
800 
801 } // namespace Digikam
802 
803 Q_DECLARE_OPERATORS_FOR_FLAGS(Digikam::DImg::PrepareMetadataFlags)
804 
805 #endif // DIGIKAM_DIMG_H
Definition: dcolorcomposer.h:36
MultiplicationFlags
Definition: dcolorcomposer.h:87
@ NoMultiplication
Definition: dcolorcomposer.h:88
Definition: dcolor.h:43
Definition: dimagehistory.h:49
Definition: dimgloaderobserver.h:41
Definition: dimgloader.h:51
Definition: dimg.h:62
COLORMODEL
Definition: dimg.h:98
@ YCBCR
Definition: dimg.h:104
@ CMYK
Definition: dimg.h:105
@ INDEXED
Definition: dimg.h:103
@ MONOCHROME
Definition: dimg.h:102
@ CIELAB
Definition: dimg.h:106
@ GRAYSCALE
Definition: dimg.h:101
@ RGB
Definition: dimg.h:100
ANGLE
Definition: dimg.h:84
@ ROT180
Definition: dimg.h:86
@ ROT270
Definition: dimg.h:87
QString embeddedText(const QString &key) const
PrepareMetadataFlag
Definition: dimg.h:419
FORMAT
Definition: dimg.h:66
@ TIFF
Definition: dimg.h:74
@ PNG
Definition: dimg.h:73
@ JP2K
Definition: dimg.h:75
@ RAW
Definition: dimg.h:79
@ JPEG
Definition: dimg.h:72
@ HEIF
Definition: dimg.h:77
@ PGF
Definition: dimg.h:76
FLIP
Definition: dimg.h:92
bool sixteenBit() const
uint width() const
uint height() const
Definition: drawdecoding.h:48
Definition: exposurecontainer.h:39
Definition: filteraction.h:43
Definition: historyimageid.h:42
Type
Definition: historyimageid.h:46
@ Intermediate
Definition: historyimageid.h:58
Definition: iccprofile.h:43
Definition: icctransform.h:49
Definition: metaengine_data.h:41
qulonglong value
Definition: itemviewutilities.cpp:592
@ NONE
Definition: dngconverteractions.h:44
Definition: datefolderview.cpp:43
bool operator==(const SearchTextSettings &a, const SearchTextSettings &b)
Definition: searchtextbar.cpp:49