How iOS converts RAW image data from RGB565 to UIImage objects

Source: Internet
Author: User

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

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.