#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]