【问题标题】:Converting RGB data into a bitmap in Objective-C++ Cocoa在 Objective-C++ Cocoa 中将 RGB 数据转换为位图
【发布时间】:2010-12-07 11:15:26
【问题描述】:

我有一个 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。有人知道如何进行这种转换吗?

【问题讨论】:

  • 你能帮我吗here拜托

标签: objective-c cocoa bitmap rgb objective-c++


【解决方案1】:

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

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);

【讨论】:

  • nschmidt 你是最棒的!!!您在图形计算/处理领域拥有如此丰富的知识。
  • 非常感谢。几天来我一直在寻找这样的代码!
  • 你能帮我吗here拜托
  • @nschmidt 我一直在尝试为 iOS 调整你的答案,这样我就可以将原始亮度值转换回 UIImage,我想我已经接近了,但我不太明白出去。 Here's我的问题,以防你有空。谢谢!
【解决方案2】:

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

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] 以保持房屋整洁。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多