#include <fstream>
#include <iostream>
#include <sstream>
using
namespace
std;
int
array[2000][2000];
int
arr[2000][2000];
int
hist[255];
int
main()
{
int
i, row = 0, j = 0, col = 0, numrows = 0, numcols = 0, MAX = 0;
ifstream infile(
"saltpepper.pgm"
);
stringstream ss;
string inputLine =
""
;
getline(infile, inputLine);
if
(inputLine.compare(
"P2"
) != 0)
cerr <<
"Version error"
<< endl;
else
cout <<
"Version : "
<< inputLine << endl;
ss << infile.rdbuf();
ss >> numcols >> numrows >> MAX;
cout << numcols <<
" columns and "
<< numrows <<
" rows"
<< endl
<<
" Maximum Intensity "
<< MAX << endl;
for
(row = 0; row <= numrows; ++row)
array[row][0] = 0;
for
(col = 0; col <= numcols; ++col)
array[0][col] = 0;
printf
(
"****\n"
);
for
(row = 1; row <= numrows; ++row) {
for
(col = 1; col <= numcols; ++col) {
ss >> array[row][col];
}
}
for
(row = 1; row <= numrows; ++row) {
for
(col = 1; col <= numcols; ++col) {
if
(array[row][col] == 0 || array[row][col] == 255)
{
arr[row][col] = (array[row - 1][col] +
array[row - 1][col - 1] +
array[row - 1][col + 1] +
array[row][col - 1] +
array[row][col + 1] +
array[row + 1][col + 1] +
array[row + 1][col] +
array[row + 1][col - 1]) / 8;
}
else
{
arr[row][col] = array[row][col];
}
}
}
ofstream outfile;
outfile.open(
"salt and pepper.pgm"
);
outfile <<
"P2"
<< endl;
outfile << numcols <<
" "
<< numrows << endl;
outfile <<
"255"
<< endl;
for
(row = 1; row <= numrows; ++row) {
for
(col = 1; col <= numcols; ++col) {
outfile << arr[row][col] <<
" "
;
}
}
outfile.close();
infile.close();
return
0;
}