PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Pitch, Backbuffer

Hi,

After playing a bit with subclassing a CGapiSurface, and I have a few questions.

First, I have an iPaq 3850. The color format of the backbuffer is 565. However, when I check the type of bitmaps loaded with a CGapiSurface, the format is 555. Why? Shouldn't the loaded bitmaps be in the same format as the display?


Second, I'm using these functions to convert to and from 555 format. However, they don't work as they should. Can someone spot any errors?

char C555ToRed(WORD Color)
{
return (Color & 0x7c00)>>10;
}
char C555ToGreen(WORD Color)
{
return (Color & 0x03e0)>>5;
}
char C555ToBlue(WORD Color)
{
return (Color & 0x001f);
}

WORD RGBTo555(char R, char G, char B)
{
return (WORD)( (R<<10) | (G<<5) | B );
}


Third. When locking a surface, the pitch returned is 480 on my device. How should I use this? I tried adding it to get to the next line, but it doesn't seem to work... Natively, my iPaq pitched are -320,1.

Thanks.
The pitch is in bytes. Since the pixels are stored as shorts, you need to halve the value returned if you are adding it on to the pointer. Either that, or you could cast to a BYTE*, add the offset, and cast back, but that would make it pretty easy to get unaligned read errors (i.e. if you try to access a DWORD that isn't located at an address that's a multiple of four, your app will crash. The ARM, MIPS, and SH3 aren't as forgiving as the x86 is). In addition, if you are accessing the pixels linearly, the pitch is going to be the same as the width times the size of a single pixel. That means you don't have to worry about the pitch, just use the width as an offset. GAPIDraw/GAPITools return an internal surface rather than the actual display. The only depend on the screen orientation your program requests, not anything else.

Basically, use
Code:

screenptr += pitch>>1;

instead of
Code:

screenptr += pitch;

to get to the next line. The pitch is based on the pointer pointing to the beginning of the line, not the end.

As for the second and first problems, the only thing I can suggest is that you try using the values without conversion. There may be some subtle feature/bug that causes the the bitmaps to say 555 instead of 565. They are both 16-bit after all.
Hi,

Efortier: Your color conversions are almost correct. The thing that's incorrect is that you have to shift each value 3 bits to the left. There have been numerous color conversions posted in the forums, but I'll post mine for 565 surfaces below anyway:

(For performance reasons the blue AND mask in ColorrefToNative and RGBToNative could be skipped, but I have chosen to keep them just in case someone sends -1 or similar as a color fill value)

Code:
class CGapiDisplay565
{
// Construction
public:
CGapiDisplay565() { };

// Operations
public:
__forceinline
DWORD ColorrefToNative(COLORREF color) const
{
return (((color & 0xF8) << 8) | ((color & 0xFC00) >> 5) | ((color & 0xF80000) >> 19));
}

__forceinline
DWORD RGBToNative(DWORD R, DWORD G, DWORD B) const
{
return (((R & 0xF8) << 8) | ((G & 0xFC) << 3) | ((B & 0xF8) >> 3));
};

__forceinline
COLORREF NativeToColorref(DWORD color) const
{
return ((color & 0xf800) >> 8) | ((color & 0x07e0) << 5) | ((color & 0x001f) << 19);
}

__forceinline
DWORD GetSurfaceRValue(DWORD color) const
{
return ((color & 0xf800) >> 8); // 5-bit
}

__forceinline
DWORD GetSurfaceGValue(DWORD color) const
{
return ((color & 0x07e0) >> 3); // 6-bit
}

__forceinline
DWORD GetSurfaceBValue(DWORD color) const
{
return ((color & 0x001f) << 3); // 5-bit
}

// Implementation
public:
virtual ~CGapiDisplay565() { };
};


Also. GapiDraw should never report that the surface is 555 if your device is 565. Are you sure that you checked the "dwPixelFormat" property of your locked surface struct? It should be set to "GDPIXELFORMAT_565" if the GAPI property gxDP.ffFormat & kfDirect565 equals kfDirect565.

/Johan
Thanks a lot. Your 565 routines helped. I didn't knew I had to shift the individual colors 3 or 2 to the left.

RE: Pixel Format problem.

I subclassed the CGapiSurface class, so that I could access the pixels data without the Lock overhead.

How do I find out what is the pixel format without locking the surface? I can't find any dwPixelFormat member of the CGapiSurface class, and dwType always return 1, so that's not it.

Thanks.