加速像素格式转换 - BGR打包到RGB平面

时间:2014-12-18 07:55:48

标签: c# arrays image-processing intrinsics pixelformat

从SDK中我获得了包含像素格式BGR的图像,即BGRBGRBGR。对于其他应用程序,我需要将此格式转换为RGB平面RRRGGGBBB。我不想仅为此任务使用额外的库,因此我必须使用自己的代码在格式之间进行转换。

我使用的是C#.NET 4.5 32位,数据是字节数组,大小相同。

现在我正在迭代数组源并将BGR值分配给目标数组中的适当位置,但这需要太长时间(130万像素图像为250毫秒)。代码运行的处理器是Intel Atom E680,可以访问MMX,SSE,SSE2,SSE3,SSSE3。

不幸的是,我不了解内在函数,也无法转换类似问题的代码,例如Fast method to copy memory with translation - ARGB to BGR以满足我的需求。

我目前用于在像素格式之间转换的代码是:

// the array with the BGRBGRBGR pixel data
byte[] source;
// the array with the RRRGGGBBB pixel data
byte[] result;
// the amount of pixels in one channel, width*height
int imageSize;

for (int i = 0; i < source.Length; i += 3)
{
    result[i/3] = source[i + 2]; // R
    result[i/3 + imageSize] = source[i + 1]; // G
    result[i/3 + imageSize * 2] = source[i]; // B
}

我尝试将对源数组的访问分成三个循环,每个循环一个循环,但它并没有真正帮助。所以我愿意接受建议。

for (int i = 0; i < source.Length; i += 3)
{
    result[i/3] = source[i + 2]; // R
}

for (int i = 0; i < source.Length; i += 3)
{
    result[i/3 + imageSize] = source[i + 1]; // G
}

for (int i = 0; i < source.Length; i += 3)
{
    result[i/3 + imageSize * 2] = source[i]; // B
}

编辑:我通过删除这样的除法和乘法将其降低到180ms,但有没有办法让它更快?它仍然非常慢,我猜是因为内存读/写不是很理想。

int targetPosition = 0;
int imageSize2 = imageSize * 2;
for (int i = 0; i < source.Length; i += 3)
{
    result[targetPosition] = source[i + 2]; // R
    targetPosition++;
}

targetPosition = 0;

for (int i = 0; i < source.Length; i += 3)
{
    result[targetPosition + imageSize] = source[i + 1]; // G
    targetPosition++;
}

targetPosition = 0;

for (int i = 0; i < source.Length; i += 3)
{
    result[targetPosition + imageSize2] = source[i]; // B
    targetPosition++;
}

感谢MBo的回答,我能够将时间从180毫秒减少到90毫秒!这是代码:

Converter.cpp:

#include "stdafx.h"

BOOL __stdcall DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved) {
return  TRUE;
}

const unsigned char Mask[] = { 0, 3, 6, 9, 
                           1, 4, 7, 10, 
                           2, 5, 8, 11, 
                           12, 13, 14, 15};

extern "C" __declspec(dllexport) char* __stdcall ConvertPixelFormat(unsigned char* source, unsigned char *target, int imgSize) {

_asm {
    //interleave r1g1b1 r2g2b2 r3g3b3 r4b4g4 r5b5g5 r6... to planar
    //           r1r2r3r4r5..... g1g2g3g4g5... b1b2b3b4b5...
        push edi
        push esi
        mov eax, source      //A address
        mov edx, target      //B address
        mov ecx, imgSize
        movdqu xmm5, Mask    //load shuffling mask
        mov edi, imgSize     //load interleave step
        mov esi, eax
        add esi, edi
        add esi, edi
        add esi, edi
        shr ecx, 2           //divide count by 4
        dec ecx              //exclude last array chunk
        jle Rest

    Cycle:
        movdqu xmm0, [eax]        //load 16 bytes
        pshufb xmm0, xmm5         //shuffle bytes, we are interested in 12 ones
        movd [edx], xmm0          //store 4 bytes of R
        psrldq xmm0, 4            //shift right register, now G is on the end
        movd [edx + edi], xmm0    //store 4 bytes of G to proper place
        psrldq xmm0, 4            //do the same for B
        movd [edx + 2 * edi], xmm0
        add eax, 12               //shift source index to the next portion
        add edx, 4                //shift destination index
        loop Cycle

    Rest:                       //treat the rest of array
        cmp eax, esi
        jae Finish
        mov ecx, [eax]
        mov [edx], cl           //R
        mov [edx + edi], ch     //G
        shr ecx, 16
        mov [edx + 2 * edi], cl //B
        add eax, 3
        add edx, 1
        jmp Rest

    Finish:
        pop esi
        pop edi
    }
}

