c# - 如何将RGB相机图像转换为SharpDX的ARGB格式?

标签 c# rgb argb

<分区>

我有一张从相机拍摄的 RGB 图像(RGB 4:4:4 色彩空间,每像素 24 位)。我用 Gorgon 2D库(基于 SharpDX 构建)将此图像显示为纹理,因此我必须将其转换为 ARGB。我使用此代码(不是我的代码)将 RGB 相机图像转换为 RGBA。

[StructLayout(LayoutKind.Sequential)]
public struct RGBA
{
            public byte r;
            public byte g;
            public byte b;
            public byte a;
}

[StructLayout(LayoutKind.Sequential)]
public struct RGB
{
            public byte r;
            public byte g;
            public byte b;
}

unsafe void internalCvt(long pixelCount, byte* rgbP, byte* rgbaP)
            {
                for (long i = 0, offsetRgb = 0; i < pixelCount; i += 4, offsetRgb += 12)
                {
                    uint c1 = *(uint*)(rgbP + offsetRgb);
                    uint c2 = *(uint*)(rgbP + offsetRgb + 3);
                    uint c3 = *(uint*)(rgbP + offsetRgb + 6);
                    uint c4 = *(uint*)(rgbP + offsetRgb + 9);
                    ((uint*)rgbaP)[i] = c1 | 0xff000000;
                    ((uint*)rgbaP)[i + 1] = c2 | 0xff000000;
                    ((uint*)rgbaP)[i + 2] = c3 | 0xff000000;
                    ((uint*)rgbaP)[i + 3] = c4 | 0xff000000;
                }
            }

public unsafe void RGB2RGBA(int pixelCount, byte[] rgbData, byte[] rgbaData)
            {
                if ((pixelCount & 3) != 0) throw new ArgumentException();
                fixed (byte* rgbP = &rgbData[0], rgbaP = &rgbaData[0])
                {
                    internalCvt(pixelCount, rgbP, rgbaP);
                }
            }

然后像这样将 RGB 转换为 RGBA:

byte[] rgb = new byte[800*600*3]; //Stored data
byte[] rgba = new byte[800 * 600 * 4];
RGB2RGBA(800*600, rgb, rgba)

并且我使用 rgba 作为 Gorgon 纹理的数据:

unsafe
{
     fixed(void* rgbaPtr = rgba)
     {
          var buff = new GorgonNativeBuffer<byte>(rgbaPtr, 800*600*4);                      
          GorgonImageBuffer imb = new GorgonImageBuffer(buff, 800, 600, BufferFormat.R8G8B8A8_UNorm);

          //Set Texture data  GorgonTexture2D                                
          Texture.SetData(imb, new SharpDX.Rectangle(0, 0, 800, 600), 0, 0, CopyMode.NoOverwrite);
         }
}

但是纹理图像的颜色不像相机图像的颜色。

enter image description here

所以我想我必须将相机图像转换为 ARGB(不是 RGBA),以便它可以显示 Gorgon 纹理,但我不知道如何使用上面的代码来完成。你们能给我一些提示吗?

谢谢!

下面是我在上面的代码中使用的 Gorgon 库类型的链接

GorgonTexture2D GorgonNativeBuffer GorgonImageBuffer

最佳答案

目标的字节顺序在内存中是R G B A。源的字节顺序在内存中是 B G R。所以除了每3个字节扩展成4个字节,把FF放到新的A channel 外,R和B channel 还需要调换位置。例如,

unsafe void internalCvt(long pixelCount, byte* rgbP, uint* rgbaP)
{
    for (long i = 0, offsetRgb = 0; i < pixelCount; i += 4, offsetRgb += 12)
    {
        uint c1 = *(uint*)(rgbP + offsetRgb);
        uint c2 = *(uint*)(rgbP + offsetRgb + 3);
        uint c3 = *(uint*)(rgbP + offsetRgb + 6);
        uint c4 = *(uint*)(rgbP + offsetRgb + 9);
        // swap R and B
        c1 = (c1 << 16) | (c1 & 0xFF00) | ((c1 >> 16) & 0xFF);
        c2 = (c2 << 16) | (c2 & 0xFF00) | ((c2 >> 16) & 0xFF);
        c3 = (c3 << 16) | (c3 & 0xFF00) | ((c3 >> 16) & 0xFF);
        c4 = (c4 << 16) | (c4 & 0xFF00) | ((c4 >> 16) & 0xFF);
        // set alpha to FF
        rgbaP[i] = c1 | 0xff000000;
        rgbaP[i + 1] = c2 | 0xff000000;
        rgbaP[i + 2] = c3 | 0xff000000;
        rgbaP[i + 3] = c4 | 0xff000000;
    }
}

或者更简单的版本,每次迭代处理一个像素并且不使用 unsafe

void internalCvt(long pixelCount, byte[] bgr, byte[] rgba)
{
    long byteCount = pixelCount * 4;
    for (long i = 0, offsetBgr = 0; i < byteCount; i += 4, offsetRgb += 3)
    {
        // R
        rgba[i] = bgr[offsetBgr + 2];
        // G
        rgba[i + 1] = bgr[offsetBgr + 1];
        // B
        rgba[i + 2] = bgr[offsetBgr];
        // A
        rgba[i + 3] = 0xFF;
    }
}

关于c# - 如何将RGB相机图像转换为SharpDX的ARGB格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57621082/

相关文章:

c# - 读取 Dictionary<string, object> 中的值

c# - 转到 VS 2010 中的定义

opencv - 使用 gstreamer 将 YUVj420p 像素格式转换为 RGB888

android - 使用 Libyuv 将 YUV_420 _888 转换为 ARGB

javascript - 如何仅使用一个提交按钮将文件发送到 MVC 操作

c# - UWP AcrylicBrush 向后兼容最低版本 14393(周年更新)

java - 查找 ImageView 错误中像素的颜色

python - 将 RGB 颜色元组转换为十六进制字符串

java - 在 Java 中处理 alpha 混合

android - 在 Android 上处理使用 ImageReader 获取的图像