blackMatrix property

  1. @override
BitMatrix blackMatrix
override

Calculates the final BitMatrix once for all requests. This could be called once from the constructor instead, but there are some advantages to doing it lazily, such as making profiling easier, and not doing heavy lifting when callers don't expect it.

Implementation

@override
BitMatrix get blackMatrix {
  if (_matrix != null) {
    return _matrix!;
  }
  final source = luminanceSource;
  final width = source.width;
  final height = source.height;
  if (width >= minimumDimension && height >= minimumDimension) {
    final luminances = source.matrix;
    int subWidth = width >> blockSizePower;
    if ((width & blockSizeMask) != 0) {
      subWidth++;
    }
    int subHeight = height >> blockSizePower;
    if ((height & blockSizeMask) != 0) {
      subHeight++;
    }
    final blackPoints =
        _calculateBlackPoints(luminances, subWidth, subHeight, width, height);

    final newMatrix = BitMatrix(width, height);
    _calculateThresholdForBlock(
      luminances,
      subWidth,
      subHeight,
      width,
      height,
      blackPoints,
      newMatrix,
    );
    _matrix = newMatrix;
  } else {
    // If the image is too small, fall back to the global histogram approach.
    _matrix = super.blackMatrix;
  }
  return _matrix!;
}