DirtyDensityMap.fromBuffer constructor

DirtyDensityMap.fromBuffer(
  1. Buffer buffer, {
  2. Int32List? scratch,
})

Implementation

factory DirtyDensityMap.fromBuffer(Buffer buffer, {Int32List? scratch}) {
  final width = buffer.width();
  final height = buffer.height();
  final size = (width + 1) * (height + 1);
  final prefix = scratch != null && scratch.length >= size
      ? scratch
      : Int32List(size);
  prefix.fillRange(0, size, 0);

  for (var y = 0; y < height; y++) {
    var rowSum = 0;
    for (var x = 0; x < width; x++) {
      if (buffer.isCellDirty(x, y)) rowSum++;
      final idx = (y + 1) * (width + 1) + (x + 1);
      prefix[idx] = prefix[idx - (width + 1)] + rowSum;
    }
  }

  return DirtyDensityMap._(width, height, prefix);
}