gpt4 book ai didi

c# - 加速像素格式转换 - BGR packed to RGB planar

转载 作者:行者123 更新时间:2023-11-30 17:45:29 29 4
gpt4 key购买 nike

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

我正在使用 C# .NET 4.5 32 位,数据位于具有相同大小的字节数组中。

现在我正在遍历数组源并将 BGR 值分配到目标数组中的适当位置,但这花费的时间太长(1.3 兆像素图像需要 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
}

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

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 毫秒!这是代码:

转换器.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);
}
}
}

最佳答案

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

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

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

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

此代码在 i5-4670 机器上转换 200 个 1.3MP cadr 需要 967 毫秒用于 pascal 变体和 140 毫秒用于 asm 变体(处理器本身的单线程速度比 Atom 680 快 6-8 倍)。速度大约为 0.75 GB/Sec (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);

关于c# - 加速像素格式转换 - BGR packed to RGB planar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27541667/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com