diff --git a/image.cpp b/image.cpp index a5082c32..387b7be3 100644 --- a/image.cpp +++ b/image.cpp @@ -23,54 +23,20 @@ using util::image::buffer; //----------------------------------------------------------------------------- template -util::image::buffer::buffer (util::extent2u _size): - buffer (_size.w, _size.h) +util::image::buffer::buffer (util::extentu<2> _size): + m_size (_size), + m_stride (1, _size.w), + m_data (std::make_unique (_size.area ())) { ; } -//----------------------------------------------------------------------------- -template -util::image::buffer::buffer (size_t _w, size_t _h, size_t _s): - w (_w), - h (_h), - s (_s), - m_data (std::make_unique (_h * _s)) -{ - CHECK_NEQ (w * h, 0); - CHECK_GE (s, w); -} - - -//----------------------------------------------------------------------------- -template -util::image::buffer::buffer (size_t _w, size_t _h): - buffer (_w, _h, _w) -{ ; } - - -//----------------------------------------------------------------------------- -template -util::image::buffer::buffer (size_t _w, - size_t _h, - size_t _s, - std::unique_ptr &&_data): - w (_w), - h (_h), - s (_s), - m_data (std::move (_data)) -{ - CHECK_NEQ (w * h, 0); - CHECK_GE (s, w); -} - - //----------------------------------------------------------------------------- template template util::image::buffer util::image::buffer::alloc (void) const { - return buffer (w, h, s); + return buffer (m_size); } @@ -102,8 +68,7 @@ template const T& buffer::operator[] (point<2,size_t> p) const { - CHECK_LT (p.x, w); - CHECK_LT (p.y, h); + CHECK (util::all (p < size ())); return begin ()[offset (p)]; } @@ -114,8 +79,7 @@ template T& buffer::operator[] (point<2,size_t> p) { - CHECK_LT (p.x, w); - CHECK_LT (p.y, h); + CHECK (util::all (p < size ())); return begin ()[offset (p)]; } @@ -126,7 +90,7 @@ template const T& buffer::operator[] (size_t idx) const { - CHECK_LT (idx, h * s); + CHECK_LT (idx, size ()); return begin ()[idx]; } @@ -137,7 +101,7 @@ template T& buffer::operator[] (size_t idx) { - CHECK_LT (idx, h * s); + CHECK_LT (idx, size ()); return begin ()[idx]; } @@ -166,7 +130,7 @@ template T* buffer::end (void) { - return begin () + h * s; + return begin () + m_size.back () * m_stride.back (); } @@ -211,7 +175,7 @@ template const T* buffer::cend (void) const { - return cbegin () + h * s; + return cbegin () + m_size.back () * m_stride.back (); } diff --git a/image.hpp b/image.hpp index 7ccea0ce..a089d30a 100644 --- a/image.hpp +++ b/image.hpp @@ -31,13 +31,13 @@ namespace util { namespace image { typedef T value_type; //--------------------------------------------------------------------- - buffer (util::extent2u); - buffer (size_t w, size_t h); - buffer (size_t w, size_t h, size_t s); - buffer (size_t w, size_t h, size_t s, std::unique_ptr &&data); + buffer (util::extentu<2>); + buffer (util::extentu<2>, std::unique_ptr &&data); buffer (const buffer&) = delete; buffer (buffer &&) = default; + buffer& operator= (const buffer&) = default; + buffer& operator= (buffer&&) = default; //--------------------------------------------------------------------- /// allocate and return a buffer of the same dimensions. contents are undefined. @@ -54,10 +54,6 @@ namespace util { namespace image { constexpr bool is_packed (void) const; - size_t w; - size_t h; - size_t s; - //--------------------------------------------------------------------- constexpr size_t offset (point<2,size_t>) const; @@ -80,6 +76,8 @@ namespace util { namespace image { const T* cend (void) const; private: + util::extent2u m_size; + util::vector2u m_stride; std::unique_ptr m_data; }; } } diff --git a/image.ipp b/image.ipp index b96e2f33..3fc32a4f 100644 --- a/image.ipp +++ b/image.ipp @@ -25,7 +25,7 @@ namespace util { namespace image { constexpr extent2u buffer::extent (void) const { - return { w, h }; + return m_size; } @@ -34,7 +34,7 @@ namespace util { namespace image { constexpr vector2u buffer::stride (void) const { - return { 1, s }; + return m_stride; } @@ -52,7 +52,7 @@ namespace util { namespace image { constexpr size_t buffer::size (void) const { - return extent ()[1] * stride ()[1]; + return extent ().back () * stride ().back (); } @@ -61,6 +61,6 @@ namespace util { namespace image { constexpr bool buffer::is_packed (void) const { - return stride ()[1] == extent ()[0]; + return stride ().back () == extent ().back (); } } } diff --git a/test/image.cpp b/test/image.cpp index 75e70792..657bd480 100644 --- a/test/image.cpp +++ b/test/image.cpp @@ -10,7 +10,7 @@ main (void) constexpr size_t W = 64; constexpr size_t H = 128; - util::image::buffer img (W, H); + util::image::buffer img ({W, H}); if (!img.is_packed ()) tap.skip ("linear position probe requires packed image allocation");