-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrayscale.cpp
More file actions
32 lines (27 loc) · 872 Bytes
/
grayscale.cpp
File metadata and controls
32 lines (27 loc) · 872 Bytes
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
#include "picture.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
if (argc < 2) {
cout << "Usage: grayscale infile.bmp [outfile.bmp]" << endl;
return 1;
}
// open the file and construct a Picture object
Picture pic(argv[1]);
// convert the colors to grayscale
for (int row=0; row<pic.height(); row++)
for (int col=0; col<pic.width(); col++) {
Color color = pic.get(row, col);
double luminance = 0.299*color.red
+ 0.587*color.green + 0.114*color.blue;
color.red = (int)luminance;
color.blue = (int)luminance;
color.green = (int)luminance;
pic.set(row, col, color);
}
// show the new picture
pic.show();
// save it, if the optional command-line argument was provided
if (argc > 2)
pic.save(argv[2]);
}