1 /*
2  *  Copyright (c) 2012 Dmitry Kazakov <dimula73@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "kis_filter_weights_buffer_test.h"
20 
21 #include <QTest>
22 
23 #include "kis_filter_strategy.h"
24 
25 //#define DEBUG_ENABLED
26 #define SANITY_CHECKS_ENABLED
27 #include "kis_filter_weights_buffer.h"
28 
29 #include "kis_debug.h"
30 
checkWeightsBuffer(KisFilterStrategy * filter,qreal scale)31 void checkWeightsBuffer(KisFilterStrategy *filter, qreal scale)
32 {
33     dbgKrita << "Testing:" << filter->name() << "Scale:" << scale;
34 
35     KisFilterWeightsBuffer buf(filter, scale);
36 
37     KisFixedPoint fp1;
38     KisFixedPoint fp2;
39 
40     const int startIndex = 1;
41     const int endIndex = 255 * qMin(scale, qreal(1.0));
42 
43     fp1.from256Frac(startIndex);
44     fp2.from256Frac(endIndex);
45 
46     while (fp2 > fp1) {
47         KisFilterWeightsBuffer::FilterWeights *w1 = buf.weights(fp1);
48         KisFilterWeightsBuffer::FilterWeights *w2 = buf.weights(fp2);
49 
50         QCOMPARE(w1->span, w2->span);
51 
52         int span = w1->span;
53 
54         // Check for symmetry around center offset
55         for (int i = 0; i < span; i++) {
56             int idx2 = span - i - 1;
57 
58             int v1 = w1->weight[i];
59             int v2 = w2->weight[idx2];
60 
61             if (v1 != v2) {
62 
63 #ifdef DEBUG_ENABLED
64                 dbgKrita << "*******";
65                 dbgKrita << "Weight" << fp1 << "|" << i << ":" << v1;
66                 dbgKrita << "Weight" << fp2 << "|" << idx2 << ":" << v2;
67 #endif /* DEBUG_ENABLED */
68 
69                 if (!(span & 0x1) && (qAbs(v1 - v2) <= (0.5 * span))) {
70 #ifdef DEBUG_ENABLED
71                     dbgKrita << "Symmetry is wrong due to evenly-sized kernel or rounding. It's ok. Accepting.";
72 #endif /* DEBUG_ENABLED */
73                 } else {
74                     QFAIL("Wrong weight symmetry");
75                 }
76             }
77         }
78 
79         fp1.inc256Frac();
80         fp2.dec256Frac();
81     }
82 }
83 
checkOneFilter(KisFilterStrategy * filter)84 void checkOneFilter(KisFilterStrategy *filter)
85 {
86     checkWeightsBuffer(filter, 1.0);
87     checkWeightsBuffer(filter, 2.0);
88     checkWeightsBuffer(filter, 0.5);
89     checkWeightsBuffer(filter, 0.25);
90     checkWeightsBuffer(filter, 0.125);
91     delete filter;
92     checkForAsymmetricZeros = false;
93 }
94 
95 
testTriangle()96 void KisFilterWeightsBufferTest::testTriangle()
97 {
98     checkForAsymmetricZeros = true;
99     checkOneFilter(new KisBilinearFilterStrategy());
100 }
101 
testHermite()102 void KisFilterWeightsBufferTest::testHermite()
103 {
104     checkOneFilter(new KisHermiteFilterStrategy());
105 }
106 
testBicubic()107 void KisFilterWeightsBufferTest::testBicubic()
108 {
109     checkOneFilter(new KisBicubicFilterStrategy());
110 }
111 
testBox()112 void KisFilterWeightsBufferTest::testBox()
113 {
114     checkOneFilter(new KisBoxFilterStrategy());
115 }
116 
testBell()117 void KisFilterWeightsBufferTest::testBell()
118 {
119     checkOneFilter(new KisBellFilterStrategy());
120 }
121 
testBSpline()122 void KisFilterWeightsBufferTest::testBSpline()
123 {
124     checkOneFilter(new KisBSplineFilterStrategy());
125 }
126 
testLanczos3()127 void KisFilterWeightsBufferTest::testLanczos3()
128 {
129     checkOneFilter(new KisLanczos3FilterStrategy());
130 }
131 
testMitchell()132 void KisFilterWeightsBufferTest::testMitchell()
133 {
134     checkForAsymmetricZeros = true;
135     checkOneFilter(new KisMitchellFilterStrategy());
136 }
137 
138 
139 
140 QTEST_MAIN(KisFilterWeightsBufferTest)
141