c++图像类的通用像素数组(支持多通道)

发布于 2024-10-12 12:43:46 字数 963 浏览 1 评论 0原文

我目前正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

演出会有结束 2024-10-19 12:43:46

如果您事先知道图像的所有尺寸,并且如果它是一个大图像(实际上,它应该是相对较小的图像),则可以将其编译时分配在堆栈上。如果您不知道这些细节或者您想分配大图像,那么在堆上分配是唯一的解决方案。

要获得更多灵感,您可能需要查看 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 and any_image models from Boost.GIL library.

娜些时光,永不杰束 2024-10-19 12:43:46

您没有提到您的平台,但从 Windows 角度来看,这是我的建议:

  • 堆栈,但请记住默认用户堆栈大小为 1MB,如果您使用 _beginthreadex() 控制线程创建,则可以更改该大小。
  • 堆,可能是最常见的选择
  • 如果您分配/释放非常大的缓冲区,或者非常频繁地分配/释放,请直接使用 ::VirtualAlloc

You didn't mention your platform, but from the Windows perspective, here's my advice:

  • Stack, but keep in mind that the default user stack size is 1MB, which can be changed if you control the thread creation using _beginthreadex().
  • Heap, probably the most common choice
  • Use ::VirtualAlloc directly if you alloc/dealloc either very large buffers, or alloc/dealloc very frequently
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文