Re: BGR->RGB

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



Hi Georgios,

On Tue, 2002-11-26 at 16:40, Georgios Kapetanakis wrote:
void bgr24_to_rgb24(unsigned char *bgrSrc, unsigned char *rgbDest,
unsigned int bgrSrcSize)
{
        unsigned int i = 0;

        for (i = 0; i < bgrSrcSize; i += 3)
        {
                *(rgbDest++) = *(bgrSrc + (i + 2)); // swap red
                *(rgbDest++) = *(bgrSrc + (i + 1)); // green is not
swapped, just copied over
                *(rgbDest++) = *(bgrSrc + i);       // swap blue
        }
}

Hm, this isn't that bad. ;). You're just doing byte-by-byte. You could
try grabbing 32-bit frames, so you can work with 32-bit variables
(glib's guint32, you can use sys/types.h's __u32 too). If so, here's my
try:

static void
bgr32_to_rgb32 (unsigned char *bgrSrc,
                unsigned char *rgbDest,
                unsigned int   size)
{
  while (size > 0) {
    (guint32)(*bgrDest) = (((guint32)(*bgrSrc))&0xff000000)>>24 |
                          (((guint32)(*bgrSrc))&0x00ff0000)>>8  |
                          (((guint32)(*bgrSrc))&0x0000ff00)<<8  |
                          (((guint32)(*bgrSrc))&0x000000ff)<<24;
    size -=4; bgrSrc += 4; rgbDest += 4;
  }
}

Maybe this gets a bit faster if you use a temporary variable instead of
the reference to ((guint32)(*bgrSrc)) too, so:

static void
bgr32_to_rgb32 (unsigned char *bgrSrc,
                unsigned char *rgbDest,
                unsigned int   size)
{
  guint32 t;
  while (size > 0) {
    t = (guint32)(*bgrSrc);
    (guint32)(*bgrDest) = (t&0xff000000)>>24 | (t&0x00ff0000)>>8  |
                          (t&0x0000ff00)<<8  | (t&0x000000ff)<<24;
    size -=4; bgrSrc += 4; rgbDest += 4;
  }
}

But I'm not fairly sure about that, I guess it won't really matter
(compilers will optimimize these bits out).

This is (supposed to be) faster because you don't do (expensive)
mathematical operations, but you simply do bit-operations (&, >>, << and
|). Of course, assembler would make this faster too, but I'm not a
genius there.

HTH,

-- 
Ronald Bultje <rbultje@xxxxxxxxxxxxxxxxxxx>
Linux Video/Multimedia developer





[Index of Archives]     [Linux DVB]     [Video Disk Recorder]     [Asterisk]     [Photo]     [DCCP]     [Netdev]     [Xorg]     [Util Linux NG]     [Xfree86]     [Free Photo Albums]     [Fedora Users]     [Fedora Women]     [ALSA Users]     [ALSA Devel]     [Linux USB]

Powered by Linux