forked from kornelski/pngquant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nearest.c
190 lines (154 loc) · 5.84 KB
/
nearest.c
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
#include "pam.h"
#include "nearest.h"
#include "mempool.h"
#include <stdlib.h>
struct color_entry {
f_pixel color;
float radius;
unsigned int index;
};
struct sorttmp {
float radius;
unsigned int index;
};
struct head {
f_pixel center;
float radius;
unsigned int num_candidates;
struct color_entry *candidates;
};
struct nearest_map {
struct head *heads;
mempool mempool;
unsigned int num_heads;
};
static int compareradius(const void *ap, const void *bp)
{
float a = ((const struct sorttmp*)ap)->radius;
float b = ((const struct sorttmp*)bp)->radius;
return a > b ? 1 : (a < b ? -1 : 0);
}
static struct head build_head(f_pixel px, const colormap *map, int num_candidates, mempool *m, unsigned int skip_index[], unsigned int *skipped)
{
struct sorttmp colors[map->colors];
unsigned int colorsused=0;
for(unsigned int i=0; i < map->colors; i++) {
if (skip_index[i]) continue;
colors[colorsused].index = i;
colors[colorsused].radius = colordifference(px, map->palette[i].acolor);
colorsused++;
}
qsort(&colors, colorsused, sizeof(colors[0]), compareradius);
assert(colorsused < 2 || colors[0].radius <= colors[1].radius);
num_candidates = MIN(colorsused, num_candidates);
struct head h;
h.candidates = mempool_new(m, num_candidates * sizeof(h.candidates[0]));
h.center = px;
h.num_candidates = num_candidates;
for(unsigned int i=0; i < num_candidates; i++) {
h.candidates[i] = (struct color_entry) {
.color = map->palette[colors[i].index].acolor,
.index = colors[i].index,
.radius = colors[i].radius,
};
}
h.radius = colors[num_candidates-1].radius/4.0f; // /2 squared
for(unsigned int i=0; i < num_candidates; i++) {
assert(colors[i].radius <= h.radius*4.0f);
// divide again as that's matching certain subset within radius-limited subset
// - 1/256 is a tolerance for miscalculation (seems like colordifference isn't exact)
if (colors[i].radius < h.radius/4.f - 1.f/256.f) {
skip_index[colors[i].index]=1;
(*skipped)++;
}
}
return h;
}
static colormap *get_subset_palette(const colormap *map)
{
// it may happen that it gets palette without subset palette or the subset is too large
int subset_size = (map->colors+3)/4;
if (map->subset_palette && map->subset_palette->colors <= subset_size) {
return map->subset_palette;
}
const colormap *source = map->subset_palette ? map->subset_palette : map;
colormap *subset_palette = pam_colormap(subset_size);
for(unsigned int i=0; i < subset_size; i++) {
subset_palette->palette[i] = source->palette[i];
}
return subset_palette;
}
struct nearest_map *nearest_init(const colormap *map)
{
mempool m = NULL;
struct nearest_map *centroids = mempool_new(&m, sizeof(*centroids));
centroids->mempool = m;
unsigned int skipped=0;
unsigned int skip_index[map->colors]; for(unsigned int j=0; j < map->colors; j++) skip_index[j]=0;
colormap *subset_palette = get_subset_palette(map);
const int selected_heads = subset_palette->colors;
centroids->heads = mempool_new(¢roids->mempool, sizeof(centroids->heads[0])*(selected_heads+1)); // +1 is fallback head
unsigned int h=0;
for(; h < selected_heads; h++)
{
unsigned int num_candiadtes = 1+(map->colors - skipped)/((1+selected_heads-h)/2);
centroids->heads[h] = build_head(subset_palette->palette[h].acolor, map, num_candiadtes, ¢roids->mempool, skip_index, &skipped);
if (centroids->heads[h].num_candidates == 0) {
break;
}
}
centroids->heads[h].radius = MAX_DIFF;
centroids->heads[h].center = (f_pixel){0,0,0,0};
centroids->heads[h].num_candidates = 0;
centroids->heads[h].candidates = mempool_new(¢roids->mempool, (map->colors - skipped) * sizeof(centroids->heads[h].candidates[0]));
for(unsigned int i=0; i < map->colors; i++) {
if (skip_index[i]) continue;
centroids->heads[h].candidates[centroids->heads[h].num_candidates++] = (struct color_entry) {
.color = map->palette[i].acolor,
.index = i,
.radius = 999,
};
}
centroids->num_heads = ++h;
// get_subset_palette could have created a copy
if (subset_palette != map->subset_palette) {
pam_freecolormap(subset_palette);
}
return centroids;
}
unsigned int nearest_search(const struct nearest_map *centroids, const f_pixel px, const float min_opaque_val, float *diff)
{
const int iebug = px.a > min_opaque_val;
const struct head *const heads = centroids->heads;
for(unsigned int i=0; i < centroids->num_heads; i++) {
float headdist = colordifference(px, heads[i].center);
if (headdist <= heads[i].radius) {
assert(heads[i].num_candidates);
unsigned int ind=heads[i].candidates[0].index;
float dist = colordifference(px, heads[i].candidates[0].color);
/* penalty for making holes in IE */
if (iebug && heads[i].candidates[0].color.a < 1) {
dist += 1.f/1024.f;
}
for(unsigned int j=1; j < heads[i].num_candidates; j++) {
float newdist = colordifference(px, heads[i].candidates[j].color);
/* penalty for making holes in IE */
if (iebug && heads[i].candidates[j].color.a < 1) {
newdist += 1.f/1024.f;
}
if (newdist < dist) {
dist = newdist;
ind = heads[i].candidates[j].index;
}
}
if (diff) *diff = dist;
return ind;
}
}
assert(0);
return 0;
}
void nearest_free(struct nearest_map *centroids)
{
mempool_free(centroids->mempool);
}