转载。 http://blog.csdn.net/u013085897/article/details/54604251
本文实现的水面倒影特效算法,实际上并没有具体原理或者公式推导,当时是参考网上一篇ps教程——《使用ps制作水面倒影特效 》实现的,不过原文链接已经忘记了。做图像算法,有时需要看大量的paper,看公式推导,弄懂算法原理在动手实践;而有时需要去模拟ps效果,比如要实现一个特效,使用ps制作需要5个步骤,那么做图像算法就是要将这个5个步骤程序化。但是每一个步骤都会涉及一个图像算法,这个算法需要自己查资料实现,并且实现的效果要与ps基本一致,这样最终程序化后合成的效果才会与ps设计出的效果尽可能接近。
水面倒影特效算法流程大体分为以下几个步骤:
1. 创建镜像图。由于是水面倒影,所以布局时原图在上,镜像图在下,这里要用到水平翻转,这个比较简单。
2. 为镜像图添加白色渐变色。实际就是创建白色渐变图层,并与镜像图做一次叠加混合,让镜像图色调与原图色调有所差异,这样在视觉上看着会更自然。
- void AddGradientColor(BMPINFO *pSrcBitmap)
- {
- int width = pSrcBitmap->lWidth;
- int height = pSrcBitmap->lHeight;
- int size = width*height;
- uchar *pSrcData = NULL;
- uchar mask_color[3] = { 255, 255, 255 };
- float alpha = 0.0f, gradient_len = 0.0f;
- pSrcData = pSrcBitmap->pPlane[0];
- gradient_len = height*1.2f;
- for (int i = 0; i < height; i++)
- {
- alpha = (float)i / gradient_len;
- alpha *= alpha;
- for (int j = 0; j < width; j++, pSrcData += 4)
- {
- pSrcData[AXJ_BLUE] = (uchar)CLAMP0255(pSrcData[AXJ_BLUE]*(1.0f - alpha) + mask_color[AXJ_BLUE]*alpha);
- pSrcData[AXJ_GREEN] = (uchar)CLAMP0255(pSrcData[AXJ_GREEN]*(1.0f - alpha) + mask_color[AXJ_GREEN]*alpha);
- pSrcData[AXJ_RED] = (uchar)CLAMP0255(pSrcData[AXJ_RED]*(1.0f - alpha) + mask_color[AXJ_RED]*alpha);
- }
- }
- }
3. 为镜像图添加水波纹特效。这也是最核心的步骤,主要涉及以下步骤:
(1)创建白噪声纹理并模糊;
- void CreateWhiteNoise(uchar *noise, int width, int height)
- {
- int r = 0;
- int size = width * height;
- srand((unsigned int)time(NULL));
- for (int i = 0; i < size; i++)
- {
- r = rand();
- r = ((r & 0xff) + ((r & 0xff00) >> 8)) & 0xff;
- r = r > 127 ? 255 : 0;
- noise[i] = (uchar)r;
- }
- }
(2)白噪声纹理经浮雕处理后,再做一次透视变换。主要为了创建合适的置换图,为下一步纹理置换做准备;
- void EmbossEffect(uchar *data, int angle, int width, int height)
- {
- float weights[3][3] = { 0.0f };
- float radian = FILTER_PI*angle / 180.0f, dr = FILTER_PI / 4.0f;
- weights[0][0] = (float)cos(radian + dr);
- weights[0][1] = (float)cos(radian + 2.0*dr);
- weights[0][2] = (float)cos(radian + 3.0*dr);
- weights[1][0] = (float)cos(radian);
- weights[1][1] = 0.0f;
- weights[1][2] = (float)cos(radian + 4.0*dr);
- weights[2][0] = (float)cos(radian - dr);
- weights[2][1] = (float)cos(radian - 2.0*dr);
- weights[2][2] = (float)cos(radian - 3.0*dr);
- int size = width*height;
- uchar *data_copy = (uchar *)malloc(size);
- memcpy(data_copy, data, size);
- int nx = 0, ny = 0, index = 0;
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- float sum = 0.0f;
- for (int m = -1; m <= 1; m++)
- {
- for (int k = -1; k <= 1; k++)
- {
- nx = CLAMP0255_XY(j + k, width - 1);
- ny = CLAMP0255_XY(i + m, height - 1);
- index = ny*width + nx;
- sum += weights[m + 1][k + 1]*data_copy[index];
- }
- }
- index = i*width + j;
- data[index] = (uchar)CLAMP0255(sum + 128);
- }
- }
- free(data_copy);
- data_copy = NULL;
- }
(3)将经多次处理后得到的白噪声纹理与镜像图做纹理置换操作,也就是模拟ps中的置换滤镜操作。所谓置换滤镜,就是将置换图中的像素点灰度值作为原图对应像素的移动距离,最后通过插值得到结果图。通过该步骤,就可以模拟出水波纹效果,还是挺好玩的。
- for (int y = 0; y < pSrcBitmap->lHeight; y++)
- {
- alpha = 0.8f - (float)y / gradient_len;
- posy = ((float)y / pSrcBitmap->lHeight)*height;
- for (int x = 0; x < pSrcBitmap->lWidth; x++, pSrcData += 4)
- {
- posx = ((float)x / pSrcBitmap->lWidth)*width;
- BilinearInterGray(noise_data0, posx, posy, width, height, &noise_val0);
- BilinearInterGray(noise_data1, posx, posy, width, height, &noise_val1);
- float offset_x = (128 - noise_val0)*hor_ratio*alpha;
- float offset_y = (128 - noise_val1)*ver_ratio*alpha;
- BilinearInterRGB(data_copy, x + offset_x, y + offset_y, pSrcBitmap->lWidth, pSrcBitmap->lHeight, &bval, &gval, &rval);
- pSrcData[AXJ_BLUE] = bval;
- pSrcData[AXJ_GREEN] = gval;
- pSrcData[AXJ_RED] = rval;
- }
- }
4. 拼接原图与水面倒影图,并柔和拼接边界。之所以柔和边界,主要是直接拼接时两张图会出现明显的分界线。柔和边界做法比较简单,主要是在边界区域做一次渐变模糊,这样从原图到倒影图的过渡就会非常自然。
下面为一些水面倒影特效结果图: