Problems with RGB

Started by
17 comments, last by ProgHawk 24 years, 1 month ago
I''m trying to get a 2d antialiased line in 16 bit color mode coded by hand, but i have a problem. In order to do this, I need to be able to read a pixel, split it up into different rgb values, and then calculate the new rgb. Here''s what i have so far. // Macro for rgb #define RGB16BIT(r,g,b) ((b%32)+((g%32)<<6)+((r%32)<<12)) // Assume DDraw is set up and there is a blue pixel on (0,0) USHORT pixel = video_buffer[0+(0*ddsd.lPitch<<1)] Now ushort has the information of (0,0) on the video buffer. But how can I assign the rgb''s to seperate integers? And after that, can I add the old red and the new red, and so on with all of the other colors, and then divide by 2 to get my new color?? I AM SERIOUSLY LOST!!!
Advertisement
First, just think about how the colors are stored in the USHORT:

0RRR RRGG GGGB BBBB

So if you want one of the colors, just mask out the parts you do not need:

// just green bits:
// 0000 0011 1110 0000 in binary
// 0 3 E 0 in hex
// if you AND the pixel with this number, all the bits
// except the green ones are set to zero.
just_green_bits = (pixel & 0x03e0);

then move the bits down so they are in the right place

// we want
// 0000 0000 000G GGGG from
// 0000 00GG GGG0 0000
// which means moving the bits to the right 5 times
green_color_of_pixel = (just_green_bits >> 5);

You should be able to get the rest.

Actually, I have two questions - are you sure that #define is right? The bits seems to be moved over to the left too much. Also, does anyone know what the % is supposed to do there?
---Grandpa Simpson - "I never thought I could shoot down a German plane, but last year I proved myself wrong!"
You are assuming the 16bit format is 5-5-5, which is an error. Actually, the bit configuration can be virtually anything (like 14-1-1), even though that''s not very likely to happen (the reason for 6 green gits is that they human eye is more sensetive for green shades)

You better write some functions to determine the bit configuration, and create some routins for handling all of them in your drawing code.
-------------------------------------------------------------LGPL 3D engine - http://www.sourceforge.net/projects/realityengine
Spejic, first of all, the bitwise operators are right. Second, the % is a MOD that tells the macro to only accept the first five bits of each of the rgb. Also, thanks for the help.
Lord Chaos, the way that I have my macro written, the computer uses 5-5-5, and it works perfectly, so his coding would work, but I haven''t tried it yet.
Yes, I assumes it worked on your computer, but if you ever plan on using your code on ANY other computer, you can''t be sure which pixel format it will use, right? So for your own sake, write code that will work on all graphic cards out there. Just a little tip

/ LC
-------------------------------------------------------------LGPL 3D engine - http://www.sourceforge.net/projects/realityengine
And how might I do that Lord Chaos?
There are a bunch of tutorials on the 5-5-5 problem. There are are a few on this site. You definately need to consider this because about half of the video cards use 5-5-5 and half use 5-6-5.

*** Triality ***
*** Triality ***
All you really need to do is to query your primary surface after you get it, and in the DDSURFACEDESC there is a pixel format descriptor. Look in that, and there are three values: dwRBitMask, dwGBitMask, dwBBitMask. These tell you what the mask is that you need to use to get your color bits. It sometimes can be annoying, but it''s best to do that rather than assume that you''ve got a 5-5-5 video card or a 5-6-5 bit alignment. Good luck!

Pythius
-Code God of Team 4 Semester 5 -DigiPen
(Actually, I''m just the whiney b**ch, but they''re about the same)
"The object of war is not to die for your country, but to make the other bastard die for his"
To find out the bit depth, take a look at the dwGBitMask item in the DDPIXELFORMAT structure which you get back from the IDirectDrawSurface::GetPixelFormat method. If the dwGBitMask is 0x03e0 (5 bits) then you have 5-5-5 (which is what you are using now) and if it is 0x07e0 (6 bits) then you have 5-6-5.


---
Grandpa Simpson - "I never thought I could shoot down a German plane, but last year I proved myself wrong!"
---Grandpa Simpson - "I never thought I could shoot down a German plane, but last year I proved myself wrong!"
Ok, the code from the tutorials didn''t work, so I tried to make my own, but there is a problem with it. The while will not end.

void Get_RGB16(void)
{
DWORD dwRBit, dwGBit, dwBBit;
dwRBit = rgb16.dwRBitMask = ddsd_main.ddpfPixelFormat.dwRBitMask;
dwGBit = rgb16.dwGBitMask = ddsd_main.ddpfPixelFormat.dwGBitMask;
dwBBit = rgb16.dwBBitMask = ddsd_main.ddpfPixelFormat.dwBBitMask;

while(!(dwRBit & 0x00000001))
dwRBit = (dwRBit>>1);
while(!(dwGBit & 0x00000001))
dwGBit = (dwGBit>>1);
while(!(dwBBit & 0x00000001))
dwBBit = (dwBBit>>1);

int RBit = dwRBit;
int GBit = dwGBit;
int BBit = dwBBit;

int step_bits = 1, cur_bits = 0;

for(int index=0; index < RBit; index++)
{
cur_bits = cur_bits + step_bits;
step_bits = (step_bits<<1);
}
rgb16.Position.rgbRed = cur_bits;
step_bits = 1;

for(index=0; index < GBit; index++)
{
cur_bits = cur_bits + step_bits;
step_bits = (step_bits<<1);
}
rgb16.Position.rgbGreen = cur_bits;
step_bits = 1;

for(index=0; index < BBit; index++)
{
cur_bits = cur_bits + step_bits;
step_bits = (step_bits<<1);
}
rgb16.Position.rgbBlue = cur_bits;
step_bits = 1;
}

This topic is closed to new replies.

Advertisement