[web] Support raw straight RGBA format in Image.toByteData() in Html (flutter/engine#28357)
This commit is contained in:
@@ -172,24 +172,25 @@ class HtmlImage implements ui.Image {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<ByteData?> toByteData({ui.ImageByteFormat format = ui.ImageByteFormat.rawRgba}) {
|
Future<ByteData?> toByteData({ui.ImageByteFormat format = ui.ImageByteFormat.rawRgba}) {
|
||||||
if (format == ui.ImageByteFormat.rawRgba) {
|
switch (format) {
|
||||||
final html.CanvasElement canvas = html.CanvasElement()
|
// TODO(ColdPaleLight): https://github.com/flutter/flutter/issues/89128
|
||||||
..width = width
|
// The format rawRgba always returns straight rather than premul currently.
|
||||||
..height = height;
|
case ui.ImageByteFormat.rawRgba:
|
||||||
final html.CanvasRenderingContext2D ctx = canvas.context2D;
|
case ui.ImageByteFormat.rawStraightRgba:
|
||||||
ctx.drawImage(imgElement, 0, 0);
|
final html.CanvasElement canvas = html.CanvasElement()
|
||||||
final html.ImageData imageData = ctx.getImageData(0, 0, width, height);
|
..width = width
|
||||||
return Future<ByteData?>.value(imageData.data.buffer.asByteData());
|
..height = height;
|
||||||
} else if (format == ui.ImageByteFormat.rawStraightRgba) {
|
final html.CanvasRenderingContext2D ctx = canvas.context2D;
|
||||||
// TODO(ColdPaleLight): https://github.com/flutter/flutter/issues/89094
|
ctx.drawImage(imgElement, 0, 0);
|
||||||
throw UnsupportedError(
|
final html.ImageData imageData = ctx.getImageData(0, 0, width, height);
|
||||||
'Image.toByteData(format: ui.ImageByteFormat.rawStraightRgba) not yet implemented for HTML');
|
return Future<ByteData?>.value(imageData.data.buffer.asByteData());
|
||||||
}
|
default:
|
||||||
if (imgElement.src?.startsWith('data:') == true) {
|
if (imgElement.src?.startsWith('data:') == true) {
|
||||||
final UriData data = UriData.fromUri(Uri.parse(imgElement.src!));
|
final UriData data = UriData.fromUri(Uri.parse(imgElement.src!));
|
||||||
return Future<ByteData?>.value(data.contentAsBytes().buffer.asByteData());
|
return Future<ByteData?>.value(data.contentAsBytes().buffer.asByteData());
|
||||||
} else {
|
} else {
|
||||||
return Future<ByteData?>.value(null);
|
return Future<ByteData?>.value(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,13 +20,19 @@ Future<void> testMain() async {
|
|||||||
await webOnlyFontCollection.ensureFontsLoaded();
|
await webOnlyFontCollection.ensureFontsLoaded();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Picture.toImage().toByteData()', () async {
|
Future<Image> createTestImageByColor(Color color) async {
|
||||||
final EnginePictureRecorder recorder = EnginePictureRecorder();
|
final EnginePictureRecorder recorder = EnginePictureRecorder();
|
||||||
final RecordingCanvas canvas =
|
final RecordingCanvas canvas =
|
||||||
recorder.beginRecording(const Rect.fromLTRB(0, 0, 2, 2));
|
recorder.beginRecording(const Rect.fromLTRB(0, 0, 2, 2));
|
||||||
canvas.drawColor(const Color(0xFFCCDD00), BlendMode.srcOver);
|
canvas.drawColor(color, BlendMode.srcOver);
|
||||||
final Picture testPicture = recorder.endRecording();
|
final Picture testPicture = recorder.endRecording();
|
||||||
final Image testImage = await testPicture.toImage(2, 2);
|
final Image testImage = await testPicture.toImage(2, 2);
|
||||||
|
return testImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
test('Picture.toImage().toByteData()', () async {
|
||||||
|
final Image testImage = await createTestImageByColor(const Color(0xFFCCDD00));
|
||||||
|
|
||||||
final ByteData bytes =
|
final ByteData bytes =
|
||||||
(await testImage.toByteData(format: ImageByteFormat.rawRgba))!;
|
(await testImage.toByteData(format: ImageByteFormat.rawRgba))!;
|
||||||
expect(
|
expect(
|
||||||
@@ -45,4 +51,15 @@ Future<void> testMain() async {
|
|||||||
pngHeader,
|
pngHeader,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Image.toByteData(format: ImageByteFormat.rawStraightRgba)', () async {
|
||||||
|
final Image testImage = await createTestImageByColor(const Color(0xAAFFFF00));
|
||||||
|
|
||||||
|
final ByteData bytes =
|
||||||
|
(await testImage.toByteData(format: ImageByteFormat.rawStraightRgba))!;
|
||||||
|
expect(
|
||||||
|
bytes.buffer.asUint32List(),
|
||||||
|
<int>[0xAA00FFFF, 0xAA00FFFF, 0xAA00FFFF, 0xAA00FFFF],
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user