| 
									
										
										
										
											2022-09-10 19:50:48 -07:00
										 |  |  | import 'dart:typed_data'; | 
					
						
							|  |  |  | import 'dart:ui' as ui; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Future<ui.Image> decodeImageFromPixels( | 
					
						
							|  |  |  |   Uint8List pixels, | 
					
						
							|  |  |  |   int width, | 
					
						
							|  |  |  |   int height, | 
					
						
							|  |  |  |   ui.PixelFormat format, { | 
					
						
							|  |  |  |   int? rowBytes, | 
					
						
							|  |  |  |   int? targetWidth, | 
					
						
							|  |  |  |   int? targetHeight, | 
					
						
							|  |  |  |   bool allowUpscaling = true, | 
					
						
							|  |  |  | }) async { | 
					
						
							|  |  |  |   if (targetWidth != null) { | 
					
						
							|  |  |  |     assert(allowUpscaling || targetWidth <= width); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (targetHeight != null) { | 
					
						
							|  |  |  |     assert(allowUpscaling || targetHeight <= height); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final ui.ImmutableBuffer buffer = | 
					
						
							|  |  |  |       await ui.ImmutableBuffer.fromUint8List(pixels); | 
					
						
							|  |  |  |   final ui.ImageDescriptor descriptor = ui.ImageDescriptor.raw( | 
					
						
							|  |  |  |     buffer, | 
					
						
							|  |  |  |     width: width, | 
					
						
							|  |  |  |     height: height, | 
					
						
							|  |  |  |     rowBytes: rowBytes, | 
					
						
							|  |  |  |     pixelFormat: format, | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  |   if (!allowUpscaling) { | 
					
						
							| 
									
										
										
										
											2022-09-11 19:52:38 -07:00
										 |  |  |     if (targetWidth != null && targetWidth > descriptor.width) { | 
					
						
							| 
									
										
										
										
											2022-09-10 19:50:48 -07:00
										 |  |  |       targetWidth = descriptor.width; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-11 19:52:38 -07:00
										 |  |  |     if (targetHeight != null && targetHeight > descriptor.height) { | 
					
						
							| 
									
										
										
										
											2022-09-10 19:50:48 -07:00
										 |  |  |       targetHeight = descriptor.height; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final ui.Codec codec = await descriptor.instantiateCodec( | 
					
						
							|  |  |  |     targetWidth: targetWidth, | 
					
						
							|  |  |  |     targetHeight: targetHeight, | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final ui.FrameInfo frameInfo = await codec.getNextFrame(); | 
					
						
							|  |  |  |   codec.dispose(); | 
					
						
							|  |  |  |   buffer.dispose(); | 
					
						
							|  |  |  |   descriptor.dispose(); | 
					
						
							|  |  |  |   return frameInfo.image; | 
					
						
							|  |  |  | } |