autoCorrectSkew static method

Uint8List autoCorrectSkew(
  1. Uint8List grayBytes, {
  2. required int width,
  3. required int height,
})

Applies automatic skew correction by rotating pixel rows based on detected angle.

Returns corrected image bytes (same dimensions).

Implementation

static Uint8List autoCorrectSkew(Uint8List grayBytes, {required int width, required int height}) {
  final angle = computeSkewAngle(grayBytes, width: width, height: height);
  if (angle < 1.0) return grayBytes; // No correction needed

  // Simple row-shift skew correction
  final result = Uint8List(grayBytes.length);
  final shiftPerRow = (angle / height * width / 90.0).round();

  for (int y = 0; y < height; y++) {
    final rowShift = (shiftPerRow * y).round().clamp(0, width - 1);
    for (int x = 0; x < width; x++) {
      final srcIdx = y * width + x;
      final dstX = (x + rowShift) % width;
      final dstIdx = y * width + dstX;
      if (srcIdx < grayBytes.length && dstIdx < result.length) {
        result[dstIdx] = grayBytes[srcIdx];
      }
    }
  }

  return result;
}