-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw2.cpp
More file actions
336 lines (246 loc) · 8.76 KB
/
hw2.cpp
File metadata and controls
336 lines (246 loc) · 8.76 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
MANIPULATING PGM IMAGE FILES
by Anna Cristina Karingal
CSCI 135 Assignment 2
Reads an image "image_in.pgm" and:
1. Creates and saves a copy of the image
2. Inverts the image and saves the new image to a new file
3. Sets all pixels to a specified brightness and saves the new image to a new file
Creates an image with:
1. A filled box inside
2. An outlined box inside
3. A blurred copy of the image with an outlined box.
Last edited: April 21, 2014
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class image {
private:
int img[100][100];
public:
// RETURNS COPY OF IMAGE
image copy(){
image imgcopy;
for (int i=0; i<100; i++){
for (int j=0; j<100; j++){
imgcopy.img [i][j] = img [i][j]; // Writes contents of original image into imgcopy
}
}
return imgcopy;
}
// SETS ALL PIXELS IN IMAGE TO BRIGHTNESS b
void fill (int b){
int i, j;
for (i=0; i <100; i++){
for (j=0; j<100; j++){
img [i][j]=b;
}
}
}
// INVERTS IMAGE
void invert(){
int b, i, j;
for (i=0; i <100; i++){
for (j=0; j<100; j++){
b = img [i][j];
img [i][j] = 100-b;
}
}
}
// DRAWS BOX WITH OUTLINE OF BRIGHTNESS b
void box_outline (int b, int r, int c, int width, int height){
int i, j;
for (i=0; i<r; i++){ //Fills pixels until row r with white
for (j=0; j<100; j++){
img [i][j] = 100;
}
}
for (i=r; i<(r+1); i++) { //Draws top outline
for (j=0; j<c; j++){
img [i][j] = 100;
}
for (j=c; j<(c+width+1); j++){
img [i][j] = b;
}
for (j=(c+width+1); j<100; j++){
img [i][j] = 100;
}
}
for (i=(r+1); i < (r+height); i++){ //Draws side outlines
for (j=0; j<c; j++){
img [i][j] = 100;
}
for (j=c; j<(c+1); j++){
img[i][j] = b;
}
for (j=(c+1); j<(c+width); j++){
img [i][j] = 100;
}
for (j=(c+width); j<(c+width+1); j++){
img [i][j] = b;
}
for (j=(c+width+1); j<100; j++) {
img [i][j] = 100; }
}
for (i=(r+height); i<(r+height+1) ;i++){ // Draws bottom outline
for (j=0; j<c; j++){
img [i][j] = 100;
}
for (j=c; j<(c+width+1); j++){
img [i][j] = b;
}
for (j=(c+width+1); j<100; j++){
img [i][j] = 100;
}
}
for (i=(r+height+1); i<100; i++){ // Fills pixels from bottom outline to end of image with white
for (j=0; j<100; j++){
img [i][j] = 100;
}
}
}
// DRAWS FILLED BOX WITH BRIGHTNESS B
void box_filled (int b, int r, int c, int width, int height){
int i, j;
for (i=0; i<r; i++){ //Fills in rows 0 to r with white
for (j=0; j<100; j++){
img [i][j] = 100;
}
}
for (int i=r; i < (r+height); i++){ // Fills in box with brightness b
for (j=0; j<c; j++){
img [i][j] = 100;
}
for (j=c; j < (c+width); j++){
img [i][j] = b;
}
for (j = (c+width); j<100; j++){
img [i][j] = 100;
}
}
for (i=(r+height); i<100; i++){ // fills in rest of rows with white
for (j=0; j<100; j++){
img [i][j] = 100;
}
}
}
// BLURS IMAGE
void blur (){
int i, j;
image c = copy(); //Creates copy of image
for (i=0; i<1; i++){ // Averages (blurs) first row
for (j=0; j<1; j++){
img [i][j] = (c.img[i][j] + c.img[i][j+1] +
c.img[i+1][j] + c.img[i+1][j+1]) / 4;
}
for (j=1; j<99; j++){
img [i][j] = (c.img[i][j-1] + c.img[i][j] + c.img[i][j+1] +
c.img[i+1][j-1] + c.img[i+1][j] + c.img[i+1][j+1]) / 6;
}
for(j=99; j<100; j++){
img [i][j] = (c.img[i][j-1] + c.img[i][j] +
c.img[i+1][j-1] + c.img[i+1][j]) / 4;
}
}
for (i=1; i<99; i++){ // Averages (blurs) image except first and last rows
for (j=0; j<1; j++){
img [i][j] = (c.img[i-1][j] + c.img[i-1][j+1] +
c.img[i][j] + c.img[i][j+1] +
c.img[i+1][j] + c.img[i+1][j+1]) / 6;
}
for (j=1; j<99; j++){
img [i][j] = (c.img[i-1][j-1] + c.img[i-1][j] + c.img[i-1][j+1] +
c.img[i][j-1] + c.img[i][j] + c.img[i][j+1] +
c.img[i+1][j-1] + c.img[i+1][j] + c.img[i+1][j+1]) / 9;
}
for(j=99; j<100; j++){
img [i][j] = (c.img[i-1][j-1] + c.img[i-1][j] +
c.img[i][j-1] + c.img[i][j] +
c.img[i+1][j-1] + c.img[i+1][j]) / 6;
}
}
for (i=99; i<100; i++){ //Blurs last row
for (j=0; j<1; j++){
img [i][j] = (c.img[i-1][j] + c.img[i-1][j+1] +
c.img[i][j] + c.img[i][j+1]) / 4;
}
for (j=1; j<99; j++){
img [i][j] = (c.img[i-1][j-1] + c.img[i-1][j] + c.img[i-1][j+1] +
c.img[i][j-1] + c.img[i][j] + c.img[i][j+1]) / 6;
}
for(j=99; j<100; j++){
img [i][j] = (c.img[i-1][j-1] + c.img[i-1][j] +
c.img[i][j-1] + c.img[i][j]) / 4;
}
}
}
// READS IMAGE FROM FILE
void read(){
ifstream ins;
ins.open("image_in.pgm");
string type;
ins >> type; // Gets file type
// Checks to see if file is PGM format
if (type == "P2") {
int height, width, max;
ins >> height >> width >> max; // Gets other metadata from file
int val, i, j;
for (i=0; i<width; i++){
for (j=0; j<height; j++){
ins >> val;
img [i][j] = val; // Reads image from file into array
}
}
}
else {
cout << "ERROR: Unreadable file." << endl;
}
ins.close();
}
// WRITES IMAGE TO FILE
void write(string name){
ofstream ons;
ons.open (name.c_str());
string type = "P2";
int height = 100;
int width = 100;
int max = 100;
// Writes metadata to file
ons << type << endl << height << endl << width << endl << max << endl;
for (int i=0; i<width; i++){
for (int j=0; j<height; j++){
ons << img [i][j] << endl; // Writes image to file
}
}
ons.close();
}
};
//DEMONSTRATING MEMBER FUNCTIONS
int main () {
image white;
white.read(); // Reads contents of image_in.pgm (an all white image) and saves to array
//Copies image_in.pgm and saves as new file
image whitecopy = white.copy();
whitecopy.write("image_copy.pgm");
//Sets all pixels to brightness 10 and saves as new file
white.fill(50);
white.write("img_0.pgm");
//Inverts original image and saves as new file
white.read();
white.invert();
white.write("img_invert.pgm");
//Creates & saves new image file with filled black 50x50 box in the middle of the image
image box1;
box1.box_filled(0, 25, 25, 50, 50);
box1.write("img_boxfilled.pgm");
//Creates & saves new image file with white 50x50 box outlined in black in the middle of the image
image box2;
box2.box_outline(0, 25, 25,50,50);
box2.write("boxoutline.pgm");
//Blurs outlined box image and saves as new file
box2.blur();
box2.write("boxoutline_blur.pgm");
return 0;
}