digiKam
haariface.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 : 2003-01-17
7  * Description : Haar Database interface
8  *
9  * Copyright (C) 2016-2018 by Mario Frank <mario dot frank at uni minus potsdam dot de>
10  * Copyright (C) 2003 by Ricardo Niederberger Cabral <nieder at mail dot ru>
11  * Copyright (C) 2009-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
12  * Copyright (C) 2009-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
13  * Copyright (C) 2009-2011 by Andi Clemens <andi dot clemens at gmail dot com>
14  *
15  * This program is free software; you can redistribute it
16  * and/or modify it under the terms of the GNU General
17  * Public License as published by the Free Software Foundation;
18  * either version 2, or (at your option)
19  * any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * ============================================================ */
27 
28 #ifndef DIGIKAM_HAAR_IFACE_H
29 #define DIGIKAM_HAAR_IFACE_H
30 
31 // Qt includes
32 
33 #include <QSet>
34 #include <QString>
35 #include <QMap>
36 #include <QList>
37 
38 // Local includes
39 
40 #include "haar.h"
41 #include "digikam_export.h"
42 
43 class QImage;
44 
45 namespace Digikam
46 {
47 
48 class DImg;
49 class ItemInfo;
50 
52 {
53 public:
54 
55  HaarProgressObserver() = default;
56  virtual ~HaarProgressObserver() = default;
57 
58  virtual void imageProcessed() = 0;
59  virtual bool isCanceled()
60  {
61  return false;
62  };
63 
64 private:
65 
66  Q_DISABLE_COPY(HaarProgressObserver)
67 };
68 
69 // --------------------------------------------------------------------------
70 
71 class DIGIKAM_DATABASE_EXPORT HaarIface
72 {
73 
74 public:
75 
77  {
78  ScannedSketch = 0,
79  HanddrawnSketch = 1
80  };
81 
83  {
84  None = 0,
85  SameAlbum = 1,
86  DifferentAlbum = 2
87  };
88 
90  {
91  NoMix = 0,
92  Union = 1,
93  Intersection = 2,
94  AlbumExclusive = 3,
95  TagExclusive = 4
96  };
97 
98  using DuplicatesResultsMap = QMap<qlonglong, QPair<double, QList<qlonglong> > >;
99 
100 public:
101 
102  explicit HaarIface();
103  explicit HaarIface(const QSet<qlonglong>& images2Scan);
104  ~HaarIface();
105 
106  static int preferredSize();
107 
111  bool indexImage(const QString& filename);
112  bool indexImage(const QString& filename, const QImage& image);
113  bool indexImage(const QString& filename, const DImg& image);
114  bool indexImage(qlonglong imageid, const QImage& image);
115  bool indexImage(qlonglong imageid, const DImg& image);
116 
117  QMap<qlonglong, double> bestMatchesForSignature(const QString& signature,
118  const QList<int>& targetAlbums,
119  int numberOfResults = 20,
120  SketchType type = ScannedSketch);
121 
127  QPair<double, QMap<qlonglong, double> > bestMatchesForImageWithThreshold(
128  qlonglong imageid,
129  double requiredPercentage,
130  double maximumPercentage,
131  const QList<int>& targetAlbums,
132  DuplicatesSearchRestrictions searchResultRestriction = DuplicatesSearchRestrictions::None,
133  SketchType type = ScannedSketch
134  );
135 
141  QPair<double, QMap<qlonglong, double> > bestMatchesForImageWithThreshold(
142  const QString& imagePath,
143  double requiredPercentage,
144  double maximumPercentage,
145  const QList<int>& targetAlbums,
146  DuplicatesSearchRestrictions searchResultRestriction = DuplicatesSearchRestrictions::None,
147  SketchType type = ScannedSketch
148  );
149 
154  QString signatureAsText(const QImage& image);
155 
160  bool fulfillsRestrictions(qlonglong imageId,
161  int albumId,
162  qlonglong originalImageId,
163  int originalAlbumId,
164  const QList<int>& targetAlbums,
165  DuplicatesSearchRestrictions searchResultRestriction);
166 
171  void getBestAndWorstPossibleScore(Haar::SignatureData* const querySig,
172  SketchType type,
173  double* const lowestAndBestScore,
174  double* const highestAndWorstScore);
175 
182  DuplicatesResultsMap findDuplicates(
183  const QSet<qlonglong>& images2Scan,
184  const QSet<qlonglong>::const_iterator& rangeBegin,
185  const QSet<qlonglong>::const_iterator& rangeEnd,
186  double requiredPercentage,
187  double maximumPercentage,
188  DuplicatesSearchRestrictions searchResultRestriction = DuplicatesSearchRestrictions::None,
189  HaarProgressObserver* const observer = nullptr
190  );
191 
195  static QSet<qlonglong> imagesFromAlbumsAndTags(const QList<int>& albums2Scan,
196  const QList<int>& tags2Scan,
197  AlbumTagRelation relation);
198 
203  static void rebuildDuplicatesAlbums(const DuplicatesResultsMap& results, bool isAlbumUpdate);
204 
209  bool retrieveSignatureFromDB(qlonglong imageid, Haar::SignatureData& sig);
210 
215  void setAlbumRootsToSearch(const QList<int>& albumRootIds);
216  void setAlbumRootsToSearch(const QSet<int>& albumRootIds);
217 
223  QImage loadQImage(const QString& filename);
224 
225 private:
226 
227  bool indexImage(qlonglong imageid);
228 
233  static QMap<QString, QString> writeSAlbumQueries(const DuplicatesResultsMap& searchResults);
234 
235  QMultiMap<double, qlonglong> bestMatches(Haar::SignatureData* const data,
236  int numberOfResults,
237  const QList<int>& targetAlbums,
238  SketchType type);
239 
240  QPair<double, QMap<qlonglong, double> > bestMatchesWithThreshold(qlonglong imageid,
241  Haar::SignatureData* const querySig,
242  double requiredPercentage,
243  double maximumPercentage,
244  const QList<int>& targetAlbums,
245  DuplicatesSearchRestrictions searchResultRestriction,
246  SketchType type);
247 
257  QMap<qlonglong, double> searchDatabase(Haar::SignatureData* const data,
258  SketchType type,
259  const QList<int>& targetAlbums,
260  DuplicatesSearchRestrictions searchResultRestriction = None,
261  qlonglong originalImageId = -1,
262  int albumId = -1);
263 
264  double calculateScore(const Haar::SignatureData& querySig,
265  const Haar::SignatureData& targetSig,
266  const Haar::Weights& weights,
267  std::reference_wrapper<Haar::SignatureMap>* const queryMaps);
268 
269 private:
270 
271  // Disable
272  HaarIface(const HaarIface&) = delete;
273  HaarIface& operator=(const HaarIface&) = delete;
274 
275 private:
276 
277  class Private;
278  Private* const d;
279 };
280 
281 } // namespace Digikam
282 
283 #endif // DIGIKAM_HAAR_IFACE_H
Definition: dimg.h:62
Definition: haariface_p.h:103
Definition: haariface.h:72
QMap< qlonglong, QPair< double, QList< qlonglong > > > DuplicatesResultsMap
Definition: haariface.h:98
AlbumTagRelation
Definition: haariface.h:90
DuplicatesSearchRestrictions
Definition: haariface.h:83
SketchType
Definition: haariface.h:77
Definition: haariface.h:52
virtual bool isCanceled()
Definition: haariface.h:59
virtual void imageProcessed()=0
virtual ~HaarProgressObserver()=default
Definition: haar.h:119
Definition: haar.h:230
Definition: datefolderview.cpp:43