Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GoldenImage.m
Go to the documentation of this file.
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 "GoldenImage.h"
6
7#import <XCTest/XCTest.h>
8#import <os/log.h>
9#include <sys/sysctl.h>
10
11@interface GoldenImage ()
12
13@end
14
15@implementation GoldenImage
16
17- (instancetype)initWithGoldenNamePrefix:(NSString*)prefix {
18 self = [super init];
19 if (self) {
20 _goldenName = [prefix stringByAppendingString:_platformName()];
21 NSBundle* bundle = [NSBundle bundleForClass:[self class]];
22 NSURL* goldenURL = [bundle URLForResource:_goldenName withExtension:@"png"];
23 NSData* data = [NSData dataWithContentsOfURL:goldenURL];
24 _image = [[UIImage alloc] initWithData:data];
25 }
26 return self;
27}
28
29- (BOOL)compareGoldenToImage:(UIImage*)image rmesThreshold:(double)rmesThreshold {
30 if (!self.image || !image) {
31 os_log_error(OS_LOG_DEFAULT, "GOLDEN DIFF FAILED: image does not exists.");
32 return NO;
33 }
34 CGImageRef imageRefA = [self.image CGImage];
35 CGImageRef imageRefB = [image CGImage];
36
37 NSUInteger widthA = CGImageGetWidth(imageRefA);
38 NSUInteger heightA = CGImageGetHeight(imageRefA);
39 NSUInteger widthB = CGImageGetWidth(imageRefB);
40 NSUInteger heightB = CGImageGetHeight(imageRefB);
41
42 if (widthA != widthB || heightA != heightB) {
43 os_log_error(OS_LOG_DEFAULT, "GOLDEN DIFF FAILED: images sizes do not match.");
44 return NO;
45 }
46 NSUInteger bytesPerPixel = 4;
47 NSUInteger size = widthA * heightA * bytesPerPixel;
48 NSMutableData* rawA = [NSMutableData dataWithLength:size];
49 NSMutableData* rawB = [NSMutableData dataWithLength:size];
50
51 if (!rawA || !rawB) {
52 os_log_error(OS_LOG_DEFAULT, "GOLDEN DIFF FAILED: image data length do not match.");
53 return NO;
54 }
55
56 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
57
58 NSUInteger bytesPerRow = bytesPerPixel * widthA;
59 NSUInteger bitsPerComponent = 8;
60 CGContextRef contextA =
61 CGBitmapContextCreate(rawA.mutableBytes, widthA, heightA, bitsPerComponent, bytesPerRow,
62 colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
63
64 CGContextDrawImage(contextA, CGRectMake(0, 0, widthA, heightA), imageRefA);
65 CGContextRelease(contextA);
66
67 CGContextRef contextB =
68 CGBitmapContextCreate(rawB.mutableBytes, widthA, heightA, bitsPerComponent, bytesPerRow,
69 colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
70 CGColorSpaceRelease(colorSpace);
71
72 CGContextDrawImage(contextB, CGRectMake(0, 0, widthA, heightA), imageRefB);
73 CGContextRelease(contextB);
74
75 const char* apos = rawA.mutableBytes;
76 const char* bpos = rawB.mutableBytes;
77 double sum = 0.0;
78 for (size_t i = 0; i < size; ++i, ++apos, ++bpos) {
79 // Skip transparent pixels.
80 if (*apos == 0 && *bpos == 0 && i % 4 == 0) {
81 i += 3;
82 apos += 3;
83 bpos += 3;
84 } else {
85 double aval = *apos;
86 double bval = *bpos;
87 double diff = aval - bval;
88 sum += diff * diff;
89 }
90 }
91 double rmse = sqrt(sum / size);
92 if (rmse > rmesThreshold) {
93 os_log_error(
94 OS_LOG_DEFAULT,
95 "GOLDEN DIFF FAILED: image diff greater than threshold. Current diff: %@, threshold: %@",
96 @(rmse), @(rmesThreshold));
97 return NO;
98 }
99 return YES;
100}
101
102NS_INLINE NSString* _platformName() {
103 NSString* systemVersion = UIDevice.currentDevice.systemVersion;
104 NSString* simulatorName =
105 [[NSProcessInfo processInfo].environment objectForKey:@"SIMULATOR_DEVICE_NAME"];
106 if (simulatorName) {
107 return [NSString stringWithFormat:@"%@_%@_simulator", simulatorName, systemVersion];
108 }
109
110 size_t size;
111 sysctlbyname("hw.model", NULL, &size, NULL, 0);
112 char* answer = malloc(size);
113 sysctlbyname("hw.model", answer, &size, NULL, 0);
114
115 NSString* results = [NSString stringWithUTF8String:answer];
116 free(answer);
117 return results;
118}
119
120@end
UIImage * image
Definition GoldenImage.h:15
NS_INLINE NSString * _platformName()
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition switches.h:259
SIN Vec< N, float > sqrt(const Vec< N, float > &x)
Definition SkVx.h:706
int BOOL