getRegion method
Implementation
Future<Image> getRegion({
required int x,
required int y,
required int width,
required int height,
}) async {
await _paintingContext.doIntermediateRasterization();
final sourceImage = (_paintingContext.intermediateImage ?? _paintingContext.publishedImage)!;
final sourceData = await sourceImage.toByteData();
final destinationData = Uint8List(width * height * 4);
final rowLength = width * 4;
for (int row = 0; row < height; row += 1) {
final sourceRowOffset = _getBitmapPixelOffset(
imageWidth: sourceImage.width,
x: x,
y: y + row,
);
final destinationRowOffset = _getBitmapPixelOffset(
imageWidth: width,
x: 0,
y: row,
);
destinationData.setRange(
destinationRowOffset,
destinationRowOffset + rowLength - 1,
Uint8List.view(sourceData!.buffer, sourceRowOffset, rowLength),
);
}
final codec = await ImageDescriptor.raw(
await ImmutableBuffer.fromUint8List(destinationData),
width: width,
height: height,
pixelFormat: PixelFormat.rgba8888,
).instantiateCodec();
return (await codec.getNextFrame()).image;
}