From 2511de1f9056e58f15a6ed26449db4ad31025ce6 Mon Sep 17 00:00:00 2001 From: Guillaume Bittoun Date: Fri, 1 Aug 2014 12:08:11 +0000 Subject: [PATCH] arraynd: adding constructors and operators for vec_t and vec_t types. --- src/image/dither/ediff.cpp | 2 +- src/image/dither/ordered.cpp | 2 +- src/image/filter/convolution.cpp | 6 ++--- src/image/filter/median.cpp | 2 +- src/image/kernel.cpp | 2 +- src/lol/math/arraynd.h | 44 ++++++++++++++++++++++++++------ 6 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/image/dither/ediff.cpp b/src/image/dither/ediff.cpp index 3deb7b8a..bc411fe0 100644 --- a/src/image/dither/ediff.cpp +++ b/src/image/dither/ediff.cpp @@ -32,7 +32,7 @@ Image Image::DitherEdiff(array2d const &kernel, ScanMode scan) const Image dst = *this; ivec2 size = dst.GetSize(); - ivec2 ksize = kernel.GetSize(); + ivec2 ksize = (ivec2)kernel.GetSize(); int kx; for (kx = 0; kx < ksize.x; kx++) diff --git a/src/image/dither/ordered.cpp b/src/image/dither/ordered.cpp index 7a7d1850..f8c960d0 100644 --- a/src/image/dither/ordered.cpp +++ b/src/image/dither/ordered.cpp @@ -45,7 +45,7 @@ static Image DitherHelper(Image const &image, array2d const &kernel, float scale, float angle) { ivec2 size = image.GetSize(); - ivec2 ksize = kernel.GetSize(); + ivec2 ksize = (ivec2)kernel.GetSize(); float cost = lol::cos(angle); float sint = lol::sin(angle); diff --git a/src/image/filter/convolution.cpp b/src/image/filter/convolution.cpp index bf53b8dc..ba3cb72d 100644 --- a/src/image/filter/convolution.cpp +++ b/src/image/filter/convolution.cpp @@ -28,7 +28,7 @@ static Image NonSepConv(Image &src, array2d const &kernel); Image Image::Convolution(array2d const &kernel) { /* Find the cell with the largest value */ - ivec2 ksize = kernel.GetSize(); + ivec2 ksize = (ivec2)kernel.GetSize(); int bestx = -1, besty = -1; float tmp = 0.f; for (int dy = 0; dy < ksize.y; ++dy) @@ -85,7 +85,7 @@ Image Image::Convolution(array2d const &kernel) Image Image::Sharpen(array2d const &kernel) { - ivec2 ksize = kernel.GetSize(); + ivec2 ksize = (ivec2)kernel.GetSize(); array2d newkernel(ksize); for (int dy = 0; dy < ksize.y; ++dy) @@ -105,7 +105,7 @@ static Image NonSepConv(Image &src, array2d const &kernel) typedef typename PixelType::type pixel_t; ivec2 const size = src.GetSize(); - ivec2 const ksize = kernel.GetSize(); + ivec2 const ksize = (ivec2)kernel.GetSize(); Image dst(size); array2d const &srcp = src.Lock2D(); diff --git a/src/image/filter/median.cpp b/src/image/filter/median.cpp index 205882b9..bdd83dea 100644 --- a/src/image/filter/median.cpp +++ b/src/image/filter/median.cpp @@ -168,7 +168,7 @@ Image Image::Median(array2d const &kernel) const else #endif { - ivec2 const ksize = kernel.GetSize(); + ivec2 const ksize = (ivec2)kernel.GetSize(); array2d list(ksize); vec4 *srcp = tmp.Lock(); diff --git a/src/image/kernel.cpp b/src/image/kernel.cpp index db4f7093..d6773950 100644 --- a/src/image/kernel.cpp +++ b/src/image/kernel.cpp @@ -186,7 +186,7 @@ static int cmpdot(const void *p1, const void *p2) array2d Image::NormalizeKernel(array2d const &kernel) { - ivec2 size = kernel.GetSize(); + ivec2 size = (ivec2)kernel.GetSize(); array tmp; tmp.Resize(size.x * size.y); diff --git a/src/lol/math/arraynd.h b/src/lol/math/arraynd.h index 9e16a4c7..84d435a9 100644 --- a/src/lol/math/arraynd.h +++ b/src/lol/math/arraynd.h @@ -43,7 +43,7 @@ public: { } - void FillSizes(int * sizes) + void FillSizes(ptrdiff_t * sizes) { *sizes = std::max((ptrdiff_t)*sizes, (ptrdiff_t)(m_initializers.size())); @@ -51,7 +51,7 @@ public: subinitializer.FillSizes(sizes - 1); } - void FillValues(T * origin, ptrdiff_t prev, int * sizes) + void FillValues(T * origin, ptrdiff_t prev, ptrdiff_t * sizes) { ptrdiff_t pos = 0; @@ -75,12 +75,12 @@ public: { } - void FillSizes(int * sizes) + void FillSizes(ptrdiff_t * sizes) { *sizes = std::max((ptrdiff_t)*sizes, (ptrdiff_t)(m_initializers.size())); } - void FillValues(T * origin, ptrdiff_t prev, int * sizes) + void FillValues(T * origin, ptrdiff_t prev, ptrdiff_t * sizes) { UNUSED(sizes); @@ -107,12 +107,19 @@ public: { } - inline arraynd(vec_t sizes, element_t e = element_t()) + inline arraynd(vec_t sizes, element_t e = element_t()) : m_sizes(sizes) { FixSizes(e); } + inline arraynd(vec_t sizes, element_t e = element_t()) + { + m_sizes = vec_t(sizes); + FixSizes(e); + } + + inline arraynd(std::initializer_list > initializer) { m_sizes[N - 1] = initializer.size(); @@ -128,6 +135,21 @@ public: inner_initializer.FillValues(&super::operator[](0), pos++, &m_sizes[N - 2]); } + /* Access elements directly using an vec_t index */ + inline element_t const & operator[](vec_t const &pos) const + { + ptrdiff_t n = pos[N - 1]; + for (ptrdiff_t i = N - 2; i >= 0; --i) + n = pos[i] + m_sizes[i + 1] * n; + return super::operator[](n); + } + + inline element_t & operator[](vec_t const &pos) + { + return const_cast( + const_cast const&>(*this)[pos]); + } + /* Access elements directly using an ivec2, ivec3 etc. index */ inline element_t const & operator[](vec_t const &pos) const { @@ -207,13 +229,19 @@ public: /* Resize the array. * FIXME: data gets scrambled; should we care? */ - inline void SetSize(vec_t sizes, element_t e = element_t()) + inline void SetSize(vec_t sizes, element_t e = element_t()) { m_sizes = sizes; FixSizes(e); } - inline vec_t GetSize() const + inline void SetSize(vec_t sizes, element_t e = element_t()) + { + m_sizes = vec_t(sizes); + FixSizes(e); + } + + inline vec_t GetSize() const { return this->m_sizes; } @@ -235,7 +263,7 @@ private: this->Resize(total_size, e); } - vec_t m_sizes; + vec_t m_sizes; }; template using array2d = arraynd<2, T...>;