Browse Source

Puts everything in a namespace

master
Joshua Moerman 11 years ago
parent
commit
0fcfd492f8
  1. 65
      wavelet/wavelet.hpp
  2. 2
      wavelet/wavelet2.cpp
  3. 37
      wavelet/wavelet_constants.hpp

65
wavelet/wavelet.hpp

@ -4,25 +4,13 @@
#include "striding_iterator.hpp"
#include "periodic_iterator.hpp"
#include "wavelet_constants.hpp"
static double const evn_coef[] = {
(1.0 + std::sqrt(3.0))/(std::sqrt(32.0)),
(3.0 + std::sqrt(3.0))/(std::sqrt(32.0)),
(3.0 - std::sqrt(3.0))/(std::sqrt(32.0)),
(1.0 - std::sqrt(3.0))/(std::sqrt(32.0))
};
static double const odd_coef[] = {
evn_coef[3],
-evn_coef[2],
evn_coef[1],
-evn_coef[0]
};
// Apply the matrix Wn with the DAUB4 coefficients
// Assumes input to be periodic
template <typename Iterator>
void wavelet_mul(Iterator begin, Iterator end){
namespace wvlt {
namespace V1 {
// Apply the matrix Wn with the DAUB4 coefficients
template <typename Iterator>
void wavelet_mul(Iterator begin, Iterator end){
auto mul = end - begin;
std::vector<double> out(mul, 0.0);
for(int i = 0; i < mul; i += 2){
@ -32,10 +20,11 @@ void wavelet_mul(Iterator begin, Iterator end){
for(int i = 0; i < mul; ++i){
*begin++ = out[i];
}
}
}
template <typename Iterator>
void wavelet_inv(Iterator begin, Iterator end){
// Apply inverse of the matrix Wn with the DAUB4 coefficients
template <typename Iterator>
void wavelet_inv(Iterator begin, Iterator end){
auto mul = end - begin;
std::vector<double> out(mul, 0.0);
Iterator bc = begin;
@ -48,12 +37,12 @@ void wavelet_inv(Iterator begin, Iterator end){
for(int i = 0; i < mul; ++i){
*bc++ = out[i];
}
}
}
// Shuffle works with an extra copy, might be inefficient, but it is at least correct ;)
// It applies the even-odd-sort matrix Sn
template <typename Iterator>
void shuffle(Iterator begin, Iterator end){
// Shuffle works with an extra copy, might be inefficient, but it is at least correct ;)
// It applies the even-odd-sort matrix Sn
template <typename Iterator>
void shuffle(Iterator begin, Iterator end){
typedef typename std::iterator_traits<Iterator>::value_type value_type;
auto s = end - begin;
assert(s % 2 == 0);
@ -62,10 +51,10 @@ void shuffle(Iterator begin, Iterator end){
std::copy(strided(begin , 2), strided(end , 2), v.begin());
std::copy(strided(begin+1, 2), strided(end+1, 2), v.begin() + s/2);
std::copy(v.begin(), v.end(), begin);
}
}
template <typename Iterator>
void unshuffle(Iterator begin, Iterator end){
template <typename Iterator>
void unshuffle(Iterator begin, Iterator end){
typedef typename std::iterator_traits<Iterator>::value_type value_type;
auto s = end - begin;
assert(s % 2 == 0);
@ -74,12 +63,12 @@ void unshuffle(Iterator begin, Iterator end){
std::copy(begin, begin + s/2, strided(v.begin(), 2));
std::copy(begin + s/2, end, strided(v.begin()+1, 2));
std::copy(v.begin(), v.end(), begin);
}
}
// Combines the matrix Wn and Sn recusrively
// Only works for inputs of size 2^m
template <typename Iterator>
void wavelet(Iterator begin, Iterator end){
// Combines the matrix Wn and Sn recusrively
// Only works for inputs of size 2^m
template <typename Iterator>
void wavelet(Iterator begin, Iterator end){
auto s = end - begin;
assert(s >= 4);
for(int i = s; i >= 4; i >>= 1){
@ -92,10 +81,10 @@ void wavelet(Iterator begin, Iterator end){
// then with Sn
shuffle(begin, end);
}
}
}
template <typename Iterator>
void unwavelet(Iterator begin, Iterator end){
template <typename Iterator>
void unwavelet(Iterator begin, Iterator end){
auto s = end - begin;
assert(s >= 4);
for(int i = 4; i <= s; i <<= 1){
@ -108,4 +97,6 @@ void unwavelet(Iterator begin, Iterator end){
// then Wn^-1
wavelet_inv(begin, end);
}
}
}
}

2
wavelet/wavelet2.cpp

@ -9,6 +9,8 @@
#include "periodic_iterator.hpp"
#include "wavelet.hpp"
using namespace wvlt::V1;
// note: we take a copy, because we will modify it in place
jcmp::image compress(std::vector<double> image, int width, double threshold, int& zeros){
auto height = image.size() / width;

37
wavelet/wavelet_constants.hpp

@ -0,0 +1,37 @@
#pragma once
#include <cmath>
namespace wvlt {
// first row of the matrix Wn
static double const evn_coef[] = {
(1.0 + std::sqrt(3.0))/(std::sqrt(32.0)),
(3.0 + std::sqrt(3.0))/(std::sqrt(32.0)),
(3.0 - std::sqrt(3.0))/(std::sqrt(32.0)),
(1.0 - std::sqrt(3.0))/(std::sqrt(32.0))
};
// second row of the matrix Wn
static double const odd_coef[] = {
evn_coef[3],
-evn_coef[2],
evn_coef[1],
-evn_coef[0]
};
// first (shifted) row of the matrix Wn^-1
static double const evn_coef_inv[] = {
evn_coef[2],
evn_coef[1],
evn_coef[0],
evn_coef[3]
};
// second (shifted) row of the matrix Wn^-1
static double const odd_coef_inv[] = {
evn_coef[3],
-evn_coef[0],
evn_coef[1],
-evn_coef[2]
};
}