"markww" <ma****@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
>
Jim Langston wrote:
>"markww" <ma****@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegr oups.com...
Hi,
I want to use the vector container class to store pixel data. Currently
I have some memory allocated using c++'s new operator. I allocate the
memory differently based on if the pixel type is unsigned char or
unsigned short like this:
int nPixelType = ?; // unsigned short, or unsigned char?
BYTE *pByte = NULL;
switch (nPixelType) {
case UNSIGNED_SHORT:
pBtye = (BYTE *)new unsigned char[256 * 256];
break;
case UNSIGNED_CHAR:
pByte = (BYTE *)new unsigned char[256 * 256];
break;
}
Then I whenever I was doing image processing tasks, I would create a
temporary pointer to the data and cast it to the correct type:
unsigned short *p = (unsigned short *)pByte;
or
unsigned char *p = (unsigned char *)pByte;
Now it was suggested to me that I stop using raw pointers like this in
favor of vector containers. Can I translate the above into a vector of
BYTE and accomplish the same thing? Would it be possible to get around
having to cast the pointer before accessing it everytime?
vector<BYTEvNew;
vNew.resize(256 * 256);
Thanks
There are a few ways to accomplish this, including a class that keeps
track
of the type of data it is and allocates appropriately, or templates, or
polymorphism (Pixel as base class, PixelShort or PixelChar as derived
classes), etc...
A vector container, itself, isn't a solution other than storing the data.
You still have to decide what type of data to store.
Hi guys,
If I write a class to do it like posted, how could I keep a list of
them - eventually I want to keep the following:
vector<CPixelContainerClassvImageStack;
the first image in the stack might have to be allocated as unsigned
char, the second might have to be allocated as unsigned short.
Well, if you want to store them that way, instead of pointers, then you
don't want to use polymorphism. So basically, just have your
CPixelContainerClass keep track of how it's stored.
enum PixelTypes
{
ShortPixels,
CharPixels
};
class CPixelContainerClass
{
public:
CPixelContainerClass( const PixelTypes pt ): PixelType_( pt ),
ShortPixel_( NULL ), CharPixel_( NULL ) {}
~CPixelContainerClass() { delete[] ShortPixel_; delete[] CharPixel_; }
PixelTypes Type() const { return PixelType_; }
void LoadData( char* Data, const int Size )
{
if ( PixelType_ == ShortPixels )
// Allocate and load data into ShortPixel_
else
// Allocate and load data into CharPixel_
}
private:
PixelTypes PixelType_;
unsigned short* ShortPixel_;
unsigned char* CharPixel_;
}
There are other ways to design this class, and maybe better ways, but you
should get the idea. You are encapsulating the data, so any class that uses
CPixelContainerClass shouldn't have to care if the data is stored in
unsigned short or unsigned char, let the class handle that trivia. If, at
some point, you do need to know outside the class, use .Type() to see how
it's stored.
This is not polymorphism, and I really don't think polymorphism is the way
to go. Personally, I might just use one pointer (unsigned char* as it's
only 1 byte) for all the data and just treat it different if PixelType_ ==
ShortPixels.
The advantage of this is you just have your vector of CPixelContainerClass
and it doesn't matter what type the data is inside of it, they are just
isntances of the same class.
I can think of off the top of my head 3 or 4 other ways to do the same
thing, such as having the pixel data itself stored in another class of
different types, etc...
It's hard to say without knowing your design constraints the best way to do
it.