IOS memcpy 纹理到双数组

发布于 2024-12-13 00:59:41 字数 1844 浏览 1 评论 0原文

您好,我使用以下函数将 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 永远不会更改值。任何想法我可能做错了什么

干杯

Hi Im using the following function to bind a sampleBuffer to a opengl texture, which works well.

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);
  }

My question is that im trying to copy imageData into a double array so i can loop though all the pixels to do some processing in a different process. Here is the code i am using which in not giving me the results i expect.

   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];
            }
        }

This code is being called directly after the texture is bound but r, g, b never change values. Any ideas what i may be doing wrong

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦纸 2024-12-20 00:59:41

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

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

如何从 UIImage (Cocoa Touch) 或 CGImage (Core Graphics) 获取像素数据?

It looks like you are allocating imageData to store pixel values as 32-bit (4-byte) integers, but then attempting to treat each entry as a double (8 bytes). That's not going to work.

The following post may help:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文