Skip to content

Commit c091098

Browse files
authored
Use round() for height inference in ImageDescriptor.instantiateCodec (#190925)
When ImageDescriptor.instantiateCodec receives only targetWidth, it derives targetHeight using `targetHeight = targetWidth ~/ (width / height)`. The mirror calculation in the same function derives `targetWidth = (targetHeight * (width / height)).round()`. The former calculation ends up flooring results and can change the original image's aspect ratio. In web_ui, both inferences use `round()`. https://github.com/flutter/flutter/blob/b5971cabf92bbc126be2a82bc87052af6aeae5a2/engine/src/flutter/lib/web_ui/lib/src/engine/image_decoder.dart#L517. The original PR flutter-team-archive/engine#19537 doesn't mention a rationale for this discrepancy. Because the current logic can change the original image's aspect ratio, it's probably the right move to make this calculation consistent with the rest. Fixes #190528
1 parent 27d9688 commit c091098

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎engine/src/flutter/lib/ui/painting.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9123,7 +9123,7 @@ base class _NativeImageDescriptor extends NativeFieldWrapperClass1 implements Im
91239123
} else if (targetWidth == null && targetHeight != null) {
91249124
targetWidth = math.max(1, (targetHeight * (width / height)).round());
91259125
} else if (targetHeight == null && targetWidth != null) {
9126-
targetHeight = math.max(1, targetWidth ~/ (width / height));
9126+
targetHeight = math.max(1, (targetWidth * (height / width)).round());
91279127
}
91289128
assert(targetWidth != null);
91299129
assert(targetHeight != null);

‎engine/src/flutter/testing/dart/image_descriptor_test.dart‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,26 @@ void main() {
7474
codec.dispose();
7575
});
7676

77+
test('image descriptor - height inference with rounding', () async {
78+
final bytes = Uint8List.fromList(List<int>.filled(3 * 2 * 4, 0xFFABCDEF));
79+
final ImmutableBuffer buffer = await ImmutableBuffer.fromUint8List(bytes);
80+
final descriptor = ImageDescriptor.raw(
81+
buffer,
82+
width: 3,
83+
height: 2,
84+
rowBytes: 3 * 4,
85+
pixelFormat: PixelFormat.rgba8888,
86+
);
87+
88+
final Codec codec = await descriptor.instantiateCodec(targetWidth: 4);
89+
final FrameInfo frame = await codec.getNextFrame();
90+
expect(frame.image.width, 4);
91+
expect(frame.image.height, 3);
92+
frame.image.dispose();
93+
codec.dispose();
94+
descriptor.dispose();
95+
});
96+
7797
test('HEIC image', () async {
7898
final Uint8List bytes = await readFile('grill_chicken.heic');
7999
final ImmutableBuffer buffer = await ImmutableBuffer.fromUint8List(bytes);

‎engine/src/flutter/testing/dart/image_resize_test.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ void main() {
114114
expect(resized.width, 1);
115115
});
116116

117+
test('pixels: resize height with constrained width - rounding', () async {
118+
final rect = BlackSquare.create(width: 3);
119+
final Image resized = await rect.resize(targetWidth: 4, allowUpscaling: true);
120+
expect(resized.width, 4);
121+
expect(resized.height, 3);
122+
resized.dispose();
123+
});
124+
117125
test('pixels: upscale image by 5x', () async {
118126
final blackSquare = BlackSquare.create();
119127
final Image resized = await blackSquare.resize(targetWidth: 10, allowUpscaling: true);

0 commit comments

Comments
 (0)