summaryrefslogtreecommitdiffstats
path: root/krita/core/kis_basic_math_toolbox.cpp
blob: d20544afe1525e4df960ee54250ce73d919e4d65 (plain)
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
/*
 *  This file is part of the KDE project
 *
 *  Copyright (c) 2005 Cyrille Berger <cberger@cberger.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kis_basic_math_toolbox.h"

KisBasicMathToolbox::KisBasicMathToolbox()
    : KisMathToolbox(KisID("Basic"))
{
}


KisBasicMathToolbox::~KisBasicMathToolbox()
{
}


void KisBasicMathToolbox::wavetrans(KisMathToolbox::KisWavelet* wav, KisMathToolbox::KisWavelet* buff, uint halfsize)
{
    uint l = (2*halfsize)*wav->depth*sizeof(float);
    for(uint i = 0; i < halfsize; i++)
    {
        float * itLL = buff->coeffs + i*buff->size*buff->depth;
        float * itHL = buff->coeffs + (i*buff->size + halfsize)*buff->depth;
        float * itLH = buff->coeffs + (halfsize+i)*buff->size*buff->depth;
        float * itHH = buff->coeffs + ( (halfsize+i)*buff->size + halfsize)*buff->depth;
        float * itS11 = wav->coeffs + 2*i*wav->size*wav->depth;
        float * itS12 = wav->coeffs + (2*i*wav->size+1)*wav->depth;
        float * itS21 = wav->coeffs + (2*i+1)*wav->size*wav->depth;
        float * itS22 = wav->coeffs + ((2*i+1)*wav->size+1)*wav->depth;
        for(uint j = 0; j < halfsize; j++)
        {
            for( uint k = 0; k < wav->depth; k++)
            {
                *(itLL++) = (*itS11 + *itS12 + *itS21 + *itS22) * M_SQRT1_2;
                *(itHL++) = (*itS11 - *itS12 + *itS21 - *itS22) * M_SQRT1_2;
                *(itLH++) = (*itS11 + *itS12 - *itS21 - *itS22) * M_SQRT1_2;
                *(itHH++) = (*(itS11++) - *(itS12++) - *(itS21++) + *(itS22++)) * M_SQRT1_2;
            }
            itS11 += wav->depth; itS12 += wav->depth;
            itS21 += wav->depth; itS22 += wav->depth;
        }
        emit nextStep();
    }
    for(uint i = 0; i < halfsize; i++)
    {
        uint p = i*wav->size*wav->depth;
        memcpy(wav->coeffs + p, buff->coeffs + p, l);
        p = (i + halfsize )*wav->size*wav->depth;
        memcpy(wav->coeffs + p, buff->coeffs + p, l);
    }
    if(halfsize != 1)
    {
        wavetrans(wav, buff, halfsize/2);
    }
}

void KisBasicMathToolbox::waveuntrans(KisMathToolbox::KisWavelet* wav, KisMathToolbox::KisWavelet* buff, uint halfsize)
{
    uint l = (2*halfsize)*wav->depth*sizeof(float);
    for(uint i = 0; i < halfsize; i++)
    {
        float * itLL = wav->coeffs + i*buff->size*buff->depth;
        float * itHL = wav->coeffs + (i*buff->size + halfsize)*buff->depth;
        float * itLH = wav->coeffs + (halfsize+i)*buff->size*buff->depth;
        float * itHH = wav->coeffs + ( (halfsize+i)*buff->size + halfsize)*buff->depth;
        float * itS11 = buff->coeffs + 2*i*wav->size*wav->depth;
        float * itS12 = buff->coeffs + (2*i*wav->size+1)*wav->depth;
        float * itS21 = buff->coeffs + (2*i+1)*wav->size*wav->depth;
        float * itS22 = buff->coeffs + ((2*i+1)*wav->size+1)*wav->depth;
        for(uint j = 0; j < halfsize; j++)
        {
            for( uint k = 0; k < wav->depth; k++)
            {
                *(itS11++) = (*itLL + *itHL + *itLH + *itHH)*0.25*M_SQRT2;
                *(itS12++) = (*itLL - *itHL + *itLH - *itHH)*0.25*M_SQRT2;
                *(itS21++) = (*itLL + *itHL - *itLH - *itHH)*0.25*M_SQRT2;
                *(itS22++) = (*(itLL++) - *(itHL++) - *(itLH++) + *(itHH++))*0.25*M_SQRT2;
            }
            itS11 += wav->depth; itS12 += wav->depth;
            itS21 += wav->depth; itS22 += wav->depth;
        }
        emit nextStep();
    }
    for(uint i = 0; i < halfsize; i++)
    {
        uint p = i*wav->size*wav->depth;
        memcpy(wav->coeffs + p, buff->coeffs + p, l);
        p = (i + halfsize )*wav->size*wav->depth;
        memcpy(wav->coeffs + p, buff->coeffs + p, l);
    }
    
    if(halfsize != wav->size/2)
    {
        waveuntrans(wav, buff, halfsize*2);
    }
}

KisMathToolbox::KisWavelet* KisBasicMathToolbox::fastWaveletTransformation(KisPaintDeviceSP src, const QRect& rect,  KisWavelet* buff)
{
    if(buff == 0)
    {
        buff = initWavelet( src, rect );
    }
    KisWavelet* wav = initWavelet( src, rect );
    transformToFR(src, wav, rect);
    wavetrans(wav, buff, wav->size / 2);
    
    return wav;
}

void KisBasicMathToolbox::fastWaveletUntransformation(KisPaintDeviceSP dst, const QRect& rect, KisWavelet* wav, KisWavelet* buff)
{
    if(buff == 0)
    {
        buff = initWavelet( dst, rect );
    }
    
    waveuntrans(wav, buff, 1 );
    transformFromFR(dst, wav, rect);
}