Mat.fromMat constructor
create a Mat reference from another Mat if copy is false,
otherwise a copy will be created.
The reference returned when copy is false shares the underlying data with
mat (kept alive via OpenCV reference counting), but the wrapper itself is
not GC-managed, so call Mat.dispose when it is no longer needed.
Implementation
factory Mat.fromMat(Mat mat, {bool copy = false, Rect? roi}) {
final p = calloc<cvg.Mat>();
final (rows, cols, elemsize) = (mat.rows, mat.cols, mat.elemSize);
try {
cvRun(
() => roi == null
? ccore.cv_Mat_create_11(mat.ref, rows, cols, mat.type.value, 0, 0, p, ffi.nullptr)
: ccore.cv_Mat_create_13(mat.ref, roi.ref, p, ffi.nullptr),
);
} catch (_) {
calloc.free(p);
rethrow;
}
if (copy) {
final dst = Mat._(p, externalSize: rows * cols * elemsize);
final dstCopy = dst.clone();
dst.dispose();
return dstCopy;
}
return Mat._(p, attach: false, externalSize: rows * cols * elemsize);
}