void MaxFilter(unsigned int *src, unsigned int *dst, int width, int height)
{
assert(src != NULL);
assert(dst != NULL);
assert(width >= 0);
assert(height >= 0);
memset(expandDwtImageBuff, 0, EXPAND_IMAGE_SIZE * sizeof(unsigned int));
memset(maxCols, 0, EXPAND_IMAGE_SIZE * sizeof(unsigned int));
memset(maxRows, 0, EXPAND_IMAGE_SIZE * sizeof(unsigned int));
int expWidth = width + 2 * EXPAND_SIZE;
int expHeight = height + 2 * EXPAND_SIZE;
// 对输入图像进行扩展
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
expandDwtImageBuff[(y + EXPAND_SIZE) * expWidth + x + EXPAND_SIZE] = src[y * width + x];
}
// 找行方向的最大值(每一行41*1邻域的最大值)
unsigned int maxColValue = 0; // 最大值
Point maxColPos = { 0, 0 }; // 最大值坐标
const int localDwtLen = 2 * EXPAND_SIZE + 1;
for (int y = EXPAND_SIZE; y < height + EXPAND_SIZE; y++)
{
int starty = y;
int endy = starty;
for (int x = EXPAND_SIZE; x < width + EXPAND_SIZE; x++)
{
if (expandDwtImageBuff[y * expWidth + x] > 0)
{
int startx = x - EXPAND_SIZE;
int endx = x + EXPAND_SIZE;
// 最大值求解
if (x == EXPAND_SIZE || maxColPos.x < startx)
{
GetMaxLocation(expandDwtImageBuff + starty * expWidth + startx, localDwtLen, &maxColValue, &maxColPos.x);
maxColPos.x += startx;
maxColPos.y += starty;
maxCols[y * expWidth + x] = maxColValue;
}
else
{
// 在移入的范围内,比较最大值和移入值
int newValue = expandDwtImageBuff[y * expWidth + endx];
if (newValue >= maxColValue)
{
maxColValue = newValue;
maxColPos.x = endx;
maxColPos.y = y;
}
maxCols[y * expWidth + x] = maxColValue;
}
}
}
maxColPos.x = 0;
maxColPos.y = 0;
}
// 找列方向的最大值(maxCols中每一列1*41邻域的最大值)
unsigned int maxRowValue = 0;
Point maxRowPos = { 0, 0 };
unsigned int localDwtBuff[2 * EXPAND_SIZE + 1] = { 0 };
for (int x = EXPAND_SIZE; x < width + EXPAND_SIZE; x++)
{
int startx = x;
int endx = startx;
for (int y = EXPAND_SIZE; y < height + EXPAND_SIZE; y++)
{
int starty = y - EXPAND_SIZE;
int endy = y + EXPAND_SIZE;
// 最大值求解
if (y == EXPAND_SIZE || maxRowPos.y < starty)
{
for (int i = 0; i < localDwtLen; i++)
localDwtBuff[i] = maxCols[(starty + i) * expWidth + startx];
GetMaxLocation(localDwtBuff, localDwtLen, &maxRowValue, &maxRowPos.y);
maxRowPos.y += starty;
maxRows[y * expWidth + x] = maxRowValue;
}
else
{
// 在移入的范围内,比较最大值和移入值
// 移入值为1*41矩形区域中最后一个值
int newValue = maxCols[endy * expWidth + x];
if (newValue >= maxRowValue)
{
maxRowValue = newValue;
maxRowPos.y = endy;
}
maxRows[y * expWidth + x] = maxRowValue;
}
if (expandDwtImageBuff[y * expWidth + x] == 0)
maxRows[y * expWidth + x] = 0;
}
}
// 切掉扩展出来的边缘
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
dst[y * width + x] = maxRows[(y + EXPAND_SIZE) * expWidth + x + EXPAND_SIZE];
}
}