IOS memcpy纹理为双数组

时间:2011-10-30 03:17:39

标签: ios core-graphics memcpy

您好我使用以下函数将sampleBuffer绑定到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 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];
            }
        }

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

干杯

1 个答案:

答案 0 :(得分:3)

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

以下帖子可能有所帮助:

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