在 Objective-C 中将 RGB 数据转换为位图可可

发布于 2024-08-07 13:09:36 字数 220 浏览 4 评论 0原文

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

I have a buffer of RGB unsigned char that I would like converted into a bitmap file, does anyone know how?

My RGB float is of the following format

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)] .....

The values for each data unit ranges from 0 to 255. anyone has any ideas how I can go about making this conversion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若沐 2024-08-14 13:09:36

您可以使用 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);

You can use CGBitmapContextCreate to make a bitmap context from your raw data. Then you can create a CGImageRef from the bitmap context and save it. Unfortunately CGBitmapContextCreate is a little picky about the format of the data. It does not support 24-bit RGB data. The loop at the beginning swizzles the rgb data to rgba with an alpha value of zero at the end. You have to include and link with ApplicationServices framework.

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);
灼痛 2024-08-14 13:09:36

借用 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]< /code> 保持房子干净。

Borrowing from nschmidt's code to produce a familiar, if someone red-eyed image:

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

Then, addSubview:iv to get the image into your view and, of course, do the obligatory [releases] to keep a clean house.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文