抗锯齿像素渲染
anti-aliased pixel rendering
本文关键字:像素 更新时间:2023-10-16
我正在尝试实现抗锯齿像素渲染。我的基本想法是渲染4个像素而不是1个,并根据每个"真实"像素到"虚拟"像素的距离为其赋予权重:
void put_aa_pixel(double x, double y)
{
int x0 = int(x);
int x1 = x0 + 1;
int y0 = int(y);
int y1 = y0 + 1;
double weight_x1 = x - x0;
double weight_x0 = 1 - weight_x1;
double weight_y1 = y - y0;
double weight_y0 = 1 - weight_x1;
put_pixel(x0, y0, int((weight_x0 * weight_y0) * 255));
put_pixel(x1, y0, int((weight_x1 * weight_y0) * 255));
put_pixel(x0, y1, int((weight_x0 * weight_y1) * 255));
put_pixel(x1, y1, int((weight_x1 * weight_y1) * 255));
}
将x和y的权重相乘,得到虚拟像素在每个实像素内的重叠区域。我天真地认为这会给我一个完美的抗锯齿效果,但在我的测试程序中的移动像素只是显示一个可怕的闪烁。它看起来比没有任何抗锯齿的简单像素差得多。
然而,当我从乘法切换到加法时,它看起来好多了:
put_pixel(x0, y0, int((weight_x0 + weight_y0) * 127.5));
put_pixel(x1, y0, int((weight_x1 + weight_y0) * 127.5));
put_pixel(x0, y1, int((weight_x0 + weight_y1) * 127.5));
put_pixel(x1, y1, int((weight_x1 + weight_y1) * 127.5));
添加权重似乎没有任何几何意义。那么,为什么这种方法效果更好呢?第一个版本有什么问题?还有更好的方法吗?
按要求:)
直观地:你的x和y权重表示从虚拟到真实像素沿轴的距离。所以实际距离是√(wx ^2 + wy ^2)这解释了为什么和的效果更好——它比乘法更接近这种形式。
有一个bug在我的代码中潜伏了半年:
double weight_x1 = x - x0;
double weight_x0 = 1 - weight_x1;
double weight_y1 = y - y0;
double weight_y0 = 1 - weight_x1; // BUG
你能看到虫子吗?是的,这是一个典型的复制粘贴错误:
double weight_y0 = 1 - weight_y1; // FIXED
修复错误后,原来的乘法方法看起来非常好。
相关文章:
- 如何在不产生任何垃圾的情况下获得C中的像素
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 如何在24位SDL_Surface上设置像素的颜色
- 如何从SDL_Surface获取特定像素的颜色
- 如何将CMSampleBufferRef/CIImage/UIImage转换为像素,例如uint8_t[]
- 在C++中查找像素 RGB 数据的最快方法是什么?
- 是否可以从 OpenGL 缓冲区获取原始大小的像素?
- 选择基于另一个垫子的非零像素的cv::Mat的一部分?
- 如何使用 sdl2 快速绘制像素网格?
- 如何在C++中进行像素操作? - Linux
- 他们如何将红外锁定像素转换为镜头前方 1m 的正常平面上的位置
- 访问随机图像像素的快速方法,最多一次
- OpenCV 像素访问点与 at() - 不同的值
- 如何使用 freetype2 访问单色位图中的像素状态
- 尝试渲染像素坐标时,简单线条渲染失败
- 计算另一个图像像素满足条件的像素值的平均值
- 如何返回定义良好的内存部分?例如来自图像数据的像素的颜色值
- 在Qt-cpp中通过像素获取QString的子字符串
- 有效地计算像素到其对应核线的距离
- 如何将像素图像导入cpp