首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Objective-C++ Cocoa中将RGB数据转换为位图

在Objective-C++ Cocoa中将RGB数据转换为位图
EN

Stack Overflow用户
提问于 2009-10-16 18:30:05
回答 2查看 18K关注 0票数 18

我有一个缓冲区的RGB无符号字符,我想转换成一个位图文件,谁知道如何转换?

我的RGB浮点数的格式如下

R (0,0),G(0,0),B(0,0),R (0,1),G(0,1),B(0,1),R (0,2),G(0,2),B(0,2) .....

每个数据单元的值范围从0到255。有没有人知道我该怎么做这个转换?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-10-17 09:25:14

您可以使用CGBitmapContextCreate从原始数据创建位图上下文。然后,您可以从位图上下文创建CGImageRef并保存它。不幸的是,CGBitmapContextCreate对数据的格式有点挑剔。它不支持24位RGB数据。开头的循环将rgb数据转换为rgba,末尾的alpha值为零。你必须包含并链接到ApplicationServices框架。

代码语言:javascript
复制
char* rgba = (char*)malloc(width*height*4);
for(int i=0; i < width*height; ++i) {
    rgba[4*i] = myBuffer[3*i];
    rgba[4*i+1] = myBuffer[3*i+1];
    rgba[4*i+2] = myBuffer[3*i+2];
    rgba[4*i+3] = 0;
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(
    rgba,
    width,
    height,
    8, // bitsPerComponent
    4*width, // bytesPerRow
    colorSpace,
    kCGImageAlphaNoneSkipLast);

CFRelease(colorSpace);

CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR("image.png"), kCFURLPOSIXPathStyle, false);

CFStringRef type = kUTTypePNG; // or kUTTypeBMP if you like
CGImageDestinationRef dest = CGImageDestinationCreateWithURL(url, type, 1, 0);

CGImageDestinationAddImage(dest, cgImage, 0);

CFRelease(cgImage);
CFRelease(bitmapContext);
CGImageDestinationFinalize(dest);
free(rgba);
票数 36
EN

Stack Overflow用户

发布于 2009-11-06 00:36:26

借用nschmidt的代码来生成一个熟悉的、如果有人红眼的图像:

代码语言:javascript
复制
int width = 11;
int height = 8;

Byte r[8][11]={
    {000,000,255,000,000,000,000,000,255,000,000},
    {000,000,000,255,000,000,000,255,000,000,000},  
    {000,000,255,255,255,255,255,255,255,000,000},
    {000,255,255,255,255,255,255,255,255,255,000},
    {255,255,255,255,255,255,255,255,255,255,255},
    {255,000,255,255,255,255,255,255,255,000,255},
    {255,000,255,000,000,000,000,000,255,000,255},
    {000,000,000,255,255,000,255,255,000,000,000}};

Byte g[8][11]={
    {000,000,255,000,000,000,000,000,255,000,000},
    {000,000,000,255,000,000,000,255,000,000,000},  
    {000,000,255,255,255,255,255,255,255,000,000},
    {000,255,255,000,255,255,255,000,255,255,000},
    {255,255,255,255,255,255,255,255,255,255,255},
    {255,000,255,255,255,255,255,255,255,000,255},
    {255,000,255,000,000,000,000,000,255,000,255},
    {000,000,000,255,255,000,255,255,000,000,000}};

Byte b[8][11]={
    {000,000,255,000,000,000,000,000,255,000,000},
    {000,000,000,255,000,000,000,255,000,000,000},  
    {000,000,255,255,255,255,255,255,255,000,000},
    {000,255,255,000,255,255,255,000,255,255,000},
    {255,255,255,255,255,255,255,255,255,255,255},
    {255,000,255,255,255,255,255,255,255,000,255},
    {255,000,255,000,000,000,000,000,255,000,255},
    {000,000,000,255,255,000,255,255,000,000,000}};

char* rgba = (char*)malloc(width*height*4);
int offset=0;
for(int i=0; i < height; ++i) 
{
    for (int j=0; j < width; j++) 
    {
        rgba[4*offset]   = r[i][j];
        rgba[4*offset+1] = g[i][j];
        rgba[4*offset+2] = b[i][j];
        rgba[4*offset+3] = 0;
        offset ++;
    }
}


CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(
                                                   rgba,
                                                   width,
                                                   height,
                                                   8, // bitsPerComponent
                                                   4*width, // bytesPerRow
                                                   colorSpace,
                                                   kCGImageAlphaNoneSkipLast);

CFRelease(colorSpace);

CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);

free(rgba);

UIImage *newUIImage = [UIImage imageWithCGImage:cgImage];

UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 11,8)];

[iv setImage:newUIImage];

然后,使用addSubview:iv命令将图像显示在视图中,当然,还必须执行[releases]命令来保持房间的整洁。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1579631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档