Original Post
🔒 Locked
24-bit variables
Advertisement
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."
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."
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
{ 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
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:
|
sigh.
It doesn''t need to get any more complicated, folks.
|
It doesn''t need to get any more complicated, folks.
quote:
Original post by Serge K
sizeof(Foo) == 4
Turn off byte alignment.
[Resist Windows XP''s Invasive Production Activation Technology!]
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.
Advertisement