如何对像素数据进行位剥离? [英] How to do bit striping on pixel data?

查看:77
本文介绍了如何对像素数据进行位剥离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个缓冲区,它们在32位处理器上运行,其中包含R,G,B位数据.

I have 3 buffers containing R, G, B bit data running on a 32-bit processor.

我需要通过以下方式组合这三个字节:

I need to combine the three bytes in the following way:

R[0] = 0b r1r2r3r4r5r6r7r8
G[0] = 0b g1g2g3g4g5g6g7g8
B[0] = 0b b1b2b3b4b5b6b7b8

int32_t Out = 0b r1g1b1r2g2b2r3g3 b3r4g4b4r5g5b5r6 g6b6r7g7b7r8g8b8 xxxxxxxx

其中xxxxxxxx继续到缓冲区中的每个下一个字节.

where xxxxxxxx is continuing on to each of the next bytes in the buffers.

我正在寻找组合它们的最佳方法.我的方法绝对无效.

I am looking for an optimal way to combine them. My approach is definitely not efficient.

这是我的方法

static void rgbcombineline(uint8_t line)
{
    uint32_t i, bit;
    uint8_t bitMask, rByte, gByte, bByte;
    uint32_t ByteExp, rgbByte;
    uint8_t *strPtr = (uint8_t*)&ByteExp;

    for (i = 0; i < (LCDpixelsCol / 8); i++)
    {
        rByte = rDispbuff[line][i];
        gByte = gDispbuff[line][i];
        bByte = bDispbuff[line][i];

        bitMask = 0b00000001;
        ByteExp = 0;
        for(bit = 0; bit < 8; bit++)
        {
            rgbByte = 0;
            rgbByte |= ((rByte & bitMask) >> bit) << 2;
            rgbByte |= ((gByte & bitMask) >> bit) << 1;
            rgbByte |= ((bByte & bitMask) >> bit);
            ByteExp |= (rgbByte << 3*bit);
            bitMask <<= 1;
        }
        TempLinebuff[((i*3)+0) +2] = *(strPtr + 2);
        TempLinebuff[((i*3)+1) +2] = *(strPtr + 1);
        TempLinebuff[((i*3)+2) +2] = *(strPtr + 0);
    }
}

推荐答案

如果可以保留1024个字节,则可以使用一个256个元素的查找表来实现所需的结果:

If you can spare 1024 bytes, you can achieve your desired result with a single 256-element lookup table:

uint32_t lookup[256] = {
    0, 1, 8, 9, 64, 65, ...
    /* map abcdefgh to a00b00c00d00e00f00g00h */
};

uint32_t result = (lookup[rByte] << 2) | (lookup[gByte] << 1) | lookup[bByte];

这仅使用3次查询,2次移位和2次or操作,应该可以接受.

This uses only 3 lookups, 2 shifts and 2 or operations, which should provide an acceptable speedup.

如果有更多空间,您也可以使用三个查找表来消除这种移位(尽管这可能会导致更差的缓存性能,因此请务必进行概要检查!)

If you have more space, you can use three lookup tables to eliminate the shifts too (although this may result in worse cache performance, so always profile to check!)

这篇关于如何对像素数据进行位剥离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