Mat.fromList constructor

Mat.fromList(
  1. int rows,
  2. int cols,
  3. MatType type,
  4. List<num> data,
)

Create a Mat from a list of data

data should be raw pixels values with exactly same length of channels * rows * cols

Mat (Size size, int type, void *data, size_t step=AUTO_STEP)

https://docs.opencv.org/4.x/d3/d63/classcv_1_1Mat.html#a9fa74fb14362d87cb183453d2441948f

Implementation

factory Mat.fromList(int rows, int cols, MatType type, List<num> data) {
  final p = calloc<cvg.Mat>();
  // copy
  final xdata = switch (type.depth) {
    MatType.CV_8U => VecU8.fromList(data.cast<int>()) as Vec,
    MatType.CV_8S => VecI8.fromList(data.cast<int>()) as Vec,
    MatType.CV_16U => VecU16.fromList(data.cast<int>()) as Vec,
    MatType.CV_16S => VecI16.fromList(data.cast<int>()) as Vec,
    MatType.CV_32S => VecI32.fromList(data.cast<int>()) as Vec,
    MatType.CV_32F => VecF32.fromList(data.cast<double>()) as Vec,
    MatType.CV_64F => VecF64.fromList(data.cast<double>()) as Vec,
    MatType.CV_16F => VecF16.fromList(data.cast<double>()) as Vec,
    _ => throw UnsupportedError("Mat.fromList for MatType ${type.asString()} unsupported"),
  };
  // copy
  cvRun(() => cffi.Mat_NewFromBytes(rows, cols, type.value, xdata.asVoid(), p));
  xdata.dispose();
  return Mat._(p);
}