2011-10-30 66 views
2

嗨我使用以下函数绑定sampleBuffer到opengl纹理,这很好。IOS memcpy纹理双阵列

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永不改变值。任何想法我可能是做错了

干杯

回答

3

它看起来像你分配imageData存储像素值的32位(4字节)整数,但随后试图将每个条目作为double( 8个字节)。这是行不通的。

以下后可能会有所帮助:

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

+0

而那些“4字节的整数”真的是四个独立的号码,一个是在像素(红,绿,蓝和alpha)每个组件。除了不同的大小,“int”和“double”也是不同格式(整数与浮点)的问题。由于这两个原因,您需要将每个组件转换为单独的“double”值。 –