toGrayscale static method
Converts RGB or RGBA bytes to a single-channel Grayscale Y-plane.
Implementation
static Uint8List toGrayscale(
Uint8List input,
int width,
int height, {
int bytesPerPixel = 4,
}) {
final gray = Uint8List(width * height);
if (bytesPerPixel == 1 || input.length == width * height) {
gray.setRange(0, input.length < gray.length ? input.length : gray.length, input);
return gray;
}
for (int i = 0, j = 0; i < input.length && j < gray.length; i += bytesPerPixel, j++) {
final r = input[i];
final g = input[i + 1];
final b = input[i + 2];
// Standard ITU-R BT.601 luma formula
gray[j] = ((r * 299 + g * 587 + b * 114) ~/ 1000).clamp(0, 255);
}
return gray;
}