This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Fix CkBrowserImageDecoder conversion of images to ImageByteFormat.rawRgba and rawStraightRgba #52089
Merged
auto-submit
merged 3 commits into
flutter:main
from
jason-simmons:web_decode_premul_rgba
Apr 17, 2024
Merged
Fix CkBrowserImageDecoder conversion of images to ImageByteFormat.rawRgba and rawStraightRgba #52089
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,21 +96,26 @@ Future<ByteData> readPixelsFromVideoFrame(VideoFrame videoFrame, ui.ImageByteFor | |
|
||
// At this point we know we want to read unencoded pixels, and that the video | ||
// frame is _not_ using the same format as the requested one. | ||
final bool isBgrFrame = videoFrame.format == 'BGRA' || videoFrame.format == 'BGRX'; | ||
if (format == ui.ImageByteFormat.rawRgba && isBgrFrame) { | ||
_bgrToRgba(pixels); | ||
return pixels.asByteData(); | ||
final bool isBgrx = videoFrame.format == 'BGRX'; | ||
final bool isBgrFrame = videoFrame.format == 'BGRA' || isBgrx; | ||
if (isBgrFrame) { | ||
if (format == ui.ImageByteFormat.rawStraightRgba || isBgrx) { | ||
_bgrToStraightRgba(pixels, isBgrx); | ||
return pixels.asByteData(); | ||
} else if (format == ui.ImageByteFormat.rawRgba) { | ||
_bgrToRawRgba(pixels); | ||
return pixels.asByteData(); | ||
} | ||
} | ||
|
||
// Last resort, just return the original pixels. | ||
return pixels.asByteData(); | ||
} | ||
|
||
/// Mutates the [pixels], converting them from BGRX/BGRA to RGBA. | ||
void _bgrToRgba(ByteBuffer pixels) { | ||
final int pixelCount = pixels.lengthInBytes ~/ 4; | ||
void _bgrToStraightRgba(ByteBuffer pixels, bool isBgrx) { | ||
final Uint8List pixelBytes = pixels.asUint8List(); | ||
for (int i = 0; i < pixelCount; i += 4) { | ||
for (int i = 0; i < pixelBytes.length; i += 4) { | ||
// It seems even in little-endian machines the BGR_ pixels are encoded as | ||
// big-endian, i.e. the blue byte is written into the lowest byte in the | ||
// memory address space. | ||
|
@@ -122,6 +127,34 @@ void _bgrToRgba(ByteBuffer pixels) { | |
// codecs that do something different. | ||
pixelBytes[i] = r; | ||
pixelBytes[i + 2] = b; | ||
if (isBgrx) { | ||
pixelBytes[i + 3] = 255; | ||
} | ||
} | ||
} | ||
|
||
/// Based on Chromium's SetRGBAPremultiply. | ||
int _premultiply(int value, int alpha) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might make sense to inline this function using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the inlining annotation |
||
if (alpha == 255) { | ||
return value; | ||
} | ||
const int kRoundFractionControl = 257 * 128; | ||
return (value * alpha * 257 + kRoundFractionControl) >> 16; | ||
} | ||
|
||
/// Mutates the [pixels], converting them from BGRX/BGRA to RGBA with | ||
/// premultiplied alpha. | ||
void _bgrToRawRgba(ByteBuffer pixels) { | ||
final Uint8List pixelBytes = pixels.asUint8List(); | ||
for (int i = 0; i < pixelBytes.length; i += 4) { | ||
final int a = pixelBytes[i + 3]; | ||
final int r = _premultiply(pixelBytes[i + 2], a); | ||
final int g = _premultiply(pixelBytes[i + 1], a); | ||
final int b = _premultiply(pixelBytes[i], a); | ||
|
||
pixelBytes[i] = r; | ||
pixelBytes[i + 1] = g; | ||
pixelBytes[i + 2] = b; | ||
} | ||
} | ||
|
||
|
@@ -133,7 +166,7 @@ bool _shouldReadPixelsUnmodified(VideoFrame videoFrame, ui.ImageByteFormat forma | |
// Do not convert if the requested format is RGBA and the video frame is | ||
// encoded as either RGBA or RGBX. | ||
final bool isRgbFrame = videoFrame.format == 'RGBA' || videoFrame.format == 'RGBX'; | ||
return format == ui.ImageByteFormat.rawRgba && isRgbFrame; | ||
return format == ui.ImageByteFormat.rawStraightRgba && isRgbFrame; | ||
} | ||
|
||
Future<ByteBuffer> readVideoFramePixelsUnmodified(VideoFrame videoFrame) async { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be inlined too so that the compiler specializes the function for possible values of
isBgrx
, true and false.