From 12f846e101d4dc19bdcffdd330798c1ca28ad417 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Fri, 4 Feb 2022 07:48:12 +0100 Subject: [PATCH] fix: Resize image with compute by using const class arguments --- lib/src/utils/matrix_file.dart | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/src/utils/matrix_file.dart b/lib/src/utils/matrix_file.dart index c18ca473..356225a0 100644 --- a/lib/src/utils/matrix_file.dart +++ b/lib/src/utils/matrix_file.dart @@ -86,9 +86,14 @@ class MatrixImageFile extends MatrixFile { Future Function(FutureOr Function(U arg) function, U arg)? compute}) async { Image? image; + final arguments = _ResizeArguments( + bytes: bytes, + maxDimension: maxDimension, + fileName: name, + ); final resizedData = compute != null - ? await compute(_resize, [bytes, maxDimension]) - : _resize([bytes, maxDimension, name]); + ? await compute(_resize, arguments) + : _resize(arguments); if (resizedData == null) { return MatrixImageFile(bytes: bytes, name: name, mimeType: mimeType); @@ -166,22 +171,31 @@ class MatrixImageFile extends MatrixFile { return thumbnailFile; } - static Uint8List? _resize(List arguments) { - final bytes = arguments[0] as Uint8List; - final maxDimension = arguments[1] as int; - final fileName = arguments[2] as String; - final image = decodeImage(bytes); + static Uint8List? _resize(_ResizeArguments arguments) { + final image = decodeImage(arguments.bytes); final resized = copyResize(image!, - height: image.height > image.width ? maxDimension : null, - width: image.width >= image.height ? maxDimension : null); + height: image.height > image.width ? arguments.maxDimension : null, + width: image.width >= image.height ? arguments.maxDimension : null); - final encoded = encodeNamedImage(resized, fileName); + final encoded = encodeNamedImage(resized, arguments.fileName); if (encoded == null) return null; return Uint8List.fromList(encoded); } } +class _ResizeArguments { + final Uint8List bytes; + final int maxDimension; + final String fileName; + + const _ResizeArguments({ + required this.bytes, + required this.maxDimension, + required this.fileName, + }); +} + class MatrixVideoFile extends MatrixFile { int? width; int? height;