简体   繁体   English

OpenTK从位图获取像素字节数组

[英]OpenTK Getting pixels byte array from a Bitmap

I need to port some OpenGL code to C# OpenTK. 我需要将一些OpenGL代码移植到C#OpenTK。 Here is the chunk where I update a mapped PBO from an array of pixels in C++ : 这是我从C ++中的像素数组更新映射的PBO的块:

    GLubyte* ptr = (GLubyte*)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
    if(ptr)
    {

        memcpy(ptr,imageInfo.Data,IMG_DATA_SIZE);
        glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);  

    }

I need to do the same in OpenTK.My image data comes from an instance of Bitmap. 我需要在OpenTK中做同样的事情。我的图像数据来自Bitmap实例。 I tried the following: 我尝试了以下方法:

        IntPtr ptr = GL.MapBuffer(BufferTarget.PixelUnpackBuffer, BufferAccess.WriteOnly);
        if(ptr != IntPtr.Zero)
       {
           BitmapData data = updateColorMap.LockBits(new System.Drawing.Rectangle(0, 0, updateColorMap.Width, updateColorMap.Height),
                 ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                 Marshal.Copy(data.Scan0, 0, ptr, IMG_DATA_SIZE);
       }

But Marshal.Copy requires the first param to be of byte[] type.I didn't find how to retrieve it from the BitmapData.It returns only IntPtr (data.Scan0) . 但是Marshal.Copy要求第一个参数是byte []类型。我没有找到如何从BitmapData检索它的方法。它仅返回IntPtr(data.Scan0)。

So how can I get the byte array from the Bitmap? 那么如何从位图获取字节数组?

UPDATE: 更新:

In the meantime I got help from the OpenTK forum and they proposed to do this instead: 在此期间,我从OpenTK论坛获得了帮助,他们建议这样做:

           unsafe
            {
                GL.BufferData(BufferTarget.PixelUnpackBuffer, new IntPtr(IMG_DATA_SIZE), IntPtr.Zero, BufferUsageHint.StreamDraw);
                byte* ptr = (byte*)GL.MapBuffer(BufferTarget.PixelUnpackBuffer, BufferAccess.WriteOnly);


                if (ptr != null)
                {
                    BitmapData data = updateDepthMap.LockBits(new System.Drawing.Rectangle(0, 0, updateDepthMap.Width, updateDepthMap.Height),
                    ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                    byte* scan0 = (byte*)data.Scan0.ToPointer();
                    for (int i = 0; i < IMG_DATA_SIZE; ++i)
                    {

                        *ptr = *scan0;
                        ++ptr;
                        ++scan0;
                    }

                    updateDepthMap.UnlockBits(data);
                    GL.UnmapBuffer(BufferTarget.PixelUnpackBuffer);

                }
            }//unsafe

Now,this works,but it is TERRIBLY SLOW ! 现在,这有效,但是速度很 The regular texture update runs 2x faster than this,which is wrong as async PBO transfer should speed up texture uploads.Indeed in my C++ version PBO upload causes almost 2x performance boost. 常规纹理更新的运行速度比此速度快2倍,这是错误的,因为异步PBO传输应加快纹理上传速度。实际上,在我的C ++版本中,PBO上传导致性能提高近2倍。

Ok so the solution is here: Copy data from from IntPtr to IntPtr 好了,解决方案就在这里: 将数据从IntPtr复制到IntPtr

tested on linux. 在linux上测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM