webImageData function
Converts an immutable MpImage into browser ImageData.
Browser image sources are eight-bit RGBA. Higher precision formats are deterministically converted to that representation at this boundary.
Implementation
JSObject webImageData(MpImage image) {
final Uint8ClampedList rgba = Uint8ClampedList(image.width * image.height * 4);
for (int pixel = 0; pixel < image.width * image.height; pixel += 1) {
final int output = pixel * 4;
switch (image) {
case MpImageUint8(:final format, :final data):
_writeUint8Pixel(rgba, output, data, pixel * format.channels, format);
case MpImageUint16(:final format, :final data):
_writeUint16Pixel(rgba, output, data, pixel * format.channels, format);
case MpImageFloat32(:final format, :final data):
_writeFloatPixel(rgba, output, data, pixel * format.channels, format);
}
}
final JSAny? constructor = globalContext['ImageData'];
if (constructor == null || !constructor.isA<JSFunction>()) {
throw const MpException(MpStatus.unavailable, 'This browser does not expose ImageData.');
}
return (constructor as JSFunction).callAsConstructor<JSObject>(
rgba.toJS,
image.width.toJS,
image.height.toJS,
);
}