When we do some image processing, we often involve RGB565 this kind of image data format. Because of its 2 bytes per pixel, the use of RGB565 without pixel transparency can basically guarantee the color of the image and reduce the size of the image data, saving bandwidth. Therefore, RGB565 will be a common format for the more economical image processing.
Here's a description of how to convert a RGB565 of raw image data into an UIImage object in iOS. See the following code:
-(uiimage*) imageFromRGB565: (void*) RawData Width: (int) Width Height: (int) height{Constsize_t bufferlength = width * Height *2; NSData*data =[NSData Datawithbytes:rawdata length:bufferlength]; Cgcolorspaceref ColorSpace=Cgcolorspacecreatedevicergb (); Cgdataproviderref provider=Cgdataprovidercreatewithcfdata ((cfdataref) data); //Creating cgimage from Cv::matCgimageref imageref = cgimagecreate (width,//widthHeight//Height 5,//bits per component -,//bits per pixelWidth *2,//BytesperrowColorSpace,//ColorSpaceKcgimagealphanoneskipfirst | Kcgbitmapbyteorder16little,//Bitmap InfoProvider//CgdataproviderrefNull//Decode false,//should interpolateKcgrenderingintentdefault//Intent ); //Getting UIImage from CgimageUIImage *finalimage =[UIImage Imagewithcgimage:imageref]; Cgimagerelease (IMAGEREF); Cgdataproviderrelease (provider); Cgcolorspacerelease (ColorSpace); returnfinalimage;}
In iOS, Quartzcore supports 16-bit RGB in a format--alphanoneskipfirst, 5 bits per component, 16 bits per pixel, and a byte order of Byteorder16little. Therefore, the R component is at a low byte, while the B component is in high bytes. Here is an example of the application:
- (void) buttontouched: (ID) sender{unsigned Short*imagebuffer = (unsigned Short*) malloc ( -* -*2); for(introw =0; Row < -; row++) {unsigned Shortcolor =0x001f; if(Row >= -) Color=0xf800; for(intCol =0; Col < -; col++) Imagebuffer[row* -+ Col] =color; } UIImage*image = [Self imagefromrgb565:imagebuffer width: -Height -]; Free (imagebuffer); Uiimageview*imageview = [[Uiimageview alloc] Initwithframe:cgrectmake ((Self.view.frame.size.width-128.0f) *0.5f, (Self.view.frame.size.height-128.0f) *0.5f,128.0f,128.0f)]; Imageview.image=image; [Self.view Addsubview:imageview]; [ImageView release];}
The above code creates an image of a 128x128 RGB565, 64 behaves red, and the next 64 behaves blue.
How iOS converts RAW image data from RGB565 to UIImage objects