Skip to main content
gamedev.net
🔒 Locked

24-bit variables

Started by Floru Nov 21, 2001 at 7:58 AM 8 replies 450+ views
Original Post
Floru
Floru
Hi. How could I use 24-bit variables. I figured something following out: typedef struct { BYTE first; BYTE second; BYTE third; } BYTETRIPLE; Like rgbtriple. The size looks correct: 24bits. But how could I use this? Can I cast it somehow to work like a normal variable? Floru
Profus
Profus
if you''re using C++ you might do something like:

class three_byte {
BYTE x,y,z;
...
}

then overload some operators and voila: your own 24-bit type is born...

or you might use a 32-bit unsigned (to avoid trouble with the 2''s complement) integer and use only 24 of it''s bits, which will probably not waste to much memory because your compiler will align the 24-bit vars in memory as if they were 32-bit anyway...

------------------------------------------------------------
"To a computer, chaos is just another kind of order."
------------------------------------------------------------"To a computer, chaos is just another kind of order."
moose_7L1
moose_7L1
If you have 24-bit graphical contents, i.e. an array of bytes where the red, green and blue values are stored as

{ r,g,b,r,g,b,r,... }

If you necessarily want a struct to describe a pixel you could do this

typedef struct pixel24_s {
unsigned char red;
unsined char green;
unsigned char blue;
} pixel24_t;

Say you have a pointer to some 24bit pixel data like this

unsigned char *pixel_data_24bit;

You could cast this pointer to an pixel24_t-pointer if you like. Like this

pixel24_t *pixel24_pointer = (pixel24_t *)pixel_data_24bit;

Then you can access each pixel''s red, green or blue value very easily like this

pixel24_point[5].red = 255;
pixel24_point[5].green = 128;
pixel24_point[5].blue = 64;

This would be done in some function like this

void PutPixel24(int x,int y,unsigned char r,unsigned char g,unsigned char b,unsigned char *pixel_data_24bit,int width) {

pixel24_t *ptr = (pixel24_t *)pixel_data_24bit;
ptr[x + y * width].red = r;
ptr[x + y * width].green = g;
ptr[x + y * width].blue = b;
}

The above function could be optimized in thousands of ways but I won''t go into that now.

In the end I would really recommend just to use a simple byte-pointer in this case and access the red-, green- and blue-members as

pixel_data_24bit[x + y * width + 0] = red;
pixel_data_24bit[x + y * width + 1] = green;
pixel_data_24bit[x + y * width + 2] = blue;

--

Q: Why do programmers always get Christmas and Halloween mixed up?
A: Because DEC 25 = OCT 31
Q: Why do programmers always get Christmas and Halloween mixed up?A: Because DEC 25 = OCT 31
Serge K
Serge K
quote:
Original post by Floru

How could I use 24-bit variables. I figured something following out:

typedef struct
{
BYTE first;
BYTE second;
BYTE third;
} BYTETRIPLE;

Like rgbtriple. The size looks correct: 24bits. But how could I use this? Can I cast it somehow to work like a normal variable?

Floru



Try something like this:

    
struct RGB888
{
BYTE B;
BYTE G;
BYTE R;

RGB888() {}

RGB888( BYTE r, BYTE g, BYTE b ) { B=b; G=g; R=r; };

RGB888( DWORD dw ) { B = BYTE( dw >> 0 );
G = BYTE( dw >> 8 );
R = BYTE( dw >> 16 ); }

operator DWORD() const { return (DWORD(B) << 0)
| (DWORD(G) << 8)
| (DWORD(R) << 16); }
};
Beer Hunter
Beer Hunter
sigh.

  
struct Foo {
unsigned Bar : 24;
};


It doesn''t need to get any more complicated, folks.
Serge K
Serge K
quote:
Original post by Beer Hunter
sigh.

struct Foo {
unsigned Bar : 24;
};

It doesn''t need to get any more complicated, folks.


How smart!!!

Just a little problem..

sizeof(Foo) == 4
Serge K
Serge K
quote:
Original post by Null and Void
Turn off byte alignment.



Are you familiar with C/C++?

It is impossible to define an arbitrary bitfield.
Bitfields are always fitted in some integer type.
There is no 3 byte integer type in C/C++.
(except some 24bit DSP, but those have no byte addressation..)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.