analyzeBlurLevel static method
Analyzes blur severity of a grayscale image buffer using Laplacian variance.
Returns a severity string: 'sharp', 'mild', 'moderate', or 'heavy'.
Implementation
static String analyzeBlurLevel(Uint8List grayBytes, {required int width, required int height}) {
if (grayBytes.isEmpty || width <= 2 || height <= 2) return 'heavy';
double sumSquared = 0;
double sum = 0;
int count = 0;
for (int y = 1; y < height - 1; y++) {
for (int x = 1; x < width - 1; x++) {
final idx = y * width + x;
if (idx + width < grayBytes.length && idx - width >= 0) {
final lap = 4 * grayBytes[idx] -
grayBytes[idx - 1] - grayBytes[idx + 1] -
grayBytes[idx - width] - grayBytes[idx + width];
sum += lap;
sumSquared += lap * lap;
count++;
}
}
}
if (count == 0) return 'heavy';
final mean = sum / count;
final variance = (sumSquared / count) - (mean * mean);
if (variance.abs() > 500) return 'sharp';
if (variance.abs() > 200) return 'mild';
if (variance.abs() > 50) return 'moderate';
return 'heavy';
}