Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Floru

24-bit variables

This topic is 6231 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
have you ever heard of c++ and classes?

Share this post


Link to post
Share on other sites
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."

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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); }
};

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
here''s part of my color class, because of something called Endian-ness you want to reverse the order of the components

  
class Color
{
union
{ //note the union doesn''t need a name

unsigned long value
struct//the struct doesn''t either

{
int blue : 8; //these "ints" only take 8 bits each

int green : 8;
int red : 8;
int alpha : 8;
}
}//don''t recall if I need to end this with ;


//used for converting to the D3DCOLORARGB type

//could have made a conversion operator but I chose not to

unsigned long ul(){ return value; }

//make some constructors and other operators

}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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..)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!