Image.file constructor
Image.file(
- String path, {
- StbiChannel desiredChannel = StbiChannel.default_,
- StbiDType dtype = StbiDType.u8,
Implementation
factory Image.file(
String path, {
StbiChannel desiredChannel = StbiChannel.default_,
StbiDType dtype = StbiDType.u8,
}) {
final pPath = path.toNativeUtf8().cast<ffi.Char>();
final pW = calloc<ffi.Int>();
final pH = calloc<ffi.Int>();
final pChannels = calloc<ffi.Int>();
final ptr = switch (dtype) {
StbiDType.u8 => c.stbi_load(pPath, pW, pH, pChannels, desiredChannel.value),
StbiDType.u16 => c.stbi_load_16(pPath, pW, pH, pChannels, desiredChannel.value),
StbiDType.f32 => c.stbi_loadf(pPath, pW, pH, pChannels, desiredChannel.value),
};
if (ptr == ffi.nullptr) {
final pMsg = c.stbi_failure_reason();
throw MNNException("Failed to load image: $path, ${pMsg.cast<Utf8>().toDartString()}");
}
final width = pW.value;
final height = pH.value;
final channels = pChannels.value;
calloc.free(pPath);
calloc.free(pW);
calloc.free(pH);
calloc.free(pChannels);
return Image.fromPointer(
ptr,
width: width,
height: height,
channels: channels,
desiredChannels: desiredChannel,
dtype: dtype,
);
}