你的位置:首页 > 操作系统

[操作系统]ios ColorLUT滤镜


通过这种方格图片实现滤镜

代码:

"CIFilter+ColorLUT.h"

"CIFilter+ColorLUT.m"

 

#import "CIFilter+ColorLUT.h"

#import <CoreImage/CoreImage.h>

#import <OpenGLES/EAGL.h>

@implementation CIFilter (ColorLUT)

+ (CIFilter *)colorCubeWithColorLUTImageNamed:(NSString *)imageName dimension:(NSInteger)n

{

    UIImage *image = [UIImage imageNamed:imageName];

    int width = CGImageGetWidth(image.CGImage);

    int height = CGImageGetHeight(image.CGImage);

    int rowNum = height / n;

    int columnNum = width / n;

    if ((width % n != 0) || (height % n != 0) || (rowNum * columnNum != n))

    {

        NSLog(@"Invalid colorLUT");

        return nil;

    }

    unsigned char *bitmap = [self createRGBABitmapFromImage:image.CGImage];  

    if (bitmap == NULL)

    {

        return nil;

    }

    int size = n * n * n * sizeof(float) * 4;

    float *data = malloc(size);

    int bitmapOffest = 0;

    int z = 0;

    for (int row = 0; row <  rowNum; row++)

    {

        for (int y = 0; y < n; y++)

        {

            int tmp = z;

            for (int col = 0; col < columnNum; col++)

            {

                for (int x = 0; x < n; x++) {

                    float r = (unsigned int)bitmap[bitmapOffest];

                    float g = (unsigned int)bitmap[bitmapOffest + 1];

                    float b = (unsigned int)bitmap[bitmapOffest + 2];

                    float a = (unsigned int)bitmap[bitmapOffest + 3];

                    int dataOffset = (z*n*n + y*n + x) * 4;

                    data[dataOffset] = r / 255.0;

                    data[dataOffset + 1] = g / 255.0;

                    data[dataOffset + 2] = b / 255.0;

                    data[dataOffset + 3] = a / 255.0;

                    bitmapOffest += 4;

                }

                z++;

            }

            z = tmp;

        }

        z += columnNum;

    }

    free(bitmap);

    CIFilter *filter = [CIFilter filterWithName:@"CIColorCube"];

    [filter setValue:[NSData dataWithBytesNoCopy:data length:size freeWhenDone:YES] forKey:@"inputCubeData"];

    [filter setValue:[NSNumber numberWithInteger:n] forKey:@"inputCubeDimension"];

    return filter;

}

+ (unsigned char *)createRGBABitmapFromImage:(CGImageRef)image

{

    CGContextRef context = NULL;

    CGColorSpaceRef colorSpace;

    unsigned char *bitmap;

    int bitmapSize;

    int bytesPerRow;

    size_t width = CGImageGetWidth(image);

    size_t height = CGImageGetHeight(image);

    bytesPerRow   = (width * 4);

    bitmapSize     = (bytesPerRow * height);    

    bitmap = malloc( bitmapSize );

    if (bitmap == NULL)

    {

        return NULL;

    }

    colorSpace = CGColorSpaceCreateDeviceRGB();

    if (colorSpace == NULL)

    {

        free(bitmap);

        return NULL;

    }

    context = CGBitmapContextCreate (bitmap,

                                     width,

                                     height,

                                     8,

                                     bytesPerRow,

                                     colorSpace,

                                     kCGImageAlphaPremultipliedLast);

    CGColorSpaceRelease( colorSpace );

    if (context == NULL)

    {

        free (bitmap);

    }

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);

    CGContextRelease(context);

    return bitmap;

}

调用时生成使用图片

CIFilter *colorCube = [CIFilter colorCubeWithColorLUTImageNamed:@"方块图片"dimension:64];

CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"选择的照片"]];

 [colorCube setValue:inputImage forKey:@"inputImage"];

CIImage *outputImage = [colorCube outputImage];

 

CIContext *context = [CIContext contextWithOptions:[NSDictionary dictionaryWithObject:(__bridge id)(CGColorSpaceCreateDeviceRGB()) forKey:kCIContextWorkingColorSpace]];

UIImage *newImage = [UIImage imageWithCGImage:[context createCGImage:outputImage fromRect:outputImage.extent]];

生成后的的滤镜图片newImage