color blending in 5551 mode???

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

Moderators: cheriff, TyRaNiD

Post Reply
deniska
Posts: 71
Joined: Mon Oct 17, 2005 1:38 pm
Location: New York

color blending in 5551 mode???

Post by deniska »

Could someone please help me out with color blending?

Basically, I need a fast function which would take 2 unsigned shorts representing PSP's 5551 colors and will return the third, representing the resulted blended color:

u16 blend(u16 color1, u16 color2);

for example, if I blend green and white, the func() should return light green...

I am really bad with bit operations, so any help / advise will be greatly appreciated...
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

as your color information is an integer (unsigned 16 bit[u16?])
add the values and shift em one right
thats it
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
User avatar
Saotome
Posts: 182
Joined: Sat Apr 03, 2004 3:45 am

Post by Saotome »

o_O thats a joke isn't it?
you mean add the 16bit values and shift them right one bit?

if you mean to add and shift each 5bit component of the 16bit color value individually, then thats correct.
infj
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

sorry to be not clear
i meant each of the R G B values (A can be ignored)
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
deniska
Posts: 71
Joined: Mon Oct 17, 2005 1:38 pm
Location: New York

Post by deniska »

I understand the actual logic behing it but not too clear on the actual syntax of bit manipulations..
could you please provide a code sample of how it's done?

You help will be greatly appreciated...
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

for one value:

Code: Select all

C3_r = (C1_r + C2_r) >> 1; // eg R value
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
marlov74
Posts: 10
Joined: Fri Feb 04, 2005 9:01 pm

Post by marlov74 »

C1 = C1 and 1111011110111100
C2 = C2 and 1111011110111100
C1 = C1 >> 1
C2 = C2 >> 1
C3 = C1 + C2
User avatar
ChaosKnight
Posts: 142
Joined: Thu Apr 14, 2005 2:08 am
Location: Florida, USA

Post by ChaosKnight »

And if you want even more examples (not PSP specific but good) you can check out the late Seumas McNally's work on software pixel blending here: http://www.ldagames.com/seumas/progblend.html
w00t
deniska
Posts: 71
Joined: Mon Oct 17, 2005 1:38 pm
Location: New York

Post by deniska »

Based on your advise I came up with following func() but it does not work
correctly for all colors.
for example when I blend magenta and white I end up with dome dark blue shade; light blue colors also become darker...
what am I doing wrong here?

PS: Thank you for all your help, guys...


unsigned short blend(unsigned short c1, unsigned short c2){
return (unsigned short)(((c1 & 0xF7BC)>>1) + ((c2 & 0xF7BC)>>1));
}
marlov74
Posts: 10
Joined: Fri Feb 04, 2005 9:01 pm

Post by marlov74 »

It should work, Mine PSP is not working right now soo I can not test.
But I did a test with Notepad and the Calculator.

White
=
31 31 31
=
11111 11111 11111 0
and
11110 11110 11110 0
=
11110 11110 11110 0
>> 1
=
01111 01111 01111 0


Magenta
=
31 0 31
11111 00000 11111 0
and
11110 11110 11110 0
=
11110 00000 11110 0
>> 1
=
01111 00000 01111 0


01111 01111 01111 0
+
01111 00000 01111 0
=
11110 01111 11110 0
=
30 15 30
=
Lighter Magenta
memon
Posts: 63
Joined: Mon Oct 03, 2005 10:51 pm

Post by memon »

This one should work too and you don't lost the one precious bit of precicion either:

Code: Select all

	const unsigned short	maskRB = 0x7c1f;
	const unsigned short	maskG = 0x03e0;
	unsigned short	res = ((((a & maskRB) + (b & maskRB) + 0x401) >> 1) & maskRB) |
		((((a & maskG) + (b & maskG) + 0x20) >> 1) & maskG);
[/code]
marlov74
Posts: 10
Joined: Fri Feb 04, 2005 9:01 pm

Post by marlov74 »

0x7c1f = 111110000011111
0x03e0 = 000001111100000
I Thought 5551 was RRRRRGGGGGBBBBBA, so where did the A Go?

(a & maskRB) + (b & maskRB) is 16 precision ok for that, or are all operations in 32 bit and then masked for 16bit in the end?
memon
Posts: 63
Joined: Mon Oct 03, 2005 10:51 pm

Post by memon »

My texture class says:

Code: Select all

inline u16 pack5551( u8 r, u8 g, u8 b, u8 a )
{
	return &#40;r >> 3&#41; | &#40;&#40;g & 0xf8&#41; << 2&#41; | &#40;&#40;b & 0xf8&#41; << 7&#41; | &#40;&#40;a & 0x80&#41; << 8&#41;;
&#125;
And it works and looks ok, so I assume that I made right assumption :)

I did not check what kind of assembly the code generates, but as the alpha bit is last (and not used in this case), it should work in 16bit. On 565 you'll need to do it in 32bit.
Post Reply