IOS-simple filter effect achieved through CPU

Source: Internet
Author: User

IOS-simple filter effect achieved through CPU

In iOS, the principle of using the CPU to implement the filter effect is very simple. Convert the image into pixel data, calculate the corresponding filter effect for each pixel, and then obtain the filtered image again.
The code for the CPU filter effect is as follows:

Header file
// CPUImageFilterUtil. h # import
  
   
# Import
   
    
# Import
    
     
# Import
     
      
// LOMOstatic const float colormatrix_lomo [] = {1.7f, 0.1f, 0.1f, 0,-73.1f, 0, 1.7f, 0.1f, 0,-73.1f, 0, 0.1f, 1.6f, 0, -73.1f, 0, 0, 0, 1.0f, 0}; // black and white static const float colormatrix_heibai [] = {0.8f, 1.6f, 0.2f, 0,-163.9f, 0.8f, 1.6f, 0.2f, 0,-163.9f, 0.8f, 1.6f, 0.2f, 0,-163.9f, 0, 0, 0, 1.0f, 0 }; // retro static const float colormatrix_huajiu [] = {0.2f, 0.5f, 0.1f, 0, 40.8f, 0.2f, 0.5f, 0.1f, 0, 40.8f, 0.2f, 0.5f, 0.1f, 0, 40.8f, 0, 0, 0, 1, 0}; // Gothic static const float colormatrix_gete [] = {1.9f,-0.3f,-0.2f, 0,-87.0f,-0.2f, 1.7f,-0.1f, 0,-87.0f,-0.1f,-0.6f, 2.0f, 0,-87.0f, 0, 0, 0, 1.0f, 0 }; // sharpen static const float colormatrix_ruise [] = {4.8f,-1.0f,-0.1f, 0,-388.4f,-0.5f, 4.4f,-0.1f, 0,-388.4f,-0.5f, -1.0f, 5.2f, 0,-388.4f, 0, 0, 0, 1.0f, 0}; // quiet static const float colormatrix_danya [] = {0.6f, 0.3f, 0.1f, 0, 73.3f, 0.2f, 0.7f, 0.1f, 0, 73.3f, 0.2f, 0.3f, 0.4f, 0, 73.3f, 0, 0, 0, 1.0f, 0 }; // wine red static const float regular [] = {1.2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.8f, 0.0f, 0, 0, 0, 1.0f, 0}; // qingning static const float colormatrix_qingning [] = {0.9f, 0, 0, 0, 0, 0, 1.1f, 0, 0, 0, 0, 0, 0.9f, 0, 0, 0, 0, 0, 1.0f, 0}; // romantic static const float colormatrix_langman [] = {0.9f, 0, 0, 0, 63.0f, 0, 0.9f, 0, 0, 63.0f, 0, 0, 0.9f, 0, 63.0f, 0, 0, 0, 1.0f, 0 }; // halo static const float colormatrix_guangyun [] = {0.9f, 0, 0, 0, 64.9f, 0, 0.9f, 0, 0, 64.9f, 0, 0, 0.9f, 0, 0, 64.9f, 0, 0, 0, 1.0f, 0}; // blues static const float colormatrix_landiao [] = {2.1f,-1.4f, 0.6f, 0.0f,-31.0f,-0.3f, 2.0f,-0.3f, 0.0f,-31.0f,-1.1f,-0.2f, 2.6f, 0.0f,-31.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; // dream static const float tables [] = {0.8f, 0.3f, 0.1f, 0.0f, Hangzhou, 0.1f, 0.9f, 0.0f, 0.0f, 46.5f, 0.1f, 0.3f, 0.7f, 0.0f, 46.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}; // static const float night [] = {1.0f, 0.0f, 0.0f, 0.0f,-66.6f, 0.0f, 1.1f, 0.0f, 0.0f,-66.6f, 0.0f, 0.0f, 1.0f, 0.0f,-66.6f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; static const float colormatrixs [] [20] = {0.8f, 1.6f, 0.2f, 0,-163.9f, 0.8f, 1.6f, 0.2f, 0,-163.9f, 0.8f, 1.6f, 0.2f, 0,-163.9f, 0, 0, 0, 1.0f, 0}, {0.2f, 0.5f, 0.1f, 0, 40.8f, 0.2f, 0.5f, 0.1f, 0, 40.8f, 0.2f, 0.5f, 0.1f, 0, 40.8f, 0, 0, 0, 1, 0}, {1.9f,-0.3f,-0.2f, 0,-87.0f,-0.2f, 1.7f, -0.1f, 0,-87.0f,-0.1f,-0.6f, 2.0f, 0,-87.0f, 0, 0, 0, 1.0f, 0}, {4.8f,-1.0f, -0.1f, 0,-388.4f,-0.5f, 4.4f,-0.1f, 0,-388.4f,-0.5f,-1.0f, 5.2f, 0,-388.4f, 0, 0, 0, 1.0f, 0 },{ 0.6f, 0.3f, 0.1f, 0, 73.3f, 0.2f, 0.7f, 0.1f, 0, 73.3f, 0.2f, 0.3f, 0.4f, 0, 73.3f, 0, 0, 0, 0, 1.0f, 0 },{ 1.2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.9f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.8f, 0.0f, 0, 0, 0, 1.0f, 0}, {0.9f, 0, 0, 0, 0, 0, 1.1f, 0, 0, 0, 0, 0, 0.9f, 0, 0, 0, 0, 0, 0, 1.0f, 0}, {0.9f, 0, 0, 0, 63.0f, 0, 0.9f, 0, 0, 63.0f, 0, 0, 0.9f, 0, 63.0f, 0, 0, 0, 1.0f, 0}, {0.9f, 0, 0, 0, 64.9f, 0, 0.9f, 0, 0, 64.9f, 0, 0, 0.9f, 0, 64.9f, 0, 0, 0, 1.0f, 0}, {2.1f,-1.4f, 0.6f, 0.0f,-31.0f,-0.3f, 2.0f,-0.3f, 0.0f, -31.0f,-1.1f,-0.2f, 2.6f, 0.0f,-31.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}, {0.8f, 0.3f, 0.1f, 0.0f, Hangzhou, 0.1f, 0.9f, 0.0f, 0.0f, Hangzhou, 0.1f, 0.3f, 0.7f, 0.0f, Hangzhou, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f, 0.0f, -66.6f, 0.0f, 1.1f, 0.0f, 0.0f,-66.6f, 0.0f, 0.0f, 1.0f, 0.0f,-66.6f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }}; @ interface CPUImageFilterUtil: NSObject + (UIImage *) imageWithImage :( UIImage *) inImage withColorMatrix :( const float *) f; @ end
     
    
   
  
Implementation File
// CPUImageFilterUtil. m # import CPUImageFilterUtil. h @ implementation CPUImageFilterUtil // return a bitmap context static CGContextRef CreateRGBABitmapContext (CGImageRef inImage) {CGContextRef context = NULL; CGColorSpaceRef colorSpace; // a pointer to the memory space, the size of the memory space is equal to the number of bytes occupied by the image using the RGB channel. Void * bitmapData; int bitmapByteCount; int bitmapBytesPerRow; // obtain the number of horizontal pixels size_t pixelsWide = CGImageGetWidth (inImage); size_t pixelsHigh = CGImageGetHeight (inImage ); // portrait // The number of bytes occupied by pixels in each row. The ARGB of each pixel occupies 8 bits (0-255) space in each of the four channels. bitmapBytesPerRow = (int) (pixelsWide * 4); // calculate the number of bytes occupied by the entire graph. bitmapByteCount = (int) (bitmapBytesPerRow * pixelsHigh ); // create a device-dependent RGB channel colorSpace = CGColorSpaceCreateDeviceRGB (); // allocate Enough memory space to accommodate the number of bytes of the image bitmapData = malloc (bitmapByteCount); // create the graphic context of CoreGraphic, this context describes some of the rendering parameters of the image that bitmaData points to in the memory space. The context = CGBitmapContextCreate (bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast ); // The pointer created by using the Create or Alloc method in Core Foundation. You need to use the CFRelease () function to release cgcolorspacerelspace (colorSpace); return context;} // return a pointer, this pointer points to an array. Every four elements in the array are on the image. The RGBA value (0-255) of a pixel. It uses an unsigned char because it has a correct value range of 0-255static unsigned char * RequestImagePixelData (UIImage * inImage) {CGImageRef img = [inImage CGImage]; CGSize size = [inImage size]; // use the above function to create the context CGContextRef cgctx = CreateRGBABitmapContext (img ); CGRect rect = {0, 0}, {size. width, size. height }}; // draw the target image to the specified context, which is actually bitmapData in the context. CGContextDrawImage (cgctx, rect, img); unsigned char * data = CGBitmapContextGetData (cgctx); // release the context CGContextRelease (cgctx) created by the above function; return data ;} static void changeRGBA (int * red, int * green, int * blue, int * alpha, const float * f) // modify the RGB value {int redV = * red; int greenV = * green; int blueV = * blue; int alphaV = * alpha; * red = f [0] * redV + f [1] * greenV + f [2] * blueV + f [3] * alphaV + f [4]; * green = f [0 + 5] * redV + f [1 + 5] * greenV + f [2 + 5] * blueV + f [3 + 5] * alphaV + f [4 + 5]; * blue = f [0 + 5*2] * redV + f [1 + 5*2] * greenV + f [2 + 5*2] * blueV + f [3 + 5*2] * alphaV + f [4 + 5*2]; * alpha = f [0 + 5*3] * redV + f [1 + 5*3] * greenV + f [2 + 5*3] * blueV + f [3 + 5*3] * alphaV + f [4 + 5*3]; if (* red> 255) {* red = 255;} if (* red <0) {* red = 0;} if (* green> 255) {* green = 255;} if (* green <0) {* green = 0;} if (* blue> 255) {* blue = 255 ;} if (* blue <0) {* blue = 0;} if (* alpha> 255) {* alpha = 255;} if (* alpha <0) {* alpha = 0 ;}+ (UIImage *) imageWithImage :( UIImage *) inImage withColorMatrix :( const float *) f {unsigned char * imgPixel = RequestImagePixelData (inImage ); CGImageRef inImageRef = [inImage CGImage]; GLuint w = (GLuint) CGImageGetWidth (inImageRef); GLuint h = (GLuint) CGImageGetHeight (inImageRef); int wOff = 0; int pixOff = 0; // The double-layer loop iterates each pixel for (GLuint y = 0; y 
Usage
UIImage *originImage = [UIImage imageNamed:@testImage];const float *colorMatrix = colormatrix_lomo;UIImage *filteredImage = [CPUImageFilterUtil imageWithImage:originImage withColorMatrix:colorMatrix];

The effect is as follows:

Effect 1 Effect 2

 

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.