[fixed]CColorConverter.cpp convert_R5G6B5toB8G8R8 buggy

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
XMight
Posts: 2
Joined: Tue May 15, 2012 8:26 am
Location: Republic of Moldova
Contact:

[fixed]CColorConverter.cpp convert_R5G6B5toB8G8R8 buggy

Post by XMight »

Hi,
I'm using Irrlicht under Android, and I run into a problem trying to save a screenshot in png. The only one that works is .tga. I debugged(traces) the engine, and I found that the method: CColorConverter::convert_R5G6B5toB8G8R8 is incorrect, it does bad bit operations and pointer arithmetic. So, instead of the code:

Code: Select all

 
void CColorConverter::convert_R5G6B5toR8G8B8(const void* sP, s32 sN, void* dP)
{
        u16* sB = (u16*)sP;
        u8 * dB = (u8 *)dP;
 
        for (s32 x = 0; x < sN; ++x)
        {
                dB[0] = (*sB & 0xf800) << 8;
                dB[1] = (*sB & 0x07e0) << 2;
                dB[2] = (*sB & 0x001f) << 3;
 
                sB += 4;
                dB += 3;
        }
}
 
There ,must be:

Code: Select all

 
void CColorConverter::convert_R5G6B5toB8G8R8(const void* sP, s32 sN, void* dP)
{
        u16* sB = (u16*)sP;
        u8 * dB = (u8 *)dP;
 
        for (s32 x = 0; x < sN; ++x)
        {
                dB[2] = (*sB & 0xf800) >> 8;
                dB[1] = (*sB & 0x07e0) >> 2;
                dB[0] = (*sB & 0x001f) << 3;
 
                sB += 1;
                dB += 3;
        }
}
 
Also, I observed that other methods have erroneous calculations too, but I didn't correct them, because I can't test them, only the one that I tested. The bad code produced lib signal 11.

If there is an svn, I can try to commit the correction, if not, please someone correct this and put there a warning message about the correctness of the other code.

P.S. Hope this will save some headache to others.
The correction that I made is based on the fact that in PNG writer, the dB is u8* with the size: h*w*3, and sB += 1 because one u16 is used to construct 3 u8.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: CColorConverter.cpp convert_R5G6B5toB8G8R8 buggy

Post by hybrid »

Yeah, your code seems almost correct. But I think that for dB[1] we have to move by 3 instead of just 2. I've commited this to 1.7 branch for now, will merge to trunk soon.
XMight
Posts: 2
Joined: Tue May 15, 2012 8:26 am
Location: Republic of Moldova
Contact:

Re: CColorConverter.cpp convert_R5G6B5toB8G8R8 buggy

Post by XMight »

Yes, I think you are right!
Post Reply