summaryrefslogtreecommitdiffstats
path: root/krita/core/kis_math_toolbox.h
blob: 0ddc545f00dd52cbcc6ca8f7ecbd9cb1ef57b525 (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
/*
 *  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.
 */

#ifndef KIS_MATH_TOOLBOX_H
#define KIS_MATH_TOOLBOX_H

#include <qobject.h>

// typedef unsigned int uint;

#include <kis_generic_registry.h>
#include "kis_paint_device.h"
#include "kis_types.h"

#include <new>

class KisMathToolbox : public QObject {
    Q_OBJECT
    public:
        struct KisFloatRepresentation {
            KisFloatRepresentation(uint nsize, uint ndepth) throw(std::bad_alloc ) : coeffs(new float[nsize*nsize*ndepth]) ,size(nsize), depth(ndepth)
            {
                // XXX: Valgrind shows that these are being used without being initialised.
                for (Q_UINT32 i = 0; i < nsize*nsize*ndepth; ++i) {
                    coeffs[i] = 0;
                }
            }
            ~KisFloatRepresentation() { if(coeffs) delete[] coeffs; }
            float* coeffs;
            uint size;
            uint depth;
        };
        typedef KisFloatRepresentation KisWavelet;
    public:
        KisMathToolbox(KisID id);
        ~KisMathToolbox();
    public:
        inline KisID id() { return m_id; };
        /**
         * This function initialize a wavelet structure
         * @param lay the layer that will be used for the transformation
         */
        inline KisWavelet* initWavelet(KisPaintDeviceSP lay, const QRect&) throw(std::bad_alloc );
        inline uint fastWaveletTotalSteps(const QRect&);
        /**
         * This function reconstruct the layer from the information of a wavelet
         * @param src layer from which the wavelet will be computed
         * @param buff if set to 0, the buffer will be initialized by the function,
         * you might want to give a buff to the function if you want to use the same buffer
         * in transformToWavelet and in untransformToWavelet, use initWavelet to initialize
         * the buffer
         */
         virtual KisWavelet* fastWaveletTransformation(KisPaintDeviceSP src, const QRect&, KisWavelet* buff = 0) =0;
        /**
         * This function reconstruct the layer from the information of a wavelet
         * @param dst layer on which the wavelet will be untransform
         * @param wav the wavelet
         * @param buff if set to 0, the buffer will be initialized by the function,
         * you might want to give a buff to the function if you want to use the same buffer
         * in transformToWavelet and in untransformToWavelet, use initWavelet to initialize
         * the buffer
         */
         virtual void fastWaveletUntransformation(KisPaintDeviceSP dst, const QRect&, KisWavelet* wav, KisWavelet* buff = 0) =0;
    signals:
        void nextStep();
    protected:
        /**
         * This function transform a paint device into a KisFloatRepresentation, this function is colorspace independant,
         * for Wavelet, Pyramid and FFT the data is allways the exact value of the channel stored in a float.
         */
        void transformToFR(KisPaintDeviceSP src, KisFloatRepresentation*, const QRect&);
        /**
         * This function transform a KisFloatRepresentation into a paint device, this function is colorspace independant,
         * for Wavelet, Pyramid and FFT the data is allways the exact value of the channel stored in a float.
         */
        void transformFromFR(KisPaintDeviceSP dst, KisFloatRepresentation*, const QRect&);
    private:
        KisID m_id;
};

class KisMathToolboxFactoryRegistry : public KisGenericRegistry<KisMathToolbox*> {
    public:
        KisMathToolboxFactoryRegistry();
        ~KisMathToolboxFactoryRegistry();
};


inline KisMathToolbox::KisWavelet* KisMathToolbox::initWavelet(KisPaintDeviceSP src, const QRect& rect) throw(std::bad_alloc )
{
    int size;
    int maxrectsize = (rect.height() < rect.width()) ? rect.width() : rect.height();
    for(size = 2; size < maxrectsize; size *= 2) ;
    Q_INT32 depth = src->colorSpace()->nColorChannels();
    return new KisWavelet(size, depth);
}

inline uint KisMathToolbox::fastWaveletTotalSteps(const QRect& rect)
{
    int size, steps;
    int maxrectsize = (rect.height() < rect.width()) ? rect.width() : rect.height();
    steps = 0;
    for(size = 2; size < maxrectsize; size *= 2) steps += size / 2; ;
    return steps;
}

#endif