ARGB

来源:互联网 发布:我的小公主 知乎 编辑:程序博客网 时间:2024/05/01 13:48

Bitmap.Config ARGB_4444:每个像素占四位,即A=4,R=4,G=4,B=4,那么一个像素点占4+4+4+4=16位 

Bitmap.Config ARGB_8888:每个像素占四位,即A=8,R=8,G=8,B=8,那么一个像素点占8+8+8+8=32位

Bitmap.Config RGB_565:每个像素占四位,即R=5,G=6,B=5,没有透明度,那么一个像素点占5+6+5=16位

Bitmap.Config ALPHA_8:每个像素占四位,只有透明度,没有颜色。

一般情况下我们都是使用的ARGB_8888,由此可知它是最占内存的,因为一个像素占32位,8位=1字节,所以一个像素占4字节的内存。假设有一张480x800的图片,如果格式为ARGB_8888,那么将会占用1500KB的内存。



我们在做一些图像处理时,往往会涉及到RGB565这种图像数据格式。由于其每个像素仅占2个字节,对于不需要像素透明度的情况下使用RGB565既能基本能保证图像的色彩,又能降低图像数据尺寸,节省带宽。因此,RGB565将会是一种常用的比较经济的图像处理的格式。


下面就来描述一下如何在iOS中将一段RGB565的原始图像数据转为UIImage对象。见以下代码:

- (UIImage*)imageFromRGB565:(void*)rawData width:(int)width height:(int)height
{
    const size_t bufferLength = width * height * 2;
    NSData *data = [NSData dataWithBytes:rawData length:bufferLength];
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
    
    // Creating CGImage from cv::Mat
    CGImageRef imageRef = CGImageCreate(width,          //width
                                        height,         //height
                                        5,              //bits per component
                                        16,             //bits per pixel
                                        width * 2,      //bytesPerRow
                                        colorSpace,     //colorspace
                                        kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder16Little,// bitmap info
                                        provider,               //CGDataProviderRef
                                        NULL,                   //decode
                                        false,                  //should interpolate
                                        kCGRenderingIntentDefault   //intent
                                        );
    
    // Getting UIImage from CGImage
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpace);
    
    return finalImage;
}

iOS中,QuartzCore支持的16位RGB就一种格式——AlphaNoneSkipFirst,每个分量5比特,每个像素16比特,字节序为ByteOrder16Little。因此,R分量位于低字节;而B分量位于高字节。下面举个应用例子:

- (void)buttonTouched:(id)sender
{   
    unsigned short *imageBuffer = (unsigned short*)malloc(128 * 128 * 2);
    for(int row = 0; row < 128; row++)
    {
        unsigned short color = 0x001f;
        if(row >= 64)
            color = 0xf800;
        
        for(int col = 0; col < 128; col++)
            imageBuffer[row * 128 + col] = color;
    }
    
    UIImage *image = [self imageFromRGB565:imageBuffer width:128 height:128];
    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];
}

以上代码创建了一幅128x128的RGB565的图像,上64行为红色;下64行为蓝色。



转自:http://www.cnblogs.com/and_he/archive/2012/12/22/ARGB.html

http://www.cnblogs.com/zenny-chen/p/4063234.html

0 0
原创粉丝点击