Fast method to copy memory with translation - ARGB to BGR

前端 未结 11 1923
野趣味
野趣味 2020-12-07 10:47

Overview

I have an image buffer that I need to convert to another format. The origin image buffer is four channels, 8 bits per channel, Alpha, Red, Green, and Blue

11条回答
  •  孤街浪徒
    2020-12-07 11:17

    This assembly function should do, however I don't know if you would like to keep old data or not, this function overrides it.

    The code is for MinGW GCC with intel assembly flavour, you will have to modify it to suit your compiler/assembler.

    extern "C" {
        int convertARGBtoBGR(uint buffer, uint size);
        __asm(
            ".globl _convertARGBtoBGR\n"
            "_convertARGBtoBGR:\n"
            "  push ebp\n"
            "  mov ebp, esp\n"
            "  sub esp, 4\n"
            "  mov esi, [ebp + 8]\n"
            "  mov edi, esi\n"
            "  mov ecx, [ebp + 12]\n"
            "  cld\n"
            "  convertARGBtoBGR_loop:\n"
            "    lodsd          ; load value from [esi] (4byte) to eax, increment esi by 4\n"
            "    bswap eax ; swap eax ( A R G B ) to ( B G R A )\n"
            "    stosd          ; store 4 bytes to [edi], increment  edi by 4\n"
            "    sub edi, 1; move edi 1 back down, next time we will write over A byte\n"
            "    loop convertARGBtoBGR_loop\n"
            "  leave\n"
            "  ret\n"
        );
    }
    

    You should call it like so:

    convertARGBtoBGR( &buffer, IMAGESIZE );
    

    This function is accessing memory only twice per pixel/packet (1 read, 1 write) comparing to your brute force method that had (at least / assuming it was compiled to register) 3 read and 3 write operations. Method is the same but implementation makes it more efficent.

提交回复
热议问题