rgb2rgba method

Uint8List rgb2rgba({
  1. required Uint8List rgb,
  2. required int width,
  3. required int height,
})

Convert RGB to RGBA

rgb is Image data in RGB format. width and height specify the width and height of the Image. Returns RGBA bytes.

Implementation

Uint8List rgb2rgba({
  required Uint8List rgb,
  required int width,
  required int height,
}) {
  assert(width > 0, 'width is too small');
  assert(height > 0, 'height is too small');
  assert(rgb.isNotEmpty, 'pixels is empty');

  if (width <= 0 || height <= 0 || rgb.isEmpty) {
    return Uint8List(0);
  }

  final pixels = calloc.allocate<Uint8>(rgb.length);
  for (var i = 0; i < rgb.length; i++) {
    pixels[i] = rgb[i];
  }

  final rgba = calloc.allocate<Uint8>(width * height * 4);

  _rgb2rgbaFunction(
    pixels,
    width,
    height,
    rgba,
  );

  final results = _copyUint8PointerToUint8List(rgba, width * height * 4);
  calloc
    ..free(pixels)
    ..free(rgba);
  return results;
}