From a SDK I get images that have the pixel format BGR packed, i.e. BGRBGRBGR
. For another application, I need to convert this format to RGB planar RRRGGGBBB
. I don't want to use an extra library just for this task, so I have to use my own code to convert between the formats.
I am using C# .NET 4.5 32bit and the data is in byte arrays which have the same size.
Right now I am iterating through the array source and assigning the BGR values to their appropriate places in the target array, but that takes too long (250ms for a 1.3 megapixel image). The processor the code runs at is a Intel Atom E680 and has access to MMX, SSE, SSE2, SSE3, SSSE3.
Unfortunately I don't have knowledge of intrinsics and couldn't convert code for a similar problem like Fast method to copy memory with translation - ARGB to BGR to suit my needs.
The code I am currently using to convert between the pixel formats is:
// 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
}
I tried splitting the access to the source array into three loops, one for each channel, but it didn't really help. So I'm open to suggestions.
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
}
edit: I got it down to 180ms by removing the division and multiplication like this, but is there a way to make it even faster? It still is very slow which I guess is because the memory reads/writes aren't very optimal.
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++;
}
Thanks to MBo's answer, I was able to reduce the time from 180ms to 90ms! Here is the code:
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# file:
// 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);
}
}
}