convertToRGBA32 method

Uint8List convertToRGBA32(
  1. ImageData imageData
)

Implementation

Uint8List convertToRGBA32(ImageData imageData) {
  final Uint8List input = imageData.bytes;
  final int width = imageData.width;
  final int height = imageData.height;
  final int format = imageData.format;

  final Uint8List output = Uint8List(width * height * 4);

  int dataIndex = 0;

  if (format == ImagePixelFormat.IPF_RGB_888.index) {
    for (int i = 0; i < height; i++) {
      for (int j = 0; j < width; j++) {
        final int index = (i * width + j) * 4;

        output[index] = input[dataIndex]; // R
        output[index + 1] = input[dataIndex + 1]; // G
        output[index + 2] = input[dataIndex + 2]; // B
        output[index + 3] = 255; // A

        dataIndex += 3;
      }
    }
  } else if (format == ImagePixelFormat.IPF_GRAYSCALED.index ||
      format == ImagePixelFormat.IPF_BINARY_8_INVERTED.index ||
      format == ImagePixelFormat.IPF_BINARY_8.index) {
    for (int i = 0; i < height; i++) {
      for (int j = 0; j < width; j++) {
        final int index = (i * width + j) * 4;
        final int gray = input[dataIndex];

        output[index] = gray;
        output[index + 1] = gray;
        output[index + 2] = gray;
        output[index + 3] = 255;

        dataIndex += 1;
      }
    }
  } else {
    throw UnsupportedError('Unsupported format: $format');
  }

  return output;
}