| View previous topic :: View next topic |
| Author |
Message |
deniska
Joined: 17 Oct 2005 Posts: 71 Location: New York
|
Posted: Mon Oct 17, 2005 2:27 pm Post subject: color blending in 5551 mode??? |
|
|
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... |
|
| Back to top |
|
 |
LuMo
Joined: 21 Aug 2005 Posts: 410 Location: Austria
|
Posted: Tue Oct 18, 2005 3:28 am Post subject: |
|
|
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 |
|
| Back to top |
|
 |
Saotome

Joined: 03 Apr 2004 Posts: 182
|
Posted: Tue Oct 18, 2005 4:27 am Post subject: |
|
|
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 |
|
| Back to top |
|
 |
LuMo
Joined: 21 Aug 2005 Posts: 410 Location: Austria
|
Posted: Tue Oct 18, 2005 4:34 am Post subject: |
|
|
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 |
|
| Back to top |
|
 |
deniska
Joined: 17 Oct 2005 Posts: 71 Location: New York
|
Posted: Tue Oct 18, 2005 5:56 am Post subject: |
|
|
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... |
|
| Back to top |
|
 |
LuMo
Joined: 21 Aug 2005 Posts: 410 Location: Austria
|
Posted: Tue Oct 18, 2005 6:23 am Post subject: |
|
|
for one value:
| Code: |
C3_r = (C1_r + C2_r) >> 1; // eg R value
|
_________________ "Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com |
|
| Back to top |
|
 |
marlov74
Joined: 04 Feb 2005 Posts: 10
|
Posted: Tue Oct 18, 2005 4:34 pm Post subject: |
|
|
C1 = C1 and 1111011110111100
C2 = C2 and 1111011110111100
C1 = C1 >> 1
C2 = C2 >> 1
C3 = C1 + C2 |
|
| Back to top |
|
 |
ChaosKnight

Joined: 14 Apr 2005 Posts: 142 Location: Florida, USA
|
Posted: Tue Oct 18, 2005 9:30 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
deniska
Joined: 17 Oct 2005 Posts: 71 Location: New York
|
Posted: Wed Oct 19, 2005 1:45 am Post subject: |
|
|
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));
} |
|
| Back to top |
|
 |
marlov74
Joined: 04 Feb 2005 Posts: 10
|
Posted: Wed Oct 19, 2005 8:10 am Post subject: |
|
|
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 |
|
| Back to top |
|
 |
memon
Joined: 03 Oct 2005 Posts: 63
|
Posted: Wed Oct 19, 2005 7:09 pm Post subject: |
|
|
This one should work too and you don't lost the one precious bit of precicion either:
| Code: | 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] |
|
| Back to top |
|
 |
marlov74
Joined: 04 Feb 2005 Posts: 10
|
Posted: Thu Oct 20, 2005 2:40 am Post subject: |
|
|
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? |
|
| Back to top |
|
 |
memon
Joined: 03 Oct 2005 Posts: 63
|
Posted: Thu Oct 20, 2005 4:06 am Post subject: |
|
|
My texture class says:
| Code: | inline u16 pack5551( u8 r, u8 g, u8 b, u8 a )
{
return (r >> 3) | ((g & 0xf8) << 2) | ((b & 0xf8) << 7) | ((a & 0x80) << 8);
} |
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. |
|
| Back to top |
|
 |
|