setPixels method

  1. @override
void setPixels(
  1. PCanvasPixels pixels, {
  2. int x = 0,
  3. int y = 0,
  4. int? width,
  5. int? height,
})
override

Sets the canvas pixels.

Implementation

@override
void setPixels(PCanvasPixels pixels,
    {int x = 0, int y = 0, int? width, int? height}) {
  var w = width ?? this.width;
  if (x + w > this.width) w = this.width - x;

  var h = height ?? this.height;
  if (y + h > this.height) h = this.height - y;

  if (w <= 0 || h <= 0) return;

  if (x > 0 || y > 0 || w != pixels.width || h != pixels.height) {
    pixels = pixels.copyRect(x, y, w.toInt(), h.toInt())!;
  }

  ui.PixelFormat pixelFormat;

  if (pixels is PCanvasPixelsRGBA) {
    pixelFormat = ui.PixelFormat.rgba8888;
  } else if (pixels is PCanvasPixelsARGB) {
    pixelFormat = ui.PixelFormat.bgra8888;
  } else {
    pixels = pixels.toPCanvasPixelsARGB();
    pixelFormat = ui.PixelFormat.bgra8888;
  }

  var pixelsData = pixels.pixels;
  var pixelsDataBuffer = pixelsData.buffer;

  var bytes = pixelsDataBuffer.asUint8List(
      pixelsData.offsetInBytes, pixelsData.lengthInBytes);

  Completer<ui.Image> imageCompelter = Completer<ui.Image>();

  ui.decodeImageFromPixels(bytes, w.toInt(), h.toInt(), pixelFormat,
      (image) => imageCompelter.complete(image));

  var opAsync =
      imageCompelter.future.then((image) => _opDrawImageAsync(image, x, y));

  _widgetPainter.addOpAsync(opAsync);
}