-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavg_lib.cpp
More file actions
58 lines (50 loc) · 1.33 KB
/
avg_lib.cpp
File metadata and controls
58 lines (50 loc) · 1.33 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
#include "EasyBMP_1.06/EasyBMP.h"
#include <iostream>
#include <iomanip>
#include <boost/filesystem.hpp>
#include <fstream>
using namespace std;
using namespace boost::filesystem;
RGBApixel getAveragePixel(BMP img, int start_x, int start_y, int width, int height){
int r_sum = 0;
int g_sum = 0;
int b_sum = 0;;
int pixels = height * width;
for(int x = start_x; x < start_x+width; x++){
for(int y = start_y; y < start_y + height; y++){
RGBApixel p = img.GetPixel(x,y);
r_sum += p.Red;
g_sum += p.Green;
b_sum += p.Blue;
}
}
int r_avg = r_sum / pixels;
int g_avg = g_sum / pixels;
int b_avg = b_sum / pixels;
RGBApixel p;
p.Red = r_avg;
p.Green = g_avg;
p.Blue = b_avg;
return p;
}
RGBApixel getAveragePixel(BMP img){
return getAveragePixel(img, 0, 0, img.TellWidth(), img.TellHeight());
}
int main(int argc, char * argv[]){
if(argc != 2){
cout << "Usage: ./avg_lib <image library path>" << endl;
return 1;
}
BMP img;
path p(argv[1]);
std::ofstream fp;
fp.open("library-averages.txt");
for (auto i = directory_iterator(p); i != directory_iterator(); i++){
BMP img;
string path_str = i->path().string();
img.ReadFromFile(path_str.c_str());
RGBApixel p = getAveragePixel(img);
fp << path_str << " " << (int) p.Red << " " << (int) p.Green << " " << (int) p.Blue << endl;
}
fp.close();
}