Home > OS >  Converting a RGBA image to RGB image
Converting a RGBA image to RGB image

Time:11-04

I try to convert a RGBA image to a RGB image (8-bit unsigned integer per channel). At first I used OpenCV and the following function for that

m_bufferMat.data = (uchar*) (ptr1);
m_bufferMat.convertTo(m_bufferMat, CV_8UC3);

But for the other parts of the application I don't need to use OpenCV, so I tried to convert the image myself so I don't need to link and include the OpenCV library. The fastest method I could imagine is to iterate trough the buffer and copy just the first 3 bytes to another buffer like the following:

for(int i = 0; i < width * height; i  ) {
    *(ptr2  ) = *(ptr1  );
    *(ptr2  ) = *(ptr1  );
    *(ptr2  ) = *(ptr1  );
    ptr1  ;
}

But for that I need to copy which could be not realy fast. The OpenCV function is 1.5 times faster than my own function. Has anybody an idea why? Can I implement a function where I don't need to copy?

CodePudding user response:

There are many optimizations that could be done. Here is a test bench program to try them and a few example optimizations:

#include <iostream>
#include <string>
#include <vector>
#include <intrin.h>
#include <functional>

volatile int width = 1920;
volatile int height = 1080;

unsigned char* src = new unsigned char[width * height * 4];
unsigned char* dst = new unsigned char[width * height * 3];
unsigned char* refDst = new unsigned char[width * height * 3];


void DefaultFunc() {
  auto ptr1 = src;
  auto ptr2 = dst;
  for (int i = 0; i < width * height; i  ) {
    *(ptr2  ) = *(ptr1  );
    *(ptr2  ) = *(ptr1  );
    *(ptr2  ) = *(ptr1  );
    ptr1  ;
  }
}

void NPreCalculatedFunc() {
  auto ptr1 = src;
  auto ptr2 = dst;
  auto n = width * height;
  for (int i = 0; i < n; i  ) {
    *(ptr2  ) = *(ptr1  );
    *(ptr2  ) = *(ptr1  );
    *(ptr2  ) = *(ptr1  );
    ptr1  ;
  }
}

void ReadFullPixelFunc() {
  unsigned int* ptr1 = (unsigned int*)src;
  auto ptr2 = dst;
  auto n = width * height;
  for (int i = 0; i < n; i  ) {
    auto srcPix = *(ptr1  );
    *(ptr2  ) = srcPix & 0xff;
    *(ptr2  ) = (srcPix >> 8) & 0xff;
    *(ptr2  ) = (srcPix >> 16) & 0xff;
  }
}
  

void ReadAndWriteFullPixelFunc() {
  unsigned int* ptr1 = (unsigned int*)src;
  unsigned int* ptr2 = (unsigned int*)dst;
  auto n = width * height / 4; 
  unsigned int writeBuf = 0;
  for (int i = n; i; i--) {   
    // by reading 4 pixels, we get to store 3 unsigned ints
    auto srcPix = *(ptr1  );    
    writeBuf = srcPix & 0x00ffffff;
    srcPix = *(ptr1  );
    writeBuf |= srcPix << 24;
    *(ptr2  ) = writeBuf;
    
    writeBuf = (srcPix >> 8) & 0xffff;
    srcPix = *(ptr1  );
    writeBuf |= (srcPix << 16);
    *(ptr2  ) = writeBuf;

    writeBuf = (srcPix >> 16) & 0xff;
    srcPix = *(ptr1  );
    writeBuf |= (srcPix << 8);
    *(ptr2  ) = writeBuf;
  }
  // todo: if width * height is not divisible by 4, process the last max 3 pixels here with the unoptimized loop
}

void ReadAndWriteFullPixelXmmFunc() {
  unsigned int* ptr1 = (unsigned int*)src;
  unsigned int* ptr2 = (unsigned int*)dst;
  auto n = width * height / 4;
  unsigned int writeBuf = 0;   
  __m128i reorder = _mm_set_epi8(0x80, 0x80, 0x80, 0x80, 14, 13, 12, 10, 9, 8, 6, 5, 4, 2, 1, 0);
  for (int i = n; i; i--) {        
    auto srcPix4_ro = _mm_shuffle_epi8(_mm_loadu_si128((__m128i*)ptr1), reorder);    // read 4 source pixels, remove alpha bytes, pack to low 12 bytes of srcPix4
    ptr1  = 4;
    _mm_storel_epi64((__m128i*)ptr2, srcPix4_ro); // store 2 first pixels
    ptr2  = 2;
    auto shifted = _mm_bsrli_si128(srcPix4_ro, 8);
    _mm_storeu_si32(ptr2, shifted); // store 3rd pixel
    ptr2  = 1;    
  }
  // todo: if width * height is not divisible by 4, process the last max 3 pixels here with the unoptimized loop
}



unsigned long long PrintShortestTime(std::function<void()> f, const char *label, unsigned long long refTime) {
  unsigned long long minTicks = ~0ull;
  memset(dst, 0, width * height * 3);
  for (int i = 0; i < 500; i  ) {
    auto start = __rdtsc();
    f();
    auto end = __rdtsc();
    auto duration = end - start;
    if (duration < minTicks) {
      minTicks = duration;
    }
  }
  if (memcmp(refDst, dst, width * height * 3)) { // test that we got the right answer
    printf("Fail - result does not equal refrence!\n");
  }
  printf("%s : %llu clock cycles - %0.3lf x base implementation time\n", label, minTicks, refTime ? ((double)minTicks/(double)refTime):1.0);
  return minTicks;
}

int main() {
  for (int i = 0; i < width * height * 4; i  ) {
    src[i] = rand() & 0xff;
  }
  DefaultFunc();
  memcpy(refDst, dst, width * height * 3);

  auto refTime = PrintShortestTime(DefaultFunc, "default, unoptimized", 0);  
  PrintShortestTime(NPreCalculatedFunc, "n precalculated", refTime);
  PrintShortestTime(ReadFullPixelFunc, "n precalculated, reading 1 pixel at a time", refTime);    
  PrintShortestTime(ReadAndWriteFullPixelFunc, "reading and writing ints at a time", refTime);
  PrintShortestTime(ReadAndWriteFullPixelXmmFunc, "with xmm intrinsincs", refTime);
}

For me, on visual studio & x64 or x86, the last version takes about 0.4x as long time as the basic version:

default, unoptimized : 7511848 clock cycles - 1.000 x base implementation time
n precalculated : 7383696 clock cycles - 0.983 x base implementation time
n precalculated, reading 1 pixel at a time : 7354644 clock cycles - 0.979 x base implementation time
reading and writing ints at a time : 4613816 clock cycles - 0.614 x base implementation time
with xmm intrinsincs : 3036824 clock cycles - 0.404 x base implementation time

It would probably be possible to optimize further by unrolling the loop, writing memory in larger chunks.

  • Related