-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtpHash.m
More file actions
85 lines (66 loc) · 2.33 KB
/
tpHash.m
File metadata and controls
85 lines (66 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#import "TpHash.h"
@implementation TpHash
+(uint64_t)ptHash:(UIImage*)image {
image = [self scaleImage:image toSize:CGSizeMake(8,8)];
uint8_t* imgArray = [self convertToGreyscale64Array:image];
int sum = 0;
for (int i = 0; i < 64; i++) {
sum += imgArray[i];
}
uint8_t avg = sum/64;
uint64_t ret = 0;
for (int i = 0; i < 64; i++) {
if(imgArray[i]>=avg) {
ret++;
}
ret <<= 1;
}
return ret;
}
+(int)hamdist:(uint64_t)x with:(uint64_t) y
{
unsigned dist = 0, val = x ^ y;
while(val)
{
++dist;
val &= val - 1;
}
return dist;
}
+(UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
+(uint8_t *) convertToGreyscale64Array:(UIImage *)i {
int kRed = 1;
int kGreen = 2;
int kBlue = 4;
int colors = kGreen;
int m_width = i.size.width;
int m_height = i.size.height;
uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rgbImage, m_width, m_height, 8, m_width * 4, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetShouldAntialias(context, NO);
CGContextDrawImage(context, CGRectMake(0, 0, m_width, m_height), [i CGImage]);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height);
for(int y = 0; y < m_height; y++) {
for(int x = 0; x < m_width; x++) {
uint32_t rgbPixel=rgbImage[y*m_width+x];
uint32_t sum=0,count=0;
if (colors & kRed) {sum += (rgbPixel>>24)&255; count++;}
if (colors & kGreen) {sum += (rgbPixel>>16)&255; count++;}
if (colors & kBlue) {sum += (rgbPixel>>8)&255; count++;}
m_imageData[y*m_width+x]=sum/count/4;
}
}
free(rgbImage);
return m_imageData;
}
@end