convertYUV420ToImage static method

Image convertYUV420ToImage(
  1. int uvRowStride,
  2. int uvPixelStride,
  3. List<Uint8List> planes,
  4. int width,
  5. int height,
)

Implementation

static Image convertYUV420ToImage(int uvRowStride, int uvPixelStride,
    List<Uint8List> planes, int width, int height) {
  final img = Image(width: width, height: height);
  for (final p in img) {
    final x = p.x;
    final y = p.y;
    final uvIndex =
        uvPixelStride * (x / 2).floor() + uvRowStride * (y / 2).floor();
    final index = y * uvRowStride +
        x; // Use the row stride instead of the image width as some devices pad the image data, and in those cases the image width != bytesPerRow. Using width will give you a distored image.
    final yp = planes[0][index];
    final up = planes[1][uvIndex];
    final vp = planes[2][uvIndex];
    p.r = (yp + vp * 1436 / 1024 - 179).round().clamp(0, 255).toInt();
    p.g = (yp - up * 46549 / 131072 + 44 - vp * 93604 / 131072 + 91)
        .round()
        .clamp(0, 255)
        .toInt();
    p.b = (yp + up * 1814 / 1024 - 227).round().clamp(0, 255).toInt();
  }

  return img;
}