-
Notifications
You must be signed in to change notification settings - Fork 124
/
exercise14.c
169 lines (148 loc) · 3.24 KB
/
exercise14.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// C Primer Plus
// Chapter 13 Exercise 14:
// Digital images, particularly those radioed back from spacecraft, may have
// glitches. Add a de-glitching function to programming exercise 12. It should
// compare each value to its immediate neighbors to the left and right, above
// and below. If the value differs by more than 1 from each of its neighbors,
// replace the value with the average of the neighboring values. You should
// round the average to the nearest integer value. Note that the points along
// the boundaries have fewer than four neighbors, so they require special
// handling.
// Note: use data.txt as input file to test this program.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define ROWS 20
#define COLS 30
void deglitch(int data[ROWS][COLS]);
int main(int argc, char *argv[])
{
FILE *fp;
int data[ROWS][COLS];
char img[ROWS][COLS + 1];
char ch;
if (argc != 3)
{
printf("Usage: %s <data file> <image file>\n", argv[0]);
exit(EXIT_FAILURE);
}
// open data file
if ((fp = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr, "Could not open file %s.\n", argv[1]);
exit(EXIT_FAILURE);
}
// read ints from file
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
if (fscanf(fp, "%d", *(data + i) + j) != 1)
{
fprintf(stderr, "Invalid or corrupted data file.\n");
exit(EXIT_FAILURE);
}
fclose(fp); // done with fp for now
// de-glitch the data
deglitch(data);
// convert ints to characters
for (int i = 0; i < ROWS; i++)
{
int j;
for (j = 0; j < COLS; j++)
{
switch (data[i][j])
{
case(0):
ch = ' ';
break;
case(1):
ch = '.';
break;
case(2):
ch = '\'';
break;
case(3):
ch = ':';
break;
case(4):
ch = '~';
break;
case(5):
ch = '*';
break;
case(6):
ch = '=';
break;
case(7):
ch = '}';
break;
case(8):
ch = '&';
break;
case(9):
ch = '#';
break;
default:
fprintf(stderr, "Error: int out of bounds.\n");
exit(EXIT_FAILURE);
}
img[i][j] = ch;
}
img[i][j] = '\0'; // j = COLS here
}
// open image file
if ((fp = fopen(argv[2], "w")) == NULL)
{
fprintf(stderr, "Could not open file %s.\n", argv[2]);
exit(EXIT_FAILURE);
}
for (int i = 0; i < ROWS; i++)
{
// print to console and file
puts(img[i]);
fprintf(fp, "%s\n", img[i]);
}
fclose(fp);
return 0;
}
void deglitch(int data[ROWS][COLS])
{
float total;
int count;
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
{
total = 0;
count = 0;
if (i + 1 < ROWS)
{
if (abs(data[i][j] - data[i + 1][j]) < 2)
continue;
total += data[i + 1][j];
count++;
}
if (j + 1 < COLS)
{
if (abs(data[i][j] - data[i][j + 1]) < 2)
continue;
total += data[i][j + 1];
count++;
}
if (i > 0)
{
if (abs(data[i][j] - data[i - 1][j]) < 2)
continue;
total += data[i - 1][j];
count++;
}
if (j > 0)
{
if (abs(data[i][j] - data[i][j - 1]) < 2)
continue;
total += data[i][j - 1];
count++;
}
// if none of the continue statements have been triggered,
// replace data[i][j] with the rounded average of its neighbors
data[i][j] = (int) rintf(total / count);
}
}