diff --git a/packages/flutter/lib/src/painting/image_provider.dart b/packages/flutter/lib/src/painting/image_provider.dart index a989271599..30b118c100 100644 --- a/packages/flutter/lib/src/painting/image_provider.dart +++ b/packages/flutter/lib/src/painting/image_provider.dart @@ -4,6 +4,7 @@ import 'dart:async'; import 'dart:io'; +import 'dart:math' as math; import 'dart:ui' as ui; import 'dart:ui' show Locale, Size, TextDirection; @@ -846,11 +847,13 @@ abstract class AssetBundleImageProvider extends ImageProvider Object.hash(_providerCacheKey, _width, _height); + int get hashCode => Object.hash(_providerCacheKey, _policy, _width, _height, _allowUpscaling); +} + +/// Configures the behavior for [ResizeImage]. +/// +/// This is used in [ResizeImage.policy] to affect how the [ResizeImage.width] +/// and [ResizeImage.height] properties are interpreted. +enum ResizeImagePolicy { + /// Sizes the image to the exact width and height specified by + /// [ResizeImage.width] and [ResizeImage.height]. + /// + /// If [ResizeImage.width] and [ResizeImage.height] are both non-null, the + /// output image will have the specified width and height (with the + /// corresponding aspect ratio) regardless of whether it matches the source + /// image's intrinsic aspect ratio. This case is similar to [BoxFit.fill]. + /// + /// If only one of `width` and `height` is non-null, then the output image + /// will be scaled to the associated width or height, and the other dimension + /// will take whatever value is needed to maintain the image's original aspect + /// ratio. These cases are simnilar to [BoxFit.fitWidth] and + /// [BoxFit.fitHeight], respectively. + /// + /// If [ResizeImage.allowUpscaling] is false (the default), the width and the + /// height of the output image will each be clamped to the intrinsic width and + /// height of the image. This may result in a different aspect ratio than the + /// aspect ratio specified by the target width and height (e.g. if the height + /// gets clamped downwards but the width does not). + /// + /// ## Examples + /// + /// The examples below show how [ResizeImagePolicy.exact] works in various + /// scenarios. In each example, the source image has a size of 300x200 + /// (landscape orientation), the red box is a 150x150 square, and the green + /// box is a 400x400 square. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + ///
ScenarioOutput
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// width: 150, + /// height: 150, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_exact_150x150_false.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// width: 150, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_exact_150xnull_false.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// height: 150, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_exact_nullx150_false.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// width: 400, + /// height: 400, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_exact_400x400_false.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// width: 400, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_exact_400xnull_false.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// height: 400, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_exact_nullx400_false.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// width: 400, + /// height: 400, + /// allowUpscaling: true, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_exact_400x400_true.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// width: 400, + /// allowUpscaling: true, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_exact_400xnull_true.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// height: 400, + /// allowUpscaling: true, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_exact_nullx400_true.png) + /// + ///
+ exact, + + /// Scales the image as necessary to ensure that it fits within the bounding + /// box specified by [ResizeImage.width] and [ResizeImage.height] while + /// maintaining its aspect ratio. + /// + /// If [ResizeImage.allowUpscaling] is true, the image will be scaled up or + /// down to best fit the bounding box; otherwise it will only ever be scaled + /// down. + /// + /// This is conceptually similar to [BoxFit.contain]. + /// + /// ## Examples + /// + /// The examples below show how [ResizeImagePolicy.fit] works in various + /// scenarios. In each example, the source image has a size of 300x200 + /// (landscape orientation), the red box is a 150x150 square, and the green + /// box is a 400x400 square. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + ///
ScenarioOutput
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// policy: ResizeImagePolicy.fit, + /// width: 150, + /// height: 150, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_fit_150x150_false.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// policy: ResizeImagePolicy.fit, + /// width: 150, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_fit_150xnull_false.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// policy: ResizeImagePolicy.fit, + /// height: 150, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_fit_nullx150_false.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// policy: ResizeImagePolicy.fit, + /// width: 400, + /// height: 400, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_fit_400x400_false.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// policy: ResizeImagePolicy.fit, + /// width: 400, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_fit_400xnull_false.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// policy: ResizeImagePolicy.fit, + /// height: 400, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_fit_nullx400_false.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// policy: ResizeImagePolicy.fit, + /// width: 400, + /// height: 400, + /// allowUpscaling: true, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_fit_400x400_true.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// policy: ResizeImagePolicy.fit, + /// width: 400, + /// allowUpscaling: true, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_fit_400xnull_true.png) + /// + ///
+ /// + /// ```dart + /// const ResizeImage( + /// AssetImage('dragon_cake.jpg'), + /// policy: ResizeImagePolicy.fit, + /// height: 400, + /// allowUpscaling: true, + /// ) + /// ``` + /// + /// + /// + /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/resize_image_policy_fit_nullx400_true.png) + /// + ///
+ fit, } /// Instructs Flutter to decode the image at the specified dimensions @@ -881,10 +1279,13 @@ class ResizeImage extends ImageProvider { /// The cached image will be directly decoded and stored at the resolution /// defined by `width` and `height`. The image will lose detail and /// use less memory if resized to a size smaller than the native size. + /// + /// At least one of `width` and `height` must be non-null. const ResizeImage( this.imageProvider, { this.width, this.height, + this.policy = ResizeImagePolicy.exact, this.allowUpscaling = false, }) : assert(width != null || height != null); @@ -892,11 +1293,20 @@ class ResizeImage extends ImageProvider { final ImageProvider imageProvider; /// The width the image should decode to and cache. + /// + /// At least one of this and [height] must be non-null. final int? width; /// The height the image should decode to and cache. + /// + /// At least one of this and [width] must be non-null. final int? height; + /// The policy that determines how [width] and [height] are interpreted. + /// + /// Defaults to [ResizeImagePolicy.exact]. + final ResizeImagePolicy policy; + /// Whether the [width] and [height] parameters should be clamped to the /// intrinsic width and height of the image. /// @@ -919,6 +1329,10 @@ class ResizeImage extends ImageProvider { } @override + @Deprecated( + 'Implement loadImage for faster image loading. ' + 'This feature was deprecated after v2.13.0-1.0.pre.', + ) ImageStreamCompleter load(ResizeImageKey key, DecoderCallback decode) { Future decodeResize(Uint8List buffer, {int? cacheWidth, int? cacheHeight, bool? allowUpscaling}) { assert( @@ -936,6 +1350,10 @@ class ResizeImage extends ImageProvider { } @override + @Deprecated( + 'Implement loadImage for image loading. ' + 'This feature was deprecated after v3.7.0-1.4.pre.', + ) ImageStreamCompleter loadBuffer(ResizeImageKey key, DecoderBufferCallback decode) { Future decodeResize(ui.ImmutableBuffer buffer, {int? cacheWidth, int? cacheHeight, bool? allowUpscaling}) { assert( @@ -962,17 +1380,56 @@ class ResizeImage extends ImageProvider { 'getTargetSize.', ); return decode(buffer, getTargetSize: (int intrinsicWidth, int intrinsicHeight) { - int? targetWidth = width; - int? targetHeight = height; - if (!allowUpscaling) { - if (targetWidth != null && targetWidth > intrinsicWidth) { - targetWidth = intrinsicWidth; - } - if (targetHeight != null && targetHeight > intrinsicHeight) { - targetHeight = intrinsicHeight; - } + switch (policy) { + case ResizeImagePolicy.exact: + int? targetWidth = width; + int? targetHeight = height; + + if (!allowUpscaling) { + if (targetWidth != null && targetWidth > intrinsicWidth) { + targetWidth = intrinsicWidth; + } + if (targetHeight != null && targetHeight > intrinsicHeight) { + targetHeight = intrinsicHeight; + } + } + + return ui.TargetImageSize(width: targetWidth, height: targetHeight); + case ResizeImagePolicy.fit: + final double aspectRatio = intrinsicWidth / intrinsicHeight; + final int maxWidth = width ?? intrinsicWidth; + final int maxHeight = height ?? intrinsicHeight; + int targetWidth = intrinsicWidth; + int targetHeight = intrinsicHeight; + + if (targetWidth > maxWidth) { + targetWidth = maxWidth; + targetHeight = targetWidth ~/ aspectRatio; + } + + if (targetHeight > maxHeight) { + targetHeight = maxHeight; + targetWidth = (targetHeight * aspectRatio).floor(); + } + + if (allowUpscaling) { + if (width == null) { + assert(height != null); + targetHeight = height!; + targetWidth = (targetHeight * aspectRatio).floor(); + } else if (height == null) { + targetWidth = width!; + targetHeight = targetWidth ~/ aspectRatio; + } else { + final int derivedMaxWidth = (maxHeight * aspectRatio).floor(); + final int derivedMaxHeight = maxWidth ~/ aspectRatio; + targetWidth = math.min(maxWidth, derivedMaxWidth); + targetHeight = math.min(maxHeight, derivedMaxHeight); + } + } + + return ui.TargetImageSize(width: targetWidth, height: targetHeight); } - return ui.TargetImageSize(width: targetWidth, height: targetHeight); }); } @@ -993,10 +1450,10 @@ class ResizeImage extends ImageProvider { if (completer == null) { // This future has completed synchronously (completer was never assigned), // so we can directly create the synchronous result to return. - result = SynchronousFuture(ResizeImageKey._(key, width, height)); + result = SynchronousFuture(ResizeImageKey._(key, policy, width, height, allowUpscaling)); } else { // This future did not synchronously complete. - completer.complete(ResizeImageKey._(key, width, height)); + completer.complete(ResizeImageKey._(key, policy, width, height, allowUpscaling)); } }); if (result != null) { diff --git a/packages/flutter/test/image_data.dart b/packages/flutter/test/image_data.dart index b873343469..c1378c23af 100644 --- a/packages/flutter/test/image_data.dart +++ b/packages/flutter/test/image_data.dart @@ -48,7 +48,7 @@ const List kAnimatedGif = [ // Constructed by the following code: // ```dart // Future someTest(WidgetTester tester) async { -// Uint8List bytes; +// Uint8List? bytes; // await tester.runAsync(() async { // const int imageWidth = 100; // const int imageHeight = 100; @@ -60,9 +60,9 @@ const List kAnimatedGif = [ // ui.decodeImageFromPixels( // pixels, imageWidth, imageHeight, ui.PixelFormat.rgba8888, // (ui.Image image) async { -// final ByteData byteData = await image.toByteData( +// final ByteData? byteData = await image.toByteData( // format: ui.ImageByteFormat.png); -// bytes = byteData.buffer.asUint8List(); +// bytes = byteData!.buffer.asUint8List(); // completer.complete(); // }, // ); @@ -91,3 +91,35 @@ const List kBlueRectPng = [ 99, 72, 140, 33, 49, 134, 196, 24, 18, 99, 72, 204, 5, 234, 78, 2, 198, 180, 170, 48, 200, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130, ]; + +/// A portrait-mode (50x100) PNG with blue pixels. +const List kBluePortraitPng = [ + 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 50, 0, + 0, 0, 100, 8, 6, 0, 0, 0, 196, 232, 99, 91, 0, 0, 0, 4, 115, 66, 73, 84, 8, 8, + 8, 8, 124, 8, 100, 136, 0, 0, 0, 140, 73, 68, 65, 84, 120, 156, 237, 207, 1, + 13, 192, 48, 12, 192, 176, 126, 252, 57, 247, 36, 46, 45, 186, 108, 4, 201, + 51, 179, 59, 63, 112, 110, 7, 124, 197, 72, 141, 145, 26, 35, 53, 70, 106, + 140, 212, 24, 169, 49, 82, 99, 164, 198, 72, 141, 145, 26, 35, 53, 70, 106, + 140, 212, 24, 169, 49, 82, 99, 164, 198, 72, 141, 145, 26, 35, 53, 70, 106, + 140, 212, 24, 169, 49, 82, 99, 164, 198, 72, 141, 145, 26, 35, 53, 70, 106, + 140, 212, 24, 169, 49, 82, 99, 164, 198, 72, 141, 145, 26, 35, 53, 70, 106, + 140, 212, 24, 169, 49, 82, 99, 164, 198, 72, 141, 145, 26, 35, 53, 70, 106, + 140, 212, 24, 169, 49, 82, 99, 164, 198, 72, 205, 11, 105, 8, 2, 198, 99, 140, + 153, 87, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130, +]; + +/// A landscape-mode (100x50) PNG with blue pixels. +const List kBlueLandscapePng = [ + 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 100, 0, + 0, 0, 50, 8, 6, 0, 0, 0, 170, 53, 126, 190, 0, 0, 0, 4, 115, 66, 73, 84, 8, 8, + 8, 8, 124, 8, 100, 136, 0, 0, 0, 143, 73, 68, 65, 84, 120, 156, 237, 209, 65, + 13, 0, 32, 16, 192, 176, 3, 255, 158, 225, 141, 2, 246, 104, 21, 44, 217, 154, + 57, 103, 200, 216, 191, 3, 120, 25, 18, 99, 72, 140, 33, 49, 134, 196, 24, 18, + 99, 72, 140, 33, 49, 134, 196, 24, 18, 99, 72, 140, 33, 49, 134, 196, 24, 18, + 99, 72, 140, 33, 49, 134, 196, 24, 18, 99, 72, 140, 33, 49, 134, 196, 24, 18, + 99, 72, 140, 33, 49, 134, 196, 24, 18, 99, 72, 140, 33, 49, 134, 196, 24, 18, + 99, 72, 140, 33, 49, 134, 196, 24, 18, 99, 72, 140, 33, 49, 134, 196, 24, 18, + 99, 72, 140, 33, 49, 134, 196, 24, 18, 99, 72, 140, 33, 49, 134, 196, 24, 18, + 99, 72, 140, 33, 49, 134, 196, 92, 164, 190, 2, 98, 53, 3, 99, 174, 0, 0, 0, + 0, 73, 69, 78, 68, 174, 66, 96, 130, +]; diff --git a/packages/flutter/test/painting/image_provider_resize_image_test.dart b/packages/flutter/test/painting/image_provider_resize_image_test.dart index 3607618624..a9b1e0c43a 100644 --- a/packages/flutter/test/painting/image_provider_resize_image_test.dart +++ b/packages/flutter/test/painting/image_provider_resize_image_test.dart @@ -20,122 +20,369 @@ void main() { PaintingBinding.instance.imageCache.clearLiveImages(); }); - test('ResizeImage resizes to the correct dimensions (up)', () async { - final Uint8List bytes = Uint8List.fromList(kTransparentImage); - final MemoryImage imageProvider = MemoryImage(bytes); - final Size rawImageSize = await _resolveAndGetSize(imageProvider); - expect(rawImageSize, const Size(1, 1)); + group('ResizeImage', () { + group('resizing', () { + test('upscales to the correct dimensions', () async { + final Uint8List bytes = Uint8List.fromList(kTransparentImage); + final MemoryImage imageProvider = MemoryImage(bytes); + final Size rawImageSize = await _resolveAndGetSize(imageProvider); + expect(rawImageSize, const Size(1, 1)); - const Size resizeDims = Size(14, 7); - final ResizeImage resizedImage = ResizeImage(MemoryImage(bytes), width: resizeDims.width.round(), height: resizeDims.height.round(), allowUpscaling: true); - const ImageConfiguration resizeConfig = ImageConfiguration(size: resizeDims); - final Size resizedImageSize = await _resolveAndGetSize(resizedImage, configuration: resizeConfig); - expect(resizedImageSize, resizeDims); - }, skip: isBrowser); // https://github.com/flutter/flutter/issues/56312 + const Size resizeDims = Size(14, 7); + final ResizeImage resizedImage = ResizeImage(MemoryImage(bytes), width: resizeDims.width.round(), height: resizeDims.height.round(), allowUpscaling: true); + const ImageConfiguration resizeConfig = ImageConfiguration(size: resizeDims); + final Size resizedImageSize = await _resolveAndGetSize(resizedImage, configuration: resizeConfig); + expect(resizedImageSize, resizeDims); + }); - test('ResizeImage resizes to the correct dimensions (down)', () async { - final Uint8List bytes = Uint8List.fromList(kBlueSquarePng); - final MemoryImage imageProvider = MemoryImage(bytes); - final Size rawImageSize = await _resolveAndGetSize(imageProvider); - expect(rawImageSize, const Size(50, 50)); + test('downscales to the correct dimensions', () async { + final Uint8List bytes = Uint8List.fromList(kBlueSquarePng); + final MemoryImage imageProvider = MemoryImage(bytes); + final Size rawImageSize = await _resolveAndGetSize(imageProvider); + expect(rawImageSize, const Size(50, 50)); - const Size resizeDims = Size(25, 25); - final ResizeImage resizedImage = ResizeImage(MemoryImage(bytes), width: resizeDims.width.round(), height: resizeDims.height.round(), allowUpscaling: true); - const ImageConfiguration resizeConfig = ImageConfiguration(size: resizeDims); - final Size resizedImageSize = await _resolveAndGetSize(resizedImage, configuration: resizeConfig); - expect(resizedImageSize, resizeDims); - }, skip: isBrowser); // https://github.com/flutter/flutter/issues/56312 + const Size resizeDims = Size(25, 25); + final ResizeImage resizedImage = ResizeImage(MemoryImage(bytes), width: resizeDims.width.round(), height: resizeDims.height.round(), allowUpscaling: true); + const ImageConfiguration resizeConfig = ImageConfiguration(size: resizeDims); + final Size resizedImageSize = await _resolveAndGetSize(resizedImage, configuration: resizeConfig); + expect(resizedImageSize, resizeDims); + }); - test('ResizeImage resizes to the correct dimensions - no upscaling', () async { - final Uint8List bytes = Uint8List.fromList(kTransparentImage); - final MemoryImage imageProvider = MemoryImage(bytes); - final Size rawImageSize = await _resolveAndGetSize(imageProvider); - expect(rawImageSize, const Size(1, 1)); + test('refuses upscaling when allowUpscaling=false', () async { + final Uint8List bytes = Uint8List.fromList(kTransparentImage); + final MemoryImage imageProvider = MemoryImage(bytes); + final Size rawImageSize = await _resolveAndGetSize(imageProvider); + expect(rawImageSize, const Size(1, 1)); - const Size resizeDims = Size(1, 1); - final ResizeImage resizedImage = ResizeImage(MemoryImage(bytes), width: resizeDims.width.round(), height: resizeDims.height.round()); - const ImageConfiguration resizeConfig = ImageConfiguration(size: resizeDims); - final Size resizedImageSize = await _resolveAndGetSize(resizedImage, configuration: resizeConfig); - expect(resizedImageSize, resizeDims); - }, skip: isBrowser); // https://github.com/flutter/flutter/issues/56312 + const Size resizeDims = Size(50, 50); + final ResizeImage resizedImage = ResizeImage(MemoryImage(bytes), width: resizeDims.width.round(), height: resizeDims.height.round()); + const ImageConfiguration resizeConfig = ImageConfiguration(size: resizeDims); + final Size resizedImageSize = await _resolveAndGetSize(resizedImage, configuration: resizeConfig); + expect(resizedImageSize, const Size(1, 1)); + }); - test('ResizeImage does not resize when no size is passed', () async { - final Uint8List bytes = Uint8List.fromList(kTransparentImage); - final MemoryImage imageProvider = MemoryImage(bytes); - final Size rawImageSize = await _resolveAndGetSize(imageProvider); - expect(rawImageSize, const Size(1, 1)); + group('with policy=fit and allowResize=false', () { + test('constrains square image to bounded portrait rect', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueSquarePng)); + await _expectImageSize(rawImage, const Size(50, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 25, height: 50, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(25, 25)); + }); - // Cannot pass in two null arguments for cache dimensions, so will use the regular - // MemoryImage - final MemoryImage resizedImage = MemoryImage(bytes); - final Size resizedImageSize = await _resolveAndGetSize(resizedImage); - expect(resizedImageSize, const Size(1, 1)); - }); + test('constrains square image to bounded landscape rect', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueSquarePng)); + await _expectImageSize(rawImage, const Size(50, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 50, height: 25, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(25, 25)); + }); - test('ResizeImage stores values', () async { - final Uint8List bytes = Uint8List.fromList(kTransparentImage); - final MemoryImage memoryImage = MemoryImage(bytes); - memoryImage.resolve(ImageConfiguration.empty); - final ResizeImage resizeImage = ResizeImage(memoryImage, width: 10, height: 20); - expect(resizeImage.width, 10); - expect(resizeImage.height, 20); - expect(resizeImage.imageProvider, memoryImage); - expect(memoryImage.resolve(ImageConfiguration.empty) != resizeImage.resolve(ImageConfiguration.empty), true); - }); + test('constrains square image to bounded square', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueSquarePng)); + await _expectImageSize(rawImage, const Size(50, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 25, height: 25, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(25, 25)); + }); - test('ResizeImage takes one dim', () async { - final Uint8List bytes = Uint8List.fromList(kTransparentImage); - final MemoryImage memoryImage = MemoryImage(bytes); - final ResizeImage resizeImage = ResizeImage(memoryImage, width: 10); - expect(resizeImage.width, 10); - expect(resizeImage.height, null); - expect(resizeImage.imageProvider, memoryImage); - expect(memoryImage.resolve(ImageConfiguration.empty) != resizeImage.resolve(ImageConfiguration.empty), true); - }); + test('constrains square image to bounded width', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueSquarePng)); + await _expectImageSize(rawImage, const Size(50, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 25, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(25, 25)); + }); - test('ResizeImage forms closure', () async { - final Uint8List bytes = Uint8List.fromList(kTransparentImage); - final MemoryImage memoryImage = MemoryImage(bytes); - final ResizeImage resizeImage = ResizeImage(memoryImage, width: 123, height: 321); + test('constrains square image to bounded height', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueSquarePng)); + await _expectImageSize(rawImage, const Size(50, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, height: 25, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(25, 25)); + }); - Future decode(ui.ImmutableBuffer buffer, {int? cacheWidth, int? cacheHeight, bool allowUpscaling = false}) { - expect(cacheWidth, 123); - expect(cacheHeight, 321); - expect(allowUpscaling, false); - return PaintingBinding.instance.instantiateImageCodecFromBuffer(buffer, cacheWidth: cacheWidth, cacheHeight: cacheHeight, allowUpscaling: allowUpscaling); - } + test('constrains portrait image to bounded portrait rect', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBluePortraitPng)); + await _expectImageSize(rawImage, const Size(50, 100)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 25, height: 60, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(25, 50)); + }); - resizeImage.loadBuffer(await resizeImage.obtainKey(ImageConfiguration.empty), decode); - }); + test('constrains portrait image to bounded landscape rect', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBluePortraitPng)); + await _expectImageSize(rawImage, const Size(50, 100)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 60, height: 25, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(12, 25)); + }); - test('ResizeImage handles sync obtainKey', () async { - final Uint8List bytes = Uint8List.fromList(kTransparentImage); - final MemoryImage memoryImage = MemoryImage(bytes); - final ResizeImage resizeImage = ResizeImage(memoryImage, width: 123, height: 321); + test('constrains portrait image to bounded square', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBluePortraitPng)); + await _expectImageSize(rawImage, const Size(50, 100)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 25, height: 25, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(12, 25)); + }); - bool isAsync = false; - resizeImage.obtainKey(ImageConfiguration.empty).then((Object key) { - expect(isAsync, false); + test('constrains portrait image to bounded width', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBluePortraitPng)); + await _expectImageSize(rawImage, const Size(50, 100)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 25, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(25, 50)); + }); + + test('constrains portrait image to bounded height', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBluePortraitPng)); + await _expectImageSize(rawImage, const Size(50, 100)); + final ResizeImage resizedImage = ResizeImage(rawImage, height: 25, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(12, 25)); + }); + + test('constrains landscape image to bounded portrait rect', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueLandscapePng)); + await _expectImageSize(rawImage, const Size(100, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 25, height: 60, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(25, 12)); + }); + + test('constrains landscape image to bounded landscape rect', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueLandscapePng)); + await _expectImageSize(rawImage, const Size(100, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 60, height: 25, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(50, 25)); + }); + + test('constrains landscape image to bounded square', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueLandscapePng)); + await _expectImageSize(rawImage, const Size(100, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 25, height: 25, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(25, 12)); + }); + + test('constrains landscape image to bounded width', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueLandscapePng)); + await _expectImageSize(rawImage, const Size(100, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 25, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(25, 12)); + }); + + test('constrains landscape image to bounded height', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueLandscapePng)); + await _expectImageSize(rawImage, const Size(100, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, height: 25, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(50, 25)); + }); + + test('leaves image as-is if constraints are bigger than image', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueSquarePng)); + await _expectImageSize(rawImage, const Size(50, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 120, height: 100, policy: ResizeImagePolicy.fit); + await _expectImageSize(resizedImage, const Size(50, 50)); + }); + }); + + group('with policy=fit and allowResize=true', () { + test('constrains square image to bounded portrait rect', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueSquarePng)); + await _expectImageSize(rawImage, const Size(50, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 100, height: 200, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(100, 100)); + }); + + test('constrains square image to bounded landscape rect', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueSquarePng)); + await _expectImageSize(rawImage, const Size(50, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 200, height: 100, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(100, 100)); + }); + + test('constrains square image to bounded square', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueSquarePng)); + await _expectImageSize(rawImage, const Size(50, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 100, height: 100, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(100, 100)); + }); + + test('constrains square image to bounded width', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueSquarePng)); + await _expectImageSize(rawImage, const Size(50, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 100, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(100, 100)); + }); + + test('constrains square image to bounded height', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueSquarePng)); + await _expectImageSize(rawImage, const Size(50, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, height: 100, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(100, 100)); + }); + + test('constrains portrait image to bounded portrait rect', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBluePortraitPng)); + await _expectImageSize(rawImage, const Size(50, 100)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 100, height: 250, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(100, 200)); + }); + + test('constrains portrait image to bounded landscape rect', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBluePortraitPng)); + await _expectImageSize(rawImage, const Size(50, 100)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 400, height: 200, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(100, 200)); + }); + + test('constrains portrait image to bounded square', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBluePortraitPng)); + await _expectImageSize(rawImage, const Size(50, 100)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 200, height: 200, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(100, 200)); + }); + + test('constrains portrait image to bounded width', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBluePortraitPng)); + await _expectImageSize(rawImage, const Size(50, 100)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 100, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(100, 200)); + }); + + test('constrains portrait image to bounded height', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBluePortraitPng)); + await _expectImageSize(rawImage, const Size(50, 100)); + final ResizeImage resizedImage = ResizeImage(rawImage, height: 200, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(100, 200)); + }); + + test('constrains landscape image to bounded portrait rect', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueLandscapePng)); + await _expectImageSize(rawImage, const Size(100, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 200, height: 400, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(200, 100)); + }); + + test('constrains landscape image to bounded landscape rect', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueLandscapePng)); + await _expectImageSize(rawImage, const Size(100, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 250, height: 100, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(200, 100)); + }); + + test('constrains landscape image to bounded square', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueLandscapePng)); + await _expectImageSize(rawImage, const Size(100, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 200, height: 200, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(200, 100)); + }); + + test('constrains landscape image to bounded width', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueLandscapePng)); + await _expectImageSize(rawImage, const Size(100, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 200, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(200, 100)); + }); + + test('constrains landscape image to bounded height', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueLandscapePng)); + await _expectImageSize(rawImage, const Size(100, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, height: 100, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(200, 100)); + }); + + test('shrinks image if constraints are smaller than image', () async { + final MemoryImage rawImage = MemoryImage(Uint8List.fromList(kBlueSquarePng)); + await _expectImageSize(rawImage, const Size(50, 50)); + final ResizeImage resizedImage = ResizeImage(rawImage, width: 25, height: 30, policy: ResizeImagePolicy.fit, allowUpscaling: true); + await _expectImageSize(resizedImage, const Size(25, 25)); + }); + }); + }, skip: isBrowser); // https://github.com/flutter/flutter/issues/73120); + + test('does not resize when no size is passed', () async { + final Uint8List bytes = Uint8List.fromList(kTransparentImage); + final MemoryImage imageProvider = MemoryImage(bytes); + final Size rawImageSize = await _resolveAndGetSize(imageProvider); + expect(rawImageSize, const Size(1, 1)); + + final ImageProvider resizedImage = ResizeImage.resizeIfNeeded(null, null, imageProvider); + final Size resizedImageSize = await _resolveAndGetSize(resizedImage); + expect(resizedImageSize, const Size(1, 1)); }); - isAsync = true; - expect(isAsync, true); - }); - test('ResizeImage handles async obtainKey', () async { - final Uint8List bytes = Uint8List.fromList(kTransparentImage); - final _AsyncKeyMemoryImage memoryImage = _AsyncKeyMemoryImage(bytes); - final ResizeImage resizeImage = ResizeImage(memoryImage, width: 123, height: 321); + test('stores values', () async { + final Uint8List bytes = Uint8List.fromList(kTransparentImage); + final MemoryImage memoryImage = MemoryImage(bytes); + memoryImage.resolve(ImageConfiguration.empty); + final ResizeImage resizeImage = ResizeImage(memoryImage, width: 10, height: 20); + expect(resizeImage.width, 10); + expect(resizeImage.height, 20); + expect(resizeImage.imageProvider, memoryImage); + expect(memoryImage.resolve(ImageConfiguration.empty) != resizeImage.resolve(ImageConfiguration.empty), true); + }); - bool isAsync = false; - resizeImage.obtainKey(ImageConfiguration.empty).then((Object key) { + test('takes one dim', () async { + final Uint8List bytes = Uint8List.fromList(kTransparentImage); + final MemoryImage memoryImage = MemoryImage(bytes); + final ResizeImage resizeImage = ResizeImage(memoryImage, width: 10); + expect(resizeImage.width, 10); + expect(resizeImage.height, null); + expect(resizeImage.imageProvider, memoryImage); + expect(memoryImage.resolve(ImageConfiguration.empty) != resizeImage.resolve(ImageConfiguration.empty), true); + }); + + test('forms closure', () async { + final Uint8List bytes = Uint8List.fromList(kTransparentImage); + final MemoryImage memoryImage = MemoryImage(bytes); + final ResizeImage resizeImage = ResizeImage(memoryImage, width: 123, height: 321, allowUpscaling: true); + + Future decode(ui.ImmutableBuffer buffer, {ui.TargetImageSizeCallback? getTargetSize}) { + return PaintingBinding.instance.instantiateImageCodecWithSize(buffer, getTargetSize: (int intrinsicWidth, int intrinsicHeight) { + expect(getTargetSize, isNotNull); + final ui.TargetImageSize targetSize = getTargetSize!(intrinsicWidth, intrinsicHeight); + expect(targetSize.width, 123); + expect(targetSize.height, 321); + return targetSize; + }); + } + + resizeImage.loadImage(await resizeImage.obtainKey(ImageConfiguration.empty), decode); + }); + + test('handles sync obtainKey', () async { + final Uint8List bytes = Uint8List.fromList(kTransparentImage); + final MemoryImage memoryImage = MemoryImage(bytes); + final ResizeImage resizeImage = ResizeImage(memoryImage, width: 123, height: 321); + + bool isAsync = false; + bool keyObtained = false; + resizeImage.obtainKey(ImageConfiguration.empty).then((Object key) { + keyObtained = true; + expect(isAsync, false); + }); + isAsync = true; + expect(isAsync, true); + expect(keyObtained, true); + }); + + test('handles async obtainKey', () async { + final Uint8List bytes = Uint8List.fromList(kTransparentImage); + final _AsyncKeyMemoryImage memoryImage = _AsyncKeyMemoryImage(bytes); + final ResizeImage resizeImage = ResizeImage(memoryImage, width: 123, height: 321); + + bool isAsync = false; + final Completer completer = Completer(); + resizeImage.obtainKey(ImageConfiguration.empty).then((Object key) { + try { + expect(isAsync, true); + } finally { + completer.complete(); + } + }); + isAsync = true; + await completer.future; expect(isAsync, true); }); - isAsync = true; - expect(isAsync, true); }); } +Future _expectImageSize(ImageProvider imageProvider, Size size) async { + final Size actualSize = await _resolveAndGetSize(imageProvider); + expect(actualSize, size); +} + Future _resolveAndGetSize( ImageProvider imageProvider, { ImageConfiguration configuration = ImageConfiguration.empty,