MonoBitmap.fromImage constructor

MonoBitmap.fromImage(
  1. Image image, {
  2. int threshold = 127,
})

Implementation

factory MonoBitmap.fromImage(Image image, {int threshold = 127}) {
  final w = image.width, h = image.height;
  final wb = (w + 7) >> 3;
  final data = Uint8List(wb * h);
  for (var y = 0; y < h; y++) {
    for (var x = 0; x < w; x++) {
      final p = image.getPixel(x, y);
      // Transparent counts as white (PNG backgrounds).
      final a = p.length > 3 ? p.aNormalized : 1.0;
      final lum = a == 0 ? 255 : getLuminanceRgb(p.r, p.g, p.b);
      if (lum <= threshold) {
        data[y * wb + (x >> 3)] |= 0x80 >> (x & 7);
      }
    }
  }
  return MonoBitmap._(w, h, wb, data);
}