# Help, how can i conver a 8 bits(256 colors) pixel to a 16 bits pixel?

## Recommended Posts

I want to convert a 8 bit pixel to a 16 bit pixel. Using directdraw a 8 bit pixel is a Byte and A 16 bit pixel is a Word variable. Basically i want to load an image pcx or bitmap of 8 bits to a direcdraw surface of 16 bits. I look up the palette table and get the pixel. for example 255,0,255 and i use the RGB api (pixel:=RGB(255,0,255)) function to convert it to a 16 bit pixel. But it doest not work. the coloer does not match! Any help.

##### Share on other sites
You'll probably have to post your conversion code to get meaningful answers. Its a simple operation, and from what you've described, you have the process correct, which leads me to believe that you've got some detail wrong in the code.

Post your code and don't forget to use the [*source*] your code here [[*/source*] tags (remove the asterisks).

##### Share on other sites

this is my function for convert a 8 bit color to a 16 color;

i think i am doing something wrong,. i am not convertting well the color "I THINK"

function  RGB16BIT(r, g, b : byte): word  ;beginRESULT:=(r shl 11) or (g shl 5) or (b); end; procedure PutPixel (ddsd : TDDSURFACEDESC2;x : integer; y : integer; r : byte; g : byte; b : byte);varpDDSColor : PWORD;pixel : word;beginpixel := RGB16BIT(r,g,b);pDDSColor := PWORD(LongWord(ddsd.lpSurface) + (y)*LongWord(ddsd.lPitch));Inc(pDDSColor,x);move(pixel,pDDSColor^,sizeof(pixel));end;var p : TRGBColor;ddsd : TDDSURFACEDESC2;beginpBackBuffer.Lock( nil, ddsd,DDLOCK_WAIT or DDLOCK_SURFACEMEMORYPTR, 0 );r:=255;g:=0;b:=255;x:=0;y:=0;PutPixel(ddsd,x,y,r,g,b)   ;pBackBuffer.UnLock(nil);pFrontBuffer.Flip ( nil, DDFLIP_WAIT );

##### Share on other sites
there are more than 1 "16 bit" color modes, so you probably need to either use something provided by directdraw / Direct3D itself, or else find out which system is being used byt he particular buffer.

I know there are at least 2 16 bit modes (where there are 5,5,6 bits per color, but the color order and which one has 6 bits is different) and also 1 or 2 5 bpp modes (15 bit modes) but you still need to know which color goes where in the particluar surface / buffer you are dealing with.

Of course, I haven't used DirectX in a few years so I can't remember what helper functions it might have to do the job.

Also, there is always the possiblity of using the SLOW!!! Win32 API for this as well, once you make a compatible DC for doing so.

##### Share on other sites
I Think I see your problem: You've forgotten to mask off the upper bits of of your Red, Green and Blue color components.

I'm not familiar with this programming language (Pascal? Delphi?) but I think it should go something like this:
function  RGB16BIT(r, g, b : byte): word  ;beginRESULT:=((r and 0xF1) shl 11) or ((g and 0xFC) shl 3) or ((b shr 3); end;

I'm not sure how type promotion works in this language, so make sure you're good there as well.

If it doesn't work, just remember that you have to mask to the most signifigant 5.6.5 bits and *then* shift to the appropriate position and *then* OR them together.

[Edited by - ravyne2001 on June 7, 2007 4:47:40 PM]

##### Share on other sites
Also, not sure how it works in your language, but you need to ensure that the data is shifted after being converted to something larger than char. For example, if the red was 0xF8, and you shifted that up by 8, you'd get 0x00 because it shifted off the edge. You want to promote it to 0x00F8, and shift that to 0xF800.

##### Share on other sites
delphi is like c++. i understand well c++.So i can translate it to delphi. if someone wants to help me in c++ or delphi.
my video card use 565 format .
i am still getting a wierd pixel color.

function  RGB16BIT(r, g, b : byte): word  ;beginRESULT:=((r and $F1) shl 11) or ((g and$FC) shl 3) or ((b shr 3));end;

##### Share on other sites
"r and $F8" not$F1. And shift by 8, not 11.

##### Share on other sites
yeah, that oughta fix it. F1 was a typo and I missed correcting that shl because I was about to be late to a meeting :)

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627662
• Total Posts
2978508

• 10
• 10
• 12
• 22
• 13