C#文件:

// Code to define the method
[DllImport("Converter.dll")]
unsafe static extern void ConvertPixelFormat(byte* source, byte* target, int imgSize);

// Code to execute the conversion
unsafe
{
    fixed (byte* sourcePointer = &source[0])
    {
        fixed (byte* resultPointer = &result[0])
        {
            ConvertPixelFormat(sourcePointer, resultPointer, imageSize);
        }
    }
}

4 个答案:

答案 0 :(得分:1)

我在Delphi中实现了这个交错问题并检查了内置的asm。我没有内在函数,所以使用普通汇编程序  pshufb等于_mm_shuffle_epi8SSSE3 intrinsic

在每个循环步骤中,我将16个字节(r1g1b1 r2g2b2 r3g3b3 r4b4g4 r5b5g5 r6)加载到128位XMM寄存器,将它们拖入(r1r2r3r4 g1g2g3g4 b1b2b3b4 xxxx)顺序,并将r,g,b块保存到目标内存(忽略最后4个字节) 。下一步加载(r5b5g5 r6g6b6 r7g7b7 ...),依此类推。

注意为了简化代码,我没有在第一个代码版本中正确处理数组的尾部。由于您可以使用此代码,因此我进行了必要的更正。

第一个版本问题示例:
imgSize = 32
数组大小= 96字节
32/4 = 8个周期
最后一个周期从第84个字节开始,读取16个字节到第99个字节 - 所以我们用完了数组范围!
我刚刚在这里添加了保护字节:GetMem(A, Size * 3 + 15);,但对于实际任务,它可能是不适用的,因此值得对最后一个数组块进行特殊处理。

对于pascal变体,此代码需要967 ms,对于asm变体,此代码需要140 ms才能在i5-4670计算机上转换200个130万像素的处理器(单个线程的处理器本身比Atom 680快6-8倍)。速度约为0.75 GB /秒(pas)和5.4 GB / Sec(asm)

const
  Mask: array[0..15] of Byte = ( 0, 3, 6, 9,
                                 1, 4, 7, 10,
                                 2, 5, 8, 11,
                                 12, 13, 14, 15);
var
  A, B: PByteArray;
  i, N, Size: Integer;
  t1, t2: DWord;
