Question about the color space on the PSP

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Harold
Posts: 7
Joined: Thu Jun 30, 2005 1:33 pm

Question about the color space on the PSP

Post by Harold »

<gush>I finally got to dig into some real development tonight and I'd like to say I'm having the time of my life. </gush>

I found this snippet which convers RGB colors down to the 4byte madness that is the psp's color space:

Code: Select all

unsigned short rgb2col&#40;unsigned char r,unsigned char g,unsigned char b&#41;&#123;return &#40;&#40;&#40;&#40;b>>3&#41; & 0x1F&#41;<<10&#41;+&#40;&#40;&#40;g>>3&#41; & 0x1F&#41;<<5&#41;+&#40;&#40;&#40;r>>3&#41; & 0x1F&#41;<<0&#41;+0x8000&#41;;&#125;
First off, is this as accurate as it gets? and second off is there a reverse operation?

Here is my pathetic contribution to the pg.c library ;)

Code: Select all

unsigned short pgGetPixel&#40;int x, int y&#41;
&#123;
	unsigned short *vptr0 = pgGetVramAddr&#40;0,0&#41;;
	return vptr0&#91;y * LINESIZE + x&#93;;
&#125;
Basically this returns the 4byte short, and thats great, but is there some way to get the constituant RGB values from it? Or maybe a way to set the palette up in some sane fashion?

sorry if this has been brought up before, a search didn't lead me to the answers I was looking for, but if its in here somewhere a link to a similar thread would be appreciated as much as a straight answer.

Thanks in advance,

-Harold
sasq
Posts: 16
Joined: Wed Jun 29, 2005 7:59 am

Post by sasq »

4-byte madness? Its a standard 16bit screenmode.

ARRRRRGGGGGBBBBB

Just read up on your shifting and or:ing operations.

Also you can set the screen to 32bit if you prefer.
Hippo
Posts: 19
Joined: Sat Jun 25, 2005 10:51 am

Post by Hippo »

In response to your contribution, couldn't you just do the following:

Code: Select all

unsigned short pgGetPixel&#40;int x, int y&#41;
&#123;
   return *&#40;unsigned short *&#41;pgGetVramAddr &#40;x, y&#41;;
&#125;
Or something that works, if this doesn't?
Harold
Posts: 7
Joined: Thu Jun 30, 2005 1:33 pm

Post by Harold »

sasq wrote:4-byte madness? Its a standard 16bit screenmode.

ARRRRRGGGGGBBBBB

Just read up on your shifting and or:ing operations.

Also you can set the screen to 32bit if you prefer.
Maybe I have it in a wacky screen mode.

If it were ARRRRRGGGGGBBBBB wouldn't something like:

Code: Select all

c = 0;
for&#40;y = 10; y < 262; y++&#41;
&#123;
	for&#40;x=10;x<470;x++&#41;
	&#123;
		pgPutPixel&#40;x,y,c&#41;;
		c+=1;
	&#125;
&#125;
start with black pixels and have a slow gradient up through blue, then start mixing blue with green and so on?

Because thats not what I see at all. What im seeing is it starting with black and quickly ramping up through red, and then it slowly starting to mix in green, but with lots of repetition. This seems weird to me, or maybe I just have my head way up my ass.

Also, can you tell me how to put it into a 32 bit screen mode? Or point me towards some information that can tell me how?

Thanks,
-Harold
maddogjt1
Posts: 13
Joined: Mon Jun 27, 2005 3:06 pm

Post by maddogjt1 »

the 16bit pixel format is actually ABBBBBGGGGGRRRRR.
f_bohmann
Posts: 16
Joined: Mon May 02, 2005 7:15 pm
Location: hamburg, germany

Post by f_bohmann »

harold, i guess you did not understand what people are telling you. :)

each pixel has 16 bits, of which 1 bit is used for alpha, and 5 bits for each red, green and blue. this "shifting orgy" you posted just scales down the color components from 8 bits per color to 5 bits per color and then shifts those bits into one 16bit value. there is absolutely no magic or orgy involved here. you just have to understand that sometimes the data you use is not 8/16/32 bits, but something in between. in 16 bit mode you have less accuracy per color, but also only half the memory useage/bandwidth than in 32bit mode.

to get back from 16 to 32 bits, you just have to do the logical operation in reverse. (mask out the bits for each color, shift them back up to 8 bits and put them into a 32 bits number).

edit: by the way, (unsigned) short is 2 bytes, not 4. :)
Last edited by f_bohmann on Fri Jul 01, 2005 12:35 am, edited 1 time in total.
Harold
Posts: 7
Joined: Thu Jun 30, 2005 1:33 pm

Post by Harold »

Thanks f_ and maddog, your posts explain better what I'm seeing. Too bad I'm stuck at work for the next 8 hours, I just want to code more.

Peace out, I'm looking forward to being a part of this community.
-Harold
User avatar
Agoln
Posts: 326
Joined: Wed Jun 08, 2005 3:14 am
Location: Fort Wayne, IN

Post by Agoln »

Harold wrote:Too bad I'm stuck at work for the next 8 hours, I just want to code more.
Me too :-/ I hate work, wish i could independent code all day :-D
Harold
Posts: 7
Joined: Thu Jun 30, 2005 1:33 pm

Post by Harold »

Ahhh, a few hours later and I've derived the answer I was looking for. Here are the macros I came up with. Thanks again.

Code: Select all

#define RGB&#40;r,g,b&#41;     &#40;&#40;&#40;&#40;b>>3&#41; & 0x1F&#41;<<10&#41;|&#40;&#40;&#40;g>>3&#41; & 0x1F&#41;<<5&#41;|&#40;&#40;&#40;r>>3&#41; & 0x1F&#41;<<0&#41;|0x8000&#41;
#define R&#40; c &#41;			&#40; &#40;c & 0x1f&#41; << 3 &#41;
#define G&#40; c &#41;			&#40; &#40;&#40;c >> 5&#41; & 0x1f &#41; << 3 &#41;
#define B&#40; c &#41;			&#40; &#40;&#40;c >> 10&#41; & 0x1f&#41; << 3 &#41;
-Harold
AnonymousTipster
Posts: 197
Joined: Fri Jul 01, 2005 2:50 am

Post by AnonymousTipster »

Sorry for the post bump, but I have been having trouble with my RGB macros. I have been using the suggested macro, and was wondering who has gotten it to work, because for me only the red element displays correctly.
I think I must have changed Nem's code somewhat incorrectly, and was wondering if somebody could give me a small code sample of the PG.c .h and any other important changes in other files.
I hope you can find time to reply.
AnonymousTipster
Posts: 197
Joined: Fri Jul 01, 2005 2:50 am

Post by AnonymousTipster »

Eureka! I've fixed it!
I was using the default code of

Code: Select all

*&#40;unsigned char *&#41;vptr= RGB16&#40;x ,x ,x&#41;;
but that was cutting off the Blue element, so when I changed it to

Code: Select all

*&#40;unsigned short *&#41;vptr= RGB16&#40;x ,x ,x&#41;;
IT WORKS!
Hooray! I can now continue with my engine and hopefull make some fun games.
Thanks for everyone that helped though.
Post Reply