1
0
Fork 0
mirror of https://gitlab.com/nkming2/nc-photos.git synced 2025-03-10 09:18:52 +01:00
nc-photos/np_ui/lib/src/pixel_image_provider.dart
2024-05-12 23:30:32 +08:00

41 lines
1.1 KiB
Dart

import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
/// ImageProvider for raw RGBA pixels
class PixelImage extends ImageProvider<PixelImage> {
const PixelImage(
this.rgba,
this.width,
this.height, {
this.scale = 1.0,
});
@override
Future<PixelImage> obtainKey(ImageConfiguration configuration) =>
SynchronousFuture<PixelImage>(this);
@override
ImageStreamCompleter loadBuffer(
PixelImage key, DecoderBufferCallback decode) =>
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;
}