29 :(UIImage*)
image rmesThreshold:(
double)rmesThreshold {
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
80 if (*apos == 0 && *bpos == 0 &&
i % 4 == 0) {
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 }
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}
SIN Vec< N, float > sqrt(const Vec< N, float > &x)