//
//  GetSimilarity.m
//  imgsimlartest
//
//  Created by test on 16/3/3.
//  Copyright © 2016年 com.facishare.CoreTest. All rights reserved.
//

#import "GetSimilarity.h"
#define ImgSizeA 10   // 2000张跑了10s
#define ImgSizeB 100  // 2000张图片 跑了9分钟
typedef enum workday
{
    SizeA,
    SizeB,
}GetSimilarityType;


@interface GetSimilarity()
@property (nonatomic,assign) Similarity similarity;
@property (nonatomic,strong) UIImage *imga;
@property (nonatomic,strong) UIImage *imgb;
@end

@implementation GetSimilarity
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.imga = [[UIImage alloc]init];
        self.imgb = [[UIImage alloc]init];
    }
    return self;
}

- (void)setImgWithImgA:(UIImage*)imgA ImgB:(UIImage*)imgB
{
    _imga = imgA;
    _imgb = imgB;
}

- (void)setImgAWidthImg:(UIImage*)img
{
    self.imga = img;
}

- (void)setImgBWidthImg:(UIImage*)img
{
    self.imgb = img;
}

- (Similarity)getSimilarityValue
{
//    self.similarity = MAX([self getSimilarityValueWithType:SizeA], [self getSimilarityValueWithType:SizeB]);
    self.similarity = [self getSimilarityValueWithType:SizeA];
    return self.similarity;
}

+ (Similarity)getSimilarityValueWithImgA:(UIImage *)imga ImgB:(UIImage *)imgb
{
    GetSimilarity * getSimilarity = [[GetSimilarity alloc]init];
    [getSimilarity setImgWithImgA:imga ImgB:imgb];
    return [getSimilarity getSimilarityValue];
}

- (Similarity)getSimilarityValueWithType:(GetSimilarityType)type 
{
    //  type 等于 SizeA吗? 如果等于的话 , 就用ImgSizeA即10个像素点 ,ImgSizeB即100个像素点
    int cursize = (type == SizeA ? ImgSizeA : ImgSizeB);
    
    int ArrSize = cursize * cursize + 1 , a[ArrSize], b[ArrSize], i, j, grey, sum = 0;
    CGSize size = { cursize, cursize};
    // 将图片压缩成 10*10 或 100*100
    UIImage * imga = [self reSizeImage:self.imga toSize:size];
    UIImage * imgb = [self reSizeImage:self.imgb toSize:size];

    // 将各个像素的灰度存入数组 , 最后一个值存储平均值
    a[ArrSize] = 0;
    b[ArrSize] = 0;
    CGPoint point;
    
    //
    for (i = 0 ; i < cursize; i++) {//计算a的灰度
        for (j = 0; j < cursize; j++) {
            point.x = i;
            point.y = j;
            grey = ToGrey([self UIcolorToRGB:[self colorAtPixel:point img:imga]]);
            a[cursize * i + j] = grey;
            a[ArrSize] += grey;
        }
    }
    // 灰度平均值
    a[ArrSize] /= (ArrSize - 1);
    
    // 计算b的灰度
    for (i = 0 ; i < cursize; i++) {
        for (j = 0; j < cursize; j++) {
            point.x = i;
            point.y = j;
            grey = ToGrey([self UIcolorToRGB:[self colorAtPixel:point img:imgb]]);
            b[cursize * i + j] = grey;
            b[ArrSize] += grey;
        }
    }
    // 灰度平均值
    b[ArrSize] /= (ArrSize - 1);
    
    // 灰度分布计算 , 将各像素点跟平均值比较,如果小于平均值 记0 , 大于 记1
    for (i = 0 ; i < ArrSize ; i++)
    {
        a[i] = (a[i] < a[ArrSize] ? 0 : 1);
        b[i] = (b[i] < b[ArrSize] ? 0 : 1);
    }
    ArrSize -= 1;
    
    // 将 A B两个存储的 0/1 进行比较 , 相等记1 , 不等记0
    for (i = 0 ; i < ArrSize ; i++)
    {
        sum += (a[i] == b[i] ? 1 : 0);
    }
    
    // 总值 / 数组个数 = 计算出平均值
    return sum * 1.0 / ArrSize;
}

