-
Notifications
You must be signed in to change notification settings - Fork 0
/
hiscolor.h
162 lines (137 loc) · 3.63 KB
/
hiscolor.h
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
#ifndef HISCOLOR_H
#define HISCOLOR_H
#include "misc.h"
#include <set>
#include <list>
#include <cstdio>
class hiscolor
{
public:
rgb color;
int frequency;
public:
hiscolor(rgb color, int frequency = 1)
{
this->color = color;
this->frequency = frequency;
}
hiscolor()
{
this->frequency = 0;
color.r = color.g = color.b = 0;
}
rgb getcolor() const
{
return color;
}
int get() const
{
return frequency;
}
void set(int frequency)
{
this->frequency = frequency;
}
void add()
{
frequency++;
}
void decrease()
{
frequency--;
}
friend inline bool operator<(const hiscolor &a, const hiscolor &b);
};
inline bool operator<(const hiscolor &a, const hiscolor &b)
{
return (a.color.r < b.color.r) || ((a.color.r == b.color.r) && (a.color.g < b.color.g)) || ((a.color.r == b.color.r)
&& (a.color.g == b.color.g) && (a.color.b < b.color.b));
}
class weightcolor
{
public:
hiscolor color;
weightcolor(hiscolor color)
{
this->color = color;
}
friend inline bool operator<(const weightcolor &a, const weightcolor &b);
};
inline bool operator<(const weightcolor &a, const weightcolor &b)
{
return (a.color.frequency < b.color.frequency) || (a.color < b.color);
}
void get_histogram(std::set<hiscolor> &histogram, image<rgb>* im , float scale = 0.95)
{
int width = im->width();
int height = im->height();
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
hiscolor color(imRef(im, x, y));
if(histogram.count(color) == 0)
histogram.insert(color);
else
{
std::set<hiscolor>::iterator it = histogram.find(color);
int frequency = it->get() + 1;
histogram.erase(it);
color.set(frequency);
histogram.insert(color);
}
}
std::set<weightcolor> sorthistogram;
for(std::set<hiscolor>::iterator it = histogram.begin(); it != histogram.end(); it++)
{
sorthistogram.insert(weightcolor(*it));
}
int totalweight = width * height * (1 - scale);
int currentweight = 0;
for(std::set<weightcolor>::iterator it = sorthistogram.begin(); it != sorthistogram.end(); it++)
{
currentweight += it->color.frequency;
std::set<hiscolor>::iterator iter = histogram.find(it->color);
histogram.erase(iter);
if(currentweight > totalweight)
break;
}
}
void get_histogram(std::set<hiscolor> &histogram, std::list<int> clist, image<rgb>* im, float scale = 0.95)
{
int width = im->width();
int height = im->height();
int size = 0;
for(std::list<int>::iterator iter = clist.begin(); iter != clist.end();iter++)
{
size++;
int y = (*iter) / width;
int x = (*iter) - y * width;
hiscolor color(imRef(im, x, y));
if(histogram.count(color) == 0)
histogram.insert(color);
else
{
std::set<hiscolor>::iterator it = histogram.find(color);
int frequency = it->get() + 1;
histogram.erase(it);
color.set(frequency);
histogram.insert(color);
}
}
std::set<weightcolor> sorthistogram;
for(std::set<hiscolor>::iterator it = histogram.begin(); it != histogram.end(); it++)
{
sorthistogram.insert(weightcolor(*it));
}
int totalweight = size * (1 - scale);
int currentweight = 0;
for(std::set<weightcolor>::iterator it = sorthistogram.begin(); it != sorthistogram.end(); it++)
{
currentweight += it->color.frequency;
std::set<hiscolor>::iterator iter = histogram.find(it->color);
histogram.erase(iter);
if(currentweight > totalweight)
break;
}
}
#endif