• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

IOS memcpy纹理到双数组

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:36:47 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

您好,我使用以下函数将 sampleBuffer 绑定(bind)到 opengl 纹理,效果很好。

void *imageData;  

- (void)captureOutputAVCaptureOutput *)captureOutput didOutputSampleBufferCMSampleBufferRef)sampleBuffer fromConnectionAVCaptureConnection *)connection 
{ 
         UIImage * image = [self generateUIImageFromSampleBuffer:sampleBuffer];

         if(imageData == NULL){
             width = CGImageGetWidth(image.CGImage);
             height = CGImageGetHeight(image.CGImage);
             CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
             imageData = malloc( height * width * 4 );
             imgcontext = CGBitmapContextCreate(imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
             CGColorSpaceRelease( colorSpace );
         }
         CGContextClearRect( imgcontext, CGRectMake( 0, 0, width, height ) );
         CGContextTranslateCTM( imgcontext, 0, height - height );   
         CGContextDrawImage( imgcontext, CGRectMake( 0, 0, width, height ), image.CGImage );

         glBindTexture( GL_TEXTURE_2D, m_textureId );
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
  }

我的问题是,我试图将 imageData 复制到双数组中,这样我就可以遍历所有像素以在不同的过程中进行一些处理。这是我正在使用的代码,它没有给我预期的结果。

   double data[width * height * 4];

    memcpy(data, imageData, width * height * 4);

       for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x += 4)
            {
                double r = data[ y * width + x];
                double g = data[ y * width + x + 1];
                double b = data[ y * width + x + 2];
                double a = data[ y * width + x + 3];
            }
        }

此代码在纹理绑定(bind)后直接调用,但 r、g、b 永远不会更改值。任何想法我可能做错了什么

干杯



Best Answer-推荐答案


看起来您正在分配 imageData 以将像素值存储为 32 位(4 字节)整数,但随后尝试将每个条目视为 double ( 8 个字节)。那是行不通的。

以下帖子可能会有所帮助:

How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?

关于IOS memcpy纹理到双数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7943105/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap