File indexing completed on 2025-01-26 04:11:17

0001 /*
0002  *  SPDX-FileCopyrightText: 2002 Patrick Julien <freak@codepimps.org>
0003  *  SPDX-FileCopyrightText: 2004-2008 Boudewijn Rempt <boud@valdyas.org>
0004  *  SPDX-FileCopyrightText: 2004 Clarence Dang <dang@kde.org>
0005  *  SPDX-FileCopyrightText: 2004 Adrian Page <adrian@pagenet.plus.com>
0006  *  SPDX-FileCopyrightText: 2004 Cyrille Berger <cberger@cberger.net>
0007  *
0008  *  SPDX-License-Identifier: GPL-2.0-or-later
0009  */
0010 
0011 #ifndef KIS_DUPLICATEOP_P_H_
0012 #define KIS_DUPLICATEOP_P_H_
0013 
0014 namespace DuplicateOpUtils {
0015 
0016 inline qreal minimizeEnergy(const qreal* m, qreal* sol, int w, int h)
0017 {
0018     qreal err = 0;
0019 
0020     if (h < 3 || w < 3) {
0021         int size = 3 * w * h;
0022 
0023         for (int i = 0; i < size; i++) {
0024             sol[i] = 1.0;
0025         }
0026 
0027         warnKrita << "WARNING: healing width or height are smaller than 3 px. The result will have artifacts!";
0028 
0029     } else {
0030         int rowstride = 3 * w;
0031 
0032         memcpy(sol, m, 3 * sizeof(qreal) * w);
0033         m += rowstride;
0034         sol += rowstride;
0035         for (int i = 1; i < h - 1; i++) {
0036             memcpy(sol, m, 3 * sizeof(qreal));
0037             m += 3; sol += 3;
0038             for (int j = 3; j < rowstride - 3; j++) {
0039                 qreal tmp = *sol;
0040                 *sol = ((*(m - 3) + * (m + 3) + * (m - rowstride) + * (m + rowstride)) + 2 * *m) / 6;
0041                 qreal diff = *sol - tmp;
0042                 err += diff * diff;
0043                 m ++; sol ++;
0044             }
0045             memcpy(sol, m, 3 * sizeof(qreal));
0046             m += 3; sol += 3;
0047         }
0048         memcpy(sol, m, 3 * sizeof(qreal) * w);
0049     }
0050 
0051     return err;
0052 }
0053 }
0054 
0055 #endif // KIS_DUPLICATEOP_P_H_