-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·342 lines (280 loc) · 6.34 KB
/
main.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
//Image Manipulation Skeleton Code
//
//
// main.c
// original by Wagner Correa, 1999
// modified by Robert Osada, 2000
// modified by Renato Werneck, 2003
// mmodified by Stephen J. Guy, 2010-2017
//This project uses the EasyBMP Cross-Platform Windows Bitmap Library
//Web page: http://easybmp.sourceforge.net
#include "image.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define STB_IMAGE_IMPLEMENTATION //only place once in one .cpp file
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION //only place once in one .cpp files
#include "stb_image_write.h"
using namespace std;
/**
* prototypes
**/
static void ShowUsage(void);
static void CheckOption(char *option, int argc, int minargc);
int main( int argc, char* argv[] ){
Image *img = NULL;
bool did_output = false;
// first argument is program name
argv++, argc--;
// look for help
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-help")) {
ShowUsage();
}
}
// no argument case
if (argc == 0) {
ShowUsage();
}
// parse arguments
while (argc > 0)
{
if (**argv == '-')
{
if (!strcmp(*argv, "-input"))
{
CheckOption(*argv, argc, 2);
if (img != NULL)
delete img;
img = new Image(argv[1]);
argv += 2, argc -= 2;
}
else if (!strcmp(*argv, "-output"))
{
CheckOption(*argv, argc, 2);
if (img == NULL) ShowUsage();
img->Write(argv[1]);
did_output = true;
argv += 2, argc -= 2;
}
else if (!strcmp(*argv, "-noise"))
{
double factor;
CheckOption(*argv, argc, 2);
if (img == NULL) ShowUsage();
factor = atof(argv[1]);
img->AddNoise(factor);
argv += 2, argc -= 2;
}
else if (!strcmp(*argv, "-brightness"))
{
double factor;
CheckOption(*argv, argc, 2);
if (img == NULL) ShowUsage();
factor = atof(argv[1]);
img->Brighten(factor);
argv += 2, argc -=2;
}
else if (!strcmp(*argv, "-contrast"))
{
double factor;
CheckOption(*argv, argc, 2);
if (img == NULL) ShowUsage();
factor = atof(argv[1]);
img->ChangeContrast(factor);
argv += 2, argc -= 2;
}
else if (!strcmp(*argv, "-saturation"))
{
double factor;
CheckOption(*argv, argc, 2);
if (img == NULL) ShowUsage();
factor = atof(argv[1]);
img->ChangeSaturation(factor);
argv += 2, argc -= 2;
}
else if (!strcmp(*argv, "-crop"))
{
int x, y, w, h;
CheckOption(*argv, argc, 5);
if (img == NULL) ShowUsage();
x = atoi(argv[1]);
y = atoi(argv[2]);
w = atoi(argv[3]);
h = atoi(argv[4]);
Image *dst = img->Crop(x, y, w, h);
delete img;
img = dst;
argv += 5, argc -= 5;
}
else if (!strcmp(*argv, "-extractChannel"))
{
int channel;
CheckOption(*argv, argc, 2);
if (img == NULL) ShowUsage();
channel = atoi(argv[1]);
img->ExtractChannel(channel);
argv += 2, argc -= 2;
}
else if (!strcmp(*argv, "-quantize"))
{
int nbits;
CheckOption(*argv, argc, 2);
if (img == NULL) ShowUsage();
nbits = atoi(argv[1]);
img->Quantize(nbits);
argv += 2, argc -= 2;
}
else if (!strcmp(*argv, "-randomDither"))
{
int nbits;
CheckOption(*argv, argc, 2);
if (img == NULL) ShowUsage();
nbits = atoi(argv[1]);
img->RandomDither(nbits);
argv += 2, argc -= 2;
}
else if (!strcmp(*argv, "-blur"))
{
int n;
CheckOption(*argv, argc, 2);
if (img == NULL) ShowUsage();
n = atoi(argv[1]);
img->Blur(n);
argv += 2, argc -= 2;
}
else if (!strcmp(*argv, "-sharpen"))
{
int n;
CheckOption(*argv, argc, 2);
if (img == NULL) ShowUsage();
n = atoi(argv[1]);
img->Sharpen(n);
argv += 2, argc -= 2;
}
else if (!strcmp(*argv, "-edgeDetect"))
{
if (img == NULL) ShowUsage();
img->EdgeDetect();
argv++, argc--;
}
else if (!strcmp(*argv, "-orderedDither"))
{
int nbits;
CheckOption(*argv, argc, 2);
if (img == NULL) ShowUsage();
nbits = atoi(argv[1]);
img->OrderedDither(nbits);
argv += 2, argc -= 2;
}
else if (!strcmp(*argv, "-FloydSteinbergDither"))
{
int nbits;
CheckOption(*argv, argc, 2);
if (img == NULL) ShowUsage();
nbits = atoi(argv[1]);
img->FloydSteinbergDither(nbits);
argv += 2, argc -= 2;
}
else if (!strcmp(*argv, "-scale"))
{
CheckOption(*argv, argc, 3);
if (img == NULL) ShowUsage();
double sx = atof(argv[1]);
double sy = atof(argv[2]);
Image *dst = img->Scale(sx, sy);
delete img;
img = dst;
argv += 3, argc -= 3;
}
else if (!strcmp(*argv, "-rotate"))
{
double angle;
Image *dst;
CheckOption(*argv, argc, 2);
if (img == NULL) ShowUsage();
angle = atof(argv[1]);
dst = img->Rotate(angle);
delete img;
img = dst;
dst = NULL;
argv += 2, argc -= 2;
}
else if (!strcmp(*argv, "-fun"))
{
if (img == NULL) ShowUsage();
img->Fun();
argv++, argc--;
}
else if (!strcmp(*argv, "-sampling"))
{
if (img == NULL) ShowUsage();
int method;
CheckOption(*argv, argc, 2);
method = atoi(argv[1]);
img->SetSamplingMethod(method);
argv += 2, argc -= 2;
}
else
{
fprintf(stderr, "image: invalid option: %s\n", *argv);
ShowUsage();
}
}
else
{
fprintf(stderr, "image: invalid option: %s\n", *argv);
ShowUsage();
}
}
if (!did_output)
{
fprintf( stderr, "Warning, you didn't tell me to output anything. I hope that's OK.\n" );
}
delete img;
return EXIT_SUCCESS;
}
/**
* ShowUsage
**/
static char options[] =
"-help\n"
"-input <file>\n"
"-output <file>\n"
"-noise <factor>\n"
"-brightness <factor>\n"
"-contrast <factor>\n"
"-saturation <factor>\n"
"-crop <x> <y> <width> <height>\n"
"-extractChannel <channel no>\n"
"-quantize <nbits>\n"
"-randomDither <nbits>\n"
"-blur <maskSize>\n"
"-sharpen <maskSize>\n"
"-edgeDetect\n"
"-orderedDither <nbits>\n"
"-FloydSteinbergDither <nbits>\n"
"-scale <sx> <sy>\n"
"-rotate <angle>\n"
"-fun\n"
"-sampling <method no>\n"
;
static void ShowUsage(void)
{
fprintf(stderr, "Usage: image -input <filename> [-option [arg ...] ...] -output <filename>\n");
fprintf(stderr, "%s", options);
exit(EXIT_FAILURE);
}
/**
* CheckOption
**/
static void CheckOption(char *option, int argc, int minargc)
{
if (argc < minargc)
{
fprintf(stderr, "Too few arguments for %s\n", option);
ShowUsage();
}
}