-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.cpp
362 lines (275 loc) · 11.1 KB
/
Image.cpp
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#pragma warning(disable:4996)
#include<iostream>
#include<cctype>
#include<cstdbool>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include "Image.h"
#include "Logger.h"
// Default constructor.
Image::Image()
{
strcpy(this->PGMType, "P2");
this->Data=nullptr;
this->Width=0;
this->Height=0;
this->MaximumGreyValue=0;
}
// Function to return the width of the image.
int Image::getWidth()
{
return this->Width;
}
// Function to return the height of the image.
int Image::getHeight()
{
return this->Height;
}
// Function to return the maximum value of the pixels in the image.
int Image::getMaximumGreyValue()
{
return this->MaximumGreyValue;
}
// Function to ignore any comments (characters that start with '#') in the file.
void Image::ignoreComments(FILE* FilePath)
{
logger("Ignoring any comments in the file...");
int Character;
char Line[100];
while((Character=fgetc(FilePath))!=EOF && isspace(Character)); // Ignoring any white-spaces.
// Recursively ignoring any comments (characters that start with '#') in the file.
if(Character=='#')
{
fgets(Line, sizeof(Line), FilePath);
ignoreComments(FilePath);
}
else
{
fseek(FilePath, -1, SEEK_CUR);
}
}
bool Image::readPGMFile(Image* PGM, const char* FileName)
{
logger("Reading the PGM file...");
FILE* PGMFile=fopen(FileName, "rb"); // Reading the file in a binary format.
if(PGMFile==nullptr) // Condition to check whether the file exists or not.
{
std::cout << "File does not exist!" << std::endl;
logger("File does not exist!");
return false;
}
ignoreComments(PGMFile); // Ignoring any comments (characters that start with '#') in the file.
fscanf(PGMFile, "%s", PGM->PGMType); // Reading the PGM type from the file, and storing it in the respective variable.
logger("Checking whether the file is of the correct PGM binary file type...");
if(strcmp(PGM->PGMType, "P5")) // Condition to check whether the file is of the correct PGM binary file type or not.
{
std::cout << "Wrong file type!" << std::endl;
logger("Wrong file type!");
exit(EXIT_FAILURE);
}
ignoreComments(PGMFile); // Ignoring any comments (characters that start with '#') in the file.
fscanf(PGMFile, "%d %d", &(PGM->Width), &(PGM->Height)); // Reading the width and height of the image from the file, and storing them in the respective variables.
ignoreComments(PGMFile); // Ignoring any comments (characters that start with '#') in the file.
fscanf(PGMFile, "%d", &(PGM->MaximumGreyValue)); // Reading the maximum gray value of the image from the file, and storing it in the respective variable.
ignoreComments(PGMFile); // Ignoring any comments (characters that start with '#') in the file.
PGM->Data=(unsigned char**)malloc(PGM->Height*sizeof(unsigned char*)); // Allocating memory to store the image information in the defined class.
// Storing the pixel information.
logger("Storing the pixel information...");
if(PGM->PGMType[1]=='5')
{
fgetc(PGMFile);
for(unsigned int i=0; i<PGM->Height; i++)
{
PGM->Data[i]=(unsigned char*)malloc(PGM->Width*sizeof(unsigned char)); // Allocating memory to store the pixel information in the defined class.
if(PGM->Data[i]==nullptr) // Condition to check whether the memory allocation was successful or not.
{
std::cout << "Memory allocation failed!" << std::endl;
logger("Memory allocation failed!");
exit(1);
}
fread(PGM->Data[i], sizeof(unsigned char), PGM->Width, PGMFile); // Reading the grey values, and storing on the allocated memory.
}
}
fclose(PGMFile); // Closing the file.
return true;
}
// Function to save/display the image details.
void Image::saveImageDetails(Image* PGM, const char* FileName)
{
logger("Saving the image details...");
FILE* PGMFile=fopen(FileName, "rb"); // Reading the file in a binary format.
const char* FileExtension=strrchr(FileName, '.'); // Storing the file extension.
logger("Displaying the image details...");
if(!FileExtension)
{
std::cout << "File extension not found in " << FileName << std::endl;
logger("File extension not found!");
}
else
{
std::cout << "File format:\t" << FileExtension+1 << std::endl;
}
std::cout << "PGM File Type:\t" << PGM->PGMType << std::endl;
// Printing the PGM file type in ASCII and binary format.
if(!strcmp(PGM->PGMType, "P2"))
{
std::cout << "PGM File Type:\tASCII" << std::endl;
}
else if(!strcmp(PGM->PGMType, "P5"))
{
std::cout << "PGM File Type:\tBinary" << std::endl;
}
else
{
std::cout << "PGM File Type:\tUnknown" << std::endl;
}
std::cout << "Width:\t" << PGM->Width << " Pixels" << std::endl;
std::cout << "Height:\t" << PGM->Height << " Pixels" << std::endl;
std::cout << "Maximum Grey Value:\t" << PGM->MaximumGreyValue << std::endl;
fclose(PGMFile); // Closing the file.
}
// Function to retrieve the pixel information.
int Image::getPixel(unsigned int Row, unsigned int Column)
{
logger("Retrieving the pixel information...");
if(Row>this->Height || Column>this->Width || Row<this->Height || Column<this->Width) // Condition to check whether the pixel information is within the image dimensions or not.
{
std::cout << "Invalid pixel location!" << std::endl;
logger("Invalid pixel location!");
exit(EXIT_FAILURE);
}
return this->Data[Row][Column];
}
// Function to set the pixel information.
void Image::setPixel(unsigned int Row, unsigned int Column, unsigned int Value)
{
logger("Setting the pixel information...");
if(Value>255 || Value<0) // Condition to check whether the pixel information is within the maximum grey value or not.
{
std::cout << "Pixel value out of range!" << std::endl;
logger("Pixel value out of range!");
exit(EXIT_FAILURE);
}
if(Row>this->Height || Column>this->Width || Row<this->Height || Column<this->Width) // Condition to check whether the pixel information is within the image dimensions or not.
{
std::cout << "Invalid pixel location!" << std::endl;
logger("Invalid pixel location!");
exit(EXIT_FAILURE);
}
this->Data[Row][Column]=Value;
}
// Function to return the image size.
int Image::getSize()
{
logger("Retrieving the image size...");
return (this->Height*this->Width);
}
// Function to convert the image to negative.
void Image::convertToNegative()
{
logger("Converting the image to negative...");
for(unsigned int i=0; i<this->Height; i++)
{
for(unsigned int j=0; j<this->Width; j++)
{
// Inverting the values of all the black and white pixels.
if(this->Data[i][j]==0)
{
this->Data[i][j]=255;
}
else
{
this->Data[i][j]=0;
}
}
}
}
// Function to calculate and return the mean pixel value of the image.
int Image::meanPixelValue()
{
int MeanPixelValue=0;
logger("Calculating the mean pixel value...");
for(unsigned int i=0; i<this->Height; i++)
{
for(unsigned int j=0; j<this->Width; j++)
{
MeanPixelValue+=this->Data[i][j];
}
}
MeanPixelValue/=this->Height*this->Width; // Calculating the mean pixel value.
return MeanPixelValue;
}
// Function to calculate and return the number of black pixels in the image.
int Image::numberOfBlackPixels()
{
int NumberOfBlackPixels=0;
logger("Calculating the number of black pixels...");
for(unsigned int i=0; i<this->Height; i++)
{
for(unsigned int j=0; j<this->Width; j++)
{
if(this->Data[i][j]==0) // Condition to check whether the pixel is black or not.
{
NumberOfBlackPixels++;
}
}
}
return NumberOfBlackPixels;
}
// Function to calculate and return the number of white pixels in the image.
int Image::numberOfWhitePixels()
{
int NumberOfWhitePixels=0;
logger("Calculating the number of white pixels...");
for(unsigned int i=0; i<this->Height; i++)
{
for(unsigned int j=0; j<this->Width; j++)
{
if(this->Data[i][j]==this->MaximumGreyValue) // Condition to check whether the pixel is white or not.
{
NumberOfWhitePixels++;
}
}
}
return NumberOfWhitePixels;
}
// Function to calculate and return the average number of black pixels in each row of the image.
int Image::averageBlackPixelsInRow()
{
int AverageBlackPixelsInRow=0;
logger("Calculating the average number of black pixels in a row...");
for(unsigned int i=0; i<this->Height; i++)
{
for(unsigned int j=0; j<this->Width; j++)
{
if(this->Data[i][j]==0)
{
AverageBlackPixelsInRow++;
}
}
}
AverageBlackPixelsInRow/=this->Height; // Calculating the average number of black pixels in a row.
return AverageBlackPixelsInRow;
}
void Image::displayImageDetails()
{
logger("Displaying the image details...");
std::cout << "PGM File Type:\t" << this->PGMType << std::endl;
// Printing the PGM file type in ASCII and binary format.
if(!strcmp(this->PGMType, "P2"))
{
std::cout << "PGM File Type:\tASCII" << std::endl;
}
else if(!strcmp(this->PGMType, "P5"))
{
std::cout << "PGM File Type:\tBinary" << std::endl;
}
else
{
std::cout << "PGM File Type:\tUnknown" << std::endl;
}
std::cout << "Width:\t" << this->Width << " Pixels" << std::endl;
std::cout << "Height:\t" << this->Height << " Pixels" << std::endl;
std::cout << "Maximum Grey Value:\t" << this->MaximumGreyValue << std::endl;
}