digiKam
Loading...
Searching...
No Matches
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://grail.cs.washington.edu/wp-content/uploads/2015/08/jacobs-1995.pdf
12 *
13 * SPDX-FileCopyrightText: 2003 by Ricardo Niederberger Cabral <nieder at mail dot ru>
14 * SPDX-FileCopyrightText: 2008-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
15 * SPDX-FileCopyrightText: 2008-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
16 *
17 * SPDX-License-Identifier: GPL-2.0-or-later
18 *
19 * ============================================================ */
20
21#pragma once
22
23// C++ includes
24
25#include <cstring>
26
27// Qt includes
28
29#include <QtGlobal>
30
31class QImage;
32
33namespace Digikam
34{
35
36class DImg;
37
38namespace Haar
39{
40
47static const float s_haar_weights[2][6][3] =
48{
49 // For scanned picture (sketch=0):
50 // Y I Q idx total occurs
51
52 {
53 { 5.00F, 19.21F, 34.37F }, // 0 58.58 1 (`DC' component)
54 { 0.83F, 1.26F, 0.36F }, // 1 2.45 3
55 { 1.01F, 0.44F, 0.45F }, // 2 1.90 5
56 { 0.52F, 0.53F, 0.14F }, // 3 1.19 7
57 { 0.47F, 0.28F, 0.18F }, // 4 0.93 9
58 { 0.30F, 0.14F, 0.27F }
59 }, // 5 0.71 16384-25=16359
60
61 // For handdrawn/painted sketch (sketch=1):
62 // Y I Q
63
64 {
65 { 4.04F, 15.14F, 22.62F },
66 { 0.78F, 0.92F, 0.40F },
67 { 0.46F, 0.53F, 0.63F },
68 { 0.42F, 0.26F, 0.25F },
69 { 0.41F, 0.14F, 0.15F },
70 { 0.32F, 0.07F, 0.38F }
71 }
72};
73
77enum { NumberOfPixels = 128 };
78
83
88
89typedef double Unit;
90
94typedef qint32 Idx;
95
96// ---------------------------------------------------------------------------------
97
99{
100public:
101
105
106 void fillPixelData(const QImage& image);
107 void fillPixelData(const DImg& image);
108};
109
110// ---------------------------------------------------------------------------------
111
113{
114public:
115
120
124 double avg[3] = { 0.0 };
125};
126
127// ---------------------------------------------------------------------------------
128
134{
135public:
136
141
143 {
144 delete[] m_indexList;
145 }
146
148
149 void fill(const Haar::Idx* const coefs)
150 {
151 // For maximum performance, we use a flat array.
152 // First 16k for negative values, second 16k for positive values.
153 // All values or false, only 2*40 are true.
154
156 int x = 0;
157
158 for (int i = 0 ; i < Haar::NumberOfCoefficients ; ++i)
159 {
160 x = coefs[i] + Haar::NumberOfPixelsSquared;
161 m_indexList[x] = true;
162 }
163 }
164
167
168 bool operator[](Haar::Idx index) const
169 {
171 }
172
173private:
174
175 // To prevent cppcheck warnings.
176
177 explicit SignatureMap(const SignatureMap& other)
178 {
181 }
182
183public:
184
185 typedef bool MapIndexType;
187
188private:
189
190 SignatureMap& operator=(const SignatureMap&); // Disable
191};
192
193// ---------------------------------------------------------------------------------
194
196{
197public:
198
199 WeightBin();
200
201 unsigned char bin(int index) const
202 {
203 if ((index >= 0) && (index < 16384))
204 {
205 return m_bin[index];
206 }
207
208 return 0;
209 }
210
211 unsigned char binAbs(int index) const
212 {
213 if ((index > 0) && (index < 16384))
214 {
215 return m_bin[index];
216 }
217 else if ((index > -16384) && (index <= 0))
218 {
219 return m_bin[-index];
220 }
221
222 return 0;
223 }
224
225public:
226
232 unsigned char m_bin[16384] = { 0 };
233};
234
235// ---------------------------------------------------------------------------------
236
238{
239public:
240
246
247public:
248
250 : m_type(type)
251 {
252 }
253
254 float weight(int weight, int channel) const
255 {
256 return (s_haar_weights[(int)m_type][weight][channel]);
257 }
258
259 float weightForAverage(int channel) const
260 {
261 return (s_haar_weights[(int)m_type][0][channel]);
262 }
263
264private:
265
266 SketchType m_type = ScannedSketch;
267};
268
269// ---------------------------------------------------------------------------------
270
272{
273
274public:
275
276 Calculator() = default;
277 ~Calculator() = default;
278
279 int calcHaar(ImageData* const imageData, SignatureData* const sigData);
280
281 void transform(ImageData* const data);
282
283private:
284
285 void haar2D(Unit a[]);
286 inline void getmLargests(Unit* const cdata, Idx* const sig);
287};
288
289} // namespace Haar
290
291} // namespace Digikam
Definition dimg.h:52
Definition haar.h:272
void transform(ImageData *const data)
Definition haar.cpp:259
int calcHaar(ImageData *const imageData, SignatureData *const sigData)
Definition haar.cpp:362
Definition haar.h:99
void fillPixelData(const QImage &image)
Definition haar.cpp:74
Unit data3[NumberOfPixelsSquared]
Definition haar.h:104
Unit data1[NumberOfPixelsSquared]
Definition haar.h:102
Unit data2[NumberOfPixelsSquared]
Definition haar.h:103
Definition haar.h:113
double avg[3]
Definition haar.h:124
Haar::Idx sig[3][Haar::NumberOfCoefficients]
Definition haar.h:119
Definition haar.h:134
~SignatureMap()
Definition haar.h:142
SignatureMap()
Definition haar.h:137
bool MapIndexType
Definition haar.h:185
MapIndexType * m_indexList
Definition haar.h:186
bool operator[](Haar::Idx index) const
Query if the given index is set. Index must be in the range -16383..16383.
Definition haar.h:168
void fill(const Haar::Idx *const coefs)
Load a set of coefficients.
Definition haar.h:149
Definition haar.h:196
unsigned char binAbs(int index) const
Definition haar.h:211
unsigned char m_bin[16384]
Definition haar.h:232
WeightBin()
Definition haar.cpp:127
unsigned char bin(int index) const
Definition haar.h:201
Definition haar.h:238
Weights(SketchType type=ScannedSketch)
Definition haar.h:249
float weight(int weight, int channel) const
Definition haar.h:254
SketchType
Definition haar.h:242
@ ScannedSketch
Definition haar.h:243
@ PaintedSketch
Definition haar.h:244
float weightForAverage(int channel) const
Definition haar.h:259
@ NumberOfPixels
Definition haar.h:77
@ NumberOfPixelsSquared
Definition haar.h:82
qint32 Idx
Definition haar.h:94
double Unit
Definition haar.h:89
@ NumberOfCoefficients
Definition haar.h:87
Definition datefolderview.cpp:34