Ios ColorLUT filter and ioscolorlut Filter

Source: Internet
Author: User

Ios ColorLUT filter and ioscolorlut Filter

Filter Using this square image

Code:

"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 * 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 + 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 );

Cgcolorspacerelstrap (colorSpace );

If (context = NULL)

{

Free (bitmap );

}

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

CGContextRelease (context );

Return bitmap;

}

Used image generated during call

CIFilter * colorCube = [CIFilter colorCubeWithColorLUTImageNamed: @ "" dimension: 64];

CIImage * inputImage = [[CIImage alloc] initWithImage: [UIImage imageNamed: @ "selected photo"];

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

CIImage * outputImage = [colorCube outputImage];

 

CIContext * context = [CIContext contextwitexceptions: [NSDictionary dictionaryWithObject :( _ bridge id) (CGColorSpaceCreateDeviceRGB () forKey: kCIContextWorkingColorSpace];

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

Generated filter image newImage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.