Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / 16 bit High Color and putpixel Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
ßetelgeuse

September 10, 1999, 08:54 PM

Hello all,

Hope someone can help me with this one.

I'm using C/C++ to write a graphics lib for a game I'm doing. I'm using Vesa, DJGPP and have complete access to the video buffer as a char[] array. I set my screen to mode 111 (640x480x16bpp) but then I'm faced with a problem. I need to be able to put a pixel on the screen, using a function like "void putpixel16(int x, int y, unsigned char r, unsigned char g, unsigned char b);". This envolves complex shifts and I don't even know where to start.

Something like "screen[(y*640*2)+x] = ...."

Any help would be appreciated, thank you.

 
Bryan Moodie

September 12, 1999, 10:07 PM


>>I'm using C/C++ to write a graphics lib for a game I'm doing. I'm using Vesa, DJGPP and have complete access to the video buffer as a char[] array. I set my screen to mode 111 (640x480x16bpp) but then I'm faced with a problem. I need to be able to put a pixel on the screen, using a function like "void putpixel16(int x, int y, unsigned char r, unsigned char g, unsigned char b);". This envolves complex shifts and I don't even know where to start.
>>
>>Something like "screen[(y*640*2)+x] = ...."


Okay, for a start using a unsigned char[] to do 16 bit would be problematic.
It's better to use an unsigned short[], i.e.

unsigned short screen16[] = (short*) screen;
screen16[x + 640*y] = RGB16(r,g,b);


I'm not sure if you know the RGB888 (RGB 24 bit) to RGB565 (RGB 16 bit) conversion,
so...

RGB16 is the macro:
#define RGB16(R,G,B) (((R >> 3) << 11) + ((G >> 2) << 5) + (B >> 3))

16 bit RGB is 565. That is 5 bits for red, 6 for green and 5 for blue.
So the macro converts the 8 bit RGB values into 5 or 6 bits, and then
combines them:

Red, from 8 bit to 5 bit (5-8 = -3) so (R >> 3)
Green, from 8 bit to 6 bit (6-8 = -2) so (G >> 2)
Blue, from 8 bit to 5 bit (5-8 = -3) so (B >> 3)

Then they are combined, Blue uses bits 0..4 (no shift)
Green uses bits 5..10 (shift left by 5)
Red uses bits 11..15 (shift left by 11)

Here is one for 15 bit: (RGB555)
#define RGB15(R,G,B) (((R >> 3) << 10) + ((G >> 3) << 5) + (B >> 3))


Anyway, I hope that helps,
-Bryan

 
This thread contains 2 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.