void spatialWeightingDeNoise(unsigned char* dst, unsigned char* src, int width, int height, int srcStride, int dstStride, int radius)
{
int offsetAddr = radius * dstStride + radius;
int boxLength = 2 * radius + 1;
int boxSize = BOX_SIZE;
float weight[BOX_SIZE] = { 0 };
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
unsigned char* boxSrc = src + w - offsetAddr;
float difSum = 0;
for (int i = 0,idx = 0; i < boxLength; i++)
{
for (int j = 0; j < boxLength; j++)
{
weight[idx] = 1.f * abs(boxSrc[j] - src[w]);
difSum += weight[idx];
idx++;
}
boxSrc += dstStride;
}
float weightSum = 0;
for (int idx = 0; idx < boxSize; idx++)
{
weight[idx] = 1 - weight[idx] / (difSum + 0.1f);
weightSum += weight[idx];
}
boxSrc = src + w - offsetAddr;
float weightDst = 0;
for (int i = 0, idx = 0; i < boxLength; i++)
{
for (int j = 0; j < boxLength; j++)
{
weight[idx] = weight[idx] / weightSum;
weightDst += weight[idx] * boxSrc[j];
idx++;
}
boxSrc += dstStride;
}
dst[w] = (unsigned char)(weightDst > 255 ? 255 : weightDst);
}
src += srcStride;
dst += dstStride;
}
}
void padding(unsigned char* dst, unsigned char* src, int width, int height, int srcStride, int dstStride, int padSize)
{
int offsetAddr = padSize * dstStride + padSize;
unsigned char* dstTmp = dst + offsetAddr;
for (int h = 0; h < height; h++)
{
memcpy(dstTmp, src, srcStride * sizeof(*src));
src += srcStride;
dstTmp += dstStride;
}
dstTmp = dst + padSize * dstStride;
for (int h = 0; h < height; h++)
{
unsigned char leftVal = dstTmp[padSize];
unsigned char rightVal = dstTmp[padSize + width - 1];
for (int w = 0; w < padSize; w++)
{
dstTmp[w] = leftVal;
dstTmp[width + padSize + w] = rightVal;
}
dstTmp += dstStride;
}
dstTmp = dst;
unsigned char* dstTop = dst + padSize * dstStride;
for (int h = 0; h < padSize; h++)
{
memcpy(dstTmp, dstTop, dstStride * sizeof(*dst));
dstTmp += dstStride;
}
dstTmp = dst + (height + padSize) * dstStride;
unsigned char* dstbotom = dstTmp - dstStride;
for (int h = 0; h < padSize; h++)
{
memcpy(dstTmp, dstbotom, dstStride * sizeof(*dst));
dstTmp += dstStride;
}
}