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

代码:

"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

ios ColorLUT滤镜的更多相关文章

  1. iOS开发 滤镜的使用

    iOS开发之滤镜的使用技巧(CoreImage)   一.滤镜的内容和效果是比较多并且复杂的 ,学习滤镜需要技巧 如下: 两个输出语句解决滤镜的属性选择问题: 1.查询效果分类中包含什么效果按住com ...

  2. iOS GPUImage 滤镜介绍

    这里直接引用官方描述: The GPUImage framework is a BSD-licensed iOS library that lets you apply GPU-accelerated ...

  3. iOS中滤镜处理及相关内存泄漏问题的解决

    最近工作之余在做一个美图秀秀的仿品 做到滤镜这块的时候  自己就参考了网上几位博主(名字忘了记,非常抱歉)的博客,但是发现跟着他们的demo做的滤镜处理,都会有很严重的内存泄漏,于是就自己按照大体的思 ...

  4. [iOS]CIFilter滤镜

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  5. iOS 简单滤镜

    转自:http://blog.csdn.net/lovechris00/article/details/51496458 1.主要是运用 ImageUtil库,把原图通过矩阵色值设置层不同滤镜效果下的 ...

  6. iOS中滤镜种类及相关介绍

  7. Android Animation学习 实现 IOS 滤镜退出动画

    IOS的用户体验做的很好,其中一点很重要的地方就是动画效果. 最近在学习Android的Animation,简单实现了一个IOS相机滤镜退出的动画: 布局文件:activity_animation_d ...

  8. 基于GPUImage的多滤镜rtmp直播推流

    之前做过开源videocore的推流改进:1)加入了美颜滤镜; 2) 加入了librtmp替换原来过于简单的rtmpclient: 后来听朋友说,在videocore上面进行opengl修改,加入新的 ...

  9. iOS滤镜实现之LOMO(美图秀秀经典LOMO)

    LOMO追求鲜艳色彩,随意.自由的态度,是一种经常使用的滤镜,今天介绍一下iOS 中LOMO滤镜的实现 首先它有3张输入图像 1.我们要处理的图像.即我们要应用LOMO滤镜的图像 2 3 在gpuim ...

随机推荐

  1. JS正则表达式收集篇

    1.验证只可输入整数或小数点后两位的数字:/^([1-9]{1}|[1-9]{1}[0-9])+(.[1-9]{1,2})?$/ 2.验证Email: /^([a-zA-Z0-9]+[_|\_|\.] ...

  2. 要将程序集“xxx.dll”标记为系统必备组件,必须对其进行强签名

    最近编译经常偶尔出现标题这个错误,有时重启电脑,就正常了,有时重启也不行,真蛋疼,后来发现把dll预先拷贝到生成目录,也可以避免这个编译错误,但是实在是麻烦,再去Google了半天,终于找到了解决方案 ...

  3. Gengxin讲STL系列——Queue和Stack

    第三篇. 感觉队列和栈是必须的……所以决定加上这两个…… 我发现我已经买域名买上隐了……今天又买了个.top……真是智障…… Queue(队列FIFO)和Statk(栈LIFO). 那么为什么要这两个 ...

  4. (转) Special members

    原地址:http://www.cplusplus.com/doc/tutorial/classes2/   Special members [NOTE: This chapter requires p ...

  5. java开发webservice

    第一部分:相关下载配置 1.开发环境   eclipse-jee-mars-2-win32-x86_64.zip  http://www.eclipse.org/downloads/index-pac ...

  6. 飘逸的python - 多条件排序及itemgetter的应用

    曾经客户端的同事用as写一大堆代码来排序,在得知python排序往往只需要一行,惊讶无比,遂对python产生浓厚的兴趣. 之前在做足球的积分榜的时候需要用到多条件排序,如果积分相同,则按净胜球,再相 ...

  7. SharePoint solution and feature management with PowerShell

    /* Author: Jiangong SUN */ Hello, I want to introduce SharePoint solution and feature management usi ...

  8. HttpURLConnection详解

    HttpURLConnection详解 07. 五 / J2EE / 没有评论   HttpURLConnection类的作用是通过HTTP协议向服务器发送请求,并可以获取服务器发回的数据. Http ...

  9. ubuntu sendmail

    一.安装 ubuntu中sendmail函数可以很方便的发送邮件,ubuntu sendmail先要安装两个包. 必需安装的两个包: 代码  sudo apt-get install sendmail ...

  10. Spyder调试错误-"TypeError: decoding Unicode is not supported"

    这是Spyder 2.7.4版本的一个Bug,升级到最新版本(2.7.9)即可. pip install --upgrade spyder Reference: https://github.com/ ...