Debian Codegraph @

cvd/haar.h

raw symbols SCIP
1 #ifndef CVD_HAAR_H
2 #define CVD_HAAR_H
3
4 #include <algorithm>
5 #include <cmath>
6 #include <cvd/image.h>
7 #include <vector>
8
9 namespace CVD
10 {
11
12 namespace Internal
13 {
14 template <class It, class TempIt>
15 inline void haar1D(It from, int w, TempIt store)
16 {
17 for(int i = 0; i < w; ++i)
18 {
19 store[i] = (from[2 * i] + from[2 * i + 1]) * M_SQRT1_2;
20 store[i + w] = (from[2 * i] - from[2 * i + 1]) * M_SQRT1_2;
21 }
22 std::copy(store, store + 2 * w, from);
23 }
24
25 template <class It, class TempIt>
26 inline void inv_haar1D(It from, int w, TempIt store)
27 {
28 for(int i = 0; i < w; ++i)
29 {
30 store[2 * i] = (from[i] + from[w + i]) * M_SQRT1_2;
31 store[2 * i + 1] = (from[i] - from[w + i]) * M_SQRT1_2;
32 }
33 std::copy(store, store + 2 * w, from);
34 }
35 }
36
37 /// computes the 1D Haar transform of a signal in place. This version takes
38 /// two iterators, and the data between them will be transformed. Will only work
39 /// correctly on 2^N data points.
40 /// @param from iterator pointing to the beginning of the data
41 /// @param to iterator pointing to the end (after the last element)
42 /// @ingroup gVision
43 template <class It>
44 inline void haar1D(It from, It to)
45 {
46 std::vector<typename std::iterator_traits<It>::value_type> store(std::distance(from, to), typename std::iterator_traits<It>::value_type());
47 for(int w = std::distance(from, to) / 2; w > 0; w /= 2)
48 Internal::haar1D(from, w, store.begin());
49 }
50
51 /// computes the inverse 1D Haar transform of a signal in place. This version takes
52 /// two iterators, and the data between them will be transformed. Will only work
53 /// correctly on 2^N data points.
54 /// @param from iterator pointing to the beginning of the data
55 /// @param to iterator pointing to the end (after the last element)
56 /// @ingroup gVision
57 template <class It>
58 inline void inv_haar1D(It from, It to)
59 {
60 std::vector<typename std::iterator_traits<It>::value_type> store(std::distance(from, to), typename std::iterator_traits<It>::value_type());
61 for(int w = 1; w < std::distance(from, to); w *= 2)
62 Internal::inv_haar1D(from, w, store.begin());
63 }
64
65 /// computes the 1D Haar transform of a signal in place. Will only work
66 /// correctly on 2^N data points.
67 /// @param from iterator pointing to the beginning of the data
68 /// @param size number of data points, should be 2^N
69 /// @ingroup gVision
70 template <class It>
71 inline void haar1D(It from, int size)
72 {
73 haar1D(from, from + size);
74 }
75
76 /// computes the inverse 1D Haar transform of a signal in place. Will only work
77 /// correctly on 2^N data points.
78 /// @param from iterator pointing to the beginning of the data
79 /// @param size number of data points, should be 2^N
80 /// @ingroup gVision
81 template <class It>
82 inline void inv_haar1D(It from, int size)
83 {
84 inv_haar1D(from, from + size);
85 }
86
87 /// computes the 2D Haar transform of a signal in place. Works only with
88 /// data with power of two dimensions, 2^N x 2^ M.
89 /// @param from iterator pointing to the beginning of the data
90 /// @param width columns of data, should be 2^N
91 /// @param height rows of data, should be 2^M
92 /// @param stride offset between rows, if negative will be set to width
93 /// @ingroup gVision
94 template <class It>
95 inline void haar2D(It from, const int width, const int height, int stride = -1)
96 {
97 if(stride < 0)
98 stride = width;
99 typedef typename std::iterator_traits<It>::value_type T;
100 std::vector<T> column(height, T());
101 std::vector<T> store(std::max(width, height), T());
102 int w = width;
103 int h = height;
104 while(w > 1 || h > 1)
105 {
106 if(w > 1)
107 {
108 for(int i = 0; i < h; ++i)
109 {
110 Internal::haar1D(from + stride * i, w / 2, store.begin());
111 }
112 }
113 if(h > 1)
114 {
115 for(int i = 0; i < w; ++i)
116 {
117 for(int j = 0; j < h; ++j)
118 column[j] = from[stride * j + i];
119 Internal::haar1D(column.begin(), h / 2, store.begin());
120 for(int j = 0; j < h; ++j)
121 from[stride * j + i] = column[j];
122 }
123 }
124 if(w > 1)
125 w /= 2;
126 if(h > 1)
127 h /= 2;
128 }
129 }
130
131 /// computes the 2D Haar transform of an image in place. Works only with
132 /// images with power of two dimensions, 2^N x 2^ M.
133 /// @param I image to be transformed
134 /// @ingroup gVision
135 template <class T>
136 inline void haar2D(BasicImage<T>& I)
137 {
138 haar2D(I.data(), I.size().x, I.size().y, I.row_stride());
139 }
140
141 }
142
143 #endif // CVD_HAAR_H