2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
+ import 'dart:math' as math;
5
6
import 'dart:typed_data' ;
6
7
7
8
import 'package:flutter/services.dart' ;
@@ -19,12 +20,49 @@ double _decodeBGR10(int x) {
19
20
return (x * slope) + intercept;
20
21
}
21
22
23
+ double _decodeHalf (int x) {
24
+ if (x == 0x7c00 ) {
25
+ return double .infinity;
26
+ }
27
+ if (x == 0xfc00 ) {
28
+ return - double .infinity;
29
+ }
30
+ final double sign = x & 0x8000 == 0 ? 1.0 : - 1.0 ;
31
+ final int exponent = (x >> 10 ) & 0x1f ;
32
+ final int fraction = x & 0x3ff ;
33
+ if (exponent == 0 ) {
34
+ return sign * math.pow (2.0 , - 14 ) * (fraction / 1024.0 );
35
+ } else {
36
+ return sign * math.pow (2.0 , exponent - 15 ) * (1.0 + fraction / 1024.0 );
37
+ }
38
+ }
39
+
22
40
bool _isAlmost (double x, double y, double epsilon) {
23
41
return (x - y).abs () < epsilon;
24
42
}
25
43
26
44
List <double > _deepRed = < double > [1.0931 , - 0.2268 , - 0.1501 ];
27
45
46
+ bool _findRGBAF16Color (
47
+ Uint8List bytes, int width, int height, List <double > color) {
48
+ final ByteData byteData = ByteData .sublistView (bytes);
49
+ expect (bytes.lengthInBytes, width * height * 8 );
50
+ expect (bytes.lengthInBytes, byteData.lengthInBytes);
51
+ bool foundDeepRed = false ;
52
+ for (int i = 0 ; i < bytes.lengthInBytes; i += 8 ) {
53
+ final int pixel = byteData.getUint64 (i, Endian .host);
54
+ final double blue = _decodeHalf ((pixel >> 32 ) & 0xffff );
55
+ final double green = _decodeHalf ((pixel >> 16 ) & 0xffff );
56
+ final double red = _decodeHalf ((pixel >> 0 ) & 0xffff );
57
+ if (_isAlmost (red, color[0 ], 0.01 ) &&
58
+ _isAlmost (green, color[1 ], 0.01 ) &&
59
+ _isAlmost (blue, color[2 ], 0.01 )) {
60
+ foundDeepRed = true ;
61
+ }
62
+ }
63
+ return foundDeepRed;
64
+ }
65
+
28
66
bool _findBGRA10Color (
29
67
Uint8List bytes, int width, int height, List <double > color) {
30
68
final ByteData byteData = ByteData .sublistView (bytes);
@@ -75,6 +113,8 @@ bool _findColor(List<Object?> result, List<double> color) {
75
113
return _findBGR10Color ((result[3 ] as Uint8List ? )! , width, height, color);
76
114
} else if (format == 'MTLPixelFormatBGRA10_XR' ) {
77
115
return _findBGRA10Color ((result[3 ] as Uint8List ? )! , width, height, color);
116
+ } else if (format == 'MTLPixelFormatRGBA16Float' ) {
117
+ return _findRGBAF16Color ((result[3 ] as Uint8List ? )! , width, height, color);
78
118
} else {
79
119
fail ('Unsupported pixel format: $format ' );
80
120
}
0 commit comments