IOS memcpy 纹理到双数组

标签 ios core-graphics memcpy

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

void *imageData;  

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)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 组中,以便我可以循环所有像素以在不同的进程中进行一些处理。这是我正在使用的代码,它没有给我预期的结果。

   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 永远不会更改值。任何想法我可能做错了什么

干杯

最佳答案

看起来您正在分配 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/

相关文章:

iphone - 在 CGPath 之间着色

c - 错误 :free(): invalid next size (fast)

c++ - 在程序结尾处在0x0037A5C2 project.exe上引发了异常:0xC0000005:访问冲突写入位置0xDDDDDDDD

c++ - memcpy 写入类型为 ‘class uint256’ 的对象

ios - 如何在 OpenGL es 2.0 中模拟累积缓冲区(尾随粒子效应)

objective-c - 如何将 CALayer 转储到 CGImageRef 中?

ios - 在 Swift 中运行 API 查询时如何显示加载屏幕?

ios - Sine CAShapeLayer 作为 CALayer 的 mask

ios - 如何使用 NSPersistentCloudKitContainer 和 SwiftUI 手动获取 CloudKit 数据以更新 UI?

ios - 返回实体中的随机属性(核心数据)