-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelperFunctions.cpp
282 lines (232 loc) · 6.16 KB
/
HelperFunctions.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
#include "HelperFunctions.h"
#include "ImageConverter.h"
#include "Header.h"
bool obtainFileStatus(const string & name)
{
ifstream fin(name);
return fin.good();
}
void validateFilename(string & name)
{
string firstExtension = "bmp";
string secondExtension = "karol";
int firstExtensionLength = firstExtension.length();
int secondExtensionLength = secondExtension.length();
displayHint();
displayInputRequest();
do
{
getline(cin, name);
// '.' not found
if (name.find('.') == string::npos)
{
name.append(".").append(firstExtension);
bool isFileExisiting = obtainFileStatus(name);
if (isFileExisiting)
{
break;
}
else
{
name.erase(name.length() - firstExtensionLength);
name.append(secondExtension);
isFileExisiting = obtainFileStatus(name);
if (isFileExisiting)
{
break;
}
}
}
else
{
bool isFileExisiting = obtainFileStatus(name);
if (isFileExisiting)
{
break;
}
}
displayHint();
displayInputRequestOnFailure();
} while (true);
cout << endl;
}
void choosePalette(char & selection)
{
cout << "Choose a color palette of the converted image." << endl;
cout << "Please enter one of the following choices:" << endl;
cout << "1) Predefined Color Palette 2) Algorithmically-chosen Color Palette" << endl;
cout << "3) Grayscale" << endl;
while (cin >> selection && selection != '1' && selection != '2' && selection != '3')
{
cout << "Please enter a number '1', '2' or '3': ";
cin.ignore(INT_MAX, '\n');
}
cin.ignore(INT_MAX, '\n');
cout << endl;
}
void chooseSaveOption(char & selection, bool & isAveragePredictorChosen)
{
cout << "Choose an extension of this image which you want to convert to." << endl;
cout << "Please enter one of the following choices:" << endl;
cout << "1) .bmp 2) .karol" << endl;
cout << "3) don't save at all" << endl;
while (cin >> selection && selection != '1' && selection != '2' && selection != '3')
{
cout << "Please enter a number '1', '2' or '3': ";
cin.ignore(INT_MAX, '\n');
}
if (selection == '2')
{
char secondSelection;
cout << endl << "Choose whether you want an Average Predictor to be used." << endl;
cout << "Please enter one of the following choices:" << endl;
cout << "1) yes 2) no" << endl;
while (cin >> secondSelection && secondSelection != '1' && secondSelection != '2')
{
cout << "Please enter a number '1' or '2'";
cin.ignore(INT_MAX, '\n');
}
if (secondSelection == '1')
{
isAveragePredictorChosen = true;
}
}
cin.ignore(INT_MAX, '\n');
cout << endl;
}
void chooseDithering(bool & isDitheringChosen)
{
char selection;
cout << "Choose whether you want a Dithering to be used." << endl;
cout << "Please enter one of the following choices:" << endl;
cout << "1) yes 2) no" << endl;
while (cin >> selection && selection != '1' && selection != '2')
{
cout << "Please enter a number '1' or '2'";
cin.ignore(INT_MAX, '\n');
}
if (selection == '1')
{
isDitheringChosen = true;
}
cin.ignore(INT_MAX, '\n');
cout << endl;
}
void displayHint()
{
system("cls");
cout << "- Now you will be asked to enter a name of a file to be processed. " << endl
<< "- If there are two files sharing the same filename and one of them has .bmp and another .karol extension" << endl
<< " then the file with .bmp extension will be chosen." << endl
<< "- If you want to override this behaviour you have to explicitly type filename with desired extension." << endl << endl;
}
void displayInputRequest()
{
cout << "Please enter the name of the image to be processed: ";
}
void displayInputRequestOnFailure()
{
cout << "Enter the proper name of the file!" << endl;
}
void displayFurtherInstructions()
{
cout << "While having your SDL window active you can decide which operation will be performed." << endl
<< "- To process another image press 'N' on your keyboard." << endl
<< "- To end this program press 'ESC' on your keyboard." << endl;
}
double ** create_two_dimensional_array(int rows, int columns)
{
double ** array = nullptr;
try
{
array = new double *[rows];
}
catch (bad_alloc & ba)
{
cout << "Error! (couldn't allocate the dynamic array)" << endl;
// displays the exact cause
cout << ba.what() << endl;
}
for (int sentinel = 0; sentinel < rows; ++sentinel)
{
try
{
*(array + sentinel) = new double[columns];
}
catch (bad_alloc & ba)
{
cout << "Error! (couldn't allocate the dynamic array)" << endl;
// displays the exact cause
cout << ba.what() << endl;
}
}
return array;
}
SDL_Color ** create_two_dimensional_SDL_array(int rows, int columns)
{
SDL_Color ** array = nullptr;
try
{
array = new SDL_Color *[rows];
}
catch (bad_alloc & ba)
{
cout << "Error! (couldn't allocate the dynamic array)" << endl;
// displays the exact cause
cout << ba.what() << endl;
}
for (int sentinel = 0; sentinel < rows; ++sentinel)
{
try
{
*(array + sentinel) = new SDL_Color[columns];
}
catch (bad_alloc & ba)
{
cout << "Error! (couldn't allocate the dynamic array)" << endl;
// displays the exact cause
cout << ba.what() << endl;
}
}
return array;
}
void fill_two_dimensional_array_with_value(double ** array, int rows, int columns, int value)
{
for (int sentinel = 0; sentinel < rows; ++sentinel)
{
for (int secondSentinel = 0; secondSentinel < columns; ++secondSentinel)
{
array[sentinel][secondSentinel] = value;
}
}
}
void delete_two_dimensional_array(double ** array, int columns)
{
for (int sentinel = 0; sentinel < columns; ++sentinel)
{
delete[] array[sentinel];
}
delete[] array;
}
void display_two_dimensional_array_rows_in_single_line_negative_numbers(double ** array, int rows, int columns)
{
for (int sentinel = 0; sentinel < rows; ++sentinel)
{
for (int secondSentinel = 0; secondSentinel < columns; ++secondSentinel)
{
cout.precision(2);
cout << fixed << array[sentinel][secondSentinel] << " ";
}
cout << endl;
}
}
void copyCurrentSDLWindow(ImageConverter & converter, SDL_Color ** inputImage, int width, int height)
{
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
inputImage[x][y] = converter.getPixel(x, y);
}
}
}