imageFromNative function
Copies a native image into an immutable Dart image.
Implementation
MpImage imageFromNative(bindings.MpImagePtr image, {String? task}) {
final NativeScope scope = NativeScope(task: task);
try {
final int width = bindings.MpImageGetWidth(image);
final int height = bindings.MpImageGetHeight(image);
final bindings.MpImageFormat format = bindings.MpImageGetFormat(image);
final int samples = width * height * bindings.MpImageGetChannels(image);
final ffi.Pointer<ffi.Pointer<ffi.Char>> error = scope.errorOutput();
return switch (format) {
bindings.MpImageFormat.kMpImageFormatSrgb ||
bindings.MpImageFormat.kMpImageFormatSrgba ||
bindings.MpImageFormat.kMpImageFormatGray8 => _uint8ImageFromNative(
scope,
image,
_dartImageFormat(format),
width,
height,
samples,
error,
),
bindings.MpImageFormat.kMpImageFormatGray16 ||
bindings.MpImageFormat.kMpImageFormatSrgb48 ||
bindings.MpImageFormat.kMpImageFormatSrgba64 => _uint16ImageFromNative(
scope,
image,
_dartImageFormat(format),
width,
height,
samples,
error,
),
bindings.MpImageFormat.kMpImageFormatVec32F1 ||
bindings.MpImageFormat.kMpImageFormatVec32F2 ||
bindings.MpImageFormat.kMpImageFormatVec32F4 => _floatImageFromNative(
scope,
image,
_dartImageFormat(format),
width,
height,
samples,
error,
),
bindings.MpImageFormat.kMpImageFormatUnknown => throw MpException(
MpStatus.dataLoss,
'The native task returned an image with an unknown format.',
task: task,
),
};
} finally {
scope.release();
}
}