nc-photos/app/lib/pixel_image_provider.dart

42 lines
1 KiB
Dart
Raw Normal View History

2022-07-12 21:45:29 +02:00
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
/// ImageProvider for raw RGBA pixels
class PixelImage extends ImageProvider<PixelImage> {
PixelImage(
this.rgba,
this.width,
this.height, {
this.scale = 1.0,
});
@override
obtainKey(ImageConfiguration configuration) =>
SynchronousFuture<PixelImage>(this);
@override
2022-11-13 10:49:36 +01:00
ImageStreamCompleter loadBuffer(
PixelImage key, DecoderBufferCallback decode) =>
2022-07-12 21:45:29 +02:00
OneFrameImageStreamCompleter(_createImageInfo());
Future<ImageInfo> _createImageInfo() async {
final codec = await ImageDescriptor.raw(
await ImmutableBuffer.fromUint8List(rgba),
width: width,
height: height,
pixelFormat: PixelFormat.rgba8888,
).instantiateCodec();
final frame = await codec.getNextFrame();
return ImageInfo(image: frame.image, scale: scale);
}
final Uint8List rgba;
final int width;
final int height;
/// The scale to place in the [ImageInfo] object of the image.
final double scale;
}