import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:image/image.dart' as imglib;

Future<Image?> convertYUV420toImageColor(CameraImage image) async {
  try {
    final int width = image.width;
    final int height = image.height;
    final int uvRowStride = image.planes[1].bytesPerRow;
    final int uvPixelStride = image.planes[1].bytesPerPixel!;
    var img = imglib.Image(height: width, width: height);

    // Fill image buffer with plane[0] from YUV420_888
    for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {
        final int uvIndex =
            uvPixelStride * (x / 2).floor() + uvRowStride * (y / 2).floor();
        final int index = y * width + x;

        final yp = image.planes[0].bytes[index];
        final up = image.planes[1].bytes[uvIndex];
        final vp = image.planes[2].bytes[uvIndex];
        // Calculate pixel color
        int r = (yp + vp * 1436 / 1024 - 179).round().clamp(0, 255);
        int g = (yp - up * 46549 / 131072 + 44 - vp * 93604 / 131072 + 91)
            .round()
            .clamp(0, 255);
        int b = (yp + up * 1814 / 1024 - 227).round().clamp(0, 255);
        // color: 0x FF  FF  FF  FF
        //           A   B   G   R
        // img.data[index] = shift | (b << 16) | (g << 8) | r;

        if (img.isBoundsSafe(height - y, x)) {
          const shift = (0xFF << 24);
          img.setPixelRgba(height - y, x, r, g, b, shift);
        }
      }
    }

    img = imglib.copyRotate(img, angle: -180);
    img = imglib.flipHorizontal(img);

    var encoder = imglib.JpegEncoder();
    var bytes = encoder.encode(img);

    return Image.memory(bytes);
  } catch (e) {
    print(">>>>>>>>>>>> ERROR:$e");
  }

  return null;
}
