如何使用boost::gil处理数字像素操作中的溢出
How to handle overflow in numeric pixel operations with boost::gil?
boost::gil的数字扩展包含如下算法:
template <typename Channel1,typename Channel2,typename ChannelR>
struct channel_plus_t : public std::binary_function<Channel1,Channel2,ChannelR> {
ChannelR operator()(typename channel_traits<Channel1>::const_reference ch1,
typename channel_traits<Channel2>::const_reference ch2) const {
return ChannelR(ch1)+ChannelR(ch2);
}
};
当用两个uint8通道值填充时,如果通道r也是uint8,则会发生溢出。
我认为计算应该
- 使用不同的类型进行处理(如何从模板通道类型派生此类型?)
- 将结果剪辑到ChannelR类型的范围以获得饱和结果(使用
boost::gil::channel_traits<ChannelR>::min_value()
/…max_value()
?)
如何以一种允许性能优化结果的方式做到这一点?
- 转换为最大可能的类型?听起来适得其反…
- 提供模板专门化库?有更好的主意吗?
我不知道这里有什么问题…我的反应是"所以不要将ChannelR设置为uint8,否则会中断"
你似乎是在争辩像
这样的代码 uint8 a=128;
uint8 b=128;
uint8 c=a+b; // Uh-Oh...
应该做一些聪明的事情(例如饱和算术)。
我建议的解决方案是使用更精确,或定义自己的channel_saturating_plus_t
与您需要的行为,就像我建议的解决方案是
uint16 c=uint16(a)+uint16(b)
或
uint8 c=saturating_add(a,b);
并且感谢GIL的创建者甚至考虑将结果类型作为单独的类型参数公开;外面有很多不喜欢的东西!
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 重载操作程序时出错>>用于类中的字符串 memebr
- 'short int'持有的值溢出,但"自动"不会溢出?
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 大于65535的C++数组[size]引发不一致的溢出
- 对字符串进行位操作
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- C++中无符号字符溢出
- 我可以在 C++ 中的函数体之外进行操作吗?
- MPI突然停止了对多个核心的操作
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 对字符数组中的元素执行逐位操作
- 如何在directx/c++中进行平移/缩放操作
- 逐位操作的隐式类型转换
- 为什么一个向量上的多线程操作很慢
- C 迭代器操作未检查溢出
- 操作溢出不起作用C
- 如果必须检查溢出或有条件地执行操作,则std::原子冗余
- 如何使用原子增量和比较与交换操作处理整数溢出
- 如何使用boost::gil处理数字像素操作中的溢出