begin
  Size := 1280 * 960 * 200;
  GetMem(A, Size * 3);
  GetMem(B, Size * 3);

  for i := 0 to Size - 1 do begin
    A[3 * i] := 1;
    A[3 * i + 1] := 2;
    A[3 * i + 2] := 3;
  end;

  t1 := GetTickCount;
  for i := 0 to Size - 1 do begin
    B[i] := A[3 * i];
    B[i + Size] := A[3 * i + 1];
    B[i + 2 * Size] := A[3 * i + 2];
  end;
  t2:= GetTickCount;

    //interleave r1g1b1 r2g2b2 r3g3b3 r4b4g4 r5b5g5 r6... to planar
    //r1r2r3r4r5..... g1g2g3g4g5... b1b2b3b4b5...
  asm
    push edi
    push esi
    mov eax, A      //A address
    mov edx, B      //B address
    mov ecx, Size
    movdqu xmm5, Mask   //load shuffling mask
    mov edi, Size       //load interleave step
    mov esi, eax
    add esi, edi
    add esi, edi
    add esi, edi
    shr ecx, 2      //divide count by 4
    dec ecx         //exclude last array chunk
    jle @@Rest

  @@Cycle:
    movdqu xmm0, [eax]   //load 16 bytes
    pshufb xmm0, xmm5    //shuffle bytes, we are interested in 12 ones
    movd [edx], xmm0     //store 4 bytes of R
    psrldq xmm0, 4        //shift right register, now G is on the end
    movd [edx + edi], xmm0   //store 4 bytes of G to proper place
    psrldq xmm0, 4            //do the same for B
    movd [edx + 2 * edi], xmm0
    add eax, 12               //shift source index to the next portion
    add edx, 4                //shift destination index
    loop @@Cycle

   @@Rest:       //treat the rest of array
    cmp eax, esi
    jae @@Finish
    mov ecx, [eax]
    mov [edx], cl   //R
    mov [edx + edi], ch  //G
    shr ecx, 16
    mov [edx + 2 * edi], cl //B
    add eax, 3
    add edx, 1
    jmp @@Rest
  @@Finish:

    pop esi
    pop edi
  end;

  Memo1.Lines.Add(Format('pas %d asm %d', [t2-t1, GetTickCount - t2]));
  FreeMem(A);
  FreeMem(B);

答案 1 :(得分:0)

您可以尝试向后计数,即int i = source.Length - 1; i >=0 ; i -= 3,因此每个for循环只读取一次属性source.Length,而不是每次迭代。

答案 2 :(得分:0)

我遵循了Ivan的建议并想出了这个改进,摆脱了分裂(在C中实现):

    int offset = 0;
    for (int i = 0; i < ARRAYSIZE(source); i += 3) {
        offset++;
        result[offset] = source[i + 2];  // R
        result[offset + imageSize] = source[i + 1];  // G
        result[offset + imageSize * 2] = source[i];  // B
    }

这可以节省我机器上大约40%的运行时间。

答案 3 :(得分:0)

第一步:避免多次阅读来源(参见答案https://stackoverflow.com/a/27542680/949044)。这也很适合CPU缓存,目前还没有使用:你正在读取3个字节中的1个字节,因此抛出了2 / 3ds的缓存行。 所以它可以这样:

int targetPositionR = 0;
int targetPositionG = imageSize;
int targetPositionB = imageSize * 2;
for (int i = 0; i < source.Length; i += 3)
{
    result[targetPositionB] = source[i]; // B
    result[targetPositionG] = source[i + 1]; // G
    result[targetPositionR] = source[i + 2]; // R
    targetPositionB++;
    targetPositionG++;
    targetPositionR++;
}

第二步:一次写入4个字节,而不是1个字节。但是,它需要一个额外的缓冲区和副本:

int[] dwPlanar = new int[imageSize*3/4];
int targetPositionR = 0;
int targetPositionG = imageSize / 4;
int targetPositionB = imageSize * 2 / 4;
for (int i = 0; i < source.Length; i += 12)
{
    int dwB = (source[i  ]) | (source[i+3] << 8) | (source[i+6] << 16) | (source[i+9]  << 24);
    int dwG = (source[i+1]) | (source[i+4] << 8) | (source[i+7] << 16) | (source[i+10] << 24);
    int dwR = (source[i+2]) | (source[i+5] << 8) | (source[i+8] << 16) | (source[i+11] << 24);
    dwPlanar[targetPositionB] = dwB; // B
    dwPlanar[targetPositionG] = dwG; // G
    dwPlanar[targetPositionR] = dwR; // R
    targetPositionB++;
    targetPositionG++;
    targetPositionR++;
}
Buffer.BlockCopy(dwPlanar,0,result,0,imageSize * 3);

我认为它会有所帮助,因为c#会进行更少的数组边界检查,并且通常最好尽可能用更大的块来编写。

(免责声明:我不熟悉c#,我不知道这段代码是否会编译,它只是一种算法)。