specify to have dynamic size of a value

closed account (S8hvC542)
For example, I'm creating a class of say pixel. This pixel could be part of a grey scale image that has 256 shades from black to white so it could be represented by an unsigned char. But what if I wanted a 16 bit representation?

How would I specify this in the class definition or is this even possible?
This is generally done by allocating a large buffer for the bitmap and casting [pointers to the buffer] to the appropriate type.
For example:
1
2
3
4
5
6
7
8
9
10
struct grayscale{
    unsigned char v;
};

struct rgba{
    unsigned char r,g,b,a;
};

unsigned char *buffer=/*...*/;
grayscale *gsp=(grayscale *)buffer;

Sizes of types are static.
closed account (S8hvC542)
Okay, I can see how that works, but I don't really see how I can code a class to support this. This is what is not possible, yes?
If what you want is to have a generic Pixel type... I suppose it can be done, but it doesn't make a lot of sense, if you ask me. You wouldn't have a bitmap where some pixels are one bit-depth and other pixels are another.
Manipulating individual pixels is also terribly inefficient.
closed account (S8hvC542)
I'm in a digital image processing graduate course. We can't use any image libraries. So say when I'm overlaying one image into another, I'm writing 3 unsigned chars (RGB) into the pixel location of background image receiving the overlay. With a pixel type, I could just assign the overlaid pixel to the background location.

Just trying to make the code more succinct.

Thanks.
Topic archived. No new replies allowed.