OGeek|极客世界-中国程序员成长平台

标题: objective-c - iOS OpenGL 使用 glTexImage2D 的参数来制作 UIImage? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:45
标题: objective-c - iOS OpenGL 使用 glTexImage2D 的参数来制作 UIImage?

我正在处理分配给我的项目的一些现有代码。

我成功调用了 glTexImage2D,如下所示:
glTexImage2D(GL_TEXTURE_2D, 0, texture->format, texture->widthTexture, texture->heightTexture, 0, texture->format, texture->type, texture->data);

我想使用传递给 glTexImage2D 的变量创建一个图像(最好是 CGImageUIImage),但不知道是否有可能。

我需要从 OpenGL View 创建许多连续图像(每秒有很多)并将它们保存以供以后使用。

我应该能够使用我在 glTexImage2D 中使用的变量来创建 CGImageUIImage 吗?

如果我应该可以,我应该怎么做?

如果不是,为什么我不能,你对我每秒多次保存/捕获我的 opengl View 内容的任务有什么建议?

编辑:我已经使用苹果提供的一些技术以及 glReadPixels 等成功捕获了图像。我想要更快的东西,这样我每秒可以获得更多图像。

编辑:查看并添加来自 Thomson 的代码后,生成的图像如下: resulting image from Thomson's code

图像与图像的外观非常相似,但水平方向重复了约 5 次,并且下方有一些随机的黑色空间。

注意:视频(每一帧)数据是通过与 iPhone 的临时网络连接来的。我相信相机正在使用 YCbCr 色彩空间拍摄每一帧

编辑:进一步审查 Thomson 的代码 我已将您的新代码复制到我的项目中,结果得到了不同的图像:

image 2

宽度:320 高度:240

我不确定如何找到纹理-> 数据中的字节数。它是一个空指针。

编辑:格式和类型

texture.type = GL_UNSIGNED_SHORT_5_6_5

texture.format = GL_RGB



Best Answer-推荐答案


嘿,binnyb,这是使用存储在 texture->data 中的数据创建 UIImage 的解决方案。 v01d 肯定是正确的,您不会获得出现在 GL 帧缓冲区中的 UIImage,但它会在数据通过帧缓冲区之前从数据中为您获取图像。 p>

原来你的纹理数据是 16 位格式,红色 5 位,绿色 6 位,蓝色 5 位。在创建 UIImage 之前,我添加了用于将 16 位 RGB 值转换为 32 位 RGBA 值的代码。我期待听到结果如何。

float width    = 512;
float height   = 512;
int   channels = 4;

// create a buffer for our image after converting it from 565 rgb to 8888rgba
u_int8_t* rawData = (u_int8_t*)malloc(width*height*channels);

// unpack the 5,6,5 pixel data into 24 bit RGB
for (int i=0; i<width*height; ++i) 
{
    // append two adjacent bytes in texture->data into a 16 bit int
    u_int16_t pixel16 = (texture->data[i*2] << 8) + texture->data[i*2+1];      
    // mask and shift each pixel into a single 8 bit unsigned, then normalize by 5/6 bit
    // max to 8 bit integer max.  Alpha set to 0.
    rawData[channels*i]   = ((pixel16 & 63488)       >> 11) / 31.0 * 255;
    rawData[channels*i+1] = ((pixel16 & 2016)  << 5  >> 10) / 63.0 * 255;
    rawData[channels*i+2] = ((pixel16 & 31)    << 11 >> 11) / 31.0 * 255;
    rawData[channels*4+3] = 0;
}

// same as before
int                    bitsPerComponent = 8;
int                    bitsPerPixel     = channels*bitsPerComponent;
int                    bytesPerRow      = channels*width;
CGColorSpaceRef        colorSpaceRef    = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo           bitmapInfo       = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent  = kCGRenderingIntentDefault;

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, 
                                                          rawData, 
                                                          channels*width*height,
                                                          NULL);
free( rawData );
CGImageRef        imageRef = CGImageCreate(width,
                                           height,
                                           bitsPerComponent,
                                           bitsPerPixel,
                                           bytesPerRow,
                                           colorSpaceRef,
                                           bitmapInfo,
                                           provider,NULL,NO,renderingIntent);

UIImage *newImage = [UIImage imageWithCGImage:imageRef];

新建图片的代码来自Creating UIImage from raw RGBA data感谢罗希特。我已经用我们原始的 320x240 图像尺寸对此进行了测试,将 24 位 RGB 图像转换为 5、6、5 格式,然后再转换为 32 位。我尚未在 512x512 图像上对其进行测试,但我预计不会有任何问题。

关于objective-c - iOS OpenGL 使用 glTexImage2D 的参数来制作 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4652202/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4