convertToBinary function
Convert a List of image bytes into a 2D matrix of bits representing black and white pixels
Implementation
BitMatrix convertToBinary(
Uint8List data,
int width,
int height, {
bool returnInverted = false,
}) {
if (data.length != width * height * 4) {
throw Exception("Malformed data passed to convertToBinary.");
}
// Convert image to grey scale
final greyScalePixels = _GrayScaleMatrix(width, height);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
final pixelIndex = (y * width + x) * 4;
final r = data[pixelIndex + 0];
final g = data[pixelIndex + 1];
final b = data[pixelIndex + 2];
final a = data[pixelIndex + 3];
greyScalePixels.set(x, y, _calculateGrayscaleLuminance(r, g, b, a));
}
}
return _convertGrayScaleToBinary(greyScalePixels, returnInverted);
}