-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMosaitImg.cpp
126 lines (112 loc) · 2.78 KB
/
MosaitImg.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
/**
* Mosait by Tangui Morlier
* Under GPLv3
*/
#include "MosaitImg.h"
#include "MosaitPixel.h"
#include <FreeImagePlus.h>
#include <vector>
MosaitImg::MosaitImg() : filename(), nb_used(0)
{}
MosaitImg::MosaitImg(string & file) : filename(file), nb_used(0)
{
fipImage image;
if (!image.load(file.c_str()))
throw string("Error while loading image");
init(image);
}
MosaitImg::MosaitImg(fipImage & image) : filename(), nb_used(0)
{
init(image);
}
MosaitImg::MosaitImg(int size_x, int size_y) :filename("randomized"), nb_used(0)
{
float r, g, b;
for(int x = 0; x < DEF_IMG_X ; x++)
for(int y = 0; y < DEF_IMG_Y ; y++)
{
r = int(rand() % 255);
g = int(rand() % 255);
b = int(rand() % 255);
MosaitPixel pixel(r/255.0, g/255.0, b/255.0);
pixels.push_back(pixel);
}
}
void MosaitImg::init(fipImage & image)
{
fipImage small = image;
small.rescale(DEF_IMG_X, DEF_IMG_Y, FILTER_BICUBIC);
small.convertToRGBF();
FIRGBF *px = (FIRGBF*) small.accessPixels();
int i = 0;
for(int x = 0; x < small.getWidth() ; x++)
for(int y = 0 ; y < small.getHeight() ; y++) {
MosaitPixel pixel(px[i].red,px[i].green,px[i].blue);
pixels.push_back(pixel);
// cerr << pixels.back() << " " << r << " " << g << " " << b << endl;
i++;
}
nb_used = 0;
distance = 0;
}
string MosaitImg::getFileName()
{
return filename;
}
/*float MosaitImg::getDistance() const
{
return distance;
}
*/
float MosaitImg::getDistanceWith(const MosaitImg & img, bool withlight) const
{
float res = 0;
int cpt;
for(cpt = 0; cpt < pixels.size() ; cpt++ )
{
res += pixels[cpt].getDistanceWith(img.pixels[cpt], withlight);
}
return res/cpt + (nb_used + img.nb_used) * MOSAITIMAGE_UNFAIRSCORE;
}
void MosaitImg::setDistanceWith(const MosaitImg & img, bool withlight)
{
distance = getDistanceWith(img, withlight);
}
bool MosaitImg::compareByDistance(const MosaitImg & img1, const MosaitImg & img2)
{
// cerr << img2.distance << " < " << img2.distance << endl;
return (img1.distance < img2.distance);
}
void MosaitImg::setAverage(const vector<MosaitImg> images)
{
if (!images.size())
return;
vector<MosaitPixel> uniq_pixels;
for(int i = 0 ; i < pixels.size() ; i++)
{
uniq_pixels.clear();
for(int y = 0 ; y < images.size(); y++)
{
uniq_pixels.push_back(images[y].pixels[i]);
}
pixels[i].setAverage(uniq_pixels);
}
}
std::ostream& operator<<(std::ostream& os, MosaitImg im)
{
os << im.getFileName() << " " << im.pixels.size() << " " ;
for (vector<MosaitPixel>::iterator iPixel = im.pixels.begin() ; iPixel < im.pixels.end() ; iPixel++)
os << *iPixel << " ";
return os;
}
istream & operator >>(istream &ins, MosaitImg & mi) {
int size = 0;
ins >> mi.filename >> size;
mi.pixels = vector<MosaitPixel>();
for (int i = 0 ; i < size ; i++)
{
MosaitPixel mp;
ins >> mp;
mi.pixels.push_back(mp);
}
}