Boost::GIL 位8* 到gray8_ptr_t 而不需要reinterpret_cast?

发布于 2024-11-09 14:01:54 字数 1544 浏览 0 评论 0原文

为了尝试遵循 GIL 的设计指南,我使用 bits__ 作为通道数据类型。我经常将外部数据包装到 GIL 图像视图中。然而,即使使用 bits__ 类型作为数据指针,我也必须在创建图像视图之前添加reinterpret_cast。采用以下代码

int width = 3;
int height = 2;

boost::gil::bits8 data8[] = {0, 1, 100, 200, 50, 51};
boost::gil::bits8* pBits8 = data8;
boost::gil::gray8_ptr_t pGray8 = pBits8;

boost::gil::gray8_view_t v = interleaved_view(width, height, pGray8, width * sizeof(boost::gil::bits8));

会导致第 6 行出现错误“error C2440: 'initializing' :无法从 'boost::gil::bits8 *' 转换为 'boost::gil::gray8_ptr_t' 1>指向的类型不相关;转换需要reinterpret_cast、C 风格转换或函数风格转换”

尽我所能深入研究源代码,看来这些类型确实无关。bits8 只是 unsigned char,但是 gray8_ptr_t 是一个指向struct Pixel 的指针,该结构的唯一元素是单个bits8,因此reinterpret_cast 看起来很安全。

但是,我经常将外部数据包装到图像视图中,并且在每个地方都使用reinterpret_cast 感觉有问题。在 GIL 中使用?

当前的解决方法:

template<class Dest, class Src>
Dest gil_safe_ptr_cast(Src src)
{
    // this cast is unsafe, use reinterpret_cast 
    BOOST_STATIC_ASSERT(false);
}
template<> boost::gil::gray8_ptr_t gil_safe_ptr_cast(boost::gil::bits8* pBits8)
{
    return reinterpret_cast<boost::gil::gray8_ptr_t>(pBits8);
}
boost::gil::bits8* pBits8 = data8;
boost::gil::gray8_ptr_t pGray8 = gil_safe_ptr_cast<boost::gil::gray8_ptr_t>(pBits8); // works
boost::gil::bits16* pBits16 = NULL;
boost::gil::gray8_ptr_t pGray82 = gil_safe_ptr_cast<boost::gil::gray8_ptr_t>(pBits16); // compile error as expected

Trying to work by the design guidelines for GIL, I use bits__ for my channel data types. I often have external data I'm wrapping into GIL image views. However, even using the bits__ types for data pointers, I have to add in a reinterpret_cast before I can create my image views. Take the following code

int width = 3;
int height = 2;

boost::gil::bits8 data8[] = {0, 1, 100, 200, 50, 51};
boost::gil::bits8* pBits8 = data8;
boost::gil::gray8_ptr_t pGray8 = pBits8;

boost::gil::gray8_view_t v = interleaved_view(width, height, pGray8, width * sizeof(boost::gil::bits8));

Results in the error on line 6 "error C2440: 'initializing' : cannot convert from 'boost::gil::bits8 *' to 'boost::gil::gray8_ptr_t'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast"

Delving into the source code as much as I can, it appears these types really are unreleated. bits8 is just unsigned char, but gray8_ptr_t is a pointer to a struct pixel<bits8,gray_layout_t>. The only element of this struct is a single bits8, so a reinterpret_cast looks safe. It also works fine for the tests I've thrown at it.

However, I wrap external data into image views quite often, and having a reinterpret_cast in every place feels problematic. Is there a safer way of constructing a pixel pointer for use in GIL?

Current workaround:

template<class Dest, class Src>
Dest gil_safe_ptr_cast(Src src)
{
    // this cast is unsafe, use reinterpret_cast 
    BOOST_STATIC_ASSERT(false);
}
template<> boost::gil::gray8_ptr_t gil_safe_ptr_cast(boost::gil::bits8* pBits8)
{
    return reinterpret_cast<boost::gil::gray8_ptr_t>(pBits8);
}
boost::gil::bits8* pBits8 = data8;
boost::gil::gray8_ptr_t pGray8 = gil_safe_ptr_cast<boost::gil::gray8_ptr_t>(pBits8); // works
boost::gil::bits16* pBits16 = NULL;
boost::gil::gray8_ptr_t pGray82 = gil_safe_ptr_cast<boost::gil::gray8_ptr_t>(pBits16); // compile error as expected

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

泪眸﹌ 2024-11-16 14:01:54

要从bits8 *转换为gray8_ptr_t,请创建一个结构体像素并将bits8提供给构造函数:

gray8_ptr_t convert_gray8_ptr_t(bits8* src) {
    return new struct pixel<bits8,gray_layout_t>(*src);
}

要转换回来,请使用结构体的转换运算符:

bits8* convert_bits8(gray8_ptr_t src) {
    bits8* result = new bits8;
    *result = (bits8) *src;
    return result;
}

当然,这两个函数都会分配内存,并且可能不需要作为函数(作为内联代码更好) 。

To convert from bits8* to gray8_ptr_t, create a struct pixel and provide the bits8 to the constructor:

gray8_ptr_t convert_gray8_ptr_t(bits8* src) {
    return new struct pixel<bits8,gray_layout_t>(*src);
}

To convert back, use the struct's conversion operator:

bits8* convert_bits8(gray8_ptr_t src) {
    bits8* result = new bits8;
    *result = (bits8) *src;
    return result;
}

Of course both of these functions allocate memory and are probably unnecessary as functions (better as inline code).

猫腻 2024-11-16 14:01:54
template<class Dest, class Src>
Dest gil_safe_ptr_cast(Src src)
{
    // this cast is unsafe, use reinterpret_cast 
    BOOST_STATIC_ASSERT(false);
}
template<> boost::gil::gray8_ptr_t gil_safe_ptr_cast(boost::gil::bits8* pBits8)
{
    return reinterpret_cast<boost::gil::gray8_ptr_t>(pBits8);
}
boost::gil::bits8* pBits8 = data8;
boost::gil::gray8_ptr_t pGray8 = gil_safe_ptr_cast<boost::gil::gray8_ptr_t>(pBits8); // works
boost::gil::bits16* pBits16 = NULL;
boost::gil::gray8_ptr_t pGray82 = gil_safe_ptr_cast<boost::gil::gray8_ptr_t>(pBits16); // compile error as expected
template<class Dest, class Src>
Dest gil_safe_ptr_cast(Src src)
{
    // this cast is unsafe, use reinterpret_cast 
    BOOST_STATIC_ASSERT(false);
}
template<> boost::gil::gray8_ptr_t gil_safe_ptr_cast(boost::gil::bits8* pBits8)
{
    return reinterpret_cast<boost::gil::gray8_ptr_t>(pBits8);
}
boost::gil::bits8* pBits8 = data8;
boost::gil::gray8_ptr_t pGray8 = gil_safe_ptr_cast<boost::gil::gray8_ptr_t>(pBits8); // works
boost::gil::bits16* pBits16 = NULL;
boost::gil::gray8_ptr_t pGray82 = gil_safe_ptr_cast<boost::gil::gray8_ptr_t>(pBits16); // compile error as expected
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文