SHOW:
|
|
- or go back to the newest paste.
1 | #include <iostream> | |
2 | #include <fstream> | |
3 | #include <string> | |
4 | #include <vector> | |
5 | ||
6 | using namespace std; | |
7 | ||
8 | int main(int argc, char* argv[]) | |
9 | { | |
10 | ifstream infile; | |
11 | ofstream outfile; | |
12 | string line, type, maxpx, comment; | |
13 | int cols, rows; | |
14 | const int SIZE = 3; | |
15 | ||
16 | - | infile.open("file.pgm", ios::binary); |
16 | + | infile.open("/Users/dotun/Desktop/file/file/lena.pgm", ios::binary); |
17 | ||
18 | - | getline(infile, type); |
18 | + | getline(infile, type); //Read first line and grab the type of file |
19 | - | getline(infile, comment); |
19 | + | //getline(infile, comment); //Read second line and discard it |
20 | ||
21 | - | infile >> cols >> rows; |
21 | + | infile >> rows >> cols; //Read third line and get columns and rows |
22 | ||
23 | getline(infile, line); | |
24 | - | getline(infile, maxpx); |
24 | + | getline(infile, maxpx); //Read highest pixel |
25 | ||
26 | - | getline(infile, line); |
26 | + | |
27 | vector<vector<char>> image(rows, vector<char>(cols, '\0')); | |
28 | vector<vector<char>> out(rows, vector<char>(cols, '\0')); | |
29 | vector<vector<int>> filter = { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } }; //Sharpen | |
30 | ||
31 | for (int i = 0; i < rows; i++) | |
32 | for (int j = 0; j < cols; j++) | |
33 | infile >> image[i][j]; | |
34 | ||
35 | infile.close(); | |
36 | ||
37 | outfile.open("/Users/dotun/Desktop/file/file/output.pgm", ios::binary); | |
38 | - | outfile.open("output.pgm", ios::binary); |
38 | + | |
39 | ||
40 | for (int i = SIZE / 2; i < rows - SIZE / 2; i++) | |
41 | - | for (int i = SIZE / 2; i < rows - SIZE / 2; i++) |
41 | + | { |
42 | for (int j = SIZE / 2; j < cols - SIZE / 2; j++) | |
43 | - | for (int j = SIZE / 2; j < cols - SIZE / 2; j++) |
43 | + | |
44 | - | { |
44 | + | char sum = '\0'; |
45 | - | char sum = '\0'; |
45 | + | for (int k = -SIZE / 2; k <= SIZE / 2; k++) |
46 | - | for (int k = -SIZE / 2; k <= SIZE / 2; k++) |
46 | + | { |
47 | - | { |
47 | + | for (int l = -SIZE / 2; l <= SIZE / 2; l++) |
48 | - | for (int l = -SIZE / 2; l <= SIZE / 2; l++) |
48 | + | { |
49 | - | { |
49 | + | sum += image[i+k][j+l] * filter[k + SIZE / 2][l + SIZE / 2]; |
50 | - | sum += image[i+k][j+l] * filter[k + SIZE / 2][l + SIZE / 2]; |
50 | + | } |
51 | - | } |
51 | + | } |
52 | - | } |
52 | + | out[i][j] = sum; |
53 | - | out[i][j] = sum; |
53 | + | |
54 | - | } |
54 | + | } |
55 | ||
56 | for (int i = 0; i < rows; i++) | |
57 | for (int j = 0; j < cols; j++) | |
58 | outfile << image[i][j]; | |
59 | - | outfile << out[i][j]; |
59 | + | |
60 | outfile.close(); | |
61 | ||
62 | return 0; | |
63 | } |