ncnn-mat.h
class NCNN_EXPORT Mat
构造函数
// empty
Mat();
// vec
Mat(int w, size_t elemsize = 4u, Allocator* allocator = 0);
// image
Mat(int w, int h, size_t elemsize = 4u, Allocator* allocator = 0);
// dim
Mat(int w, int h, int c, size_t elemsize = 4u, Allocator* allocator = 0);
// cube
Mat(int w, int h, int d, int c, size_t elemsize = 4u, Allocator* allocator = 0);
// packed vec
Mat(int w, size_t elemsize, int elempack, Allocator* allocator = 0);
// packed image
Mat(int w, int h, size_t elemsize, int elempack, Allocator* allocator = 0);
// packed dim
Mat(int w, int h, int c, size_t elemsize, int elempack, Allocator* allocator = 0);
// packed cube
Mat(int w, int h, int d, int c, size_t elemsize, int elempack, Allocator* allocator = 0);
// copy
Mat(const Mat& m);
// external vec
Mat(int w, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
// external image
Mat(int w, int h, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
// external dim
Mat(int w, int h, int c, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
// external cube
Mat(int w, int h, int d, int c, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
// external packed vec
Mat(int w, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
// external packed image
Mat(int w, int h, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
// external packed dim
Mat(int w, int h, int c, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
// external packed cube
Mat(int w, int h, int d, int c, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
// release
~Mat();
// assign
Mat& operator=(const Mat& m);
。。。
。。。
。。。
参数:
w:图像宽
h:图像高
c:通道数
elemsize:元素字节数 // 4 = float32/int32, // 2 = float16, // 1 = int8/uint8, // 0 = empty
elempack
构造函数实现内部调用create重载函数
NCNN_FORCEINLINE Mat::Mat()
: data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
{
}
NCNN_FORCEINLINE Mat::Mat(int _w, size_t _elemsize, Allocator* _allocator)
: data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
{
create(_w, _elemsize, _allocator);
}
NCNN_FORCEINLINE Mat::Mat(int _w, int _h, size_t _elemsize, Allocator* _allocator)
: data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
{
create(_w, _h, _elemsize, _allocator);
}
NCNN_FORCEINLINE Mat::Mat(int _w, int _h, int _c, size_t _elemsize, Allocator* _allocator)
: data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
{
create(_w, _h, _c, _elemsize, _allocator);
}
NCNN_FORCEINLINE Mat::Mat(int _w, int _h, int _d, int _c, size_t _elemsize, Allocator* _allocator)
: data(0), refcount(0), elemsize(0), elempack(0), allocator(0), dims(0), w(0), h(0), d(0), c(0), cstep(0)
{
create(_w, _h, _d, _c, _elemsize, _allocator);
}
...
...
...
创建Mat对象后,根据构造函数重载对应调用create重载函数,实现数据内存空间申请,字节对齐等操作,没有赋值,默认值是?
void Mat::create(int _w, int _h, int _d, int _c, size_t _elemsize, int _elempack, Allocator* _allocator)
{
if (dims == 4 && w == _w && h == _h && d == _d && c == _c && elemsize == _elemsize && elempack == _elempack && allocator == _allocator)
return;
release();
elemsize = _elemsize;
elempack = _elempack;
allocator = _allocator;
dims = 4;
w = _w;
h = _h;
d = _d;
c = _c;
cstep = alignSize((size_t)w * h * d * elemsize, 16) / elemsize;
if (total() > 0)
{
size_t totalsize = alignSize(total() * elemsize, 4);
if (allocator)
data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount));
else
data = fastMalloc(totalsize + (int)sizeof(*refcount));
refcount = (int*)(((unsigned char*)data) + totalsize);
*refcount = 1;
}
}