c++图像类的通用像素数组(支持多通道)
我目前正在 C++ 中开发一个简单的图像类,它在内部使用 FreeImage 来加载和解析图像文件。无论如何,我不太确定如何尽可能通用、简单和安全地实现我的像素阵列。例如,该类应该处理不同的通道数。因为现在我只是想根据 freeImage 提供给我的文件类型和像素信息来决定使用哪种像素格式,所以必须在运行时实际加载图像时做出确切的决定。我认为处理这个问题的唯一方法是在图像类定义中拥有一个指向某些抽象“像素”基类的指针,然后在堆上分配正确的格式,如下所示(基本上是工厂模式):
//pseudo code
class Image{
template<class T, class TNumChannels>
struct Pixel
{
T v[TNumChannels];
};
class BasePixelArray{...};
class RGBPixelArray :
public std::vector<Pixel<uint8, 3> >,
public BasePixelArray
{
...
};
private:
BasePixelArray * m_pixelPtr;
public:
void loadImage(const std::string & _bla)
{
//parse and decide what pixelformat to use, i.e
m_pixelPtr = static_cast<BasePixelArray*>(new RGBPixelArray);
//fill array
....
}
};
我不认为这非常理想,因为我想避免从堆分配,但由于它必须在运行时完成,我真的想不出其他任何事情,所以我想知道你们中的任何人是否可能有更好的主意!
谢谢
I am currently working on a simple image class in c++ which uses FreeImage internally to load and parse the image files. Anyways I am not really sure about how to implement my pixel array as generic, simple and safe as possible. For instance the class is supposed to handle different channel counts. Since for now I simply want to decide which pixel format to use based on the file type and pixel information freeImage gives me, the exact decisions have to be made at runtime, when actually loading the image. I think the only way to deal with this is to have a pointer to some abstract "Pixels" baseclass in the image class definition, and then allocate the correct format on the heap like this (basically a factory pattern):
//pseudo code
class Image{
template<class T, class TNumChannels>
struct Pixel
{
T v[TNumChannels];
};
class BasePixelArray{...};
class RGBPixelArray :
public std::vector<Pixel<uint8, 3> >,
public BasePixelArray
{
...
};
private:
BasePixelArray * m_pixelPtr;
public:
void loadImage(const std::string & _bla)
{
//parse and decide what pixelformat to use, i.e
m_pixelPtr = static_cast<BasePixelArray*>(new RGBPixelArray);
//fill array
....
}
};
i don't think this is ideal at all since I would like to avoid allocating from the heap , but since it has to be done at runtime I could not really think of anything else, so I was wondering if anybody of you guys might have a better idea!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您事先知道图像的所有尺寸,并且如果它是一个大图像(实际上,它应该是相对较小的图像),则可以将其编译时分配在堆栈上。如果您不知道这些细节或者您想分配大图像,那么在堆上分配是唯一的解决方案。
要获得更多灵感,您可能需要查看 Boost.GIL 库。
If you know all dimensions of image beforehand and if it's a large image (actually, it should be relatively small image), you can make it compile-time allocation on stack. If you don't know those details or you want to allocate large image, then allocation on heap is the only solution.
For more inspiration, you may want to take a look at the
image
andany_image
models from Boost.GIL library.您没有提到您的平台,但从 Windows 角度来看,这是我的建议:
::VirtualAlloc
You didn't mention your platform, but from the Windows perspective, here's my advice:
_beginthreadex()
.::VirtualAlloc
directly if you alloc/dealloc either very large buffers, or alloc/dealloc very frequently