- (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
{
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
    [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return reSizeImage;
}

unsigned int ToGrey(unsigned int rgb)
{
    unsigned int blue   = (rgb & 0x000000FF) >> 0;
    unsigned int green  = (rgb & 0x0000FF00) >> 8;
    unsigned int red    = (rgb & 0x00FF0000) >> 16;
    return ( red*38 +  green * 75 +  blue * 15 )>>7;
}

- (unsigned int)UIcolorToRGB:(UIColor*)color
{
    unsigned int RGB,R,G,B;
    RGB = R = G = B = 0x00000000;
    CGFloat r,g,b,a;
    [color getRed:&r green:&g blue:&b alpha:&a];
    R = r * 256 ;
    G = g * 256 ;
    B = b * 256 ;
    RGB = (R << 16) | (G << 8) | B ;
    return RGB;
}

- (UIColor *)colorAtPixel:(CGPoint)point img:(UIImage*)img {
    // 如果点不在范围内 , 则退出
    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, img.size.width, img.size.height), point)) { return nil; }
    
    NSInteger   pointX  = trunc(point.x); // 将数据转成 double类型
    NSInteger   pointY  = trunc(point.y);
    
    CGImageRef  cgImage = img.CGImage; // image 转 CGImage
    
    NSUInteger  width   = img.size.width;
    NSUInteger  height  = img.size.height;
    
    int bytesPerPixel   = 4;
    int bytesPerRow     = bytesPerPixel * 1;
    NSUInteger bitsPerComponent = 8;
    unsigned char pixelData[4] = { 0, 0, 0, 0 };
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixelData, 1, 1, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    
    CGColorSpaceRelease(colorSpace);
    CGContextSetBlendMode(context, kCGBlendModeCopy);
    
    // Draw the pixel we are interested in onto the bitmap context
    CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
    CGContextRelease(context);
    // Convert color values [0..255] to floats [0.0..1.0]
  
    CGFloat red   = (CGFloat)pixelData[0] / 255.0f;
    CGFloat green = (CGFloat)pixelData[1] / 255.0f;
    CGFloat blue  = (CGFloat)pixelData[2] / 255.0f;
    CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}




- (CGFloat)getSimilarityGroupWithImg:(UIImage*)img
{
    //  type 等于 SizeA吗? 如果等于的话 , 就用ImgSizeA即10个像素点 ,ImgSizeB即100个像素点
    int cursize = 10;
    
    int ArrSize = cursize * cursize + 1 , a[ArrSize], i, j, grey, sum = 0;
    CGSize size = { cursize, cursize};
    // 将图片压缩成 10*10 或 100*100
    UIImage * img_100 = [self reSizeImage:img toSize:size];

    // 将各个像素的灰度存入数组 , 最后一个值存储平均值
    for(int n = 0 ; n < ArrSize ; n++){
        a[n] = 0;
    }
    
    CGPoint point;
    int greySunm = 0;
    //
    for (i = 0 ; i < cursize; i++) {//计算a的灰度
        for (j = 0; j < cursize; j++) {
            point.x = i;
            point.y = j;
            grey = ToGrey([self UIcolorToRGB:[self colorAtPixel:point img:img_100]]);
            a[cursize * i + j] = grey;
            a[ArrSize] += grey;
            greySunm += grey;
        }
    }
    // 灰度平均值
    a[ArrSize] /= (ArrSize - 1);
    
    
//    CGFloat pinjun_1 = a[ArrSize]/100;
//    CGFloat pinjun_2 = greySunm/100;

    // 灰度分布计算 , 将各像素点跟平均值比较,如果小于平均值 记0 , 大于 记1
    for (i = 0 ; i < ArrSize ; i++)
    {
        a[i] = (a[i] < a[ArrSize] ? 0 : 1);
    }
    ArrSize -= 1;
    
    // 将 A B两个存储的 0/1 进行比较 , 相等记1 , 不等记0
    for (i = 0 ; i < ArrSize ; i++)
    {
        sum += a[i];
    }
    
    // 总值 / 数组个数 = 计算出平均值
    return sum * 1.0 / ArrSize;
}



@end
