createRawImageFromCameraImage method Null safety

RawImageF createRawImageFromCameraImage(
  1. dynamic image,
  2. int baseAngle,
  3. {NativeDataStruct? reusableData}
)

Implementation

RawImageF createRawImageFromCameraImage(CameraImage image, int baseAngle, {NativeDataStruct? reusableData}) {
  const YUV_420_888 = 0x00000023;

  int width = image.width;
  int height = image.height;
  RawImageF result;

  if (reusableData != null) {
    int totalBytes = width * height * 3;

    if (reusableData.size != totalBytes) {
      reusableData.resize(totalBytes);
    }
  }

  switch (image.format.group) {
    case ImageFormatGroup.unknown:
      throw Exception("Unknown image format");

    case ImageFormatGroup.yuv420:
      Pointer<Uint8> yuv420Data = getRawDataPointer(image.planes);

      if (image.format.raw == YUV_420_888 && width != image.planes.first.bytesPerRow) {
        Pointer<Uint8> temp =
            _removePaddingFromYUV420_888Pointer(yuv420Data, width, height, image.planes.first.bytesPerRow);

        malloc.free(yuv420Data);

        yuv420Data = temp;
      }

      result = _convertYUV2RGB(yuv420Data, width, height, Format.FORMAT_YUV_NV21,
          baseAngle: baseAngle, reusableData: reusableData);

      malloc.free(yuv420Data);

      break;

    case ImageFormatGroup.bgra8888:
      NativeDataStruct data = NativeDataStruct();

      convertRAW(image.planes, data);

      RawImageF temp = RawImageF(image.width, image.height, Format.FORMAT_BGR, data.pointer!.cast());

      result = convertBGRA88882RGB(temp, baseAngle: baseAngle, reusableData: reusableData);

      break;

    case ImageFormatGroup.jpeg:
      throw Exception("Unsupported image format jpeg");

    case ImageFormatGroup.nv21:
      throw Exception("Unsupported image format nv21");
  }

  return result;
}