getBlackMatrix method

  1. @override
BitMatrix getBlackMatrix()
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 getBlackMatrix() {
  if (_matrix != null) {
    return _matrix!;
  }
  var source = luminanceSource;
  var width = source.width;
  var height = source.height;
  if (width >= _minimumDimension && height >= _minimumDimension) {
    var luminances = source.getMatrix();
    var subWidth = width >> _blockSizePower;
    if ((width & _blockSizeMask) != 0) {
      subWidth++;
    }
    var subHeight = height >> _blockSizePower;
    if ((height & _blockSizeMask) != 0) {
      subHeight++;
    }
    var blackPoints =
        _calculateBlackPoints(luminances, subWidth, subHeight, width, height);

    var 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.getBlackMatrix();
  }
  return _matrix!;
}