Skip to content

Commit 7072872

Browse files
Fix CkBrowserImageDecoder conversion of images to ImageByteFormat.rawRgba and rawStraightRgba (flutter#52089)
VideoFrames of decoded images typically contain BGRA format. The CkBrowserImageDecoder implementation of toByteData needs to convert that into RGBA format. If the output format is rawRgba then it also needs to apply premultipled alpha. Also fixes an issue where _bgrToRgba was not converting all pixels in the image. Fixes flutter#135409 Fixes flutter#144770
1 parent 0113af1 commit 7072872

3 files changed

Lines changed: 78 additions & 8 deletions

File tree

lib/web_ui/lib/src/engine/canvaskit/image_web_codecs.dart

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,26 @@ Future<ByteData> readPixelsFromVideoFrame(VideoFrame videoFrame, ui.ImageByteFor
9696

9797
// At this point we know we want to read unencoded pixels, and that the video
9898
// frame is _not_ using the same format as the requested one.
99-
final bool isBgrFrame = videoFrame.format == 'BGRA' || videoFrame.format == 'BGRX';
100-
if (format == ui.ImageByteFormat.rawRgba && isBgrFrame) {
101-
_bgrToRgba(pixels);
102-
return pixels.asByteData();
99+
final bool isBgrx = videoFrame.format == 'BGRX';
100+
final bool isBgrFrame = videoFrame.format == 'BGRA' || isBgrx;
101+
if (isBgrFrame) {
102+
if (format == ui.ImageByteFormat.rawStraightRgba || isBgrx) {
103+
_bgrToStraightRgba(pixels, isBgrx);
104+
return pixels.asByteData();
105+
} else if (format == ui.ImageByteFormat.rawRgba) {
106+
_bgrToRawRgba(pixels);
107+
return pixels.asByteData();
108+
}
103109
}
104110

105111
// Last resort, just return the original pixels.
106112
return pixels.asByteData();
107113
}
108114

109115
/// Mutates the [pixels], converting them from BGRX/BGRA to RGBA.
110-
void _bgrToRgba(ByteBuffer pixels) {
111-
final int pixelCount = pixels.lengthInBytes ~/ 4;
116+
void _bgrToStraightRgba(ByteBuffer pixels, bool isBgrx) {
112117
final Uint8List pixelBytes = pixels.asUint8List();
113-
for (int i = 0; i < pixelCount; i += 4) {
118+
for (int i = 0; i < pixelBytes.length; i += 4) {
114119
// It seems even in little-endian machines the BGR_ pixels are encoded as
115120
// big-endian, i.e. the blue byte is written into the lowest byte in the
116121
// memory address space.
@@ -122,6 +127,35 @@ void _bgrToRgba(ByteBuffer pixels) {
122127
// codecs that do something different.
123128
pixelBytes[i] = r;
124129
pixelBytes[i + 2] = b;
130+
if (isBgrx) {
131+
pixelBytes[i + 3] = 255;
132+
}
133+
}
134+
}
135+
136+
/// Based on Chromium's SetRGBAPremultiply.
137+
@pragma('dart2js:tryInline')
138+
int _premultiply(int value, int alpha) {
139+
if (alpha == 255) {
140+
return value;
141+
}
142+
const int kRoundFractionControl = 257 * 128;
143+
return (value * alpha * 257 + kRoundFractionControl) >> 16;
144+
}
145+
146+
/// Mutates the [pixels], converting them from BGRX/BGRA to RGBA with
147+
/// premultiplied alpha.
148+
void _bgrToRawRgba(ByteBuffer pixels) {
149+
final Uint8List pixelBytes = pixels.asUint8List();
150+
for (int i = 0; i < pixelBytes.length; i += 4) {
151+
final int a = pixelBytes[i + 3];
152+
final int r = _premultiply(pixelBytes[i + 2], a);
153+
final int g = _premultiply(pixelBytes[i + 1], a);
154+
final int b = _premultiply(pixelBytes[i], a);
155+
156+
pixelBytes[i] = r;
157+
pixelBytes[i + 1] = g;
158+
pixelBytes[i + 2] = b;
125159
}
126160
}
127161

@@ -133,7 +167,7 @@ bool _shouldReadPixelsUnmodified(VideoFrame videoFrame, ui.ImageByteFormat forma
133167
// Do not convert if the requested format is RGBA and the video frame is
134168
// encoded as either RGBA or RGBX.
135169
final bool isRgbFrame = videoFrame.format == 'RGBA' || videoFrame.format == 'RGBX';
136-
return format == ui.ImageByteFormat.rawRgba && isRgbFrame;
170+
return format == ui.ImageByteFormat.rawStraightRgba && isRgbFrame;
137171
}
138172

139173
Future<ByteBuffer> readVideoFramePixelsUnmodified(VideoFrame videoFrame) async {

lib/web_ui/test/canvaskit/image_golden_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,24 @@ void _testCkBrowserImageDecoder() {
895895

896896
debugRestoreWebDecoderExpireDuration();
897897
});
898+
899+
test('ImageDecoder toByteData(translucent PNG)', () async {
900+
final CkBrowserImageDecoder image = await CkBrowserImageDecoder.create(
901+
data: kTranslucentPng,
902+
debugSource: 'test',
903+
);
904+
final ui.FrameInfo frame = await image.getNextFrame();
905+
906+
ByteData? data = await frame.image.toByteData(format: ui.ImageByteFormat.rawStraightRgba);
907+
expect(data!.buffer.asUint8List(),
908+
<int>[0x22, 0x44, 0x66, 0x80, 0x22, 0x44, 0x66, 0x80,
909+
0x22, 0x44, 0x66, 0x80, 0x22, 0x44, 0x66, 0x80]);
910+
911+
data = await frame.image.toByteData();
912+
expect(data!.buffer.asUint8List(),
913+
<int>[0x11, 0x22, 0x33, 0x80, 0x11, 0x22, 0x33, 0x80,
914+
0x11, 0x22, 0x33, 0x80, 0x11, 0x22, 0x33, 0x80]);
915+
});
898916
}
899917

900918
Future<void> expectFrameData(ui.FrameInfo frame, List<int> data) async {

lib/web_ui/test/canvaskit/test_data.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,21 @@ final Uint8List kAnimatedGif = Uint8List.fromList(<int> [
3535
0xf9, 0x04, 0x00, 0x0a, 0x00, 0xff, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x01,
3636
0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3b,
3737
]);
38+
39+
/// A 2x2 translucent PNG.
40+
final Uint8List kTranslucentPng = Uint8List.fromList(<int> [
41+
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49,
42+
0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x01, 0x03,
43+
0x00, 0x00, 0x00, 0x48, 0x78, 0x9f, 0x67, 0x00, 0x00, 0x00, 0x20, 0x63, 0x48,
44+
0x52, 0x4d, 0x00, 0x00, 0x7a, 0x26, 0x00, 0x00, 0x80, 0x84, 0x00, 0x00, 0xfa,
45+
0x00, 0x00, 0x00, 0x80, 0xe8, 0x00, 0x00, 0x75, 0x30, 0x00, 0x00, 0xea, 0x60,
46+
0x00, 0x00, 0x3a, 0x98, 0x00, 0x00, 0x17, 0x70, 0x9c, 0xba, 0x51, 0x3c, 0x00,
47+
0x00, 0x00, 0x06, 0x50, 0x4c, 0x54, 0x45, 0x22, 0x44, 0x66, 0xff, 0xff, 0xff,
48+
0x5c, 0x83, 0x6d, 0xb6, 0x00, 0x00, 0x00, 0x01, 0x74, 0x52, 0x4e, 0x53, 0x80,
49+
0xad, 0x5e, 0x5b, 0x46, 0x00, 0x00, 0x00, 0x01, 0x62, 0x4b, 0x47, 0x44, 0x01,
50+
0xff, 0x02, 0x2d, 0xde, 0x00, 0x00, 0x00, 0x07, 0x74, 0x49, 0x4d, 0x45, 0x07,
51+
0xe8, 0x04, 0x0c, 0x15, 0x16, 0x21, 0xc3, 0x89, 0xee, 0x25, 0x00, 0x00, 0x00,
52+
0x0c, 0x49, 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0x60, 0x60, 0x60, 0x00, 0x00,
53+
0x00, 0x04, 0x00, 0x01, 0x27, 0x34, 0x27, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x49,
54+
0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
55+
]);

0 commit comments

Comments
 (0)