arrays.hpp 4.63 KB
Newer Older
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
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective icvers.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

Ilya Lavrenov's avatar
Ilya Lavrenov committed
42 43
#include "opencv2/core/base.hpp"

44 45 46
#ifndef __OPENCV_DENOISING_ARRAYS_HPP__
#define __OPENCV_DENOISING_ARRAYS_HPP__

47 48 49
namespace cv
{

Ilya Lavrenov's avatar
Ilya Lavrenov committed
50 51 52
template <class T>
struct Array2d
{
53 54 55 56
    T* a;
    int n1,n2;
    bool needToDeallocArray;

57 58
    Array2d(const Array2d& array2d):
        a(array2d.a), n1(array2d.n1), n2(array2d.n2), needToDeallocArray(false)
59
    {
Ilya Lavrenov's avatar
Ilya Lavrenov committed
60 61 62
        if (array2d.needToDeallocArray)
        {
            CV_Error(Error::BadDataPtr, "Copy constructor for self allocating arrays not supported");
63 64
        }
    }
65 66

    Array2d(T* _a, int _n1, int _n2):
Ilya Lavrenov's avatar
Ilya Lavrenov committed
67 68 69
        a(_a), n1(_n1), n2(_n2), needToDeallocArray(false)
    {
    }
70

71 72
    Array2d(int _n1, int _n2):
        n1(_n1), n2(_n2), needToDeallocArray(true)
73 74 75 76
    {
        a = new T[n1*n2];
    }

Ilya Lavrenov's avatar
Ilya Lavrenov committed
77 78 79
    ~Array2d()
    {
        if (needToDeallocArray)
80
            delete[] a;
81 82
    }

Ilya Lavrenov's avatar
Ilya Lavrenov committed
83 84
    T* operator [] (int i)
    {
85 86
        return a + i*n2;
    }
87

Ilya Lavrenov's avatar
Ilya Lavrenov committed
88 89
    inline T* row_ptr(int i)
    {
90 91 92 93
        return (*this)[i];
    }
};

Ilya Lavrenov's avatar
Ilya Lavrenov committed
94 95 96
template <class T>
struct Array3d
{
97 98 99
    T* a;
    int n1,n2,n3;
    bool needToDeallocArray;
100 101

    Array3d(T* _a, int _n1, int _n2, int _n3):
Ilya Lavrenov's avatar
Ilya Lavrenov committed
102 103 104
        a(_a), n1(_n1), n2(_n2), n3(_n3), needToDeallocArray(false)
    {
    }
105

106 107
    Array3d(int _n1, int _n2, int _n3):
        n1(_n1), n2(_n2), n3(_n3), needToDeallocArray(true)
108 109 110 111
    {
        a = new T[n1*n2*n3];
    }

Ilya Lavrenov's avatar
Ilya Lavrenov committed
112 113 114
    ~Array3d()
    {
        if (needToDeallocArray)
115
            delete[] a;
116 117
    }

Ilya Lavrenov's avatar
Ilya Lavrenov committed
118 119
    Array2d<T> operator [] (int i)
    {
120 121 122 123
        Array2d<T> array2d(a + i*n2*n3, n2, n3);
        return array2d;
    }

Ilya Lavrenov's avatar
Ilya Lavrenov committed
124 125
    inline T* row_ptr(int i1, int i2)
    {
126 127 128 129
        return a + i1*n2*n3 + i2*n3;
    }
};

Ilya Lavrenov's avatar
Ilya Lavrenov committed
130 131 132
template <class T>
struct Array4d
{
133 134 135 136
    T* a;
    int n1,n2,n3,n4;
    bool needToDeallocArray;
    int steps[4];
137

Ilya Lavrenov's avatar
Ilya Lavrenov committed
138 139
    void init_steps()
    {
140 141 142 143
        steps[0] = n2*n3*n4;
        steps[1] = n3*n4;
        steps[2] = n4;
        steps[3] = 1;
144 145
    }

Ilya Lavrenov's avatar
Ilya Lavrenov committed
146
    Array4d(T* _a, int _n1, int _n2, int _n3, int _n4) :
147
        a(_a), n1(_n1), n2(_n2), n3(_n3), n4(_n4), needToDeallocArray(false)
Ilya Lavrenov's avatar
Ilya Lavrenov committed
148
    {
149
        init_steps();
Ilya Lavrenov's avatar
Ilya Lavrenov committed
150
    }
151

Ilya Lavrenov's avatar
Ilya Lavrenov committed
152
    Array4d(int _n1, int _n2, int _n3, int _n4) :
153
        n1(_n1), n2(_n2), n3(_n3), n4(_n4), needToDeallocArray(true)
154
    {
155 156
        a = new T[n1*n2*n3*n4];
        init_steps();
Ilya Lavrenov's avatar
Ilya Lavrenov committed
157
    }
158

Ilya Lavrenov's avatar
Ilya Lavrenov committed
159 160 161
    ~Array4d()
    {
        if (needToDeallocArray)
162
            delete[] a;
163 164
    }

Ilya Lavrenov's avatar
Ilya Lavrenov committed
165 166
    Array3d<T> operator [] (int i)
    {
167 168 169 170
        Array3d<T> array3d(a + i*n2*n3*n4, n2, n3, n4);
        return array3d;
    }

Ilya Lavrenov's avatar
Ilya Lavrenov committed
171 172
    inline T* row_ptr(int i1, int i2, int i3)
    {
173 174 175
        return a + i1*n2*n3*n4 + i2*n3*n4 + i3*n4;
    }

Ilya Lavrenov's avatar
Ilya Lavrenov committed
176 177
    inline int step_size(int dimension)
    {
178 179 180 181
        return steps[dimension];
    }
};

182 183
}

184
#endif