| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
template <class It> |
| 71 |
inline void haar1D(It from, int size) |
| 72 |
{ |
| 73 |
haar1D(from, from + size); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
template <class It> |
| 82 |
inline void inv_haar1D(It from, int size) |
| 83 |
{ |
| 84 |
inv_haar1D(from, from + size); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 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 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 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 |