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
[Web] Fix BMP encoder #29448
Merged
Merged
[Web] Fix BMP encoder #29448
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1c8b584
Impl
dkwingsmt a2efaed
Add test
dkwingsmt 095d214
Fully fix
dkwingsmt fed897f
Fix analysis
dkwingsmt 5235706
Use a differnt test method
dkwingsmt f66bc03
Better doc
dkwingsmt 66d79a2
Merge remote-tracking branch 'upstream/master' into fix-web-bmp-conve…
dkwingsmt e1a20bf
add tolerance
dkwingsmt 9f9d375
Fix
dkwingsmt 28f51a2
Assign by pixel
dkwingsmt 24d637e
Add swap
dkwingsmt 095e7b9
Better var name
dkwingsmt c0b1906
Update painting.dart
dkwingsmt 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
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 |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'dart:html'; | ||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:test/bootstrap/browser.dart'; | ||
| import 'package:test/test.dart'; | ||
| import 'package:ui/src/engine.dart'; | ||
| import 'package:ui/ui.dart' hide TextStyle; | ||
|
|
||
| void main() { | ||
| internalBootstrapBrowserTest(() => testMain); | ||
| } | ||
|
|
||
| typedef _ListPredicate<T> = bool Function(List<T>); | ||
| _ListPredicate<T> deepEqualList<T>(List<T> a) { | ||
| return (List<T> b) { | ||
| if (a.length != b.length) | ||
| return false; | ||
| for (int i = 0; i < a.length; i += 1) { | ||
| if (a[i] != b[i]) | ||
| return false; | ||
| } | ||
| return true; | ||
| }; | ||
| } | ||
|
|
||
| Matcher listEqual(List<int> source, {int tolerance = 0}) { | ||
| return predicate( | ||
| (List<int> target) { | ||
| if (source.length != target.length) | ||
| return false; | ||
| for (int i = 0; i < source.length; i += 1) { | ||
| if ((source[i] - target[i]).abs() > tolerance) | ||
| return false; | ||
| } | ||
| return true; | ||
| }, | ||
| source.toString(), | ||
| ); | ||
| } | ||
|
|
||
| // Converts `rawPixels` into a list of bytes that represent raw pixels in rgba8888. | ||
| // | ||
| // Each element of `rawPixels` represents a bytes in order 0xRRGGBBAA, with | ||
| // pixel order Left to right, then top to bottom. | ||
| Uint8List _pixelsToBytes(List<int> rawPixels) { | ||
| return Uint8List.fromList((() sync* { | ||
| for (final int pixel in rawPixels) { | ||
| yield (pixel >> 24) & 0xff; // r | ||
| yield (pixel >> 16) & 0xff; // g | ||
| yield (pixel >> 8) & 0xff; // b | ||
| yield (pixel >> 0) & 0xff; // a | ||
| } | ||
| })().toList()); | ||
| } | ||
|
|
||
| Future<Image> _encodeToHtmlThenDecode(Uint8List rawBytes, int width, int height) async { | ||
| final ImageDescriptor descriptor = ImageDescriptor.raw( | ||
| await ImmutableBuffer.fromUint8List(rawBytes), | ||
| width: 2, | ||
| height: 2, | ||
| pixelFormat: PixelFormat.rgba8888, | ||
| ); | ||
| return (await (await descriptor.instantiateCodec()).getNextFrame()).image; | ||
| } | ||
|
|
||
| Future<void> testMain() async { | ||
| test('Correctly encodes an opaque image', () async { | ||
| // A 2x2 testing image without transparency. | ||
| final Image sourceImage = await _encodeToHtmlThenDecode( | ||
| _pixelsToBytes( | ||
| <int>[0xFF0102FF, 0x04FE05FF, 0x0708FDFF, 0x0A0B0C00], | ||
| ), 2, 2, | ||
| ); | ||
| final Uint8List actualPixels = Uint8List.sublistView( | ||
| (await sourceImage.toByteData(format: ImageByteFormat.rawStraightRgba))!); | ||
| // The `benchmarkPixels` is identical to `sourceImage` except for the fully | ||
| // transparent last pixel, whose channels are turned 0. | ||
| final Uint8List benchmarkPixels = _pixelsToBytes( | ||
| <int>[0xFF0102FF, 0x04FE05FF, 0x0708FDFF, 0x00000000], | ||
| ); | ||
| expect(actualPixels, listEqual(benchmarkPixels)); | ||
| }); | ||
|
|
||
| test('Correctly encodes a transparent image', () async { | ||
| // A 2x2 testing image with transparency. | ||
| final Image sourceImage = await _encodeToHtmlThenDecode( | ||
| _pixelsToBytes( | ||
| <int>[0xFF800006, 0xFF800080, 0xFF8000C0, 0xFF8000FF], | ||
| ), 2, 2, | ||
| ); | ||
| final Image blueBackground = await _encodeToHtmlThenDecode( | ||
| _pixelsToBytes( | ||
| <int>[0x0000FFFF, 0x0000FFFF, 0x0000FFFF, 0x0000FFFF], | ||
| ), 2, 2, | ||
| ); | ||
| // The standard way of testing the raw bytes of `sourceImage` is to draw | ||
| // the image onto a canvas and fetch its data (see HtmlImage.toByteData). | ||
| // But here, we draw an opaque background first before drawing the image, | ||
| // and test if the blended result is expected. | ||
| // | ||
| // This is because, if we only draw the `sourceImage`, the resulting pixels | ||
| // will be slightly off from the raw pixels. The reason is unknown, but | ||
| // very likely because the canvas.getImageData introduces rounding errors | ||
| // if any pixels are left semi-transparent, which might be caused by | ||
| // converting to and from pre-multiplied values. See | ||
| // https://github.com/flutter/flutter/issues/92958 . | ||
| final CanvasElement canvas = CanvasElement() | ||
| ..width = 2 | ||
| ..height = 2; | ||
| final CanvasRenderingContext2D ctx = canvas.context2D; | ||
| ctx.drawImage((blueBackground as HtmlImage).imgElement, 0, 0); | ||
| ctx.drawImage((sourceImage as HtmlImage).imgElement, 0, 0); | ||
|
|
||
| final ImageData imageData = ctx.getImageData(0, 0, 2, 2); | ||
| final List<int> actualPixels = imageData.data; | ||
|
|
||
| final Uint8List benchmarkPixels = _pixelsToBytes( | ||
| <int>[0x0603F9FF, 0x80407FFF, 0xC0603FFF, 0xFF8000FF], | ||
| ); | ||
| expect(actualPixels, listEqual(benchmarkPixels, tolerance: 1)); | ||
| }); | ||
| } | ||
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.
Do the opacity tests get any better if you just do a 1x1 pixel image that's fully transparent?
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.
The test is fine if all pixels of the image is either fully opaque or fully transparent. The problem occurs if it contains any pixel between 1 and 254. So 1x1 fully transparent should work fine (and has been tested in the opaque image one.)