[IOS uses Cgimage to get and modify picture pixels]

Source: Internet
Author: User

#defineMASK8 (x) ((x) & 0xFF)#defineR (x) (MASK8 (x))#defineG (x) (MASK8 (x >> 8))#defineB (x) (MASK8 (x >> 16))#defineA (x) (MASK8 (x >> 24))#defineRgbamake (R, G, B, a) (MASK8 (R) | MASK8 (g) << 8 | MASK8 (b) << 16 | Mask8 (a) <<)-(UIImage *) Processusingpixels: (uiimage*) Inputimage {//1. Get The raw pixels of the image//defines the maximum 32-bit shaping pointer *inputpixelsUInt32 *Inputpixels; //convert picture to Cgimageref, get parameter: Length width height, number of bytes per pixel (4), number of bits per rCgimageref Inputcgimage =[Inputimage Cgimage]; Nsuinteger Inputwidth=cgimagegetwidth (inputcgimage); Nsuinteger Inputheight=cgimagegetheight (inputcgimage); Cgcolorspaceref ColorSpace=Cgcolorspacecreatedevicergb (); Nsuinteger Bytesperpixel=4; Nsuinteger bitspercomponent=8; //number of bytes per lineNsuinteger Inputbytesperrow = Bytesperpixel *Inputwidth; //Open memory Area, point to first pixel addressInputpixels = (UInt32 *) calloc (inputheight * inputwidth,sizeof(UInt32)); //creates a pixel layer based on the pointer, the preceding argumentCgcontextref context =cgbitmapcontextcreate (Inputpixels, Inputwidth, Inputheight, Bitsperco Mponent, Inputbytesperrow, ColorSpace, Kcgimagealphapremultipliedlast |kcgbitmapbyteorder32big); //drawing images at the interface based on current pixelsCgcontextdrawimage (Context, CGRectMake (0,0, Inputwidth, inputheight), inputcgimage); //next is the key!!! Pixel processing--------------------------------------------------------     for(Nsuinteger j =0; J < Inputheight; J + +) {         for(Nsuinteger i =0; i < inputwidth; i++) {UInt32* Currentpixel = Inputpixels + (J * inputwidth) +i; UInt32 Color= *Currentpixel;                    UInt32 Br,thisr,thisg,thisb,thisa; //here the direct shift gets the value of the RBGA, and the output writes very well! Thisr=R (color); THISG=G (color); THISB=B (color); Thisa=A (color); //NSLog (@ "%d,%d,%d,%d", Thisr,thisg,thisb,thisa);Br=b (color)-R (color); *currentpixel =rgbamake (BR, BR, BR, A (color)); }    }    //Create a new diagram//4. Create a new UIImageCgimageref Newcgimage =cgbitmapcontextcreateimage (context); UIImage* Processedimage =[UIImage Imagewithcgimage:newcgimage]; //Release//5. cleanup!cgcolorspacerelease (colorspace);    Cgcontextrelease (context);        Free (inputpixels); returnprocessedimage;}

[IOS uses Cgimage to get and modify picture pixels]

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.