diff --git a/engine/src/flutter/lib/ui/painting.dart b/engine/src/flutter/lib/ui/painting.dart index f5e11c1a65..84e5769b2a 100644 --- a/engine/src/flutter/lib/ui/painting.dart +++ b/engine/src/flutter/lib/ui/painting.dart @@ -1019,13 +1019,11 @@ enum Clip { antiAliasWithSaveLayer, } -// Indicates that the image should not be resized in this dimension. -// -// Used by [instantiateImageCodec] as a magical value to disable resizing -// in the given dimension. -// -// This needs to be kept in sync with "kDoNotResizeDimension" in codec.cc -const int _kDoNotResizeDimension = -1; +/// Indicates that the image should not be resized in this dimension. +/// +/// Used by [instantiateImageCodec] as a magical value to disable resizing +/// in the given dimension. +const int kDoNotResizeDimension = -1; /// A description of the style to use when drawing on a [Canvas]. /// @@ -1667,7 +1665,7 @@ Future instantiateImageCodec(Uint8List list, { int targetHeight, }) { return _futurize( - (_Callback callback) => _instantiateImageCodec(list, callback, null, targetWidth ?? _kDoNotResizeDimension, targetHeight ?? _kDoNotResizeDimension) + (_Callback callback) => _instantiateImageCodec(list, callback, null, targetWidth ?? kDoNotResizeDimension, targetHeight ?? kDoNotResizeDimension) ); } @@ -1677,9 +1675,9 @@ Future instantiateImageCodec(Uint8List list, { /// image, in image pixels. Image in this context refers to image in every frame of the [Codec]. /// If [targetWidth] and [targetHeight] are not equal to the intrinsic dimensions of the /// image, then the image will be scaled after being decoded. If exactly one of -/// these two arguments is not equal to [_kDoNotResizeDimension], then the aspect +/// these two arguments is not equal to [kDoNotResizeDimension], then the aspect /// ratio will be maintained while forcing the image to match the given dimension. -/// If both are equal to [_kDoNotResizeDimension], then the image maintains its real size. +/// If both are equal to [kDoNotResizeDimension], then the image maintains its real size. /// /// Returns an error message if the instantiation has failed, null otherwise. String _instantiateImageCodec(Uint8List list, _Callback callback, _ImageInfo imageInfo, int targetWidth, int targetHeight) @@ -1724,7 +1722,7 @@ void decodeImageFromPixels( ) { final _ImageInfo imageInfo = _ImageInfo(width, height, format.index, rowBytes); final Future codecFuture = _futurize( - (_Callback callback) => _instantiateImageCodec(pixels, callback, imageInfo, targetWidth ?? _kDoNotResizeDimension, targetHeight ?? _kDoNotResizeDimension) + (_Callback callback) => _instantiateImageCodec(pixels, callback, imageInfo, targetWidth ?? kDoNotResizeDimension, targetHeight ?? kDoNotResizeDimension) ); codecFuture.then((Codec codec) => codec.getNextFrame()) .then((FrameInfo frameInfo) => callback(frameInfo.image)); diff --git a/engine/src/flutter/lib/ui/painting/codec.cc b/engine/src/flutter/lib/ui/painting/codec.cc index b3fdaf4db5..829b9db65e 100644 --- a/engine/src/flutter/lib/ui/painting/codec.cc +++ b/engine/src/flutter/lib/ui/painting/codec.cc @@ -33,9 +33,6 @@ namespace flutter { namespace { -// This needs to be kept in sync with _kDoNotResizeDimension in painting.dart -const int kDoNotResizeDimension = -1; - // This must be kept in sync with the enum in painting.dart enum PixelFormat { kRGBA8888, @@ -217,10 +214,10 @@ static void InstantiateImageCodec(Dart_NativeArguments args) { ImageDecoder::ImageDescriptor descriptor; descriptor.decompressed_image_info = image_info; - if (targetWidth != kDoNotResizeDimension) { + if (targetWidth > 0) { descriptor.target_width = targetWidth; } - if (targetHeight != kDoNotResizeDimension) { + if (targetHeight > 0) { descriptor.target_height = targetHeight; } descriptor.data = std::move(buffer); diff --git a/engine/src/flutter/lib/web_ui/lib/src/ui/painting.dart b/engine/src/flutter/lib/web_ui/lib/src/ui/painting.dart index 65795e8df1..09190abbff 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/ui/painting.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/ui/painting.dart @@ -1628,6 +1628,12 @@ enum PixelFormat { bgra8888, } +/// Indicates that the image should not be resized in this dimension. +/// +/// Used by [instantiateImageCodec] as a magical value to disable resizing +/// in the given dimension. +const int kDoNotResizeDimension = -1; + class _ImageInfo { _ImageInfo(this.width, this.height, this.format, this.rowBytes) { rowBytes ??= width * 4; @@ -1705,9 +1711,12 @@ class Codec { /// /// The returned future can complete with an error if the image decoding has /// failed. -Future instantiateImageCodec(Uint8List list, - {double decodedCacheRatioCap = double.infinity}) { +Future instantiateImageCodec(Uint8List list, { + int targetWidth, + int targetHeight, +}) { return engine.futurize((engine.Callback callback) => + // TODO: Implement targetWidth and targetHeight support. _instantiateImageCodec(list, callback, null)); } diff --git a/engine/src/flutter/testing/dart/image_resize_test.dart b/engine/src/flutter/testing/dart/image_resize_test.dart index 5976fab97f..b5e1d364a0 100644 --- a/engine/src/flutter/testing/dart/image_resize_test.dart +++ b/engine/src/flutter/testing/dart/image_resize_test.dart @@ -97,6 +97,14 @@ void main() { expect(resized.height, 1); expect(resized.width, 10); }); + + test('pixels: large negative dimensions', () async { + final BlackSquare blackSquare = BlackSquare.create(); + final Image resized = + await blackSquare.resize(targetHeight: -100, targetWidth: -99999); + expect(resized.height, 2); + expect(resized.width, 2); + }); } class BlackSquare {