1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2003-01-17
 * Description : Haar Database interface
 *
 * SPDX-FileCopyrightText: 2016-2018 by Mario Frank <mario dot frank at uni minus potsdam dot de>
 * SPDX-FileCopyrightText: 2003      by Ricardo Niederberger Cabral <nieder at mail dot ru>
 * SPDX-FileCopyrightText: 2009-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * SPDX-FileCopyrightText: 2009-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 * SPDX-FileCopyrightText: 2009-2011 by Andi Clemens <andi dot clemens at gmail dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "haariface_p.h"

#define ENABLE_DEBUG_DUPLICATES 0

#if ENABLE_DEBUG_DUPLICATES
#   define DEBUG_DUPLICATES(x) qCDebug(DIGIKAM_DATABASE_LOG) << x;
#else
#   define DEBUG_DUPLICATES(x)
#endif

namespace Digikam
{

HaarIface::HaarIface()
    : d(new Private)
{
    qRegisterMetaType<DuplicatesResultsMap>("HaarIface::DuplicatesResultsMap");
}

HaarIface::HaarIface(const QSet<qlonglong>& images2Scan)
    : HaarIface()
{
    d->rebuildSignatureCache(images2Scan);
}

HaarIface::~HaarIface()
{
    delete d;
}

void HaarIface::setAlbumRootsToSearch(const QList<int>& albumRootIds)
{
    setAlbumRootsToSearch(QSet<int>(albumRootIds.begin(), albumRootIds.end()));
}

void HaarIface::setAlbumRootsToSearch(const QSet<int>& albumRootIds)
{
    d->setAlbumRootsToSearch(albumRootIds);
}

int HaarIface::preferredSize()
{
    return Haar::NumberOfPixels;
}

bool HaarIface::indexImage(const QString& filename)
{
    QImage image = loadQImage(filename);

    if (image.isNull())
    {
        return false;
    }

    return indexImage(filename, image);
}

bool HaarIface::indexImage(const QString& filename, const QImage& image)
{
    ItemInfo info = ItemInfo::fromLocalFile(filename);

    if (info.isNull())
    {
        return false;
    }

    return indexImage(info.id(), image);
}

bool HaarIface::indexImage(const QString& filename, const DImg& image)
{
    ItemInfo info = ItemInfo::fromLocalFile(filename);

    if (info.isNull())
    {
        return false;
    }

    return indexImage(info.id(), image);
}

bool HaarIface::indexImage(qlonglong imageid, const QImage& image)
{
    if (image.isNull())
    {
        return false;
    }

    d->setImageDataFromImage(image);

    return indexImage(imageid);
}

bool HaarIface::indexImage(qlonglong imageid, const DImg& image)
{
    if (image.isNull())
    {
        return false;
    }

    d->setImageDataFromImage(image);

    return indexImage(imageid);
}

// NOTE: private method: d->m_data has been filled

bool HaarIface::indexImage(qlonglong imageid)
{
    Haar::Calculator haar;
    haar.transform(d->imageData());

    Haar::SignatureData sig;
    haar.calcHaar(d->imageData(), &sig);

    // Store main entry

    DatabaseBlob blob;
    QByteArray array = blob.write(sig);

    ItemInfo info(imageid);

    if (!info.isNull() && info.isVisible())
    {
        QDateTime modDateTime = SimilarityDbAccess().backend()->asDBDateTime(info.modDateTime());

        SimilarityDbAccess().backend()->execSql(QString::fromUtf8("REPLACE INTO ImageHaarMatrix "
                                                                  " (imageid, modificationDate, uniqueHash, matrix) "
                                                                  " VALUES(?, ?, ?, ?);"),
                                                imageid, modDateTime, info.uniqueHash(), array);
    }

    return true;
}

QString HaarIface::signatureAsText(const QImage& image)
{
    d->setImageDataFromImage(image);

    Haar::Calculator haar;
    haar.transform(d->imageData());
    Haar::SignatureData sig;
    haar.calcHaar(d->imageData(), &sig);

    DatabaseBlob blob;
    QByteArray array = blob.write(sig);

    return QString::fromUtf8(array.toBase64());
}

QPair<double, QMap<qlonglong, double> > HaarIface::bestMatchesForImageWithThreshold(const QString& imagePath,
                                                                                    double requiredPercentage,
                                                                                    double maximumPercentage,
                                                                                    const QList<int>& targetAlbums,
                                                                                        DuplicatesSearchRestrictions
                                                                                    searchResultRestriction,
                                                                                    SketchType type)
{
    DImg image(imagePath);

    if (image.isNull())
    {
        return QPair<double, QMap<qlonglong, double> >();
    }

    d->setImageDataFromImage(image);

    Haar::Calculator haar;
    haar.transform(d->imageData());
    Haar::SignatureData sig;
    haar.calcHaar(d->imageData(), &sig);

    // Remove all previous similarities from pictures

    SimilarityDbAccess().db()->removeImageSimilarity(0);

    // Apply duplicates search for the image. Use the image id 0 which cannot be present.

    return bestMatchesWithThreshold(0,
                                    &sig,
                                    requiredPercentage,
                                    maximumPercentage,
                                    targetAlbums,
                                    searchResultRestriction,
                                    type);
}

QPair<double, QMap<qlonglong, double> > HaarIface::bestMatchesForImageWithThreshold(qlonglong imageId,
                                                                                    double requiredPercentage,
                                                                                    double maximumPercentage,
                                                                                    const QList<int>& targetAlbums,
                                                                                        DuplicatesSearchRestrictions
                                                                                    searchResultRestriction,
                                                                                    SketchType type)
{
    Haar::SignatureData sig;

    if (d->hasSignatureCache())
    {
        if (!d->retrieveSignatureFromCache(imageId, sig))
        {
            return {};
        }
    }
    else
    {
        if (!retrieveSignatureFromDB(imageId, sig))
        {
            return {};
        }
    }

    return bestMatchesWithThreshold(imageId,
                                    &sig,
                                    requiredPercentage,
                                    maximumPercentage,
                                    targetAlbums,
                                    searchResultRestriction,
                                    type);
}

QMap<qlonglong, double> HaarIface::bestMatchesForSignature(const QString& signature,
                                                           const QList<int>& targetAlbums,
                                                           int numberOfResults,
                                                           SketchType type)
{
    QByteArray bytes = QByteArray::fromBase64(signature.toLatin1());

    DatabaseBlob blobReader;
    Haar::SignatureData sig;
    blobReader.read(bytes, sig);

    // Get all matching images with their score and save their similarity to the signature, i.e. id -2

    QMultiMap<double, qlonglong> matches = bestMatches(&sig, numberOfResults, targetAlbums, type);
    QMap<qlonglong, double> result;

    for (QMultiMap<double, qlonglong>::const_iterator it = matches.constBegin() ;
         it != matches.constEnd() ; ++it)
    {
        // Add the image id and the normalised score (make sure that it is positive and between 0 and 1.

        result.insert(it.value(), (0.0 - (it.key() / 100)));
    }

    return result;
}

QMultiMap<double, qlonglong> HaarIface::bestMatches(const Haar::SignatureData* const querySig,
                                                    int numberOfResults,
                                                    const QList<int>& targetAlbums,
                                                    SketchType type)
{
    QMap<qlonglong, double> scores = searchDatabase(querySig, type, targetAlbums);

    // Find out the best matches, those with the lowest score
    // We make use of the feature that QMap keys are sorted in ascending order
    // Of course, images can have the same score, so we need a multi map

    QMultiMap<double, qlonglong> bestMatches;
    bool                         initialFill = false;
    double                       score, worstScore, bestScore;
    qlonglong                    id;

    for (QMap<qlonglong, double>::const_iterator it = scores.constBegin() ;
         it != scores.constEnd() ; ++it)
    {
        score = it.value();
        id    = it.key();

        if (!initialFill)
        {
            // as long as the maximum number of results is not reached, just fill up the map

            bestMatches.insert(score, id);
            initialFill = (bestMatches.size() >= numberOfResults);
        }
        else
        {
            // find the last entry, the one with the highest (=worst) score

            QMultiMap<double, qlonglong>::iterator last = bestMatches.end();
            --last;
            worstScore = last.key();

            // if the new entry has a higher score, put it in the list and remove that last one

            if      (score < worstScore)
            {
                bestMatches.erase(last);
                bestMatches.insert(score, id);
            }
            else if (score == worstScore)
            {
                bestScore = bestMatches.begin().key();

                // if the score is identical for all entries, increase the maximum result number

                if (score == bestScore)
                {
                    bestMatches.insert(score, id);
                }
            }
        }
    }

/*
    for (QMap<double, qlonglong>::iterator it = bestMatches.begin(); it != bestMatches.end(); ++it)
    {
        qCDebug(DIGIKAM_DATABASE_LOG) << it.key() << it.value();
    }
*/

    return bestMatches;
}

QPair<double, QMap<qlonglong, double> > HaarIface::bestMatchesWithThreshold(qlonglong imageid,
                                                                            Haar::SignatureData* const querySig,
                                                                            double requiredPercentage,
                                                                            double maximumPercentage,
                                                                            const QList<int>& targetAlbums,
                                                                                DuplicatesSearchRestrictions
                                                                            searchResultRestriction,
                                                                            SketchType type)
{
    int albumId                    = CoreDbAccess().db()->getItemAlbum(imageid);
    QMap<qlonglong, double> scores = searchDatabase(querySig,
                                                    type,
                                                    targetAlbums,
                                                    searchResultRestriction,
                                                    imageid,
                                                    albumId);
    double lowest, highest;
    getBestAndWorstPossibleScore(querySig, type, &lowest, &highest);

    // The range between the highest (worst) and lowest (best) score
    // example: 0.2 and 0.5 -> 0.3

    double scoreRange      = highest - lowest;

    // The lower the requiredPercentage is, the higher will the result be.
    // example: 0.7 -> 0.3

    double percentageRange = 1.0 - requiredPercentage;

    // example: 0.2 + (0.3 * 0.3) = 0.2 + 0.09 = 0.29

    double requiredScore   = lowest + scoreRange * percentageRange;

    // Set the supremum which solves the problem that if
    // required == maximum, no results will be returned.
    // Eg, id required == maximum == 50.0, only images with exactly this
    // similarity are returned. But users expect also to see images
    // with similarity 50,x.

    double supremum = (floor(maximumPercentage * 100 + 1.0)) / 100;

    QMap<qlonglong, double> bestMatches;<--- Shadow variable
    double score, percentage, avgPercentage = 0.0;
    QPair<double, QMap<qlonglong, double> > result;
    qlonglong id;

    for (QMap<qlonglong, double>::const_iterator it = scores.constBegin() ;
         it != scores.constEnd() ; ++it)
    {
        score = it.value();
        id    = it.key();

        // If the score of the picture is at most the required (maximum) score and

        if (score <= requiredScore)
        {
            percentage = 1.0 - (score - lowest) / scoreRange;

            // If the found image is the original one (check by id) or the percentage is below the maximum.

            if ((id == imageid) || (percentage < supremum))
            {
                bestMatches.insert(id, percentage);

                // If the current image is not the original, use the images similarity for the average percentage
                // Also, save the similarity of the found image to the original image.

                if (id != imageid)
                {
                    // Store the similarity if the reference image has a valid image id

                    if (imageid > 0)
                    {
                        SimilarityDbAccess().db()->setImageSimilarity(id, imageid, percentage);
                    }

                    avgPercentage += percentage;
                }
            }
        }
    }

    // Debug output

    if (bestMatches.count() > 1)
    {
        // The average percentage is the sum of all percentages
        // (without the original picture) divided by the count of pictures -1.
        // Subtracting 1 is necessary since the original picture is not used for the calculation.

        avgPercentage = avgPercentage / (bestMatches.count() - 1);

        qCDebug(DIGIKAM_DATABASE_LOG) << "Duplicates with id and score:";

        for (QMap<qlonglong, double>::const_iterator it = bestMatches.constBegin() ; it != bestMatches.constEnd() ; ++it)
        {
            qCDebug(DIGIKAM_DATABASE_LOG) << it.key() << QString::number(it.value() * 100) + QLatin1Char('%');
        }
    }

    result.first  = avgPercentage;
    result.second = bestMatches;

    return result;
}

bool HaarIface::fulfillsRestrictions(qlonglong imageId, int albumId,
                                     qlonglong originalImageId,
                                     int originalAlbumId,
                                     const QList<int>& targetAlbums,
                                     DuplicatesSearchRestrictions searchResultRestriction)
{
    if      (imageId == originalImageId)
    {
        return true;
    }
    else if (targetAlbums.isEmpty() || targetAlbums.contains(albumId))
    {
        return (
                (searchResultRestriction == None)                                             ||
                ((searchResultRestriction == SameAlbum)      && (originalAlbumId == albumId)) ||
                ((searchResultRestriction == DifferentAlbum) && (originalAlbumId != albumId))
               ) ;
    }
    else
    {
        return false;
    }
}

QMap<qlonglong, double> HaarIface::searchDatabase(const Haar::SignatureData* const querySig,
                                                  SketchType type, const QList<int>& targetAlbums,
                                                  DuplicatesSearchRestrictions searchResultRestriction,
                                                  qlonglong originalImageId,
                                                  int originalAlbumId)
{
    // The table of constant weight factors applied to each channel and the weight bin

    Haar::Weights weights((Haar::Weights::SketchType)type);

    // layout the query signature for fast lookup

    Haar::SignatureMap queryMapY, queryMapI, queryMapQ;
    queryMapY.fill(querySig->sig[0]);
    queryMapI.fill(querySig->sig[1]);
    queryMapQ.fill(querySig->sig[2]);
    const std::reference_wrapper<Haar::SignatureMap> queryMaps[3] = { queryMapY, queryMapI, queryMapQ };

    // Map imageid -> score. Lowest score is best.
    // any newly inserted value will be initialized with a score of 0, as required

    QMap<qlonglong, double> scores;

    // if no cache is used or the cache signature map is empty, query the database

    if (!d->hasSignatureCache())
    {
        d->rebuildSignatureCache();
    }

    for (auto it = d->signatureCache()->constBegin() ; it != d->signatureCache()->constEnd() ; ++it)
    {
        // If the image is the original one or
        // No restrictions apply or
        // SameAlbum restriction applies and the albums are equal or
        // DifferentAlbum restriction applies and the albums differ
        // then calculate the score.

        const qlonglong& imageId = it.key();

        if (fulfillsRestrictions(imageId, d->albumCache()->value(imageId), originalImageId,
                                 originalAlbumId, targetAlbums, searchResultRestriction))
        {
            const Haar::SignatureData& data = it.value();
            scores[imageId]                 = calculateScore(*querySig, data, weights, queryMaps);
        }
    }

    return scores;
}

QImage HaarIface::loadQImage(const QString& filename)
{
    // NOTE: Can be optimized using DImg.

    QImage image;

    if (JPEGUtils::isJpegImage(filename))
    {
        // use fast jpeg loading

        if (!JPEGUtils::loadJPEGScaled(image, filename, Haar::NumberOfPixels))
        {
            // try QT now.

            if (!image.load(filename))
            {
                return QImage();
            }
        }
    }
    else
    {
        // use default QT image loading

        if (!image.load(filename))
        {
            return QImage();
        }
    }

    return image;
}

bool HaarIface::retrieveSignatureFromDB(qlonglong imageid, Haar::SignatureData& sig)
{
    QList<QVariant> values;
    SimilarityDbAccess().backend()->execSql(QString::fromUtf8("SELECT matrix FROM ImageHaarMatrix "
                                                              " WHERE imageid=?;"),
                                            imageid, &values);

    if (values.isEmpty())
    {
        return false;
    }

    DatabaseBlob blob;

    blob.read(values.first().toByteArray(), sig);
    return true;
}

void HaarIface::getBestAndWorstPossibleScore(Haar::SignatureData* const sig,
                                             SketchType type,
                                             double* const lowestAndBestScore,
                                             double* const highestAndWorstScore)
{
    Haar::Weights weights(static_cast<Haar::Weights::SketchType>(type));
    double score = 0;

    // In the first step, the score is initialized with the weighted color channel averages.
    // We don't know the target channel average here, we only now its not negative => assume 0

    for (int channel = 0 ; channel < 3 ; ++channel)
    {
        score += weights.weightForAverage(channel) * fabs(sig->avg[channel] /*- targetSig.avg[channel]*/);
    }

    *highestAndWorstScore = score;

    // Next consideration: The lowest possible score is reached if the signature is identical.
    // The first step (see above) will result in 0 - skip it.
    // In the second step, for every coefficient in the sig that have query and target in common,
    // so in our case all 3*40, subtract the specifically assigned weighting.

    score = 0;

    for (int channel = 0 ; channel < 3 ; ++channel)
    {
        Haar::Idx* const coefs = sig->sig[channel];

        for (int coef = 0 ; coef < Haar::NumberOfCoefficients ; ++coef)
        {
            score -= weights.weight(d->weightBin.binAbs(coefs[coef]), channel);
        }
    }

    *lowestAndBestScore = score;
}


QMap<QString, QString> HaarIface::writeSAlbumQueries(const DuplicatesResultsMap& searchResults)
{
    // Build search XML from the results. Store list of ids of similar images.

    QMap<QString, QString> queries;

    for (auto it = searchResults.constBegin() ; it != searchResults.constEnd() ; ++it)
    {
        SearchXmlWriter writer;
        writer.writeGroup();
        writer.writeField(QLatin1String("imageid"), SearchXml::OneOf);
        writer.writeValue(it->second);
        writer.finishField();

        // Add the average similarity as field

        writer.writeField(QLatin1String("noeffect_avgsim"), SearchXml::Equal);
        writer.writeValue(it->first * 100);
        writer.finishField();
        writer.finishGroup();
        writer.finish();

        // Use the id of the first duplicate as name of the search

        queries.insert(QString::number(it.key()), writer.xml());
    }

    return queries;
}

void HaarIface::rebuildDuplicatesAlbums(const DuplicatesResultsMap& results, bool isAlbumUpdate)
{
    // Build search XML from the results. Store list of ids of similar images.

    QMap<QString, QString> queries = writeSAlbumQueries(results);

    // Write the new search albums to the database.

    CoreDbAccess access;
    CoreDbTransaction transaction(&access);

    // Full rebuild: delete all old searches.

    if (!isAlbumUpdate)
    {
        access.db()->deleteSearches(DatabaseSearch::DuplicatesSearch);
    }

    // Create new groups, or update existing searches.

    for (QMap<QString, QString>::const_iterator it = queries.constBegin() ;
         it != queries.constEnd() ; ++it)
    {
        if (isAlbumUpdate)
        {
            access.db()->deleteSearch(it.key().toInt());
        }

        access.db()->addSearch(DatabaseSearch::DuplicatesSearch, it.key(), it.value());
    }
}

QSet<qlonglong> HaarIface::imagesFromAlbumsAndTags(const QList<int>& albums2Scan,
                                                   const QList<int>& tags2Scan,
                                                   AlbumTagRelation relation)
{
    QSet<qlonglong> imagesFromAlbums;
    QSet<qlonglong> imagesFromTags;
    QSet<qlonglong> images;

    // Get all items DB id from all albums and all collections

    for (int albumId : std::as_const(albums2Scan))
    {
        const auto list = CoreDbAccess().db()->getItemIDsInAlbum(albumId);
        imagesFromAlbums.unite(QSet<qlonglong>(list.begin(), list.end()));
    }

    // Get all items DB id from all tags

    for (int albumId : std::as_const(tags2Scan))
    {
        const auto list = CoreDbAccess().db()->getItemIDsInTag(albumId);
        imagesFromTags.unite(QSet<qlonglong>(list.begin(), list.end()));
    }

    switch (relation)
    {
        case Union:
        {
            // ({} UNION A) UNION T = A UNION T

            images.unite(imagesFromAlbums).unite(imagesFromTags);
            break;
        }

        case Intersection:
        {
            // ({} UNION A) INTERSECT T = A INTERSECT T

            images.unite(imagesFromAlbums).intersect(imagesFromTags);
            break;
        }

        case AlbumExclusive:
        {
            // ({} UNION A) = A

            images.unite(imagesFromAlbums);

            // (A INTERSECT T) = A'

            imagesFromAlbums.intersect(imagesFromTags);

            // A\A' = albums without tags

            images.subtract(imagesFromAlbums);
            break;
        }

        case TagExclusive:
        {
            // ({} UNION T) = TT

            images.unite(imagesFromTags);

            // (A INTERSECT T) = A' = T'

            imagesFromAlbums.intersect(imagesFromTags);

            // T\T' = tags without albums

            images.subtract(imagesFromAlbums);
            break;
        }

        case NoMix:
        {
            if ((albums2Scan.isEmpty() && tags2Scan.isEmpty()))
            {
                qCWarning(DIGIKAM_GENERAL_LOG) << "Duplicates search: Both the albums and the tags "
                                                  "list are non-empty but the album/tag relation "
                                                  "stated a NoMix. Skipping duplicates search";
                return {};
            }
            else
            {
                // ({} UNION A) UNION T = A UNION T = A Xor T

                images.unite(imagesFromAlbums).unite(imagesFromTags);
            }
        }
    }

    return images;
}

HaarIface::DuplicatesResultsMap HaarIface::findDuplicates(const QSet<qlonglong>& images2Scan,
                                                          const QSet<qlonglong>::const_iterator& rangeBegin,
                                                          const QSet<qlonglong>::const_iterator& rangeEnd,
                                                          RefImageSelMethod refImageSelectionMethod,
                                                          const QSet<qlonglong>& refs,
                                                          double requiredPercentage,
                                                          double maximumPercentage,
                                                          DuplicatesSearchRestrictions searchResultRestriction,
                                                          HaarProgressObserver* const observer)
{
    static const QList<int>                 emptyTargetAlbums;
    DuplicatesResultsMap                    resultsMap;
    DuplicatesResultsMap::iterator          resultsIterator;
    QSet<qlonglong>::const_iterator         images2ScanIterator;
    QPair<double, QMap<qlonglong, double> > bestMatches;<--- Shadow variable
    QList<qlonglong>                        duplicates;
    QSet<qlonglong>                         resultsCandidates;
    const bool                              singleThread = ((rangeBegin == images2Scan.constBegin()) &&
                                                            (rangeEnd   == images2Scan.constEnd()));

    // create signature cache map for fast lookup

    if (!d->hasSignatureCache())
    {
        d->rebuildSignatureCache(images2Scan);
    }

    for (images2ScanIterator = rangeBegin ; images2ScanIterator != rangeEnd ; ++images2ScanIterator)
    {

#if ENABLE_DEBUG_DUPLICATES

        {
            ItemInfo info(*images2ScanIterator);
            const QString path = info.filePath();
            const QString name = info.name();
            DEBUG_DUPLICATES("Iterate image: " << name << "Path: " << path);
        }

#endif

        if (observer && observer->isCanceled())
        {
            break;
        }

        if (!resultsCandidates.contains(*images2ScanIterator))
        {
            // find images with required similarity

            bestMatches = bestMatchesForImageWithThreshold(*images2ScanIterator,
                                                           requiredPercentage,
                                                           maximumPercentage,
                                                           emptyTargetAlbums,
                                                           searchResultRestriction,
                                                           ScannedSketch);

            // We need only the image ids from the best matches map.

            duplicates = bestMatches.second.keys();

            // the list will usually contain one image: the original. Filter out.

            if (
                !(duplicates.isEmpty())     &&
                !((duplicates.count() == 1) &&
                (duplicates.first() == *images2ScanIterator))
               )
            {
                DEBUG_DUPLICATES("\tHas duplicates");

                // Use the oldest image date or larger pixel/file size as the reference image.
                // Or if the image is in the refImage list

                QDateTime refDateTime;
                QDateTime refModDateTime;
                quint64   refPixelSize  = 0;
                qlonglong refFileSize   = 0;
                qlonglong reference     = *images2ScanIterator;

                const bool useReferenceImages = (
                                                 (refImageSelectionMethod == RefImageSelMethod::PreferFolder) ||
                                                 (refImageSelectionMethod == RefImageSelMethod::ExcludeFolder)
                                                );

                bool referenceFound = false;

                if (useReferenceImages)
                {
                    for (auto it = refs.begin() ; it != refs.end() ; ++it)
                    {

#if ENABLE_DEBUG_DUPLICATES

                        {
                            ItemInfo info(*it);
                            const QString path = info.filePath();
                            const QString name = info.name();
                            DEBUG_DUPLICATES("\tReference image: " << name << "Path: " << path << ", Id: " << info.id());
                        }

#endif

                        if (*it == *images2ScanIterator)
                        {
                            // image of images2ScanIterator is already in the references present, so take it as the
                            // reference

                            DEBUG_DUPLICATES("\tReference found!");
                            referenceFound = true;
                            break;
                        }
                    }
                }

                if (
                    !useReferenceImages                                                               ||
                    (!referenceFound && (refImageSelectionMethod == RefImageSelMethod::PreferFolder)) ||
                    (referenceFound  && (refImageSelectionMethod == RefImageSelMethod::ExcludeFolder))
                   )
                {
                    DEBUG_DUPLICATES("\tChecking Duplicates")

                    for (const qlonglong& refId : std::as_const(duplicates))
                    {

#if ENABLE_DEBUG_DUPLICATES

                        {
                            ItemInfo info(refId);
                            const QString path = info.filePath();
                            const QString name = info.name();
                            DEBUG_DUPLICATES("\t\tDuplicates: " << name << "Path: " << path << ", Id: " << info.id());
                        }

#endif

                        ItemInfo info(refId);
                        quint64 infoPixelSize = (quint64)info.dimensions().width() *
                                                (quint64)info.dimensions().height();

                        referenceFound = false;

                        if (useReferenceImages)
                        {
                            for (auto it = refs.begin() ; it != refs.end() ; ++it)
                            {

#if ENABLE_DEBUG_DUPLICATES

                                {
                                    ItemInfo info(*it);
                                    const QString path = info.filePath();
                                    const QString name = info.name();
                                    DEBUG_DUPLICATES("\t\tReference image: " << name << "Path: " << path << ", Id: " << info.id());
                                }

#endif

                                if (*it == refId)
                                {
                                    DEBUG_DUPLICATES("\t\tReference found!");
                                    referenceFound = true;
                                    break;
                                }
                            }
                        }

                        const bool preferFolderCond  = (
                                                        referenceFound &&
                                                        (refImageSelectionMethod == RefImageSelMethod::PreferFolder)
                                                       );

                        const bool excludeFolderCond = (
                                                        !referenceFound &&
                                                        (refImageSelectionMethod == RefImageSelMethod::ExcludeFolder)
                                                       );

                        const bool newerCreationCond = (
                                                        (refImageSelectionMethod == RefImageSelMethod::NewerCreationDate) &&
                                                        (!refDateTime.isValid() || (info.dateTime() >  refDateTime))
                                                       );

                        const bool newerModCond      = (
                                                        (refImageSelectionMethod == RefImageSelMethod::NewerModificationDate) &&
                                                        (!refModDateTime.isValid() || (info.modDateTime() >  refModDateTime))
                                                       );

                        const bool olderOrLargerCond = (
                                                        (refImageSelectionMethod == RefImageSelMethod::OlderOrLarger)          &&
                                                        (!refDateTime.isValid()                                                ||
                                                        (infoPixelSize   >  refPixelSize)                                      ||
                                                        ((infoPixelSize  == refPixelSize) && (info.fileSize() >  refFileSize)) ||
                                                        (
                                                         (infoPixelSize  == refPixelSize) && (info.fileSize() == refFileSize)  &&
                                                         (info.dateTime() <  refDateTime))
                                                        )
                                                       );

                        if (preferFolderCond || excludeFolderCond || newerCreationCond || newerModCond || olderOrLargerCond)
                        {
                            reference      = refId;
                            refDateTime    = info.dateTime();
                            refModDateTime = info.modDateTime();
                            refFileSize    = info.fileSize();
                            refPixelSize   = infoPixelSize;

#if ENABLE_DEBUG_DUPLICATES

                            {
                                const QString path = info.filePath();
                                const QString name = info.name();
                                DEBUG_DUPLICATES("\t\tUse as eference image: " << name << "Path: " << path << ", Id: " << info.id() << "Pixelsize: " << infoPixelSize << ", File size: " << refFileSize << ", Datetime: " << refDateTime);
                            }

#endif

                            if (preferFolderCond || excludeFolderCond)
                            {
                                break;
                            }
                        }
                    }
                }

                resultsMap.insert(reference, qMakePair(bestMatches.first, duplicates));

                resultsCandidates << *images2ScanIterator;
                resultsCandidates.unite(QSet<qlonglong>(duplicates.begin(), duplicates.end()));
            }
        }

        // if an imageid is not a results candidate, remove it
        // from the cached signature map as well,
        // to greatly improve speed

        if (singleThread && !resultsCandidates.contains(*images2ScanIterator))
        {
            d->signatureCache()->remove(*images2ScanIterator);
        }

        if (observer)
        {
            ItemInfo info(*images2ScanIterator);
            observer->imageProcessed(
                                     info,
                                     QImage(),              // See bug 496691: performance issue - do not forward a loaded preview.
                                     duplicates.count()
                                    );
        }
    }

#if ENABLE_DEBUG_DUPLICATES

    DEBUG_DUPLICATES("Results:");

    for (auto i = resultsMap.constBegin() ; i != resultsMap.constEnd() ; ++i)
    {
        ItemInfo info(i.key());
        const QString path = info.filePath();
        const QString name = info.name();
        DEBUG_DUPLICATES("\t\tReference image: " << name << "Path: " << path << ", Id: " << info.id());
    }

#endif

    return resultsMap;
}

double HaarIface::calculateScore(const Haar::SignatureData& querySig,
                                 const Haar::SignatureData& targetSig,
                                 const Haar::Weights& weights,
                                 const std::reference_wrapper<Haar::SignatureMap>* const queryMaps)
{
    double score = 0.0;

    // Step 1: Initialize scores with average intensity values of all three channels

    for (int channel = 0 ; channel < 3 ; ++channel)
    {
        score += weights.weightForAverage(channel) * fabs(querySig.avg[channel] - targetSig.avg[channel]);
    }

    // Step 2: Decrease the score if query and target have significant coefficients in common

    int x        = 0;

    for (int channel = 0 ; channel < 3 ; ++channel)
    {
        const Haar::SignatureMap& queryMap = queryMaps[channel];

        for (int coef = 0 ; coef < Haar::NumberOfCoefficients ; ++coef)
        {
            // x is a pixel index, either positive or negative, 0..16384

            x = targetSig.sig[channel][coef];

            // If x is a significant coefficient with the same sign in the query signature as well,
            // decrease the score (lower is better)
            // Note: both method calls called with x accept positive or negative values

            if ((queryMap)[x])
            {
                score -= weights.weight(d->weightBin.binAbs(x), channel);
            }
        }
    }

    return score;
}

} // namespace Digikam