Скачиваний:
29
Добавлен:
02.05.2014
Размер:
9.17 Кб
Скачать
unsigned char **massiwR,**massiwG,**massiwB; // Массивы цветов в которых хранится исходное изображение
bool empty; // Для определения пустоты содержимого
int dimensionX, dimensionY; // Для размеров картинки по X и Y

void smoothing( int type ) {
unsigned char **ppMassiwR,**ppMassiwG,**ppMassiwB; // Вспомогательные массивы

if ( empty ) return;

int dim, sum; // размер матрицы коэффициентов - размер кисти, sum - сумма элементов матрицы (для нормировки)
if ( type == 0 || type == 1 ) {
dim = 3;
}
else if ( type == 2 || type == 3 ) {
dim = 5;
}

int **matrixOfKoef; // от конкретных коэффициентов матрицы зависит вид размытия
matrixOfKoef = new int*[dim];
for ( int i = 0; i < dim; i ++ ) {
matrixOfKoef[i] = new int[dim];
}

// инициализация вспомогательных массивов
ppMassiwR = new unsigned char *[dimensionX];
for ( pox = 0; pox < dimensionX; pox++) {
ppMassiwR[pox] = new unsigned char[dimensionY];
for ( poy = 0 ; poy < dimensionY; poy++ ) {
ppMassiwR[pox][poy] = massiwR[pox][poy];
}
}

ppMassiwG = new unsigned char *[dimensionX];
for ( pox = 0; pox < dimensionX; pox++) {
ppMassiwG[pox] = new unsigned char[dimensionY];
for ( poy = 0 ; poy < dimensionY; poy++ ) {
ppMassiwG[pox][poy] = massiwG[pox][poy];
}
}

ppMassiwB = new unsigned char *[dimensionX];
for ( pox = 0; pox < dimensionX; pox++) {
ppMassiwB[pox] = new unsigned char[dimensionY];
for ( poy = 0 ; poy < dimensionY; poy++ ) {
ppMassiwB[pox][poy] = massiwB[pox][poy];
}
}

// собственно наводка резкости
// два варианта для "кисти" размера 3*3 и 5*5
if ( type == 0 ) {
matrixOfKoef[0][0] = -1; matrixOfKoef[0][1] = -1; matrixOfKoef[0][2] = -1;
matrixOfKoef[1][0] = -1; matrixOfKoef[1][1] = 14; matrixOfKoef[1][2] = -1;
matrixOfKoef[2][0] = -1; matrixOfKoef[2][1] = -1; matrixOfKoef[2][2] = -1;
sum = 6;
}
else if ( type == 1 ) {
matrixOfKoef[0][0] = -2; matrixOfKoef[0][1] = -2; matrixOfKoef[0][2] = -2;
matrixOfKoef[1][0] = -2; matrixOfKoef[1][1] = 32; matrixOfKoef[1][2] = -2;
matrixOfKoef[2][0] = -2; matrixOfKoef[2][1] = -2; matrixOfKoef[2][2] = -2;
sum = 16;
}
else if ( type == 2 ) {
matrixOfKoef[0][0] = 0; matrixOfKoef[0][1] = -1; matrixOfKoef[0][2] = -2; matrixOfKoef[0][3] = -1; matrixOfKoef[0][4] = 0;
matrixOfKoef[1][0] = -1; matrixOfKoef[1][1] = -2; matrixOfKoef[1][2] = 2; matrixOfKoef[1][3] = -2; matrixOfKoef[1][4] = -1;
matrixOfKoef[2][0] = -2; matrixOfKoef[2][1] = 2; matrixOfKoef[2][2] = 32; matrixOfKoef[2][3] = 2; matrixOfKoef[2][4] = -2;
matrixOfKoef[3][0] = -1; matrixOfKoef[3][1] = -2; matrixOfKoef[3][2] = 2; matrixOfKoef[3][3] = -2; matrixOfKoef[3][4] = -1;
matrixOfKoef[4][0] = 0; matrixOfKoef[4][1] = -1; matrixOfKoef[4][2] = -2; matrixOfKoef[4][3] = -1; matrixOfKoef[4][4] = 0;
sum = 16;
}
else if ( type == 3 ) {
matrixOfKoef[0][0] = -1; matrixOfKoef[0][1] = -2; matrixOfKoef[0][2] = -4; matrixOfKoef[0][3] = -2; matrixOfKoef[0][4] = -1;
matrixOfKoef[1][0] = -2; matrixOfKoef[1][1] = -4; matrixOfKoef[1][2] = 4; matrixOfKoef[1][3] = -4; matrixOfKoef[1][4] = -2;
matrixOfKoef[2][0] = -4; matrixOfKoef[2][1] = 4; matrixOfKoef[2][2] = 64; matrixOfKoef[2][3] = 4; matrixOfKoef[2][4] = -4;
matrixOfKoef[3][0] = -2; matrixOfKoef[3][1] = -4; matrixOfKoef[3][2] = 4; matrixOfKoef[3][3] = -4; matrixOfKoef[3][4] = -2;
matrixOfKoef[4][0] = -1; matrixOfKoef[4][1] = -2; matrixOfKoef[4][2] = -4; matrixOfKoef[4][3] = -2; matrixOfKoef[4][4] = -1;
sum = 28;
}

if ( type == 0 || type == 1 ) {
for ( pox = 1; pox < dimensionX - 1; pox++ ) {
for ( poy = 1 ; poy < dimensionY - 1; poy++ ) {
massiwR[pox][poy] = In0255( ( matrixOfKoef[0][0]*ppMassiwR[pox-1][poy-1] + matrixOfKoef[1][0]*ppMassiwR[pox][poy-1] + matrixOfKoef[2][0]*ppMassiwR[pox+1][poy-1] + matrixOfKoef[0][1]*ppMassiwR[pox-1][poy] + matrixOfKoef[1][1]*ppMassiwR[pox][poy] + matrixOfKoef[2][1]*ppMassiwR[pox+1][poy] + matrixOfKoef[0][2]*ppMassiwR[pox-1][poy+1] + matrixOfKoef[1][2]*ppMassiwR[pox][poy+1] + matrixOfKoef[2][2]*ppMassiwR[pox+1][poy+1])/sum );
massiwG[pox][poy] = In0255( ( matrixOfKoef[0][0]*ppMassiwG[pox-1][poy-1] + matrixOfKoef[1][0]*ppMassiwG[pox][poy-1] + matrixOfKoef[2][0]*ppMassiwG[pox+1][poy-1] + matrixOfKoef[0][1]*ppMassiwG[pox-1][poy] + matrixOfKoef[1][1]*ppMassiwG[pox][poy] + matrixOfKoef[2][1]*ppMassiwG[pox+1][poy] + matrixOfKoef[0][2]*ppMassiwG[pox-1][poy+1] + matrixOfKoef[1][2]*ppMassiwG[pox][poy+1] + matrixOfKoef[2][2]*ppMassiwG[pox+1][poy+1])/sum );
massiwB[pox][poy] = In0255( ( matrixOfKoef[0][0]*ppMassiwB[pox-1][poy-1] + matrixOfKoef[1][0]*ppMassiwB[pox][poy-1] + matrixOfKoef[2][0]*ppMassiwB[pox+1][poy-1] + matrixOfKoef[0][1]*ppMassiwB[pox-1][poy] + matrixOfKoef[1][1]*ppMassiwB[pox][poy] + matrixOfKoef[2][1]*ppMassiwB[pox+1][poy] + matrixOfKoef[0][2]*ppMassiwB[pox-1][poy+1] + matrixOfKoef[1][2]*ppMassiwB[pox][poy+1] + matrixOfKoef[2][2]*ppMassiwB[pox+1][poy+1])/sum );
}
}
}
else if ( type == 2 || type == 3 ) {
for (pox = 2; pox < dimensionX - 2; pox++ ) {
for (poy = 2; poy < dimensionY - 2; poy++ ) {
massiwR[pox][poy] = In0255(
( matrixOfKoef[0][0]*ppMassiwR[pox-2][poy-2] + matrixOfKoef[1][0]*ppMassiwR[pox-1][poy-2] + matrixOfKoef[2][0]*ppMassiwR[pox][poy-2] + matrixOfKoef[3][0]*ppMassiwR[pox+1][poy-2] + matrixOfKoef[4][0]*ppMassiwR[pox+2][poy-2] +
matrixOfKoef[0][1]*ppMassiwR[pox-2][poy-1] + matrixOfKoef[1][1]*ppMassiwR[pox-1][poy-1] + matrixOfKoef[2][1]*ppMassiwR[pox][poy-1] + matrixOfKoef[3][1]*ppMassiwR[pox+1][poy-1] + matrixOfKoef[4][1]*ppMassiwR[pox+2][poy-1] +
matrixOfKoef[0][2]*ppMassiwR[pox-2][poy] + matrixOfKoef[1][2]*ppMassiwR[pox-1][poy] + matrixOfKoef[2][2]*ppMassiwR[pox][poy] + matrixOfKoef[3][2]*ppMassiwR[pox+1][poy] + matrixOfKoef[4][2]*ppMassiwR[pox+2][poy-2] +
matrixOfKoef[0][3]*ppMassiwR[pox-2][poy+1] + matrixOfKoef[1][3]*ppMassiwR[pox-1][poy+1] + matrixOfKoef[2][3]*ppMassiwR[pox][poy+1] + matrixOfKoef[3][3]*ppMassiwR[pox+1][poy+1] + matrixOfKoef[4][3]*ppMassiwR[pox+2][poy+1] +
matrixOfKoef[0][4]*ppMassiwR[pox-2][poy+2] + matrixOfKoef[1][4]*ppMassiwR[pox-1][poy+2] + matrixOfKoef[2][4]*ppMassiwR[pox][poy+2] + matrixOfKoef[3][4]*ppMassiwR[pox+1][poy+2] + matrixOfKoef[4][4]*ppMassiwR[pox+2][poy+2])/sum );
massiwG[pox][poy] = In0255(
( matrixOfKoef[0][0]*ppMassiwG[pox-2][poy-2] + matrixOfKoef[1][0]*ppMassiwG[pox-1][poy-2] + matrixOfKoef[2][0]*ppMassiwG[pox][poy-2] + matrixOfKoef[3][0]*ppMassiwG[pox+1][poy-2] + matrixOfKoef[4][0]*ppMassiwG[pox+2][poy-2] +
matrixOfKoef[0][1]*ppMassiwG[pox-2][poy-1] + matrixOfKoef[1][1]*ppMassiwG[pox-1][poy-1] + matrixOfKoef[2][1]*ppMassiwG[pox][poy-1] + matrixOfKoef[3][1]*ppMassiwG[pox+1][poy-1] + matrixOfKoef[4][1]*ppMassiwG[pox+2][poy-1] +
matrixOfKoef[0][2]*ppMassiwG[pox-2][poy] + matrixOfKoef[1][2]*ppMassiwG[pox-1][poy] + matrixOfKoef[2][2]*ppMassiwG[pox][poy] + matrixOfKoef[3][2]*ppMassiwG[pox+1][poy] + matrixOfKoef[4][2]*ppMassiwG[pox+2][poy-2] +
matrixOfKoef[0][3]*ppMassiwG[pox-2][poy+1] + matrixOfKoef[1][3]*ppMassiwG[pox-1][poy+1] + matrixOfKoef[2][3]*ppMassiwG[pox][poy+1] + matrixOfKoef[3][3]*ppMassiwG[pox+1][poy+1] + matrixOfKoef[4][3]*ppMassiwG[pox+2][poy+1] +
matrixOfKoef[0][4]*ppMassiwG[pox-2][poy+2] + matrixOfKoef[1][4]*ppMassiwG[pox-1][poy+2] + matrixOfKoef[2][4]*ppMassiwG[pox][poy+2] + matrixOfKoef[3][4]*ppMassiwG[pox+1][poy+2] + matrixOfKoef[4][4]*ppMassiwG[pox+2][poy+2])/sum );
massiwB[pox][poy] = In0255(
( matrixOfKoef[0][0]*ppMassiwB[pox-2][poy-2] + matrixOfKoef[1][0]*ppMassiwB[pox-1][poy-2] + matrixOfKoef[2][0]*ppMassiwB[pox][poy-2] + matrixOfKoef[3][0]*ppMassiwB[pox+1][poy-2] + matrixOfKoef[4][0]*ppMassiwB[pox+2][poy-2] +
matrixOfKoef[0][1]*ppMassiwB[pox-2][poy-1] + matrixOfKoef[1][1]*ppMassiwB[pox-1][poy-1] + matrixOfKoef[2][1]*ppMassiwB[pox][poy-1] + matrixOfKoef[3][1]*ppMassiwB[pox+1][poy-1] + matrixOfKoef[4][1]*ppMassiwB[pox+2][poy-1] +
matrixOfKoef[0][2]*ppMassiwB[pox-2][poy] + matrixOfKoef[1][2]*ppMassiwB[pox-1][poy] + matrixOfKoef[2][2]*ppMassiwB[pox][poy] + matrixOfKoef[3][2]*ppMassiwB[pox+1][poy] + matrixOfKoef[4][2]*ppMassiwB[pox+2][poy-2] +
matrixOfKoef[0][3]*ppMassiwB[pox-2][poy+1] + matrixOfKoef[1][3]*ppMassiwB[pox-1][poy+1] + matrixOfKoef[2][3]*ppMassiwB[pox][poy+1] + matrixOfKoef[3][3]*ppMassiwB[pox+1][poy+1] + matrixOfKoef[4][3]*ppMassiwB[pox+2][poy+1] +
matrixOfKoef[0][4]*ppMassiwB[pox-2][poy+2] + matrixOfKoef[1][4]*ppMassiwB[pox-1][poy+2] + matrixOfKoef[2][4]*ppMassiwB[pox][poy+2] + matrixOfKoef[3][4]*ppMassiwB[pox+1][poy+2] + matrixOfKoef[4][4]*ppMassiwB[pox+2][poy+2])/sum );
}
}
}

// освобождение памяти
for ( pox = 0; pox < dimensionX; pox++) {
delete ppMassiwR[pox];
}
delete ppMassiwR;

for ( pox = 0; pox < dimensionX; pox++) {
delete ppMassiwG[pox];
}
delete ppMassiwG;

for ( pox = 0; pox < dimensionX; pox++) {
delete ppMassiwB[pox];
}
delete ppMassiwB;
}

// итог работы функции - новое "размытое" содержание массивов massiwR, massiwG, massiwB