//锐化图像
void CppSharpenImage(byte* bitmap, int width, int height, int sharp)
{
//每行的字节数
int line_bytes_num = width * 4;
//被处理的当前行的行首指针
byte* line_head_ptr;
//被处理的当前像素点的指针
byte* cur;
//临时数组,存放像素点计算结果
int n1 = height, n2 = width * 3;
byte** temp = new byte*[n1];
for (int i = 0; i < n1; ++i)
{
temp[i] = new byte[n2];
}
/* 周围八个点
**[1] [2] [3]
**[4] [X] [5]
**[6] [7] [8]
*/
byte* cur1;
byte* cur2;
byte* cur3;
byte* cur4;
byte* cur5;
byte* cur6;
byte* cur7;
byte* cur8;
//临时变量
int temp_b, temp_g, temp_r, vb, vg, vr;
for (int x = 1; x < height - 1; x++)
{
line_head_ptr = bitmap + x * line_byte_num;
for (int y = 1; y < width - 1; y++)
{
cur = line_head_ptr + y * 4;
//获取周围八个点的指针
cur1 = cur - line_bytes_num - 4;
cur2 = cur - lint_bytes_num;
cur3 = cur - line_bytes_num + 4;
cur4 = cur - 4;
cur5 = cur + 4;
cur6 = cur + line_bytes_num - 4;
cur7 = cur + line_bytes_num;
cur8 = cur + line_bytes_num + 4;
//计算周围白个点的平均BGR
temp_b = (cur1[0] + cur2[0] + cur3[0] + cur4[0] + cur5[0] + cur6[0] + cur7[0] + cur8[0]) / 8;
temp_g = (cur1[1] + cur2[1] + cur3[1] + cur4[1] + cur5[1] + cur6[1] + cur7[1] + cur8[1]) / 8;
temp_r = (cur1[2] + cur2[2] + cur3[2] + cur4[2] + cur5[2] + cur6[2] + cur7[2] + cur8[2]) / 8;
vb = cur[0] + temp_b * sharp;
vg = cur[1] + temp_g * sharp;
vr = cur[2] + temp_r * sharp;
vb = vb > 255 ? 255 : vb;
vb = vb < 0 ? 0 : vb;
vg = vg > 255 ? 255 : vg;
vg = vg < 0 ? 0 : vg;
vr = vr > 255 ? 255 : vr;
vr = vr < 0 ? 0 : vr;
temp[x][y*3] = (byte)vb;
temp[x][y*3+1] = (byte)vg;
temp[x][y*3+2] = (byte)vr;
}
}
}
转载于:https://my.oschina.net/dukson/blog/313543