Fast method to copy memory with translation - ARGB to BGR

前端 未结 11 1911
野趣味
野趣味 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:28

    The obvious, using pshufb.

    #include 
    #include 
    #include 
    
    // needs:
    // orig is 16-byte aligned
    // imagesize is a multiple of 4
    // dest has 4 trailing scratch bytes
    void convert(uint8_t *orig, size_t imagesize, uint8_t *dest) {
        assert((uintptr_t)orig % 16 == 0);
        assert(imagesize % 4 == 0);
        __m128i mask = _mm_set_epi8(-128, -128, -128, -128, 13, 14, 15, 9, 10, 11, 5, 6, 7, 1, 2, 3);
        uint8_t *end = orig + imagesize * 4;
        for (; orig != end; orig += 16, dest += 12) {
            _mm_storeu_si128((__m128i *)dest, _mm_shuffle_epi8(_mm_load_si128((__m128i *)orig), mask));
        }
    }
    

提交回复
热议问题