decodeImageFromPixels function

void decodeImageFromPixels(
  1. Uint8List pixels,
  2. int width,
  3. int height,
  4. PixelFormat format,
  5. ImageDecoderCallback callback,
  6. {int? rowBytes,
  7. int? targetWidth,
  8. int? targetHeight,
  9. bool allowUpscaling = true}
)

Convert an array of pixel values into an Image object.

The pixels parameter is the pixel data. They are packed in bytes in the order described by format, then grouped in rows, from left to right, then top to bottom.

The rowBytes parameter is the number of bytes consumed by each row of pixels in the data buffer. If unspecified, it defaults to width multiplied by the number of bytes per pixel in the provided format.

The targetWidth and targetHeight arguments specify the size of the output image, in image pixels. If they are not equal to the intrinsic dimensions of the image, then the image will be scaled after being decoded. If the allowUpscaling parameter is not set to true, both dimensions will be capped at the intrinsic dimensions of the image, even if only one of them would have exceeded those intrinsic dimensions. If exactly one of these two arguments is specified, then the aspect ratio will be maintained while forcing the image to match the other given dimension. If neither is specified, then the image maintains its intrinsic size.

Scaling the image to larger than its intrinsic size should usually be avoided, since it causes the image to use more memory than necessary. Instead, prefer scaling the Canvas transform. If the image must be scaled up, the allowUpscaling parameter must be set to true.

Implementation

void decodeImageFromPixels(
  Uint8List pixels,
  int width,
  int height,
  PixelFormat format,
  ImageDecoderCallback callback, {
  int? rowBytes,
  int? targetWidth,
  int? targetHeight,
  bool allowUpscaling = true,
}) {
  if (targetWidth != null) {
    assert(allowUpscaling || targetWidth <= width);
  }
  if (targetHeight != null) {
    assert(allowUpscaling || targetHeight <= height);
  }

  ImmutableBuffer.fromUint8List(pixels)
    .then((ImmutableBuffer buffer) {
      final ImageDescriptor descriptor = ImageDescriptor.raw(
        buffer,
        width: width,
        height: height,
        rowBytes: rowBytes,
        pixelFormat: format,
      );

      if (!allowUpscaling) {
        if (targetWidth != null && targetWidth! > descriptor.width) {
          targetWidth = descriptor.width;
        }
        if (targetHeight != null && targetHeight! > descriptor.height) {
          targetHeight = descriptor.height;
        }
      }

      descriptor
        .instantiateCodec(
          targetWidth: targetWidth,
          targetHeight: targetHeight,
        )
        .then((Codec codec) {
          final Future<FrameInfo> frameInfo = codec.getNextFrame();
          codec.dispose();
          return frameInfo;
        })
        .then((FrameInfo frameInfo) {
          buffer.dispose();
          descriptor.dispose();

          return callback(frameInfo.image);
        });
  });
}