Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bucket filling in flutter

Tags:

flutter

dart

I am working on drawing app, which needs bucket filling also. Any idea on how to perform bucket filling in Flutter?

enter image description here

like image 906
Nikhil Avatar asked Aug 27 '18 11:08

Nikhil


1 Answers

You would have to write your own algorithm. I think you could port this one to dart.

One fundamental you need is how to get color of a pixel of an image:

Color getPixelColor(ByteData rgbaImageData, int imageWidth, int imageHeight, int x, int y) {
  assert(x >= 0 && x < imageWidth);
  assert(y >= 0 && y < imageHeight);

  final byteOffset = x * 4 + y * imageWidth * 4;

  final r = rgbaImageData.getUint8(byteOffset);
  final g = rgbaImageData.getUint8(byteOffset + 1);
  final b = rgbaImageData.getUint8(byteOffset + 2);
  final a = rgbaImageData.getUint8(byteOffset + 3);

  return Color.fromARGB(a, r, g, b);
}

You can use it like this:

Image image = ...;

final rgbaImageData = await image.toByteData(format: ui.ImageByteFormat.rawRgba);

print(getPixelColor(rgbaImageData, image.width, image.height, x, y));

Manipulating it follows the same scheme (setUint8).

like image 104
boformer Avatar answered Oct 18 '22 15:10

boformer