digiKam
haar.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 2d transform
8  * Wavelet algorithms, metric and query ideas based on the paper
9  * "Fast Multiresolution Image Querying"
10  * by Charles E. Jacobs, Adam Finkelstein and David H. Salesin.
11  * https://www.cs.washington.edu/homes/salesin/abstracts.html
12  *
13  * Copyright (C) 2003 by Ricardo Niederberger Cabral <nieder at mail dot ru>
14  * Copyright (C) 2008-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
15  * Copyright (C) 2008-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
16  *
17  * This program is free software; you can redistribute it
18  * and/or modify it under the terms of the GNU General
19  * Public License as published by the Free Software Foundation;
20  * either version 2, or (at your option)
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * ============================================================ */
29 
30 #ifndef DIGIKAM_HAAR_H
31 #define DIGIKAM_HAAR_H
32 
33 // C++ includes
34 
35 #include <cstring>
36 
37 // Qt includes
38 
39 #include <QtGlobal>
40 
41 class QImage;
42 
43 namespace Digikam
44 {
45 
46 class DImg;
47 
48 namespace Haar
49 {
50 
57 static const float s_haar_weights[2][6][3] =
58 {
59  // For scanned picture (sketch=0):
60  // Y I Q idx total occurs
61 
62  { { 5.00F, 19.21F, 34.37F }, // 0 58.58 1 (`DC' component)
63  { 0.83F, 1.26F, 0.36F }, // 1 2.45 3
64  { 1.01F, 0.44F, 0.45F }, // 2 1.90 5
65  { 0.52F, 0.53F, 0.14F }, // 3 1.19 7
66  { 0.47F, 0.28F, 0.18F }, // 4 0.93 9
67  { 0.30F, 0.14F, 0.27F }
68  }, // 5 0.71 16384-25=16359
69 
70  // For handdrawn/painted sketch (sketch=1):
71  // Y I Q
72 
73  { { 4.04F, 15.14F, 22.62F },
74  { 0.78F, 0.92F, 0.40F },
75  { 0.46F, 0.53F, 0.63F },
76  { 0.42F, 0.26F, 0.25F },
77  { 0.41F, 0.14F, 0.15F },
78  { 0.32F, 0.07F, 0.38F }
79  }
80 };
81 
85 enum { NumberOfPixels = 128 };
86 
91 
95 enum { NumberOfCoefficients = 40 };
96 
97 typedef double Unit;
98 
100 typedef qint32 Idx;
101 
102 // ---------------------------------------------------------------------------------
103 
105 {
106 public:
107 
111 
112  void fillPixelData(const QImage& image);
113  void fillPixelData(const DImg& image);
114 };
115 
116 // ---------------------------------------------------------------------------------
117 
119 {
120 public:
121 
126 
130  double avg[3] = { 0.0 };
131 };
132 
133 // ---------------------------------------------------------------------------------
134 
140 {
141 public:
142 
144  {
146  }
147 
149  {
150  delete[] m_indexList;
151  }
152 
154 
155  void fill(Haar::Idx* const coefs)
156  {
157  // For maximum performance, we use a flat array.
158  // First 16k for negative values, second 16k for positive values.
159  // All values or false, only 2*40 are true.
160 
161  memset(m_indexList, 0, sizeof(MapIndexType[2 * Haar::NumberOfPixelsSquared]));
162  int x = 0;
163 
164  for (int i = 0 ; i < Haar::NumberOfCoefficients ; ++i)
165  {
166  x = coefs[i] + Haar::NumberOfPixelsSquared;
167  m_indexList[x] = true;
168  }
169  }
170 
173 
174  bool operator[](Haar::Idx index) const
175  {
177  }
178 
179 private:
180 
181  // To prevent cppcheck warnings.
182 
183  explicit SignatureMap(const SignatureMap& other)
184  {
187  }
188 
189 public:
190 
191  typedef bool MapIndexType;
193 
194 private:
195 
196  SignatureMap& operator=(const SignatureMap&); // Disable
197 };
198 
199 // ---------------------------------------------------------------------------------
200 
202 {
203 public:
204 
205  explicit WeightBin();
206 
207  unsigned char bin(int index) const
208  {
209  return m_bin[index];
210  }
211 
212  unsigned char binAbs(int index) const
213  {
214  return ((index > 0) ? m_bin[index] : m_bin[-index]);
215  }
216 
217 public:
218 
224  unsigned char m_bin[16384] = { 0 };
225 };
226 
227 // ---------------------------------------------------------------------------------
228 
229 class Weights
230 {
231 public:
232 
234  {
236  PaintedSketch = 1
237  };
238 
239 public:
240 
242  : m_type(type)
243  {
244  }
245 
246  float weight(int weight, int channel) const
247  {
248  return (s_haar_weights[(int)m_type][weight][channel]);
249  }
250 
251  float weightForAverage(int channel) const
252  {
253  return (s_haar_weights[(int)m_type][0][channel]);
254  }
255 
256 private:
257 
258  SketchType m_type;
259 };
260 
261 // ---------------------------------------------------------------------------------
262 
264 {
265 
266 public:
267 
268  explicit Calculator();
269  ~Calculator();
270 
271  int calcHaar(ImageData* const imageData, SignatureData* const sigData);
272 
273  void transform(ImageData* const data);
274 
275 private:
276 
277  void haar2D(Unit a[]);
278  inline void getmLargests(Unit* const cdata, Idx* const sig);
279 };
280 
281 } // namespace Haar
282 
283 } // namespace Digikam
284 
285 #endif // DIGIKAM_HAAR_H
Definition: dimg.h:62
Definition: haar.h:264
Calculator()
Definition: haar.cpp:173
~Calculator()
Definition: haar.cpp:177
void transform(ImageData *const data)
Definition: haar.cpp:278
int calcHaar(ImageData *const imageData, SignatureData *const sigData)
Definition: haar.cpp:381
Definition: haar.h:105
void fillPixelData(const QImage &image)
Definition: haar.cpp:85
Unit data3[NumberOfPixelsSquared]
Definition: haar.h:110
Unit data1[NumberOfPixelsSquared]
Definition: haar.h:108
Unit data2[NumberOfPixelsSquared]
Definition: haar.h:109
Definition: haar.h:119
double avg[3]
Definition: haar.h:130
Haar::Idx sig[3][Haar::NumberOfCoefficients]
Definition: haar.h:125
Definition: haar.h:140
~SignatureMap()
Definition: haar.h:148
void fill(Haar::Idx *const coefs)
Load a set of coefficients.
Definition: haar.h:155
SignatureMap()
Definition: haar.h:143
bool MapIndexType
Definition: haar.h:191
MapIndexType * m_indexList
Definition: haar.h:192
bool operator[](Haar::Idx index) const
Query if the given index is set. Index must be in the range -16383..16383.
Definition: haar.h:174
Definition: haar.h:202
unsigned char binAbs(int index) const
Definition: haar.h:212
unsigned char m_bin[16384]
Definition: haar.h:224
WeightBin()
Definition: haar.cpp:138
unsigned char bin(int index) const
Definition: haar.h:207
Definition: haar.h:230
Weights(SketchType type=ScannedSketch)
Definition: haar.h:241
float weight(int weight, int channel) const
Definition: haar.h:246
SketchType
Definition: haar.h:234
@ ScannedSketch
Definition: haar.h:235
@ PaintedSketch
Definition: haar.h:236
float weightForAverage(int channel) const
Definition: haar.h:251
@ NumberOfPixelsSquared
Definition: haar.h:90
qint32 Idx
Keep this definition constant at qint32 (guaranteed binary size!)
Definition: haar.h:100
double Unit
Definition: haar.h:97
@ NumberOfPixels
Definition: haar.h:85
@ NumberOfCoefficients
Definition: haar.h:95
Definition: datefolderview.cpp:43