|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:html'; |
| 6 | +import 'dart:typed_data'; |
| 7 | + |
| 8 | +import 'package:test/bootstrap/browser.dart'; |
| 9 | +import 'package:test/test.dart'; |
| 10 | +import 'package:ui/src/engine.dart'; |
| 11 | +import 'package:ui/ui.dart' hide TextStyle; |
| 12 | + |
| 13 | +void main() { |
| 14 | + internalBootstrapBrowserTest(() => testMain); |
| 15 | +} |
| 16 | + |
| 17 | +typedef _ListPredicate<T> = bool Function(List<T>); |
| 18 | +_ListPredicate<T> deepEqualList<T>(List<T> a) { |
| 19 | + return (List<T> b) { |
| 20 | + if (a.length != b.length) |
| 21 | + return false; |
| 22 | + for (int i = 0; i < a.length; i += 1) { |
| 23 | + if (a[i] != b[i]) |
| 24 | + return false; |
| 25 | + } |
| 26 | + return true; |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +Matcher listEqual(List<int> source, {int tolerance = 0}) { |
| 31 | + return predicate( |
| 32 | + (List<int> target) { |
| 33 | + if (source.length != target.length) |
| 34 | + return false; |
| 35 | + for (int i = 0; i < source.length; i += 1) { |
| 36 | + if ((source[i] - target[i]).abs() > tolerance) |
| 37 | + return false; |
| 38 | + } |
| 39 | + return true; |
| 40 | + }, |
| 41 | + source.toString(), |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +// Converts `rawPixels` into a list of bytes that represent raw pixels in rgba8888. |
| 46 | +// |
| 47 | +// Each element of `rawPixels` represents a bytes in order 0xRRGGBBAA, with |
| 48 | +// pixel order Left to right, then top to bottom. |
| 49 | +Uint8List _pixelsToBytes(List<int> rawPixels) { |
| 50 | + return Uint8List.fromList((() sync* { |
| 51 | + for (final int pixel in rawPixels) { |
| 52 | + yield (pixel >> 24) & 0xff; // r |
| 53 | + yield (pixel >> 16) & 0xff; // g |
| 54 | + yield (pixel >> 8) & 0xff; // b |
| 55 | + yield (pixel >> 0) & 0xff; // a |
| 56 | + } |
| 57 | + })().toList()); |
| 58 | +} |
| 59 | + |
| 60 | +Future<Image> _encodeToHtmlThenDecode( |
| 61 | + Uint8List rawBytes, |
| 62 | + int width, |
| 63 | + int height, { |
| 64 | + PixelFormat pixelFormat = PixelFormat.rgba8888, |
| 65 | +}) async { |
| 66 | + final ImageDescriptor descriptor = ImageDescriptor.raw( |
| 67 | + await ImmutableBuffer.fromUint8List(rawBytes), |
| 68 | + width: width, |
| 69 | + height: height, |
| 70 | + pixelFormat: pixelFormat, |
| 71 | + ); |
| 72 | + return (await (await descriptor.instantiateCodec()).getNextFrame()).image; |
| 73 | +} |
| 74 | + |
| 75 | +Future<void> testMain() async { |
| 76 | + test('Correctly encodes an opaque image', () async { |
| 77 | + // A 2x2 testing image without transparency. |
| 78 | + final Image sourceImage = await _encodeToHtmlThenDecode( |
| 79 | + _pixelsToBytes( |
| 80 | + <int>[0xFF0102FF, 0x04FE05FF, 0x0708FDFF, 0x0A0B0C00], |
| 81 | + ), 2, 2, |
| 82 | + ); |
| 83 | + final Uint8List actualPixels = Uint8List.sublistView( |
| 84 | + (await sourceImage.toByteData(format: ImageByteFormat.rawStraightRgba))!); |
| 85 | + // The `benchmarkPixels` is identical to `sourceImage` except for the fully |
| 86 | + // transparent last pixel, whose channels are turned 0. |
| 87 | + final Uint8List benchmarkPixels = _pixelsToBytes( |
| 88 | + <int>[0xFF0102FF, 0x04FE05FF, 0x0708FDFF, 0x00000000], |
| 89 | + ); |
| 90 | + expect(actualPixels, listEqual(benchmarkPixels)); |
| 91 | + }); |
| 92 | + |
| 93 | + test('Correctly encodes an opaque image in bgra8888', () async { |
| 94 | + // A 2x2 testing image without transparency. |
| 95 | + final Image sourceImage = await _encodeToHtmlThenDecode( |
| 96 | + _pixelsToBytes( |
| 97 | + <int>[0xFF0102FF, 0x04FE05FF, 0x0708FDFF, 0x0A0B0C00], |
| 98 | + ), 2, 2, pixelFormat: PixelFormat.bgra8888, |
| 99 | + ); |
| 100 | + final Uint8List actualPixels = Uint8List.sublistView( |
| 101 | + (await sourceImage.toByteData(format: ImageByteFormat.rawStraightRgba))!); |
| 102 | + // The `benchmarkPixels` is the same as `sourceImage` except that the R and |
| 103 | + // G channels are swapped and the fully transparent last pixel is turned 0. |
| 104 | + final Uint8List benchmarkPixels = _pixelsToBytes( |
| 105 | + <int>[0x0201FFFF, 0x05FE04FF, 0xFD0807FF, 0x00000000], |
| 106 | + ); |
| 107 | + expect(actualPixels, listEqual(benchmarkPixels)); |
| 108 | + }); |
| 109 | + |
| 110 | + test('Correctly encodes a transparent image', () async { |
| 111 | + // A 2x2 testing image with transparency. |
| 112 | + final Image sourceImage = await _encodeToHtmlThenDecode( |
| 113 | + _pixelsToBytes( |
| 114 | + <int>[0xFF800006, 0xFF800080, 0xFF8000C0, 0xFF8000FF], |
| 115 | + ), 2, 2, |
| 116 | + ); |
| 117 | + final Image blueBackground = await _encodeToHtmlThenDecode( |
| 118 | + _pixelsToBytes( |
| 119 | + <int>[0x0000FFFF, 0x0000FFFF, 0x0000FFFF, 0x0000FFFF], |
| 120 | + ), 2, 2, |
| 121 | + ); |
| 122 | + // The standard way of testing the raw bytes of `sourceImage` is to draw |
| 123 | + // the image onto a canvas and fetch its data (see HtmlImage.toByteData). |
| 124 | + // But here, we draw an opaque background first before drawing the image, |
| 125 | + // and test if the blended result is expected. |
| 126 | + // |
| 127 | + // This is because, if we only draw the `sourceImage`, the resulting pixels |
| 128 | + // will be slightly off from the raw pixels. The reason is unknown, but |
| 129 | + // very likely because the canvas.getImageData introduces rounding errors |
| 130 | + // if any pixels are left semi-transparent, which might be caused by |
| 131 | + // converting to and from pre-multiplied values. See |
| 132 | + // https://github.com/flutter/flutter/issues/92958 . |
| 133 | + final CanvasElement canvas = CanvasElement() |
| 134 | + ..width = 2 |
| 135 | + ..height = 2; |
| 136 | + final CanvasRenderingContext2D ctx = canvas.context2D; |
| 137 | + ctx.drawImage((blueBackground as HtmlImage).imgElement, 0, 0); |
| 138 | + ctx.drawImage((sourceImage as HtmlImage).imgElement, 0, 0); |
| 139 | + |
| 140 | + final ImageData imageData = ctx.getImageData(0, 0, 2, 2); |
| 141 | + final List<int> actualPixels = imageData.data; |
| 142 | + |
| 143 | + final Uint8List benchmarkPixels = _pixelsToBytes( |
| 144 | + <int>[0x0603F9FF, 0x80407FFF, 0xC0603FFF, 0xFF8000FF], |
| 145 | + ); |
| 146 | + expect(actualPixels, listEqual(benchmarkPixels, tolerance: 1)); |
| 147 | + }); |
| 148 | +} |
0 commit comments