From d321d65fc87b124b36616be3ce0df7bb0cf2845a Mon Sep 17 00:00:00 2001 From: nihuini Date: Mon, 20 Aug 2018 18:38:14 +0800 Subject: [PATCH] Mat range for zero-copy slicing reference --- src/mat.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/mat.h b/src/mat.h index 613892959..43f350041 100644 --- a/src/mat.h +++ b/src/mat.h @@ -82,6 +82,14 @@ public: template T* row(int y); template const T* row(int y) const; + // range reference + Mat channel_range(int c, int channels); + const Mat channel_range(int c, int channels) const; + Mat row_range(int y, int rows); + const Mat row_range(int y, int rows) const; + Mat range(int x, int n); + const Mat range(int x, int n) const; + // access raw data template operator T*(); template operator const T*() const; @@ -626,6 +634,36 @@ inline const T* Mat::row(int y) const return (const T*)data + w * y; } +inline Mat Mat::channel_range(int _c, int channels) +{ + return Mat(w, h, channels, (unsigned char*)data + cstep * _c * elemsize, elemsize, allocator); +} + +inline const Mat Mat::channel_range(int _c, int channels) const +{ + return Mat(w, h, channels, (unsigned char*)data + cstep * _c * elemsize, elemsize, allocator); +} + +inline Mat Mat::row_range(int y, int rows) +{ + return Mat(w, rows, (unsigned char*)data + w * y * elemsize, elemsize, allocator); +} + +inline const Mat Mat::row_range(int y, int rows) const +{ + return Mat(w, rows, (unsigned char*)data + w * y * elemsize, elemsize, allocator); +} + +inline Mat Mat::range(int x, int n) +{ + return Mat(n, (unsigned char*)data + x * elemsize, elemsize, allocator); +} + +inline const Mat Mat::range(int x, int n) const +{ + return Mat(n, (unsigned char*)data + x * elemsize, elemsize, allocator); +} + template inline Mat::operator T*() {